示例#1
0
        private void ReadToDataProvider()
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            if (!File.Exists(fileName))
            {
                return;
            }
            _gribFs = new FileStream(fileName, FileMode.Open);
            _gribBr = new BinaryReader(_gribFs);
            char[] fileFg    = _gribBr.ReadChars(4);
            string fileFgStr = string.Join("", fileFg);

            if (!string.Equals(fileFgStr, "GRIB"))   //01-04:GRIB的文件标识
            {
                throw new Exception("文件错误");
            }
            byte[] bytes = _gribBr.ReadBytes(3);       //05-07:读取3字节的整数
            _dataLength = MathHelper.Bytes2Int(bytes); //整个文件的数据长度,bytes[0]*16*16*16+bytes[1]*16*16+bytes[2];
            if (_dataLength == 0)
            {
                return;
            }
            Byte version = _gribBr.ReadByte();  //08:版本,为1

            _pds = new PDSClass(_gribFs, _gribBr);

            if (_pds.ThreeSection == enumThreeSection.BMS)
            {
                _thirdSec = new BMSClass(_gribFs, _gribBr);
            }
            else
            {
                _thirdSec    = new GDSClass(_gribFs, _gribBr);
                _width       = (_thirdSec as GDSClass).LatPointsNum;
                _height      = (_thirdSec as GDSClass).LonPointsNum;
                _resolutionX = (_thirdSec as GDSClass).LatResolution;
                _resolutionY = (_thirdSec as GDSClass).LonResolution;
            }
            _bds = new BDSClass(_gribFs, _gribBr, (_thirdSec as GDSClass).LonPointsNum, (_thirdSec as GDSClass).LatPointsNum);

            float[] xDatas = _bds.OriginData as float[];
            int     length = xDatas.Length;

            float[] values = new float[length];
            for (int i = 0; i < length; i++)
            {
                values[i] = GribHelper.ComputeActualValue(xDatas[i], _pds.DecimalFactor, _bds.BinaryScale, _bds.ReferenceValue);
            }
            _values        = values;
            _coordEnvelope = new CoordEnvelope(-180, 180, -90, 90);
        }
示例#2
0
        internal PDSClass(FileStream fs, BinaryReader br)
        {
            if (br == null)
            {
                return;
            }
            PDSLength = MathHelper.Bytes2Int(br.ReadBytes(3));  //01-03,Length in bytes of PDS
            if (PDSLength == 0)
            {
                return;
            }
            //04	Parameter table version number - currently 2 for international exchange
            fs.Seek(1, SeekOrigin.Current);
            //05	Originating Center
            OriginatingCenter = GribHelper.GetOringinatingCenter(br.ReadByte());
            //06	Generating process or model ID (center dependent)
            //07	Grid identification - used for fixed grid types, GDS is used for specific grid definition
            //08	Flag specifying the presence or absence of a GDS or a BMS
            fs.Seek(2, SeekOrigin.Current);
            ThreeSection = GribHelper.GetThreeSection(br.ReadByte());
            //09	Parameter and units
            object[] objs = GribHelper.GetParameterAndUnits(br.ReadByte());
            if (objs != null && objs.Length != 0)
            {
                Parameter = objs[0].ToString();
                Units     = objs[1].ToString();
            }
            //10	Level or Layer Type
            //11-12	Level or Layer values
            objs = GribHelper.GetLevelOrLayer(br.ReadByte(), MathHelper.Bytes2Int(br.ReadBytes(2)));
            //if (objs != null && objs.Length != 0)
            //{
            //    LevelType = objs[0].ToString();
            //    LevelValue = (int)objs[1];
            //}

            //13	Reference Time - Year of century
            //14	Month of year
            //15	Day of month
            //16	Hour of day
            //17	Minute of hour
            byte[]   byteVlues = br.ReadBytes(5);
            DateTime time      = GribHelper.GetDateTime(byteVlues);

            TimeString = time.ToString("yyyy-MM-dd hh:mm");
            //18	Forecast time unit
            //19	P1 - Period of time (Number of time units). 0 for analysis or initialized analysis.
            //20	P2 - Period of time (Number of time units) or time interval between successive analyses, successive initialized analyses, or forecasts, undergoing averaging or accumulation.
            //21	Time range indicator

            //22-23	Number included in average, when octet 21 (Table 5) indicates an average or accumulation; otherwise set to zero.
            //24	Number Missing from averages or accumulations.
            //25	Century of Initial (Reference) time (=20 until Jan. 1, 2001, 21 afterwards)
            //26	Identification of sub-center
            //27-28	The decimal scale factor D. A negative value is indicated by setting the high order bit (bit No. 1) in octet 27 to 1 (on).
            fs.Seek(9, SeekOrigin.Current);
            DecimalFactor = MathHelper.Bytes2Int(br.ReadBytes(2));
            //29-40	Reserved (need not be present)
            //41-nnn	Reserved for originating center use.
            fs.Seek(PDSLength - 28, SeekOrigin.Current);
        }