public static string GetAttributeValue(H5ObjectWithAttributes objectWithAttributes, string name) { if (objectWithAttributes is null) { throw new ArgumentNullException(nameof(objectWithAttributes)); } if (name is null) { throw new ArgumentNullException(nameof(name)); } H5AttributeId h5AttributeId = H5A.open(objectWithAttributes, name); H5DataTypeId h5DataTypeId = H5A.getType(h5AttributeId); if (H5T.isVariableString(h5DataTypeId)) { VariableLengthString[] variableLengthStrings = new VariableLengthString[1]; H5A.read(h5AttributeId, h5DataTypeId, new H5Array <VariableLengthString> (variableLengthStrings)); H5T.close(h5DataTypeId); H5A.close(h5AttributeId); return(variableLengthStrings[0].ToString()); } byte[] bytes = new byte[H5T.getSize(h5DataTypeId)]; H5A.read(h5AttributeId, h5DataTypeId, new H5Array <byte> (bytes)); H5T.close(h5DataTypeId); H5A.close(h5AttributeId); return(Encoding.ASCII.GetString(bytes)); }
public static string AttributeAsString(H5AttributeId _attributeId) { H5DataTypeId dataTypeId = H5A.getType(_attributeId); bool isVariableLength = H5T.isVariableString(dataTypeId); if (isVariableLength) { // Variable length string attribute // NOTE: This section only works if the array length is 1 VariableLengthString[] value = new VariableLengthString[1]; H5A.read <VariableLengthString>(_attributeId, dataTypeId, new H5Array <VariableLengthString>(value)); return(value[0].ToString()); } else { // Make length smaller so null termination character is not read int length = (int)H5T.getSize(dataTypeId) - 1; // Fixed length string attribute byte[] valueBytes = new byte[length]; H5A.read <byte>(_attributeId, dataTypeId, new H5Array <byte>(valueBytes)); string value = System.Text.ASCIIEncoding.ASCII.GetString(valueBytes); return(value); } }
private void createHD5DataObject(H5GroupId h5GroupId, string pathName, ref HD5DataSetObject dataObject) { H5DataSetId datasetid = null; H5DataSpaceId spaceid = null; H5DataTypeId dataTypeid = null; try { dataObject.GroupId = h5GroupId; datasetid = H5D.open(h5GroupId, pathName); dataObject.DatasetID = datasetid; dataObject.DatasetName = pathName; spaceid = H5D.getSpace(datasetid); var dims = H5S.getSimpleExtentDims(spaceid); dataTypeid = H5D.getType(datasetid); dataObject.Dim = dims.Length; HDF5DotNet.H5T.H5TClass classType = H5T.getClass(dataTypeid); int size = H5T.getSize(dataTypeid); H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT; if (classType == H5T.H5TClass.INTEGER) { sign = H5T.getSign(dataTypeid); } //var rank = H5S.getSimpleExtentNDims(space); //var statu = H5S.getSimpleExtentDims(space); Boolean bString = H5T.isVariableString(dataTypeid); //String name = H5T.getMemberName(dataType, 0); // var type2 = H5T.getNativeType(dataType, H5T.Direction.DEFAULT); Type type = getTypeof(classType, size, sign); dataObject.DataType = type; dataObject.Data = readData(dataObject); } catch (Exception e) { Console.WriteLine(e.Message); } finally{ if (datasetid != null) { H5D.close(datasetid); } if (spaceid != null) { H5S.close(spaceid); } if (dataTypeid != null) { H5T.close(dataTypeid); } } }
static void test_classes() { try { Console.Write("Testing datatype classes"); /*------------------------------------------------------------- * Check class of some atomic types. *-----------------------------------------------------------*/ H5T.H5TClass tcls = H5T.getClass(H5T.H5Type.NATIVE_INT); if (tcls != H5T.H5TClass.INTEGER) { Console.WriteLine("Test class should have been H5T_INTEGER"); nerrors++; } tcls = H5T.getClass(H5T.H5Type.NATIVE_DOUBLE); if (tcls != H5T.H5TClass.FLOAT) { Console.WriteLine("Test class should have been H5T_FLOAT"); nerrors++; } H5DataTypeId op_type = H5T.create(H5T.CreateClass.OPAQUE, 1); tcls = H5T.getClass(op_type); if (tcls != H5T.H5TClass.OPAQUE) { Console.WriteLine("Test class should have been H5T_OPAQUE"); nerrors++; } // Create a VL datatype of char. It should be a VL, not a string class. H5DataTypeId vlcId = H5T.vlenCreate(H5T.H5Type.NATIVE_UCHAR); // Make certain that the correct classes can be detected tcls = H5T.getClass(vlcId); if (tcls != H5T.H5TClass.VLEN) { Console.WriteLine("Test class should have been H5T_VLEN"); nerrors++; } // Make certain that an incorrect class is not detected */ if (tcls == H5T.H5TClass.STRING) { Console.WriteLine("Test class should not be H5T_STRING"); nerrors++; } // Create a VL string. It should be a string, not a VL class. H5DataTypeId vlsId = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(vlsId, -1); // for H5T_VARIABLE tcls = H5T.getClass(vlsId); if (tcls != H5T.H5TClass.STRING) { Console.WriteLine("Test class should have been H5T_STRING"); nerrors++; } if (tcls == H5T.H5TClass.VLEN) { Console.WriteLine("Test class should not be H5T_VLEN"); nerrors++; } // Check that this is a variable-length string. if (!H5T.isVariableString(vlsId)) { Console.WriteLine("This type should be a variable-length string"); nerrors++; } // Close datatype H5T.close(vlcId); Console.WriteLine("\t\t\t\tPASSED"); } // end of try block catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // end of test_classes