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 static double[][] ReadMesh(string fileName) { double[][] meshes = new double[3][]; string[] meshNames = { "x", "y", "z" }; H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY); for (int i = 0; i < meshNames.Length; i++) { H5DataSetId dsId = H5D.open(fileId, "/Mesh/" + meshNames[i]); H5DataTypeId dtId = H5D.getType(dsId); if (!H5T.equal(dtId, H5T.copy(H5T.H5Type.NATIVE_FLOAT))) { Console.WriteLine("Error: Invalid dataset type, expected {0}", H5T.H5Type.NATIVE_FLOAT); } float[] mesh = new float[H5D.getStorageSize(dsId) / H5T.getSize(dtId)]; H5D.read(dsId, dtId, new H5Array <float>(mesh)); meshes[i] = mesh.Select(x => (double)x * 1000.0).ToArray(); // m -> mm H5D.close(dsId); H5T.close(dtId); } H5F.close(fileId); return(meshes); }
public void H5Dget_typeTest1() { hsize_t[] dims = { 1024, 2048 }; hid_t space = H5S.create_simple(3, dims, null); hid_t dset = H5D.create(m_v0_test_file, "dset", H5T.STD_I16LE, space); Assert.IsTrue(dset >= 0); hid_t type = H5D.get_type(dset); Assert.IsTrue(type >= 0); Assert.IsTrue(H5T.equal(type, H5T.STD_I16LE) > 0); Assert.IsTrue(H5T.close(type) >= 0); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create(m_v2_test_file, "dset", H5T.STD_I16LE, space); Assert.IsTrue(dset >= 0); type = H5D.get_type(dset); Assert.IsTrue(type >= 0); Assert.IsTrue(H5T.equal(type, H5T.STD_I16LE) > 0); Assert.IsTrue(H5T.close(type) >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
public static int WriteDataset <T>(int groupId, string name, T[,] dset) where T : struct { ulong[] dims = new ulong[] { (ulong)dset.GetLength(0), (ulong)dset.GetLength(1) }; ulong[] maxDims = null; var spaceId = H5S.create_simple(2, dims, maxDims); var datatype = GetDatatype(typeof(T)); var typeId = H5T.copy(datatype); if (datatype == H5T.C_S1) { H5T.set_size(datatype, new IntPtr(2)); //var wdata = Encoding.ASCII.GetBytes((char[,]) dset); } name = ToHdf5Name(name); var datasetId = H5D.create(groupId, name, datatype, spaceId); GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned); var result = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5D.close(datasetId); H5S.close(spaceId); H5T.close(typeId); return(result); }
public static unsafe void AddMass(long fileId, ContainerType container) { long res; var typeId = TestUtils.GetHdfTypeIdFromType(typeof(TestStructL1)); for (int i = 0; i < 1000; i++) { var dims = new ulong[] { 2, 2, 3 }; if (i == 450) { var acpl_id = H5P.create(H5P.ATTRIBUTE_CREATE); res = H5P.set_char_encoding(acpl_id, H5T.cset_t.UTF8); var name = "字形碼 / 字形码, Zìxíngmǎ"; TestUtils.Add(container, fileId, "mass_attributes", name, typeId, TestData.NonNullableStructData.AsSpan(), dims, cpl: acpl_id); } else { var name = $"mass_{i.ToString("D4")}"; TestUtils.Add(container, fileId, "mass_attributes", name, typeId, TestData.NonNullableStructData.AsSpan(), dims); } } res = H5T.close(typeId); }
public static double ReadAttribute(string file, string dataSetOrGroup, string attribute) { double attr = Double.NaN; try { H5FileId fileId = H5F.open(file, H5F.OpenMode.ACC_RDONLY); H5ObjectInfo objectInfo = H5O.getInfoByName(fileId, dataSetOrGroup); H5GroupId groupId = null; H5DataSetId dataSetId = null; H5AttributeId attrId; if (objectInfo.objectType == H5ObjectType.GROUP) { groupId = H5G.open(fileId, dataSetOrGroup); attrId = H5A.open(groupId, attribute); } else { dataSetId = H5D.open(fileId, dataSetOrGroup); attrId = H5A.open(dataSetId, attribute); } H5DataTypeId attrTypeId = H5A.getType(attrId); double[] dAttrs = new double[] { }; if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT))) { float[] fAttrs = new float[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))]; H5A.read(attrId, attrTypeId, new H5Array <float>(fAttrs)); dAttrs = (from f in fAttrs select(double) f).ToArray(); } else if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_DOUBLE))) { dAttrs = new double[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))]; H5A.read(attrId, attrTypeId, new H5Array <double>(dAttrs)); } H5T.close(attrTypeId); H5A.close(attrId); if (groupId != null) { H5G.close(groupId); } if (dataSetId != null) { H5D.close(dataSetId); } H5F.close(fileId); return((double)dAttrs[0]); } catch (HDFException e) { Console.WriteLine("Error: Unhandled HDF5 exception"); Console.WriteLine(e.Message); } return(attr); }
/// <summary> /// 写数据集属性 /// </summary> public void WriteDatasetAttribute(string datasetName, string attrName, string value) { H5DataSetId datasetId = H5D.open(_fileId, datasetName); H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5T.setSize(typeId, value.Length); H5AttributeId attrId = H5A.create(datasetId, attrName, typeId, spaceId); if (value != "") { H5Array <byte> buffer = new H5Array <byte>(Encoding.Default.GetBytes(value)); H5A.write(attrId, typeId, buffer); } if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (attrId != null) { H5A.close(attrId); } if (datasetId != null) { H5D.close(datasetId); } }
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 double[,] ReadFieldData2D(string file, string dataSet) { H5FileId fileId = H5F.open(file, H5F.OpenMode.ACC_RDONLY); H5DataSetId fDataSetId = H5D.open(fileId, dataSet); H5DataTypeId fDataTypeId = H5D.getType(fDataSetId); long[] dims = H5S.getSimpleExtentDims(H5D.getSpace(fDataSetId)).ToArray(); double[,] data = new double[dims[0], dims[1]]; H5D.read(fDataSetId, fDataTypeId, new H5Array <double>(data)); double[,] fieldValues = new double[dims[1], dims[0]]; for (int i = 0; i < dims[1]; i++) { for (int j = 0; j < dims[0]; j++) { fieldValues[i, j] = (double)data[j, i]; } } H5T.close(fDataTypeId); H5D.close(fDataSetId); H5F.close(fileId); return(fieldValues); }
// information: https://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5T/h5ex_t_cmpd.c //or: https://www.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%3Frhtocid%3Dtoc6.5%23TOC_6_8_Complex_Combinationsbc-22 public static int WriteCompounds <T>(hid_t groupId, string name, IEnumerable <T> list) //where T : struct { Type type = typeof(T); var size = Marshal.SizeOf(type); var cnt = list.Count(); var typeId = create_type(type); var log10 = (int)Math.Log10(cnt); ulong pow = (ulong)Math.Pow(10, log10); ulong c_s = Math.Min(1000, pow); ulong[] chunk_size = new ulong[] { c_s }; ulong[] dims = new ulong[] { (ulong)cnt }; long dcpl = 0; if (list.Count() == 0 || log10 == 0) { } else { dcpl = create_property(chunk_size); } // Create dataspace. Setting maximum size to NULL sets the maximum // size to be the current size. var spaceId = H5S.create_simple(dims.Length, dims, null); // Create the dataset and write the compound data to it. var datasetId = H5D.create(groupId, name, typeId, spaceId, H5P.DEFAULT, dcpl); IntPtr p = Marshal.AllocHGlobal(size * (int)dims[0]); var ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); foreach (var strct in list) { writer.Write(getBytes(strct)); } var bytes = ms.ToArray(); GCHandle hnd = GCHandle.Alloc(bytes, GCHandleType.Pinned); var statusId = H5D.write(datasetId, typeId, spaceId, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); /* * Close and release resources. */ H5D.close(datasetId); H5S.close(spaceId); H5T.close(typeId); H5P.close(dcpl); Marshal.FreeHGlobal(p); return(statusId); }
public static void Write1DArray <T>(Hdf5Dataset _dataset, T[] _array) { if (_dataset.Dataspace.NumberOfDimensions != 1) { throw new Hdf5ArrayDimensionsMismatchException(); } if ((ulong)_array.Length != _dataset.Dataspace.DimensionProperties[0].CurrentSize) { throw new Hdf5ArraySizeMismatchException(); } var datasetId = H5O.open(_dataset.FileId.Value, _dataset.Path.FullPath).ToId(); GCHandle arrayHandle = GCHandle.Alloc(_array, GCHandleType.Pinned); var typeId = H5T.copy(_dataset.DataType.NativeType.Value).ToId(); int result = H5D.write( datasetId.Value, typeId.Value, H5S.ALL, H5S.ALL, H5P.DEFAULT, arrayHandle.AddrOfPinnedObject()); arrayHandle.Free(); H5T.close(typeId.Value); H5O.close(datasetId.Value); FileHelper.FlushToFile(_dataset.FileId); }
public void Put(Array data, ulong[] location = null) { ulong[] shape = data.Shape(); WithDataSpace((h5Ref, dsRef) => { long memDataSpace = H5S.ALL; if (location != null) { int selection = H5S.select_none(dsRef); if (selection < 0) { throw new H5SSException("Couldn't clear dataspace selection"); } ulong[] stride = Ones(shape.Length); selection = H5S.select_hyperslab(dsRef, H5S.seloper_t.SET, location, stride, stride, shape ); if (selection < 0) { throw new H5SSException("Couldn't select hyperslab"); } memDataSpace = H5S.create_simple(shape.Length, shape, shape); } IntPtr iPtr; var effectiveSize = data.Length * ElementSize; //if (DataType == HDF5DataType.String) //{ // // Convert to byte array... //} //else //{ //} var dtype = H5D.get_type(h5Ref); // Return? iPtr = CreateNativeArray(data, dtype); // copy to unmanaged array? var success = H5D.write(h5Ref, dtype, memDataSpace, dsRef, H5P.DEFAULT, iPtr); H5T.close(dtype); if (location != null) { H5S.close(memDataSpace); } Marshal.FreeHGlobal(iPtr); if (success < 0) { throw new H5SSException(string.Format("Couldn't write to dataset: {0}", this.Path)); } }); }
public static IEnumerable <OffsetInfo> GetCompoundInfo(Type type, bool ieee = false) { //Type t = typeof(T); var strtype = H5T.copy(H5T.C_S1); int strsize = (int)H5T.get_size(strtype); int curSize = 0; List <OffsetInfo> offsets = new List <OffsetInfo>(); foreach (var x in type.GetFields()) { OffsetInfo oi = new OffsetInfo() { name = x.Name, type = x.FieldType, datatype = ieee ? GetDatatypeIEEE(x.FieldType) : GetDatatype(x.FieldType), size = x.FieldType == typeof(string) ? stringLength(x) : Marshal.SizeOf(x.FieldType), offset = 0 + curSize }; if (oi.datatype == H5T.C_S1) { strtype = H5T.copy(H5T.C_S1); H5T.set_size(strtype, new IntPtr(oi.size)); oi.datatype = strtype; } if (oi.datatype == H5T.STD_I64BE) { oi.size = oi.size * 2; } curSize = curSize + oi.size; offsets.Add(oi); } H5T.close(strtype); return(offsets); }
public void H5TcloseTest1() { hid_t dtype = H5T.copy(H5T.IEEE_F64LE); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.close(dtype) >= 0); }
public static T ReadEnumAttribute <T>(hid_t hid, string key) where T : struct, IConvertible { var attribute = H5A.open(hid, key); if (attribute < 0) { throw new ArgumentException(string.Format("Attribute {0} not found.", key)); } var type = H5A.get_type(attribute); if (type < 0) { H5A.close(attribute); throw new Exception("H5A.get_type failed."); } var size = H5T.get_size(type).ToInt32(); if (size == 0) { H5T.close(type); H5A.close(attribute); throw new Exception("H5T.get_size failed."); } var unmanagedBuffer = new UnmanagedBuffer(size); H5A.read(attribute, type, unmanagedBuffer); H5T.close(type); H5A.close(attribute); return(unmanagedBuffer.ReadEnum <T>()); }
public void H5DwriteTest1() { string utf8string = "Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο"; byte[] wdata = Encoding.UTF8.GetBytes(utf8string); hid_t dtype = H5T.create(H5T.class_t.STRING, new IntPtr(wdata.Length)); Assert.IsTrue(H5T.set_cset(dtype, H5T.cset_t.UTF8) >= 0); Assert.IsTrue(H5T.set_strpad(dtype, H5T.str_t.SPACEPAD) >= 0); hid_t dset_v0 = H5D.create(m_v0_test_file, "dset", dtype, m_space_scalar); Assert.IsTrue(dset_v0 >= 0); hid_t dset_v2 = H5D.create(m_v2_test_file, "dset", dtype, m_space_scalar); Assert.IsTrue(dset_v2 >= 0); GCHandle hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); Assert.IsTrue(H5D.write(dset_v0, dtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); Assert.IsTrue(H5D.write(dset_v2, dtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); hnd.Free(); Assert.IsTrue(H5T.close(dtype) >= 0); Assert.IsTrue(H5D.close(dset_v2) >= 0); Assert.IsTrue(H5D.close(dset_v0) >= 0); }
public void H5DreadTest1() { byte[] rdata = new byte[512]; hid_t mem_type = H5T.copy(H5T.C_S1); Assert.IsTrue(H5T.set_size(mem_type, new IntPtr(2)) >= 0); GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); Assert.IsTrue(H5D.read(m_v0_ascii_dset, mem_type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); for (int i = 0; i < 256; ++i) { // H5T.FORTRAN_S1 is space (= ASCII dec. 32) padded if (i != 32) { Assert.IsTrue(rdata[2 * i] == (byte)i); } else { Assert.IsTrue(rdata[64] == (byte)0); } Assert.IsTrue(rdata[2 * i + 1] == (byte)0); } hnd.Free(); Assert.IsTrue(H5T.close(mem_type) >= 0); }
public static int WritePrimitiveAttribute <T>(hid_t groupId, string name, Array attributes, string datasetName = null) //where T : struct { var tmpId = groupId; if (!string.IsNullOrWhiteSpace(datasetName)) { var datasetId = H5D.open(groupId, datasetName); if (datasetId > 0) { groupId = datasetId; } } int rank = attributes.Rank; ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)attributes.GetLength(i)); }).ToArray(); ulong[] maxDims = null; var spaceId = H5S.create_simple(rank, dims, maxDims); var datatype = GetDatatype(typeof(T)); var typeId = H5T.copy(datatype); var attributeId = H5A.create(groupId, name, datatype, spaceId); GCHandle hnd = GCHandle.Alloc(attributes, GCHandleType.Pinned); var result = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject()); hnd.Free(); H5A.close(attributeId); H5S.close(spaceId); H5T.close(typeId); if (tmpId != groupId) { H5D.close(groupId); } return(result); }
private static void WriteAttribute(H5ObjectWithAttributes target, string name, string value) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Name must not be empty (or null)", "name"); } if (string.IsNullOrEmpty(value)) { throw new ArgumentException("Value must not be empty or null", "value"); } H5DataTypeId dtype; byte[] strdata = EncodeStringData(value, out dtype); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5AttributeId attributeId = H5A.create(target, name, dtype, spaceId); H5A.write(attributeId, dtype, new H5Array <byte>(strdata)); H5A.close(attributeId); H5T.close(dtype); H5S.close(spaceId); }
public static int WriteDatasetFromArray <T>(hid_t groupId, string name, Array dset, string datasetName = null) //where T : struct { int rank = dset.Rank; ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)dset.GetLength(i)); }).ToArray(); ulong[] maxDims = null; var spaceId = H5S.create_simple(rank, dims, maxDims); var datatype = GetDatatype(typeof(T)); var typeId = H5T.copy(datatype); if (datatype == H5T.C_S1) { H5T.set_size(datatype, new IntPtr(2)); } var datasetId = H5D.create(groupId, name, datatype, spaceId); GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned); var result = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5D.close(datasetId); H5S.close(spaceId); H5T.close(typeId); return(result); }
private static double[,] ReadDataArray(hid_t fileLoc, string name, bool transpose = false) { hid_t dset = H5D.open(fileLoc, name); hid_t fspace = H5D.get_space(dset); hid_t count = H5S.get_simple_extent_ndims(fspace); hid_t type = H5D.get_type(dset); hsize_t[] dims = new hsize_t[count]; hsize_t[] maxdims = new hsize_t[count]; H5S.get_simple_extent_dims(fspace, dims, maxdims); H5S.close(fspace); byte[] rdata = new byte[dims[0] * dims[1] * 8]; hid_t mem_type = H5T.copy(H5T.NATIVE_DOUBLE); H5T.set_size(mem_type, new IntPtr(8)); GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); H5D.read(dset, mem_type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5T.close(mem_type); if (transpose) { double[,] val = new double[dims[1], dims[0]]; int cnt = 0; for (int i = 0; i < (int)dims[0]; i++) { for (int j = 0; j < (int)dims[1]; j++) { val[j, i] = BitConverter.ToDouble(rdata, cnt * 8); cnt++; } } return(val); } else { double[,] val = new double[dims[0], dims[1]]; int cnt = 0; for (int i = 0; i < (int)dims[0]; i++) { for (int j = 0; j < (int)dims[1]; j++) { val[i, j] = BitConverter.ToDouble(rdata, cnt * 8); cnt++; } } return(val); } }
public static (int success, hid_t CreatedgroupId) WriteStringAttributes(hid_t groupId, string name, IEnumerable <string> strs, string datasetName = null) { hid_t tmpId = groupId; if (!string.IsNullOrWhiteSpace(datasetName)) { hid_t datasetId = H5D.open(groupId, datasetName); if (datasetId > 0) { groupId = datasetId; } } // create UTF-8 encoded attributes hid_t datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); H5T.set_cset(datatype, H5T.cset_t.UTF8); H5T.set_strpad(datatype, H5T.str_t.SPACEPAD); int strSz = strs.Count(); hid_t spaceId = H5S.create_simple(1, new ulong[] { (ulong)strSz }, null); var attributeId = H5A.create(groupId, name, datatype, spaceId); GCHandle[] hnds = new GCHandle[strSz]; IntPtr[] wdata = new IntPtr[strSz]; int cntr = 0; foreach (string str in strs) { hnds[cntr] = GCHandle.Alloc( Encoding.UTF8.GetBytes(str), GCHandleType.Pinned); wdata[cntr] = hnds[cntr].AddrOfPinnedObject(); cntr++; } var hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); var result = H5A.write(attributeId, datatype, hnd.AddrOfPinnedObject()); hnd.Free(); for (int i = 0; i < strSz; ++i) { hnds[i].Free(); } H5A.close(attributeId); H5S.close(spaceId); H5T.close(datatype); if (tmpId != groupId) { H5D.close(groupId); } return(result, attributeId); }
public Dictionary <string, string> TryReadDataTable(string datasetName) { Dictionary <string, string> result = null; var subDsDic = ds.GetSubDatasets(); if (subDsDic.Count > 0) { H5ID h5FileId = H5F.open(fileName, H5F.ACC_RDONLY); H5ID datasetId = H5D.open(h5FileId, datasetName); H5ID typeId = H5D.get_type(datasetId); H5ID spaceId = H5D.get_space(datasetId); if (H5T.get_class(typeId) == H5T.class_t.COMPOUND) { int numCount = H5T.get_nmembers(typeId); var size = H5T.get_size(typeId); byte[] buffer = new byte[size.ToInt32()]; GCHandle hnd = GCHandle.Alloc(buffer, GCHandleType.Pinned); int ndims = H5S.get_simple_extent_ndims(spaceId); if (ndims == 1) { result = new Dictionary <string, string>(); H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); for (uint i = 0; i < numCount; i++) { string name = Marshal.PtrToStringAnsi(H5T.get_member_name(typeId, i)); int offset = H5T.get_member_offset(typeId, i).ToInt32(); H5ID subTypeId = H5T.get_member_type(typeId, i); H5T.class_t typeClass = H5T.get_member_class(typeId, i); string value = ReadBuffer(buffer, offset, typeClass, subTypeId); result.Add(name, value); H5T.close(subTypeId); } } hnd.Free(); } if (spaceId != 0) { H5S.close(spaceId); } if (typeId != 0) { H5T.close(typeId); } if (datasetId != 0) { H5D.close(datasetId); } if (h5FileId != 0) { H5F.close(h5FileId); } } return(result); }
private static void WriteFile(string filePath) { var file = H5F.create(filePath, H5F.CreateMode.ACC_TRUNC); var group = H5G.create(file, "/group"); H5G.close(group); const int RANK = 2; const int DIM0 = 3; const int DIM1 = 4; var dims = new long[RANK] { DIM0, DIM1 }; var dataSpace = H5S.create_simple(RANK, dims); var dataSet = H5D.create(file, "/group/dataset", H5T.H5Type.NATIVE_INT, dataSpace); H5S.close(dataSpace); var data = new int[DIM0, DIM1] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; H5D.write(dataSet, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(data)); var dataType = new H5DataTypeId(H5T.H5Type.NATIVE_INT); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var integerAttribute = H5A.create(dataSet, "int", dataType, dataSpace); H5A.write(integerAttribute, dataType, new H5Array <int>(new int[1] { 42 })); H5A.close(integerAttribute); H5S.close(dataSpace); //H5T.close(dataType); // Read-only. var str = "Hello, world!"; var strBytes = Encoding.ASCII.GetBytes(str); // There is a H5T.get_cset, but there does not seem to be a way of setting the character encoding, i.e. set_cset. dataType = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(dataType, strBytes.Length); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var stringAttribute = H5A.create(dataSet, "string", dataType, dataSpace); H5A.write(stringAttribute, dataType, new H5Array <byte>(strBytes)); H5A.close(stringAttribute); H5S.close(dataSpace); H5T.close(dataType); H5D.close(dataSet); H5F.close(file); }
public void H5TcreateTest1() { hid_t dtype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.is_variable_str(dtype) > 0); Assert.IsTrue(H5T.close(dtype) >= 0); }
public static Hdf5Dataset CreateDataset( Hdf5Identifier _fileId, Hdf5Path _parentPath, string _name, Hdf5DataTypes _datatype, int _numberOfDimensions, List <Hdf5DimensionProperty> _properties) { Hdf5Path path = _parentPath.Append(_name); UInt64[] dimensionSize = new UInt64[_numberOfDimensions]; UInt64[] maxSize = null; // new UInt64[_numberOfDimensions]; int i = 0; foreach (var property in _properties) { dimensionSize[i] = property.CurrentSize; //if (property.MaximumSize == UInt64.MaxValue) //{ // maxSize[i] = H5S.UNLIMITED; //} //else //{ // maxSize[i] = property.MaximumSize; //} i++; } Hdf5Identifier dataspaceId = H5S.create_simple(_numberOfDimensions, dimensionSize, maxSize).ToId(); //TODO handle string datasets Hdf5Identifier typeId = H5T.copy(TypeHelper.GetNativeType(_datatype).Value).ToId(); var status = H5T.set_order(typeId.Value, H5T.order_t.LE); Hdf5Identifier datasetId = H5D.create(_fileId.Value, path.FullPath, typeId.Value, dataspaceId.Value).ToId(); Hdf5Dataset dataset = null; if (datasetId.Value > 0) { dataset = new Hdf5Dataset(_fileId, datasetId, path.FullPath) { DataType = TypeHelper.GetDataTypeFromDataset(datasetId), Dataspace = DataspaceHelper.GetDataspace(datasetId) }; H5D.close(datasetId.Value); } H5T.close(typeId.Value); FileHelper.FlushToFile(_fileId); return(dataset); }
public static unsafe void AddString(long fileId, ContainerType container) { long res; var dims = new ulong[] { 2, 2, 3 }; /* "extendible contiguous non-external dataset not allowed" */ // fixed length string attribute (ASCII) var typeIdFixed = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdFixed, new IntPtr(2)); res = H5T.set_cset(typeIdFixed, H5T.cset_t.ASCII); var dataFixed = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "AA", "ZZ", "!!" }; var dataFixedChar = dataFixed .SelectMany(value => Encoding.ASCII.GetBytes(value)) .ToArray(); TestUtils.Add(container, fileId, "string", "fixed", typeIdFixed, dataFixedChar.AsSpan(), dims); res = H5T.close(typeIdFixed); // variable length string attribute (ASCII) var typeIdVar = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdVar, H5T.VARIABLE); res = H5T.set_cset(typeIdVar, H5T.cset_t.ASCII); var dataVar = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "AA", "ZZ", "!!" }; var dataVarIntPtr = dataVar.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray(); TestUtils.Add(container, fileId, "string", "variable", typeIdVar, dataVarIntPtr.AsSpan(), dims); foreach (var ptr in dataVarIntPtr) { Marshal.FreeCoTaskMem(ptr); } res = H5T.close(typeIdVar); // variable length string attribute (UTF8) var typeIdVarUTF8 = H5T.copy(H5T.C_S1); res = H5T.set_size(typeIdVarUTF8, H5T.VARIABLE); res = H5T.set_cset(typeIdVarUTF8, H5T.cset_t.UTF8); var dataVarUTF8 = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", " ", "ÄÄ", "的的", "!!" }; var dataVarUTF8IntPtr = dataVarUTF8.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray(); TestUtils.Add(container, fileId, "string", "variableUTF8", typeIdVarUTF8, dataVarUTF8IntPtr.AsSpan(), dims); foreach (var ptr in dataVarUTF8IntPtr) { Marshal.FreeCoTaskMem(ptr); } res = H5T.close(typeIdVarUTF8); }
public static (bool success, IEnumerable <string> result) ReadStrings(long groupId, string name, string alternativeName) { long datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE); H5T.set_cset(datatype, H5T.cset_t.UTF8); H5T.set_strpad(datatype, H5T.str_t.NULLTERM); //name = ToHdf5Name(name); var datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(name)); if (datasetId < 0) //does not exist? { datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(alternativeName)); } if (datasetId <= 0) { Hdf5Utils.LogError?.Invoke($"Error reading {groupId}. Name:{name}. AlternativeName:{alternativeName}"); return(false, Array.Empty <string>()); } long spaceId = H5D.get_space(datasetId); long count = H5S.get_simple_extent_npoints(spaceId); H5S.close(spaceId); var strs = new List <string>(); if (count >= 0) { IntPtr[] rdata = new IntPtr[count]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); H5D.read(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); for (int i = 0; i < rdata.Length; ++i) { int len = 0; while (Marshal.ReadByte(rdata[i], len) != 0) { ++len; } byte[] buffer = new byte[len]; Marshal.Copy(rdata[i], buffer, 0, buffer.Length); string s = Encoding.UTF8.GetString(buffer); strs.Add(s); // H5.free_memory(rdata[i]); } hnd.Free(); } H5T.close(datatype); H5D.close(datasetId); return(true, strs); }
public override void Dispose() { base.Dispose(); if (ID > 0) { ID = H5T.close(ID); } }
public void H5Tarray_createTest1() { hsize_t[] dims = new hsize_t[] { 3, 3 }; hid_t dtype = H5T.array_create(H5T.IEEE_F64LE, (uint)dims.Length, dims); Assert.IsTrue(dtype >= 0); Assert.IsTrue(H5T.close(dtype) >= 0); }