/// <summary> /// 获得数据集的类型 /// </summary> public string GetDatasetType(string datasetName) { H5DataSetId datasetId = H5D.open(_fileId, datasetName); H5DataTypeId typeId = H5D.getType(datasetId); H5T.H5TClass typeClass = H5T.getClass(typeId); return(typeClass.ToString()); }
private Tuple <H5DataSetId, int> load_nd_datasetEx(Blob <T> blob, string strDatasetName, bool bReshape, int nMinDim = 1, int nMaxDim = int.MaxValue, H5GroupId id = null, bool bAllowSingleItems = false) { H5DataSetId ds = null; int nSingleItemSize = 0; try { if (id != null) { ds = H5D.open(id, strDatasetName); } else { ds = H5D.open(m_file, strDatasetName); } if (ds == null) { m_log.FAIL("Failed to find the dataset '" + strDatasetName + "'!"); } // Verify that the number of dimensions are in the accepted range. H5DataSpaceId dsSpace = H5D.getSpace(ds); if (dsSpace == null) { m_log.FAIL("Failed to get the dataset space!"); } int nDims = H5S.getSimpleExtentNDims(dsSpace); m_log.CHECK_GE(nDims, nMinDim, "The dataset dim is out of range!"); m_log.CHECK_LE(nDims, nMaxDim, "The dataset dim is out of range!"); long[] rgDims = H5S.getSimpleExtentDims(dsSpace); // Verify that the data format is what we expect: float or double H5DataTypeId dsType = H5D.getType(ds); if (dsType == null) { m_log.FAIL("Failed to get the dataset type!"); } H5T.H5TClass dataClass = H5T.getClass(dsType); switch (dataClass) { case H5T.H5TClass.FLOAT: m_log.WriteLine("Datatype class: H5T_FLOAT"); break; case H5T.H5TClass.INTEGER: m_log.WriteLine("Datatype class: H5T_INTEGER"); break; default: m_log.FAIL("Unsupported datatype class: " + dataClass.ToString()); break; } List <int> rgBlobDims = new List <int>(); for (int i = 0; i < nDims; i++) { rgBlobDims.Add((int)rgDims[i]); } if (bReshape) { blob.Reshape(rgBlobDims); } else { if (!Utility.Compare <int>(rgBlobDims, blob.shape())) { if (!bAllowSingleItems || (rgBlobDims.Count == 1 && rgBlobDims[0] != 1)) { string strSrcShape = Utility.ToString <int>(rgBlobDims); m_log.FAIL("Cannot load blob from hdf5; shape mismatch. Source shape = " + strSrcShape + ", target shape = " + blob.shape_string); } if (rgBlobDims.Count == 1) { nSingleItemSize = rgBlobDims[0]; } } } } catch (Exception excpt) { if (ds != null) { H5D.close(ds); ds = null; } throw excpt; } return(new Tuple <H5DataSetId, int>(ds, nSingleItemSize)); }
public void GetDataset <T>(H5FileId fileId, string datasetName, string groupName, T[, ,] datasetOut, DataValueType type) { H5GroupId groupId = H5G.open(fileId, groupName); H5DataSetId dataSetId = H5D.open(groupId, datasetName /*"EV_Emissive"*/); switch (type) { case DataValueType.FLOAT: H5DataTypeId tidfloat = new H5DataTypeId(H5T.H5Type.NATIVE_FLOAT); // Read the array back H5D.read(dataSetId, tidfloat, new H5Array <T>(datasetOut));//(dataSetId, tid1, new H5Array<int>(vlReadBackArray)); // H5T.close(tidfloat); break; case DataValueType.INT: H5DataTypeId tidint = new H5DataTypeId(H5T.H5Type.NATIVE_INT); // H5T.H5TClass c = H5T.getMemberClass(tid0); // Read the array back H5D.read(dataSetId, tidint, new H5Array <T>(datasetOut));//(dataSetId, tid1, new H5Array<int>(vlReadBackArray)); //H5T.close(tidint); break; case DataValueType.COMPOUND: H5DataTypeId tid0 = H5D.getType(dataSetId); int nMember = H5T.getNMembers(tid0); H5DataSpaceId spaceid = H5D.getSpace(dataSetId); long[] dims = H5S.getSimpleExtentDims(spaceid);//得到数据数组的大小,比如[3,1800,2048] int length = 1; for (int i = 0; i < dims.Length; i++) { length *= (int)dims[i]; } for (int i = 0; i < nMember; i++) { string memberName = H5T.getMemberName(tid0, i); H5DataTypeId memberTypeId = H5T.getMemberType(tid0, i); H5T.H5TClass dataClass = H5T.getClass(memberTypeId); //得到数据集的类型 string typeName = dataClass.ToString(); if (typeName == "INTEGER") //目前先只支持整形的 { H5DataTypeId tidtmp = H5T.create(H5T.CreateClass.COMPOUND, sizeof(int)); H5T.insert(tidtmp, memberName, 0, H5T.H5Type.NATIVE_INT); int[] dataTmp = new int[length]; H5D.read(dataSetId, tidtmp, new H5Array <int>(dataTmp)); for (int j = 0; j < length; j++) { datasetOut[0, j, i] = (T)Convert.ChangeType(dataTmp[j], datasetOut[0, j, i].GetType()); } } } H5S.close(spaceid); break; default: break; } H5D.close(dataSetId); H5G.close(groupId); //H5F.close(fileId); }
public DatasetInfo GetDatasetInfo(H5FileId fileId, string datasetName, string groupName) { DatasetInfo datasetInfo = new DatasetInfo(); datasetInfo.band = 1; datasetInfo.col = 1; datasetInfo.rank = 1; datasetInfo.row = 1; H5GroupId groupId = H5G.open(fileId, groupName); H5DataSetId dataSetId = H5D.open(groupId, datasetName); // ulong storeSize = H5D.getStorageSize(dataSetId); //得到数据数组存储大小 H5DataSpaceId spaceid = H5D.getSpace(dataSetId); long[] dims = H5S.getSimpleExtentDims(spaceid); //得到数据数组的大小,比如[3,1800,2048] datasetInfo.rank = H5S.getSimpleExtentNDims(spaceid); //得到数据数组的维数,比如3 int dimCount = dims.Length; if (dimCount == 2) { datasetInfo.col = Convert.ToInt32(dims[1]);//宽 datasetInfo.row = Convert.ToInt32(dims[0]); } else if (dimCount == 3) { datasetInfo.band = Convert.ToInt32(dims[0]); //波段数 datasetInfo.col = Convert.ToInt32(dims[2]); //宽 datasetInfo.row = Convert.ToInt32(dims[1]); //高 } else if (dimCount == 1) { datasetInfo.row = Convert.ToInt32(dims[0]);//高 } H5DataTypeId typeId = H5D.getType(dataSetId); H5T.H5TClass dataClass = H5T.getClass(typeId);//得到数据集的类型 string typeName = dataClass.ToString(); switch (typeName) { case "FLOAT": datasetInfo.type = DataValueType.FLOAT; break; case "INTEGER": datasetInfo.type = DataValueType.INT; break; case "COMPOUND": datasetInfo.type = DataValueType.COMPOUND; H5DataTypeId tid0 = H5D.getType(dataSetId); int nMember = H5T.getNMembers(tid0); datasetInfo.col = nMember; break; default: datasetInfo.type = DataValueType.EMPTY; break; } H5T.close(typeId); H5S.close(spaceid); H5D.close(dataSetId); H5G.close(groupId); return(datasetInfo); }
public Dictionary <string, string> GetAttributes(string datasetName) { if (string.IsNullOrEmpty(datasetName) || !_datasetNames.Contains(datasetName)) { return(null); } H5DataSetId datasetId = null; H5GroupId groupId = null; H5DataTypeId typeId = null; H5DataSpaceId spaceId = null; //H5PropertyListId psId = null; try { int groupIndex = datasetName.LastIndexOf('/'); if (groupIndex == -1) { datasetId = H5D.open(_h5FileId, datasetName); } else { string groupName = datasetName.Substring(0, groupIndex + 1); string dsName = datasetName.Substring(groupIndex + 1); groupId = H5G.open(_h5FileId, groupName); datasetId = H5D.open(groupId, dsName); } if (datasetId == null) { return(null); } Dictionary <string, string> attValues = new Dictionary <string, string>(); typeId = H5D.getType(datasetId); H5T.H5TClass type = H5T.getClass(typeId); int tSize = H5T.getSize(typeId); spaceId = H5D.getSpace(datasetId); long[] dims = H5S.getSimpleExtentDims(spaceId); long storageSize = H5D.getStorageSize(datasetId); attValues.Add("DataSetName", datasetName); attValues.Add("DataType", type.ToString()); attValues.Add("DataTypeSize", tSize.ToString() + "Byte"); attValues.Add("Dims", String.Join("*", dims)); attValues.Add("StorageSize", storageSize.ToString() + "Byte"); int attrCount = H5A.getNumberOfAttributes(datasetId); for (int i = 0; i < attrCount; i++) { string attName = H5A.getNameByIndex(datasetId, "/" + datasetName, H5IndexType.NAME, H5IterationOrder.NATIVE, (ulong)i); attValues.Add(attName, ReadAttributeValue(datasetId, attName)); } return(attValues); } finally { if (spaceId != null) { H5S.close(spaceId); } if (typeId != null) { H5T.close(typeId); } if (datasetId != null) { H5D.close(datasetId); } if (groupId != null) { H5G.close(groupId); } } }