示例#1
0
        public static double[][] ReadMesh(string fileName)
        {
            double[][] meshes    = new double[3][];
            string[]   meshNames = { "x", "y", "z" };

            H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);

            for (int i = 0; i < meshNames.Length; i++)
            {
                H5DataSetId  dsId = H5D.open(fileId, "/Mesh/" + meshNames[i]);
                H5DataTypeId dtId = H5D.getType(dsId);

                if (!H5T.equal(dtId, H5T.copy(H5T.H5Type.NATIVE_FLOAT)))
                {
                    Console.WriteLine("Error: Invalid dataset type, expected {0}", H5T.H5Type.NATIVE_FLOAT);
                }

                float[] mesh = new float[H5D.getStorageSize(dsId) / H5T.getSize(dtId)];
                H5D.read(dsId, dtId, new H5Array <float>(mesh));

                meshes[i] = mesh.Select(x => (double)x * 1000.0).ToArray(); // m -> mm

                H5D.close(dsId);
                H5T.close(dtId);
            }

            H5F.close(fileId);

            return(meshes);
        }
示例#2
0
        public string GetFileApi(string source, string filename, string destination)
        {
            var fileId    = H5F.open(source, H5F.OpenMode.ACC_RDONLY);
            var datasetId = H5D.open(fileId, filename);
            //var datasetTypeId = new H5DataTypeId(H5T.H5Type.NATIVE_OPAQUE);
            //var space = H5D.getSpace(datasetId);
            //var dims = H5S.getSimpleExtentDims(space);
            var dataType = H5D.getType(datasetId);

            long size      = H5D.getStorageSize(datasetId);
            var  fileBytes = new byte[size];
            var  h5Array   = new H5Array <byte>(fileBytes);

            H5D.read(datasetId, dataType, h5Array);
            H5D.close(datasetId);
            H5F.close(fileId);

            var outPath = Path.Combine(destination, filename);

            File.WriteAllBytes(outPath, fileBytes);

            return(destination);
        }
示例#3
0
        private void ReadHDF5Mesh()
        {
            double[][] meshes    = new double[3][];
            string[]   meshNames = { "phi", "r", "theta" };

            H5FileId fileId = H5F.open(m_resultFile, H5F.OpenMode.ACC_RDONLY);

            if (HDF5.ReadAttribute(m_resultFile, "/Mesh", "MeshType") != 2)
            {
                Console.WriteLine("Error: Invalid NF2FF mesh type in <{0}>", m_resultFile);
                return;
            }

            for (int i = 0; i < meshNames.Length; i++)
            {
                H5DataSetId  dsId = H5D.open(fileId, "/Mesh/" + meshNames[i]);
                H5DataTypeId dtId = H5D.getType(dsId);

                if (!H5T.equal(dtId, H5T.copy(H5T.H5Type.NATIVE_FLOAT)))
                {
                    Console.WriteLine("Error: Invalid dataset type, expected {0}", H5T.H5Type.NATIVE_FLOAT);
                }

                float[] mesh = new float[H5D.getStorageSize(dsId) / H5T.getSize(dtId)];
                H5D.read(dsId, dtId, new H5Array <float>(mesh));

                meshes[i] = mesh.Select(x => (double)x).ToArray();

                H5D.close(dsId);
                H5T.close(dtId);
            }

            H5F.close(fileId);

            m_theta = meshes[2];
            m_phi   = meshes[0];
        }
示例#4
0
        public Dictionary <string, string> GetAttributes(string datasetName)
        {
            if (string.IsNullOrEmpty(datasetName) || !_datasetNames.Contains(datasetName))
            {
                return(null);
            }
            H5DataSetId   datasetId = null;
            H5GroupId     groupId   = null;
            H5DataTypeId  typeId    = null;
            H5DataSpaceId spaceId   = null;

            //H5PropertyListId psId = null;
            try
            {
                int groupIndex = datasetName.LastIndexOf('/');
                if (groupIndex == -1)
                {
                    datasetId = H5D.open(_h5FileId, datasetName);
                }
                else
                {
                    string groupName = datasetName.Substring(0, groupIndex + 1);
                    string dsName    = datasetName.Substring(groupIndex + 1);
                    groupId   = H5G.open(_h5FileId, groupName);
                    datasetId = H5D.open(groupId, dsName);
                }
                if (datasetId == null)
                {
                    return(null);
                }
                Dictionary <string, string> attValues = new Dictionary <string, string>();

                typeId = H5D.getType(datasetId);
                H5T.H5TClass type  = H5T.getClass(typeId);
                int          tSize = H5T.getSize(typeId);

                spaceId = H5D.getSpace(datasetId);
                long[] dims        = H5S.getSimpleExtentDims(spaceId);
                long   storageSize = H5D.getStorageSize(datasetId);

                attValues.Add("DataSetName", datasetName);
                attValues.Add("DataType", type.ToString());
                attValues.Add("DataTypeSize", tSize.ToString() + "Byte");
                attValues.Add("Dims", String.Join("*", dims));
                attValues.Add("StorageSize", storageSize.ToString() + "Byte");

                int attrCount = H5A.getNumberOfAttributes(datasetId);
                for (int i = 0; i < attrCount; i++)
                {
                    string attName = H5A.getNameByIndex(datasetId, "/" + datasetName, H5IndexType.NAME, H5IterationOrder.NATIVE, (ulong)i);
                    attValues.Add(attName, ReadAttributeValue(datasetId, attName));
                }
                return(attValues);
            }
            finally
            {
                if (spaceId != null)
                {
                    H5S.close(spaceId);
                }
                if (typeId != null)
                {
                    H5T.close(typeId);
                }
                if (datasetId != null)
                {
                    H5D.close(datasetId);
                }
                if (groupId != null)
                {
                    H5G.close(groupId);
                }
            }
        }
示例#5
0
        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);
        }