public HDFAttributeDef[] GetHDFAttributeDefs() { HDFAttribute[] globalAttrs = Hdf4FileAttr.H4File.GlobalAttrs; List <HDFAttributeDef> attributeDef5s = new List <HDFAttributeDef>(); for (int i = 0; i < globalAttrs.Length; i++) { HDFAttribute attribute4 = globalAttrs[i]; HDFAttributeDef attribute5 = new HDFAttributeDef(); attribute5.Name = attribute4.Name; attribute5.Size = attribute4.Count; attribute5.Type = Utility.GetAttrType(attribute4.DataType); if (attribute4.DataType == HDF4Helper.DataTypeDefinitions.DFNT_CHAR8) { attribute5.Value = GetNewAttr(attribute4.Value); } else { attribute5.Value = attribute4.Value; } attributeDef5s.Add(attribute5); } if (attributeDef5s.Count == 0) { return(null); } else { attributeDef5s.Sort((cur, last) => cur.Name.CompareTo(last.Name)); return(attributeDef5s.ToArray()); } }
public HDFDatasetDef(string name, HDFAttributeDefCollection attCollection, HDFAttributeDef bandnameAtt, Type valueType) { _name = name; _attCollection = attCollection; _bandnameAtt = bandnameAtt; _valueType = valueType; }
private void GetHDFAttributeDefs() { List <HDFAttributeDef[]> lstDefses = new List <HDFAttributeDef[]>(); //foreach (H4SDS dataset in H4File.Datasets) for (int i = 0; i < H4File.Datasets.Length; i++) { H4SDS dataset = H4File.Datasets[i]; HDFAttributeDef[] attributeDef5s = new HDFAttributeDef[dataset.SDAttributes.Length]; lstDefses.Add(attributeDef5s); //foreach (HDFAttribute attribute4 in dataset.SDAttributes) for (int j = 0; j < dataset.SDAttributes.Length; j++) { HDFAttribute attribute4 = dataset.SDAttributes[j]; HDFAttributeDef attribute5 = new HDFAttributeDef(); attribute5.Name = attribute4.Name; attribute5.Size = attribute4.Count; attribute5.Type = Utility.GetAttrType(attribute4.DataType); attribute5.Value = attribute4.Value; attributeDef5s[j] = attribute5; } } DatasetsAttributeDefs = lstDefses; }
public static void WriteAttribute(HDFAttributeDef hDFAttributeDef, H5DataTypeId dataTypeId, H5AttributeId attributeId) { if (hDFAttributeDef.Value == null || hDFAttributeDef.Value.ToString() == "") { return; } H5A.write <T>(attributeId, dataTypeId, new H5Array <T>(hDFAttributeDef.Value as T[])); }
//public byte[] GetNewAttr(string attr) //{ // string newAttr = attr.Replace(UpperLeftPointOld, UpperLeftPointNew) // .Replace(LowerRightPointOld, LowerRightPointNew) // .Replace(XDimOld, XDimNew) // .Replace(YDimOld, YDimNew); // //return newAttr;//.ToCharArray(); // var stringLength = newAttr.Length; // byte[] bs = Encoding.Default.GetBytes(newAttr); // return bs; //} public HDFAttributeDef[] GetHDFAttributeDefs() { HDFAttribute[] globalAttrs = Hdf4FileAttr.H4File.GlobalAttrs; HDFAttributeDef[] attributeDef5s = new HDFAttributeDef[globalAttrs.Length]; for (int i = 0; i < globalAttrs.Length; i++) { HDFAttribute attribute4 = globalAttrs[i]; HDFAttributeDef attribute5 = new HDFAttributeDef(); attribute5.Name = attribute4.Name; attribute5.Size = attribute4.Count; attribute5.Type = Utility.GetBaseType(attribute4.DataType); if (attribute4.DataType == HDF4Helper.DataTypeDefinitions.DFNT_CHAR8) { attribute5.Value = GetNewAttr(attribute4.Value); } else { attribute5.Value = attribute4.Value; } attributeDef5s[i] = attribute5; } return(attributeDef5s); }
private static void ConvertHdf4To5(Hdf4FileAttrs hdf4FileAttrs, string f5name, Action <string, int, int> messageAction, SEnvelope outenvelope, float dstResolution, ISpatialReference dstSpatialReference) { try { // Create a new file using H5F_ACC_TRUNC access, // default file creation properties, and default file // access properties. H5FileId fileId = H5F.create(f5name, H5F.CreateMode.ACC_TRUNC); int nxf5 = hdf4FileAttrs.Hdf4FileAttr.XDim; int nyf5 = hdf4FileAttrs.Hdf4FileAttr.YDim; int rank = 2; //测试读取的科学数据集及其属性 int sdscount = hdf4FileAttrs.Hdf4FileAttr.DataFields.Count; SEnvelope envelopeNew = null; UInt16[] rows = null; //正向查找表 UInt16[] cols = null; //正向查找表 Size srcStepSize = Size.Empty; Size outSize = Size.Empty; try { for (int k = 0; k < sdscount; k++) { ConvertHdf4To5BySds(hdf4FileAttrs, messageAction, k, nyf5, nxf5, rank, fileId, outenvelope, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); if (envelopeNew == null && dstSpatialReference != null && outenvelope != null) { envelopeNew = outenvelope; } } if (envelopeNew != null) { hdf4FileAttrs.RefreshPointString(envelopeNew); } } finally { rows = null; cols = null; srcStepSize = Size.Empty; GC.Collect(); } HDFAttributeDef[] attributeDef5s = hdf4FileAttrs.GetHDFAttributeDefs(); List <HDFAttributeDef> updateFileAttrs = new List <HDFAttributeDef>(); updateFileAttrs.AddRange(attributeDef5s); if (dstSpatialReference != null && dstSpatialReference.IsSame(SpatialReference.GetDefault())) { //投影信息 string projtype = "Geopraphic Latitude longtitude"; HDFAttributeDef projecttype = new HDFAttributeDef("Projection Type", typeof(string), projtype.Length, projtype); updateFileAttrs.Add(projecttype); //写四角坐标信息 HDFAttributeDef lefttopY = new HDFAttributeDef("Left-Top Latitude", typeof(float), 1, new float[] { (float)outenvelope.YMax }); updateFileAttrs.Add(lefttopY); HDFAttributeDef lefttopX = new HDFAttributeDef("Left-Top Longtitude", typeof(float), 1, new float[] { (float)outenvelope.XMin }); updateFileAttrs.Add(lefttopX); HDFAttributeDef boottomrightY = new HDFAttributeDef("Bottom-Right Latitude", typeof(float), 1, new float[] { (float)outenvelope.YMin }); updateFileAttrs.Add(boottomrightY); HDFAttributeDef boottomrightX = new HDFAttributeDef("Bottom-Right Longtitude", typeof(float), 1, new float[] { (float)outenvelope.XMax }); updateFileAttrs.Add(boottomrightX); HDFAttributeDef xResolution = new HDFAttributeDef("Longtitude Resolution", typeof(float), 1, new float[] { (float)dstResolution }); updateFileAttrs.Add(xResolution); HDFAttributeDef yResolution = new HDFAttributeDef("Latitude Resolution", typeof(float), 1, new float[] { (float)dstResolution }); updateFileAttrs.Add(yResolution); } foreach (HDFAttributeDef attributeDef5 in updateFileAttrs) { WriteHdfAttributes.WriteHdfAttribute(fileId, attributeDef5); } H5F.close(fileId); } catch (Exception ex) { throw new Exception("拼接Hdf4时出错,具体信息:" + ex.Message, ex); } }
public static void WriteHdfAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef) { H5DataSpaceId dataSpaceId = H5S.create_simple(1, new long[] { (long)hDFAttributeDef.Size }); H5DataTypeId dataTypeId = null; H5AttributeId attributeId = null; try { switch (Type.GetTypeCode(hDFAttributeDef.Type)) { case TypeCode.Byte: dataTypeId = H5T.copy(H5T.H5Type.STD_U8BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <byte> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Char: dataTypeId = H5T.copy(H5T.H5Type.STD_U8BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <char> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Double: //dataTypeId = H5T.copy(H5T.H5Type.IEEE_F64BE); dataTypeId = H5T.copy(H5T.H5Type.IEEE_F64LE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <double> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Int16: dataTypeId = H5T.copy(H5T.H5Type.STD_I16BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <Int16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Int32: dataTypeId = H5T.copy(H5T.H5Type.STD_I32BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <Int32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Int64: dataTypeId = H5T.copy(H5T.H5Type.STD_I64BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <Int64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Object: dataTypeId = H5T.copy(H5T.H5Type.STD_REF_OBJ); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <object> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.Single: dataTypeId = H5T.copy(H5T.H5Type.IEEE_F32BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <Single> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.String: dataTypeId = H5T.copy(H5T.H5Type.C_S1); dataSpaceId = H5S.create(H5S.H5SClass.SCALAR); attributeId = WriteStringAttribute(fileOrdatasetId, hDFAttributeDef, dataSpaceId, dataTypeId); break; case TypeCode.UInt16: dataTypeId = H5T.copy(H5T.H5Type.STD_U16BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <UInt16> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.UInt32: dataTypeId = H5T.copy(H5T.H5Type.STD_U32BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <UInt32> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; case TypeCode.UInt64: dataTypeId = H5T.copy(H5T.H5Type.STD_U64BE); attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); WriteTAttribute <UInt64> .WriteAttribute(hDFAttributeDef, dataTypeId, attributeId); break; } } catch (Exception ex) { int i = 9; int j = i; } finally { if (attributeId != null) { H5A.close(attributeId); } } }
private static H5AttributeId WriteStringAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef, H5DataSpaceId dataSpaceId, H5DataTypeId dataTypeId) { string attValue = Convert.ToString(hDFAttributeDef.Value); int stringLength = attValue.Length; H5T.setSize(dataTypeId, stringLength); H5AttributeId attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); byte[] bs = Encoding.Default.GetBytes(attValue); H5A.write(attributeId, dataTypeId, new H5Array <byte>(bs)); return(attributeId); }