private bool getOMXFileAttributes() { string version = "unknown"; // OMX Version H5AttributeId verId = H5A.open(fileId, omxVersionName); H5T.H5TClass verCl = H5T.getClass(H5A.getType(verId)); if (verCl.Equals(H5T.H5TClass.STRING)) { Byte[] buff = getAttribute <byte>(verId); ASCIIEncoding enc = new ASCIIEncoding(); version = enc.GetString(buff); } else if (verCl.Equals(H5T.H5TClass.FLOAT)) { float[] buff = getAttribute <float>(verId); version = buff.ToString(); // produces garbage - bail out from here... throw new NotImplementedException(); } else { throw new NotImplementedException(); } //Console.WriteLine("omx version: {0}", version); this.OmxVersion = version; H5A.close(verId); // matrix shape H5AttributeId shId = H5A.open(fileId, omxShapeAttr); H5T.H5TClass shCl = H5T.getClass(H5A.getType(shId)); if (shCl.Equals(H5T.H5TClass.INTEGER)) { int[] shape = getAttribute <int>(shId); this.Shape[0] = (long)shape[0]; this.Shape[1] = (long)shape[1]; } else if (shCl.Equals(H5T.H5TClass.FLOAT)) { float[] shape = getAttribute <float>(shId); this.Shape[0] = (long)shape[0]; this.Shape[1] = (long)shape[1]; // returns garbage, bail out from here throw new NotImplementedException(); } else { throw new NotImplementedException(); } H5A.close(shId); return(true); }
/// <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 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 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 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); } } }
//private string ArrayToString<T>(T[] v) //{ // StringBuilder sb = new StringBuilder(); // //sb.Append("["); // foreach (T t in v) // { // sb.Append(t.ToString()); // sb.Append(","); // } // if (sb.Length > 1) // sb.Remove(sb.Length - 1, 1); // //sb.Append("]"); // return sb.ToString(); //} private object GetAttributeValue(H5ObjectWithAttributes obj, string attributeName) { H5AttributeId attId = null; attId = H5A.open(obj, attributeName); if (attId == null) { return(null); } H5DataTypeId typeId = null; H5DataTypeId dtId = null; H5AttributeInfo attInfo = null; H5DataSpaceId spaceId = null; H5DataTypeId oldTypeId = null; object retObject = null; try { typeId = H5A.getType(attId); attInfo = H5A.getInfo(attId); dtId = H5A.getType(attId); spaceId = H5A.getSpace(attId); int dataSize = H5T.getSize(dtId); // oldTypeId = typeId; typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT); H5T.H5TClass typeClass = H5T.getClass(typeId); long[] dims = H5S.getSimpleExtentDims(spaceId); long dimSize = 1; if (dims.Length == 0) { dimSize = 1; } else { foreach (long dim in dims) { dimSize *= dim; } } switch (typeClass) { case H5T.H5TClass.STRING: long size = attInfo.dataSize; byte[] chars = ReadArray <byte>(size, attId, typeId); retObject = Encoding.ASCII.GetString(chars); break; case H5T.H5TClass.INTEGER: H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT; sign = H5T.getSign(oldTypeId); switch (dataSize) { case 1: retObject = ReadArray <byte>(dimSize, attId, typeId); break; case 2: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: retObject = ReadArray <Int16>(dimSize, attId, typeId); break; case H5T.Sign.UNSIGNED: retObject = ReadArray <UInt16>(dimSize, attId, typeId); break; } break; case 4: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: retObject = ReadArray <Int32>(dimSize, attId, typeId); break; case H5T.Sign.UNSIGNED: retObject = ReadArray <UInt32>(dimSize, attId, typeId); break; } break; case 8: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: retObject = ReadArray <Int64>(dimSize, attId, typeId); break; case H5T.Sign.UNSIGNED: retObject = ReadArray <UInt64>(dimSize, attId, typeId); break; } break; } break; case H5T.H5TClass.FLOAT: switch (dataSize) { case 4: retObject = ReadArray <float>(dimSize, attId, typeId); break; case 8: retObject = ReadArray <double>(dimSize, attId, typeId); break; } break; } return(retObject); } finally { if (spaceId != null) { H5S.close(spaceId); } if (attId != null) { H5A.close(attId); } if (oldTypeId != null) { H5T.close(oldTypeId); } if (typeId != null) { H5T.close(typeId); } if (dtId != null) { H5T.close(dtId); } } }
private void ReadOldDataSetData(string dataSetName, int bandIndex, out int bandWidth, out int bandHeight, out enumDataType dataType, out object retObject) { bandHeight = bandWidth = 0; dataType = enumDataType.UInt16; retObject = null; H5FileId _h5FileId = null; H5DataSpaceId spaceid = null; H5DataSetId dataSetId = null; try { _h5FileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY); //先找出含有指定波段的数据集 dataSetId = H5D.open(_h5FileId, dataSetName); spaceid = H5D.getSpace(dataSetId); long[] dims = H5S.getSimpleExtentDims(spaceid); //得到数据数组的大小,比如[3,1800,2048] int rank = H5S.getSimpleExtentNDims(spaceid); //得到数据数组的维数,比如3 int size = 0; if (rank == 1) { bandHeight = bandWidth = 1; size = bandWidth * bandHeight * rank; } else if (rank == 2) { bandWidth = Convert.ToInt32(dims[0]); bandHeight = Convert.ToInt32(dims[1]); size = bandWidth * bandHeight; } else if (rank == 3) { List <long> r = dims.ToList <long>(); r.Sort(); long[] temp = r.ToArray(); bandWidth = Convert.ToInt32(temp[1]); bandHeight = Convert.ToInt32(temp[2]); size = bandWidth * bandHeight * Convert.ToInt32(temp[0]); } int outSize = bandWidth * bandHeight; H5DataTypeId typeId = H5D.getType(dataSetId); H5T.H5TClass typeClass = H5T.getClass(typeId);//得到数据集的类型 int dataSize = H5T.getSize(typeId); H5DataTypeId newTypeId = null; switch (typeClass) { case H5T.H5TClass.INTEGER: H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT; sign = H5T.getSign(typeId); switch (dataSize) { case 1: newTypeId = H5T.copy(H5T.H5Type.NATIVE_B8); retObject = ReadArray <byte>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Byte; break; case 2: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: newTypeId = H5T.copy(H5T.H5Type.NATIVE_SHORT); retObject = ReadArray <Int16>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Int16; break; case H5T.Sign.UNSIGNED: newTypeId = H5T.copy(H5T.H5Type.NATIVE_USHORT); retObject = ReadArray <UInt16>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.UInt16; break; } break; case 4: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: newTypeId = H5T.copy(H5T.H5Type.NATIVE_INT); retObject = ReadArray <Int32>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Int32; break; case H5T.Sign.UNSIGNED: newTypeId = H5T.copy(H5T.H5Type.NATIVE_UINT); retObject = ReadArray <UInt32>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.UInt32; break; } break; case 8: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: newTypeId = H5T.copy(H5T.H5Type.NATIVE_LONG); retObject = ReadArray <Int64>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Int64; break; case H5T.Sign.UNSIGNED: newTypeId = H5T.copy(H5T.H5Type.NATIVE_ULONG); retObject = ReadArray <UInt64>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.UInt64; break; } break; } break; case H5T.H5TClass.FLOAT: switch (dataSize) { case 4: newTypeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT); retObject = ReadArray <float>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Float; break; case 8: newTypeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE); retObject = ReadArray <double>(size, dataSetId, newTypeId, bandIndex, outSize); dataType = enumDataType.Double; break; } break; } } finally { H5S.close(spaceid); H5D.close(dataSetId); H5F.close(_h5FileId); } }
public AttributeValue GetAttribute(H5FileId fileId, string attributeName) { H5AttributeId attributeId = H5A.openName(fileId, attributeName); //根据属性名称得到属性Id H5DataTypeId attributeType = H5A.getType(attributeId); //得到属性数据类型 int size = H5T.getSize(attributeType); H5T.H5TClass typeClass = H5T.getClass(attributeType); H5DataSpaceId spaceId = H5A.getSpace(attributeId); long[] dims = H5S.getSimpleExtentDims(spaceId); int rank = H5S.getSimpleExtentNDims(spaceId); H5T.H5Type h5type; Type dataType = null; AttributeValue atrributeData = new AttributeValue(); atrributeData.dataValue = null; atrributeData.valueType = DataValueType.EMPTY; atrributeData.rank = rank; switch (typeClass) { case H5T.H5TClass.FLOAT: h5type = H5T.H5Type.NATIVE_FLOAT; if (rank == 1) { float[] floatDatatmp = new float[dims[0]]; H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array <float>(floatDatatmp)); atrributeData.dataValue = floatDatatmp; } else if (rank == 2) { float[,] floatDatatmp = new float[dims[0], dims[1]]; H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array <float>(floatDatatmp)); atrributeData.dataValue = floatDatatmp; } atrributeData.valueType = DataValueType.FLOAT; break; case H5T.H5TClass.INTEGER: h5type = H5T.H5Type.NATIVE_INT; // int[,] intDatatmp = null; if (rank == 1) { int[] intDatatmp = new int[dims[0]]; H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array <int>(intDatatmp)); atrributeData.dataValue = intDatatmp; } else if (rank == 2) { int[,] intDatatmp = new int[dims[0], dims[1]]; H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array <int>(intDatatmp)); atrributeData.dataValue = intDatatmp; } atrributeData.valueType = DataValueType.INT; break; case H5T.H5TClass.STRING: h5type = H5T.H5Type.C_S1; if (rank == 0) { string[] stringDatatmp = new string[1]; byte[] bytedata = new byte[255]; H5A.read(attributeId, attributeType, new H5Array <byte>(bytedata)); //H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array<string>(stringDatatmp)); stringDatatmp[0] = Encoding.Default.GetString(bytedata).Trim('\0'); atrributeData.dataValue = stringDatatmp; } else if (rank == 1) { string[] stringDatatmp = new string[dims[0]]; // string stringDatatmp = ""; // byte[] bytedata = new byte[255]; // byte[,] bytedata = new byte[2,255]; // H5DataTypeId memtype = H5T.copy(H5T.H5Type.C_S1); //H5T.setVariableSize(memtype); //H5T.setSize(attributeType, size); // H5A.read(attributeId, memtype, new H5Array<string>(stringDatatmp)); // H5A.read(attributeId, new H5DataTypeId(h5type), new H5Array<string>(stringDatatmp)); // stringDatatmp[0] = Encoding.Default.GetString(bytedata).Trim('\0'); //string test = Encoding.Default.GetString(bytedata).Trim('\0'); // atrributeData.dataValue = stringDatatmp; // VariableLengthString[] value = new VariableLengthString[1]; atrributeData.dataValue = stringDatatmp; } else if (rank == 2) { string[,] stringDatatmp = new string[dims[0], dims[1]]; //H5DataTypeId memtype = H5T.copy(H5T.H5Type.C_S1); //H5T.setVariableSize(memtype); //H5A.read(attributeId, memtype, new H5Array<string>(stringDatatmp)); atrributeData.dataValue = stringDatatmp; } atrributeData.valueType = DataValueType.STRING; break; default: h5type = H5T.H5Type.C_S1; break; } H5T.close(attributeType); H5S.close(spaceId); H5A.close(attributeId); return(atrributeData); }
} // test_vlen_dtype static void test_copy() { try { Console.Write("Testing copying datatypes"); // Make a copy of a predefined type. H5DataTypeId inttype = H5T.copy(H5T.H5Type.NATIVE_INT); int intsize = H5T.getSize(inttype); // Make a copy of that type. H5DataTypeId tcopy1 = H5T.copy(inttype); int tcopy1_size = H5T.getSize(tcopy1); // The sizes of the copies should be the same. if (intsize != tcopy1_size) { Console.WriteLine("test_copy: copy types incorrectly"); nerrors++; } // Close second type H5T.close(tcopy1); /* * Test copy a datatype from a dataset. */ // Create a new file. H5FileId fileId = H5F.create("sometypes.h5", H5F.CreateMode.ACC_TRUNC); // Create a dataset with a simple dataspace. hssize_t[] dims = { DIM0, DIM1 }; H5DataSpaceId dspace = H5S.create_simple(2, dims); H5DataSetId dset = H5D.create(fileId, "test_types", inttype, dspace); // Obtain the datatype from the dataset. H5DataTypeId dstype = H5T.copy(dset); // Check this datatype's class, size, and sign. H5T.H5TClass tclass = H5T.getClass(dstype); if (tclass != H5T.H5TClass.INTEGER) { Console.WriteLine("test_copy: copy of dataset's datatype has incorrect class"); nerrors++; } int tsize = H5T.getSize(dstype); if (tsize != intsize) { Console.WriteLine("test_copy: copy of dataset's datatype has incorrect size"); nerrors++; } H5T.Sign tsign = H5T.getSign(dstype); if (tsign != H5T.Sign.TWOS_COMPLEMENT) { Console.WriteLine("test_copy: copy of dataset's datatype has incorrect sign, {0}", tsign); nerrors++; } // Close objects. H5T.close(inttype); H5S.close(dspace); H5T.close(dstype); H5D.close(dset); 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_copy
} // test_compound_dtype static void test_vlen_dtype(H5FileId fileId) { try { Console.Write("Testing variable-length datatypes"); // Create a VL datatype of int. H5DataTypeId vltId = H5T.vlenCreate(H5T.H5Type.NATIVE_UINT); // Make certain that the correct classes can be detected H5T.H5TClass tcls = H5T.getClass(vltId); if (tcls != H5T.H5TClass.VLEN) { Console.WriteLine("Test class should have been H5T_VLEN"); nerrors++; } // Create a dataset with a simple dataspace. hssize_t[] dims = { DIM1 }; H5DataSpaceId spaceId = H5S.create_simple(1, dims); H5DataSetId dsetId = H5D.create(fileId, "Vlen Dataset", vltId, spaceId); // Change to the custom memory allocation routines for reading VL data. H5PropertyListId xferpId = H5P.create(H5P.PropertyListClass.DATASET_XFER); // Writing unsafe { hvl_t[] wdata = new hvl_t[DIM1]; /* Information to write */ hvl_t[] wdata2 = new hvl_t[DIM1]; /* Information to write */ hvl_t[] rdata = new hvl_t[DIM1]; /* Information read in */ /* Allocate and initialize VL data to write */ for (uint ii = 0; ii < DIM1; ii++) { IntPtr iPtr = new IntPtr((ii + 1) * sizeof(uint)); wdata[ii].p = H5CrtHeap.Allocate(iPtr).ToPointer(); wdata[ii].len = ii + 1; ((uint *)wdata[ii].p)[0] = ii * 10; wdata2[ii].p = (void *)0; wdata2[ii].len = 0; } /* end for */ H5D.write(dsetId, vltId, new H5Array <hvl_t>(wdata)); // Read from dataset before writing data. H5D.read(dsetId, vltId, new H5Array <hvl_t>(rdata)); // Write "nil" data to disk. H5D.write(dsetId, vltId, new H5Array <hvl_t>(wdata2)); // Read from dataset with "nil" data. H5D.read(dsetId, vltId, new H5Array <hvl_t>(rdata)); // Check data read in. // Write data to dataset. H5D.write(dsetId, vltId, new H5Array <hvl_t>(wdata)); // Close resources. H5D.close(dsetId); H5T.close(vltId); H5S.close(spaceId); // Open the dataset. dsetId = H5D.open(fileId, "Vlen Dataset"); // Get dataspace and datatype for the dataset. spaceId = H5D.getSpace(dsetId); vltId = H5D.getType(dsetId); H5AllocCallback allocCallback = new H5AllocCallback(Program.crtHeapAllocate); H5FreeCallback freeCallback = new H5FreeCallback(Program.crtHeapFree); H5P.setVlenMemManager(xferpId, allocCallback, IntPtr.Zero, freeCallback, IntPtr.Zero); // Read dataset from disk. H5D.read(dsetId, vltId, new H5DataSpaceId(H5S.H5SType.ALL), new H5DataSpaceId(H5S.H5SType.ALL), xferpId, new H5Array <hvl_t>(rdata)); // Reclaim the read VL data. H5D.vlenReclaim(vltId, spaceId, xferpId, new H5Array <hvl_t>(rdata)); } // end of unsafe // Close resources. H5D.close(dsetId); H5T.close(vltId); H5S.close(spaceId); H5P.close(xferpId); 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_vlen_dtype
static void test_classes() { try { Console.Write("Testing datatype classes"); /*------------------------------------------------------------- * Check class of some atomic types. *-----------------------------------------------------------*/ H5T.H5TClass tcls = H5T.getClass(H5T.H5Type.NATIVE_INT); if (tcls != H5T.H5TClass.INTEGER) { Console.WriteLine("Test class should have been H5T_INTEGER"); nerrors++; } tcls = H5T.getClass(H5T.H5Type.NATIVE_DOUBLE); if (tcls != H5T.H5TClass.FLOAT) { Console.WriteLine("Test class should have been H5T_FLOAT"); nerrors++; } H5DataTypeId op_type = H5T.create(H5T.CreateClass.OPAQUE, 1); tcls = H5T.getClass(op_type); if (tcls != H5T.H5TClass.OPAQUE) { Console.WriteLine("Test class should have been H5T_OPAQUE"); nerrors++; } // Create a VL datatype of char. It should be a VL, not a string class. H5DataTypeId vlcId = H5T.vlenCreate(H5T.H5Type.NATIVE_UCHAR); // Make certain that the correct classes can be detected tcls = H5T.getClass(vlcId); if (tcls != H5T.H5TClass.VLEN) { Console.WriteLine("Test class should have been H5T_VLEN"); nerrors++; } // Make certain that an incorrect class is not detected */ if (tcls == H5T.H5TClass.STRING) { Console.WriteLine("Test class should not be H5T_STRING"); nerrors++; } // Create a VL string. It should be a string, not a VL class. H5DataTypeId vlsId = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(vlsId, -1); // for H5T_VARIABLE tcls = H5T.getClass(vlsId); if (tcls != H5T.H5TClass.STRING) { Console.WriteLine("Test class should have been H5T_STRING"); nerrors++; } if (tcls == H5T.H5TClass.VLEN) { Console.WriteLine("Test class should not be H5T_VLEN"); nerrors++; } // Check that this is a variable-length string. if (!H5T.isVariableString(vlsId)) { Console.WriteLine("This type should be a variable-length string"); nerrors++; } // Close datatype H5T.close(vlcId); Console.WriteLine("\t\t\t\tPASSED"); } // end of try block catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // end of test_classes
static void test_enum_dtype(H5FileId fileId) { short i, j; string[] mname = { "RED", "GREEN", "BLUE", "YELLOW", "PINK", "PURPLE", "ORANGE", "WHITE" }; short[,] spoints2 = new short[DIM0, DIM1]; short[,] scheck2 = new short[DIM0, DIM1]; try { Console.Write("Testing enumeration datatypes"); // Create the data space */ hssize_t[] dims = { DIM0, DIM1 }; H5DataSpaceId dspace = H5S.create_simple(2, dims); // Construct enum type based on native type H5DataTypeId etype = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT); // Insert members to type. for (i = 0; i < 8; i++) { H5T.enumInsert(etype, mname[i], ref i); } // Assign a name to the enum type, close it, and open it by name. H5T.commit(fileId, "Color Type", etype); H5T.close(etype); H5DataTypeId color_type = H5T.open(fileId, "Color Type"); // Check its class H5T.H5TClass tcls = H5T.getClass(color_type); if (tcls != H5T.H5TClass.ENUM) { Console.WriteLine("test_enum: class of color_type = {0} is incorrect, should be ENUM", tcls); } // Create the dataset H5DataSetId dsetId = H5D.create(fileId, DSET_ENUM_NAME, color_type, dspace); // Construct enum type based on native type in memory. H5DataTypeId etype_m = H5T.enumCreate(H5T.H5Type.NATIVE_SHORT); // Insert members to type. for (i = 0; i < 8; i++) { H5T.enumInsert(etype_m, mname[i], ref i); } // Initialize the dataset and buffer. for (i = 0; i < DIM0; i++) { for (j = 0; j < DIM1; j++) { spoints2[i, j] = i; scheck2[i, j] = 0; } } // Write the data to the dataset. H5D.write(dsetId, etype_m, new H5Array <short>(spoints2)); // Close objects. H5D.close(dsetId); H5T.close(color_type); H5S.close(dspace); H5T.close(etype_m); // Open dataset again to check the type. dsetId = H5D.open(fileId, DSET_ENUM_NAME); // Get dataset's datatype. H5DataTypeId dstype = H5D.getType(dsetId); // Get the datatype's class and check that it is of class ENUM. H5T.H5TClass tclass = H5T.getClass(dstype); if (tclass != H5T.H5TClass.ENUM) { Console.WriteLine("Type should be an enum class"); nerrors++; } // Check name of an enum value. int memb_num = 2; string memb_name = H5T.enumNameOf(dstype, ref memb_num); if (memb_name != "BLUE") { Console.WriteLine("Member name of value 2 should be BLUE"); nerrors++; } // Check value of an enum name. int memb_value = 0; H5T.enumValueOf(dstype, memb_name, out memb_value); if (memb_value != 2) { Console.WriteLine("Member value of BLUE should be 2"); nerrors++; } // Check member's value by member number. H5T.getMemberValue(dstype, 4, out memb_value); // Read data back. H5D.read(dsetId, dstype, new H5Array <short>(scheck2)); // Close objects. H5D.close(dsetId); H5T.close(dstype); Console.WriteLine("\t\t\t\tPASSED"); } // end of try block catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // end of test_enum_dtype
} // test_attr_compound_write static void test_attr_compound_read() { try { Console.Write("Testing read attribute with compound datatype"); // Open file. H5FileId fileId = H5F.open(COMP_FNAME, H5F.OpenMode.ACC_RDWR); // Open the dataset. H5DataSetId dsetId = H5D.open(fileId, DSET1_NAME); // Verify the correct number of attributes for this dataset. H5ObjectInfo oinfo = H5O.getInfo(dsetId); if (oinfo.nAttributes != 1) { Console.WriteLine("\ntest_attr_basic_read: incorrect number of attributes: read {0} - should be {1}", oinfo.nAttributes, 1); nerrors++; } // Open first attribute for the dataset. H5AttributeId attrId = H5A.openByIndex(dsetId, ".", H5IndexType.CRT_ORDER, H5IterationOrder.INCREASING, 0); // Verify dataspace. H5DataSpaceId spaceId = H5A.getSpace(attrId); int rank = H5S.getSimpleExtentNDims(spaceId); if (rank != ATTR4_RANK) { Console.WriteLine("\ntest_attr_compound_read: incorrect rank = {0} - should be {1}", rank, ATTR4_RANK); nerrors++; } long[] dims = H5S.getSimpleExtentDims(spaceId); if (dims[0] != ATTR4_DIM1) { Console.WriteLine("\ntest_attr_compound_read: incorrect dim[0] = {0} - should be {1}", dims[0], ATTR4_DIM1); nerrors++; } if (dims[1] != ATTR4_DIM2) { Console.WriteLine("\ntest_attr_compound_read: incorrect dim[1] = {0} - should be {1}", dims[1], ATTR4_DIM2); nerrors++; } // Close dataspace. H5S.close(spaceId); // Verify datatype of the attribute. H5DataTypeId typeId = H5A.getType(attrId); H5T.H5TClass t_class = H5T.getClass(typeId); if (t_class != H5T.H5TClass.COMPOUND) { Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type."); nerrors++; } int nfields = H5T.getNMembers(typeId); if (nfields != 3) { Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type."); nerrors++; } // Check name against this list string[] memb_names = { ATTR4_FIELDNAME1, ATTR4_FIELDNAME2, ATTR4_FIELDNAME3 }; int[] memb_offsets = { 0, 1, 5 }; // list of member offsets H5DataTypeId mtypeId; // member type H5T.H5TClass memb_cls1; // member classes retrieved different ways string memb_name; // member name int memb_idx; // member index int memb_offset, idx; // member offset, loop index // how to handle int versus uint for memb_idx and idx??? // For each member, check its name, class, index, and size. for (idx = 0; idx < nfields; idx++) { // Get the type of the ith member to test other functions later. mtypeId = H5T.getMemberType(typeId, idx); // Get the name of the ith member. memb_name = H5T.getMemberName(typeId, idx); if (memb_name != memb_names[idx]) { Console.WriteLine("test_compound_dtypes: incorrect member name, {0}, for member no {1}", memb_name, idx); nerrors++; } // Get the class of the ith member and then verify the class. memb_cls1 = H5T.getMemberClass(typeId, idx); if (memb_cls1 != H5T.H5TClass.INTEGER) { Console.WriteLine("test_compound_dtypes: incorrect class, {0}, for member no {1}", memb_cls1, idx); nerrors++; } // Get member's index back using its name and verify it. memb_idx = H5T.getMemberIndex(typeId, memb_name); if (memb_idx != idx) { Console.WriteLine("test_attr_compound_read: H5T.getMemberName and/or H5T.getMemberIndex returned false values."); nerrors++; } // Get member's offset and verify it. memb_offset = H5T.getMemberOffset(typeId, idx); if (memb_offset != memb_offsets[idx]) { Console.WriteLine("test_attr_compound_read: H5T.getMemberOffset returned incorrect value - {0}, should be {1}", memb_offset, memb_offsets[idx]); nerrors++; } // Get member's size and verify it. int tsize = H5T.getSize(mtypeId); switch (idx) { case 0: if (tsize != H5T.getSize(H5T.H5Type.STD_U8LE)) { Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value."); nerrors++; } break; case 1: if (tsize != H5T.getSize(H5T.H5Type.NATIVE_INT)) { Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value."); nerrors++; } break; case 2: if (tsize != H5T.getSize(H5T.H5Type.STD_I64BE)) { Console.WriteLine("test_attr_compound_read: H5T.getSize returned incorrect value."); nerrors++; } break; default: Console.WriteLine("test_attr_compound_read: We should only have 3 members."); nerrors++; break; } // end switch // Close current member type. H5T.close(mtypeId); } // end for // Prepare the check array to verify read data. It should be the same as the attr_data4 array // in the previous test function test_attr_compound_write. attr4_struct[,] check = new attr4_struct[ATTR4_DIM1, ATTR4_DIM2]; // Initialize the dataset int ii, jj, nn; for (ii = nn = 0; ii < ATTR4_DIM1; ii++) { for (jj = 0; jj < ATTR4_DIM2; jj++) { check[ii, jj].c = 't'; check[ii, jj].i = nn++; check[ii, jj].l = (ii * 10 + jj * 100) * nn; } } // Read attribute information. attr4_struct[,] read_data4 = new attr4_struct[ATTR4_DIM1, ATTR4_DIM2]; H5A.read(attrId, typeId, new H5Array <attr4_struct>(read_data4)); // Verify values read in. for (ii = 0; ii < ATTR4_DIM1; ii++) { for (jj = 0; jj < ATTR4_DIM2; jj++) { if ((check[ii, jj].c != read_data4[ii, jj].c) || (check[ii, jj].i != read_data4[ii, jj].i) || (check[ii, jj].l != read_data4[ii, jj].l)) { Console.WriteLine("test_attr_compound_read: Incorrect read data: {0}, should be {1}", read_data4[ii, jj], check[ii, jj]); nerrors++; } } } // Close resources. H5T.close(typeId); H5A.close(attrId); H5D.close(dsetId); H5F.close(fileId); Console.WriteLine("\t\tPASSED"); } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_attr_compound_read
/// <summary> /// this still need to improve later. /// </summary> /// <param name="classType"></param> /// <param name="size"></param> /// <param name="sign"></param> /// <returns></returns> private Type getTypeof(H5T.H5TClass classType, int size, H5T.Sign sign) { if (sign == H5T.Sign.UNSIGNED) { if (classType == H5T.H5TClass.INTEGER) { if (size == 2) { return(typeof(UInt16)); } else if (size == 4) { return(typeof(UInt32)); } else if (size == 8) { return(typeof(UInt64)); } } // remain the same if (classType == H5T.H5TClass.FLOAT) { if (size == 4) { return(typeof(float)); } else if (size == 8) { return(typeof(double)); } } } else { if (classType == H5T.H5TClass.INTEGER) { if (size == 2) { return(typeof(Int16)); } else if (size == 4) { return(typeof(Int32)); } else if (size == 8) { return(typeof(Int64)); } } if (classType == H5T.H5TClass.FLOAT) { if (size == 4) { return(typeof(float)); } else if (size == 8) { return(typeof(double)); } } if (classType == H5T.H5TClass.STRING) { return(typeof(String)); } if (classType == H5T.H5TClass.ENUM) { return(typeof(Enum)); } if (classType == H5T.H5TClass.BITFIELD) { return(typeof(Boolean)); } if (classType == H5T.H5TClass.ARRAY) { return(typeof(Array)); } if (classType == H5T.H5TClass.COMPOUND) { return(typeof(Object)); } } return(null); }
/// <summary> /// 得到指定属性集合中指定属性名的属性值,未对异常进行处理 /// </summary> /// <param name="obj"></param> /// <param name="attributeName"></param> /// <returns></returns> private String getAttributeValue(H5ObjectWithAttributes obj, String attributeName) { H5AttributeId attId = null; attId = H5A.open(obj, attributeName); if (attId == null) { return(null); } H5DataTypeId typeId = null; H5DataTypeId dtId = null; H5AttributeInfo attInfo = null; H5DataSpaceId spaceId = null; object attributeVal = null; typeId = H5A.getType(attId); attInfo = H5A.getInfo(attId); dtId = H5A.getType(attId); spaceId = H5A.getSpace(attId); int dataSize = H5T.getSize(dtId); typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT); H5T.H5TClass typeClass = H5T.getClass(typeId); long[] dims = H5S.getSimpleExtentDims(spaceId); if (dims.Length == 0) { dims = new long[1]; dims[0] = 1; } switch (typeClass) { case H5T.H5TClass.STRING: long size = attInfo.dataSize; byte[] chars = readAttribute <byte>(size, attId, typeId); attributeVal = Encoding.ASCII.GetString(chars); break; case H5T.H5TClass.INTEGER: H5T.Sign sign = H5T.Sign.TWOS_COMPLEMENT; sign = H5T.getSign(typeId); switch (dataSize) { case 1: attributeVal = readAttribute <byte>(dims[0], attId, typeId); break; case 2: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: attributeVal = readAttribute <Int16>(dims[0], attId, typeId); break; case H5T.Sign.UNSIGNED: attributeVal = readAttribute <UInt16>(dims[0], attId, typeId); break; } break; case 4: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: attributeVal = readAttribute <Int32>(dims[0], attId, typeId); break; case H5T.Sign.UNSIGNED: attributeVal = readAttribute <UInt32>(dims[0], attId, typeId); break; } break; case 8: switch (sign) { case H5T.Sign.TWOS_COMPLEMENT: attributeVal = readAttribute <Int64>(dims[0], attId, typeId); break; case H5T.Sign.UNSIGNED: attributeVal = readAttribute <UInt64>(dims[0], attId, typeId); break; } break; } break; case H5T.H5TClass.FLOAT: switch (dataSize) { case 4: attributeVal = readAttribute <float>(dims[0], attId, typeId); break; case 8: attributeVal = readAttribute <double>(dims[0], attId, typeId); break; } break; } if (spaceId != null) { H5S.close(spaceId); } if (attId != null) { H5A.close(attId); } if (typeId != null) { H5T.close(typeId); } if (dtId != null) { H5T.close(dtId); } return(arrayToString(attributeVal)); }
public AttributeValue GetAttribute(string attribXml, string attributeName, H5FileId fileId) { H5AttributeId attributeId = H5A.openName(fileId, attributeName); //根据属性名称得到属性Id H5DataTypeId attributeType = H5A.getType(attributeId); //得到属性数据类型 H5T.H5TClass typeClass = H5T.getClass(attributeType); H5DataSpaceId spaceId = H5A.getSpace(attributeId); int rank = H5S.getSimpleExtentNDims(spaceId); AttributeValue atrributeData = new AttributeValue(); string[] stringDatatmp = new string[1]; stringDatatmp[0] = "NULL"; XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; settings.ValidationType = ValidationType.None; settings.ValidationEventHandler += settings_ValidationEventHandler; settings.CheckCharacters = false; StringReader xmlSr = ChangeXmlInGBCode(attribXml); XmlReader reader = XmlReader.Create(xmlSr); XmlDocument xml = new XmlDocument(); xml.Load(reader); XmlNodeList node = xml.GetElementsByTagName("hdf5:Attribute"); foreach (XmlNode child in node) { if (child.OuterXml.Contains(attributeName)) { stringDatatmp[0] = child.InnerText; } } stringDatatmp[0] = stringDatatmp[0].Replace("\r\n", ""); stringDatatmp[0] = stringDatatmp[0].Trim(); atrributeData.dataValue = stringDatatmp; switch (typeClass) { case H5T.H5TClass.FLOAT: atrributeData.valueType = DataValueType.FLOAT; break; case H5T.H5TClass.INTEGER: atrributeData.valueType = DataValueType.INT; break; case H5T.H5TClass.STRING: atrributeData.valueType = DataValueType.STRING; break; default: break; } atrributeData.rank = rank; H5T.close(attributeType); H5S.close(spaceId); H5A.close(attributeId); return(atrributeData); }