示例#1
0
        public static int WriteDataset <T>(int groupId, string name, T[,] dset) where T : struct
        {
            ulong[] dims     = new ulong[] { (ulong)dset.GetLength(0), (ulong)dset.GetLength(1) };
            ulong[] maxDims  = null;
            var     spaceId  = H5S.create_simple(2, dims, maxDims);
            var     datatype = GetDatatype(typeof(T));
            var     typeId   = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
                //var wdata = Encoding.ASCII.GetBytes((char[,]) dset);
            }
            name = ToHdf5Name(name);
            var      datasetId = H5D.create(groupId, name, datatype, spaceId);
            GCHandle hnd       = GCHandle.Alloc(dset, GCHandleType.Pinned);
            var      result    = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                           hnd.AddrOfPinnedObject());

            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);
            return(result);
        }
示例#2
0
        public void H5DreadTest1()
        {
            byte[] rdata = new byte[512];

            hid_t mem_type = H5T.copy(H5T.C_S1);

            Assert.IsTrue(H5T.set_size(mem_type, new IntPtr(2)) >= 0);

            GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.read(m_v0_ascii_dset, mem_type, H5S.ALL,
                                   H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            for (int i = 0; i < 256; ++i)
            {
                // H5T.FORTRAN_S1 is space (= ASCII dec. 32) padded
                if (i != 32)
                {
                    Assert.IsTrue(rdata[2 * i] == (byte)i);
                }
                else
                {
                    Assert.IsTrue(rdata[64] == (byte)0);
                }
                Assert.IsTrue(rdata[2 * i + 1] == (byte)0);
            }
            hnd.Free();

            Assert.IsTrue(H5T.close(mem_type) >= 0);
        }
示例#3
0
        public static long OpenHDFDataType(Type clrType, long maxSize = 1)
        {
            if (clrType == typeof(double))
            {
                return(H5T.copy(H5T.NATIVE_DOUBLE));
            }

            if (clrType == typeof(float))
            {
                return(H5T.copy(H5T.NATIVE_FLOAT));
            }

            if (clrType == typeof(int))
            {
                return(H5T.copy(H5T.NATIVE_INT32));
            }

            if (clrType == typeof(long))
            {
                return(H5T.copy(H5T.NATIVE_INT64));
            }

            if (clrType == typeof(string))
            {
                var type   = H5T.copy(H5T.C_S1);
                var ptr    = new IntPtr(maxSize);//Leak?
                var status = H5T.set_size(type, ptr);
                Debug.Assert(status >= 0);
                return(type);
            }
            throw new NotImplementedException();
        }
        public static int WriteDatasetFromArray <T>(hid_t groupId, string name, Array dset, string datasetName = null) //where T : struct
        {
            int rank = dset.Rank;

            ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)dset.GetLength(i)); }).ToArray();

            ulong[] maxDims  = null;
            var     spaceId  = H5S.create_simple(rank, dims, maxDims);
            var     datatype = GetDatatype(typeof(T));
            var     typeId   = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }
            var      datasetId = H5D.create(groupId, name, datatype, spaceId);
            GCHandle hnd       = GCHandle.Alloc(dset, GCHandleType.Pinned);
            var      result    = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                           hnd.AddrOfPinnedObject());

            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);
            return(result);
        }
示例#5
0
        public static IEnumerable <OffsetInfo> GetCompoundInfo(Type type, bool ieee = false)
        {
            //Type t = typeof(T);
            var strtype = H5T.copy(H5T.C_S1);
            int strsize = (int)H5T.get_size(strtype);
            int curSize = 0;
            List <OffsetInfo> offsets = new List <OffsetInfo>();

            foreach (var x in type.GetFields())
            {
                OffsetInfo oi = new OffsetInfo()
                {
                    name     = x.Name,
                    type     = x.FieldType,
                    datatype = ieee ? GetDatatypeIEEE(x.FieldType) : GetDatatype(x.FieldType),
                    size     = x.FieldType == typeof(string) ? stringLength(x) : Marshal.SizeOf(x.FieldType),
                    offset   = 0 + curSize
                };
                if (oi.datatype == H5T.C_S1)
                {
                    strtype = H5T.copy(H5T.C_S1);
                    H5T.set_size(strtype, new IntPtr(oi.size));
                    oi.datatype = strtype;
                }
                if (oi.datatype == H5T.STD_I64BE)
                {
                    oi.size = oi.size * 2;
                }
                curSize = curSize + oi.size;

                offsets.Add(oi);
            }
            H5T.close(strtype);
            return(offsets);
        }
示例#6
0
        public static unsafe void AddDataWithSharedDataType(long fileId, ContainerType container)
        {
            long typeId = H5T.copy(H5T.C_S1);

            H5T.set_size(typeId, H5T.VARIABLE);
            H5T.set_cset(typeId, H5T.cset_t.UTF8);
            H5T.commit(fileId, "string_t", typeId);

            var data     = new string[] { "001", "11", "22", "33", "44", "55", "66", "77", "  ", "AA", "ZZ", "!!" };
            var dataChar = data
                           .SelectMany(value => Encoding.ASCII.GetBytes(value + '\0'))
                           .ToArray();

            fixed(byte *dataVarPtr = dataChar)
            {
                var basePtr = new IntPtr(dataVarPtr);

                var addresses = new IntPtr[]
                {
                    IntPtr.Add(basePtr, 0), IntPtr.Add(basePtr, 4), IntPtr.Add(basePtr, 7), IntPtr.Add(basePtr, 10),
                    IntPtr.Add(basePtr, 13), IntPtr.Add(basePtr, 16), IntPtr.Add(basePtr, 19), IntPtr.Add(basePtr, 22),
                    IntPtr.Add(basePtr, 25), IntPtr.Add(basePtr, 28), IntPtr.Add(basePtr, 31), IntPtr.Add(basePtr, 34)
                };

                fixed(void *dataVarAddressesPtr = addresses)
                {
                    TestUtils.Add(container, fileId, "shared_data_type", "shared_data_type", typeId, dataVarAddressesPtr, length: 12);
                }
            }

            if (H5I.is_valid(typeId) > 0)
            {
                H5T.close(typeId);
            }
        }
示例#7
0
        private static double[,] ReadDataArray(hid_t fileLoc, string name, bool transpose = false)
        {
            hid_t dset   = H5D.open(fileLoc, name);
            hid_t fspace = H5D.get_space(dset);
            hid_t count  = H5S.get_simple_extent_ndims(fspace);

            hid_t type = H5D.get_type(dset);

            hsize_t[] dims    = new hsize_t[count];
            hsize_t[] maxdims = new hsize_t[count];

            H5S.get_simple_extent_dims(fspace, dims, maxdims);
            H5S.close(fspace);


            byte[] rdata = new byte[dims[0] * dims[1] * 8];

            hid_t mem_type = H5T.copy(H5T.NATIVE_DOUBLE);

            H5T.set_size(mem_type, new IntPtr(8));

            GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(mem_type);

            if (transpose)
            {
                double[,] val = new double[dims[1], dims[0]];
                int cnt = 0;
                for (int i = 0; i < (int)dims[0]; i++)
                {
                    for (int j = 0; j < (int)dims[1]; j++)
                    {
                        val[j, i] = BitConverter.ToDouble(rdata, cnt * 8);
                        cnt++;
                    }
                }
                return(val);
            }
            else
            {
                double[,] val = new double[dims[0], dims[1]];
                int cnt = 0;
                for (int i = 0; i < (int)dims[0]; i++)
                {
                    for (int j = 0; j < (int)dims[1]; j++)
                    {
                        val[i, j] = BitConverter.ToDouble(rdata, cnt * 8);
                        cnt++;
                    }
                }
                return(val);
            }
        }
        public static unsafe void AddString(long fileId, ContainerType container)
        {
            long res;

            var dims = new ulong[] { 2, 2, 3 }; /* "extendible contiguous non-external dataset not allowed" */

            // fixed length string attribute (ASCII)
            var typeIdFixed = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdFixed, new IntPtr(2));
            res = H5T.set_cset(typeIdFixed, H5T.cset_t.ASCII);

            var dataFixed     = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", "  ", "AA", "ZZ", "!!" };
            var dataFixedChar = dataFixed
                                .SelectMany(value => Encoding.ASCII.GetBytes(value))
                                .ToArray();

            TestUtils.Add(container, fileId, "string", "fixed", typeIdFixed, dataFixedChar.AsSpan(), dims);

            res = H5T.close(typeIdFixed);

            // variable length string attribute (ASCII)
            var typeIdVar = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdVar, H5T.VARIABLE);
            res = H5T.set_cset(typeIdVar, H5T.cset_t.ASCII);

            var dataVar       = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", "  ", "AA", "ZZ", "!!" };
            var dataVarIntPtr = dataVar.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray();

            TestUtils.Add(container, fileId, "string", "variable", typeIdVar, dataVarIntPtr.AsSpan(), dims);

            foreach (var ptr in dataVarIntPtr)
            {
                Marshal.FreeCoTaskMem(ptr);
            }

            res = H5T.close(typeIdVar);

            // variable length string attribute (UTF8)
            var typeIdVarUTF8 = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdVarUTF8, H5T.VARIABLE);
            res = H5T.set_cset(typeIdVarUTF8, H5T.cset_t.UTF8);

            var dataVarUTF8       = new string[] { "00", "11", "22", "33", "44", "55", "66", "77", "  ", "ÄÄ", "的的", "!!" };
            var dataVarUTF8IntPtr = dataVarUTF8.Select(x => Marshal.StringToCoTaskMemUTF8(x)).ToArray();

            TestUtils.Add(container, fileId, "string", "variableUTF8", typeIdVarUTF8, dataVarUTF8IntPtr.AsSpan(), dims);

            foreach (var ptr in dataVarUTF8IntPtr)
            {
                Marshal.FreeCoTaskMem(ptr);
            }

            res = H5T.close(typeIdVarUTF8);
        }
示例#9
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);
        }
        /// <summary>
        /// Reads an n-dimensional dataset.
        /// </summary>
        /// <typeparam name="T">Generic parameter strings or primitive type</typeparam>
        /// <param name="groupId">id of the group. Can also be a file Id</param>
        /// <param name="name">name of the dataset</param>
        /// <returns>The n-dimensional dataset</returns>
        public static Array ReadDatasetToArray <T>(hid_t groupId, string name) //where T : struct
        {
            var datatype = GetDatatype(typeof(T));


            var   datasetId = H5D.open(groupId, name);
            var   spaceId   = H5D.get_space(datasetId);
            int   rank      = H5S.get_simple_extent_ndims(spaceId);
            long  count     = H5S.get_simple_extent_npoints(spaceId);
            Array dset;
            Type  type = typeof(T);

            if (rank >= 0 && count >= 0)
            {
                int     rankChunk;
                ulong[] maxDims   = new ulong[rank];
                ulong[] dims      = new ulong[rank];
                ulong[] chunkDims = new ulong[rank];
                hid_t   memId     = H5S.get_simple_extent_dims(spaceId, dims, maxDims);
                long[]  lengths   = dims.Select(d => Convert.ToInt64(d)).ToArray();
                dset = Array.CreateInstance(type, lengths);
                var typeId   = H5D.get_type(datasetId);
                var mem_type = H5T.copy(datatype);
                if (datatype == H5T.C_S1)
                {
                    H5T.set_size(datatype, new IntPtr(2));
                }

                var propId = H5D.get_create_plist(datasetId);

                if (H5D.layout_t.CHUNKED == H5P.get_layout(propId))
                {
                    rankChunk = H5P.get_chunk(propId, rank, chunkDims);
                }

                memId = H5S.create_simple(rank, dims, maxDims);
                GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned);
                H5D.read(datasetId, datatype, memId, spaceId,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());
                hnd.Free();
            }
            else
            {
                dset = Array.CreateInstance(type, new long[1] {
                    0
                });
            }
            H5D.close(datasetId);
            H5S.close(spaceId);
            return(dset);
        }
示例#11
0
        public static int WriteDatasetFromArray <T>(hid_t groupId, string name, Array dset, string datasetName = null) //where T : struct
        {
            int rank = dset.Rank;

            ulong[] dims = Enumerable.Range(0, rank).Select(i => { return((ulong)dset.GetLength(i)); }).ToArray();

            ulong[] maxDims  = null;
            var     spaceId  = H5S.create_simple(rank, dims, maxDims);
            var     datatype = GetDatatype(typeof(T));
            var     typeId   = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }
            long dcpl = H5P.DEFAULT;

            if (datatype != H5T.C_S1)
            {
                Console.WriteLine("Do you want to compress the file? (Y/N)");
                string response = "N";
                response = Console.ReadLine();
                if (response == "Y" || response == "y")
                {
                    var   cnt   = dset.Length;
                    var   log10 = (int)Math.Log10(cnt);
                    ulong pow   = (ulong)Math.Pow(10, log10);
                    //ulong c_s = Math.Min(1000, pow);
                    ulong[] chunk_size = new ulong[] { pow };
                    //chunk_size[0] = ulong.Parse(Console.ReadLine());
                    Console.WriteLine("Auto-Chunking with chunk-size of {0} samples.", pow);
                    dcpl = create_property(chunk_size);
                }
                else
                {
                    Console.WriteLine("Creating an Uncompressed File...");
                }
            }
            var      datasetId = H5D.create(groupId, name, datatype, spaceId, H5P.DEFAULT, dcpl);
            GCHandle hnd       = GCHandle.Alloc(dset, GCHandleType.Pinned);
            var      result    = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                           hnd.AddrOfPinnedObject());

            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(typeId);
            return(result);
        }
示例#12
0
        private static hid_t AddDataString(hid_t parentLoc, string name, string data)
        {
            byte[][] wdata = new byte[1][];
            wdata[0] = ASCIIEncoding.ASCII.GetBytes(data);
            int n = wdata[0].Length + 1;

            /*
             * Create file and memory datatypes.  For this example we will save
             * the strings as FORTRAN strings, therefore they do not need space
             * for the null terminator in the file.
             */


            hsize_t[] dims = new hsize_t[1];
            dims[0] = (hsize_t)1;
            hid_t  filetype = H5T.copy(H5T.FORTRAN_S1);
            herr_t status   = H5T.set_size(filetype, new IntPtr(n - 1));
            hid_t  memtype  = H5T.copy(H5T.C_S1);

            status = H5T.set_size(memtype, new IntPtr(n));

            /*
             * Create dataspace.  Setting maximum size to NULL sets the maximum
             * size to be the current size.
             */
            hid_t space = H5S.create_simple(1, dims, null);

            /*
             * Create the dataset and write the string data to it.
             */
            hid_t dset = H5D.create(parentLoc, name, filetype, space, H5P.DEFAULT, H5P.DEFAULT,
                                    H5P.DEFAULT);
            GCHandle hnd = GCHandle.Alloc(wdata[0], GCHandleType.Pinned);

            // herr_t flag= H5D.write(dataSetId, type, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());
            status = H5D.write(dset, memtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            /*
             * Close and release resources.
             */
            status = H5D.close(dset);
            status = H5S.close(space);
            status = H5T.close(filetype);
            status = H5T.close(memtype);

            return(dset);
        }
示例#13
0
        public static int WriteAsciiString(hid_t groupId, string name, string str)
        {
            var spaceNullId   = H5S.create(H5S.class_t.NULL);
            var spaceScalarId = H5S.create(H5S.class_t.SCALAR);

            // create two datasets of the extended ASCII character set
            // store as H5T.FORTRAN_S1 -> space padding

            int strLength = str.Length;

            ulong[] dims = { (ulong)strLength, 1 };

            /* Create the dataset. */
            //name = ToHdf5Name(name);

            var spaceId   = H5S.create_simple(1, dims, null);
            var datasetId = H5D.create(groupId, name,
                                       H5T.FORTRAN_S1, spaceId);

            H5S.close(spaceId);

            // we write from C and must provide null-terminated strings

            byte[] wdata = new byte[strLength * 2];
            //for (int i = 0; i < strLength; ++i)
            //{
            //    wdata[2 * i] = (byte)i;
            //}
            for (int i = 0; i < strLength; ++i)
            {
                wdata[2 * i] = Convert.ToByte(str[i]);
            }

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

            H5T.set_size(memId, new IntPtr(2));
            //H5T.set_strpad(memId, H5T.str_t.NULLTERM);
            GCHandle hnd    = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      result = H5D.write(datasetId, memId, H5S.ALL,
                                        H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();
            H5T.close(memId);
            H5D.close(datasetId);
            return(result);
        }
示例#14
0
        private static string ReadDataCharArray(hid_t fileLoc, string name)
        {
            hid_t dset   = H5D.open(fileLoc, name);
            hid_t fspace = H5D.get_space(dset);
            hid_t count  = H5S.get_simple_extent_ndims(fspace);

            hid_t type = H5D.get_type(dset);


            hsize_t[] dims    = new hsize_t[count];
            hsize_t[] maxdims = new hsize_t[count];

            H5S.get_simple_extent_dims(fspace, dims, maxdims);
            H5S.close(fspace);


            byte[] rdata = new byte[dims[0]];

            hid_t mem_type = H5T.copy(type);

            H5T.set_size(mem_type, new IntPtr(1));

            GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(mem_type);

            char[] val = new char[dims[0]];
            for (int i = 0; i < (int)dims[0]; i += 2)
            {
                val[i] = BitConverter.ToChar(rdata, i);
            }


            return(val.ToString());
        }
示例#15
0
        public static T[,] ReadDataset <T>(int groupId, string name) where T : struct
        {
            var      datatype = GetDatatype(typeof(T));

            name = ToHdf5Name(name);

            var      datasetId = H5D.open(groupId, name);
            var      spaceId   = H5D.get_space(datasetId);
            int      rank      = H5S.get_simple_extent_ndims(spaceId);
            long     count     = H5S.get_simple_extent_npoints(spaceId);
            int      rankChunk;
            ulong[]  maxDims   = new ulong[rank];
            ulong[]  dims      = new ulong[rank];
            ulong[]  chunkDims = new ulong[rank];
            var      memId     = H5S.get_simple_extent_dims(spaceId, dims, maxDims);
            T[,] dset = new T[dims[0], dims[1]];
            var      typeId   = H5D.get_type(datasetId);
            var      mem_type = H5T.copy(datatype);
            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }

            var      propId = H5D.get_create_plist(datasetId);

            if (H5D.layout_t.CHUNKED == H5P.get_layout(propId))
            {
                rankChunk = H5P.get_chunk(propId, rank, chunkDims);
            }

            memId = H5S.create_simple(rank, dims, maxDims);
            GCHandle hnd = GCHandle.Alloc(dset, GCHandleType.Pinned);
            H5D.read(datasetId, datatype, memId, spaceId,
                     H5P.DEFAULT, hnd.AddrOfPinnedObject());
            hnd.Free();
            H5D.close(typeId);
            H5D.close(datasetId);
            H5S.close(spaceId);
            return(dset);
        }
        private static hid_t CreateFixedStringType(byte[] bytes, bool utf8)
        {
#if true
            var type = H5T.create(H5T.class_t.STRING, new IntPtr(bytes.Length));
            if (type < 0)
            {
                throw new Exception("Failed to create string type");
            }
#else
            var type = H5T.copy(H5T.C_S1);
            if (type < 0)
            {
                throw new Exception("Failed to create string type");
            }

            // TODO: bytes.Length or bytes.Length + 1?
            if (H5T.set_size(type, new IntPtr(bytes.Length)) < 0)
            {
                H5T.close(type);
                throw new Exception("Failed to set type size");
            }
#endif

            if (utf8)
            {
                if (H5T.set_cset(type, H5T.cset_t.UTF8) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set cset");
                }
            }

            if (H5T.set_strpad(type, H5T.str_t.NULLTERM) < 0)
            {
                H5T.close(type);
                throw new Exception("Failed to set strpad");
            }

            return(type);
        }
示例#17
0
        public static IEnumerable ReadStrings(string filename, string dataset)
        {
            var f = H5F.open(filename, H5F.ACC_RDONLY);

            if (f < 0)
            {
                throw new Exception("Could not open file: " + filename);
            }

            var dset = H5D.open(f, Encoding.ASCII.GetBytes(dataset), H5P.DEFAULT);

            if (dset < 0)
            {
                throw new Exception("Could not open dataset: " + dataset);
            }

            var filetype = H5D.get_type(dset);
            var sdim     = H5T.get_size(filetype) + 1;

            var space = H5D.get_space(dset);
            var ndims = H5S.get_simple_extent_ndims(space);

            ulong[] dims = new ulong[ndims];
            H5S.get_simple_extent_dims(space, dims, null);
            var memtype = H5T.copy(H5T.C_S1);
            var status  = H5T.set_size(memtype, sdim);

            int len = (int)(dims[0] * (ulong)sdim * SIZEOF_CHAR);

            byte[] buffer = new byte[len];
            IntPtr ptr    = Marshal.AllocHGlobal(len);

            status = H5D.read(dset, memtype, H5S.ALL, H5S.ALL, H5P.DEFAULT, ptr);
            Marshal.Copy(ptr, buffer, 0, len);
            Marshal.FreeHGlobal(ptr);

            string s = Encoding.ASCII.GetString(buffer);

            return(s.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries));
        }
示例#18
0
        private static double ReadDataValue(hid_t fileLoc, string name)
        {
            hid_t dset = H5D.open(fileLoc, name);

            byte[] rdata = new byte[8];

            hid_t mem_type = H5T.copy(H5T.NATIVE_DOUBLE);

            H5T.set_size(mem_type, new IntPtr(8));

            GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(mem_type);

            double val = BitConverter.ToDouble(rdata, 0);

            return(val);
        }
示例#19
0
        private static string ReadDataString(hid_t fileLoc, string name)
        {
            hid_t dset = H5D.open(fileLoc, name);
            hid_t type = H5D.get_type(dset);
            // H5T.is_variable_str(type);
            IntPtr size = H5T.get_size(type);

            hid_t fspace   = H5D.get_space(dset);
            hid_t mem_type = H5T.copy(type);

            H5T.set_size(mem_type, size);


            byte[]   buffer = new byte[size.ToInt32()];
            GCHandle hnd    = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(mem_type);

            // remove the last "/0"
            if (buffer[buffer.Length - 1] == 0)
            {
                byte[] buffer2 = new byte[buffer.Length - 1];
                for (int i = 0; i < buffer2.Length; i++)
                {
                    buffer2[i] = buffer[i];
                }


                return(ASCIIEncoding.ASCII.GetString(buffer2));
            }
            return(ASCIIEncoding.ASCII.GetString(buffer));
        }
示例#20
0
        //public static void WriteStringsToAscii(int groupId, string name, IEnumerable<string> strs)
        //{
        //    var maxLength = strs.Select(s => s.Length).Max();
        //    ulong strSz = (ulong)strs.Count();
        //    var buf = strs.SelectMany(s => s.PadRight(maxLength).ToCharArray()).ToArray();

        //    ulong[] dimsa = new ulong[] { strSz, 1 };

        //    /* Create the data space for the dataset. */
        //    ulong[] dims = new ulong[] { strSz, (ulong)maxLength + 1 };
        //    var spaceId = H5S.create_simple(2, dims, null);

        //    /* Create the dataset. */
        //    name = ToHdf5Name(name);
        //    var datasetId = H5D.create(groupId, name, H5T.STD_I32BE, spaceId, H5P.DEFAULT);

        //    var aId = H5S.create_simple(2, dimsa, null);

        //    var aTypeId = H5T.copy(H5T.C_S1);
        //    H5T.set_size(aTypeId, new IntPtr(maxLength));

        //    var attr = H5A.create(datasetId, "string-att", aTypeId, aId, H5P.DEFAULT);

        //    GCHandle hnd = GCHandle.Alloc(buf, GCHandleType.Pinned);
        //    H5A.write(attr, aTypeId, hnd.AddrOfPinnedObject());
        //    hnd.Free();

        //    /* End access to the dataset and release resources used by it. */
        //    H5D.close(datasetId);

        //    /* Terminate access to the data space. */
        //    H5S.close(spaceId);

        //    H5S.close(aId);
        //    H5T.close(aTypeId);
        //    H5A.close(attr);
        //}
        public static int WriteStringsToAscii(int groupId, string name, IEnumerable <string> strs)
        {
            var   maxLength = strs.Select(s => s.Length).Max();
            ulong strSz     = (ulong)strs.Count();
            var   buf       = strs.SelectMany(s => s.PadRight(maxLength).ToCharArray()).ToArray();

            ulong[] dimsa = new ulong[] { strSz, 1 };

            /* Create the data space for the dataset. */
            ulong[] dims    = new ulong[] { strSz, (ulong)maxLength };
            var     spaceId = H5S.create_simple(2, dims, null);

            /* Create the dataset. */
            name = ToHdf5Name(name);

            //var aId = H5S.create_simple(2, dimsa, null);

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

            H5T.set_size(aTypeId, new IntPtr(2));
            byte[] wdata = new byte[2 * buf.Length];
            for (int i = 0; i < buf.Length; ++i)
            {
                wdata[2 * i] = (byte)buf[i];
            }

            var      datasetId = H5D.create(groupId, name, H5T.FORTRAN_S1, spaceId);
            GCHandle hnd       = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            var      result    = H5D.write(datasetId, aTypeId, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                           hnd.AddrOfPinnedObject());

            hnd.Free();
            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(aTypeId);
            return(result);
        }
示例#21
0
        public static string ReadAsciiString(hid_t groupId, string name)
        {
            var datatype = H5T.FORTRAN_S1;

            //name = ToHdf5Name(name);

            var datasetId = H5D.open(groupId, name);
            var spaceId   = H5D.get_space(datasetId);
            int rank      = H5S.get_simple_extent_ndims(spaceId);

            ulong[] maxDims   = new ulong[rank];
            ulong[] dims      = new ulong[rank];
            ulong[] chunkDims = new ulong[rank];
            var     memId_n   = H5S.get_simple_extent_dims(spaceId, dims, null);

            // we write from C and must provide null-terminated strings

            byte[] wdata = new byte[dims[0] * 2];

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

            H5T.set_size(memId, new IntPtr(2));
            //H5T.set_strpad(memId, H5T.str_t.NULLTERM);
            GCHandle hnd      = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      resultId = H5D.read(datasetId, memId, H5S.ALL,
                                         H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            wdata = wdata.Where((b, i) => i % 2 == 0).
                    Select(b => (b == 0)?(byte)32:b).ToArray();
            string result = Encoding.ASCII.GetString(wdata);

            H5T.close(memId);
            H5D.close(datasetId);
            return(result);
        }
示例#22
0
        private static double[] ReadDataVector(hid_t fileLoc, string name)
        {
            hid_t dset   = H5D.open(fileLoc, name);
            hid_t fspace = H5D.get_space(dset);
            hid_t count  = H5S.get_simple_extent_ndims(fspace);

            hid_t type = H5D.get_type(dset);

            hsize_t[] dims    = new hsize_t[count];
            hsize_t[] maxdims = new hsize_t[count];

            H5S.get_simple_extent_dims(fspace, dims, maxdims);
            H5S.close(fspace);


            byte[] rdata = new byte[dims[0] * 8];

            hid_t mem_type = H5T.copy(H5T.NATIVE_DOUBLE);

            H5T.set_size(mem_type, new IntPtr(8));

            GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);

            H5D.read(dset, mem_type, H5S.ALL,
                     H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();
            H5T.close(mem_type);

            double[] val = new double[dims[0]];
            for (int i = 0; i < (int)dims[0]; i++)
            {
                val[i] = BitConverter.ToDouble(rdata, i * 8);
            }

            return(val);
        }
示例#23
0
        public static Array ReadPrimitiveAttributes <T>(hid_t groupId, string name) //where T : struct
        {
            Type type     = typeof(T);
            var  datatype = GetDatatype(type);

            var attributeId = H5A.open(groupId, name);
            var spaceId     = H5A.get_space(attributeId);
            int rank        = H5S.get_simple_extent_ndims(spaceId);

            ulong[] maxDims = new ulong[rank];
            ulong[] dims    = new ulong[rank];
            hid_t   memId   = H5S.get_simple_extent_dims(spaceId, dims, maxDims);

            long[] lengths    = dims.Select(d => Convert.ToInt64(d)).ToArray();
            Array  attributes = Array.CreateInstance(type, lengths);

            var typeId   = H5A.get_type(attributeId);
            var mem_type = H5T.copy(datatype);

            if (datatype == H5T.C_S1)
            {
                H5T.set_size(datatype, new IntPtr(2));
            }

            var propId = H5A.get_create_plist(attributeId);

            memId = H5S.create_simple(rank, dims, maxDims);
            GCHandle hnd = GCHandle.Alloc(attributes, GCHandleType.Pinned);

            H5A.read(attributeId, datatype, hnd.AddrOfPinnedObject());
            hnd.Free();
            H5A.close(typeId);
            H5A.close(attributeId);
            H5S.close(spaceId);
            return(attributes);
        }
示例#24
0
        public void Create(string key, object value)
        {
            Host.With((id) =>
            {
                long dataspace   = 0;
                long typeId      = 0;
                long attributeId = 0;
                try
                {
                    if (value is Array)
                    {
                        var array = (Array)value;
                        dataspace = H5S.create_simple(array.Rank, array.Shape(), null);
                        throw new NotImplementedException("Scalar attributes only");
                    }
                    else
                    {
                        dataspace = H5S.create(H5S.class_t.SCALAR);
                    }

                    IntPtr nativeMemory;

                    if (value is String)
                    {
                        var str = (string)value;
                        typeId  = H5T.copy(H5T.C_S1);
                        H5T.set_size(typeId, new IntPtr(str.Length));
                        nativeMemory = Marshal.StringToHGlobalAnsi(str);
                    }
                    else if (value is long)
                    {
                        var l        = new long[] { (long)value };
                        typeId       = H5T.copy(H5T.NATIVE_INT64);
                        nativeMemory = Marshal.AllocHGlobal(sizeof(long));
                        Marshal.Copy(l, 0, nativeMemory, 1);
                    }
                    else
                    {
                        throw new NotImplementedException("Unsupported attribute type: " + value.GetType().Name);
                    }

                    attributeId = H5A.create(id, key, typeId, dataspace);
                    H5A.write(attributeId, typeId, nativeMemory);
                    Marshal.FreeHGlobal(nativeMemory);
                }
                finally
                {
                    if (attributeId > 0)
                    {
                        H5A.close(attributeId);
                    }
                    if (typeId > 0)
                    {
                        H5T.close(typeId);
                    }
                    if (dataspace > 0)
                    {
                        H5S.close(dataspace);
                    }
                }
            });
        }
示例#25
0
        public static long GetHdfTypeIdFromType(long fileId, Type type)
        {
            Type elementType;

            elementType = type.IsArray ? type.GetElementType() : type;

            if (elementType == typeof(bool))
            {
                return(H5T.NATIVE_UINT8);
            }
            else if (elementType == typeof(Byte))
            {
                return(H5T.NATIVE_UINT8);
            }
            else if (elementType == typeof(SByte))
            {
                return(H5T.NATIVE_INT8);
            }
            else if (elementType == typeof(UInt16))
            {
                return(H5T.NATIVE_UINT16);
            }
            else if (elementType == typeof(Int16))
            {
                return(H5T.NATIVE_INT16);
            }
            else if (elementType == typeof(UInt32))
            {
                return(H5T.NATIVE_UINT32);
            }
            else if (elementType == typeof(Int32))
            {
                return(H5T.NATIVE_INT32);
            }
            else if (elementType == typeof(UInt64))
            {
                return(H5T.NATIVE_UINT64);
            }
            else if (elementType == typeof(Int64))
            {
                return(H5T.NATIVE_INT64);
            }
            else if (elementType == typeof(Single))
            {
                return(H5T.NATIVE_FLOAT);
            }
            else if (elementType == typeof(Double))
            {
                return(H5T.NATIVE_DOUBLE);
            }
            else if (elementType == typeof(string) || elementType == typeof(IntPtr))
            {
                long typeId = 0;

                if (H5I.is_valid(fileId) > 0 && H5L.exists(fileId, "string_t") > 0)
                {
                    typeId = H5T.open(fileId, "string_t");
                }
                else
                {
                    typeId = H5T.copy(H5T.C_S1);

                    H5T.set_size(typeId, H5T.VARIABLE);
                    H5T.set_cset(typeId, H5T.cset_t.UTF8);

                    if (fileId > -1 && H5T.commit(fileId, "string_t", typeId) < 0)
                    {
                        throw new Exception(ErrorMessage.TypeConversionHelper_CouldNotCommitDataType);
                    }
                }

                return(typeId);
            }
            else if (elementType.IsValueType && !elementType.IsPrimitive && !elementType.IsEnum)
            {
                long typeId = 0;

                if (H5I.is_valid(fileId) > 0 && H5L.exists(fileId, elementType.Name) > 0)
                {
                    typeId = H5T.open(fileId, elementType.Name);
                }
                else
                {
                    typeId = H5T.create(H5T.class_t.COMPOUND, new IntPtr(Marshal.SizeOf(elementType)));

                    foreach (FieldInfo fieldInfo in elementType.GetFields())
                    {
                        long fieldType = -1;

                        fieldType = TypeConversionHelper.GetHdfTypeIdFromType(fileId, fieldInfo.FieldType);

                        H5T.insert(typeId, fieldInfo.Name, Marshal.OffsetOf(elementType, fieldInfo.Name), fieldType);

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

                    if (fileId > -1 && H5T.commit(fileId, elementType.Name, typeId) < 0)
                    {
                        throw new Exception(ErrorMessage.TypeConversionHelper_CouldNotCommitDataType);
                    }
                }

                return(typeId);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
示例#26
0
        /// <summary>
        /// Currently does not support arrays
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_objectId"></param>
        /// <param name="_title"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static Hdf5Attribute CreateAttribute <T>(Hdf5Identifier _objectId, string _title, T _value)
        {
            ulong[] sizes = new ulong[1] {
                1
            };
            Hdf5Identifier dataspaceId;
            Hdf5Identifier attributeId;
            Hdf5Identifier typeId;
            Hdf5Attribute  attribute = null;

            Type type     = _value.GetType();
            var  datatype = TypeHelper.GetDataTypesEnum(type);

            if (datatype != Hdf5DataTypes.String)
            {
                var tempType = TypeHelper.GetNativeType(datatype);

                typeId = H5T.copy(tempType.Value).ToId();
                var dataTypeObject = TypeHelper.GetDataTypeByType(typeId);

                var status = H5T.set_order(typeId.Value, H5T.order_t.LE);

                dataspaceId = H5S.create_simple(1, sizes, null).ToId();

                attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId();

                if (attributeId.Value > 0)
                {
                    WriteValue(dataTypeObject, attributeId, _value);
                }
            }
            else
            {
                string tempValue = Convert.ToString(_value);

                dataspaceId = H5S.create(H5S.class_t.SCALAR).ToId();
                typeId      = H5T.copy(H5T.C_S1).ToId();
                int length = tempValue.Length + 1;
                var result = H5T.set_size(typeId.Value, new IntPtr(length));

                attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId();

                IntPtr valueArray = Marshal.StringToHGlobalAnsi(tempValue);

                result = H5A.write(attributeId.Value, typeId.Value, valueArray);

                Marshal.FreeHGlobal(valueArray);
            }

            H5S.close(dataspaceId.Value);
            H5T.close(typeId.Value);
            H5A.close(attributeId.Value);

            if (attributeId.Value > 0)
            {
                attribute = new Hdf5Attribute
                {
                    Value = _value,
                    Name  = _title,
                    Id    = attributeId
                };
            }

            return(attribute);
        }
        public static bool WriteVariableStringAttribute(hid_t hid, string key, IEnumerable <string> values, bool utf8)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var exists = H5A.exists(hid, key);

            if (exists < 0)
            {
                throw new Exception("H5A.exists failed");
            }

            if (exists == 0) // Attribute doesn't exist
            {
#if true
                var type = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }
#else
                var type = H5T.copy(H5T.C_S1);
                if (type < 0)
                {
                    throw new Exception("Failed to create string type");
                }

                if (H5T.set_size(type, H5T.VARIABLE) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set type size");
                }
#endif

                if (utf8)
                {
                    if (H5T.set_cset(type, H5T.cset_t.UTF8) < 0)
                    {
                        H5T.close(type);
                        throw new Exception("Failed to set cset");
                    }
                }

                if (H5T.set_strpad(type, H5T.str_t.NULLTERM) < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to set strpad");
                }

                var space = values.Count() == 1 ? H5S.create(H5S.class_t.SCALAR)
                    : H5S.create_simple(1, new ulong[1] {
                    (ulong)values.Count()
                }, null);
                if (space < 0)
                {
                    H5T.close(type);
                    throw new Exception("Failed to create data space");
                }

                var attribute = H5A.create(hid, key, type, space);
                H5S.close(space);
                if (attribute < 0)
                {
                    H5T.close(type);
                    throw new Exception(string.Format("Failed to create attribute \"{0}\"", key));
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }
            else
            {
                // Attribute exists.
                var attribute = H5A.open(hid, key);
                if (attribute < 0)
                {
                    throw new Exception(string.Format("Failed to open attribute \"{0}\"", key));
                }

                var type = H5A.get_type(attribute);
                if (type < 0)
                {
                    H5A.close(attribute);
                    throw new Exception("Failed to get data type");
                }

                var pinnedObjects = new PinnedObject[values.Count()];
                var data          = new IntPtr[values.Count()];
                int count         = 0;
                foreach (string str in values)
                {
                    var bytes = str.ToBytes(utf8);
                    pinnedObjects[count] = new PinnedObject(bytes);
                    data[count]          = pinnedObjects[count];
                    count += 1;
                }

                H5A.write(attribute, type, new PinnedObject(data));

                H5T.close(type);
                H5A.close(attribute);
            }

            return(true);
        }
示例#28
0
        public static unsafe void AddString(long fileId, ContainerType container)
        {
            long res;

            var dims = new ulong[] { 2, 2, 3 }; /* "extendible contiguous non-external dataset not allowed" */

            // fixed length string attribute + null terminate (ASCII)
            var typeIdFixed_nullterm = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdFixed_nullterm, new IntPtr(4));
            res = H5T.set_cset(typeIdFixed_nullterm, H5T.cset_t.ASCII);
            res = H5T.set_strpad(typeIdFixed_nullterm, H5T.str_t.NULLTERM);

            var dataFixed_nullterm     = new string[] { "00\00", "11\0 ", "22\0 ", "3\0  ", "44 \0", "555\0", "66 \0", "77\0 ", "  \0 ", "AA \0", "ZZ \0", "!!\0 " };
            var dataFixedChar_nullterm = dataFixed_nullterm
                                         .SelectMany(value => Encoding.ASCII.GetBytes(value))
                                         .ToArray();

            TestUtils.Add(container, fileId, "string", "fixed+nullterm", typeIdFixed_nullterm, dataFixedChar_nullterm.AsSpan(), dims);

            res = H5T.close(typeIdFixed_nullterm);

            // fixed length string attribute + null padding (ASCII)
            var typeIdFixed_nullpad = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdFixed_nullpad, new IntPtr(4));
            res = H5T.set_cset(typeIdFixed_nullpad, H5T.cset_t.ASCII);
            res = H5T.set_strpad(typeIdFixed_nullpad, H5T.str_t.NULLPAD);

            var dataFixed_nullpad     = new string[] { "0\00\0", "11\0\0", "22\0\0", "3 \0\0", " 4\0\0", "55 5", "66\0\0", "77\0\0", "  \0\0", "AA\0\0", "ZZ\0\0", "!!\0\0" };
            var dataFixedChar_nullpad = dataFixed_nullpad
                                        .SelectMany(value => Encoding.ASCII.GetBytes(value))
                                        .ToArray();

            TestUtils.Add(container, fileId, "string", "fixed+nullpad", typeIdFixed_nullpad, dataFixedChar_nullpad.AsSpan(), dims);

            res = H5T.close(typeIdFixed_nullpad);

            // fixed length string attribute + space padding (ASCII)
            var typeIdFixed_spacepad = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdFixed_spacepad, new IntPtr(4));
            res = H5T.set_cset(typeIdFixed_spacepad, H5T.cset_t.ASCII);
            res = H5T.set_strpad(typeIdFixed_spacepad, H5T.str_t.SPACEPAD);

            var dataFixed_spacepad     = new string[] { "00  ", "11  ", "22  ", "3   ", " 4  ", "55 5", "66  ", "77  ", "    ", "AA  ", "ZZ  ", "!!  " };
            var dataFixedChar_spacepad = dataFixed_spacepad
                                         .SelectMany(value => Encoding.ASCII.GetBytes(value))
                                         .ToArray();

            TestUtils.Add(container, fileId, "string", "fixed+spacepad", typeIdFixed_spacepad, dataFixedChar_spacepad.AsSpan(), dims);

            res = H5T.close(typeIdFixed_spacepad);

            // variable length string attribute (ASCII)
            var typeIdVar = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdVar, H5T.VARIABLE);
            res = H5T.set_cset(typeIdVar, H5T.cset_t.ASCII);
            res = H5T.set_strpad(typeIdVar, H5T.str_t.NULLPAD);

            var dataVar     = new string[] { "001", "11", "22", "33", "44", "55", "66", "77", "  ", "AA", "ZZ", "!!" };
            var dataVarChar = dataVar
                              .SelectMany(value => Encoding.ASCII.GetBytes(value + '\0'))
                              .ToArray();

            fixed(byte *dataVarPtr = dataVarChar)
            {
                var basePtr = new IntPtr(dataVarPtr);

                var addresses = new IntPtr[]
                {
                    IntPtr.Add(basePtr, 0), IntPtr.Add(basePtr, 4), IntPtr.Add(basePtr, 7), IntPtr.Add(basePtr, 10),
                    IntPtr.Add(basePtr, 13), IntPtr.Add(basePtr, 16), IntPtr.Add(basePtr, 19), IntPtr.Add(basePtr, 22),
                    IntPtr.Add(basePtr, 25), IntPtr.Add(basePtr, 28), IntPtr.Add(basePtr, 31), IntPtr.Add(basePtr, 34)
                };

                fixed(void *dataVarAddressesPtr = addresses)
                {
                    TestUtils.Add(container, fileId, "string", "variable", typeIdVar, dataVarAddressesPtr, dims);
                }
            }

            res = H5T.close(typeIdVar);

            // variable length string attribute + space padding (ASCII)
            var typeIdVar_spacepad = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdVar_spacepad, H5T.VARIABLE);
            res = H5T.set_cset(typeIdVar_spacepad, H5T.cset_t.ASCII);
            res = H5T.set_strpad(typeIdVar_spacepad, H5T.str_t.SPACEPAD);

            var dataVar_spacepad     = new string[] { "001  ", "1 1 ", "22  ", "33", "44", "55", "66", "77", "  ", "AA", "ZZ", "!!" };
            var dataVarChar_spacepad = dataVar_spacepad
                                       .SelectMany(value => Encoding.ASCII.GetBytes(value + '\0'))
                                       .ToArray();

            fixed(byte *dataVarPtr_spacepad = dataVarChar_spacepad)
            {
                var basePtr = new IntPtr(dataVarPtr_spacepad);

                var addresses = new IntPtr[]
                {
                    IntPtr.Add(basePtr, 0), IntPtr.Add(basePtr, 6), IntPtr.Add(basePtr, 11), IntPtr.Add(basePtr, 16),
                    IntPtr.Add(basePtr, 19), IntPtr.Add(basePtr, 22), IntPtr.Add(basePtr, 25), IntPtr.Add(basePtr, 28),
                    IntPtr.Add(basePtr, 31), IntPtr.Add(basePtr, 34), IntPtr.Add(basePtr, 37), IntPtr.Add(basePtr, 40)
                };

                fixed(void *addressesPtr = addresses)
                {
                    TestUtils.Add(container, fileId, "string", "variable+spacepad", typeIdVar_spacepad, addressesPtr, dims);
                }
            }

            res = H5T.close(typeIdVar);

            // variable length string attribute (UTF8)
            var typeIdVarUTF8 = H5T.copy(H5T.C_S1);

            res = H5T.set_size(typeIdVarUTF8, H5T.VARIABLE);
            res = H5T.set_cset(typeIdVarUTF8, H5T.cset_t.UTF8);
            res = H5T.set_strpad(typeIdVarUTF8, H5T.str_t.NULLPAD);

            var dataVarUTF8     = new string[] { "00", "111", "22", "33", "44", "55", "66", "77", "  ", "ÄÄ", "的的", "!!" };
            var dataVarCharUTF8 = dataVarUTF8
                                  .SelectMany(value => Encoding.UTF8.GetBytes(value + '\0'))
                                  .ToArray();

            fixed(byte *dataVarPtrUTF8 = dataVarCharUTF8)
            {
                var basePtr = new IntPtr(dataVarPtrUTF8);

                var addresses = new IntPtr[]
                {
                    IntPtr.Add(basePtr, 0), IntPtr.Add(basePtr, 3), IntPtr.Add(basePtr, 7), IntPtr.Add(basePtr, 10),
                    IntPtr.Add(basePtr, 13), IntPtr.Add(basePtr, 16), IntPtr.Add(basePtr, 19), IntPtr.Add(basePtr, 22),
                    IntPtr.Add(basePtr, 25), IntPtr.Add(basePtr, 28), IntPtr.Add(basePtr, 33), IntPtr.Add(basePtr, 40)
                };

                fixed(void *addressesPtr = addresses)
                {
                    TestUtils.Add(container, fileId, "string", "variableUTF8", typeIdVarUTF8, addressesPtr, dims);
                }
            }
        }
示例#29
0
        private static long GetHdfTypeIdFromType(Type type)
        {
            var elementType = type.IsArray ? type.GetElementType() : type;

            if (elementType == typeof(bool))
            {
                return(H5T.NATIVE_UINT8);
            }

            else if (elementType == typeof(byte))
            {
                return(H5T.NATIVE_UINT8);
            }

            else if (elementType == typeof(sbyte))
            {
                return(H5T.NATIVE_INT8);
            }

            else if (elementType == typeof(ushort))
            {
                return(H5T.NATIVE_UINT16);
            }

            else if (elementType == typeof(short))
            {
                return(H5T.NATIVE_INT16);
            }

            else if (elementType == typeof(uint))
            {
                return(H5T.NATIVE_UINT32);
            }

            else if (elementType == typeof(int))
            {
                return(H5T.NATIVE_INT32);
            }

            else if (elementType == typeof(ulong))
            {
                return(H5T.NATIVE_UINT64);
            }

            else if (elementType == typeof(long))
            {
                return(H5T.NATIVE_INT64);
            }

            else if (elementType == typeof(float))
            {
                return(H5T.NATIVE_FLOAT);
            }

            else if (elementType == typeof(double))
            {
                return(H5T.NATIVE_DOUBLE);
            }

            // issues: https://en.wikipedia.org/wiki/Long_double
            //else if (elementType == typeof(decimal))
            //    return H5T.NATIVE_LDOUBLE;

            else if (elementType.IsEnum)
            {
                var baseTypeId = TestUtils.GetHdfTypeIdFromType(Enum.GetUnderlyingType(elementType));
                var typeId     = H5T.enum_create(baseTypeId);

                foreach (var value in Enum.GetValues(type))
                {
                    var value_converted = Convert.ToInt64(value);
                    var name            = Enum.GetName(type, value_converted);

                    var handle = GCHandle.Alloc(value_converted, GCHandleType.Pinned);
                    H5T.enum_insert(typeId, name, handle.AddrOfPinnedObject());
                }

                return(typeId);
            }

            else if (elementType == typeof(string) || elementType == typeof(IntPtr))
            {
                var typeId = H5T.copy(H5T.C_S1);

                H5T.set_size(typeId, H5T.VARIABLE);
                H5T.set_cset(typeId, H5T.cset_t.UTF8);

                return(typeId);
            }
            else if (elementType.IsValueType && !elementType.IsPrimitive)
            {
                var typeId = H5T.create(H5T.class_t.COMPOUND, new IntPtr(Marshal.SizeOf(elementType)));

                foreach (var fieldInfo in elementType.GetFields())
                {
                    var fieldType    = TestUtils.GetHdfTypeIdFromType(fieldInfo.FieldType);
                    var attribute    = fieldInfo.GetCustomAttribute <H5NameAttribute>(true);
                    var hdfFieldName = attribute is not null ? attribute.Name : fieldInfo.Name;

                    H5T.insert(typeId, hdfFieldName, Marshal.OffsetOf(elementType, fieldInfo.Name), fieldType);

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

                return(typeId);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
示例#30
0
        public static void ClassInit(TestContext testContext)
        {
            // create test files which persists across file tests
            m_v0_class_file = Utilities.H5TempFile(ref m_v0_class_file_name,
                                                   H5F.libver_t.EARLIEST);
            Assert.IsTrue(m_v0_class_file >= 0);
            m_v2_class_file = Utilities.H5TempFile(ref m_v2_class_file_name);
            Assert.IsTrue(m_v2_class_file >= 0);

            m_space_null = H5S.create(H5S.class_t.NULL);
            Assert.IsTrue(m_space_null >= 0);
            m_space_scalar = H5S.create(H5S.class_t.SCALAR);
            Assert.IsTrue(m_space_scalar >= 0);

            // create two datasets of the extended ASCII character set
            // store as H5T.FORTRAN_S1 -> space padding

            hsize_t[] dims = { 256 };

            hid_t space = H5S.create_simple(1, dims, null);

            m_v0_ascii_dset = H5D.create(m_v0_class_file, "ASCII",
                                         H5T.FORTRAN_S1, space);
            m_v2_ascii_dset = H5D.create(m_v2_class_file, "ASCII",
                                         H5T.FORTRAN_S1, space);
            Assert.IsTrue(H5S.close(space) >= 0);

            // we write from C and must provide null-terminated strings

            byte[] wdata = new byte[512];
            for (int i = 0; i < 256; ++i)
            {
                wdata[2 * i] = (byte)i;
            }

            hid_t mem_type = H5T.copy(H5T.C_S1);

            Assert.IsTrue(H5T.set_size(mem_type, new IntPtr(2)) >= 0);

            GCHandle hnd = GCHandle.Alloc(wdata, GCHandleType.Pinned);

            Assert.IsTrue(H5D.write(m_v0_ascii_dset, mem_type, H5S.ALL,
                                    H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            Assert.IsTrue(H5D.write(m_v2_ascii_dset, mem_type, H5S.ALL,
                                    H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            hnd.Free();

            Assert.IsTrue(H5T.close(mem_type) >= 0);

            // create UTF-8 encoded test datasets

            hid_t dtype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            Assert.IsTrue(H5T.set_cset(dtype, H5T.cset_t.UTF8) >= 0);
            Assert.IsTrue(H5T.set_strpad(dtype, H5T.str_t.SPACEPAD) >= 0);

            hid_t dspace = H5S.create_simple(1,
                                             new hsize_t[] { (hsize_t)m_utf8strings.Count }, null);

            m_v0_utf8_dset = H5D.create(m_v0_class_file, "UTF-8", dtype, dspace);
            Assert.IsTrue(m_v0_utf8_dset >= 0);
            m_v2_utf8_dset = H5D.create(m_v2_class_file, "UTF-8", dtype, dspace);
            Assert.IsTrue(m_v2_utf8_dset >= 0);

            GCHandle[] hnds   = new GCHandle[m_utf8strings.Count];
            IntPtr[]   wdata1 = new IntPtr[m_utf8strings.Count];

            for (int i = 0; i < m_utf8strings.Count; ++i)
            {
                hnds[i] = GCHandle.Alloc(
                    Encoding.UTF8.GetBytes((string)m_utf8strings[i]),
                    GCHandleType.Pinned);
                wdata1[i] = hnds[i].AddrOfPinnedObject();
            }

            hnd = GCHandle.Alloc(wdata1, GCHandleType.Pinned);
            Assert.IsTrue(H5D.write(m_v0_utf8_dset, dtype, H5S.ALL, H5S.ALL,
                                    H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            Assert.IsTrue(H5D.write(m_v2_utf8_dset, dtype, H5S.ALL, H5S.ALL,
                                    H5P.DEFAULT, hnd.AddrOfPinnedObject()) >= 0);
            hnd.Free();

            for (int i = 0; i < m_utf8strings.Count; ++i)
            {
                hnds[i].Free();
            }

            Assert.IsTrue(H5S.close(dspace) >= 0);
            Assert.IsTrue(H5T.close(dtype) >= 0);
        }