protected override void OnInitialize() { if (IOHelper.CheckLinkExists(_vdsMetaFileId, _currentPath)) { _groupId = H5G.open(_vdsMetaFileId, _currentPath); if (H5A.exists(_groupId, "unit") > 0) { _unit = IOHelper.ReadAttribute <string>(_groupId, "unit").FirstOrDefault(); } if (H5A.exists(_groupId, "transfer_function_set") > 0) { _vdsMetaTransferFunctionSet = IOHelper.ReadAttribute <hdf_transfer_function_t>(_groupId, "transfer_function_set").ToList(); } else { _vdsMetaTransferFunctionSet = new List <hdf_transfer_function_t>(); } H5G.close(_groupId); } else { _vdsMetaTransferFunctionSet = new List <hdf_transfer_function_t>(); } }
public static void UpdateCampaignInfoSet() { long vdsFileId = -1; long vdsMetaFileId = -1; long groupId = -1; lock (_lock) { try { if (File.Exists(_options.VdsFilePath)) { vdsFileId = H5F.open(_options.VdsFilePath, H5F.ACC_RDONLY); vdsMetaFileId = H5F.open(_options.VdsMetaFilePath, H5F.ACC_RDONLY); Program.CampaignInfoSet = GeneralHelper.GetCampaignInfoSet(vdsFileId, false); } else { Program.CampaignInfoSet = new List <CampaignInfo>(); } Program.CampaignDescriptionSet = Program.CampaignInfoSet.ToDictionary(campaignInfo => campaignInfo.Name, campaignInfo => { if (IOHelper.CheckLinkExists(vdsMetaFileId, campaignInfo.Name)) { try { groupId = H5G.open(vdsMetaFileId, campaignInfo.Name); if (H5A.exists(groupId, "description") > 0) { return(IOHelper.ReadAttribute <string>(groupId, "description").First()); } } finally { if (H5I.is_valid(groupId) > 0) { H5G.close(groupId); } } } return("no description available"); }); } finally { if (H5I.is_valid(vdsFileId) > 0) { H5F.close(vdsFileId); } if (H5I.is_valid(vdsMetaFileId) > 0) { H5F.close(vdsMetaFileId); } } } }
public void H5AexistsTest2() { Assert.IsFalse( H5A.exists(Utilities.RandomInvalidHandle(), ".") >= 0); Assert.IsFalse( H5A.exists(m_v2_class_file, "") >= 0); }
public static bool WriteFixedStringAttribute(hid_t hid, string key, string value, bool utf8) { var exists = H5A.exists(hid, key); if (exists < 0) { throw new Exception("H5A.exists failed"); } if (exists == 0) // Attribute doesn't exist { var bytes = value.ToBytes(utf8); var type = CreateFixedStringType(bytes, utf8); var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { H5T.close(type); throw new Exception("Failed to create scalar space"); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); H5T.close(type); throw new Exception(string.Format("Failed to create attribute \"{0}\"", key)); } H5A.write(attribute, type, new PinnedObject(bytes)); H5A.close(attribute); H5S.close(space); H5T.close(type); return(true); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { throw new Exception(string.Format("Failed to open attribute \"{0}\"", key)); } try { return(WriteFixedStringAttribute(attribute, value, utf8)); } finally { H5A.close(attribute); } } }
public static bool WriteStringAttribute(hid_t hid, string key, string value, bool utf8 = true, bool variable = true) { if (H5A.exists(hid, key) == 0) { // Attribute doesn't exist. return(variable ? CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8) : CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8)); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { return(false); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); return(false); } var typeClass = H5T.get_class(type); if (typeClass == H5T.class_t.NO_CLASS) { H5T.close(type); H5T.close(attribute); return(false); } if (typeClass != H5T.class_t.STRING) { H5T.close(type); H5T.close(attribute); throw new ArgumentException("H5T.get_class(type) != H5T.class_t.STRING"); } utf8 = H5T.get_cset(type) == H5T.cset_t.UTF8; bool ok; if (H5T.is_variable_str(type) > 0) { ok = CreateOrOverwriteVariableStringAttribute(hid, key, new string[] { value }, utf8); } else { ok = CreateOrOverwriteFixedStringAttribute(hid, key, value, utf8); } H5T.close(type); H5T.close(attribute); return(ok); } }
public static bool WriteScalarNumericAttribute <T>(hid_t hid, string key, T value, hid_t type) where T : struct { if (key == null) { throw new ArgumentNullException("key"); } var exists = H5A.exists(hid, key); if (exists < 0) { throw new Exception("H5A.exists failed"); } if (exists == 0) { var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { throw new Exception("Failed to create scalar space"); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); throw new Exception(string.Format("Failed to create attribute \"{0}\"", key)); } H5S.close(space); object boxedValue = value; H5A.write(attribute, type, new PinnedObject(boxedValue)); H5A.close(attribute); return(true); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { throw new Exception(string.Format("Failed to open attribute \"{0}\"", key)); } try { return(WriteScalarNumericAttribute(attribute, value, type)); } finally { H5A.close(attribute); } } }
public static bool Exists(hid_t loc_id, string name) { int rv; if ((rv = H5A.exists(loc_id, name)) < 0) { throw new H5LibraryException($"H5L.exists returned ({rv})"); } return(rv > 0); }
public static bool ItemExists(long groupId, string groupName, Hdf5ElementType type) { switch (type) { case Hdf5ElementType.Group: case Hdf5ElementType.Dataset: return(H5L.exists(groupId, NormalizedName(groupName)) > 0); case Hdf5ElementType.Attribute: return(H5A.exists(groupId, NormalizedName(groupName)) > 0); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
public static (bool valid, string name) GetRealAttributeName(long id, string name, string alternativeName) { string normalized = NormalizedName(name); if (!String.IsNullOrEmpty(normalized) && H5A.exists(id, normalized) > 0) { return(true, normalized); } normalized = NormalizedName(alternativeName); if (!String.IsNullOrEmpty(normalized) && H5A.exists(id, normalized) > 0) { return(true, normalized); } return(false, ""); }
public void H5AexistsTest1() { htri_t check = H5A.exists(m_v0_class_file, "."); Assert.IsTrue(check >= 0); check = H5A.exists(m_v0_class_file, "NAC"); Assert.IsTrue(check >= 0); check = H5A.exists(m_v2_class_file, "A"); Assert.IsTrue(check >= 0); hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE, m_space_scalar); Assert.IsTrue(att >= 0); check = H5A.exists(m_v2_test_file, "A"); Assert.IsTrue(check > 0); Assert.IsTrue(H5A.close(att) >= 0); }
public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, Func <long> createAttributeCallback) { long attributeId = -1; long attributeTypeId_actual = -1; bool isNew; try { if (H5A.exists(locationId, name) > 0) { attributeId = H5A.open(locationId, name); attributeTypeId_actual = H5A.get_type(attributeId); if (H5T.equal(attributeTypeId_actual, attributeTypeId) <= 0) { throw new Exception($"{ ErrorMessage.IOHelper_DataTypeMismatch } Attribute: '{ name }'."); } isNew = false; } else { attributeId = createAttributeCallback.Invoke(); isNew = true; } if (H5I.is_valid(attributeId) <= 0) { throw new Exception($"{ ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute } Attribute: '{ name }'."); } } finally { if (H5I.is_valid(attributeTypeId_actual) > 0) { H5T.close(attributeTypeId_actual); } } return(attributeId, isNew); }
protected override void OnInitialize() { if (IOHelper.CheckLinkExists(_vdsMetaFileId, _currentPath)) { _groupId = H5G.open(_vdsMetaFileId, _currentPath); if (H5A.exists(_groupId, "aggregate_function_set") > 0) { _vdsMetaAggregateFunctionSet = IOHelper.ReadAttribute <hdf_aggregate_function_t>(_groupId, "aggregate_function_set").ToList(); } else { _vdsMetaAggregateFunctionSet = new List <hdf_aggregate_function_t>(); } H5G.close(_groupId); } else { _vdsMetaAggregateFunctionSet = new List <hdf_aggregate_function_t>(); } }
protected override void OnInitialize() { if (H5A.exists(_vdsLocationId, "tag_set") > 0) { _vdsTagSet = IOHelper.ReadAttribute <hdf_tag_t>(_vdsLocationId, "tag_set").ToList(); } else { _vdsTagSet = new List <hdf_tag_t>(); } if (IOHelper.CheckLinkExists(_vdsMetaFileId, _currentPath)) { _groupId = H5G.open(_vdsMetaFileId, _currentPath); _description = string.Empty; if (H5A.exists(_groupId, "description") > 0) { _description = IOHelper.ReadAttribute <string>(_groupId, "description").First(); } if (H5A.exists(_groupId, "tag_set") > 0) { _vdsMetaTagSet = IOHelper.ReadAttribute <hdf_tag_t>(_groupId, "tag_set").ToList(); } else { _vdsMetaTagSet = new List <hdf_tag_t>(); } H5G.close(_groupId); } else { _vdsMetaTagSet = new List <hdf_tag_t>(); } }
public void WriteCampaignDocumentation(string directoryPath, CampaignInfo campaignInfo) { long campaign_groupId = -1; string description; string filePath; RestructuredTextWriter restructuredTextWriter; List <string> groupNameSet; Console.Clear(); groupNameSet = campaignInfo.VariableInfoSet.Select(variableInfo => variableInfo.VariableGroupSet.Last()).Distinct().ToList(); filePath = Path.Combine(directoryPath, $"{ campaignInfo.Name.ToLower().Replace("/", "_").TrimStart('_') }.rst"); try { using (StreamWriter streamWriter = new StreamWriter(new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))) { restructuredTextWriter = new RestructuredTextWriter(streamWriter); // campaign header restructuredTextWriter.WriteHeading(campaignInfo.Name.TrimStart('/').Replace("/", " / "), SectionHeader.Section); // campaign description restructuredTextWriter.WriteLine(); try { if (IOHelper.CheckLinkExists(_vdsMetaFileId, campaignInfo.Name)) { campaign_groupId = H5G.open(_vdsMetaFileId, campaignInfo.Name); if (H5A.exists(campaign_groupId, "description") > 0) { description = IOHelper.ReadAttribute <string>(campaign_groupId, "description").First(); } else { description = "no description available"; } } else { description = "no description available"; } restructuredTextWriter.WriteNote(description); } finally { if (H5I.is_valid(campaign_groupId) > 0) { H5G.close(campaign_groupId); } } // groups foreach (string groupName in groupNameSet) { RestructuredTextTable restructuredTextTable; List <VariableInfo> groupedVariableInfoSet; restructuredTextWriter.WriteLine(); restructuredTextWriter.WriteHeading(groupName, SectionHeader.SubSection); restructuredTextWriter.WriteLine(); restructuredTextTable = new RestructuredTextTable(new List <string>() { "Name", "Unit", "Guid" }); groupedVariableInfoSet = campaignInfo.VariableInfoSet.Where(variableInfo => variableInfo.VariableGroupSet.Last() == groupName).OrderBy(variableInfo => variableInfo.VariableNameSet.Last()).ToList(); // variables groupedVariableInfoSet.ForEach(variableInfo => { long variable_groupId = -1; string groupPath; string name; string guid; string unit; List <hdf_transfer_function_t> transferFunctionSet; List <hdf_aggregate_function_t> aggregateFunctionSet; // name name = variableInfo.VariableNameSet.Last(); if (name.Count() > 43) { name = $"{ name.Substring(0, 40) }..."; } // guid guid = $"{ variableInfo.Name.Substring(0, 8) }..."; // unit, transferFunctionSet, aggregateFunctionSet unit = string.Empty; try { groupPath = variableInfo.GetPath(); if (IOHelper.CheckLinkExists(_vdsMetaFileId, groupPath)) { variable_groupId = H5G.open(_vdsMetaFileId, groupPath); if (H5A.exists(variable_groupId, "unit") > 0) { unit = IOHelper.ReadAttribute <string>(variable_groupId, "unit").FirstOrDefault(); } if (H5A.exists(variable_groupId, "transfer_function_set") > 0) { transferFunctionSet = IOHelper.ReadAttribute <hdf_transfer_function_t>(variable_groupId, "transfer_function_set").ToList(); } if (H5A.exists(variable_groupId, "aggregate_function_set") > 0) { aggregateFunctionSet = IOHelper.ReadAttribute <hdf_aggregate_function_t>(variable_groupId, "aggregate_function_set").ToList(); } } } finally { if (H5I.is_valid(variable_groupId) > 0) { H5G.close(variable_groupId); } } // restructuredTextTable.AddRow(new List <string> { name, unit, guid }); //transferFunctionSet.ForEach(x => streamWriter.Write($"{ x.date_time }, { x.type }, { x.option }, { x.argument } | ")); //aggregateFunctionSet.ForEach(x => streamWriter.Write($"{ x.type }, { x.argument } | ")); }); restructuredTextWriter.WriteTable(restructuredTextTable); restructuredTextWriter.WriteLine(); } } Console.WriteLine($"The file has been successfully written to:\n{ filePath }"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("\nPress any key to continue ..."); Console.ReadKey(); }
public Task <string> GetCampaignDocumentation(string campaigInfoName) { long vdsMetaFileId = -1; string csvFileName; CampaignInfo campaignInfo; List <string> groupNameSet; campaignInfo = Program.CampaignInfoSet.First(campaign => campaign.Name == campaigInfoName); return(Task.Run(() => { this.CheckState(); try { if (File.Exists(_options.VdsMetaFilePath)) { vdsMetaFileId = H5F.open(_options.VdsMetaFilePath, H5F.ACC_RDONLY); } csvFileName = Path.Combine(_options.SupportDirectoryPath, "EXPORT", $"OneDAS_{ campaignInfo.Name.ToLower().Replace("/", "_").TrimStart('_') }_{ Guid.NewGuid().ToString() }.csv"); groupNameSet = campaignInfo.VariableInfoSet.SelectMany(variableInfo => variableInfo.VariableGroupSet.Last().Split('\n')).Distinct().ToList(); using (StreamWriter streamWriter = new StreamWriter(new FileStream(csvFileName, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.UTF8)) { // campaign header streamWriter.WriteLine($"# { campaignInfo.Name.TrimStart('/').Replace("/", " / ") }"); // campaign description streamWriter.WriteLine($"# { Program.CampaignDescriptionSet[campaigInfoName] }"); // header streamWriter.WriteLine("Group;Name;Unit;Transfer function;Aggregate function;Guid"); // groups foreach (string groupName in groupNameSet) { List <VariableInfo> groupedVariableInfoSet; groupedVariableInfoSet = campaignInfo.VariableInfoSet.Where(variableInfo => variableInfo.VariableGroupSet.Last().Split('\n').Contains(groupName)).OrderBy(variableInfo => variableInfo.VariableNameSet.Last()).ToList(); // variables groupedVariableInfoSet.ForEach(variableInfo => { long variable_groupId = -1; string transferFunction; string aggregateFunction; string groupPath; string name; string guid; string unit; List <hdf_transfer_function_t> transferFunctionSet; List <hdf_aggregate_function_t> aggregateFunctionSet; name = variableInfo.VariableNameSet.Last(); guid = variableInfo.Name; unit = string.Empty; transferFunctionSet = new List <hdf_transfer_function_t>(); aggregateFunctionSet = new List <hdf_aggregate_function_t>(); try { groupPath = GeneralHelper.CombinePath(campaignInfo.Name, variableInfo.Name); if (H5I.is_valid(vdsMetaFileId) > 0 && IOHelper.CheckLinkExists(vdsMetaFileId, groupPath)) { variable_groupId = H5G.open(vdsMetaFileId, groupPath); if (H5A.exists(variable_groupId, "unit") > 0) { unit = IOHelper.ReadAttribute <string>(variable_groupId, "unit").FirstOrDefault(); } if (H5A.exists(variable_groupId, "transfer_function_set") > 0) { transferFunctionSet = IOHelper.ReadAttribute <hdf_transfer_function_t>(variable_groupId, "transfer_function_set").ToList(); } if (H5A.exists(variable_groupId, "aggregate_function_set") > 0) { aggregateFunctionSet = IOHelper.ReadAttribute <hdf_aggregate_function_t>(variable_groupId, "aggregate_function_set").ToList(); } } } finally { if (H5I.is_valid(variable_groupId) > 0) { H5G.close(variable_groupId); } } // transfer function transferFunction = string.Empty; transferFunctionSet.ForEach(tf => { if (!string.IsNullOrWhiteSpace(transferFunction)) { transferFunction += " | "; } transferFunction += $"{ tf.date_time }, { tf.type }, { tf.option }, { tf.argument }"; }); transferFunction = $"\"{ transferFunction }\""; // aggregate function aggregateFunction = string.Empty; aggregateFunctionSet.ForEach(af => { if (!string.IsNullOrWhiteSpace(aggregateFunction)) { aggregateFunction += " | "; } aggregateFunction += $"{ af.type }, { af.argument }"; }); aggregateFunction = $"\"{ aggregateFunction }\""; // write row streamWriter.WriteLine($"{ groupName };{ name };{ unit };{ transferFunction };{ aggregateFunction };{ guid }"); }); } } return $"download/{ Path.GetFileName(csvFileName) }"; } finally { if (H5I.is_valid(vdsMetaFileId) > 0) { H5F.close(vdsMetaFileId); } } })); }
public static List <CampaignInfo> InternalUpdateCampaignInfoSet(long fileId, bool isLazyLoading, List <CampaignInfo> campaignInfoSet = null, string campaignGroupPath = "") { ulong idx; idx = 0; if (campaignInfoSet == null) { campaignInfoSet = new List <CampaignInfo>(); } GeneralHelper.SuppressErrors(() => H5L.iterate(fileId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx, Callback, Marshal.StringToHGlobalAnsi("/"))); return(campaignInfoSet); int Callback(long campaignGroupId, IntPtr intPtrName, ref H5L.info_t info, IntPtr intPtrUserData) { ulong idx2 = 0; long groupId = -1; long datasetId = -1; int level; int formatVersion; string name; string fullName; string userData; H5O.type_t objectType; CampaignInfo currentCampaignInfo; StringBuilder filePath; DateTime dateTime; // if (H5A.exists(fileId, "format_version") > 0) // raw file { formatVersion = IOHelper.ReadAttribute <int>(fileId, "format_version").First(); } else // virtual file { formatVersion = -1; } filePath = new StringBuilder(260); H5F.get_name(fileId, filePath, new IntPtr(260)); dateTime = DateTime.ParseExact(IOHelper.ReadAttribute <string>(fileId, "date_time").First(), "yyyy-MM-ddTHH-mm-ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); name = Marshal.PtrToStringAnsi(intPtrName); userData = Marshal.PtrToStringAnsi(intPtrUserData); fullName = GeneralHelper.CombinePath(userData, name); level = userData.Split("/".ToArray()).Count(); // this is necessary, since H5Oget_info_by_name is slow because it wants verbose object header data // and H5G_loc_info is not directly accessible // only chance is to modify source code (H5Oget_info_by_name) datasetId = H5D.open(campaignGroupId, name); if (H5I.is_valid(datasetId) > 0) { objectType = H5O.type_t.DATASET; } else { groupId = H5G.open(campaignGroupId, name); if (H5I.is_valid(groupId) > 0) { objectType = H5O.type_t.GROUP; } else { objectType = H5O.type_t.UNKNOWN; } } switch (level) { case 1: case 2: break; case 3: if (objectType == H5O.type_t.GROUP) { if (!string.IsNullOrWhiteSpace(campaignGroupPath) && fullName != campaignGroupPath) { return(0); } currentCampaignInfo = campaignInfoSet.FirstOrDefault(campaignInfo => campaignInfo.Name == fullName); if (currentCampaignInfo == null) { currentCampaignInfo = new CampaignInfo(fullName, null, isLazyLoading); campaignInfoSet.Add(currentCampaignInfo); } currentCampaignInfo.Update(new FileContext(fileId, formatVersion, dateTime, filePath.ToString())); } break; } if (objectType == H5O.type_t.GROUP && level < 3) { H5L.iterate(groupId, H5.index_t.NAME, H5.iter_order_t.INC, ref idx2, Callback, Marshal.StringToHGlobalAnsi(fullName)); } // clean up if (H5I.is_valid(groupId) > 0) { H5G.close(groupId); } if (H5I.is_valid(datasetId) > 0) { H5D.close(datasetId); } return(0); } }
private static bool CreateOrOverwriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8) { if (H5A.exists(hid, key) == 0) { // Attribute doesn't exist. #if true var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); if (type < 0) { return(false); } #else var type = H5T.copy(H5T.C_S1); if (type < 0) { return(false); } H5T.set_size(type, H5T.VARIABLE); #endif if (utf8) { H5T.set_cset(type, H5T.cset_t.UTF8); } H5T.set_strpad(type, H5T.str_t.NULLTERM); var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR) : H5S.create_simple(1, new ulong[1] { (ulong)values.Count() }, null); if (space < 0) { H5T.close(type); return(false); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); H5T.close(type); return(false); } H5S.close(space); var pinnedObjects = new PinnedObject[values.Count()]; var data = new IntPtr[values.Count()]; int count = 0; foreach (string str in values) { var bytes = str.ToBytes(utf8); pinnedObjects[count] = new PinnedObject(bytes); data[count] = pinnedObjects[count]; count += 1; } H5A.write(attribute, type, new PinnedObject(data)); H5T.close(type); H5A.close(attribute); } else { // Attribute exists. var attribute = H5A.open(hid, key); if (attribute < 0) { return(false); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); return(false); } var pinnedObjects = new PinnedObject[values.Count()]; var data = new IntPtr[values.Count()]; int count = 0; foreach (string str in values) { var bytes = str.ToBytes(utf8); pinnedObjects[count] = new PinnedObject(bytes); data[count] = pinnedObjects[count]; count += 1; } H5A.write(attribute, type, new PinnedObject(data)); H5T.close(type); H5A.close(attribute); } return(true); }
private static bool CreateOrOverwriteFixedStringAttribute(hid_t hid, string key, string value, bool utf8) { if (H5A.exists(hid, key) == 0) { // Attribute doesn't exist. var bytes = value.ToBytes(utf8); #if true var type = H5T.create(H5T.class_t.STRING, new IntPtr(bytes.Length)); if (type < 0) { return(false); } #else var type = H5T.copy(H5T.C_S1); if (type < 0) { return(false); } H5T.set_size(type, new IntPtr(bytes.Length)); #endif if (utf8) { H5T.set_cset(type, H5T.cset_t.UTF8); } H5T.set_strpad(type, H5T.str_t.NULLTERM); var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { H5T.close(type); return(false); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); H5T.close(type); return(false); } H5A.write(attribute, type, new PinnedObject(bytes)); H5A.close(attribute); } else { // Attribute exists. var attribute = H5A.open(hid, key); if (attribute < 0) { return(false); } var type = H5A.get_type(attribute); H5A.write(attribute, type, new PinnedObject(value.ToBytes(utf8))); H5T.close(type); H5A.close(attribute); } return(true); }
public static bool WriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8) { if (key == null) { throw new ArgumentNullException("key"); } if (values == null) { throw new ArgumentNullException("values"); } var exists = H5A.exists(hid, key); if (exists < 0) { throw new Exception("H5A.exists failed"); } if (exists == 0) // Attribute doesn't exist { #if true var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); if (type < 0) { throw new Exception("Failed to create string type"); } #else var type = H5T.copy(H5T.C_S1); if (type < 0) { throw new Exception("Failed to create string type"); } if (H5T.set_size(type, H5T.VARIABLE) < 0) { H5T.close(type); throw new Exception("Failed to set type size"); } #endif if (utf8) { if (H5T.set_cset(type, H5T.cset_t.UTF8) < 0) { H5T.close(type); throw new Exception("Failed to set cset"); } } if (H5T.set_strpad(type, H5T.str_t.NULLTERM) < 0) { H5T.close(type); throw new Exception("Failed to set strpad"); } var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR) : H5S.create_simple(1, new ulong[1] { (ulong)values.Count() }, null); if (space < 0) { H5T.close(type); throw new Exception("Failed to create data space"); } var attribute = H5A.create(hid, key, type, space); H5S.close(space); if (attribute < 0) { H5T.close(type); throw new Exception(string.Format("Failed to create attribute \"{0}\"", key)); } var pinnedObjects = new PinnedObject[values.Count()]; var data = new IntPtr[values.Count()]; int count = 0; foreach (string str in values) { var bytes = str.ToBytes(utf8); pinnedObjects[count] = new PinnedObject(bytes); data[count] = pinnedObjects[count]; count += 1; } H5A.write(attribute, type, new PinnedObject(data)); H5T.close(type); H5A.close(attribute); } else { // Attribute exists. var attribute = H5A.open(hid, key); if (attribute < 0) { throw new Exception(string.Format("Failed to open attribute \"{0}\"", key)); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); throw new Exception("Failed to get data type"); } var pinnedObjects = new PinnedObject[values.Count()]; var data = new IntPtr[values.Count()]; int count = 0; foreach (string str in values) { var bytes = str.ToBytes(utf8); pinnedObjects[count] = new PinnedObject(bytes); data[count] = pinnedObjects[count]; count += 1; } H5A.write(attribute, type, new PinnedObject(data)); H5T.close(type); H5A.close(attribute); } return(true); }
public static bool WriteEnumAttribute <T>(hid_t hid, string key, T value) where T : struct, IConvertible { var enumSize = EnumHelper.GetUnderlyingTypeSize <T>(); if (!Enum.IsDefined(typeof(T), value)) { throw new ArgumentException("value"); } if (H5A.exists(hid, key) == 0) { // Attribute doesn't exist. var type = CreateEnumType <T>(); if (type < 0) { return(false); } var space = H5S.create(H5S.class_t.SCALAR); if (space < 0) { H5T.close(type); return(false); } var attribute = H5A.create(hid, key, type, space); if (attribute < 0) { H5S.close(space); H5T.close(type); return(false); } var unmanagedBuffer = new UnmanagedBuffer(enumSize); unmanagedBuffer.WriteEnum(value); H5A.write(attribute, type, unmanagedBuffer); H5A.close(attribute); H5S.close(space); H5T.close(type); } else { var attribute = H5A.open(hid, key); if (attribute < 0) { return(false); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); return(false); } var unmanagedBuffer = new UnmanagedBuffer(enumSize); unmanagedBuffer.WriteEnum(value); H5A.write(attribute, type, unmanagedBuffer); H5T.close(type); H5A.close(attribute); } return(true); }