Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            var fname = "hdf4.hdf";

            var hdf = new H4File(null, null, null, new long[] { 0, 0 });

            hdf.Load(fname);
            H4SDS[] sds = hdf.Datasets;

            //测试读取的科学数据集及其属性
            for (int i = 0; i < hdf.Num_Datasets; i++)
            {
                H4SDS          sd    = hdf.Datasets[i];
                HDFAttribute[] attrs = sd.SDAttributes;
                if (sd.Rank == 2)
                {
                    int    buffersize = (int)sd.Dimsizes[0] * sd.Dimsizes[1];
                    int    typesize   = HDFDataType.GetSize(sd.Datatype);
                    IntPtr ptr        = Marshal.AllocHGlobal(buffersize * typesize);
                    sd.Read(new int[] { 0, 0 }, null, sd.Dimsizes, ptr);
                    short[] buffer = new short[buffersize];
                    Marshal.Copy(ptr, buffer, 0, buffersize);
                    Marshal.FreeHGlobal(ptr);
                }
            }

            //byte[] head1024 = GetHeader1024Bytes(fname);
            //object args = null;

            //var hdf4Driver = new HDF4Driver();
            //hdf4Driver.Open(fname, head1024, enumDataProviderAccess.ReadOnly, null);
            //var prd = new Hdf4RasterDataProvider(fname, head1024, hdf4Driver, args);

            //List<IRasterBand> _rasterBands = new List<IRasterBand>();
            //for (int i = 0; i < sds.Length; i++)
            //{
            //    CloudSatRasterBand rasterBand = new CloudSatRasterBand(prd, sds[i], i + 1);
            //    _rasterBands.Add(rasterBand);
            //    break;
            //}
        }
Ejemplo n.º 4
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];
                    }
                }
            }
        }
Ejemplo n.º 5
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();
            }
        }
Ejemplo n.º 6
0
        private void ConvertHdf4To5(string f4name, string f5name, Action <string> messageAction)
        {
            var hdf = new H4File(null, null, null, new long[] { 0, 0 });

            hdf.Load(f4name);
            H4SDS[] sds = hdf.Datasets;

            // 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);

            //测试读取的科学数据集及其属性
            //for (int k = 0; k < hdf.Num_Datasets; k++)
            for (int k = 0; k < 1; k++)
            {
                H4SDS          sd     = hdf.Datasets[k];
                HDFAttribute[] attrs  = sd.SDAttributes;
                string         sdName = sd.Name;
                int            rank   = sd.Rank;

                if (messageAction != null)
                {
                    messageAction(string.Format("正在转换数据集 {0}", sdName));
                }

                if (rank == 2)
                {
                    int nx = sd.Dimsizes[0];
                    int ny = sd.Dimsizes[1];

                    int    buffersize = nx * ny;
                    int    typesize   = HDFDataType.GetSize(sd.Datatype);
                    IntPtr ptr        = Marshal.AllocHGlobal(buffersize * typesize);
                    sd.Read(new int[] { 0, 0 }, null, sd.Dimsizes, ptr);
                    short[] buffer = new short[buffersize];
                    Marshal.Copy(ptr, buffer, 0, buffersize);
                    Marshal.FreeHGlobal(ptr);

                    // Data and input buffer initialization.
                    int[,] data = new int[nx, ny];
                    for (int i = 0; i < nx; i++)
                    {
                        for (int j = 0; j < ny; j++)
                        {
                            int index = i * ny + j;
                            data[i, j] = buffer[index];
                        }
                    }

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

                    // Define datatype for the data in the file.
                    H5DataTypeId dtypeId = H5T.copy(H5T.H5Type.NATIVE_INT);

                    // 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(H5T.H5Type.NATIVE_INT), new H5Array <int>(data));

                    // Close dataset and file.
                    H5D.close(dsetId);
                    H5F.close(fileId);
                }
            }
        }