public static unsafe void AddDataWithSharedDataType(long fileId, ContainerType container) { long typeId = H5T.copy(H5T.C_S1); H5T.set_size(typeId, H5T.VARIABLE); H5T.set_cset(typeId, H5T.cset_t.UTF8); H5T.commit(fileId, "string_t", typeId); var data = new string[] { "001", "11", "22", "33", "44", "55", "66", "77", " ", "AA", "ZZ", "!!" }; var dataChar = data .SelectMany(value => Encoding.ASCII.GetBytes(value + '\0')) .ToArray(); fixed(byte *dataVarPtr = dataChar) { var basePtr = new IntPtr(dataVarPtr); var addresses = new IntPtr[] { IntPtr.Add(basePtr, 0), IntPtr.Add(basePtr, 4), IntPtr.Add(basePtr, 7), IntPtr.Add(basePtr, 10), IntPtr.Add(basePtr, 13), IntPtr.Add(basePtr, 16), IntPtr.Add(basePtr, 19), IntPtr.Add(basePtr, 22), IntPtr.Add(basePtr, 25), IntPtr.Add(basePtr, 28), IntPtr.Add(basePtr, 31), IntPtr.Add(basePtr, 34) }; fixed(void *dataVarAddressesPtr = addresses) { TestUtils.Add(container, fileId, "shared_data_type", "shared_data_type", typeId, dataVarAddressesPtr, length: 12); } } if (H5I.is_valid(typeId) > 0) { H5T.close(typeId); } }
public void H5TcommitTest2() { // can't commit pre-defined types Assert.IsFalse( H5T.commit(m_v0_test_file, "foo", H5T.IEEE_F64BE) >= 0); Assert.IsFalse( H5T.commit(m_v2_test_file, "bar", H5T.IEEE_F64BE) >= 0); }
public void H5TcommitTest3() { Assert.IsFalse( H5T.commit(m_v0_test_file, "foo", Utilities.RandomInvalidHandle()) >= 0); Assert.IsFalse( H5T.commit(Utilities.RandomInvalidHandle(), "foo", Utilities.RandomInvalidHandle()) >= 0); }
public void H5TcommitTest1() { hid_t dtype = H5T.copy(H5T.IEEE_F64LE); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.commit(m_v0_test_file, "foo", dtype) >= 0); // can't commit twice Assert.IsFalse(H5T.commit(m_v0_test_file, "bar", dtype) >= 0); // can't commit to different files Assert.IsFalse(H5T.commit(m_v2_test_file, "bar", dtype) >= 0); Assert.IsTrue(H5T.close(dtype) >= 0); }
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