public void H5DOappendTestSWMR1() { hsize_t[] dims = { 6, 0 }; hsize_t[] maxdims = { 6, H5S.UNLIMITED }; hsize_t[] chunk_dims = { 2, 5 }; int[] cbuf = new int [6]; 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); hsize_t[] boundary = { 0, 1 }; hid_t dapl = H5P.create(H5P.DATASET_ACCESS); Assert.IsTrue(dapl >= 0); H5D.append_cb_t cb = DOappend_func; Assert.IsTrue( H5P.set_append_flush(dapl, 2, boundary, cb, new IntPtr(99)) >= 0); hid_t dst = H5D.create(m_v3_test_file_swmr, "dset", H5T.NATIVE_INT, dsp, H5P.DEFAULT, dcpl, dapl); Assert.IsTrue(dst >= 0); GCHandle hnd = GCHandle.Alloc(cbuf, GCHandleType.Pinned); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 6; ++j) { cbuf[j] = ((i * 6) + (j + 1)) * -1; } Assert.IsTrue( H5DO.append(dst, H5P.DEFAULT, 1, new IntPtr(1), H5T.NATIVE_INT, hnd.AddrOfPinnedObject()) >= 0); } hnd.Free(); Assert.IsTrue(H5D.close(dst) >= 0); Assert.IsTrue(H5P.close(dapl) >= 0); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5S.close(dsp) >= 0); }
public void AppendOrCreateDataset(Array dataset) { if (_chunkDims == null) { if (dataset.Rank < 1) { string msg = "Empty array was passed. Ignoring."; Hdf5Utils.LogError?.Invoke(msg); return; } for (int dimension = 1; dimension <= dataset.Rank; dimension++) { var size = dataset.GetUpperBound(dimension - 1) + 1; if (size == 0) { string msg = $"Empty array was passed for dimension {dimension}. Ignoring."; Hdf5Utils.LogError?.Invoke(msg); return; } } _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); } }
/// <summary> /// Reads an n-dimensional dataSet. /// </summary> /// <typeparam name="T">Generic parameter strings or primitive type</typeparam> /// <param name="groupId">id of the group. Can also be a file Id</param> /// <param name="name">name of the dataSet</param> /// <returns>The n-dimensional dataSet</returns> public static Array ReadDataSetToArray <T>(long groupId, string name) { var dataType = GetDataType(typeof(T)); var dataSetId = H5D.open(groupId, name); var spaceId = H5D.get_space(dataSetId); int rank = H5S.get_simple_extent_ndims(spaceId); long count = H5S.get_simple_extent_npoints(spaceId); Array dset; Type type = typeof(T); if (rank >= 0 && count >= 0) { int rankChunk; ulong[] maxDims = new ulong[rank]; ulong[] dims = new ulong[rank]; ulong[] chunkDims = new ulong[rank]; long memId = H5S.get_simple_extent_dims(spaceId, dims, maxDims); long[] lengths = dims.Select(Convert.ToInt64).ToArray(); dset = Array.CreateInstance(type, lengths); var typeId = H5D.get_type(dataSetId); var mem_type = H5T.copy(dataType); if (dataType == H5T.C_S1) { H5T.set_size(dataType, new IntPtr(2)); } var propId = H5D.get_create_plist(dataSetId); if (H5D.layout_t.CHUNKED == H5P.get_layout(propId)) { rankChunk = H5P.get_chunk(propId, rank, chunkDims); } memId = H5S.create_simple(rank, dims, maxDims); GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned); H5D.read(dataSetId, dataType, memId, spaceId, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); } else { dset = Array.CreateInstance(type, new long[1] { 0 }); } H5D.close(dataSetId); H5S.close(spaceId); return(dset); }
public static (bool success, IEnumerable <string> result) ReadStrings(long groupId, string name, string alternativeName) { 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 typeId = H5D.get_type(datasetId); 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, typeId, 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 = Hdf5Utils.ReadStringBuffer(buffer); strs.Add(s); // H5.free_memory(rdata[i]); } hnd.Free(); } H5T.close(typeId); H5D.close(datasetId); return(true, strs); }
/// <summary> /// Create a new hdf5 `H5DataSet` at `loc_id`. /// </summary> /// <remarks> /// `maxdims` may be `null` in which case it is set to `dims`. /// </remarks> internal static H5DataSet Create(hid_t loc_id, string key, int rank, long[] dims, long[] maxdims, Type primitive_type) { hid_t dcpl; // the 'dataset creation property list' controls chunking.. if (maxdims == null || dims.SequenceEqual(maxdims)) { dcpl = H5P.DEFAULT; } else if (HasH5Pcreate) { // ..which is needed for later resizing: var chunk = new ulong[rank]; // the chunk is of size 1 in each 'unlimited' dimension and of size 'maxdims' // for all other dimensions (just like the 'SPECdata/Intensities' dataset): for (int i = 0; i < rank; i++) { if (maxdims[i] == H5Space.Unlimited) { chunk[i] = 1UL; } else if (maxdims[i] > 0) { checked { chunk[i] = (ulong)maxdims[i]; } } else { throw new ArgumentException($"invalid value in parameter 'maxdims'"); } } dcpl = H5P.create(H5P.DATASET_CREATE); H5P.set_chunk(dcpl, rank, chunk); } else { maxdims = dims; dcpl = H5P.DEFAULT; } hid_t id; using (H5Space space = H5Space.Create(rank, dims, maxdims)) using (H5Type dtype = H5Type.Create(primitive_type)) { if ((id = H5D.create(loc_id, key, dtype.ID, space.ID, H5P.DEFAULT, dcpl, H5P.DEFAULT)) < 0) { throw new H5LibraryException($"H5D.create() returned ({id})"); } } return(FromID(id)); }
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)); } long dcpl = H5P.DEFAULT; if (datatype != H5T.C_S1) { Console.WriteLine("Do you want to compress the file? (Y/N)"); string response = "N"; response = Console.ReadLine(); if (response == "Y" || response == "y") { var cnt = dset.Length; 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[] { pow }; //chunk_size[0] = ulong.Parse(Console.ReadLine()); Console.WriteLine("Auto-Chunking with chunk-size of {0} samples.", pow); dcpl = create_property(chunk_size); } else { Console.WriteLine("Creating an Uncompressed File..."); } } var datasetId = H5D.create(groupId, name, datatype, spaceId, H5P.DEFAULT, dcpl); 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_num_chunksTest1() { hsize_t[] dims = { 10, 10 }; hsize_t[] max_dims = { H5S.UNLIMITED, H5S.UNLIMITED }; hid_t space = H5S.create_simple(2, dims, max_dims); hid_t dcpl = H5P.create(H5P.DATASET_CREATE); Assert.IsTrue(dcpl >= 0); hsize_t[] chunk = { 4, 4 }; Assert.IsTrue(H5P.set_chunk(dcpl, 2, chunk) >= 0); Assert.IsTrue(H5P.set_alloc_time(dcpl, H5D.alloc_time_t.EARLY) >= 0); Assert.IsTrue(H5P.set_fill_time(dcpl, H5D.fill_time_t.ALLOC) >= 0); hid_t dset = H5D.create(m_v0_test_file, "Early Bird", H5T.IEEE_F32BE, space, H5P.DEFAULT, dcpl); Assert.IsTrue(dset >= 0); // This should work but doesn't: // Assert.IsTrue(H5D.get_num_chunks(dset, H5S.ALL, ref nchunks) >= 0); hid_t fspace = H5D.get_space(dset); Assert.IsTrue(fspace >= 0); Assert.IsTrue(H5S.select_all(fspace) >= 0); hsize_t nchunks = 0; Assert.IsTrue(H5D.get_num_chunks(dset, fspace, ref nchunks) >= 0); Assert.IsTrue(nchunks == 9); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create(m_v2_test_file, "Early Bird", H5T.IEEE_F32BE, space, H5P.DEFAULT, dcpl); Assert.IsTrue(dset >= 0); // This should work but doesn't: // Assert.IsTrue(H5D.get_num_chunks(dset, H5S.ALL, ref nchunks) >= 0); fspace = H5D.get_space(dset); Assert.IsTrue(fspace >= 0); Assert.IsTrue(H5S.select_all(fspace) >= 0); nchunks = 0; Assert.IsTrue(H5D.get_num_chunks(dset, fspace, ref nchunks) >= 0); Assert.IsTrue(nchunks == 9); Assert.IsTrue(H5D.close(dset) >= 0); }
/// <summary> /// 读取指定数据集,未对异常进行处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datasetName"></param> /// <param name="bandN"></param> /// <param name="bandH"></param> /// <param name="bandW"></param> /// <returns></returns> public T[] ReadDataArray <T>(String datasetName, ref int bandN, ref int bandH, ref int bandW) { H5DataSetId datasetId = null; H5DataSpaceId spaceId = null; H5DataTypeId typeId = null; long[] dims = null; if (!String.IsNullOrEmpty(datasetName) && _datasetNames.Contains(datasetName)) { datasetId = H5D.open(_fileId, datasetName); spaceId = H5D.getSpace(datasetId); dims = H5S.getSimpleExtentDims(spaceId); if (dims.Length == 2) { bandN = 1; bandH = (int)dims[0]; bandW = (int)dims[1]; } else if (dims.Length == 3) { bandN = (int)dims[0]; bandH = (int)dims[1]; bandW = (int)dims[2]; } typeId = H5D.getType(datasetId); typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT); T[] dv = new T[bandN * bandH * bandW]; H5D.read <T>(datasetId, typeId, new H5Array <T>(dv)); if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (datasetId != null) { H5D.close(datasetId); } return(dv); } else { throw new Exception("未查到指定数据集!"); } }
public void H5Dcreate_anonTest1() { hsize_t[] dims = { 1024, 2048 }; hid_t space = H5S.create_simple(3, dims, null); hid_t dset = H5D.create_anon(m_v0_test_file, H5T.STD_I16LE, space); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); dset = H5D.create_anon(m_v2_test_file, H5T.STD_I16LE, space); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
public override T With <T>(Func <long, T> action) { var h5GRef = parent.Open(); long h5Ref = H5D.open(h5GRef, Name); try { return(action(h5Ref)); } finally { H5D.close(h5Ref); H5G.close(h5GRef); } }
private void DatasetCreateProcedure() { string name = Thread.CurrentThread.Name; hid_t space = H5S.create(H5S.class_t.SCALAR); Assert.IsTrue(space >= 0); hid_t dset = H5D.create(m_shared_file_id, name, H5T.STD_I32BE, space); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue(H5S.close(space) >= 0); }
/// <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 (!DatasetExists) { Hdf5Utils.LogInfo?.Invoke("Data set does not exist."); return; } H5D.close(datasetId); H5P.close(propId); H5S.close(spaceId); if (itIsSafeToAlsoFreeManagedObjects) { } }
public override void With(Action <long> action) { var h5GRef = parent.Open(); long h5Ref = H5D.open(h5GRef, Name); try { action(h5Ref); } finally { H5D.close(h5Ref); H5G.close(h5GRef); } }
private bool getOMXMatrixTables() { // get info about the tables this.dataGroup = H5G.open(fileId, dataGroupName); this.NumMatrix = (int)H5G.getNumObjects(this.dataGroup); this.MatrixNames = new List <string>(); for (int i = 0; i < NumMatrix; i++) { string matName = H5G.getObjectNameByIndex(this.dataGroup, (ulong)i); MatrixNames.Add(matName); H5DataSetId matId = H5D.open(dataGroup, matName); tables.Add(matName, matId); } return(true); }
public void H5Pget_layoutTestVDS1() { 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); H5D.layout_t layout = H5P.get_layout(dcpl); Assert.IsTrue(layout == H5D.layout_t.VIRTUAL); Assert.IsTrue(H5P.close(dcpl) >= 0); Assert.IsTrue(H5D.close(vds) >= 0); }
private bool getOMXIndexMaps() { this.luGroup = H5G.open(fileId, luGroupName); this.NumIndexMap = (int)H5G.getNumObjects(this.luGroup); this.IndexMapNames = new List <string>(); for (int i = 0; i < NumIndexMap; i++) { string imName = H5G.getObjectNameByIndex(this.luGroup, (ulong)i); IndexMapNames.Add(imName); H5DataSetId imId = H5D.open(luGroup, imName); indexMaps.Add(imName, imId); } return(true); }
private static hid_t AddDataString(hid_t parentLoc, string name, string data) { byte[][] wdata = new byte[1][]; wdata[0] = ASCIIEncoding.ASCII.GetBytes(data); int n = wdata[0].Length + 1; /* * Create file and memory datatypes. For this example we will save * the strings as FORTRAN strings, therefore they do not need space * for the null terminator in the file. */ hsize_t[] dims = new hsize_t[1]; dims[0] = (hsize_t)1; hid_t filetype = H5T.copy(H5T.FORTRAN_S1); herr_t status = H5T.set_size(filetype, new IntPtr(n - 1)); hid_t memtype = H5T.copy(H5T.C_S1); status = H5T.set_size(memtype, new IntPtr(n)); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ hid_t space = H5S.create_simple(1, dims, null); /* * Create the dataset and write the string data to it. */ hid_t dset = H5D.create(parentLoc, name, filetype, space, H5P.DEFAULT, H5P.DEFAULT, H5P.DEFAULT); GCHandle hnd = GCHandle.Alloc(wdata[0], GCHandleType.Pinned); // herr_t flag= H5D.write(dataSetId, type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); status = H5D.write(dset, memtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); /* * Close and release resources. */ status = H5D.close(dset); status = H5S.close(space); status = H5T.close(filetype); status = H5T.close(memtype); return(dset); }
public static IEnumerable <T> ReadCompounds <T>(hid_t groupId, string name) where T : struct { Type type = typeof(T); hid_t typeId = 0; // open dataset var datasetId = H5D.open(groupId, name); typeId = CreateType(type); var compoundSize = Marshal.SizeOf(type); /* * Get dataspace and allocate memory for read buffer. */ var spaceId = H5D.get_space(datasetId); int rank = H5S.get_simple_extent_ndims(spaceId); ulong[] dims = new ulong[rank]; var ndims = H5S.get_simple_extent_dims(spaceId, dims, null); int rows = Convert.ToInt32(dims[0]); byte[] bytes = new byte[rows * compoundSize]; // Read the data. GCHandle hnd = GCHandle.Alloc(bytes, GCHandleType.Pinned); IntPtr hndAddr = hnd.AddrOfPinnedObject(); H5D.read(datasetId, typeId, spaceId, H5S.ALL, H5P.DEFAULT, hndAddr); int counter = 0; IEnumerable <T> strcts = Enumerable.Range(1, rows).Select(i => { byte[] select = new byte[compoundSize]; Array.Copy(bytes, counter, select, 0, compoundSize); T s = fromBytes <T>(select); counter = counter + compoundSize; return(s); }); /* * Close and release resources. */ H5D.vlen_reclaim(typeId, spaceId, H5P.DEFAULT, hndAddr); hnd.Free(); H5D.close(datasetId); H5S.close(spaceId); H5T.close(typeId); return(strcts); }
/// <summary> /// Returns the data types of the matrix /// </summary> /// <param name="mapName"></param> /// <returns></returns> public H5DataTypeId GetMatrixDataType(string matName) { H5DataTypeId matDataId = null; // check that index map exists if (tables.ContainsKey(matName)) { H5DataSetId matId; tables.TryGetValue(matName, out matId); matDataId = H5D.getType(matId); } else { Console.WriteLine("table {0} not found in file", matName); } return(matDataId); }
public static void ClassCleanup() { // close the sample datasets Assert.IsTrue(H5D.close(m_v2_ascii_dset) >= 0); Assert.IsTrue(H5D.close(m_v0_ascii_dset) >= 0); Assert.IsTrue(H5D.close(m_v2_utf8_dset) >= 0); Assert.IsTrue(H5D.close(m_v0_utf8_dset) >= 0); // close the global test files Assert.IsTrue(H5F.close(m_v0_class_file) >= 0); Assert.IsTrue(H5F.close(m_v2_class_file) >= 0); Assert.IsTrue(H5S.close(m_space_null) >= 0); Assert.IsTrue(H5S.close(m_space_scalar) >= 0); File.Delete(m_v0_class_file_name); File.Delete(m_v2_class_file_name); }
public static T[,] Read2DArray <T>(this H5FileId fileId, string dataSetName) { var dataset = H5D.open(fileId, dataSetName); var space = H5D.getSpace(dataset); var dims = H5S.getSimpleExtentDims(space); var dataType = H5D.getType(dataset); if (typeof(T) == typeof(string)) { // this will also need a string hack... } T[,] dataArray = new T[dims[0], dims[1]]; var wrapArray = new H5Array <T>(dataArray); H5D.read(dataset, dataType, wrapArray); return(dataArray); }
public static void ClassInit(TestContext testContext) { #if HDF5_VER1_10 // create test files which persists across file tests m_v3_class_file = Utilities.H5TempFileSWMR(ref m_v3_class_file_name); Assert.IsTrue(m_v3_class_file >= 0); m_lcpl = H5P.create(H5P.LINK_CREATE); Assert.IsTrue(H5P.set_create_intermediate_group(m_lcpl, 1) >= 0); m_lcpl_utf8 = H5P.copy(m_lcpl); Assert.IsTrue( H5P.set_char_encoding(m_lcpl_utf8, H5T.cset_t.UTF8) >= 0); // create a sample dataset 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_class_file, "int6x6", 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); #endif }
public static void WriteDataCube(H5FileId fileId, UInt16[,,] datacube) { H5GroupId dataGroup = H5G.create(fileId, "/data"); H5GroupId dataSubGroup = H5G.create(dataGroup, "DEsoftware"); long[] dims = new long[3] { datacube.GetLength(0), datacube.GetLength(1), datacube.GetLength(2) }; H5DataSpaceId spaceId = H5S.create_simple(3, dims); H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT); H5DataSetId dataSetId = H5D.create(dataSubGroup, "data", typeId, spaceId); // create attribute emd_group_type for dataSubGroup, which is required to have value 1 int par = 1; long[] attdims = new long[1]; int[] AttArray = new int[1] { par }; dims[0] = AttArray.Length; H5AttributeId attributeId = H5A.create(dataSubGroup, "emd_group_type", H5T.copy(H5T.H5Type.NATIVE_UCHAR), H5S.create_simple(1, dims)); H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_INT), new H5Array <int>(AttArray)); H5A.close(attributeId); // write datacube to "data", which contains whole 3D datacube H5D.write <ushort>(dataSetId, typeId, new H5Array <ushort>(datacube)); // create three more array for three dimensions long[] dim1 = new long[1] { datacube.GetLength(0) }; double[] dimarray = new double [datacube.GetLength(0)]; spaceId = H5S.create_simple(3, dim1); typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE); dataSetId = H5D.create(dataSubGroup, "dim1", typeId, spaceId); H5D.write <double>(dataSetId, typeId, new H5Array <double>(dimarray)); H5S.close(spaceId); H5T.close(typeId); H5D.close(dataSetId); H5G.close(dataGroup); H5G.close(dataSubGroup); }
/// <summary> /// Returns the data types of the index map /// </summary> /// <param name="mapName"></param> /// <returns></returns> public H5DataTypeId GetMappingDataType(string mapName) { H5DataTypeId mapDataId = null; // check that index map exists if (indexMaps.ContainsKey(mapName)) { H5DataSetId mapId; indexMaps.TryGetValue(mapName, out mapId); mapDataId = H5D.getType(mapId); } else { Console.WriteLine("index map {0} not found in file", mapName); } return(mapDataId); }
public static int WriteAsciiString(hid_t groupId, string name, string str) { var spaceNullId = H5S.create(H5S.class_t.NULL); var spaceScalarId = H5S.create(H5S.class_t.SCALAR); // create two datasets of the extended ASCII character set // store as H5T.FORTRAN_S1 -> space padding int strLength = str.Length; ulong[] dims = { (ulong)strLength, 1 }; /* Create the dataset. */ //name = ToHdf5Name(name); var spaceId = H5S.create_simple(1, dims, null); var datasetId = H5D.create(groupId, name, H5T.FORTRAN_S1, spaceId); H5S.close(spaceId); // we write from C and must provide null-terminated strings byte[] wdata = new byte[strLength * 2]; //for (int i = 0; i < strLength; ++i) //{ // wdata[2 * i] = (byte)i; //} for (int i = 0; i < strLength; ++i) { wdata[2 * i] = Convert.ToByte(str[i]); } var memId = H5T.copy(H5T.C_S1); H5T.set_size(memId, new IntPtr(2)); //H5T.set_strpad(memId, H5T.str_t.NULLTERM); GCHandle hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned); int result = H5D.write(datasetId, memId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); H5T.close(memId); H5D.close(datasetId); return(result); }
public static int WriteStrings(hid_t groupId, string name, IEnumerable <string> strs, string datasetName = null) { // create UTF-8 encoded test datasets 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 datasetId = H5D.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 = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()); hnd.Free(); for (int i = 0; i < strSz; ++i) { hnds[i].Free(); } H5D.close(datasetId); H5S.close(spaceId); H5T.close(datatype); return(result); }
/// <summary> /// Returns mapping index - assumes that matrices are square and uses first dimension as the map size /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mapName">Name of index map</param> public T[] GetMapping <T>(string mapName) { var mapData = new T[Shape[0]]; // check that index map exists if (indexMaps.ContainsKey(mapName)) { H5DataSetId mapId; indexMaps.TryGetValue(mapName, out mapId); H5D.read(mapId, H5D.getType(mapId), new H5Array <T>(mapData)); } else { Console.WriteLine("index map {0} not found in file", mapName); } return(mapData); }
private void WriteData() { Console.WriteLine("Creating H5 file {0}...", filename); // Rank is the number of dimensions of the data array. const int RANK = 1; // Create an HDF5 file. // The enumeration type H5F.CreateMode provides only the legal // creation modes. Missing H5Fcreate parameters are provided // with default values. H5FileId fileId = H5F.create(filename, H5F.CreateMode.ACC_TRUNC); // Prepare to create a data space for writing a 1-dimensional // signed integer array. long[] dims = new long[RANK]; dims[0] = count; // Put descending ramp data in an array so that we can // write it to the file. mData[] dset_data = new mData[count]; for (int i = 0; i < count; i++) { dset_data[i] = new mData(i + 80, i + 40, i + 1); } // Create a data space to accommodate our 1-dimensional array. // The resulting H5DataSpaceId will be used to create the // data set. H5DataSpaceId spaceId = H5S.create_simple(RANK, dims); // Create a copy of a standard data type. We will use the // resulting H5DataTypeId to create the data set. We could // have used the HST.H5Type data directly in the call to // H5D.create, but this demonstrates the use of H5T.copy // and the use of a H5DataTypeId in H5D.create. H5DataTypeId typeId = H5T.copy(H5T.H5Type.STD_REF_OBJ); // Find the size of the type int typeSize = H5T.getSize(typeId); // Create the data set. H5DataSetId dataSetId = H5D.create(fileId, dataSetName, typeId, spaceId); // Write the integer data to the data set. H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.STD_REF_OBJ), new H5Array <mData>(dset_data)); H5D.close(dataSetId); H5F.close(fileId); Console.WriteLine("H5 file {0} created successfully!", filename); }
public static IEnumerable <string> ReadStrings(hid_t groupId, string name) { 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.NULLTERM); //name = ToHdf5Name(name); var datasetId = H5D.open(groupId, name); hid_t 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(strs); }
public void H5DopenTest1() { Assert.IsTrue( H5D.close(H5D.create(m_v0_test_file, "dset", H5T.IEEE_F64BE, m_space_null)) >= 0); hid_t dset = H5D.open(m_v0_test_file, "dset"); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); Assert.IsTrue( H5D.close(H5D.create(m_v2_test_file, "dset", H5T.IEEE_F64BE, m_space_null)) >= 0); dset = H5D.open(m_v0_test_file, "dset"); Assert.IsTrue(dset >= 0); Assert.IsTrue(H5D.close(dset) >= 0); }