public GRIB2DataRepresentationSection(FileStream fs)
 {
     _sectionHeader = new GRIB2SectionHeader(fs);
     _dataPoints    = GribNumberHelper.Int4(fs);
     _dataTemplate  = (int)GribNumberHelper.Uint2(fs);
     SetOtherAttributeByDataTemplateNo(fs);
 }
        private void SetOtherAttributeByDataTemplateNo(FileStream fs)
        {
            _referenceValue     = GribNumberHelper.IEEEfloat4(fs);
            _binaryScaleFactor  = GribNumberHelper.Int2(fs);
            _decimalScaleFactor = GribNumberHelper.Int2(fs);
            _numberOfBits       = fs.ReadByte();
            _originalType       = fs.ReadByte();
            switch (_dataTemplate)
            {
            case 0:        //0 - Grid point data - simple packing
            case 1:        //1 - Matrix values - simple packing
            {
                break;
            }

            case 2:
            case 3:
            {
                _splittingMethod = fs.ReadByte();
                // octet 23
                _missingValueManagement = fs.ReadByte();
                // octet 24 - 27
                _primaryMissingValue = GribNumberHelper.IEEEfloat4(fs);
                // octet 28 - 31
                _secondaryMissingValue = GribNumberHelper.IEEEfloat4(fs);
                // octet 32 - 35
                _numberOfGroups = GribNumberHelper.Int4(fs);
                // octet 36
                _referenceGroupWidths = fs.ReadByte();
                // octet 37
                _bitsGroupWidths = fs.ReadByte();
                // according to documentation subtract referenceGroupWidths
                _bitsGroupWidths = _bitsGroupWidths - _referenceGroupWidths;
                // octet 38 - 41
                _referenceGroupLength = GribNumberHelper.Int4(fs);
                // octet 42
                _lengthIncrement = fs.ReadByte();
                // octet 43 - 46
                _lengthLastGroup = GribNumberHelper.Int4(fs);
                // octet 47
                _bitsScaledGroupLength = fs.ReadByte();
                if (_dataTemplate == 3)
                {
                    // case 3 // complex packing & spatial differencing
                    _orderSpatial      = fs.ReadByte();
                    _descriptorSpatial = fs.ReadByte();
                }
                break;
            }

            case 4:
            case 4000:
            {
                _compressionMethod = fs.ReadByte();
                _compressionRatio  = fs.ReadByte();
                break;
            }
            }
        }
        public GRIB2DataRepresentationSection(FileStream fs)
        {
            long position = fs.Position;

            _sectionHeader = new GRIB2SectionHeader(fs);
            _dataPoints    = GribNumberHelper.Int4(fs);
            _dataTemplate  = (int)GribNumberHelper.Uint2(fs);
            SetOtherAttributeByDataTemplateNo(fs);
            fs.Seek(position + _sectionHeader.SectionLength, SeekOrigin.Begin);
        }
Exemplo n.º 4
0
        private float _dstart;         //Dstart ― offset from origin to inner bound


        public GRIB2GridDefinitionSection(FileStream fs)
        {
            _sectionHeader  = new GRIB2SectionHeader(fs);
            _source         = fs.ReadByte();
            _pointsNumber   = GribNumberHelper.Int4(fs);
            _olon           = fs.ReadByte();
            _iolon          = fs.ReadByte();
            _gridTemplateNo = GribNumberHelper.Int2(fs);
            _gridName       = GetGridNameByGridNo();
            SetAttributeByGridTemplateNo(fs);
        }
        private float _dstart;         //Dstart ― offset from origin to inner bound


        public GRIB2GridDefinitionSection(FileStream fs)
        {
            long position = fs.Position;

            _sectionHeader  = new GRIB2SectionHeader(fs);
            _source         = fs.ReadByte();
            _pointsNumber   = GribNumberHelper.Int4(fs);
            _olon           = fs.ReadByte();
            _iolon          = fs.ReadByte();
            _gridTemplateNo = GribNumberHelper.Int2(fs);
            _gridName       = GetGridNameByGridNo();
            SetAttributeByGridTemplateNo(fs);
            fs.Seek(position + _sectionHeader.SectionLength, SeekOrigin.Begin);
        }
Exemplo n.º 6
0
        private void ReadToDataProvider()
        {
            if (string.IsNullOrEmpty(_fileName) || !File.Exists(_fileName))
            {
                return;
            }
            long       gdsOffset     = 0;    // GDS offset from start of file
            bool       startAtHeader = true; // otherwise skip to GDS
            bool       processGDS    = true;
            FileStream _fs           = new FileStream(_fileName, FileMode.Open, FileAccess.Read);

            GRIB2IndicatorSection      iSection  = null;
            GRIB2IdentificationSection idSection = null;
            GRIB2LocalUseSection       lus       = null;
            GRIB2GridDefinitionSection gds       = null;

            while (_fs.Position < _fs.Length)
            {
                if (startAtHeader)
                {
                    // begining of record
                    if (!SeekHeader(_fs, _fs.Length))
                    {
                        goto setAttributes;
                    }
                    // Read Section 0 Indicator Section
                    iSection    = new GRIB2IndicatorSection(_fs); // section 0
                    _discipline = iSection.Displine;
                    // Read other Sections
                    idSection      = new GRIB2IdentificationSection(_fs); // Section 1
                    _referenceTime = idSection.ReferenceTime;
                } // end startAtHeader
                if (processGDS)
                {
                    // check for Local Use Section 2
                    lus = new GRIB2LocalUseSection(_fs);
                    // obtain GDS offset in the file for this record
                    gdsOffset = _fs.Position;
                    // Section 3
                    gds = new GRIB2GridDefinitionSection(_fs);
                }

                // obtain PDS offset in the file for this record
                long pdsOffset = _fs.Position;
                IGRIB2ProductDefinitionSection  pds = new GRIB2ProductDefinitionSection(_fs);  // Section 4
                IGRIB2DataRepresentationSection drs = new GRIB2DataRepresentationSection(_fs); // Section 5
                IGribBitMapSection bms      = new GRIB2BitMapSection(_fs, gds.PointsNumber);   // Section 6
                long             dataOffset = _fs.Position + 5;
                GRIB2DataSection ds         = new GRIB2DataSection(_fs, dataOffset);           //Section 7
                GRIB2Record      record     = new GRIB2Record(iSection.Displine, gds, pds, drs, bms, ds, gdsOffset, pdsOffset, dataOffset);
                _records.Add(record);
                _parameterList.Add(record.ParameterName);
                if (_fs.Position > _fs.Length)
                {
                    _fs.Seek(0, System.IO.SeekOrigin.Begin);
                    goto setAttributes;
                }
                int ending = GribNumberHelper.Int4(_fs);
                if (ending == 926365495)
                {
                    // record ending string 7777 as a number
                    startAtHeader = true;
                    processGDS    = true;
                }
                else
                {
                    //_fs.Seek(4, SeekOrigin.Current);
                    int section = _fs.ReadByte(); // check if GDS or PDS section, 3 or 4
                    //reset back to begining of section
                    _fs.Seek(_fs.Position - 5, System.IO.SeekOrigin.Begin);
                    if (section == 3)
                    {
                        startAtHeader = false;
                        processGDS    = true;
                    }
                    else if (section == 4)
                    {
                        startAtHeader = false;
                        processGDS    = false;
                    }
                    else
                    {
                        GribEndSection es = new GribEndSection(_fs);
                        if (es.IsEndFound)
                        {
                            startAtHeader = true;
                            processGDS    = true;
                        }
                        else
                        {
                            goto setAttributes;
                        }
                    }
                }
            }
setAttributes:
            SetAttribute(gds);
        }
        private void SetAttributeByGridTemplateNo(FileStream fs)
        {
            int scaleFactorRadius, scaleDvalueRadius, scaleFactorMajor, scaleDvalueMajor,
                scaleFactorMinor, scaleDvalueMinor;
            float ratio;

            if ((_gridTemplateNo >= 50 && _gridTemplateNo <= 53) || _gridTemplateNo == 100 || _gridTemplateNo == 120)
            {
                if (_gridTemplateNo >= 50 && _gridTemplateNo <= 53)
                {
                    _j      = GribNumberHelper.IEEEfloat4(fs);
                    _k      = GribNumberHelper.IEEEfloat4(fs);
                    _m      = GribNumberHelper.IEEEfloat4(fs);
                    _method = fs.ReadByte();
                    _mode   = fs.ReadByte();
                    if (_gridTemplateNo == 51)
                    {
                        _spLat         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _spLon         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                    }
                    else if (_gridTemplateNo == 52)
                    {
                        _poleLat = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _poleLon = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _factor  = GribNumberHelper.Int4(fs);
                    }
                    else if (_gridTemplateNo == 53)
                    {
                        _spLat         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _spLon         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                        _poleLat       = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _poleLon       = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _factor        = GribNumberHelper.Int4(fs);
                    }
                }
                else if (_gridTemplateNo == 100)
                {
                    _n2          = fs.ReadByte();
                    _n3          = fs.ReadByte();
                    _ni          = GribNumberHelper.Int2(fs);
                    _nd          = fs.ReadByte();
                    _poleLat     = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _poleLon     = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonofcenter = GribNumberHelper.Int4(fs);
                    _position    = fs.ReadByte();
                    _order       = fs.ReadByte();
                    _scanMode    = fs.ReadByte();
                    _nt          = GribNumberHelper.Int4(fs);
                }
                else
                {
                    _nb            = GribNumberHelper.Int4(fs);
                    _nr            = GribNumberHelper.Int4(fs);
                    _latFirstPoint = GribNumberHelper.Int4(fs);
                    _lonFirstPoint = GribNumberHelper.Int4(fs);
                    _dx            = GribNumberHelper.Int4(fs);
                    _dstart        = GribNumberHelper.IEEEfloat4(fs);
                    _scanMode      = fs.ReadByte();
                }
            }
            else
            {
                _shape            = fs.ReadByte();
                scaleFactorRadius = fs.ReadByte();
                scaleDvalueRadius = GribNumberHelper.Int4(fs);
                scaleFactorMajor  = fs.ReadByte();
                scaleDvalueMajor  = GribNumberHelper.Int4(fs);
                scaleFactorMinor  = fs.ReadByte();
                scaleDvalueMinor  = GribNumberHelper.Int4(fs);
                _nx = GribNumberHelper.Int4(fs);
                _ny = GribNumberHelper.Int4(fs);
                switch (_gridTemplateNo)
                {
                // Latitude/Longitude Grid
                case 0:
                case 1:
                case 2:
                case 3:
                {
                    _angle             = GribNumberHelper.Int4(fs);
                    _subDivisionsAngle = GribNumberHelper.Int4(fs);
                    if (_angle == 0)
                    {
                        ratio = TenToNegSix;
                    }
                    else
                    {
                        ratio = _angle / _subDivisionsAngle;
                    }
                    _latFirstPoint  = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _lonFirstPoint  = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _resolutionFlag = fs.ReadByte();
                    _latEndPoint    = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _lonEndPoint    = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _dx             = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _dy             = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _scanMode       = fs.ReadByte();
                    //  1, 2, and 3 needs checked
                    if (_gridTemplateNo == 1)
                    {
                        //Rotated Latitude/longitude
                        _spLat         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _spLon         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                    }
                    else if (_gridTemplateNo == 2)
                    {
                        //Stretched Latitude/longitude
                        _poleLat = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _poleLon = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _factor  = GribNumberHelper.Int4(fs);
                    }
                    else if (_gridTemplateNo == 3)
                    {
                        //Stretched and Rotated Latitude/longitude
                        _spLat         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _spLon         = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                        _poleLat       = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _poleLon       = GribNumberHelper.Int4(fs) * TenToNegSix;
                        _factor        = GribNumberHelper.Int4(fs);
                    }
                    break;
                }

                case 10:      // Mercator
                {
                    _latFirstPoint  = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonFirstPoint  = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _resolutionFlag = fs.ReadByte();
                    _latD           = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _latEndPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonEndPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _scanMode       = fs.ReadByte();
                    _angle          = GribNumberHelper.Int4(fs);
                    _dx             = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _dy             = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    break;
                }

                case 20:      // Polar stereographic projection
                {
                    _latFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _resolutionFlag   = fs.ReadByte();
                    _latD             = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lov              = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _dx               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _dy               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _projectionCenter = fs.ReadByte();
                    _scanMode         = fs.ReadByte();
                    break;
                }

                case 30:      // Lambert Conformal
                {
                    _latFirstPoint    = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _lonFirstPoint    = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _resolutionFlag   = fs.ReadByte();
                    _latD             = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _lov              = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _dx               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _dy               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _projectionCenter = fs.ReadByte();
                    _scanMode         = fs.ReadByte();
                    _latin1           = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _latin2           = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _spLat            = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    _spLon            = (float)(GribNumberHelper.Int4(fs) * TenToNegSix);
                    break;
                }

                case 31:      // Albers Equal Area
                {
                    _latFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _resolutionFlag   = fs.ReadByte();
                    _latD             = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lov              = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _dx               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _dy               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _projectionCenter = fs.ReadByte();
                    _scanMode         = fs.ReadByte();
                    _latin1           = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _latin2           = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _spLat            = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _spLon            = GribNumberHelper.Int4(fs) * TenToNegSix;
                    break;
                }

                case 40:
                case 41:
                case 42:
                case 43:      // Gaussian latitude/longitude
                {
                    _angle             = GribNumberHelper.Int4(fs);
                    _subDivisionsAngle = GribNumberHelper.Int4(fs);
                    if (_angle == 0)
                    {
                        ratio = TenToNegSix;
                    }
                    else
                    {
                        ratio = _angle / _subDivisionsAngle;
                    }
                    _latFirstPoint  = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _lonFirstPoint  = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _resolutionFlag = fs.ReadByte();
                    _latEndPoint    = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _lonEndPoint    = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _dx             = (float)(GribNumberHelper.Int4(fs) * ratio);
                    _paralellNumber = fs.ReadByte();
                    _scanMode       = fs.ReadByte();
                    if (_gridTemplateNo == 41)
                    {
                        _spLat         = GribNumberHelper.Int4(fs) * ratio;
                        _spLon         = GribNumberHelper.Int4(fs) * ratio;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                    }
                    else if (_gridTemplateNo == 42)
                    {
                        _poleLat = GribNumberHelper.Int4(fs) * ratio;
                        _poleLon = GribNumberHelper.Int4(fs) * ratio;
                        _factor  = GribNumberHelper.Int4(fs);
                    }
                    else if (_gridTemplateNo == 43)
                    {
                        _spLat         = GribNumberHelper.Int4(fs) * ratio;
                        _spLon         = GribNumberHelper.Int4(fs) * ratio;
                        _rotationAngle = GribNumberHelper.IEEEfloat4(fs);
                        _poleLat       = GribNumberHelper.Int4(fs) * ratio;
                        _poleLon       = GribNumberHelper.Int4(fs) * ratio;
                        _factor        = GribNumberHelper.Int4(fs);
                    }
                    break;
                }

                case 90:      // Space view perspective or orthographic
                {
                    _lap            = GribNumberHelper.Int4(fs);
                    _lop            = GribNumberHelper.Int4(fs);
                    _resolutionFlag = fs.ReadByte();
                    _dx             = GribNumberHelper.Int4(fs);
                    _dy             = GribNumberHelper.Int4(fs);
                    _xp             = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _yp             = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _scanMode       = fs.ReadByte();
                    _angle          = GribNumberHelper.Int4(fs);
                    _altitude       = GribNumberHelper.Int4(fs) * 1000000;
                    _xo             = GribNumberHelper.Int4(fs);
                    _yo             = GribNumberHelper.Int4(fs);
                    break;
                }

                case 110:      // Equatorial azimuthal equidistant projection
                {
                    _latFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _lonFirstPoint    = GribNumberHelper.Int4(fs) * TenToNegSix;
                    _resolutionFlag   = fs.ReadByte();
                    _dx               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _dy               = (float)(GribNumberHelper.Int4(fs) * TenToNegThree);
                    _projectionCenter = fs.ReadByte();
                    _scanMode         = fs.ReadByte();
                    break;
                }
                }
            }
        }
Exemplo n.º 8
0
 public GRIB2SectionHeader(FileStream fs)
 {
     _sectionLength = GribNumberHelper.Int4(fs);
     _sectionNo     = fs.ReadByte();
 }
Exemplo n.º 9
0
        private void SetOtherAttributeByProductDefinition(FileStream fs)
        {
            // octet 10
            _parameterCategory = fs.ReadByte();
            // octet 11
            _parameterNumber = fs.ReadByte();
            switch (_productDefinitionTemplateNo)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            {
                // octet 12
                _typeGenProcess = fs.ReadByte();
                // octet 13
                _backGenProcess = fs.ReadByte();
                // octet 14
                _analysisGenProcess = fs.ReadByte();
                // octet 15-16
                _hoursAfter = GribNumberHelper.Int2(fs);
                // octet 17
                _minutesAfter = fs.ReadByte();
                // octet 18
                _timeRangeUnit = fs.ReadByte();
                // octet 19-22
                _forecastTime = GribNumberHelper.Int4(fs);
                // octet 23
                _typeFirstFixedSurface = fs.ReadByte();
                // octet 24
                int scaleFirstFixedSurface = fs.ReadByte();
                // octet 25-28
                int valueFirstFixedSurface = GribNumberHelper.Int4(fs);
                _firstFixedSurfaceValue = (float)((scaleFirstFixedSurface == 0 || valueFirstFixedSurface == 0) ? valueFirstFixedSurface : System.Math.Pow(valueFirstFixedSurface, -scaleFirstFixedSurface));
                // octet 29
                _typeSecondFixedSurface = fs.ReadByte();
                // octet 30
                int scaleSecondFixedSurface = fs.ReadByte();
                // octet 31-34
                int valueSecondFixedSurface = GribNumberHelper.Int4(fs);
                _secondFixedSurfaceValue = (float)((scaleSecondFixedSurface == 0 || valueSecondFixedSurface == 0) ? valueSecondFixedSurface : System.Math.Pow(valueSecondFixedSurface, -scaleSecondFixedSurface));
                if (_productDefinitionTemplateNo == 8)
                {
                    //  35-41 bytes
                    int year   = GribNumberHelper.Int2(fs);
                    int month  = (fs.ReadByte()) - 1;
                    int day    = fs.ReadByte();
                    int hour   = fs.ReadByte();
                    int minute = fs.ReadByte();
                    int second = fs.ReadByte();
                    // 42 - 46
                    int timeRanges        = fs.ReadByte();
                    int missingDataValues = GribNumberHelper.Int4(fs);
                    // 47 - 48
                    int outmostTimeRange = fs.ReadByte();
                    int missing          = fs.ReadByte();
                    // 49 - 53
                    int statisticalProcess = fs.ReadByte();
                    int timeIncrement      = GribNumberHelper.Int4(fs);
                    // 54 - 58
                    int indicatorTR = fs.ReadByte();
                    int lengthTR    = GribNumberHelper.Int4(fs);
                }
                break;
            }

            case 20:
            {
                _typeGenProcess = fs.ReadByte();
                // octet 13
                _backGenProcess = fs.ReadByte();
                // octet 14
                _analysisGenProcess = fs.ReadByte();
                // octet 15-16
                _hoursAfter = GribNumberHelper.Int2(fs);
                // octet 17
                _minutesAfter = fs.ReadByte();
                // octet 18
                _timeRangeUnit = fs.ReadByte();
                // octet 19-22
                _forecastTime = GribNumberHelper.Int4(fs);
                break;
            }

            case 30:
            {
                // octet 12
                _typeGenProcess = fs.ReadByte();
                // octet 13
                _backGenProcess = fs.ReadByte();
                // octet 14
                _nb = fs.ReadByte();
                fs.Seek(10 * _nb, SeekOrigin.Current);
                break;
            }

            case 254:
            {
                break;
            }
            }
        }