/// <summary> /// 读取指定数据集,未对异常进行处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datasetName"></param> /// <param name="bandN"></param> /// <param name="bandH"></param> /// <param name="bandW"></param> /// <returns></returns> public T[] ReadDataArray <T>(String datasetName, ref int bandN, ref int bandH, ref int bandW) { H5DataSetId datasetId = null; H5DataSpaceId spaceId = null; H5DataTypeId typeId = null; long[] dims = null; if (!String.IsNullOrEmpty(datasetName) && _datasetNames.Contains(datasetName)) { datasetId = H5D.open(_fileId, datasetName); spaceId = H5D.getSpace(datasetId); dims = H5S.getSimpleExtentDims(spaceId); if (dims.Length == 2) { bandN = 1; bandH = (int)dims[0]; bandW = (int)dims[1]; } else if (dims.Length == 3) { bandN = (int)dims[0]; bandH = (int)dims[1]; bandW = (int)dims[2]; } typeId = H5D.getType(datasetId); typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT); T[] dv = new T[bandN * bandH * bandW]; H5D.read <T>(datasetId, typeId, new H5Array <T>(dv)); if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (datasetId != null) { H5D.close(datasetId); } return(dv); } else { throw new Exception("未查到指定数据集!"); } }
/// <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)); }
/// <summary> /// 写入数据集,未对异常进行处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datasetName"></param> /// <param name="bandN"></param> /// <param name="bandH"></param> /// <param name="bandW"></param> public void WriteDataArray <T>(String datasetName, T[] data, int bandN, int bandH, int bandW) { long[] dims = null; H5DataSpaceId spaceId = null; H5DataTypeId typeId = null; H5DataSetId datasetId = null; if (data == null || data.Length == 0) { throw new System.IO.IOException("无效数据块!"); } if (bandN == 1) { dims = new long[2]; dims[0] = bandH; dims[1] = bandW; spaceId = H5S.create_simple(2, dims); } else { dims = new long[3]; dims[0] = bandN; dims[1] = bandH; dims[2] = bandW; spaceId = H5S.create_simple(3, dims); } /* 设置数据类型,目前只设置了byte short、int、float相关的类型 */ if (data[0] is Byte) { typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR); } else if (data[0] is Int16) { typeId = H5T.copy(H5T.H5Type.NATIVE_SHORT); } else if (data[0] is UInt16) { typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT); } else if (data[0] is Int32) { typeId = H5T.copy(H5T.H5Type.NATIVE_INT); } else if (data[0] is UInt32) { typeId = H5T.copy(H5T.H5Type.NATIVE_UINT); } else if (data[0] is Single) { typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT); } //H5T.setOrder(typeId, H5T.Order.LE); typeId = H5T.getNativeType(typeId, H5T.Direction.DEFAULT); datasetId = H5D.create(_fileId, datasetName, typeId, spaceId); H5D.write <T>(datasetId, typeId, new H5Array <T>(data)); if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (datasetId != null) { H5D.close(datasetId); } }
//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); } } }
static void test_compound_dtype(H5FileId fileId) { uint i, j, n; try { Console.Write("Testing compound datatypes"); // Allocate space for the points & check arrays s1[,] points = new s1[DIM0, DIM1]; s1[,] check = new s1[DIM0, DIM1]; // Initialize the dataset for (i = n = 0; i < DIM0; i++) { for (j = 0; j < DIM1; j++) { points[i, j].c = 't'; points[i, j].i = n++; points[i, j].l = (i * 10 + j * 100) * n; } } // Create the data space hssize_t[] dims = { DIM0, DIM1 }; H5DataSpaceId spaceId = H5S.create_simple(2, dims); // Create compound datatype for disk storage H5DataTypeId typeId = H5T.create(H5T.CreateClass.COMPOUND, 16); // Insert members H5T.insert(typeId, "c", 0, H5T.H5Type.STD_U8LE); H5T.insert(typeId, "i", 1, H5T.H5Type.STD_U32LE); H5T.insert(typeId, "l", 5, H5T.H5Type.STD_I64BE); // Create the dataset H5DataSetId dsetId = H5D.create(fileId, DSET_COMPOUND_NAME, typeId, spaceId); // Write the dataset H5D.write(dsetId, typeId, new H5Array <s1>(points)); // Close dataset and dataspace H5D.close(dsetId); H5S.close(spaceId); H5T.close(typeId); // Open dataset again to check various functions. dsetId = H5D.open(fileId, DSET_COMPOUND_NAME); // Get its type and native type. H5DataTypeId dset_typeId = H5D.getType(dsetId); H5DataTypeId native_type = H5T.getNativeType(dset_typeId, H5T.Direction.DEFAULT); // Check name against this list string[] memb_names = { "c", "i", "l" }; int[] memb_offsets = { 0, 1, 5 }; H5DataTypeId mtypeId; // member type H5T.H5TClass memb_cls1, memb_cls2; // member classes retrieved different ways string memb_name; // member name int memb_idx; // member index // Get the number of members in the type. int nmembers = H5T.getNMembers(native_type); // For each member, check its name, class, index, and size. for (int ii = 0; ii < nmembers; ii++) { // Get the type of the ith member. mtypeId = H5T.getMemberType(native_type, ii); // Get the name of the ith member. memb_name = H5T.getMemberName(native_type, ii); if (memb_name != memb_names[ii]) { Console.WriteLine("test_compound_dtypes: incorrect member name, {0}, for member no {1}", memb_name, i); nerrors++; } // Get the class of the ith member and then verify the class. memb_cls1 = H5T.getMemberClass(native_type, ii); if (memb_cls1 != H5T.H5TClass.INTEGER) { Console.WriteLine("test_compound_dtypes: incorrect class, {0}, for member no {1}", memb_cls1, ii); nerrors++; } // Get the class via type id memb_cls2 = H5T.getClass(mtypeId); if (memb_cls1 != memb_cls2) { Console.WriteLine("test_compound_dtypes: H5T.getMemberClass and H5T.getClass return different classes for the same type."); nerrors++; } // Get member's index back from its name and verify it. memb_idx = H5T.getMemberIndex(dset_typeId, memb_name); if (memb_idx != ii) { Console.WriteLine("test_compound_dtypes: H5T.getMemberName and/or H5T.getMemberIndex returned false values."); nerrors++; } // Get member's offset and verify it. int memb_offset = H5T.getMemberOffset(dset_typeId, ii); if (memb_offset != memb_offsets[ii]) { Console.WriteLine("test_compound_dtypes: Incorrect offset value {0}, should be {1}.", memb_offset, memb_offsets[ii]); nerrors++; } // Get size of the member's type and verify it. int tsize = H5T.getSize(mtypeId); switch (ii) { case 0: //Console.WriteLine("tsize = {0}, STD_U8LE = {1}", tsize, H5T.getSize(H5T.H5Type.STD_U8LE)); if (tsize != H5T.getSize(H5T.H5Type.STD_U8LE)) { Console.WriteLine("test_compound_dtypes: First member has incorrect size"); nerrors++; } break; case 1: if (tsize != H5T.getSize(H5T.H5Type.STD_U32LE)) { Console.WriteLine("test_compound_dtypes: Second member has incorrect size"); nerrors++; } break; case 2: if (tsize != H5T.getSize(H5T.H5Type.STD_I64BE)) { Console.WriteLine("test_compound_dtypes: Third member has incorrect size"); nerrors++; } break; default: Console.WriteLine("test_compound_dtypes: Only 3 members."); break; } // end switch // Close current member type. H5T.close(mtypeId); } // end for // Close objects. H5T.close(dset_typeId); H5T.close(native_type); H5D.close(dsetId); 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++; } } // test_compound_dtype