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); } } }
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); } } }
/// <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); } }
private bool getOMXMatrixTables() { // get info about the tables this.dataGroup = H5G.open(fileId, dataGroupName); this.NumMatrix = (int)H5G.getNumObjects(this.dataGroup); this.MatrixNames = new List <string>(); for (int i = 0; i < NumMatrix; i++) { string matName = H5G.getObjectNameByIndex(this.dataGroup, (ulong)i); MatrixNames.Add(matName); H5DataSetId matId = H5D.open(dataGroup, matName); tables.Add(matName, matId); } return(true); }
private bool getOMXIndexMaps() { this.luGroup = H5G.open(fileId, luGroupName); this.NumIndexMap = (int)H5G.getNumObjects(this.luGroup); this.IndexMapNames = new List <string>(); for (int i = 0; i < NumIndexMap; i++) { string imName = H5G.getObjectNameByIndex(this.luGroup, (ulong)i); IndexMapNames.Add(imName); H5DataSetId imId = H5D.open(luGroup, imName); indexMaps.Add(imName, imId); } return(true); }
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); }
public void SimpleGroupCreateFind() { // Create an HDF5 file. // The enumeration type H5F.CreateMode provides only the legal // creation modes. Missing H5Fcreate parameters are provided // with default values. H5FileId fileId = H5F.create(TEST_FILE, H5F.CreateMode.ACC_TRUNC); // Create a group in the file H5GroupId groupId = H5G.create(fileId, "/simple"); // Close everything down. H5G.close(groupId); H5F.close(fileId); Assert.IsTrue(System.IO.File.Exists(TEST_FILE)); fileId = H5F.open(TEST_FILE, H5F.OpenMode.ACC_RDONLY); groupId = H5G.open(fileId, "/simple"); Assert.AreEqual(0, H5G.getNumObjects(groupId)); H5G.close(groupId); H5F.close(fileId); }
} // test_group_iterate static void test_group_info() { try { Console.Write("Testing getting group information"); // Open the file. H5FileId fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); // Open the root group. H5GroupId groupId = H5G.open(fileId, "/"); // Get number of objects in the root group. long num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_info: Root group should have 3 objects: /fromRoot, /Data, and /singleDataset, but is {0}", num_objs); nerrors++; } // The root group has three objects in alphabetical order: // intArray // notfromRoot // shortArray // Note that, in order for this function to work correctly when taking the third // parameter as H5IndexType.CRT_ORDER, the group's creation order must be tracked // by setting the group creation property list with the C function H5Pset_link_creation_order, // which is not included in this .NET wrappers version. - Mar, 2008 // Check object #3, should be "notfromRoot" string obj_name = H5L.getNameByIndex(groupId, ".", H5IndexType.NAME, H5IterationOrder.INCREASING, 2); if (obj_name != "singleDataset") { Console.WriteLine("\ntest_group_info: Incorrect name for 2nd objects from root group: {0}, should be {1}", obj_name, "singleDataset"); nerrors++; } // Close root group. H5G.close(groupId); // Open group "/fromRoot" groupId = H5G.open(fileId, "/fromRoot"); H5GInfo oinfo = H5G.getInfo(groupId); if (oinfo.nLinks != 3) { Console.WriteLine("\ntest_group_info: /fromRoot should have 3 objects: intArray, shortArray, notfromRoot, but is {0}", num_objs); nerrors++; } // Check objects #1 and #3, should be "intArray" and "notfromRoot" obj_name = H5L.getNameByIndex(groupId, ".", H5IndexType.NAME, H5IterationOrder.INCREASING, 0); if (obj_name != "intArray") { Console.WriteLine("\ntest_group_info: Incorrect name for 1st object from group named /fromRoot: {0}, should be {1}", obj_name, "intArray"); nerrors++; } obj_name = H5L.getNameByIndex(groupId, ".", H5IndexType.NAME, H5IterationOrder.INCREASING, 2, new H5PropertyListId(H5P.Template.DEFAULT)); if (obj_name != "shortArray") { Console.WriteLine("\ntest_group_info: Incorrect name for 3rd object from group named /fromRoot: {0}, should be {1}", obj_name, "shortArray"); nerrors++; } // Close objects and file. H5G.close(groupId); H5F.close(fileId); Console.WriteLine("\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_group_info
static void test_group_basics() { try { Console.Write("Testing group basics"); // Create the file. H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC); // Create a group. H5GroupId groupId = H5G.create(fileId, "/fromRoot"); // Create a dataspace for common use. hssize_t[] dims = { 1000, 20 }; H5DataSpaceId dspace = H5S.create_simple(RANK, dims); // Create a dataset using file as location with absolute path name. H5DataSetId dset1Id = H5D.create(fileId, "/fromRoot/intArray", H5T.H5Type.NATIVE_INT, dspace); // Create a dataset using group as location with absolute path name. H5DataSetId dset2Id = H5D.create(groupId, "/fromRoot/shortArray", H5T.H5Type.NATIVE_SHORT, dspace); // Create a dataset using group as location with relative path name. H5DataSetId dset3Id = H5D.create(groupId, "notfromRoot", H5T.H5Type.NATIVE_UCHAR, dspace); ObjectInfo info = H5G.getObjectInfo(fileId, "/fromRoot/intArray", true); if (info.nHardLinks != 1) { Console.WriteLine("\ntest_group_basics: number of hardlinks for /fromRoot/intArray should be = {0}", info.nHardLinks); } if (info.objectType != H5GType.DATASET) { Console.WriteLine("\ntest_group_basics: Object should be a dataset"); } // Close objects and files. H5D.close(dset1Id); H5D.close(dset2Id); H5D.close(dset3Id); H5S.close(dspace); H5G.close(groupId); // Check various number of objects. H5GroupId rootId = H5G.open(fileId, "/"); hssize_t num_objs = H5G.getNumObjects(rootId); if (num_objs != 1) { Console.WriteLine("\ntest_group_basics: incorrect num_objs = {0} for root group\n", num_objs); nerrors++; } groupId = H5G.open(fileId, "fromRoot"); num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_basics: incorrect num_objs = {0} for group \"fromRoot\"\n", num_objs); nerrors++; } H5G.close(rootId); H5G.close(groupId); H5F.close(fileId); if (nerrors == 0) { Console.WriteLine("\t\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_group_basics
/* * test_group_iterate -- tests that group iterating works properly. * - opens the file created in the test_group_basics * - creates more groups and datasets * - iterates through root group and each sub group priting out name of each object */ static void test_group_iterate() { try { Console.Write("Testing group iteration"); // Open the file. H5FileId fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); // Create a group in the file. H5GroupId groupId = H5G.create(fileId, "/Data"); // Open first dataset. H5DataSetId dset1Id = H5D.open(fileId, "/fromRoot/intArray"); // Get dataspace of this dataset. H5DataSpaceId dspace = H5D.getSpace(dset1Id); // Create a dataset in the group using absolute name. H5DataSetId dataset = H5D.create(groupId, "/Data/IntData", H5T.H5Type.NATIVE_INT, dspace); // Close the first dataset. H5S.close(dspace); H5D.close(dataset); // Create the second dataset. hssize_t[] dims = { 500, 20 }; dspace = H5S.create_simple(RANK, dims); dataset = H5D.create(groupId, "/Data/FloatData", H5T.H5Type.NATIVE_FLOAT, dspace); // Close objects and file. H5D.close(dataset); H5G.close(groupId); H5F.close(fileId); // Now reopen the file and group in the file. fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); groupId = H5G.open(fileId, "/Data"); // Access "IntData" dataset in the group. dataset = H5D.open(groupId, "IntData"); // Create a dataset in the root group. dataset = H5D.create(fileId, "/singleDataset", H5T.H5Type.NATIVE_INT, dspace); // Various checks on number of objects groupId = H5G.open(fileId, "/"); hssize_t num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_iterate: / should have 3 objects: /fromRoot, /Data, and /singleDataset, but is {0}", num_objs); } H5G.close(groupId); groupId = H5G.open(fileId, "/fromRoot"); num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_iterate: /fromRoot should have 3 objects: intArray, shortArray, notfromRoot, but is {0}", num_objs); } // Use iterator to see the names of the objects in the root group and sub groups. H5GIterateCallback myCallback; myCallback = file_info; int x = 0; Console.WriteLine(); Console.WriteLine("\tRoot Group iterating:"); int index = H5G.iterate(fileId, "/", myCallback, "Object name:", ref x); Console.WriteLine(); Console.WriteLine("\t/fromRoot iterating:"); x = 0; index = H5G.iterate(fileId, "/fromRoot", myCallback, "Object name:", ref x); // Close objects and file. H5D.close(dataset); H5G.close(groupId); H5S.close(dspace); H5F.close(fileId); Console.WriteLine("\t\t\t\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_group_iterate
public SkimMatrix Read(string fileNameAndGroupAndDataTable, int field, float scale) { //mb merged these changes to local develop before pushing #210 //hdf5 filename contain "filename/group/skim" //get the index of group int hdf5GroupEnd = fileNameAndGroupAndDataTable.LastIndexOf("/"); string fileNameAndGroup = hdf5GroupEnd > 0 ? fileNameAndGroupAndDataTable.Substring(0, hdf5GroupEnd) : fileNameAndGroupAndDataTable; //get the index of filename int hdf5NameEnd = hdf5GroupEnd > 0 ? fileNameAndGroup.LastIndexOf("/") : -1; string groupName = fileNameAndGroup.Substring(hdf5NameEnd + 1); //get the omx/hdf5 filename string HDFName = fileNameAndGroup.Substring(0, hdf5NameEnd); string groupAndDataTable = fileNameAndGroupAndDataTable.Substring(hdf5NameEnd); string hdfFile = Path.Combine(_path, HDFName); FileInfo file = new FileInfo(hdfFile); if (!file.Exists) { throw new FileNotFoundException(string.Format("The skim file {0} could not be found.", file.FullName)); } H5FileId dataFile = H5F.open(hdfFile, H5F.OpenMode.ACC_RDONLY); H5DataSetId dataSet = H5D.open(dataFile, groupAndDataTable); H5DataSpaceId space = H5D.getSpace(dataSet); long[] size2 = H5S.getSimpleExtentDims(space); long nRows = size2[0]; long nCols = size2[1]; Debug.Assert(nRows == nCols); long numZones = _mapping.Count(); int[] lookupMap = null; string lookupMapName = null; string lookupGroupName = "lookup"; H5GroupId luGroup = H5G.open(dataFile, lookupGroupName); if (H5G.getNumObjects(luGroup) == 1L) { lookupMapName = H5G.getObjectNameByIndex(luGroup, 0); H5DataSetId lookupDataSet = H5D.open(dataFile, string.Concat(lookupGroupName, "/", lookupMapName)); H5DataTypeId lookupMapType = H5D.getType(lookupDataSet); H5DataSpaceId lookupMapSpace = H5D.getSpace(lookupDataSet); long lookupMapSize = H5S.getSimpleExtentDims(lookupMapSpace)[0]; lookupMap = new int[lookupMapSize]; H5Array <int> lookupWrapArray = new H5Array <int>(lookupMap); H5D.read(lookupDataSet, lookupMapType, lookupWrapArray); } if (lookupMap != null) { if (lookupMap.Length != nRows) { Global.PrintFile.WriteLine(string.Format("DATA WARNING: skim file: {0} has a lookup map named {1} but its length ({2}) is different than the matrix size ({3}) in group table {4}", hdfFile, lookupMapName, lookupMap.Length, nRows, groupAndDataTable)); lookupMap = null; } } // if the count in the hdf5 file is larger than the number of // tazs in the mapping, ignore the values over the total number //of tazs in the mapping because these are not valid zones. _matrix = new ushort[numZones][]; for (int i = 0; i < numZones; i++) { _matrix[i] = new ushort[numZones]; } //OMX is a square matrix of doubles //In addition to the data folder for matrices, an OMX file has a lookup folder //with a zone mapping vector. However, this is ignored since DaySim also has one. //Therefore, it is assumed the OMX matrix does not skip rows/cols and every row/col //corresponds to an actual zone in the DaySim zone mapping file by index double[,] dataArray = new double[nRows, nCols]; H5Array <double> wrapArray = new H5Array <double>(dataArray); H5DataTypeId tid1 = H5D.getType(dataSet); H5D.read(dataSet, tid1, wrapArray); long numSuccessfuLookups = 0; for (int row = 0; row < nRows; row++) { int mappingKey = (lookupMap == null) ? (row + 1) : lookupMap[row]; if (_mapping.TryGetValue(mappingKey, out int mappedRow)) { for (int col = 0; col < nCols; col++) { mappingKey = (lookupMap == null) ? (col + 1) : lookupMap[col]; if (_mapping.TryGetValue(mappingKey, out int mappedCol)) { double value = dataArray[row, col] * scale; ++numSuccessfuLookups; if (value > 0) { if (value > ushort.MaxValue - 1) { value = ushort.MaxValue - 1; } _matrix[mappedRow][mappedCol] = (ushort)value; //bug #208 deferred but this will eventually changed be Convert.ToUInt16 to avoid 0.57*100=56 bug } } } //for col } } //for row Global.PrintFile.WriteLine(string.Format("Loaded skim file: {0} dataset: {1}, scale={6} with input matrix size={2} and DaySim matrix size={3} zones. Percentage successful mappings {4}. {5}", hdfFile, groupAndDataTable, nRows, numZones, (numSuccessfuLookups / (nRows * nCols)) * 100.0, lookupMap != null ? string.Format("Using lookupMap {0}.", lookupMapName) : "No lookupMap.", scale), true); SkimMatrix skimMatrix = new SkimMatrix(_matrix); return(skimMatrix); }
public void SimpleDataReadWrite() { // Create an HDF5 file. // The enumeration type H5F.CreateMode provides only the legal // creation modes. Missing H5Fcreate parameters are provided // with default values. H5FileId fileId = H5F.create(TEST_FILE, H5F.CreateMode.ACC_TRUNC); // Create a group in the file H5GroupId groupId = H5G.create(fileId, "/simple"); // Prepare to create a data space for writing a 1-dimensional // signed integer array. const int RANK = 1; long[] dims = new long[RANK]; const int SIZE = 12; dims[0] = SIZE; // Put descending ramp data in an array so that we can // write it to the file. int[] dset_data = new int[SIZE]; for (int i = 0; i < SIZE; i++) { dset_data[i] = SIZE - i; } // Create a data space to accommodate our 1-dimensional array. // The resulting H5DataSpaceId will be used to create the // data set. H5DataSpaceId spaceId = H5S.create_simple(RANK, dims); // Create the data set. H5DataSetId dataSetId = H5D.create(fileId, "/arrayIntExample", H5T.H5Type.NATIVE_INT, spaceId); // Write the integer data to the data set. H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(dset_data)); // If we were writing a single value it might look like this. // Create the data set. H5DataSetId scalarId = H5D.create(fileId, "/scalarIntExample", H5T.H5Type.NATIVE_INT, spaceId); int singleValue = 100; H5D.writeScalar(scalarId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), ref singleValue); // Close everything down. H5D.close(dataSetId); H5D.close(scalarId); H5S.close(spaceId); H5G.close(groupId); H5F.close(fileId); Assert.IsTrue(System.IO.File.Exists(TEST_FILE)); fileId = H5F.open(TEST_FILE, H5F.OpenMode.ACC_RDONLY); Assert.IsTrue(fileId.Id > 0); groupId = H5G.open(fileId, "/simple"); Assert.IsTrue(groupId.Id > 0); Assert.AreEqual(0, H5G.getNumObjects(groupId)); // Open the data set dataSetId = H5D.open(fileId, "/arrayIntExample"); Assert.IsTrue(dataSetId.Id > 0); long datasetsize = H5D.getStorageSize(dataSetId); Assert.AreEqual(SIZE * sizeof(int), datasetsize); // Read the integer data back from the data set int[] readDataBack = new int[SIZE]; H5D.read(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(readDataBack)); for (int i = 0; i < SIZE; i++) { Assert.AreEqual(SIZE - i, readDataBack[i]); } // Read back the single-int example scalarId = H5D.open(fileId, "/scalarIntExample"); Assert.IsTrue(scalarId.Id > 0); H5D.readScalar <int>(scalarId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), ref singleValue); Assert.AreEqual(100, singleValue); H5D.close(dataSetId); H5D.close(scalarId); H5G.close(groupId); H5F.close(fileId); }