Example #1
0
        private static void ConvertHdf4To5(Hdf4FileAttrs hdf4FileAttrs, string f5name,
                                           Action <string, int, int> messageAction)
        {
            try
            {
                // Create a new file using H5F_ACC_TRUNC access,
                // default file creation properties, and default file
                // access properties.
                H5FileId fileId = H5F.create(f5name, H5F.CreateMode.ACC_TRUNC);

                int nxf5 = hdf4FileAttrs.Hdf4FileAttr.XDim;
                int nyf5 = hdf4FileAttrs.Hdf4FileAttr.YDim;
                int rank = 2;

                //测试读取的科学数据集及其属性
                int sdscount = hdf4FileAttrs.Hdf4FileAttr.DataFields.Count;
                for (int k = 0; k < sdscount; k++)
                {
                    ConvertHdf4To5BySds(hdf4FileAttrs, messageAction, k, nyf5, nxf5, rank, fileId);
                }

                HDFAttributeDef[] attributeDef5s = hdf4FileAttrs.GetHDFAttributeDefs();
                foreach (HDFAttributeDef attributeDef5 in attributeDef5s)
                {
                    WriteHdfAttributes.WriteHdfAttribute(fileId, attributeDef5);
                }

                H5F.close(fileId);
            }
            catch (Exception ex)
            {
                throw new Exception("拼接Hdf4时出错,具体信息:" + ex.Message, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Create a temporary HDF5 with SWMR access and return
        /// its name and a file handle.
        /// </summary>
        public static hid_t H5TempFileSWMR(ref string fileName)
        {
            hid_t fapl = H5P.create(H5P.FILE_ACCESS);

            if (fapl < 0)
            {
                throw new ApplicationException("H5P.create failed.");
            }
            if (H5P.set_libver_bounds(fapl, H5F.libver_t.LATEST) < 0)
            {
                throw new ApplicationException("H5P.set_libver_bounds failed.");
            }
            if (H5P.set_fclose_degree(fapl, H5F.close_degree_t.STRONG) < 0)
            {
                throw new ApplicationException("H5P.set_fclose_degree failed.");
            }
            fileName = Path.GetTempFileName();
            hid_t file = H5F.create(fileName, H5F.ACC_TRUNC | H5F.ACC_SWMR_WRITE,
                                    H5P.DEFAULT, fapl);

            if (file < 0)
            {
                throw new ApplicationException("H5F.create failed.");
            }
            if (H5P.close(fapl) < 0)
            {
                throw new ApplicationException("H5P.close failed.");
            }
            return(file);
        }
Example #3
0
 private static H5FileId GetFileId()
 {
     return
         (File.Exists(Global.GetOutputPath(Global.Configuration.HDF5Path))
                                 ? H5F.open(Global.GetOutputPath(Global.Configuration.HDF5Path), H5F.OpenMode.ACC_RDWR)
                                 : H5F.create(Global.GetOutputPath(Global.Configuration.HDF5Path), H5F.CreateMode.ACC_EXCL));
 }
Example #4
0
        /// <summary>
        /// Create a temporary HDF5 file IN MEMORY and return its name and
        /// a file handle.
        /// </summary>
        public static hid_t H5TempFile(ref string fileName,
                                       H5F.libver_t version = H5F.libver_t.LATEST,
                                       bool backing_store   = false)
        {
            hid_t fapl = H5P.create(H5P.FILE_ACCESS);

            if (fapl < 0)
            {
                throw new ApplicationException("H5P.create failed.");
            }
            if (H5P.set_libver_bounds(fapl, version) < 0)
            {
                throw new ApplicationException("H5P.set_libver_bounds failed.");
            }
            // use the core VFD, 64K increments, no backing store
            if (H5P.set_fapl_core(fapl, new IntPtr(65536),
                                  (uint)(backing_store ? 1 : 0)) < 0)
            {
                throw new ApplicationException("H5P.set_fapl_core failed.");
            }
            fileName = Path.GetTempFileName();
            hid_t file = H5F.create(fileName, H5F.ACC_TRUNC, H5P.DEFAULT, fapl);

            if (file < 0)
            {
                throw new ApplicationException("H5F.create failed.");
            }
            if (H5P.close(fapl) < 0)
            {
                throw new ApplicationException("H5P.close failed.");
            }
            return(file);
        }
Example #5
0
        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);
        }
Example #6
0
        public MatFile(string fileName)
        {
            FileName = fileName;
            FileId   = H5F.create(FileName, H5F.CreateMode.ACC_TRUNC);
            GroupId  = H5G.open(FileId, "/");

            Variables = new Dictionary <string, MatVar>();
        }
Example #7
0
        private void Menu_2()
        {
            long vdsFileId     = -1;
            long vdsMetaFileId = -1;
            long fcPropertyId  = -1;

            string vdsFilePath;
            string vdsMetaFilePath;

            List <CampaignInfo>    campaignInfoSet;
            IList <HdfElementBase> currentList;

            //
            vdsFilePath     = Path.Combine(Program.BaseDirectoryPath, "VDS.h5");
            vdsMetaFilePath = Path.Combine(Program.BaseDirectoryPath, "VDS_META.h5");

            try
            {
                if (File.Exists(vdsFilePath))
                {
                    vdsFileId = H5F.open(vdsFilePath, H5F.ACC_RDONLY);
                }
                else
                {
                    return;
                }

                if (File.Exists(vdsMetaFilePath))
                {
                    vdsMetaFileId = H5F.open(vdsMetaFilePath, H5F.ACC_RDWR);
                }

                if (vdsMetaFileId == -1)
                {
                    fcPropertyId = H5P.create(H5P.FILE_CREATE);
                    H5P.set_file_space(fcPropertyId, H5F.file_space_type_t.ALL_PERSIST);
                    vdsMetaFileId = H5F.create(vdsMetaFilePath, H5F.ACC_TRUNC, fcPropertyId);
                }

                campaignInfoSet = GeneralHelper.GetCampaignInfoSet(vdsFileId, true);
                currentList     = campaignInfoSet.Cast <HdfElementBase>().ToList();

                new VdsMetaNavigator(vdsFileId, vdsMetaFileId, "/", currentList);
            }
            finally
            {
                if (H5I.is_valid(vdsFileId) > 0)
                {
                    H5F.close(vdsFileId);
                }
                if (H5I.is_valid(vdsMetaFileId) > 0)
                {
                    H5F.close(vdsMetaFileId);
                }
            }
        }
Example #8
0
        public void H5Fis_hdf5Test1()
        {
            string fname = Path.GetTempFileName();
            hid_t  file  = H5F.create(fname, H5F.ACC_TRUNC);

            Assert.IsTrue(file >= 0);
            Assert.IsTrue(H5F.close(file) >= 0);
            Assert.IsTrue(H5F.is_hdf5(fname) > 0);
            File.Delete(fname);
        }
Example #9
0
        public static void ClassInit(TestContext testContext)
        {
            hid_t fapl = H5P.create(H5P.FILE_ACCESS);

            Assert.IsTrue(fapl >= 0);
            Assert.IsTrue(
                H5P.set_libver_bounds(fapl, H5F.libver_t.LATEST) >= 0);
            m_shared_file_id = H5F.create(m_shared_file_name, H5F.ACC_TRUNC,
                                          H5P.DEFAULT, fapl);
            Assert.IsTrue(H5P.close(fapl) >= 0);
        }
Example #10
0
        public void Setup()
        {
            bytes_ = RandomExtension.RandomBytes(1024);

            file_     = new FileStream("HDF5Benchmark_FileIO.dat", FileMode.Create, FileAccess.ReadWrite);
            fileRows_ = 0;

            h5file_  = H5F.create("HDF5Benchmark_FileIO.h5", H5F.ACC_TRUNC);
            dataSet_ = HDF5Helper.CreateDataSet(h5file_, "data", H5T.NATIVE_UINT8,
                                                new ulong[] { 0 }, new ulong[] { H5S.UNLIMITED }, new ulong[] { (ulong)bytes_.Length });
            dataSetRows_ = 0;
        }
Example #11
0
        public void H5FcloseTest1()
        {
            string fname = Path.GetTempFileName();
            hid_t  file  = H5F.create(fname, H5F.ACC_TRUNC);

            Assert.IsTrue(file >= 0);
            Assert.IsTrue(H5F.close(file) >= 0);
            file = H5F.open(fname, H5F.ACC_RDONLY);
            Assert.IsTrue(file >= 0);
            Assert.IsTrue(H5F.close(file) >= 0);
            File.Delete(fname);
        }
Example #12
0
        static void Main2222(string[] args)
        {
            var h5 = H5F.create(@"D:\test.h5", H5F.ACC_TRUNC);

            var typeId = H5T.create(H5T.class_t.COMPOUND, new IntPtr(40));

            var strtype = H5T.copy(H5T.C_S1);

            H5T.set_size(strtype, new IntPtr(16));

            H5T.insert(typeId, "Name", new IntPtr(0), strtype);
            H5T.insert(typeId, "x_pos", new IntPtr(16), H5T.NATIVE_INT32);
            H5T.insert(typeId, "y_pos", new IntPtr(20), H5T.NATIVE_INT32);
            H5T.insert(typeId, "Mass", new IntPtr(24), H5T.NATIVE_FLOAT);
            H5T.insert(typeId, "Temperature", new IntPtr(32), H5T.NATIVE_DOUBLE);

            ulong[] dims       = new ulong[] { 10000 };
            ulong[] chunk_size = new ulong[] { 1000 };

            var spaceid = H5S.create_simple(dims.Length, dims, null);


            var dcpl = H5P.create(H5P.DATASET_CREATE);

            H5P.set_layout(dcpl, H5D.layout_t.COMPACT);
            H5P.set_deflate(dcpl, 6);

            H5P.set_chunk(dcpl, chunk_size.Length, chunk_size);



            var datasetid = H5D.create(h5, "Table1", typeId, spaceid, H5P.DEFAULT, dcpl);

            ComType ct = new ComType()
            {
                Name        = "aabb",
                x_pos       = 2,
                y_pos       = 1,
                Mass        = 1.24F,
                Temperature = 45.7,
            };

            IntPtr p = Marshal.AllocHGlobal(40 * (int)dims[0]);

            Marshal.StructureToPtr(ct, p, false);



            H5D.write(datasetid, typeId, spaceid, H5S.ALL, H5P.DEFAULT, p);

            H5F.close(h5);
        }
Example #13
0
        static void test_attr_plist()
        {
            try
            {
                Console.Write("Testing attribute property lists");
                hssize_t[] dims = { 256, 512 };

                const string PLST_FILE_NAME = ("tattr_plist.h5");
                hssize_t[]   dims1          = { SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3 };
                hssize_t[]   dims2          = { ATTR1_DIM };

                // Create file.
                H5FileId fileId = H5F.create(PLST_FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                // Create dataspace for dataset.
                H5DataSpaceId space1_Id = H5S.create_simple(SPACE1_RANK, dims1);

                // Create a dataset.
                H5DataSetId dsetId = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_UCHAR, space1_Id);

                // Create dataspace for attribute.
                H5DataSpaceId space2_Id = H5S.create_simple(ATTR1_RANK, dims2);

                // Create default property list for attribute.
                H5PropertyListId plist
                    = H5P.create(H5P.PropertyListClass.ATTRIBUTE_CREATE);

                // Create an attribute for the dataset using the property list.
                H5AttributeId attrId = H5A.create(dsetId, ATTR1_NAME, new H5DataTypeId(H5T.H5Type.NATIVE_INT), space2_Id, plist);

                // Close all objects.
                H5S.close(space1_Id);
                H5S.close(space2_Id);
                H5P.close(plist);
                H5A.close(attrId);
                H5D.close(dsetId);
                H5F.close(fileId);

                Console.WriteLine("\t\t\tPASSED");
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_attr_plist
Example #14
0
        public void SimpleOpenClose()
        {
            // 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);

            // Close the file.
            H5F.close(fileId);

            Assert.IsTrue(System.IO.File.Exists(TEST_FILE));
        }
Example #15
0
 public HDF5Helper(String path, bool createFlag)
 {
     if (!createFlag)
     {
         _fileId = H5F.open(path, H5F.OpenMode.ACC_RDONLY);
     }
     else
     {
         _fileId = H5F.create(path, H5F.CreateMode.ACC_TRUNC);
     }
     getFileAttributeNames();
     getDatasetNames();
 }
Example #16
0
        public void H5FcreateTest1()
        {
            string fname = Path.GetTempFileName();
            hid_t  file  = H5F.create(fname, H5F.ACC_EXCL);

            // this is expected, because Path.GetTempFileName() creates
            // an empty file
            Assert.IsFalse(file >= 0);

            file = H5F.create(fname, H5F.ACC_TRUNC);
            Assert.IsTrue(file >= 0);
            Assert.IsTrue(H5F.close(file) >= 0);
            File.Delete(fname);
        }
Example #17
0
        } // test_copy

        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("TEST: HDF5DotNet Datatype API");
            Console.WriteLine();
            try
            {
                // Suppress error printing from the C library.
                H5E.suppressPrinting();

                // Create the file
                H5FileId fileId = H5F.create("test_types.h5", H5F.CreateMode.ACC_TRUNC);

                // Invokes individual datatype tests
                test_classes();
                test_integer_dtype();
                test_float_dtype();
                test_compound_dtype(fileId);
                test_enum_dtype(fileId);
                test_vlen_dtype(fileId);
                test_copy();

                // Close the file
                H5F.close(fileId);
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }

            // Report results.
            Console.WriteLine();
            if (nerrors > 0)
            {
                Console.WriteLine("Test(s) failed: {0}", nerrors, "occurred!");
            }
            else
            {
                Console.WriteLine("---- All datatype tests passed.");
            }
            Console.WriteLine();
        }
Example #18
0
        // TODO: add create with both dimensions defined in shape.
        /// <summary>
        /// Create OMX file with no matrix tables
        /// </summary>
        /// <param name="zones">Number of zones - matrix tables will all be square</param>
        /// <param name="overwrite">true to automatically overwrite file on disc, false will prompt user to overwrite</param>
        public void CreateFileOMX(int zones, bool overwrite)
        {
            overwriteCheck(overwrite);

            // create and open are two seperate hanldes, need to close create after finished
            H5FileId createFile = H5F.create(filepath, H5F.CreateMode.ACC_EXCL);

            H5F.close(createFile);

            fileId = H5F.open(filepath, H5F.OpenMode.ACC_RDWR);

            this.Shape = new long[] { zones, zones };
            H5DataTypeId[] matDataTypes = null;

            this.IsValid = setOMXFileContents(matDataTypes);
        }
Example #19
0
        public static int OpenFile(string filename, bool readOnly = false)
        {
            int fileId;

            if (File.Exists(filename))
            {
                uint access = (readOnly) ? H5F.ACC_RDONLY : H5F.ACC_RDWR;
                fileId = H5F.open(filename, access);
            }
            else
            {
                fileId = H5F.create(filename,
                                    H5F.ACC_TRUNC);         //,H5P.DEFAULT,H5P.DEFAULT
            }
            return(fileId);
        }
Example #20
0
        static void TestExtent()
        {
            var file = H5F.create("test-extent.h5", H5F.ACC_TRUNC);

            TestExtent(file, "1", new ulong[] { 0, 0 }, new ulong[] { 2 });

            WriteLine("--------------------------------------------------");

            TestExtent(file, "2", new ulong[] { 0, 4 }, new ulong[] { 2 });

            WriteLine("--------------------------------------------------");

            TestExtent(file, "3", new ulong[] { 0, 4 }, new ulong[] { 2, 4 });

            H5F.close(file);
        }
        public static unsafe string PrepareTestFile(H5F.libver_t version, Action <long> action)
        {
            var  filePath = Path.GetTempFileName();
            long res;

            // file
            var faplId = H5P.create(H5P.FILE_ACCESS);

            res = H5P.set_libver_bounds(faplId, version, version);
            var fileId = H5F.create(filePath, H5F.ACC_TRUNC, 0, faplId);

            action?.Invoke(fileId);
            res = H5F.close(fileId);

            return(filePath);
        }
Example #22
0
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("TEST: HDF5DotNet Dataset API");
            Console.WriteLine();
            try
            {
                const string FILE_NAME = ("Dataset.h5");

                // Suppress error printing from the C library.
                H5E.suppressPrinting();

                // Create a new file using H5F_ACC_TRUNC access,
                // default file creation properties, and default file
                // access properties.
                H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                test_create(fileId);         // test creating dataset
                test_onedim_array(fileId);   // test writing one-dimensional array
                test_twodims_array();        // test writing multiple-dimensional arrays
                test_fivedims_array(fileId);

                // Close the file.
                H5F.close(fileId);
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
            Console.WriteLine();
            if (nerrors > 0)
            {
                Console.WriteLine("Test(s) failed: ", nerrors, "occurred!");
            }
            else
            {
                Console.WriteLine("---- All dataset tests passed.");
            }
            Console.WriteLine();
        }
Example #23
0
        public static H5FileId InitializeHDF(int numpos, int height, int width, UInt16[,,] datacube, string fullpath)
        {
            // write in HDF5 (.h5) format

            // generate standard groups
            H5FileId fileId = H5F.create(fullpath,
                                         H5F.CreateMode.ACC_TRUNC);
            //H5FileId fileId = H5F.create("D:/2017/Pixelated Camera/CameraSoftware/FileFormat/Test/test5.emd",
            //H5F.CreateMode.ACC_TRUNC);
            //NumberAttributeGenerator(fileId, "voltage", Convert.ToSingle(300));

            //H5GroupId dataGroup = H5G.create(fileId, "/data");  //dash is required for root group
            H5GroupId userGroup    = H5G.create(fileId, "/user");
            H5GroupId micGroup     = H5G.create(fileId, "/microscope");
            H5GroupId sampleGroup  = H5G.create(fileId, "/sample");
            H5GroupId commentGroup = H5G.create(fileId, "/comments");

            // generate attributes for user group, all attributes are sting
            StringAttributeGenerator(userGroup, "user", "Chenyu Zhang");
            StringAttributeGenerator(userGroup, "email", "*****@*****.**");
            StringAttributeGenerator(userGroup, "institution", "UW-Madison");
            StringAttributeGenerator(userGroup, "department", "Materials Science and Engineering");

            // generate attributes for microscope group
            StringAttributeGenerator(micGroup, "voltage units", "kV");
            NumberAttributeGenerator(micGroup, "voltage", Convert.ToSingle(300));
            StringAttributeGenerator(micGroup, "wavelength units", "nm");
            NumberAttributeGenerator(micGroup, "wavelength", Convert.ToSingle(0.00197));

            // generate attributes for sample group
            StringAttributeGenerator(sampleGroup, "material", "STO");
            StringAttributeGenerator(sampleGroup, "preparation", "Mechanical polishing and ion milling");
            StringAttributeGenerator(sampleGroup, "Zone Axis", "[1][0][0]");

            // Write 3D data cube to the file
            WriteDataCube(fileId, datacube);

            // close groups and file
            H5G.close(userGroup);
            H5G.close(micGroup);
            H5G.close(sampleGroup);
            H5G.close(commentGroup);
            //H5G.close(dataGroup);
            H5F.close(fileId);

            return(fileId);
        }
Example #24
0
        public static Hdf5File Create(string filename, bool @overwrite = true)
        {
            if (File.Exists(filename))
            {
                throw new Hdf5FileExistsException();
            }

            var fileId = H5F.create(filename, @overwrite ? H5F.ACC_TRUNC : H5F.ACC_EXCL).ToId();

            if (fileId.Value > 0)
            {
                H5F.close(fileId.Value);
                return(new Hdf5File(filename));
            }

            throw new Hdf5UnknownException();
        }
Example #25
0
    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);
    }
Example #26
0
        public static Hdf5File Create(string _filename)
        {
            if (File.Exists(_filename))
            {
                throw new Hdf5FileExistsException();
            }

            Hdf5Identifier fileId = H5F.create(_filename, H5F.ACC_EXCL).ToId();

            if (fileId.Value > 0)
            {
                H5F.close(fileId.Value);
                return(new Hdf5File(_filename));
            }

            throw new Hdf5UnknownException();
        }
Example #27
0
        static void WriteFile(string filePath)
        {
            var file = H5F.create(filePath, H5F.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 ulong[RANK] {
                DIM0, DIM1
            };
            var dataSpace = H5S.create_simple(RANK, dims, null);

            var dataSet = H5D.create(file, "/group/dataset", H5T.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, H5T.NATIVE_INT, H5S.ALL, H5S.ALL, H5P.DEFAULT, new PinnedObject(data));

            var hello = "早上好!";

            WriteStringAttribute(dataSet, "string", hello, true, false);
            WriteStringAttribute(dataSet, "string-ascii", "Hello, world!", false, false);
            WriteStringAttribute(dataSet, "string-vlen", hello, true, true);

            dataSpace = H5S.create(H5S.class_t.SCALAR);
            var doubleAttribute = H5A.create(dataSet, "double", H5T.NATIVE_DOUBLE, dataSpace);

            H5S.close(dataSpace);
            H5A.write(doubleAttribute, H5T.NATIVE_DOUBLE, new PinnedObject(new double[] { Math.PI }));
            H5A.close(doubleAttribute);

            H5D.close(dataSet);
            H5F.close(file);
        }
Example #28
0
        } // test_chunked_dset

        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("TEST: HDF5DotNet Property List API");
            Console.WriteLine();
            try
            {
                const string FILE_NAME = ("Dataset.h5");

                // Suppress error printing from the C library.
                H5E.suppressPrinting();

                // Create a new file using H5F_ACC_TRUNC access,
                // default file creation properties, and default file
                // access properties.
                H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC);

                test_buffer();
                test_chunked_dset();

                // Close the file.
                H5F.close(fileId);
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
            Console.WriteLine();
            if (nerrors > 0)
            {
                Console.WriteLine("Test(s) failed: ", nerrors, "occurred!");
            }
            else
            {
                Console.WriteLine("---- All property list tests passed.");
            }
            Console.WriteLine();
        }
Example #29
0
        public void H5TSforeachTest1()
        {
            // run only if we have a thread-safe build of the library
            hbool_t flag = 0;

            Assert.IsTrue(H5.is_library_threadsafe(ref flag) >= 0);
            if (flag > 0)
            {
                List <string> ls = new List <string>();
                for (int i = 0; i < NUM_FILES; ++i)
                {
                    ls.Add(Path.GetTempFileName());
                }

                var totalLength = 0;

                Parallel.ForEach <string, TaskLocals>
                    (ls,
                    () =>
                {
                    TaskLocals tl    = new TaskLocals();
                    tl.handle        = -1;
                    tl.runningLength = 0;
                    return(tl);
                },
                    (name, loop, taskLocals) =>
                {
                    // handle is "thread-local"
                    taskLocals.handle = H5F.create(name, H5F.ACC_TRUNC);
                    Assert.IsTrue(H5F.close(taskLocals.handle) >= 0);
                    Assert.IsTrue(H5F.is_hdf5(name) > 0);
                    File.Delete(name);
                    taskLocals.handle         = -1;
                    taskLocals.runningLength += name.Length;
                    return(taskLocals);
                },
                    (taskLocals) =>
                {
                    Interlocked.Add(ref totalLength, taskLocals.runningLength);
                }
                    );

                Assert.IsTrue(totalLength > NUM_FILES);
            }
        }
    private void WriteData(List <mData> data)
    {
        Console.WriteLine("Creating H5 file {0}...", filename);
        const int RANK = 1;

        long[] dims = new long[RANK];
        dims[0] = count;
        H5FileId      fileId    = H5F.create(filename, H5F.CreateMode.ACC_TRUNC);
        H5DataSpaceId spaceId   = H5S.create_simple(RANK, dims);
        H5DataTypeId  typeId    = H5T.copy(H5T.H5Type.STD_REF_OBJ);
        int           typeSize  = H5T.getSize(typeId);
        H5DataSetId   dataSetId = H5D.create(fileId, dataSetName, typeId, spaceId);

        H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.STD_REF_OBJ), new H5Array <mData>(data.ToArray()));
        H5D.close(dataSetId);
        H5F.close(fileId);
        Console.WriteLine("H5 file {0} created successfully!", filename);
    }