context: /*
   * This file is part of GribCS.
   * This code is based on an automatic conversion of JGRIB Beta 7 
   * (http://jgrib.sourceforge.net/) from Java to C#.
   * 
   * C# code: Copyright 2006-2010 Seaware AB, PO Box 1244, SE-131 28 
   * Nacka Strand, Sweden, info@seaware.se.
   * 
   * Java-code: Copyright 1997-2006 Unidata Program Center/University 
   * Corporation for Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
   * support@unidata.ucar.edu.
   * 
   * GribCS is free software: you can redistribute it and/or modify it under 
   * the terms of the GNU Lesser General Public License as published by the 
   * Free Software Foundation, either version 3 of the License, or (at your 
   * option) any later version.
   * 
   * GribCS is distributed in the hope that it will be useful, but 
   * WITHOUT ANY WARRANTY; without even the implied warranty of 
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
   * General Public License for more details.
   * 
   * You should have received a copy of the GNU Lesser General Public License 
   * along with GribCS.  If not, see <http://www.gnu.org/licenses/>.
  */
  using System;
  using Seaware.GribCS;
  using System.Runtime.InteropServices;
  
  namespace Seaware.GribCS.Grib1
  {
  	
  	/// <summary> A class that scans a GRIB file to extract product information. </summary>
      [GuidAttribute("0FB284A1-77DB-45f6-8F36-2E0300D7E3CF")]
      [ClassInterface(ClassInterfaceType.None)]
  	public sealed class Grib1Input : Seaware.GribCS.Grib1.IGrib1Input
  	{
  		/// <summary> Grib edition number 1, 2 or 0 not a Grib file.</summary>
  		/// <throws>  NotSupportedException </throws>
  		/// <returns> int 0 not a Grib file, 1 Grib1, 2 Grib2
  		/// </returns>
  		public int Edition
  		{
  			get
  			{
  				long length = (raf.Length < 4000L)?raf.Length:4000L;
  				if (!seekHeader(raf, length))
  				{
  					return 0; // not valid Grib file
  				}
  				//  Read Section 0 Indicator Section to get Edition number
  				Grib1IndicatorSection is_Renamed = new Grib1IndicatorSection(raf); // section 0
  				return is_Renamed.GribEdition;
  			}
  			// end getEdition
  			
  		}
  		/// <summary> Get products of the GRIB file.
  		/// 
  		/// </summary>
  		/// <returns> products 
  		/// </returns>
  		public System.Collections.ArrayList Products
  		{
  			get
  			{
  				return products;
  			}
  			
  		}
  		/// <summary> Get records of the GRIB file.
  		/// 
  		/// </summary>
  		/// <returns> records 
  		/// </returns>
  		public System.Collections.ArrayList Records
  		{
  			get
  			{
  				return records;
  			}
  			
  		}
  		/// <summary> Get GDS's of the GRIB file.
  		/// 
  		/// </summary>
  		/// <returns> gdsHM 
  		/// </returns>
  		//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'"
  		public System.Collections.Hashtable GDSs
  		{
  			get
  			{
  				return gdsHM;
  			}
  			
  		}
  
          public int ProductsCount
          {
              get
              {
                  return products.Count;
              }
          }
          public int RecordsCount
          {
              get
              {
                  return records.Count;
              }
          }
  
  		/*
  		* raf for grib file
  		*/
  		private System.IO.FileStream raf = null;
  		
  		/*
  		* the header of Grib record
  		*/
  		private System.String header = "GRIB";
  		
  		/*
  		* stores records of Grib file, records consist of objects for each section.
  		* there are 5 sections to a Grib1 record.
  		*/
  		//UPGRADE_NOTE: Final was removed from the declaration of 'records '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  		private System.Collections.ArrayList records = new System.Collections.ArrayList();
  		
  		/*
  		* stores products of Grib file, products have enough info to get the
  		* metadata about a parameter and the data. products are lightweight
  		* records. 
  		*/
  		//UPGRADE_NOTE: Final was removed from the declaration of 'products '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  		private System.Collections.ArrayList products = new System.Collections.ArrayList();
  		
  		/*
  		* stores GDS of Grib1 file, there is possibility of more than 1
  		*/
  		//UPGRADE_NOTE: Final was removed from the declaration of 'gdsHM '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  		//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'"
  		private System.Collections.Hashtable gdsHM = new System.Collections.Hashtable();
  		
  		// *** constructors *******************************************************
  		
  		/// <summary> Constructs a <tt>Grib1Input</tt> object from a raf.
  		/// 
  		/// </summary>
  		/// <param name="raf">with GRIB content
  		/// 
  		/// </param>
  		//UPGRADE_TODO: Class 'java.io.RandomAccessFile' was converted to 'System.IO.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioRandomAccessFile'"
  		public Grib1Input(System.IO.FileStream raf)
  		{
  			this.raf = raf;
  		}
  
          public Grib1Input()
          {
          }
  		
  		/// <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 = (System.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;
  			
  			//System.out.println("file position =" + raf.Position); 
  			while (raf.Position < raf.Length)
  			{
                  if (seekHeader(raf, raf.Length, out startOffset))
  				{
  					// Read Section 0 Indicator Section
  					Grib1IndicatorSection is_Renamed = new Grib1IndicatorSection(raf);
  					//System.out.println( "Grib record length=" + is.getGribLength());
  					// EOR (EndOfRecord) calculated so skipping data sections is faster
  					long EOR = raf.Position + is_Renamed.GribLength - is_Renamed.Length;
  					
  					// Read Section 1 Product Definition Section PDS
  					pds = new Grib1ProductDefinitionSection(raf);
  					if (pds.LengthErr)
  						continue;
  					
  					if (pds.gdsExists())
  					{
  						// Read Section 2 Grid Definition Section GDS
  						gds = new Grib1GridDefinitionSection(raf);
  					}
  					else
  					{
  						// GDS doesn't exist so make one
  						//System.out.println("GribRecord: No GDS included.");
  						//System.out.println("Process ID:" + pds.getProcess_Id() );
  						//System.out.println("Grid ID:" + pds.getGrid_Id() );
  						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(raf); // should be length of BMS
  
gt: 						if ((length + raf.Position) < EOR)