Ejemplo n.º 1
0
        public void FirstDataset(Array dataset)
        {
            if (FalseGroupId)
            {
                throw new Exception("cannot call FirstDataset because group or file couldn't be created");
            }
            if (DatasetExists)
            {
                throw new Exception("cannot call FirstDataset because dataset already exists");
            }

            Rank        = dataset.Rank;
            currentDims = GetDims(dataset);

            /* Create the data space with unlimited dimensions. */
            spaceId = H5S.create_simple(Rank, currentDims, maxDims);

            /* Modify dataset creation properties, i.e. enable chunking  */
            propId = H5P.create(H5P.DATASET_CREATE);
            status = H5P.set_chunk(propId, Rank, chunkDims);

            /* Create a new dataset within the file using chunk creation properties.  */
            datasetId = H5D.create(GroupId, Hdf5Utils.NormalizedName(Datasetname), datatype, spaceId, H5P.DEFAULT, propId);

            /* Write data to dataset */
            GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned);

            status = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                               hnd.AddrOfPinnedObject());
            hnd.Free();
            H5S.close(spaceId);
        }
Ejemplo n.º 2
0
        public static T ReadObject <T>(long groupId, T readValue, string groupName)
        {
            if (readValue == null)
            {
                throw new ArgumentNullException(nameof(readValue));
            }

            Type tyObject = readValue.GetType();
            //foreach (Attribute attr in Attribute.GetCustomAttributes(tyObject))
            //{
            //    if (attr is Hdf5GroupName)
            //        groupName = (attr as Hdf5GroupName).Name;
            //    if (attr is Hdf5SaveAttribute)
            //    {
            //        Hdf5SaveAttribute atLeg = attr as Hdf5SaveAttribute;
            //        if (atLeg.SaveKind == Hdf5Save.DoNotSave)
            //            return readValue;
            //    }
            //}
            bool isGroupName = !string.IsNullOrWhiteSpace(groupName);

            if (isGroupName)
            {
                groupId = H5G.open(groupId, Hdf5Utils.NormalizedName(groupName));
            }

            ReadFields(tyObject, readValue, groupId);
            ReadProperties(tyObject, readValue, groupId);

            if (isGroupName)
            {
                CloseGroup(groupId);
            }
            return(readValue);
        }
Ejemplo n.º 3
0
        public void AppendOrCreateDataset(Array dataset)
        {
            if (chunkDims == null)
            {
                chunkDims = new[]
                { Convert.ToUInt64(dataset.GetLongLength(0)), Convert.ToUInt64(dataset.GetLongLength(1)) };

                Rank        = dataset.Rank;
                currentDims = GetDims(dataset);

                /* Create the data space with unlimited dimensions. */
                spaceId = H5S.create_simple(Rank, currentDims, maxDims);

                /* Modify dataset creation properties, i.e. enable chunking  */
                propId = H5P.create(H5P.DATASET_CREATE);
                status = H5P.set_chunk(propId, Rank, chunkDims);

                /* Create a new dataset within the file using chunk creation properties.  */
                datasetId = H5D.create(GroupId, Hdf5Utils.NormalizedName(Datasetname), datatype, spaceId, H5P.DEFAULT, propId);

                /* Write data to dataset */
                GCHandle hnd = GCHandle.Alloc(dataset, GCHandleType.Pinned);
                status = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
                                   hnd.AddrOfPinnedObject());
                hnd.Free();
                H5S.close(spaceId);
            }
            else
            {
                AppendDataset(dataset);
            }
        }
Ejemplo n.º 4
0
        public static (bool success, IEnumerable <string> result) ReadStrings(long groupId, string name, string alternativeName)
        {
            long datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            H5T.set_cset(datatype, H5T.cset_t.UTF8);
            H5T.set_strpad(datatype, H5T.str_t.NULLTERM);

            //name = ToHdf5Name(name);

            var datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(name));

            if (datasetId < 0) //does not exist?
            {
                datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(alternativeName));
            }
            if (datasetId <= 0)
            {
                Hdf5Utils.LogError?.Invoke($"Error reading {groupId}. Name:{name}. AlternativeName:{alternativeName}");
                return(false, Array.Empty <string>());
            }
            long spaceId = H5D.get_space(datasetId);

            long count = H5S.get_simple_extent_npoints(spaceId);

            H5S.close(spaceId);

            var strs = new List <string>();

            if (count >= 0)
            {
                IntPtr[] rdata = new IntPtr[count];
                GCHandle hnd   = GCHandle.Alloc(rdata, GCHandleType.Pinned);
                H5D.read(datasetId, datatype, H5S.ALL, H5S.ALL,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());

                for (int i = 0; i < rdata.Length; ++i)
                {
                    int len = 0;
                    while (Marshal.ReadByte(rdata[i], len) != 0)
                    {
                        ++len;
                    }
                    byte[] buffer = new byte[len];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);
                    string s = Encoding.UTF8.GetString(buffer);

                    strs.Add(s);

                    // H5.free_memory(rdata[i]);
                }
                hnd.Free();
            }
            H5T.close(datatype);
            H5D.close(datasetId);
            return(true, strs);
        }
Ejemplo n.º 5
0
        // private readonly ReaderWriterLockSlim lock_ = new ReaderWriterLockSlim();

        public Hdf5AcquisitionFileWriter(string filename, string groupName = "ROOT")
        {
            H5E.set_auto(H5E.DEFAULT, null, IntPtr.Zero);
            //lock_.EnterWriteLock();
            _filename  = filename;
            fileId     = Hdf5.CreateFile(filename);
            _groupName = groupName;
            _groupId   = Hdf5.CreateGroup(fileId, Hdf5Utils.NormalizedName(_groupName));

            Header       = new Hdf5AcquisitionFile();
            _nrOfRecords = 0;
            _sampleCount = 0;
            //lock_.ExitWriteLock();
        }
Ejemplo n.º 6
0
        public static bool GroupExists(long groupId, string groupName)
        {
            bool exists = false;

            try
            {
                H5G.info_t info = new H5G.info_t();
                var        gid  = H5G.get_info_by_name(groupId, Hdf5Utils.NormalizedName(groupName), ref info);
                exists = gid == 0;
            }
            catch (Exception)
            {
            }
            return(exists);
        }
Ejemplo n.º 7
0
        public static (int success, long CreatedgroupId) WriteStrings(long groupId, string name, IEnumerable <string> strs, string datasetName = null)
        {
            // create UTF-8 encoded test datasets

            long datatype = H5T.create(H5T.class_t.STRING, H5T.VARIABLE);

            H5T.set_cset(datatype, H5T.cset_t.UTF8);
            H5T.set_strpad(datatype, H5T.str_t.SPACEPAD);

            int  strSz   = strs.Count();
            long spaceId = H5S.create_simple(1, new[] { (ulong)strSz }, null);

            var datasetId = H5D.create(groupId, Hdf5Utils.NormalizedName(name), datatype, spaceId);

            GCHandle[] hnds  = new GCHandle[strSz];
            IntPtr[]   wdata = new IntPtr[strSz];

            int cntr = 0;

            foreach (string str in strs)
            {
                hnds[cntr] = GCHandle.Alloc(
                    Encoding.UTF8.GetBytes(str),
                    GCHandleType.Pinned);
                wdata[cntr] = hnds[cntr].AddrOfPinnedObject();
                cntr++;
            }

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

            var result = H5D.write(datasetId, datatype, H5S.ALL, H5S.ALL,
                                   H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

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

            H5D.close(datasetId);
            H5S.close(spaceId);
            H5T.close(datatype);
            return(result, datasetId);
        }
Ejemplo n.º 8
0
        public static int WriteAsciiString(long 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, Hdf5Utils.NormalizedName(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);
        }
Ejemplo n.º 9
0
        public static string ReadAsciiString(long groupId, string name)
        {
            var datatype = H5T.FORTRAN_S1;

            //name = ToHdf5Name(name);

            var datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(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);
        }
Ejemplo n.º 10
0
        public static int WriteUnicodeString(long groupId, string name, string str, H5T.str_t strPad = H5T.str_t.SPACEPAD)
        {
            byte[] wdata = Encoding.UTF8.GetBytes(str);

            long spaceId = H5S.create(H5S.class_t.SCALAR);

            long dtype = H5T.create(H5T.class_t.STRING, new IntPtr(wdata.Length));

            H5T.set_cset(dtype, H5T.cset_t.UTF8);
            H5T.set_strpad(dtype, strPad);

            long datasetId = H5D.create(groupId, Hdf5Utils.NormalizedName(name), dtype, spaceId);

            GCHandle hnd    = GCHandle.Alloc(wdata, GCHandleType.Pinned);
            int      result = H5D.write(datasetId, dtype, H5S.ALL,
                                        H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());

            hnd.Free();

            H5T.close(dtype);
            H5D.close(datasetId);
            H5S.close(spaceId);
            return(result);
        }
Ejemplo n.º 11
0
 public static long CreateGroup(long groupId, string groupName)
 {
     return((GroupExists(groupId, groupName))
         ? H5G.open(groupId, Hdf5Utils.NormalizedName(groupName))
         : H5G.create(groupId, Hdf5Utils.NormalizedName(groupName)));
 }
Ejemplo n.º 12
0
        public static string ReadUnicodeString(long groupId, string name)
        {
            var datasetId = H5D.open(groupId, Hdf5Utils.NormalizedName(name));
            var typeId    = H5D.get_type(datasetId);

            if (H5T.is_variable_str(typeId) > 0)
            {
                var  spaceId = H5D.get_space(datasetId);
                long count   = H5S.get_simple_extent_npoints(spaceId);

                IntPtr[] rdata = new IntPtr[count];

                GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);
                H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                         H5P.DEFAULT, hnd.AddrOfPinnedObject());

                var attrStrings = new List <string>();
                for (int i = 0; i < rdata.Length; ++i)
                {
                    int attrLength = 0;
                    while (Marshal.ReadByte(rdata[i], attrLength) != 0)
                    {
                        ++attrLength;
                    }

                    byte[] buffer = new byte[attrLength];
                    Marshal.Copy(rdata[i], buffer, 0, buffer.Length);

                    string stringPart = Encoding.UTF8.GetString(buffer);

                    attrStrings.Add(stringPart);

                    H5.free_memory(rdata[i]);
                }

                hnd.Free();
                H5S.close(spaceId);
                H5D.close(datasetId);

                return(attrStrings[0]);
            }

            // Must be a non-variable length string.
            int    size = H5T.get_size(typeId).ToInt32();
            IntPtr iPtr = Marshal.AllocHGlobal(size);

            int result = H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL,
                                  H5P.DEFAULT, iPtr);

            if (result < 0)
            {
                throw new IOException("Failed to read dataset");
            }

            var strDest = new byte[size];

            Marshal.Copy(iPtr, strDest, 0, size);
            Marshal.FreeHGlobal(iPtr);

            H5D.close(datasetId);

            return(Encoding.UTF8.GetString(strDest).TrimEnd((char)0));
        }