} // end of test_classes static void test_integer_dtype() { const string GEN_FILE_NAME = "gen_types.h5"; const string INT_TYPE_NAME = "new integer type 1"; try { Console.Write("Testing getting some integer information"); H5FileId genfileId = H5F.open(GEN_FILE_NAME, H5F.OpenMode.ACC_RDONLY); // Open the datatype to check. H5DataTypeId dtypeId = H5T.open(genfileId, INT_TYPE_NAME); int offset = H5T.getOffset(dtypeId); if (offset != 0) { Console.WriteLine("Incorrect offset: {0}, should be {1}", offset, 0); nerrors++; } int size = H5T.getSize(dtypeId); if (size != 3) { Console.WriteLine("Incorrect size: {0}, should be {1}", size, 3); nerrors++; } // Close datatype and file. H5T.close(dtypeId); H5F.close(genfileId); Console.WriteLine("\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++; } } // test_integer_dtype
public static long GetHdfTypeIdFromType(long fileId, Type type) { Type elementType; elementType = type.IsArray ? type.GetElementType() : type; if (elementType == typeof(bool)) { return(H5T.NATIVE_UINT8); } else if (elementType == typeof(Byte)) { return(H5T.NATIVE_UINT8); } else if (elementType == typeof(SByte)) { return(H5T.NATIVE_INT8); } else if (elementType == typeof(UInt16)) { return(H5T.NATIVE_UINT16); } else if (elementType == typeof(Int16)) { return(H5T.NATIVE_INT16); } else if (elementType == typeof(UInt32)) { return(H5T.NATIVE_UINT32); } else if (elementType == typeof(Int32)) { return(H5T.NATIVE_INT32); } else if (elementType == typeof(UInt64)) { return(H5T.NATIVE_UINT64); } else if (elementType == typeof(Int64)) { return(H5T.NATIVE_INT64); } else if (elementType == typeof(Single)) { return(H5T.NATIVE_FLOAT); } else if (elementType == typeof(Double)) { return(H5T.NATIVE_DOUBLE); } else if (elementType == typeof(string) || elementType == typeof(IntPtr)) { long typeId = 0; if (H5I.is_valid(fileId) > 0 && H5L.exists(fileId, "string_t") > 0) { typeId = H5T.open(fileId, "string_t"); } else { typeId = H5T.copy(H5T.C_S1); H5T.set_size(typeId, H5T.VARIABLE); H5T.set_cset(typeId, H5T.cset_t.UTF8); if (fileId > -1 && H5T.commit(fileId, "string_t", typeId) < 0) { throw new Exception(ErrorMessage.TypeConversionHelper_CouldNotCommitDataType); } } return(typeId); } else if (elementType.IsValueType && !elementType.IsPrimitive && !elementType.IsEnum) { long typeId = 0; if (H5I.is_valid(fileId) > 0 && H5L.exists(fileId, elementType.Name) > 0) { typeId = H5T.open(fileId, elementType.Name); } else { typeId = H5T.create(H5T.class_t.COMPOUND, new IntPtr(Marshal.SizeOf(elementType))); foreach (FieldInfo fieldInfo in elementType.GetFields()) { long fieldType = -1; fieldType = TypeConversionHelper.GetHdfTypeIdFromType(fileId, fieldInfo.FieldType); H5T.insert(typeId, fieldInfo.Name, Marshal.OffsetOf(elementType, fieldInfo.Name), fieldType); if (H5I.is_valid(fieldType) > 0) { H5T.close(fieldType); } } if (fileId > -1 && H5T.commit(fileId, elementType.Name, typeId) < 0) { throw new Exception(ErrorMessage.TypeConversionHelper_CouldNotCommitDataType); } } return(typeId); } else { throw new NotSupportedException(); } }
/// <summary> /// Constructs a new EpochHDF5Persistor with an HDF5 file at the given path. /// </summary> /// <param name="filename">Desired HDF5 path</param> /// <param name="assocFilePrefix">Prefix for auxiliary (e.g. image) file associated with this HDF5 file</param> /// <param name="guidGenerator">Function for generating new UUIDs (e.g. Guid.NewGuid)</param> /// <param name="compression">Automatically numeric data compression (0 = none, 9 = maximum)</param> public EpochHDF5Persistor(string filename, string assocFilePrefix, Func <Guid> guidGenerator, uint compression = 9) : base(guidGenerator) { if (filename == null) { throw new ArgumentException("File name must not be null", "filename"); } if (compression > 9) { throw new ArgumentException("Compression must be 0-9", "compression"); } if (assocFilePrefix == null) { assocFilePrefix = ""; } this.AssociatedFilePrefix = assocFilePrefix; NumericDataCompression = compression; EpochGroupsIDs = new Stack <EpochGroupIDs>(); var fInfo = new FileInfo(filename); string prefixedFilePath = fInfo.DirectoryName + Path.DirectorySeparatorChar + this.AssociatedFilePrefix + fInfo.Name; var currentFile = new FileInfo(prefixedFilePath); if (currentFile.Exists) { fileId = H5F.open(prefixedFilePath, H5F.OpenMode.ACC_RDWR); string_t = H5T.open(fileId, "STRING40"); keyval_t = H5T.open(fileId, "KEY40VAR40"); measurement_t = H5T.open(fileId, "MEASUREMENT"); extdevmeasurement_t = H5T.open(fileId, "EXTDEV_MEASUREMENT"); //TODO Check persistence version } else { fileId = H5F.create(prefixedFilePath, H5F.CreateMode.ACC_EXCL); WriteAttribute(fileId, "version", Version); // Create our standard String type (string of length FIXED_STRING_LENGTH characters) string_t = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(string_t, 40); H5T.commit(fileId, "STRING40", string_t); // Create our key/value compound type (two strings of length 40 characters) keyval_t = H5T.create(H5T.CreateClass.COMPOUND, 80); H5T.insert(keyval_t, "key", 0, string_t); H5T.insert(keyval_t, "value", FIXED_STRING_LENGTH, string_t); H5T.commit(fileId, "KEY40VAR40", keyval_t); // Create the Measurement compound type measurement_t = H5T.create(H5T.CreateClass.COMPOUND, 48); // confirm 48 is enough/too much/whatever H5T.insert(measurement_t, "quantity", 0, H5T.H5Type.NATIVE_DOUBLE); H5T.insert(measurement_t, "unit", H5T.getSize(H5T.H5Type.NATIVE_DOUBLE), string_t); H5T.commit(fileId, "MEASUREMENT", measurement_t); // Create the ExtDev/Measurement compound type extdevmeasurement_t = H5T.create(H5T.CreateClass.COMPOUND, H5T.getSize(string_t) + 2 * H5T.getSize(measurement_t)); H5T.insert(extdevmeasurement_t, "externalDevice", 0, string_t); H5T.insert(extdevmeasurement_t, "measurement", H5T.getSize(string_t), measurement_t); H5T.commit(fileId, "EXTDEV_MEASUREMENT", extdevmeasurement_t); } Interlocked.Increment(ref _openHdf5FileCount); }
static void test_enum_dtype(H5FileId fileId) { short i, j; string[] mname = { "RED", "GREEN", "BLUE", "YELLOW", "PINK", "PURPLE", "ORANGE", "WHITE" }; short[,] spoints2 = new short[DIM0, DIM1]; short[,] scheck2 = new short[DIM0, DIM1]; try { Console.Write("Testing enumeration datatypes"); // Create the data space */ hssize_t[] dims = { DIM0, DIM1 }; H5DataSpaceId dspace = H5S.create_simple(2, dims); // Construct enum type based on native type H5DataTypeId etype = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT); // Insert members to type. for (i = 0; i < 8; i++) { H5T.enumInsert(etype, mname[i], ref i); } // Assign a name to the enum type, close it, and open it by name. H5T.commit(fileId, "Color Type", etype); H5T.close(etype); H5DataTypeId color_type = H5T.open(fileId, "Color Type"); // Check its class H5T.H5TClass tcls = H5T.getClass(color_type); if (tcls != H5T.H5TClass.ENUM) { Console.WriteLine("test_enum: class of color_type = {0} is incorrect, should be ENUM", tcls); } // Create the dataset H5DataSetId dsetId = H5D.create(fileId, DSET_ENUM_NAME, color_type, dspace); // Construct enum type based on native type in memory. H5DataTypeId etype_m = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT); // Insert members to type. for (i = 0; i < 8; i++) { H5T.enumInsert(etype_m, mname[i], ref i); } // Initialize the dataset and buffer. for (i = 0; i < DIM0; i++) { for (j = 0; j < DIM1; j++) { spoints2[i, j] = i; scheck2[i, j] = 0; } } // Write the data to the dataset. H5D.write(dsetId, etype_m, new H5Array <short>(spoints2)); // Close objects. H5D.close(dsetId); H5T.close(color_type); H5S.close(dspace); H5T.close(etype_m); // Open dataset again to check the type. dsetId = H5D.open(fileId, DSET_ENUM_NAME); // Get dataset's datatype. H5DataTypeId dstype = H5D.getType(dsetId); // Get the datatype's class and check that it is of class ENUM. H5T.H5TClass tclass = H5T.getClass(dstype); if (tclass != H5T.H5TClass.ENUM) { Console.WriteLine("Type should be an enum class"); nerrors++; } // Check name of an enum value. int memb_num = 2; string memb_name = H5T.enumNameOf(dstype, ref memb_num); if (memb_name != "BLUE") { Console.WriteLine("Member name of value 2 should be BLUE"); nerrors++; } // Check value of an enum name. int memb_value = 0; H5T.enumValueOf(dstype, memb_name, out memb_value); if (memb_value != 2) { Console.WriteLine("Member value of BLUE should be 2"); nerrors++; } // Check member's value by member number. H5T.getMemberValue(dstype, 4, out memb_value); // Read data back. H5D.read(dsetId, dstype, new H5Array <short>(scheck2)); // Close objects. H5D.close(dsetId); H5T.close(dstype); 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_enum_dtype
} // test_integer_dtype static void test_float_dtype() { try { Console.Write("Testing getting some floating-point information"); const string GEN_FILE_NAME = "gen_types.h5"; const string FLOAT_TYPE_NAME = "new float type 1"; // Open file pre-generated by a C program. H5FileId genfileId = H5F.open(GEN_FILE_NAME, H5F.OpenMode.ACC_RDONLY); // Open the datatype to check. H5DataTypeId dtypeId = H5T.open(genfileId, FLOAT_TYPE_NAME); // Get and check fields. H5FloatingBitFields fields = H5T.getFields(dtypeId); if (fields.signBitPosition != 44) { Console.WriteLine("Incorrect sign bit position: {0}, should be {1}", fields.signBitPosition, 44); nerrors++; } if (fields.exponentBitPosition != 34) { Console.WriteLine("Incorrect exponential bit position: {0}, should be {1}", fields.exponentBitPosition, 34); nerrors++; } if (fields.nExponentBits != 10) { Console.WriteLine("Incorrect size of exponent: {0}, should be {1}", fields.nExponentBits, 10); nerrors++; } if (fields.mantissaBitPosition != 3) { Console.WriteLine("Incorrect mantissa bit-position: {0}, should be {1}", fields.mantissaBitPosition, 3); nerrors++; } if (fields.nMantissaBits != 31) { Console.WriteLine("Incorrect size of mantissa: {0}, should be {1}", fields.nMantissaBits, 44); nerrors++; } // Check precision. int precision = H5T.getPrecision(dtypeId); if (precision != 42) { Console.WriteLine("Incorrect precision: {0}, should be {1}", precision, 42); nerrors++; } // Check offset. int offset = H5T.getOffset(dtypeId); if (offset != 3) { Console.WriteLine("Incorrect offset: {0}, should be {1}", offset, 3); nerrors++; } // Check norm. H5T.Norm norm = H5T.getNorm(dtypeId); if (norm != H5T.Norm.IMPLIED) // need to be determined, not really 3 { Console.WriteLine("Incorrect norm: {0}, should be {1}", norm, H5T.Norm.IMPLIED); nerrors++; } // Check size. int size = H5T.getSize(dtypeId); if (size != 7) { Console.WriteLine("Incorrect size: {0}, should be {1}", size, 7); nerrors++; } // Close datatype and file. H5T.close(dtypeId); H5F.close(genfileId); Console.WriteLine("\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_float_dtype