예제 #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;
        }
예제 #2
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;
 }