public void H5DflushTestSWMR2() { hsize_t[] dims = { 6, 6 }; hsize_t[] maxdims = { 6, H5S.UNLIMITED }; hsize_t[] chunk_dims = { 2, 5 }; int[] cbuf = new int[36]; hid_t dsp = H5S.create_simple(2, dims, maxdims); Assert.IsTrue(dsp >= 0); hid_t dcpl = H5P.create(H5P.DATASET_CREATE); Assert.IsTrue(dcpl >= 0); Assert.IsTrue(H5P.set_chunk(dcpl, 2, chunk_dims) >= 0); hid_t dst = H5D.create(m_v3_test_file_swmr, "dset", H5T.NATIVE_INT, dsp, H5P.DEFAULT, dcpl); Assert.IsTrue(dst >= 0); GCHandle hnd = GCHandle.Alloc(cbuf, GCHandleType.Pinned); Assert.IsTrue(H5D.write(dst, H5T.NATIVE_INT, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0); hnd.Free(); Assert.IsTrue(H5D.flush(dst) >= 0); Assert.IsTrue(H5D.close(dst) >= 0); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5S.close(dsp) >= 0); }
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 void AppendOrCreateDataset(Array dataset) { if (_chunkDims == null) { _chunkDims = new[] { Convert.ToUInt64(dataset.GetLongLength(0)), Convert.ToUInt64(dataset.GetLongLength(1)) }; Rank = dataset.Rank; _currentDims = GetDims(dataset); /* Create the data space with unlimited dimensions. */ _spaceId = H5S.create_simple(Rank, _currentDims, _maxDims); /* Modify dataset creation properties, i.e. enable chunking */ _propId = H5P.create(H5P.DATASET_CREATE); _status = H5P.set_chunk(_propId, Rank, _chunkDims); /* Create a new dataset within the file using chunk creation properties. */ _datasetId = H5D.create(GroupId, Hdf5Utils.NormalizedName(Datasetname), _datatype, _spaceId, H5P.DEFAULT, _propId); /* Write data to dataset */ GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned); _status = H5D.write(_datasetId, _datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5S.close(_spaceId); _spaceId = -1; } else { AppendDataset(dataset); } }
public void FirstDataset(Array dataset) { if (FalseGroupId) { throw new Exception("cannot call FirstDataset because group or file couldn't be created"); } if (DatasetExists) { throw new Exception("cannot call FirstDataset because dataset already exists"); } Rank = dataset.Rank; currentDims = GetDims(dataset); /* Create the data space with unlimited dimensions. */ spaceId = H5S.create_simple(Rank, currentDims, maxDims); /* Modify dataset creation properties, i.e. enable chunking */ propId = H5P.create(H5P.DATASET_CREATE); status = H5P.set_chunk(propId, Rank, chunkDims); /* Create a new dataset within the file using chunk creation properties. */ datasetId = H5D.create(GroupId, Datasetname, datatype, spaceId, H5P.DEFAULT, propId, H5P.DEFAULT); /* Write data to dataset */ GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned); status = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5S.close(spaceId); }
public static unsafe void AddDataspaceNull(long fileId, ContainerType container) { var spaceId = H5S.create(H5S.class_t.NULL); TestUtils.Add(container, fileId, "dataspace", "null", H5T.NATIVE_DOUBLE, null, spaceId); H5S.close(spaceId); }
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); }
private static void AppendData <T>(hid_t dataSet, T[] data, ref int rows) { var fileSpace = H5D.get_space(dataSet); if (fileSpace < 0) { throw new Exception("Failed to get data space of data set."); } var offset = new ulong[] { (ulong)rows }; var count = new ulong[] { (ulong)data.Length }; if (H5S.select_hyperslab(fileSpace, H5S.seloper_t.SET, offset, null, count, null) < 0) { throw new Exception("H5S.select_hyperslab failed."); } var memSpace = H5S.create_simple(1, count, null); if (memSpace < 0) { throw new Exception("H5S.create_simple failed."); } if (H5D.write(dataSet, NumericTypeToHDF5Type <T>(), memSpace, fileSpace, H5P.DEFAULT, new PinnedObject(data)) < 0) { throw new Exception("H5D.write failed."); } H5S.close(memSpace); H5S.close(fileSpace); rows += data.Length; }
public void H5Dget_spaceTest1() { 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 space1 = H5D.get_space(dset); Assert.IsTrue(space1 >= 0); Assert.IsTrue(H5S.extent_equal(space, space1) > 0); Assert.IsTrue(H5S.close(space1) >= 0); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create(m_v2_test_file, "dset", H5T.STD_I16LE, space); Assert.IsTrue(dset >= 0); space1 = H5D.get_space(dset); Assert.IsTrue(space1 >= 0); Assert.IsTrue(H5S.extent_equal(space, space1) > 0); Assert.IsTrue(H5S.close(space1) >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
/// <summary> /// Writes the entire matrix table /// </summary> /// <typeparam name="T"></typeparam> /// <param name="matName"></param> /// <param name="rowIndex"></param> /// <returns></returns> public void SetMatrix <T>(string matName, T[,] data) { // check that matrix exists if (tables.ContainsKey(matName)) { H5DataSetId matId; tables.TryGetValue(matName, out matId); H5DataTypeId matDataId = H5D.getType(matId); H5DataSpaceId spaceId = H5S.create_simple(2, Shape); long[] start = { 0, 0 }; long[] count = { Shape[0], Shape[1] }; var h5matrix = new H5Array <T>(data); H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count); H5DataSpaceId readSpaceId = H5S.create_simple(2, count); H5D.write(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix); H5S.close(spaceId); H5S.close(readSpaceId); } else { Console.WriteLine("table {0} not found in matrix file", matName); } return; }
// Matrix Specific Methods // TODO: // 1. add handling for matrix title // 2. add specification of NA values // 3. other attributes: pa-format flag, year int, source string /// <summary> /// Returns a row of the matrix /// </summary> /// <typeparam name="T"></typeparam> /// <param name="matName"></param> /// <param name="rowIndex"></param> /// <returns></returns> public T[] GetMatrixRow <T>(string matName, int rowIndex) { var rowData = new T[Shape[1]]; // check that matrix exists if (tables.ContainsKey(matName)) { H5DataSetId matId; tables.TryGetValue(matName, out matId); H5DataTypeId matDataId = H5D.getType(matId); H5DataSpaceId spaceId = H5S.create_simple(2, Shape); var h5matrix = new H5Array <T>(rowData); long[] start = { rowIndex, 0 }; long[] count = { 1, Shape[1] }; H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count); H5DataSpaceId readSpaceId = H5S.create_simple(2, count); H5D.read(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix); H5S.close(spaceId); H5S.close(readSpaceId); } else { Console.WriteLine("table {0} not found in matrix file", matName); } return(rowData); }
public void H5DOappendTestSWMR2() { hsize_t[] dims = { 0 }; hsize_t[] maxdims = { H5S.UNLIMITED }; hsize_t[] chunk_dims = { 10 }; uint[] cbuf = { 123, 456, 789 }; hid_t dsp = H5S.create_simple(1, dims, maxdims); Assert.IsTrue(dsp >= 0); hid_t dcpl = H5P.create(H5P.DATASET_CREATE); Assert.IsTrue(dcpl >= 0); Assert.IsTrue(H5P.set_chunk(dcpl, 1, chunk_dims) >= 0); hid_t dst = H5D.create(m_v3_test_file_no_swmr, "dset1", H5T.NATIVE_UINT, dsp, H5P.DEFAULT, dcpl, H5P.DEFAULT); Assert.IsTrue(dst >= 0); GCHandle hnd = GCHandle.Alloc(cbuf, GCHandleType.Pinned); Assert.IsTrue( H5DO.append(dst, H5P.DEFAULT, 0, new IntPtr(3), H5T.NATIVE_UINT, hnd.AddrOfPinnedObject()) >= 0); hnd.Free(); Assert.IsTrue(H5D.close(dst) >= 0); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5S.close(dsp) >= 0); }
public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, ulong length, ulong[] dimensionLimitSet) { return(IOHelper.OpenOrCreateAttribute(locationId, name, attributeTypeId, () => { long dataspaceId = -1; long attributeId = -1; try { dataspaceId = H5S.create_simple(1, new ulong[] { length }, dimensionLimitSet); attributeId = H5A.create(locationId, name, attributeTypeId, dataspaceId); if (H5I.is_valid(attributeId) <= 0) { throw new Exception(ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute); } } finally { if (H5I.is_valid(dataspaceId) > 0) { H5S.close(dataspaceId); } } return attributeId; })); }
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 void H5Pget_virtual_vspaceTestVDS1() { hid_t vds = H5D.open(m_vds_class_file, "VDS"); Assert.IsTrue(vds >= 0); hid_t dcpl = H5D.get_create_plist(vds); Assert.IsTrue(dcpl >= 0); IntPtr count = IntPtr.Zero; Assert.IsTrue(H5P.get_virtual_count(dcpl, ref count) >= 0); Assert.IsTrue(3 == count.ToInt32()); for (int i = 0; i < count.ToInt32(); ++i) { size_t index = new size_t(i); hid_t vspace = H5P.get_virtual_vspace(dcpl, index); Assert.IsTrue(vspace >= 0); Assert.IsTrue(H5S.is_regular_hyperslab(vspace) > 0); Assert.IsTrue(H5S.close(vspace) >= 0); } Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5D.close(vds) >= 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 bool setOMXFileAttributes() { // write OMX attributes H5DataSpaceId dspace; H5DataTypeId dtype; H5AttributeId attr; // OMX version dspace = H5S.create(H5S.H5SClass.SCALAR); dtype = H5T.copy(H5T.H5Type.C_S1); // string datatype H5T.setSize(dtype, dllVersion[0].Length); attr = H5A.create(fileId, omxVersionName, dtype, dspace); ASCIIEncoding ascii = new ASCIIEncoding(); H5A.write(attr, dtype, new H5Array <System.Byte>(ascii.GetBytes(dllVersion[0]))); H5A.close(attr); // OMX shape - only 2D tables dspace = H5S.create_simple(1, new long[] { 2 }); dtype = H5T.copy(H5T.H5Type.NATIVE_INT); attr = H5A.create(fileId, omxShapeAttr, dtype, dspace); int[] shape = new int[2]; shape[0] = (int)Shape[0]; shape[1] = (int)Shape[1]; H5A.write <int>(attr, dtype, new H5Array <int>(shape)); H5S.close(dspace); H5A.close(attr); return(true); }
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); }
public void H5Dget_storage_sizeTest1() { hsize_t[] dims = { 1024, 2048 }; hid_t space = H5S.create_simple(2, dims, null); hid_t dcpl = H5P.create(H5P.DATASET_CREATE); Assert.IsTrue(dcpl >= 0); Assert.IsTrue( H5P.set_alloc_time(dcpl, H5D.alloc_time_t.EARLY) >= 0); hid_t dset = H5D.create(m_v0_test_file, "dset", H5T.STD_I16LE, space, H5P.DEFAULT, dcpl); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.get_storage_size(dset) == 4194304); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create(m_v2_test_file, "dset", H5T.STD_I16LE, space, H5P.DEFAULT, dcpl); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.get_storage_size(dset) == 4194304); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
private void Write(H5GroupId parent, string name, IEnumerable <IMeasurement> measurements) { H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)measurements.Count() }); // Set compression options for dataset H5PropertyListId dataSetPropertyList = H5P.create(H5P.PropertyListClass.DATASET_CREATE); H5P.setDeflate(dataSetPropertyList, NumericDataCompression); H5P.setChunk(dataSetPropertyList, new long[] { (long)measurements.Count() }); H5DataSetId dataSetId = H5D.create(parent, name, measurement_t, spaceId, new H5PropertyListId(H5P.Template.DEFAULT), dataSetPropertyList, new H5PropertyListId(H5P.Template.DEFAULT)); MeasurementT[] ms = new MeasurementT[measurements.Count()]; int ilmCount = 0; foreach (IMeasurement m in measurements) { MeasurementT mt = Convert(m); ms[ilmCount++] = mt; } H5D.write <MeasurementT>(dataSetId, measurement_t, new H5Array <MeasurementT>(ms)); H5D.close(dataSetId); H5S.close(spaceId); }
// 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 void H5DfillTest1() { hsize_t[] dims = { 10 }; hid_t space = H5S.create_simple(1, dims, null); Assert.IsTrue(H5S.select_all(space) >= 0); double[] v = new double[10]; double fill = 1.0; GCHandle v_hnd = GCHandle.Alloc(v, GCHandleType.Pinned); GCHandle fill_hnd = GCHandle.Alloc(fill, GCHandleType.Pinned); Assert.IsTrue( H5D.fill(fill_hnd.AddrOfPinnedObject(), H5T.NATIVE_DOUBLE, v_hnd.AddrOfPinnedObject(), H5T.NATIVE_DOUBLE, space) >= 0); fill_hnd.Free(); v_hnd.Free(); for (int i = 0; i < v.Length; ++i) { Assert.IsTrue(v[i] == 1.0); } Assert.IsTrue(H5S.close(space) >= 0); }
public void H5Dcreate_anonTest2() { hsize_t[] dims = { 10, 10, 10 }; hsize_t[] max_dims = { H5S.UNLIMITED, H5S.UNLIMITED, H5S.UNLIMITED }; hid_t space = H5S.create_simple(3, dims, max_dims); hid_t dcpl = H5P.create(H5P.DATASET_CREATE); Assert.IsTrue(dcpl >= 0); hsize_t[] chunk = { 64, 64, 64 }; Assert.IsTrue(H5P.set_chunk(dcpl, 3, chunk) >= 0); Assert.IsTrue(H5P.set_deflate(dcpl, 9) >= 0); hid_t dset = H5D.create_anon(m_v0_test_file, H5T.IEEE_F32BE, space, dcpl); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create_anon(m_v2_test_file, H5T.IEEE_F32BE, space, dcpl); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
public static unsafe void AddSomeLinks(long fileId) { long res; var groupId = H5G.create(fileId, "simple"); var groupId_sub = H5G.create(groupId, "sub"); // datasets var dataspaceId1 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 }); var datasetId1 = H5D.create(fileId, "D", H5T.NATIVE_INT8, dataspaceId1); var data1 = new byte[] { 1 }; fixed(void *ptr = data1) { res = H5D.write(datasetId1, H5T.NATIVE_INT8, dataspaceId1, dataspaceId1, 0, new IntPtr(ptr)); } res = H5D.close(datasetId1); res = H5S.close(dataspaceId1); var dataspaceId2 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 }); var datasetId2 = H5D.create(groupId, "D1", H5T.NATIVE_INT8, dataspaceId2); res = H5D.close(datasetId2); res = H5S.close(dataspaceId2); var dataspaceId3 = H5S.create_simple(1, new ulong[] { 1 }, new ulong[] { 1 }); var datasetId3 = H5D.create(groupId_sub, "D1.1", H5T.NATIVE_INT8, dataspaceId3); res = H5D.close(datasetId3); res = H5S.close(dataspaceId3); res = H5G.close(groupId); res = H5G.close(groupId_sub); }
public void H5DiterateTest1() { int[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; IntPtr count_ptr = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(count_ptr, 0); hsize_t[] dims = { 10 }; hid_t space = H5S.create_simple(1, dims, null); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.select_all(space) >= 0); GCHandle buf_hnd = GCHandle.Alloc(buf, GCHandleType.Pinned); H5D.operator_t cb = DelegateMethod; Assert.IsTrue( H5D.iterate(buf_hnd.AddrOfPinnedObject(), H5T.NATIVE_INT, space, cb, count_ptr) >= 0); buf_hnd.Free(); int count = Marshal.ReadInt32(count_ptr); // expect the sum of the buffer elements Assert.IsTrue(count == 45); Assert.IsTrue(H5S.close(space) >= 0); Marshal.FreeHGlobal(count_ptr); }
/// <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 void H5Sget_select_hyper_blocklistTest1() { hsize_t[] dims = { 1, 2, 3 }; hid_t space = H5S.create_simple(dims.Length, dims, dims); Assert.IsTrue(space > 0); hsize_t[] start = { 0, 0, 0 }; hsize_t[] count = { 1, 1, 1 }; hsize_t[] block = { 1, 2, 3 }; Assert.IsTrue( H5S.select_hyperslab(space, H5S.seloper_t.SET, start, null, count, block) >= 0); Assert.IsTrue(H5S.get_select_hyper_nblocks(space) == 1); hsize_t[] buf = new hsize_t [2 * dims.Length]; Assert.IsTrue( H5S.get_select_hyper_blocklist(space, 0, 1, buf) >= 0); for (int i = 0; i < dims.Length; ++i) { buf[i] = 0; buf[i + 3] = dims[i] - 1; } Assert.IsTrue(H5S.close(space) >= 0); }
public void H5SdecodeTest1() { hsize_t[] dims = { 1, 2, 3 }; hid_t space = H5S.create_simple(dims.Length, dims, dims); Assert.IsTrue(space > 0); size_t nalloc = new IntPtr(); Assert.IsTrue(H5S.encode(space, null, ref nalloc) >= 0); byte[] buf = new byte [nalloc.ToInt32()]; Assert.IsTrue(H5S.encode(space, buf, ref nalloc) >= 0); Assert.IsTrue(H5S.close(space) >= 0); space = H5S.decode(buf); Assert.IsTrue(space >= 0); Assert.IsTrue(H5S.get_simple_extent_ndims(space) == dims.Length); hsize_t[] tdims = new hsize_t[dims.Length]; Assert.IsTrue( H5S.get_simple_extent_dims(space, tdims, null) == dims.Length); for (int i = 0; i < dims.Length; ++i) { Assert.IsTrue(tdims[i] == dims[i]); } Assert.IsTrue(H5S.close(space) >= 0); }
/// <summary> /// /// </summary> /// <returns></returns> public static Hdf5Dataspace GetDataspace(Hdf5Identifier _datasetId) { var dataspaceId = H5D.get_space(_datasetId.Value).ToId(); int rank = H5S.get_simple_extent_ndims(dataspaceId.Value); ulong[] dims = new ulong[rank]; ulong[] maxDims = new ulong[rank]; H5S.get_simple_extent_dims(dataspaceId.Value, dims, maxDims); Hdf5Dataspace dataspace = new Hdf5Dataspace { Id = dataspaceId, NumberOfDimensions = rank }; for (int i = 0; i < dims.Length; i++) { Hdf5DimensionProperty property = new Hdf5DimensionProperty { CurrentSize = dims[i], //MaximumSize = maxDims[i] }; dataspace.DimensionProperties.Add(property); } H5S.close(dataspaceId.Value); return(dataspace); }
/// <summary> /// Dispose function as suggested in the stackoverflow discussion below /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238 /// </summary> /// <param name="itIsSafeToAlsoFreeManagedObjects"></param> protected virtual void Dispose(bool itIsSafeToAlsoFreeManagedObjects) { if (!Hdf5Utils.GetRealName(GroupId, Datasetname, string.Empty).valid) { Hdf5Utils.LogInfo?.Invoke("Dataset does not exist."); return; } if (_datasetId >= 0) { H5D.close(_datasetId); } if (_propId >= 0) { H5P.close(_propId); } if (_spaceId >= 0) { H5S.close(_spaceId); } if (itIsSafeToAlsoFreeManagedObjects) { } }
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); }