Example #1
0
        /// <summary> scans the Grib2 file obtaining Products or Records that contain all 
        /// needed information for data extraction later. For most purposes, 
        /// getProductsOnly should be set to true, it's lightweight of getRecords.
        /// </summary>
        /// <param gridTemplateName="getProductsOnly">
        /// </param>
        /// <param gridTemplateName="oneRecord">
        /// </param>
        /// <returns> success
        /// </returns>
        /// <throws>  GribNotSupportedException </throws>
        /// <throws>  IOException </throws>
        public bool scan(bool getProductsOnly, bool oneRecord)
        {
            if (raf == null)
            {
                throw new ApplicationException("Grib2Input.scan called without file");
            }

            long start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            Grib2IndicatorSection is_Renamed = null;
            Grib2IdentificationSection id = null;
            Grib2LocalUseSection lus = null;
            Grib2GridDefinitionSection gds = null;
            // if gribStream.getFilePointer() != 0 then called from Grib2IndexExtender
            if (raf.Position > 4)
            {
                raf.Seek(raf.Position - 4, System.IO.SeekOrigin.Begin);
                Grib2EndSection es = new Grib2EndSection(raf);
                if (!es.EndFound)
                {
                    // ending found somewhere in file
                    throw new NoValidGribException("Grib2Input.scan failed to find end of record");
                }
                //System.out.println( "Scan succeeded to find end of record");
            }
            //System.out.println("Scan file pointer =" + gribStream.getFilePointer());
            long GdsOffset = 0; // GDS offset from start of file
            bool startAtHeader = true; // otherwise skip to GDS
            bool processGDS = true;
            while (raf.Position < raf.Length)
            {
                if (startAtHeader)
                {
                    // begining of record
                    if (!seekHeader(raf, raf.Length))
                    {
                        //System.out.println( "Scan seekHeader failed" );
                        throw new NoValidGribException("Grib2Input.scan failed to find header");
                    }

                    // Read Section 0 Indicator Section
                    is_Renamed = new Grib2IndicatorSection(raf); // numberOfSection 0
                    //System.out.println( "Grib record lengthOfSection=" + is.getGribLength());

                    // Read other Sections
                    id = new Grib2IdentificationSection(raf); // Section 1
                } // end startAtHeader
                if (processGDS)
                {
                    // check for Local Use Section 2
                    lus = new Grib2LocalUseSection(raf);

                    // obtain GDS offset in the file for this record
                    GdsOffset = raf.Position;

                    // Section 3
                    gds = new Grib2GridDefinitionSection(raf, getProductsOnly);
                    //System.out.println( "GDS lengthOfSection=" + gds.getLength() );
                } // end processGDS

                // obtain PDS offset in the file for this record
                long PdsOffset = raf.Position;

                Grib2ProductDefinitionSection pds = new Grib2ProductDefinitionSection(raf); // Section 4

                Grib2DataRepresentationSection drs = null;
                Grib2BitMapSection bms = null;
                Grib2DataSection ds = null;
                drs = new Grib2DataRepresentationSection(raf); // Section 5

                bms = new Grib2BitMapSection(raf, gds); // Section 6

                //descriptorSpatial =  new Grib2DataSection( getData, gribStream, gds, drs, bms ); //Section 7
                ds = new Grib2DataSection(false, raf, gds, drs, bms); //Section 7

                // assume scan ok
                if (getProductsOnly)
                {
                    Grib2Product gp = new Grib2Product(header, is_Renamed, id, getGDSkey(gds), pds, GdsOffset, PdsOffset);
                    products.Add(gp);
                }
                else
                {
                    Grib2Record gr = new Grib2Record(header, is_Renamed, id, gds, pds, drs, bms, GdsOffset, PdsOffset, lus);
                    records.Add(gr);
                }
                if (oneRecord)
                    return true;

                // early return because ending "7777" missing
                if (raf.Position > raf.Length)
                {
                    raf.Seek(0, System.IO.SeekOrigin.Begin);
                    return true;
                }

                // EndSection processing numberOfSection 8
                int ending = GribNumbers.int4(raf);
                //System.out.println( "ending = " + ending );
                if (ending == 926365495)
                {
                    // record ending string 7777 as a number
                    startAtHeader = true;
                    processGDS = true;
                }
                else
                {
                    int section = raf.ReadByte(); // check if GDS or PDS numberOfSection, 3 or 4
                    //System.out.println( "numberOfSection = " + numberOfSection );
                    //reset back to begining of numberOfSection
                    raf.Seek(raf.Position - 5, System.IO.SeekOrigin.Begin);

                    if (section == 3)
                    {
                        // start processing at GDS
                        startAtHeader = false;
                        processGDS = true;
                    }
                    else if (section == 4)
                    {
                        // start processing at PDS
                        startAtHeader = false;
                        processGDS = false;
                    }
                    else
                    {
                        // error
                        Grib2EndSection es = new Grib2EndSection(raf);
                        if (es.EndFound)
                        {
                            // ending found somewhere in file
                            startAtHeader = true;
                            processGDS = true;
                        }
                        else
                        {
                            throw new NoValidGribException("Grib2Input.scan failed to find end of record");
                        }
                    }
                }
            }
            return true;
        }
Example #2
0
        // --Commented out by Inspection START (11/16/05 2:14 PM):
        //   public Grib2Product(){
        //   }
        // --Commented out by Inspection STOP (11/16/05 2:14 PM)
        /// <summary> Constructor.</summary>
        /// <param gridTemplateName="header">
        /// </param>
        /// <param gridTemplateName="is">
        /// </param>
        /// <param gridTemplateName="id">
        /// </param>
        /// <param gridTemplateName="gdsKey">
        /// </param>
        /// <param gridTemplateName="pds">
        /// </param>
        /// <param gridTemplateName="GdsOffset">
        /// </param>
        /// <param gridTemplateName="PdsOffset">PDS offset in Grib file
        /// </param>
        public Grib2Product(System.String header, Grib2IndicatorSection is_Renamed, Grib2IdentificationSection id, System.String gdsKey, Grib2ProductDefinitionSection pds, long GdsOffset, long PdsOffset)
        {
            this.header = header;
            this.disciplineId = is_Renamed.Discipline;
            this.id = id;
            this.referenceTime = id.ReferenceTime;
            this.gdsKey = gdsKey;
            this.pds = pds;
            this.GdsOffset = GdsOffset;
            this.PdsOffset = PdsOffset;

            // HERE NEW

            discipline = Grib2Resolver.ResolveDiscipline(this.disciplineId, this.id.Center_id);
            parameterCategory = Grib2Resolver.ResolveParameterCategory(this.disciplineId, this.ID.Center_id, this.ID.Master_table_version, this.ID.Local_table_version, this.PDS.ParameterCategory);
            parameter = Grib2Resolver.ResolveParameter(this.disciplineId, this.ID.Center_id, this.ID.Master_table_version, this.ID.Local_table_version, this.PDS.ParameterCategory, this.PDS.ParameterNumber);

            this._productIdentification = new Grib2ProductId(discipline, parameterCategory, parameter);
        }
Example #3
0
 /// <summary> Construction for Grib2Record.</summary>
 /// <param gridTemplateName="header">
 /// </param>
 /// <param gridTemplateName="is">
 /// </param>
 /// <param gridTemplateName="id">
 /// </param>
 /// <param gridTemplateName="gds">
 /// </param>
 /// <param gridTemplateName="pds">
 /// </param>
 /// <param gridTemplateName="drs">
 /// </param>
 /// <param gridTemplateName="bms">
 /// </param>
 /// <param gridTemplateName="GdsOffset">
 /// </param>
 /// <param gridTemplateName="PdsOffset">PDS offset in Grib file
 /// </param>
 public Grib2Record(System.String header, Grib2IndicatorSection is_Renamed, 
        Grib2IdentificationSection id, Grib2GridDefinitionSection gds, 
        Grib2ProductDefinitionSection pds, Grib2DataRepresentationSection drs, 
        Grib2BitMapSection bms, long GdsOffset, long PdsOffset,
        Grib2LocalUseSection lus)
 {
     this.header = header;
     this.is_Renamed = is_Renamed;
     this.id = id;
     this.gds = gds;
     this.pds = pds;
     this.drs = drs;
     //this.bms = bms;
     this.GdsOffset = GdsOffset;
     this.PdsOffset = PdsOffset;
     this.lus = lus;
 }
Example #4
0
        /// <summary> Reads the Grib data with a certain offsets in the file.
        /// 
        /// </summary>
        /// <param gridTemplateName="GdsOffset">
        /// </param>
        /// <param gridTemplateName="PdsOffset">
        /// </param>
        /// <throws>  IOException  if gribStream does not contain a valid GRIB record. </throws>
        /// <returns> float[]
        /// </returns>
        public float[] getData(long GdsOffset, long PdsOffset)
        {
            if (raf == null)
            {
                throw new ApplicationException("Grib2Input.scan called without file");
            }

            long start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            raf.Seek(GdsOffset, System.IO.SeekOrigin.Begin);

            // Need numberOfSection 3, 4, 5, 6, and 7 to read/interpet the data
            Grib2GridDefinitionSection gds = new Grib2GridDefinitionSection(raf, false); // Section 3 no checksum

            raf.Seek(PdsOffset, System.IO.SeekOrigin.Begin); // could have more than one pds for a gds
            Grib2ProductDefinitionSection pds = new Grib2ProductDefinitionSection(raf); // Section 4

            Grib2DataRepresentationSection drs = new Grib2DataRepresentationSection(raf); // Section 5

            Grib2BitMapSection bms = new Grib2BitMapSection(raf, gds); // Section 6

            Grib2DataSection ds = new Grib2DataSection(true, raf, gds, drs, bms); // Section 7
            //System.out.println("DS offset=" + descriptorSpatial.getOffset() );

            return ds.Data;
        }