Esempio n. 1
0
        private static Hdf4FileAttr[] getHdf4FileAttrs(string[] f4names)
        {
            var hdf4FileAttrs = new Hdf4FileAttr[f4names.Length];

            for (int i = 0; i < f4names.Length; i++)
            {
                hdf4FileAttrs[i] = new Hdf4FileAttr(f4names[i]);
            }
            return(hdf4FileAttrs);
        }
Esempio n. 2
0
 public bool IsSameClassHdf4(Hdf4FileAttr attr)
 {
     if (HDFEOSVersion != attr.HDFEOSVersion)
     {
         return(false);
     }
     if (GridName != attr.GridName)
     {
         return(false);
     }
     if (Projection != attr.Projection)
     {
         return(false);
     }
     if (ProjParams != attr.ProjParams)
     {
         return(false);
     }
     if (XDim != attr.XDim)
     {
         return(false);
     }
     if (YDim != attr.YDim)
     {
         return(false);
     }
     //if (XMin - attr.XMin > double.Epsilon)
     //    return false;
     //if (YMin - attr.YMin > double.Epsilon)
     //    return false;
     //if (XMax - attr.XMax > double.Epsilon)
     //    return false;
     //if (YMax - attr.YMax > double.Epsilon)
     //    return false;
     if (CellWidth - attr.CellWidth > CellWidth / 1000000)
     {
         return(false);
     }
     if (CellHeight - attr.CellHeight > CellWidth / 1000000)
     {
         return(false);
     }
     if (DataFields != null && !DataFields.IsSameDataFields(attr.DataFields))
     {
         return(false);
     }
     return(true);
 }
Esempio n. 3
0
        private static void ConvertHdf4To5BySds <T>(Hdf4FileAttrs hdf4FileAttrs, int nyf5, int nxf5, int k, int rank,
                                                    H5FileId fileId, string sdName, HDF4Helper.DataTypeDefinitions dtaDefinitions,
                                                    Action <string, int, int> messageAction)
        {
            T[,] data = new T[nyf5, nxf5];
            //foreach (Hdf4FileAttr hdf4FileAttr in hdf4FileAttrs)
            for (int i = 0; i < hdf4FileAttrs.Count; i++)
            {
                Hdf4FileAttr hdf4FileAttr = hdf4FileAttrs[i];
                if (messageAction != null)
                {
                    messageAction(String.Format("正在转换数据集 {0}", sdName), k, i);
                }
                H4SDS sd = hdf4FileAttr.H4File.Datasets[k];
                GetDataByHdf4(sd, rank, hdf4FileAttr, data);
            }

            // 保存数据
            // Describe the size of the array and create the data space for fixed
            // size dataset.
            long[] dims = new long[rank];
            dims[0] = Convert.ToInt64(nyf5);
            dims[1] = Convert.ToInt64(nxf5);
            H5DataSpaceId dspaceId = H5S.create_simple(rank, dims);

            H5T.H5Type h5Type = Utility.GetH5Type(dtaDefinitions);
            // Define datatype for the data in the file.
            H5DataTypeId dtypeId = H5T.copy(h5Type);

            // Create the data set DATASETNAME.
            H5DataSetId dsetId = H5D.create(fileId, sdName, dtypeId, dspaceId);

            // Write the one-dimensional data set array
            H5D.write(dsetId, new H5DataTypeId(h5Type), new H5Array <T>(data));//H5A

            HDFAttributeDef[] attributeDef5s = hdf4FileAttrs[0].DatasetsAttributeDefs[k];
            foreach (HDFAttributeDef attributeDef5 in attributeDef5s)
            {
                WriteHdfAttributes.WriteHdfAttribute(dsetId, attributeDef5);
            }

            // Close dataset and file.
            H5D.close(dsetId);
        }
Esempio n. 4
0
        public Hdf4FileAttr Clone()
        {
            var hdf4FileAttr = new Hdf4FileAttr
            {
                CellHeight    = CellHeight,
                CellWidth     = CellWidth,
                DataFields    = DataFields.Clone(),
                GridName      = GridName,
                H4File        = H4File,
                HDFEOSVersion = HDFEOSVersion,
                ProjParams    = ProjParams,
                Projection    = ProjParams,
                XDim          = XDim,
                YDim          = YDim,
                XMin          = XMin,
                XMax          = XMax,
                YMin          = YMin,
                YMax          = YMax
            };

            return(hdf4FileAttr);
        }
Esempio n. 5
0
        private static bool Validate(Hdf4FileAttr[] hdf4FileAttrs)
        {
            if (hdf4FileAttrs.Length < 1)
            {
                return(false);
            }
            if (hdf4FileAttrs.Length == 1)
            {
                return(true);
            }

            Hdf4FileAttr hdf4FileAttr = hdf4FileAttrs[0];

            for (int i = 1; i < hdf4FileAttrs.Length; i++)
            {
                if (!hdf4FileAttr.IsSameClassHdf4(hdf4FileAttrs[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 6
0
        private static void GetDataByHdf4 <T>(H4SDS sd, int rank, Hdf4FileAttr hdf4FileAttr, T[,] data)
        {
            int xOffset = hdf4FileAttr.XOffset;
            int yOffset = hdf4FileAttr.YOffset;

            if (rank == 2)
            {
                int nx         = hdf4FileAttr.XDim;
                int ny         = hdf4FileAttr.YDim;
                int buffersize = nx * ny;
                // int typesize = HDFDataType.GetSize(sd.Datatype);

                T[]      buffer = new T[buffersize];
                GCHandle h      = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                try
                {
                    IntPtr bufferPtr = h.AddrOfPinnedObject();
                    sd.Read(new int[] { 0, 0 }, null, sd.Dimsizes, bufferPtr);
                }
                finally
                {
                    h.Free();
                }

                // Data and input buffer initialization.
                for (int j = 0; j < ny; j++)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        int index  = j * nx + i;
                        int iIndex = i + xOffset;
                        int jIndex = j + yOffset;
                        data[jIndex, iIndex] = buffer[index];
                    }
                }
            }
        }
Esempio n. 7
0
        private static void ConvertHdf4To5BySds <T>(Hdf4FileAttrs hdf4FileAttrs, int nyf5, int nxf5, int k, int rank,
                                                    H5FileId fileId, string sdName, HDF4Helper.DataTypeDefinitions dtaDefinitions, Action <string, int, int> messageAction,
                                                    SEnvelope envelopeNew, T fillValue, float dstResolution, ISpatialReference dstSpatialReference, ref UInt16[] rows, ref UInt16[] cols, ref Size srcStepSize, ref Size outSize)
        {
            T[,] data = new T[nyf5, nxf5];
            H5DataSetId dsetId = null;

            T[,] dataNew = null;
            SDataByProject <T> dataByProject = null;

            try
            {
                for (int i = 0; i < hdf4FileAttrs.Count; i++)
                {
                    Hdf4FileAttr hdf4FileAttr = hdf4FileAttrs[i];
                    if (messageAction != null)
                    {
                        messageAction(String.Format("正在转换数据集 {0}", sdName), k, i);
                    }
                    H4SDS sd = hdf4FileAttr.H4File.Datasets[k];
                    GetDataByHdf4(sd, rank, hdf4FileAttr, data);
                }

                dataNew = data;
                if (outSize.IsEmpty)
                {
                    outSize = new Size(nxf5, nyf5);
                }
                if (dstSpatialReference != null && dstSpatialReference.IsSame(SpatialReference.GetDefault()))
                {
                    if (rows == null && cols == null)
                    {
                        PrjEnvelope     srcEnvelope = new PrjEnvelope(hdf4FileAttrs.Hdf4FileAttr.Envelope.XMin, hdf4FileAttrs.Hdf4FileAttr.Envelope.XMax, hdf4FileAttrs.Hdf4FileAttr.Envelope.YMin, hdf4FileAttrs.Hdf4FileAttr.Envelope.YMax);
                        PrjEnvelope     outenvelpoe = new PrjEnvelope(envelopeNew.XMin, envelopeNew.XMax, envelopeNew.YMin, envelopeNew.YMax);
                        FilePrjSettings prjSetting  = new FilePrjSettings();
                        prjSetting.OutEnvelope    = outenvelpoe;
                        prjSetting.OutResolutionX = dstResolution;
                        prjSetting.OutResolutionY = dstResolution;
                        outSize = prjSetting.OutSize;
                        Size  inSize         = new Size(hdf4FileAttrs.Hdf4FileAttr.XDim, hdf4FileAttrs.Hdf4FileAttr.YDim);
                        float dstResoultion  = dstResolution;
                        float srcResoultionX = (float)(hdf4FileAttrs.Hdf4FileAttr.CellWidth);
                        float srcResoultionY = (float)(hdf4FileAttrs.Hdf4FileAttr.CellHeight);
                        dataByProject = GetDataByProject <T>(dataNew, srcEnvelope, outenvelpoe, inSize, outSize, dstResoultion, srcResoultionX, srcResoultionY, fillValue, dstSpatialReference, out rows, out cols, out srcStepSize);
                    }
                    else
                    {
                        T[,] dstData  = new T[outSize.Height, outSize.Width];
                        dataByProject = DoProject <T>(dataNew, fillValue, ref outSize, ref srcStepSize, dstData, rows, cols);
                    }
                    if (dataByProject != null)
                    {
                        dataNew = dataByProject.Data;
                    }
                }

                long[] dims = new long[rank];
                dims[0] = Convert.ToInt64(outSize.Height);
                dims[1] = Convert.ToInt64(outSize.Width);
                H5DataSpaceId dspaceId = H5S.create_simple(rank, dims);
                H5T.H5Type    h5Type   = Utility.GetH5Type(dtaDefinitions);
                // Define datatype for the data in the file.
                H5DataTypeId dtypeId = H5T.copy(h5Type);

                // Create the data set DATASETNAME.
                dsetId = H5D.create(fileId, sdName, dtypeId, dspaceId);
                // Write the one-dimensional data set array
                H5D.write(dsetId, new H5DataTypeId(h5Type), new H5Array <T>(dataNew));//H5A

                HDFAttributeDef[] attributeDef5s = hdf4FileAttrs[0].DatasetsAttributeDefs[k];
                foreach (HDFAttributeDef attributeDef5 in attributeDef5s)
                {
                    WriteHdfAttributes.WriteHdfAttribute(dsetId, attributeDef5);
                }
            }
            finally
            {
                // Close dataset and file.
                if (dsetId != null)
                {
                    H5D.close(dsetId);
                }
                if (data != null)
                {
                    data = null;
                }
                if (dataNew != null)
                {
                    dataNew = null;
                }
                GC.Collect();
            }
        }