Beispiel #1
0
        public Dictionary <string, string> GetDatasetAttributes(string datasetName)
        {
            StringBuilder sds_name = new StringBuilder();
            int           rank     = 0;

            int[] dimsizes = new int[MAX_DIMSIZES];
            HDF4Helper.DataTypeDefinitions datatype;
            int attCount = 0;

            try
            {
                int dsIndex = HDF4Helper.SDnametoindex(sd_id, datasetName);
                System.Diagnostics.Debug.WriteLine($"name:{datasetName} index:{dsIndex}");
                int dsObjId = HDF4Helper.SDselect(sd_id, dsIndex);
                System.Diagnostics.Debug.WriteLine($"dsObjId:{dsObjId}");
                HDF4Helper.SDgetinfo(dsObjId, sds_name, out rank, dimsizes, out datatype, out attCount);
                //int dsCount = 0;
                //HDF4Helper.SDfileinfo(id, out dsCount, out attCount);
                if (attCount == 0)
                {
                    return(null);
                }
                return(GetAttributes(dsObjId, attCount));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Beispiel #2
0
 public void Dispose()
 {
     if (sd_id > 0)
     {
         HDF4Helper.SDend(sd_id);
     }
 }
Beispiel #3
0
        private string[] GetALLDataset()
        {
            int num_datasets     = 0;
            int num_global_attrs = 0;

            HDF4Helper.SDfileinfo(sd_id, out num_datasets, out num_global_attrs);
            string[] dsNames = ReadSDinfo(sd_id, num_datasets);
            return(dsNames);
        }
Beispiel #4
0
        private string ReadAttribute(int objId, int attIndex)
        {
            HDF4Helper.DataTypeDefinitions dType = HDF4Helper.DataTypeDefinitions.DFNT_CHAR;
            int           count    = 0;
            StringBuilder attrName = new StringBuilder(256);

            HDF4Helper.SDattrinfo(objId, attIndex, attrName, out dType, out count);
            string attValue = GetAttributevalue(dType, objId, attIndex, count);

            return(attValue);
        }
Beispiel #5
0
        private void GetAttribute(int objId, int attIndex, out string attName, out string attValue)
        {
            attName  = "";
            attValue = "";
            StringBuilder attr_name = new StringBuilder(256);

            HDF4Helper.DataTypeDefinitions data_type;
            int data_count = 0;

            HDF4Helper.SDattrinfo(objId, attIndex, attr_name, out data_type, out data_count);
            attName  = attr_name.ToString();
            attValue = GetAttributevalue(data_type, objId, attIndex, data_count);
        }
Beispiel #6
0
 public string GetAttributeValue(string datasetName, string attributeName)
 {
     try
     {
         int dsIndex  = HDF4Helper.SDnametoindex(sd_id, datasetName);
         int dsObjId  = HDF4Helper.SDselect(sd_id, dsIndex);
         int attIndex = HDF4Helper.SDfindattr(dsObjId, attributeName);
         return(ReadAttribute(dsObjId, attIndex));
     }
     finally
     {
     }
 }
Beispiel #7
0
        protected void Init(Dataset ds)
        {
            this.ds = ds;
            var subDsDic = ds.GetSubDatasets();

            if (Path.GetExtension(fileName).ToLower() != ".l1b" && subDsDic.Count > 0)
            {
                isMultiDs = true;
                //初始化IHdfOperator
                {
                    int _h5FileId = H5F.open(fileName, H5F.ACC_RDONLY);

                    if (_h5FileId >= 0)
                    {
                        hdfOperator = new Hdf5Operator(fileName);
                        //HDF5

                        H5F.close(_h5FileId);
                    }
                    else if (HDF4Helper.IsHdf4(fileName))
                    {
                        //HDF4
                        hdfOperator = new Hdf4Operator(fileName);
                    }
                }
                _fileAttrs = hdfOperator?.GetAttributes();

                GetAllSubDatasetFullpaths(ds);

                TryGetSizeOfMultiDs();
            }
            else if (ds.RasterCount > 0)
            {
                double[] geoTrans = new double[6];
                ds.GetGeoTransform(geoTrans);

                Width       = ds.RasterXSize;
                Height      = ds.RasterYSize;
                ResolutionX = Convert.ToSingle(geoTrans[1]);
                ResolutionY = Math.Abs(Convert.ToSingle(geoTrans[5]));
                BandCount   = ds.RasterCount;
                DataType    = ds.GetRasterBand(1).DataType;
                DriverName  = ds.GetDriver().ShortName;
                string wkt = ds.GetProjection();
                if (!string.IsNullOrEmpty(wkt))
                {
                    SpatialRef = new SpatialReference(wkt);
                }
            }
        }
Beispiel #8
0
        public bool GetDataSizeInfos(string datasetName, out int rank, out int[] dimsizes, out HDF4Helper.DataTypeDefinitions hdf4Type, out Type dataType, out int dataTypeSize)
        {
            StringBuilder sds_name = new StringBuilder();

            rank         = 0;
            dimsizes     = new int[MAX_DIMSIZES];
            dataType     = typeof(UInt16);
            hdf4Type     = HDF4Helper.DataTypeDefinitions.DFNT_NUINT16;
            dataTypeSize = 0;

            int dsIndex  = HDF4Helper.SDnametoindex(sd_id, datasetName);
            int dsObjId  = HDF4Helper.SDselect(sd_id, dsIndex);
            int attCount = 0;

            HDF4Helper.SDgetinfo(dsObjId, sds_name, out rank, dimsizes, out hdf4Type, out attCount);
            if (rank != 0)
            {
                dataType = GetTypeFromHDF4DataType(hdf4Type, out dataTypeSize);
                return(true);
            }

            return(false);
        }
Beispiel #9
0
        public Dictionary <string, string> GetAttributes()
        {
            if (!_readFileAttrs)
            {
                _readFileAttrs = true;

                try
                {
                    int dsCount  = 0;
                    int attCount = 0;
                    HDF4Helper.SDfileinfo(sd_id, out dsCount, out attCount);
                    if (attCount == 0)
                    {
                        return(null);
                    }
                    _fileAttrs = GetAttributes(sd_id, attCount);
                }
                finally
                {
                }
            }
            return(_fileAttrs);
        }
Beispiel #10
0
 private static string[] ReadSDinfo(int sd_id, int num_datasets)
 {
     string[] dsNames = new string[num_datasets];
     for (int dsIndex = 0; dsIndex < num_datasets; dsIndex++)
     {
         int sds_id = HDF4Helper.SDselect(sd_id, dsIndex);
         try
         {
             StringBuilder sds_name = new StringBuilder(256);
             int           rank;
             int[]         dimsizes = new int[256];
             HDF4Helper.DataTypeDefinitions data_type;
             int num_attrs;
             HDF4Helper.SDgetinfo(sds_id, sds_name, out rank, dimsizes, out data_type, out num_attrs);
             dsNames[dsIndex] = sds_name.ToString();
         }
         finally
         {
             HDF4Helper.SDend(sds_id);
         }
     }
     return(dsNames);
 }
Beispiel #11
0
        private string GetAttributevalue(HDF4Helper.DataTypeDefinitions data_type, int objId, int attIndex, int data_count)
        {
            StringBuilder attr_Data = new StringBuilder(256);

            switch (data_type)
            {
            case HDF4Helper.DataTypeDefinitions.DFNT_CHAR:
            case HDF4Helper.DataTypeDefinitions.DFNT_UCHAR:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                byte[] dest = new byte[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (char value in dest)
                {
                    attr_Data.Append(value);
                }
                break;
            }

            case HDF4Helper.DataTypeDefinitions.DFNT_INT8:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                byte[] dest = new byte[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (sbyte value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
                break;
            }

            case HDF4Helper.DataTypeDefinitions.DFNT_UINT8:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                byte[] dest = new byte[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (byte value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
                break;
            }

            case HDF4Helper.DataTypeDefinitions.DFNT_INT16:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(short)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                short[] dest = new short[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (short value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_UINT16:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(short)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                short[] dest = new short[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (ushort value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_INT32:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                int[] dest = new int[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (int value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_UINT32:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                int[] dest = new int[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (uint value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_FLOAT32:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                float[] dest = new float[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (float value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_FLOAT64:
            {
                IntPtr attr_buff = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * data_count);
                HDF4Helper.SDreadattr(objId, attIndex, attr_buff);
                double[] dest = new double[data_count];
                Marshal.Copy(attr_buff, dest, 0, data_count);
                Marshal.FreeHGlobal(attr_buff);
                foreach (double value in dest)
                {
                    attr_Data.Append(value);
                    attr_Data.Append(",");
                }
            }
            break;

            case HDF4Helper.DataTypeDefinitions.DFNT_NINT8:
            case HDF4Helper.DataTypeDefinitions.DFNT_NUINT8:
            case HDF4Helper.DataTypeDefinitions.DFNT_NINT16:
            case HDF4Helper.DataTypeDefinitions.DFNT_NUINT16:
            case HDF4Helper.DataTypeDefinitions.DFNT_NINT32:
            case HDF4Helper.DataTypeDefinitions.DFNT_NUINT32:
            case HDF4Helper.DataTypeDefinitions.DFNT_NFLOAT32:
            case HDF4Helper.DataTypeDefinitions.DFNT_NFLOAT64:
                break;

            default:
                break;
            }
            string str = attr_Data.ToString();

            if (str != null)
            {
                if (str.EndsWith(","))
                {
                    str = str.Substring(0, str.Length - 1);
                }
                str = str.Replace('\0', ' ');
            }
            return(str);
        }
Beispiel #12
0
 public Hdf4Operator(string fname)
 {
     _fname        = fname;
     sd_id         = HDF4Helper.SDstart(_fname, HDF4Helper.AccessCodes.DFACC_READ);
     _datasetNames = GetALLDataset();
 }