private void Write(H5GroupId parent, string name, IMeasurement m) { H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)1 }); H5DataSetId dataSetId = H5D.create(parent, name, measurement_t, spaceId); MeasurementT mt = Convert(m); H5D.writeScalar <MeasurementT>(dataSetId, measurement_t, ref mt); H5D.close(dataSetId); H5S.close(spaceId); }
private void Write(H5GroupId parent, string name, double d) { H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { 1 }); H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_DOUBLE, spaceId); double data = d; H5D.writeScalar <double>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE), ref data); H5D.close(dataSetId); H5S.close(spaceId); }
private void Write(H5GroupId parent, string name, ulong ul) { H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { 1 }); H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_ULLONG, spaceId); ulong data = ul; H5D.writeScalar <ulong>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_ULLONG), ref data); H5D.close(dataSetId); H5S.close(spaceId); }
static void test_h5s_basic() { try { int rank; // Logical rank of dataspace hssize_t[] dims1 = { SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3 }; hssize_t[] dims2 = { SPACE2_DIM1, SPACE2_DIM2, SPACE2_DIM3, SPACE2_DIM4 }; hssize_t[] max2 = { SPACE2_MAX1, SPACE2_MAX2, SPACE2_MAX3, SPACE2_MAX4 }; hssize_t[] tmax = new hssize_t[4]; // Output message about test being performed. Console.Write("Testing dataspace manipulation"); // Create a simple dataspace and check its rank. H5DataSpaceId sid1 = H5S.create_simple(SPACE1_RANK, dims1); rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE1_RANK) { Console.WriteLine("\ntest_h5s_basic: Incorrect rank {0}, should be SPACE1_RANK({1})", rank, SPACE1_RANK); nerrors++; } // Check its dims. hssize_t[] tdims1 = new hssize_t[3]; tdims1 = H5S.getSimpleExtentDims(sid1); int i; for (i = 0; i < rank; i++) { if (tdims1[i] != dims1[i]) { Console.WriteLine("\ntest_h5s_basic: read tdims1[{0}] = {1} differs from dims1[{0}] = {2}", i, tdims1[i], dims1[i]); nerrors++; } } // Create another simple dataspace and check its rank, dims, and maxdims. H5DataSpaceId sid2 = H5S.create_simple(SPACE2_RANK, dims2, max2); rank = H5S.getSimpleExtentNDims(sid2); if (rank != SPACE2_RANK) { Console.WriteLine("\ntest_h5s_basic: Incorrect rank {0}, should be SPACE1_RANK({1})", rank, SPACE1_RANK); nerrors++; } hssize_t[] tdims2 = new hssize_t[3]; tdims2 = H5S.getSimpleExtentDims(sid2); tmax = H5S.getSimpleExtentMaxDims(sid2); for (i = 0; i < rank; i++) { if (tdims2[i] != dims2[i]) { Console.WriteLine("\ntest_h5s_basic: read tdims2[{0}] = {1} differs from dims2[{0}] = {2}", i, tdims2[i], dims2[i]); nerrors++; } } for (i = 0; i < rank; i++) { if (tmax[i] != max2[i]) { Console.WriteLine("\ntest_h5s_basic: read tmax[{0}] = {1} differs from max2[{0}] = {2}", i, tmax[i], max2[i]); nerrors++; } } // Close all dataspaces. H5S.close(sid1); H5S.close(sid2); /* * Try writing simple dataspaces without setting their extents. */ // Create the file H5FileId fid1 = H5F.create(BASICFILE, H5F.CreateMode.ACC_TRUNC); // Create dataspaces for testing. dims1[0] = SPACE1_DIM1; sid1 = H5S.create(H5S.H5SClass.SIMPLE); sid2 = H5S.create_simple(1, dims1, dims1); // This dataset's space has no extent; it should not be created try { H5DataSetId dset1 = H5D.create(fid1, BASICDATASET, H5T.H5Type.NATIVE_INT, sid1); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to create a dataset whose space has no extent."); nerrors++; } catch (H5DcreateException) { } // does nothing, it should fail // Create dataset with good dataspace. H5DataSetId dataset = H5D.create(fid1, BASICDATASET2, H5T.H5Type.NATIVE_INT, sid2); // Try some writes with the bad dataspace (sid1) try { hssize_t nelems = 10; // Number of dataspace elements H5D.writeScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), sid1, sid2, new H5PropertyListId(H5P.Template.DEFAULT), ref nelems); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to write to a dataset with space that has no extent."); nerrors++; } catch (H5DwriteException) { } // does nothing, it should fail // Make sure that dataspace reads using the bad dataspace fail try { hssize_t n = 10; // Number of dataspace elements H5D.readScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), sid1, sid2, new H5PropertyListId(H5P.Template.DEFAULT), ref n); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to read a dataset with space that has no extent."); nerrors++; } catch (H5DreadException) { } // does nothing, it should fail // Close objects and file. H5D.close(dataset); H5S.close(sid1); H5S.close(sid2); H5F.close(fid1); Console.WriteLine("\t\t\t\tPASSED"); } // end of try catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_h5s_basic
static void test_h5s_scalar() { try { hssize_t[] tdims = new hssize_t[3]; // Output message about test being performed. Console.Write("Testing dataspace during writing"); // Create the file. H5FileId fid1 = H5F.create(DATAFILE, H5F.CreateMode.ACC_TRUNC); // Create scalar dataspace. H5DataSpaceId sid1 = H5S.create_simple(SPACE3_RANK, null); // Get the logical rank of dataspace and verify it. int rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE3_RANK) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}, should be SPACE3_RANK({1})", rank, SPACE3_RANK); nerrors++; } // Create and write the dataset. uint space3_data = 65; H5DataSetId dataset = H5D.create(fid1, "Dataset1", H5T.H5Type.NATIVE_UINT, sid1); H5D.writeScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_UINT), ref space3_data); // Close objects and file. H5D.close(dataset); H5S.close(sid1); H5F.close(fid1); /* Open the file and verify the dataspace. */ // Open the file. fid1 = H5F.open(DATAFILE, H5F.OpenMode.ACC_RDWR); // Create a dataset. dataset = H5D.open(fid1, "Dataset1"); // Get dataset's dataspace. sid1 = H5D.getSpace(dataset); rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE3_RANK) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}", rank); } tdims = H5S.getSimpleExtentDims(sid1); //Console.WriteLine("tdims[0] = {0}, tdims[1] = {1}", tdims[0], tdims[1]); if (rank != 0) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}", rank); } // Read the dataset. uint rdata = 0; H5D.readScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_UINT), ref rdata); if (rdata != space3_data) { Console.WriteLine("\ntest_h5s_scalar: incorrect data {0}, should be {1}", rdata, space3_data); } // Close objects. H5D.close(dataset); H5S.close(sid1); H5F.close(fid1); Console.WriteLine("\t\t\tPASSED"); } // end of try catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_h5s_scalar_write
public void SimpleDataReadWrite() { // 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(TEST_FILE, H5F.CreateMode.ACC_TRUNC); // Create a group in the file H5GroupId groupId = H5G.create(fileId, "/simple"); // Prepare to create a data space for writing a 1-dimensional // signed integer array. const int RANK = 1; long[] dims = new long[RANK]; const int SIZE = 12; dims[0] = SIZE; // Put descending ramp data in an array so that we can // write it to the file. int[] dset_data = new int[SIZE]; for (int i = 0; i < SIZE; i++) { dset_data[i] = SIZE - i; } // 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 the data set. H5DataSetId dataSetId = H5D.create(fileId, "/arrayIntExample", H5T.H5Type.NATIVE_INT, spaceId); // Write the integer data to the data set. H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(dset_data)); // If we were writing a single value it might look like this. // Create the data set. H5DataSetId scalarId = H5D.create(fileId, "/scalarIntExample", H5T.H5Type.NATIVE_INT, spaceId); int singleValue = 100; H5D.writeScalar(scalarId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), ref singleValue); // Close everything down. H5D.close(dataSetId); H5D.close(scalarId); H5S.close(spaceId); H5G.close(groupId); H5F.close(fileId); Assert.IsTrue(System.IO.File.Exists(TEST_FILE)); fileId = H5F.open(TEST_FILE, H5F.OpenMode.ACC_RDONLY); Assert.IsTrue(fileId.Id > 0); groupId = H5G.open(fileId, "/simple"); Assert.IsTrue(groupId.Id > 0); Assert.AreEqual(0, H5G.getNumObjects(groupId)); // Open the data set dataSetId = H5D.open(fileId, "/arrayIntExample"); Assert.IsTrue(dataSetId.Id > 0); long datasetsize = H5D.getStorageSize(dataSetId); Assert.AreEqual(SIZE * sizeof(int), datasetsize); // Read the integer data back from the data set int[] readDataBack = new int[SIZE]; H5D.read(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(readDataBack)); for (int i = 0; i < SIZE; i++) { Assert.AreEqual(SIZE - i, readDataBack[i]); } // Read back the single-int example scalarId = H5D.open(fileId, "/scalarIntExample"); Assert.IsTrue(scalarId.Id > 0); H5D.readScalar <int>(scalarId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), ref singleValue); Assert.AreEqual(100, singleValue); H5D.close(dataSetId); H5D.close(scalarId); H5G.close(groupId); H5F.close(fileId); }