Exemple #1
0
        public static double ReadAttribute(string file, string dataSetOrGroup, string attribute)
        {
            double attr = Double.NaN;

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

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

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

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

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

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

            return(attr);
        }
Exemple #2
0
        private void Write(H5GroupId parent, string name, IEnumerable <IMeasurement> measurements)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] {
                (long)measurements.Count()
            });


            // Set compression options for dataset
            H5PropertyListId dataSetPropertyList = H5P.create(H5P.PropertyListClass.DATASET_CREATE);

            H5P.setDeflate(dataSetPropertyList, NumericDataCompression);
            H5P.setChunk(dataSetPropertyList, new long[] { (long)measurements.Count() });

            H5DataSetId dataSetId = H5D.create(parent,
                                               name,
                                               measurement_t,
                                               spaceId,
                                               new H5PropertyListId(H5P.Template.DEFAULT),
                                               dataSetPropertyList,
                                               new H5PropertyListId(H5P.Template.DEFAULT));

            MeasurementT[] ms       = new MeasurementT[measurements.Count()];
            int            ilmCount = 0;

            foreach (IMeasurement m in measurements)
            {
                MeasurementT mt = Convert(m);
                ms[ilmCount++] = mt;
            }

            H5D.write <MeasurementT>(dataSetId, measurement_t, new H5Array <MeasurementT>(ms));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
Exemple #3
0
        // Function used with H5L.iterate
        protected H5IterationResult Op_Func(H5GroupId id,
                                            string objectName,
                                            H5LinkInfo info, Object param)
        {
            H5ObjectInfo objInfo = H5O.getInfoByName(id, objectName);

            string groupName = (string)param;

            switch (objInfo.objectType)
            {
            case H5ObjectType.DATASET:
                OnHdfObjInfoFound(objectName, HdfObjectType.Dataset, groupName);
                break;

            case H5ObjectType.GROUP:

                GetHdfObjInfo(objectName);

                break;

            case H5ObjectType.NAMED_DATATYPE:
                break;

            case H5ObjectType.UNKNOWN:
                break;
            }

            return(H5IterationResult.SUCCESS);
        }
Exemple #4
0
        } // test_group_basics

        /*
         * Operator function.
         */
        static int file_info(H5GroupId id, string objectName, Object param)
        {
            string str_param = (string)param;

            Console.WriteLine("\t   {0}: {1}", str_param, objectName);
            return(0);
        }
Exemple #5
0
 public EpochGroupIDs(H5GroupId group,
                      H5GroupId subGroups,
                      H5GroupId epochs)
 {
     GroupId     = group;
     SubGroupsId = subGroups;
     EpochsId    = epochs;
 }
Exemple #6
0
        static void test_file_open()
        {
            try
            {
                // Output message about test being performed.
                Console.Write("Testing file opening I/O");

                // First ensure the file does not exist
                File.Delete(FILE2);

                // Try opening a non-existent file.  This should fail.
                try
                {
                    H5FileId non_exist_file = H5F.open(FILE2, H5F.OpenMode.ACC_RDWR);

                    // should fail, but didn't, print out the error message.
                    Console.WriteLine("\ntest_file_open: Attempting to open a non-existent file.");
                    nerrors++;
                }
                catch (H5FopenException) { } // does nothing, it should fail

                // Open the file.
                H5FileId fileId = H5F.open(FILE1, H5F.OpenMode.ACC_RDWR);

                // Create dataspace for the dataset in the file.
                hssize_t[]    dims   = { 20 };
                H5DataSpaceId dspace = H5S.create_simple(RANK, dims);

                // Create a group.
                H5GroupId groupId = H5G.create(fileId, GROUP_NAME);

                // Create a dataset using file as location.
                H5DataSetId dset1Id = H5D.create(fileId, DSET1_NAME, H5T.H5Type.NATIVE_INT, dspace);

                // Create a dataset using group as location.
                H5DataSetId dset2Id = H5D.create(groupId, DSET2_NAME, H5T.H5Type.NATIVE_SHORT, dspace);

                // Close objects and files.
                H5D.close(dset1Id);
                H5D.close(dset2Id);
                H5S.close(dspace);
                H5G.close(groupId);
                H5F.close(fileId);

                Console.WriteLine("\t\t\t\tPASSED");
            }
            catch (HDFException anyHDF5E)
            {
                Console.WriteLine(anyHDF5E.Message);
                nerrors++;
            }
            catch (System.Exception sysE)
            {
                Console.WriteLine(sysE.TargetSite);
                Console.WriteLine(sysE.Message);
                nerrors++;
            }
        } // test_file_open
Exemple #7
0
        private void GetGroupDatasetNames(string groupName)
        {
            H5GroupId h5GroupId = H5G.open(_h5FileId, groupName);

            try
            {
                long dscount = H5G.getNumObjects(h5GroupId);
                for (int i = 0; i < dscount; i++)
                {
                    string     objname = H5G.getObjectNameByIndex(h5GroupId, (ulong)i);
                    ObjectInfo objInfo = H5G.getObjectInfo(h5GroupId, objname, false);
                    switch (objInfo.objectType)
                    {
                    case H5GType.DATASET:
                        if (objInfo.objectType == H5GType.DATASET)
                        {
                            if (groupName == "/")
                            {
                                _datasetNames.Add(objname);
                            }
                            else
                            {
                                _datasetNames.Add(groupName + objname);
                            }
                        }
                        break;

                    case H5GType.GROUP:
                        if (groupName == "/")
                        {
                            GetGroupDatasetNames(objname + "/");
                        }
                        else
                        {
                            GetGroupDatasetNames(groupName + objname + "/");
                        }
                        break;

                    case H5GType.LINK:
                        break;

                    case H5GType.TYPE:
                        break;

                    default:
                        break;
                    }
                }
            }
            finally
            {
                if (h5GroupId != null)
                {
                    H5G.close(h5GroupId);
                }
            }
        }
Exemple #8
0
 // Function used with H5L.iterate
 static H5IterationResult MyH5LFunction(H5GroupId id,
                                        string objectName,
                                        H5LinkInfo info, Object param)
 {
     Console.WriteLine("The object name is {0}", objectName);
     Console.WriteLine("The linkType is {0}", info.linkType);
     Console.WriteLine("The object parameter is {0}", param);
     return(H5IterationResult.SUCCESS);
 }
 public NeuralNetworkH5Loader(string h5File)
 {
     if (h5File is null)
     {
         throw new ArgumentNullException(nameof(h5File));
     }
     H5FileId              = H5F.open(h5File, H5F.OpenMode.ACC_RDONLY);
     H5GroupId             = H5G.open(H5FileId, "/");
     H5GroupIdModelWeights = H5G.open(H5GroupId, "model_weights");
 }
Exemple #10
0
        // Generate string attribute
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a string
        public static void StringAttributeGenerator(H5GroupId GroupName, string AttName, string AttContent)
        {
            char[]        AttContentChar = AttContent.ToCharArray();
            byte[]        asciiStr       = ASCIIEncoding.ASCII.GetBytes(AttContentChar);
            int           length         = asciiStr.Length;
            H5AttributeId attributeId    = H5A.create(GroupName, AttName, H5T.create(H5T.CreateClass.STRING, length), H5S.create(H5S.H5SClass.SCALAR));

            H5A.write(attributeId, H5T.create(H5T.CreateClass.STRING, length), new H5Array <byte>(asciiStr));
            H5A.close(attributeId);
        }
Exemple #11
0
        // Generate double attributes
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a single floating number, here 64bit double is used, to generate subgroups in Data
        public static void DoubleAttributeGenerator(H5GroupId GroupName, string AttName, double AttContent)
        {
            double[] AttArray = new double[1] {
                AttContent
            };
            long[] dims = new long[1];
            dims[0] = AttArray.Length;
            H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_DOUBLE), H5S.create_simple(1, dims));

            H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <double>(AttArray));
            H5A.close(attributeId);
        }
Exemple #12
0
        // Generate floating number attributes
        // GroupName: target group for the new attribute
        // AttName: attribute name
        // AttContent: content for the attribute, has to be a single floating number, here 32bit floating number is used, consider using 64bit double if necessary
        public static void NumberAttributeGenerator(H5GroupId GroupName, string AttName, float AttContent)
        {
            float[] AttArray = new float[1] {
                AttContent
            };
            long[] dims = new long[1];
            dims[0] = AttArray.Length;
            H5AttributeId attributeId = H5A.create(GroupName, AttName, H5T.copy(H5T.H5Type.NATIVE_FLOAT), H5S.create_simple(1, dims));

            H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_FLOAT), new H5Array <float>(AttArray));
            H5A.close(attributeId);
        }
Exemple #13
0
        private void createHD5GroupObject(H5GroupId id, String name, ref HDF5GroupObject hdf5Obj)
        {
            hdf5Obj.GroupID   = id;
            hdf5Obj.GroupName = name;

            long num = H5G.getNumObjects(hdf5Obj.GroupID);

            // the items under the group
            for (ulong i = 0; i < (ulong)num; i++)
            {
                String objName = H5G.getObjectNameByIndex(hdf5Obj.GroupID, i);

                // get the link info.
                //linfo = H5L.getInfo(groupID, objName);

                ObjectInfo objInfo = H5G.getObjectInfo(hdf5Obj.GroupID, objName, true);
                if (objInfo.objectType == H5GType.GROUP)
                {
                    String pathName = "/" + name + "/" + objName;
                    if (name.CompareTo("/") == 0)
                    {
                        pathName = "/" + objName;
                    }
                    H5GroupId groupID = null;
                    try
                    {
                        groupID = H5G.open(hdf5Obj.GroupID, pathName);
                        HDF5GroupObject subObj = new HDF5GroupObject();
                        subObj.ShortName = objName;
                        createHD5GroupObject(groupID, pathName, ref subObj);
                        hdf5Obj.SubGroups.Add(subObj);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        if (groupID != null)
                        {
                            H5G.close(groupID);
                        }
                    }
                }
                else
                if (objInfo.objectType == H5GType.DATASET)
                {
                    String           pathName   = "/" + name + "/" + objName;
                    HD5DataSetObject dataObject = new HD5DataSetObject();
                    dataObject.ShortName = objName;
                    createHD5DataObject(hdf5Obj.GroupID, pathName, ref dataObject);
                    hdf5Obj.Datasets.Add(dataObject);
                }
            }
        }
Exemple #14
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);
                }
            }
        }
Exemple #15
0
        private void Write(H5GroupId parent, string name, double d)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] {
                1
            });
            H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_DOUBLE, spaceId);

            double data = d;

            H5D.writeScalar <double>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE), ref data);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
Exemple #16
0
        private void Write(H5GroupId parent, string name, ulong ul)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] {
                1
            });
            H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_ULLONG, spaceId);

            ulong data = ul;

            H5D.writeScalar <ulong>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_ULLONG), ref data);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
Exemple #17
0
        private static H5GroupId CreateGroupIfNoneExists(H5FileId fileId, string path)
        {
            H5GroupId group = null;

            if (H5L.Exists(fileId, path))
            {
                group = H5G.open(fileId, path);
            }
            else
            {
                group = H5G.create(fileId, path);
            }
            return(group);
        }
Exemple #18
0
        private void Write(H5GroupId parent, string name, IMeasurement m)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] {
                (long)1
            });
            H5DataSetId dataSetId = H5D.create(parent, name, measurement_t, spaceId);

            MeasurementT mt = Convert(m);

            H5D.writeScalar <MeasurementT>(dataSetId, measurement_t, ref mt);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
Exemple #19
0
        public override void Serialize(Epoch e, string fileTag)
        {
            if (!e.StartTime)
            {
                throw new PersistanceException("Epoch must have a start time.");
            }

            if (CurrentEpochGroupID == null)
            {
                throw new InvalidOperationException("Cannot serialize an Epoch without an open EpochGroup. Call BeginEpochGroup before attempting to serialize an Epoch.");
            }

            var       groupName = "epoch-" + e.ProtocolID + "-" + GuidGenerator();
            H5GroupId epochID   = H5G.create(CurrentEpochGroupID.EpochsId, groupName); //epochGroupMetadata

            log.DebugFormat("Serializing Epoch to HDF5 {0}", groupName);

            WriteAttribute(epochID, "protocolID", e.ProtocolID);

            // WriteEpochStart and duration
            WriteAttribute(epochID, startTimeOffsetName, ((DateTimeOffset)e.StartTime).Offset.TotalHours); //TimeZone.CurrentTimeZone.GetUtcOffset(((DateTimeOffset)e.StartTime).DateTime).TotalHours);
            WriteAttribute(epochID, startTimeUtcName, ((DateTimeOffset)e.StartTime).Ticks);
            WriteAttribute(epochID, "durationSeconds", ((TimeSpan)e.Duration).TotalSeconds);
            if (fileTag != null)
            {
                WriteAttribute(epochID, "fileTag", fileTag);
            }

            //Write keywords
            WriteKeywords(epochID, e.Keywords);

            // WriteBackground
            Write(epochID, "background", e.Background);

            // WriteProtocolParams
            WriteDictionary(epochID, "protocolParameters", e.ProtocolParameters);

            // WriteStimulus
            WriteStimuli(epochID, "stimuli", e.Stimuli);

            // WriteResponse
            WriteResponses(epochID, "responses", e.Responses);


            H5G.close(epochID);

            H5F.flush(fileId);
        }
Exemple #20
0
        /// <summary>
        /// 得到所有数据集合名,未对异常进行处理
        /// </summary>
        private void getDatasetNames()
        {
            H5GroupId groupId = H5G.open(_fileId, "/");
            long      dscount = H5G.getNumObjects(groupId);

            for (int ii = 0; ii < dscount; ++ii)
            {
                String v = H5G.getObjectNameByIndex(groupId, (ulong)ii);
                _datasetNames.Add(v);
            }

            if (groupId != null)
            {
                H5G.close(groupId);
            }
        }
Exemple #21
0
        public static H5FileId InitializeHDF(int numpos, int height, int width, UInt16[,,] datacube, string fullpath)
        {
            // write in HDF5 (.h5) format

            // generate standard groups
            H5FileId fileId = H5F.create(fullpath,
                                         H5F.CreateMode.ACC_TRUNC);
            //H5FileId fileId = H5F.create("D:/2017/Pixelated Camera/CameraSoftware/FileFormat/Test/test5.emd",
            //H5F.CreateMode.ACC_TRUNC);
            //NumberAttributeGenerator(fileId, "voltage", Convert.ToSingle(300));

            //H5GroupId dataGroup = H5G.create(fileId, "/data");  //dash is required for root group
            H5GroupId userGroup    = H5G.create(fileId, "/user");
            H5GroupId micGroup     = H5G.create(fileId, "/microscope");
            H5GroupId sampleGroup  = H5G.create(fileId, "/sample");
            H5GroupId commentGroup = H5G.create(fileId, "/comments");

            // generate attributes for user group, all attributes are sting
            StringAttributeGenerator(userGroup, "user", "Chenyu Zhang");
            StringAttributeGenerator(userGroup, "email", "*****@*****.**");
            StringAttributeGenerator(userGroup, "institution", "UW-Madison");
            StringAttributeGenerator(userGroup, "department", "Materials Science and Engineering");

            // generate attributes for microscope group
            StringAttributeGenerator(micGroup, "voltage units", "kV");
            NumberAttributeGenerator(micGroup, "voltage", Convert.ToSingle(300));
            StringAttributeGenerator(micGroup, "wavelength units", "nm");
            NumberAttributeGenerator(micGroup, "wavelength", Convert.ToSingle(0.00197));

            // generate attributes for sample group
            StringAttributeGenerator(sampleGroup, "material", "STO");
            StringAttributeGenerator(sampleGroup, "preparation", "Mechanical polishing and ion milling");
            StringAttributeGenerator(sampleGroup, "Zone Axis", "[1][0][0]");

            // Write 3D data cube to the file
            WriteDataCube(fileId, datacube);

            // close groups and file
            H5G.close(userGroup);
            H5G.close(micGroup);
            H5G.close(sampleGroup);
            H5G.close(commentGroup);
            //H5G.close(dataGroup);
            H5F.close(fileId);

            return(fileId);
        }
Exemple #22
0
        public static void WriteDataCube(H5FileId fileId, UInt16[,,] datacube)
        {
            H5GroupId dataGroup    = H5G.create(fileId, "/data");
            H5GroupId dataSubGroup = H5G.create(dataGroup, "DEsoftware");

            long[] dims = new long[3] {
                datacube.GetLength(0), datacube.GetLength(1), datacube.GetLength(2)
            };

            H5DataSpaceId spaceId   = H5S.create_simple(3, dims);
            H5DataTypeId  typeId    = H5T.copy(H5T.H5Type.NATIVE_USHORT);
            H5DataSetId   dataSetId = H5D.create(dataSubGroup, "data",
                                                 typeId, spaceId);

            // create attribute emd_group_type for dataSubGroup, which is required to have value 1
            int par = 1;

            long[] attdims  = new long[1];
            int[]  AttArray = new int[1] {
                par
            };
            dims[0] = AttArray.Length;
            H5AttributeId attributeId = H5A.create(dataSubGroup, "emd_group_type", H5T.copy(H5T.H5Type.NATIVE_UCHAR), H5S.create_simple(1, dims));

            H5A.write(attributeId, H5T.copy(H5T.H5Type.NATIVE_INT), new H5Array <int>(AttArray));
            H5A.close(attributeId);

            // write datacube to "data", which contains whole 3D datacube
            H5D.write <ushort>(dataSetId, typeId, new H5Array <ushort>(datacube));

            // create three more array for three dimensions
            long[] dim1 = new long[1] {
                datacube.GetLength(0)
            };
            double[] dimarray = new double [datacube.GetLength(0)];
            spaceId   = H5S.create_simple(3, dim1);
            typeId    = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
            dataSetId = H5D.create(dataSubGroup, "dim1", typeId, spaceId);
            H5D.write <double>(dataSetId, typeId, new H5Array <double>(dimarray));

            H5S.close(spaceId);
            H5T.close(typeId);
            H5D.close(dataSetId);
            H5G.close(dataGroup);
            H5G.close(dataSubGroup);
        }
Exemple #23
0
        public static List <string> GetChildGroupNames(H5GroupId _groupId)
        {
            List <string> names = new List <string>();

            ulong count = (ulong)H5G.getNumObjects(_groupId);;

            for (ulong i = 0; i < count; i++)
            {
                string name = H5G.getObjectNameByIndex(_groupId, i);

                ObjectInfo info = H5G.getObjectInfo(_groupId, name, false);
                if (info.objectType == H5GType.GROUP)
                {
                    names.Add(name);
                }
            }

            return(names);
        }
Exemple #24
0
        /// <summary>
        /// 得到所有文件属性名,未对异常进行处理
        /// </summary>
        private void getFileAttributeNames()
        {
            H5GroupId groupId  = H5G.open(_fileId, "/");
            int       attCount = H5A.getNumberOfAttributes(groupId);

            for (int ii = 0; ii < attCount; ++ii)
            {
                String attName = H5A.getNameByIndex(groupId, "/", H5IndexType.NAME, H5IterationOrder.NATIVE, (ulong)ii);
                if (attName != null)
                {
                    _fileAttributeNames.Add(attName);
                }
            }

            if (groupId != null)
            {
                H5G.close(groupId);
            }
        }
Exemple #25
0
        private static H5DataSetId CreateDatasetIfNoneExists(H5FileId fileId, string setName, int dim1, H5DataTypeId dataType)
        {
            H5DataSetId dataSetId = null;

            char[]   sep     = { '/' };
            string[] strings = setName.Split(sep);
            string   path    = "/";

            for (int x = 0; x < strings.Length - 1; x++)
            {
                path += strings[x] + "/";
                H5GroupId groupId = CreateGroupIfNoneExists(fileId, path);
            }

            long[] dims = new long[] {
                dim1
            };
            long[] maxDims = new long[] {
                -1
            };

            if (H5L.Exists(fileId, setName))
            {
                dataSetId = H5D.open(fileId, setName);
                H5D.setExtent(dataSetId, dims);
            }
            else
            {
                H5PropertyListId linkp   = H5P.create(H5P.PropertyListClass.LINK_CREATE);
                H5PropertyListId accessp = H5P.create(H5P.PropertyListClass.DATASET_ACCESS);
                H5PropertyListId createp = H5P.create(H5P.PropertyListClass.DATASET_CREATE);
                H5P.setChunk(createp, dims);
                H5P.setDeflate(createp, 1);

                H5DataSpaceId sId = H5S.create_simple(1, dims, maxDims);

                dataSetId = H5D.create(fileId, setName, dataType, sId, linkp, createp, accessp);
            }

            return(dataSetId);
        }
Exemple #26
0
        private void WriteConfigurationSpans(H5GroupId gID, IEnumerable <IConfigurationSpan> configurationSpans)
        {
            var spansId = H5G.create(gID, DATA_CONFIGURATION_SPANS_GROUP);

            uint i         = 0;
            var  totalTime = TimeSpan.Zero;

            foreach (var configSpan in configurationSpans)
            {
                var spanId = H5G.create(spansId, DATA_CONFIGURATION_SPAN_PREFIX + i++);

                WriteAttribute(spanId, "startTimeSeconds", totalTime.TotalSeconds);
                totalTime += configSpan.Time;

                WriteAttribute(spanId, "timeSpanSeconds", configSpan.Time.TotalSeconds);
                foreach (var nodeConfig in configSpan.Nodes)
                {
                    WriteDictionary(spanId, nodeConfig.Name, nodeConfig.Configuration);
                }
            }
        }
Exemple #27
0
        private static T[,] Read2DArray <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);

            if (typeof(T) == typeof(string))
            {
                // this will also need a string hack...
                T[,] dataArray = new T[dims[0], 2];
                return(dataArray);
            }
            else
            {
                T[,] dataArray = new T[dims[0], dims[1]];
                var wrapArray = new H5Array <T>(dataArray);
                H5D.read(dataset, dataType, wrapArray);
                return(dataArray);
            }
        }
Exemple #28
0
        public void GetHdfObjInfo(string groupName)
        {
            H5GroupId          groupId = H5G.open(m_fileId, groupName);
            H5LIterateCallback myDelegate;

            myDelegate = Op_Func;
            ulong linkNumber = 0;

            int attriNum = H5A.getNumberOfAttributes(groupId);

            for (int i = 0; i < attriNum; i++)
            {
                string attriName = H5A.getNameByIndex(groupId, groupName, H5IndexType.CRT_ORDER, H5IterationOrder.NATIVE, (ulong)i);

                OnHdfObjInfoFound(attriName, HdfObjectType.Attribute, groupName);
            }


            H5L.iterate(groupId, H5IndexType.NAME, H5IterationOrder.NATIVE, ref linkNumber, myDelegate, groupName);
            H5G.close(groupId);
        }
        /// <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);
        }
Exemple #30
0
        private void WriteResponses(H5GroupId parent, string name, IDictionary <IExternalDevice, Response> dict)
        {
            H5GroupId dictId = H5G.create(parent, name);

            foreach (var ed in dict.Keys)
            {
                Response r = dict[ed];

                H5GroupId rId = H5G.create(dictId, ed.Name);
                WriteAttribute(rId, "deviceName", ed.Name);
                WriteAttribute(rId, "deviceManufacturer", ed.Manufacturer);
                WriteAttribute(rId, "inputTimeUTC", r.InputTime.ToUniversalTime().ToString());
                WriteAttribute(rId, "sampleRate", r.SampleRate.QuantityInBaseUnit);
                WriteAttribute(rId, "sampleRateUnits", r.SampleRate.BaseUnit);
                Write(rId, "data", r.Data);

                WriteResponseDataConfigurationSpans(rId, r);

                H5G.close(rId);
            }

            H5G.close(dictId);
        }
        private void WriteResponses(H5GroupId parent, string name, IDictionary<IExternalDevice, Response> dict)
        {
            H5GroupId dictId = H5G.create(parent, name);

            foreach (var ed in dict.Keys)
            {
                Response r = dict[ed];

                H5GroupId rId = H5G.create(dictId, ed.Name);
                WriteAttribute(rId, "deviceName", ed.Name);
                WriteAttribute(rId, "deviceManufacturer", ed.Manufacturer);
                WriteAttribute(rId, "inputTimeUTC", r.InputTime.ToUniversalTime().ToString());
                WriteAttribute(rId, "sampleRate", r.SampleRate.QuantityInBaseUnit);
                WriteAttribute(rId, "sampleRateUnits", r.SampleRate.BaseUnit);
                Write(rId, "data", r.Data);

                WriteResponseDataConfigurationSpans(rId, r);

                H5G.close(rId);
            }

            H5G.close(dictId);
        }
        private void Write(H5GroupId parent, string name, string str)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Name must not be empty (or null)", "name");

            if (string.IsNullOrEmpty(str))
                throw new ArgumentException("Value must not be empty or null", "value");

            H5DataTypeId dtype;
            byte[] data = EncodeStringData(str, out dtype);

            H5DataSpaceId spaceId = H5S.create_simple(1, new long[] { 1 });
            H5DataSetId dataSetId = H5D.create(parent, name, dtype, spaceId);

            H5D.write(dataSetId, string_t, new H5Array<byte>(data));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
        private void Write(H5GroupId parent, string name, IDictionary<IExternalDevice, Epoch.EpochBackground> background)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)background.Keys.Count });
            H5DataSetId dataSetId = H5D.create(parent, name, extdevmeasurement_t, spaceId);

            ExtdevBackgroundMeasurementT[] emts = new ExtdevBackgroundMeasurementT[background.Keys.Count];
            int count = 0;

            foreach (var ed in background.Keys)
            {
                IMeasurement m = background[ed].Background;
                IMeasurement sr = background[ed].SampleRate;

                emts[count] = Convert(ed, m, sr);
                count++;
            }

            H5D.write<ExtdevBackgroundMeasurementT>(dataSetId, extdevmeasurement_t, new H5Array<ExtdevBackgroundMeasurementT>(emts));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
Exemple #34
0
 public Group(Container container, string groupname)
 {
     _groupname = groupname;
     _group_id = H5G.open((H5LocId)container.Id, groupname);
 }
        private void WriteDictionary(H5GroupId parent, string name, IDictionary<string, object> dict)
        {
            if (name == null)
                throw new ArgumentException("Dictionary name may not be null", "name");

            H5GroupId dictId = H5G.create(parent, name);
            try
            {
                // Make a local copy of Keys
                var keys = dict.Keys.Select(k => k).ToList();

                foreach (var key in keys)
                {
                    if(!dict.ContainsKey(key))
                    {
                        log.WarnFormat("Dictionary value for key {0} missing during persistence. This likely means someone else modified the dictionary while it was being persisted.", key);
                        continue;
                    }

                    var value = dict[key];

                    if(value == null)
                    {
                        WriteAttribute(dictId, key, "<null>");
                    }
                    else if (value is double[])
                    {
                        WriteAttribute(dictId, key, (double[])value);
                    }
                    else if (value is IMeasurement)
                    {
                        var measurement = value as IMeasurement;
                        WriteAttribute(dictId, key + "_quantity", measurement.Quantity);
                        WriteAttribute(dictId, key + "_units", measurement.DisplayUnit);
                        WriteAttribute(dictId, key, measurement.ToString());
                    }
                    else if (value is decimal)
                    {
                        WriteAttribute(dictId, key, (decimal)value);
                    }
                    else if (value is double)
                    {
                        WriteAttribute(dictId, key, (double)value);
                    }
                    else if (value is long)
                    {
                        WriteAttribute(dictId, key, (long)value);
                    }
                    else if (value is float)
                    {
                        WriteAttribute(dictId, key, (float)value);
                    }
                    else if (value is int)
                    {
                        WriteAttribute(dictId, key, (int)value);
                    }
                    else if (value is short)
                    {
                        WriteAttribute(dictId, key, (short) value);
                    }
                    else if (value is byte)
                    {
                        short tmp = (byte)value;
                        WriteAttribute(dictId, key, tmp);
                    }
                    else if (value is UInt16)
                    {
                        int tmp = (UInt16) value;
                        WriteAttribute(dictId, key, tmp);
                    }
                    else if (value is UInt32)
                    {
                        long tmp = (UInt32) value;
                        WriteAttribute(dictId, key, tmp);
                    }
                    else if (value is bool)
                    {
                        WriteAttribute(dictId, key, (bool)value);
                    }
                    else if (value is string)
                    {
                        WriteAttribute(dictId, key, (string)value);
                    }
                    else
                    {
                        log.WarnFormat("Dictionary value ({0} : {1}) is of usupported type. Falling back to string representation.", key, value);
                        WriteAttribute(dictId, key, String.Format("{0}", value));
                    }
                }
            }
            finally
            {
                H5G.close(dictId);
            }
        }
 public EpochGroupIDs(H5GroupId group,
     H5GroupId subGroups,
     H5GroupId epochs)
 {
     GroupId = group;
     SubGroupsId = subGroups;
     EpochsId = epochs;
 }
Exemple #37
0
        } // test_group_basics

        /*
         * Operator function.
         */
        static int file_info(H5GroupId id, string objectName, Object param)
        {
            Console.WriteLine("The object name is {0}", objectName);
            return 0;
        }
        private void WriteConfigurationSpans(H5GroupId gID, IEnumerable<IConfigurationSpan> configurationSpans)
        {
            var spansId = H5G.create(gID, DATA_CONFIGURATION_SPANS_GROUP);

            uint i = 0;
            var totalTime = TimeSpan.Zero;
            foreach (var configSpan in configurationSpans)
            {
                var spanId = H5G.create(spansId, DATA_CONFIGURATION_SPAN_PREFIX + i++);

                WriteAttribute(spanId, "startTimeSeconds", totalTime.TotalSeconds);
                totalTime += configSpan.Time;

                WriteAttribute(spanId, "timeSpanSeconds", configSpan.Time.TotalSeconds);
                foreach (var nodeConfig in configSpan.Nodes)
                {
                    WriteDictionary(spanId, nodeConfig.Name, nodeConfig.Configuration);
                }

            }
        }
        private void Write(H5GroupId parent, string name, IDictionary<IExternalDevice, Measurement> dict)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)dict.Keys.Count });
            H5DataSetId dataSetId = H5D.create(parent, name, extdevmeasurement_t, spaceId);

            ExtdevMeasurementT[] emts = new ExtdevMeasurementT[dict.Keys.Count];
            int count = 0;

            foreach (var ed in dict.Keys)
            {
                IMeasurement m = dict[ed];

                emts[count] = Convert(ed, m);
                count++;
            }

            H5D.write<ExtdevMeasurementT>(dataSetId, extdevmeasurement_t, new H5Array<ExtdevMeasurementT>(emts));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
        private void Write(H5GroupId parent, string name, ulong ul)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { 1 });
            H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_ULLONG, spaceId);

            ulong data = ul;
            H5D.writeScalar<ulong>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_ULLONG), ref data);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
 private void WriteResponseDataConfigurationSpans(H5GroupId rId, Response r)
 {
     WriteConfigurationSpans(rId, r.DataConfigurationSpans);
 }
        private void Write(H5GroupId parent, string name, double d)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { 1 });
            H5DataSetId dataSetId = H5D.create(parent, name, H5T.H5Type.NATIVE_DOUBLE, spaceId);

            double data = d;
            H5D.writeScalar<double>(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE), ref data);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
 private void WriteStimulusDataConfigurationSpans(H5GroupId sId, IStimulus s)
 {
     WriteConfigurationSpans(sId, s.OutputConfigurationSpans);
 }
Exemple #44
0
 // Function used with 
 static int myFunction(H5GroupId id, string objectName, Object param)
 {
     Console.WriteLine("The object name is {0}", objectName);
     Console.WriteLine("The object parameter is {0}", param);
     return 0;
 }
        private void Write(H5GroupId parent, string name, IMeasurement m)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)1 });
            H5DataSetId dataSetId = H5D.create(parent, name, measurement_t, spaceId);

            MeasurementT mt = Convert(m);
            H5D.writeScalar<MeasurementT>(dataSetId, measurement_t, ref mt);

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
        private void Write(H5GroupId parent, string name, IEnumerable<IMeasurement> measurements)
        {
            H5DataSpaceId spaceId = H5S.create_simple(1, new long[1] { (long)measurements.Count() });

            // Set compression options for dataset
            H5PropertyListId dataSetPropertyList = H5P.create(H5P.PropertyListClass.DATASET_CREATE);
            H5P.setDeflate(dataSetPropertyList, NumericDataCompression);
            H5P.setChunk(dataSetPropertyList, new long[] {(long)measurements.Count()});

            H5DataSetId dataSetId = H5D.create(parent,
                name,
                measurement_t,
                spaceId,
                new H5PropertyListId(H5P.Template.DEFAULT),
                dataSetPropertyList,
                new H5PropertyListId(H5P.Template.DEFAULT));

            MeasurementT[] ms = new MeasurementT[measurements.Count()];
            int ilmCount = 0;
            foreach (IMeasurement m in measurements)
            {
                MeasurementT mt = Convert(m);
                ms[ilmCount++] = mt;
            }

            H5D.write<MeasurementT>(dataSetId, measurement_t, new H5Array<MeasurementT>(ms));

            H5D.close(dataSetId);
            H5S.close(spaceId);
        }
        private void WriteStimuli(H5GroupId parent, string name, IDictionary<IExternalDevice, IStimulus> dict)
        {
            H5GroupId dictId = H5G.create(parent, name);

            foreach (var ed in dict.Keys)
            {
                var s = dict[ed];

                H5GroupId sId = H5G.create(dictId, ed.Name);
                WriteAttribute(sId, "deviceName", ed.Name);
                WriteAttribute(sId, "deviceManufacturer", ed.Manufacturer);
                WriteAttribute(sId, "stimulusID", s.StimulusID);
                WriteAttribute(sId, "stimulusUnits", s.Units);

                WriteDictionary(sId, "parameters", s.Parameters);

                WriteStimulusDataConfigurationSpans(sId, s);

                H5G.close(sId);
            }

            H5G.close(dictId);
        }