public static void WriteDataset <T>(long locationId, string datasetName, T[] valueSet) { Contract.Requires(valueSet != null, nameof(valueSet)); long datasetId = -1; try { datasetId = H5D.open(locationId, datasetName); IOHelper.Write <T>(datasetId, valueSet, DataContainerType.Dataset); } finally { if (H5I.is_valid(datasetId) > 0) { H5D.close(datasetId); } } }
private void OpenFile(string filePath, DateTime startDateTime, IList <ChannelContext> channelContextSet) { long propertyId = -1; long datasetId = -1; long groupId = -1; bool isNew; try { _fileId = -1; // open file if (File.Exists(filePath)) { _fileId = H5F.open(filePath, H5F.ACC_RDWR, H5P.DEFAULT); if (_fileId < 0) { try { File.Copy(filePath, filePath + ".backup"); } catch { // } } } // create file if (_fileId < 0) { propertyId = H5P.create(H5P.FILE_CREATE); H5P.set_userblock(propertyId, USERBLOCK_SIZE); _fileId = H5F.create(filePath, H5F.ACC_TRUNC, propertyId); } if (_fileId < 0) { throw new Exception($"{ ErrorMessage.Mat73Writer_CouldNotOpenOrCreateFile } File: { filePath }."); } // prepare channels foreach (ChannelContext channelContext in channelContextSet) { var periodInSeconds = (ulong)Math.Round(_settings.FilePeriod.TotalSeconds, MidpointRounding.AwayFromZero); var samplesPerSecond = channelContext.ChannelDescription.SampleRate.SamplesPerSecond; (var chunkLength, var chunkCount) = GeneralHelper.CalculateChunkParameters(periodInSeconds, samplesPerSecond); this.PrepareChannel(_fileId, channelContext.ChannelDescription, chunkLength, chunkCount); } // info groupId = this.OpenOrCreateStruct(_fileId, "/info").GroupId; (datasetId, isNew) = this.OpenOrCreateChannel(groupId, "last_completed_chunk", 1, 1); if (isNew) { IOHelper.Write(datasetId, new double[] { -1 }, DataContainerType.Dataset); } // text entries _textEntrySet[2].Content = startDateTime.ToString("yyyy-MM-ddTHH-mm-ss"); this.PrepareAllTextEntries(_textEntrySet); } finally { if (H5I.is_valid(datasetId) > 0) { H5D.close(datasetId); } if (H5I.is_valid(propertyId) > 0) { H5P.close(propertyId); } if (H5I.is_valid(groupId) > 0) { H5G.close(groupId); } H5F.flush(_fileId, H5F.scope_t.GLOBAL); // write preamble if (H5I.is_valid(_fileId) > 0) { H5F.close(_fileId); this.WritePreamble(filePath); } } _fileId = H5F.open(filePath, H5F.ACC_RDWR); }
public static void PrepareAttribute <T>(long locationId, string name, T[] valueSet, ulong[] dimensionLimitSet, bool initializeAttribute, [CallerMemberName()] string callerMemberName = "") { Contract.Requires(valueSet != null, nameof(valueSet)); long fileId = -1; long typeId = -1; long attributeId = -1; ulong[] dimensionSet; bool isNew; Type elementType; dimensionSet = new ulong[] { 0 }; elementType = typeof(T); try { fileId = H5I.get_file_id(locationId); // create attribute typeId = TypeConversionHelper.GetHdfTypeIdFromType(fileId, elementType); (attributeId, isNew) = IOHelper.OpenOrCreateAttribute(locationId, name, typeId, (ulong)valueSet.LongLength, dimensionLimitSet); // write attribute data (regenerate attribute if necessary) if (initializeAttribute) { ulong[] oldValueSetCount; if (!isNew && callerMemberName != nameof(IOHelper.PrepareAttribute)) { oldValueSetCount = IOHelper.PrepareAttributeValueSet(attributeId, ref valueSet, false); } else { oldValueSetCount = new ulong[1] { (ulong)valueSet.Count() } }; if (valueSet.Count() == (int)oldValueSetCount[0]) { IOHelper.Write(attributeId, valueSet, DataContainerType.Attribute); } else { H5A.close(attributeId); H5A.delete(locationId, name); IOHelper.PrepareAttribute(locationId, name, valueSet, dimensionLimitSet, true); } } } finally { if (H5I.is_valid(attributeId) > 0) { H5A.close(attributeId); } if (H5I.is_valid(typeId) > 0) { H5T.close(typeId); } if (H5I.is_valid(fileId) > 0) { H5F.close(fileId); } } }