Esempio n. 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);
        }
Esempio n. 2
0
        public static double ReadAttribute(string file, string dataSetOrGroup, string attribute)
        {
            double attr = Double.NaN;

            try
            {
                H5FileId      fileId     = H5F.open(file, H5F.OpenMode.ACC_RDONLY);
                H5ObjectInfo  objectInfo = H5O.getInfoByName(fileId, dataSetOrGroup);
                H5GroupId     groupId    = null;
                H5DataSetId   dataSetId  = null;
                H5AttributeId attrId;

                if (objectInfo.objectType == H5ObjectType.GROUP)
                {
                    groupId = H5G.open(fileId, dataSetOrGroup);
                    attrId  = H5A.open(groupId, attribute);
                }
                else
                {
                    dataSetId = H5D.open(fileId, dataSetOrGroup);
                    attrId    = H5A.open(dataSetId, attribute);
                }
                H5DataTypeId attrTypeId = H5A.getType(attrId);

                double[] dAttrs = new double[] { };
                if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT)))
                {
                    float[] fAttrs = new float[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))];
                    H5A.read(attrId, attrTypeId, new H5Array <float>(fAttrs));
                    dAttrs = (from f in fAttrs select(double) f).ToArray();
                }
                else if (H5T.equal(attrTypeId, H5T.copy(H5T.H5Type.NATIVE_DOUBLE)))
                {
                    dAttrs = new double[H5S.getSimpleExtentNPoints(H5A.getSpace(attrId))];
                    H5A.read(attrId, attrTypeId, new H5Array <double>(dAttrs));
                }

                H5T.close(attrTypeId);
                H5A.close(attrId);
                if (groupId != null)
                {
                    H5G.close(groupId);
                }
                if (dataSetId != null)
                {
                    H5D.close(dataSetId);
                }
                H5F.close(fileId);

                return((double)dAttrs[0]);
            }

            catch (HDFException e)
            {
                Console.WriteLine("Error: Unhandled HDF5 exception");
                Console.WriteLine(e.Message);
            }

            return(attr);
        }
Esempio n. 3
0
        public static double[, ,] ReadFieldData3D(string fileName)
        {
            H5FileId     fileId      = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
            H5DataSetId  fDataSetId  = H5D.open(fileId, "/FieldData/FD/f0");
            H5DataTypeId fDataTypeId = H5D.getType(fDataSetId);

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

            long[] dims = H5S.getSimpleExtentDims(H5D.getSpace(fDataSetId)).ToArray();
            if (dims.Length != 3)
            {
                Console.WriteLine("Error: Invalid field data dimensions");
            }

            float[, ,] data = new float[dims[0], dims[1], dims[2]];
            H5D.read(fDataSetId, fDataTypeId, new H5Array <float>(data));

            // Reorder
            double[, ,] fieldValues = new double[dims[2], dims[1], dims[0]];
            for (int i = 0; i < dims[0]; i++)
            {
                for (int j = 0; j < dims[1]; j++)
                {
                    for (int k = 0; k < dims[2]; k++)
                    {
                        fieldValues[k, j, i] = data[i, j, k];
                    }
                }
            }

            return(fieldValues);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public object this[string key]
        {
            get
            {
                return(Host.With <object>((objId) =>
                {
                    long attrId = 0;
                    long dtypeId = 0;
                    try
                    {
                        attrId = H5A.open(objId, key);
                        if (attrId < 0)
                        {
                            throw new ArgumentException($"Unknown Attribute: {key}", nameof(key));
                        }
                        dtypeId = H5A.get_type(attrId);
                        int size = H5T.get_size(dtypeId).ToInt32();

                        IntPtr iPtr = Marshal.AllocHGlobal(size);
                        int result = H5A.read(attrId, dtypeId, iPtr);
                        if (result < 0)
                        {
                            throw new IOException("Failed to read attribute");
                        }

                        if (H5T.equal(dtypeId, H5T.NATIVE_INT64) > 0)
                        {
                            var dest = new long[1];
                            Marshal.Copy(iPtr, dest, 0, 1);
                            Marshal.FreeHGlobal(iPtr);
                            return dest[0];
                        }
                        else // Must be a string...
                        {
                            var dest = new byte[size];
                            Marshal.Copy(iPtr, dest, 0, size);
                            Marshal.FreeHGlobal(iPtr);
                            return Encoding.ASCII.GetString(dest).TrimEnd((Char)0);
                        }

//                        return null;
                    }
                    finally
                    {
                        if (attrId > 0)
                        {
                            H5A.close(attrId);
                        }
                        if (dtypeId > 0)
                        {
                            H5T.close(dtypeId);
                        }
                    }
                }));
            }
        }
Esempio n. 6
0
        public static (long DatasetId, bool IsNew) OpenOrCreateDataset(long locationId, string datasetPath, long datasetTypeId, Func <long> createDatasetCallback)
        {
            Contract.Requires(createDatasetCallback != null);

            long datasetId            = -1;
            long datasetTypeId_actual = -1;

            bool isNew;

            try
            {
                if (IOHelper.CheckLinkExists(locationId, datasetPath))
                {
                    datasetId            = H5D.open(locationId, datasetPath);
                    datasetTypeId_actual = H5D.get_type(datasetId);

                    if (H5T.equal(datasetTypeId_actual, datasetTypeId) <= 0)
                    {
                        throw new Exception($"{ ErrorMessage.IOHelper_DataTypeMismatch } Dataset: '{ datasetPath }'.");
                    }

                    isNew = false;
                }
                else
                {
                    datasetId = createDatasetCallback.Invoke();

                    isNew = true;
                }

                if (H5I.is_valid(datasetId) <= 0)
                {
                    throw new Exception($"{ ErrorMessage.IOHelper_CouldNotOpenOrCreateDataset } Dataset: '{ datasetPath }'.");
                }
            }
            finally
            {
                if (H5I.is_valid(datasetTypeId_actual) > 0)
                {
                    H5T.close(datasetTypeId_actual);
                }
            }

            return(datasetId, isNew);
        }
Esempio n. 7
0
        public static (long AttributeId, bool IsNew) OpenOrCreateAttribute(long locationId, string name, long attributeTypeId, Func <long> createAttributeCallback)
        {
            long attributeId            = -1;
            long attributeTypeId_actual = -1;

            bool isNew;

            try
            {
                if (H5A.exists(locationId, name) > 0)
                {
                    attributeId            = H5A.open(locationId, name);
                    attributeTypeId_actual = H5A.get_type(attributeId);

                    if (H5T.equal(attributeTypeId_actual, attributeTypeId) <= 0)
                    {
                        throw new Exception($"{ ErrorMessage.IOHelper_DataTypeMismatch } Attribute: '{ name }'.");
                    }

                    isNew = false;
                }
                else
                {
                    attributeId = createAttributeCallback.Invoke();

                    isNew = true;
                }

                if (H5I.is_valid(attributeId) <= 0)
                {
                    throw new Exception($"{ ErrorMessage.IOHelper_CouldNotOpenOrCreateAttribute } Attribute: '{ name }'.");
                }
            }
            finally
            {
                if (H5I.is_valid(attributeTypeId_actual) > 0)
                {
                    H5T.close(attributeTypeId_actual);
                }
            }

            return(attributeId, isNew);
        }
Esempio n. 8
0
        public void H5Aget_typeTest1()
        {
            hid_t att = H5A.create(m_v2_test_file, "A", H5T.IEEE_F64LE,
                                   m_space_scalar);

            Assert.IsTrue(att >= 0);
            hid_t type = H5A.get_type(att);

            Assert.IsTrue(type >= 0);
            Assert.IsTrue(H5T.equal(type, H5T.IEEE_F64LE) > 0);
            Assert.IsTrue(H5T.close(type) >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);

            att = H5A.create(m_v0_test_file, "A", H5T.IEEE_F64LE,
                             m_space_scalar);
            Assert.IsTrue(att >= 0);
            type = H5A.get_type(att);
            Assert.IsTrue(type >= 0);
            Assert.IsTrue(H5T.equal(type, H5T.IEEE_F64LE) > 0);
            Assert.IsTrue(H5T.close(type) >= 0);
            Assert.IsTrue(H5A.close(att) >= 0);
        }
Esempio n. 9
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];
        }
Esempio n. 10
0
        public static Type GetTypeFromHdfTypeId(long typeId)
        {
            long typeId_reference = -1;
            Type type             = null;

            type = null;

            if (H5T.equal(typeId, H5T.NATIVE_UINT8) > 0)
            {
                type = typeof(Byte);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_INT8) > 0)
            {
                type = typeof(SByte);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_UINT16) > 0)
            {
                type = typeof(UInt16);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_INT16) > 0)
            {
                type = typeof(Int16);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_UINT32) > 0)
            {
                type = typeof(UInt32);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_INT32) > 0)
            {
                type = typeof(Int32);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_UINT64) > 0)
            {
                type = typeof(UInt64);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_INT64) > 0)
            {
                type = typeof(Int64);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_FLOAT) > 0)
            {
                type = typeof(Single);
            }
            else if (H5T.equal(typeId, H5T.NATIVE_DOUBLE) > 0)
            {
                type = typeof(Double);
            }
            else if (H5T.equal(typeId, typeId_reference = TypeConversionHelper.GetHdfTypeIdFromType(typeof(string))) > 0)
            {
                type = typeof(string);
            }
            else if (H5T.equal(typeId, typeId_reference = TypeConversionHelper.GetHdfTypeIdFromType(typeof(hdf_aggregate_function_t))) > 0)
            {
                type = typeof(hdf_aggregate_function_t);
            }
            else if (H5T.equal(typeId, typeId_reference = TypeConversionHelper.GetHdfTypeIdFromType(typeof(hdf_tag_t))) > 0)
            {
                type = typeof(hdf_tag_t);
            }
            else if (H5T.equal(typeId, typeId_reference = TypeConversionHelper.GetHdfTypeIdFromType(typeof(hdf_transfer_function_t))) > 0)
            {
                type = typeof(hdf_transfer_function_t);
            }

            if (H5I.is_valid(typeId_reference) > 0)
            {
                H5T.close(typeId_reference);
            }

            if (type == null)
            {
                throw new NotSupportedException();
            }

            return(type);
        }