예제 #1
0
파일: Grib1Input.cs 프로젝트: nmangue/NGrib
        }         // end scan

        /// <summary> scans a Grib file to gather information that could be used to
        /// create an index or dump the metadata contents.
        ///
        /// </summary>
        /// <param name="getProducts">products have enough information for data extractions
        /// </param>
        /// <param name="oneRecord">returns after processing one record in the Grib file
        /// </param>
        /// <throws>  NotSupportedException </throws>
        public IEnumerable <Grib1Record> scanRecords()
        {
            InputStream.Seek(0, SeekOrigin.Begin);

            // stores the number of times a particular GDS is used
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable  gdsCounter = new System.Collections.Hashtable();
            Grib1ProductDefinitionSection pds        = null;
            Grib1GridDefinitionSection    gds        = null;
            long startOffset = -1;


            while (InputStream.Position < InputStream.Length)
            {
                if (seekHeader(InputStream, InputStream.Length, out startOffset))
                {
                    // Read Section 0 Indicator Section
                    Grib1IndicatorSection is_Renamed = new Grib1IndicatorSection(InputStream);

                    // EOR (EndOfRecord) calculated so skipping data sections is faster
                    long EOR = InputStream.Position + is_Renamed.GribLength - is_Renamed.Length;

                    // Read Section 1 Product Definition Section PDS
                    pds = new Grib1ProductDefinitionSection(InputStream);
                    if (pds.LengthErr)
                    {
                        continue;
                    }

                    if (pds.gdsExists())
                    {
                        // Read Section 2 Grid Definition Section GDS
                        gds = new Grib1GridDefinitionSection(InputStream);
                    }
                    else
                    {
                        // GDS doesn't exist so make one



                        gds = (Grib1GridDefinitionSection) new Grib1Grid(pds);
                    }

                    // obtain BMS or BDS offset in the file for this product
                    long dataOffset;
                    if (pds.Center == 98)
                    {
                        // check for ecmwf offset by 1 bug
                        int length = (int)GribNumbers.uint3(InputStream);                         // should be length of BMS
                        if ((length + InputStream.Position) < EOR)
                        {
                            dataOffset = InputStream.Position - 3;                             // ok
                        }
                        else
                        {
                            dataOffset = InputStream.Position - 2;
                        }
                    }
                    else
                    {
                        dataOffset = InputStream.Position;
                    }

                    // position filePointer to EndOfRecord
                    InputStream.Seek(EOR, System.IO.SeekOrigin.Begin);


                    // assume scan ok
                    Grib1Record gr = new Grib1Record(header, is_Renamed, pds, gds, dataOffset, InputStream.Position, startOffset);

                    var currentPosition = InputStream.Position;
                    yield return(gr);

                    InputStream.Position = currentPosition;

                    // early return because ending "7777" missing
                    if (InputStream.Position > InputStream.Length)
                    {
                        InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                        checkGDSkeys(gds, gdsCounter);
                        throw new BadGribFormatException("Grib1Input: GRIB ending missing. Possible file corruption");
                    }
                }         // end if seekHeader
            }             // end while raf.Position < raf.Length

            checkGDSkeys(gds, gdsCounter);
        }         // end scan
예제 #2
0
파일: Grib1Input.cs 프로젝트: nmangue/NGrib
        /// <summary> scans a Grib file to gather information that could be used to
        /// create an index or dump the metadata contents.
        ///
        /// </summary>
        /// <param name="getProducts">products have enough information for data extractions
        /// </param>
        /// <param name="oneRecord">returns after processing one record in the Grib file
        /// </param>
        /// <throws>  NotSupportedException </throws>
        public void scan(bool getProducts, bool oneRecord)
        {
            long start = (DateTime.Now.Ticks - 621355968000000000) / 10000;

            // stores the number of times a particular GDS is used
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable  gdsCounter = new System.Collections.Hashtable();
            Grib1ProductDefinitionSection pds        = null;
            Grib1GridDefinitionSection    gds        = null;
            long startOffset = -1;


            while (InputStream.Position < InputStream.Length)
            {
                if (seekHeader(InputStream, InputStream.Length, out startOffset))
                {
                    // Read Section 0 Indicator Section
                    Grib1IndicatorSection is_Renamed = new Grib1IndicatorSection(InputStream);

                    // EOR (EndOfRecord) calculated so skipping data sections is faster
                    long EOR = InputStream.Position + is_Renamed.GribLength - is_Renamed.Length;

                    // Read Section 1 Product Definition Section PDS
                    pds = new Grib1ProductDefinitionSection(InputStream);
                    if (pds.LengthErr)
                    {
                        continue;
                    }

                    if (pds.gdsExists())
                    {
                        // Read Section 2 Grid Definition Section GDS
                        gds = new Grib1GridDefinitionSection(InputStream);
                    }
                    else
                    {
                        // GDS doesn't exist so make one



                        gds = (Grib1GridDefinitionSection) new Grib1Grid(pds);
                    }

                    // obtain BMS or BDS offset in the file for this product
                    long dataOffset = 0;
                    if (pds.Center == 98)
                    {
                        // check for ecmwf offset by 1 bug
                        int length = (int)GribNumbers.uint3(InputStream);                          // should be length of BMS
                        if ((length + InputStream.Position) < EOR)
                        {
                            dataOffset = InputStream.Position - 3;                             // ok
                        }
                        else
                        {
                            dataOffset = InputStream.Position - 2;
                        }
                    }
                    else
                    {
                        dataOffset = InputStream.Position;
                    }

                    // position filePointer to EndOfRecord
                    InputStream.Seek(EOR, System.IO.SeekOrigin.Begin);


                    // assume scan ok
                    if (getProducts)
                    {
                        Grib1Product gp = new Grib1Product(header, pds, getGDSkey(gds, gdsCounter), dataOffset, InputStream.Position);
                        products.Add(gp);
                    }
                    else
                    {
                        Grib1Record gr = new Grib1Record(header, is_Renamed, pds, gds, dataOffset, InputStream.Position, startOffset);
                        records.Add(gr);
                    }

                    if (oneRecord)
                    {
                        return;
                    }

                    // early return because ending "7777" missing
                    if (InputStream.Position > InputStream.Length)
                    {
                        InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                        Console.Error.WriteLine("Grib1Input: possible file corruption");
                        checkGDSkeys(gds, gdsCounter);
                        return;
                    }
                }         // end if seekHeader
            }             // end while raf.Position < raf.Length


            //   (System.currentTimeMillis()- start) + " milliseconds");
            checkGDSkeys(gds, gdsCounter);
            return;
        }         // end scan