Ejemplo 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes the entire matrix table
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="matName"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public void SetMatrix <T>(string matName, T[,] data)
        {
            // check that matrix exists
            if (tables.ContainsKey(matName))
            {
                H5DataSetId matId;
                tables.TryGetValue(matName, out matId);

                H5DataTypeId  matDataId = H5D.getType(matId);
                H5DataSpaceId spaceId   = H5S.create_simple(2, Shape);

                long[] start    = { 0, 0 };
                long[] count    = { Shape[0], Shape[1] };
                var    h5matrix = new H5Array <T>(data);

                H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count);
                H5DataSpaceId readSpaceId = H5S.create_simple(2, count);

                H5D.write(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix);
                H5S.close(spaceId);
                H5S.close(readSpaceId);
            }
            else
            {
                Console.WriteLine("table {0} not found in matrix file", matName);
            }
            return;
        }
Ejemplo n.º 3
0
        public static double[,] ReadFieldData2D(string file, string dataSet)
        {
            H5FileId     fileId      = H5F.open(file, H5F.OpenMode.ACC_RDONLY);
            H5DataSetId  fDataSetId  = H5D.open(fileId, dataSet);
            H5DataTypeId fDataTypeId = H5D.getType(fDataSetId);

            long[] dims = H5S.getSimpleExtentDims(H5D.getSpace(fDataSetId)).ToArray();
            double[,] data = new double[dims[0], dims[1]];
            H5D.read(fDataSetId, fDataTypeId, new H5Array <double>(data));

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

            H5T.close(fDataTypeId);
            H5D.close(fDataSetId);
            H5F.close(fileId);

            return(fieldValues);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        //Load weights from hdf5 file. Weights must be saved as a vector per layer
        public static float[] loadH5(string path, string dsname)
        {
            //Get file id
            var h5fid = H5F.open(path, H5F.OpenMode.ACC_RDONLY);
            //Get dataset id
            var h5did = H5D.open(h5fid, dsname);
            //Dataset size
            var h5space = H5D.getSpace(h5did);
            var h5size  = H5S.getSimpleExtentDims(h5space);

            //Dataset size to array
            var S = h5size.ToArray();

            //Empty double array for the data
            double[] data = new double[S[0]];

            //Read the dataset

            var h5array = new H5Array <double>(data);
            var h5dtype = H5D.getType(h5did);

            H5D.read(h5did, h5dtype, h5array);

            //Convert to float
            float[] newarray = new float[data.Length];

            Parallel.For(0, data.Length, (k) =>
            {
                newarray[k] = (float)data[k];
            });

            return(newarray);
        }
Ejemplo n.º 6
0
        // Matrix Specific Methods
        // TODO:
        // 1. add handling for matrix title
        // 2. add specification of NA values
        // 3. other attributes: pa-format flag, year int, source string

        /// <summary>
        /// Returns a row of the matrix
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="matName"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public T[] GetMatrixRow <T>(string matName, int rowIndex)
        {
            var rowData = new T[Shape[1]];

            // check that matrix exists
            if (tables.ContainsKey(matName))
            {
                H5DataSetId matId;
                tables.TryGetValue(matName, out matId);

                H5DataTypeId  matDataId = H5D.getType(matId);
                H5DataSpaceId spaceId   = H5S.create_simple(2, Shape);

                var h5matrix = new H5Array <T>(rowData);

                long[] start = { rowIndex, 0 };
                long[] count = { 1, Shape[1] };
                H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count);
                H5DataSpaceId readSpaceId = H5S.create_simple(2, count);

                H5D.read(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix);
                H5S.close(spaceId);
                H5S.close(readSpaceId);
            }
            else
            {
                Console.WriteLine("table {0} not found in matrix file", matName);
            }
            return(rowData);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获得数据集的类型
        /// </summary>
        public string GetDatasetType(string datasetName)
        {
            H5DataSetId  datasetId = H5D.open(_fileId, datasetName);
            H5DataTypeId typeId    = H5D.getType(datasetId);

            H5T.H5TClass typeClass = H5T.getClass(typeId);
            return(typeClass.ToString());
        }
Ejemplo n.º 8
0
        void LoadRowData()
        {
            var dtype = H5D.getType(Id);
            var size  = H5T.getSize(dtype);

            _row_data = new byte[FindNumberOfRows(), size];
            H5D.read(Id, dtype, new H5Array <byte>(_row_data));

            //TODO: Does this work with more than one row? Dunno. Probs not.
        }
Ejemplo n.º 9
0
        private void createHD5DataObject(H5GroupId h5GroupId, string pathName, ref HD5DataSetObject dataObject)
        {
            H5DataSetId   datasetid  = null;
            H5DataSpaceId spaceid    = null;
            H5DataTypeId  dataTypeid = null;

            try
            {
                dataObject.GroupId     = h5GroupId;
                datasetid              = H5D.open(h5GroupId, pathName);
                dataObject.DatasetID   = datasetid;
                dataObject.DatasetName = pathName;
                spaceid = H5D.getSpace(datasetid);
                var dims = H5S.getSimpleExtentDims(spaceid);
                dataTypeid     = H5D.getType(datasetid);
                dataObject.Dim = dims.Length;
                HDF5DotNet.H5T.H5TClass classType = H5T.getClass(dataTypeid);
                int      size = H5T.getSize(dataTypeid);
                H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT;
                if (classType == H5T.H5TClass.INTEGER)
                {
                    sign = H5T.getSign(dataTypeid);
                }
                //var rank = H5S.getSimpleExtentNDims(space);
                //var statu = H5S.getSimpleExtentDims(space);
                Boolean bString = H5T.isVariableString(dataTypeid);
                //String name = H5T.getMemberName(dataType, 0);
                // var type2 = H5T.getNativeType(dataType, H5T.Direction.DEFAULT);
                Type type = getTypeof(classType, size, sign);
                dataObject.DataType = type;
                dataObject.Data     = readData(dataObject);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally{
                if (datasetid != null)
                {
                    H5D.close(datasetid);
                }
                if (spaceid != null)
                {
                    H5S.close(spaceid);
                }
                if (dataTypeid != null)
                {
                    H5T.close(dataTypeid);
                }
            }
        }
Ejemplo n.º 10
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("未查到指定数据集!");
            }
        }
Ejemplo n.º 11
0
        /// <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);
        }
Ejemplo n.º 12
0
        /// <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);
        }
Ejemplo n.º 13
0
        /// <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);
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        private static int[] GetInt32DataSet(H5FileId dataFile, string path)
        {
            if (H5L.Exists(dataFile, path))
            {
                var          dataSet   = H5D.open(dataFile, path);
                var          space     = H5D.getSpace(dataSet);
                var          size2     = H5S.getSimpleExtentDims(space);
                long         count     = size2[0];
                var          dataArray = new Int32[count];
                var          wrapArray = new H5Array <Int32>(dataArray);
                H5DataTypeId tid1      = H5D.getType(dataSet);

                H5D.read(dataSet, tid1, wrapArray);

                return(dataArray);
            }
            return(null);
        }
Ejemplo n.º 16
0
        //Reading and Printing Methods
        private static double[] GetDoubleDataSet(H5FileId dataFile, string path)
        {
            if (H5L.Exists(dataFile, path))
            {
                H5DataSetId      dataSet   = H5D.open(dataFile, path);
                H5DataSpaceId    space     = H5D.getSpace(dataSet);
                long[]           size2     = H5S.getSimpleExtentDims(space);
                long             count     = size2[0];
                double[]         dataArray = new double[count];
                H5Array <double> wrapArray = new H5Array <double>(dataArray);
                H5DataTypeId     tid1      = H5D.getType(dataSet);

                H5D.read(dataSet, tid1, wrapArray);

                return(dataArray);
            }
            return(null);
        }
Ejemplo n.º 17
0
        public static void GetDataSet <T> (H5FileOrGroupId groupOrFileId, string name, out T[,,,] array, int width, int height, int depth, int number)
        {
            if (groupOrFileId is null)
            {
                throw new ArgumentNullException(nameof(groupOrFileId));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            H5DataSetId  h5DataSetId  = H5D.open(groupOrFileId, name);
            H5DataTypeId h5DataTypeId = H5D.getType(h5DataSetId);

            array = new T[height, width, depth, number];
            H5D.read(h5DataSetId, h5DataTypeId, new H5Array <T> (array));
            H5T.close(h5DataTypeId);
            H5D.close(h5DataSetId);
        }
Ejemplo n.º 18
0
        public static void GetDataSet <T> (H5FileOrGroupId groupOrFileId, string name, out T[] array, int length)
        {
            if (groupOrFileId is null)
            {
                throw new ArgumentNullException(nameof(groupOrFileId));
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            array = new T[length];
            H5DataSetId  h5DataSetId  = H5D.open(groupOrFileId, name);
            H5DataTypeId h5DataTypeId = H5D.getType(h5DataSetId);

            H5D.read(h5DataSetId, h5DataTypeId, new H5Array <T> (array));
            H5T.close(h5DataTypeId);
            H5D.close(h5DataSetId);
        }
Ejemplo n.º 19
0
        public static T ReadScalar <T>(H5FileId fileId, string datasetName)
        {
            H5DataSetId   dataset  = null;
            H5DataSpaceId space    = null;
            H5DataTypeId  dataType = null;

            long[] dims;
            T      data = default(T);

            try
            {
                dataset  = H5D.open(fileId, datasetName);
                space    = H5D.getSpace(dataset);
                dims     = H5S.getSimpleExtentDims(space);
                dataType = H5D.getType(dataset);

                H5D.readScalar <T>(dataset, dataType, ref data);
                if (typeof(T) == typeof(string))
                {
                    int    stringLength = H5T.getSize(dataType);
                    byte[] buffer       = new byte[2 * stringLength];
                    H5D.read(dataset, dataType, new H5Array <byte>(buffer));
                    string stuff = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                    return((T)(stuff.SplitInParts(stringLength).Select(ss => (T)(object)ss)));
                }
                return(data);
            }
            catch
            {
                return(default(T));
            }
            finally
            {
                if (space != null)
                {
                    H5S.close(space);
                }
                if (dataset != null)
                {
                    H5D.close(dataset);
                }
            }
        }
Ejemplo n.º 20
0
        public static T[] Read1DArray <T>(H5FileId fileId, string dataSetName)
        {
            H5DataSetId   dataset  = null;
            H5DataSpaceId space    = null;
            H5DataTypeId  dataType = null;

            long[] dims;

            try
            {
                dataset  = H5D.open(fileId, dataSetName);
                space    = H5D.getSpace(dataset);
                dims     = H5S.getSimpleExtentDims(space);
                dataType = H5D.getType(dataset);
                if (typeof(T) == typeof(string))
                {
                    int    stringLength = H5T.getSize(dataType);
                    int    a            = (int)dims[0];
                    byte[] buffer       = new byte[(int)(dims[0]) * stringLength];
                    H5D.read(dataset, dataType, new H5Array <byte>(buffer));
                    string stuff = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                    return(stuff.SplitInParts(stringLength).Select(ss => (T)(object)ss).ToArray());
                }
                T[] dataArray = new T[dims[0]];
                var wrapArray = new H5Array <T>(dataArray);
                H5D.read(dataset, dataType, wrapArray);
                return(dataArray);
            }
            catch {
                return(null);
            }
            finally
            {
                if (space != null)
                {
                    H5S.close(space);
                }
                if (dataset != null)
                {
                    H5D.close(dataset);
                }
            }
        }
Ejemplo n.º 21
0
        public static T[] Read1DArray <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))
            {
                int    stringLength = H5T.getSize(dataType);
                byte[] buffer       = new byte[dims[0] * stringLength];
                H5D.read(dataset, dataType, new H5Array <byte>(buffer));
                string stuff = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                return(stuff.SplitInParts(stringLength).Select(ss => (T)(object)ss).ToArray());
            }
            T[] dataArray = new T[dims[0]];
            var wrapArray = new H5Array <T>(dataArray);

            H5D.read(dataset, dataType, wrapArray);
            return(dataArray);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 暂时不用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileId"></param>
        /// <param name="datasetName"></param>
        /// <param name="groupName"></param>
        /// <param name="datasetOut"></param>
        /// <param name="rowIndex"></param>
        /// <param name="rowcount"></param>
        /// <param name="colcount"></param>
        public void GetDataset <T>(H5FileId fileId, string datasetName, string groupName, T[,] datasetOut, int rowIndex, int rowcount, int colcount)
        {
            H5GroupId    groupId   = H5G.open(fileId, groupName);
            H5DataSetId  dataSetId = H5D.open(groupId, datasetName /*"EV_Emissive"*/);
            H5DataTypeId tid0      = H5D.getType(dataSetId);

            H5DataSpaceId spaceid = H5D.getSpace(dataSetId);


            long[] start = new long[2];
            start[0] = rowIndex;
            start[1] = 0;
            long[] count = new long[2];
            count[0] = rowcount;
            count[1] = colcount;

            H5S.selectHyperslab(spaceid, H5S.SelectOperator.SET, start, count);

            //long[] dimes = new long[2];
            //dimes[0] = 1;
            //dimes[1] = 8192;

            H5DataSpaceId simpleSpaceid = H5S.create_simple(2, count);


            H5PropertyListId listid = new H5PropertyListId(H5P.Template.DEFAULT);

            H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_INT);//数据类型

            // Read the array back

            //int[,] dataSet = new int[cout[0], cout[1]];
            H5D.read(dataSetId, tid1, simpleSpaceid, spaceid, listid, new H5Array <T>(datasetOut));

            H5S.close(simpleSpaceid);
            H5S.close(spaceid);
            H5T.close(tid0);
            H5D.close(dataSetId);
            H5G.close(groupId);
        }
Ejemplo n.º 23
0
        private static T[, ,] Read3DArray <T>(H5GroupId groupID, string name)
        {
            var dataset  = H5D.open(groupID, name);
            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], 0, 0];
                return(dataArray);
            }
            else
            {
                T[, ,] dataArray = new T[dims[0], dims[1], dims[2]];
                var wrapArray = new H5Array <T>(dataArray);
                H5D.read(dataset, dataType, wrapArray);
                return(dataArray);
            }
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
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];
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Method to read an individual cell or block of data from a matrix
        /// rowIndex and colIndex are the zero-based offsets into the matrix
        /// rowLength and colLength are the size of the arrays (min 1)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="matName"></param>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        /// <param name="rowLength"></param>
        /// <param name="colLength"></param>
        /// <returns></returns>
        public T[,] GetMatrixBlock <T>(string matName, int rowIndex, int colIndex, int rowLength, int colLength)
        {
            // check that block parameters are legit
            if ((rowLength < 1 | colLength < 1) | (rowIndex + rowLength > Shape[0] | colIndex + colLength > Shape[1]))
            {
                Console.WriteLine("invalid block size and/or index, must be non-zero in both dimensions and within matrix size");
                return(null);
            }
            var blockData = new T[rowLength, colLength];

            // check that matrix exists
            if (tables.ContainsKey(matName))
            {
                H5DataSetId matId;
                tables.TryGetValue(matName, out matId);

                H5DataTypeId  matDataId = H5D.getType(matId);
                H5DataSpaceId spaceId   = H5S.create_simple(2, Shape);

                var h5matrix = new H5Array <T>(blockData);

                long[] start = { rowIndex, colIndex };
                long[] count = { rowLength, colLength };
                H5S.selectHyperslab(spaceId, H5S.SelectOperator.SET, start, count);
                H5DataSpaceId readSpaceId = H5S.create_simple(2, count);

                H5D.read(matId, matDataId, readSpaceId, spaceId, H5P.create(H5P.PropertyListClass.DATASET_XFER), h5matrix);
                H5S.close(spaceId);
                H5S.close(readSpaceId);
            }
            else
            {
                Console.WriteLine("table {0} not found in matrix file", matName);
            }

            return(blockData);
        }
Ejemplo n.º 27
0
        void FindAttribute(int i)
        {
            var attr_field = "FIELD_" + i + "_NAME";

            var attr  = H5A.open(Id, attr_field);
            var dtype = H5A.getType(attr);
            var size  = H5T.getSize(dtype);

            var mtype  = H5T.create(H5T.CreateClass.STRING, size);
            var buffer = new byte[size];

            H5A.read(attr, mtype, new H5Array <byte>(buffer));

            var attr_datatype = H5T.getMemberType(H5D.getType(Id), i);
            var attr_size     = H5T.getSize(attr_datatype);

            var attr_class = H5T.getMemberClass(H5D.getType(Id), i).ToString();
            var attr_name  = Encoding.GetString(buffer).Replace('\0', ' ').Trim();

            switch (attr_class)
            {
            case "STRING":
                _attributes[i] = new StringAttribute(attr_name, attr_size);
                break;

            case "INTEGER":
                _attributes[i] = new IntegerAttribute(attr_name, attr_size);
                break;

            case "FLOAT":
                _attributes[i] = new FloatingPointAttribute(attr_name, attr_size);
                break;

            default:
                throw new ArgumentException("Unknown attribute type " + attr_class, "attr_type");
            }
        }
Ejemplo n.º 28
0
        private static T[] Read1DArray <T>(H5GroupId fileId, string dataSetName)
        {
            var dataset  = H5D.open(fileId, dataSetName);
            var space    = H5D.getSpace(dataset);
            var dims     = H5S.getSimpleExtentDims(space);
            var dataType = H5D.getType(dataset);

            T[] dataArray = null;
            if (typeof(T) == typeof(string))
            {
                // this will also need a string hack...
                dataArray = new T[dims[0]];
                H5D.close(dataset);
                return(dataArray);
            }
            else
            {
                dataArray = new T[dims[0]];
                var wrapArray = new H5Array <T>(dataArray);
                H5D.read(dataset, dataType, wrapArray);
                H5D.close(dataset);
                return(dataArray);
            }
        }
Ejemplo n.º 29
0
        private static void ReadFile(string filePath)
        {
            var file = H5F.open(filePath, H5F.OpenMode.ACC_RDONLY);

            var dataSet = H5D.open(file, "/group/dataset");

            var fileSpace = H5D.getSpace(dataSet);

            var rank = H5S.getSimpleExtentNDims(fileSpace);

            WriteLine("Rank: {0}", rank);

            var dims = H5S.getSimpleExtentDims(fileSpace);

            Write("Dims:");
            foreach (var d in dims)
            {
                Write(" {0}", d);
            }
            WriteLine();

            H5S.close(fileSpace);

            var ints         = new int[1];
            var intAttribute = H5A.openName(dataSet, "int");

            H5A.read(intAttribute, H5A.getType(intAttribute), new H5Array <int>(ints));
            WriteLine("int: {0}", ints[0]);
            H5A.close(intAttribute);

            var stringAttribute = H5A.openName(dataSet, "string");
            var stringType      = H5A.getType(stringAttribute);
            var stringSize      = H5T.getSize(stringType);

            WriteLine("string length: {0}", stringSize);
            var buffer = new byte[stringSize];

            H5A.read(stringAttribute, stringType, new H5Array <byte>(buffer));
            WriteLine("string: {0}", Encoding.ASCII.GetString(buffer));
            H5T.close(stringType);
            H5A.close(stringAttribute);

            if (rank == 2)
            {
                var data = new int[dims[0], dims[1]];
                H5D.read(dataSet, H5D.getType(dataSet), new H5Array <int>(data));

                for (int i = 0; i < data.GetLength(0); ++i)
                {
                    for (int j = 0; j < data.GetLength(1); ++j)
                    {
                        Write(" {0}", data[i, j]);
                    }

                    WriteLine();
                }
            }

            H5D.close(dataSet);

            H5F.close(file);
        }
Ejemplo n.º 30
0
        public SkimMatrix Read(string filename, int field, float scale)
        {
            Console.WriteLine("Reading {0}", filename);
            int hdf5NameEnd = filename.IndexOf("/");

            // the first part of the name in the roster file is the omx/hdf5 file
            string HDFName = filename.Substring(0, hdf5NameEnd);

            //rename filename to be only the name of the skim matrix inside of the skim file
            //skims are stored in the "data" folder within the omx/hdf5 file
            filename = filename.Substring(hdf5NameEnd);

            string hdfFile = _path + "\\" + HDFName;

            var  dataFile = H5F.open(hdfFile, H5F.OpenMode.ACC_RDONLY);
            var  dataSet  = H5D.open(dataFile, filename);
            var  space    = H5D.getSpace(dataSet);
            var  size2    = H5S.getSimpleExtentDims(space);
            long nRows    = size2[0];
            long nCols    = size2[1];
            long numZones = _mapping.Count();

            // if the count in the hdf5 file is larger than the number of
            // tazs in the mapping, ignore the values over the total number
            //of tazs in the mapping because these are not valid zones.
            _matrix = new ushort[numZones][];
            for (var i = 0; i < numZones; i++)
            {
                _matrix[i] = new ushort[numZones];
            }

            //OMX is a square matrix of doubles
            //In addition to the data folder for matrices, an OMX file has a lookup folder
            //with a zone mapping vector.  However, this is ignored since DaySim also has one.
            //Therefore, it is assumed the OMX matrix does not skip rows/cols and every row/col
            //corresponds to an actual zone in the DaySim zone mapping file by index
            //Scaling should be set to TRUE since OMX stores doubles (not scaled integers)
            var          dataArray = new double[nRows, nCols];
            var          wrapArray = new H5Array <double>(dataArray);
            H5DataTypeId tid1      = H5D.getType(dataSet);

            H5D.read(dataSet, tid1, wrapArray);

            for (var row = 0; row < nRows; row++)
            {
                if (_mapping.ContainsKey(row + 1))
                {
                    for (var col = 0; col < nCols; col++)
                    {
                        if (_mapping.ContainsKey(col + 1))
                        {
                            var value = dataArray[row, col] * scale;

                            if (value > 0)
                            {
                                if (value > ushort.MaxValue - 1)
                                {
                                    value = ushort.MaxValue - 1;
                                }

                                _matrix[_mapping[row + 1]][_mapping[col + 1]] = (ushort)value;
                            }
                        }
                    }
                }
            }

            var skimMatrix = new SkimMatrix(_matrix);

            return(skimMatrix);
        }