Beispiel #1
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;
            //}
        }
Beispiel #2
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);
                }
            }
        }