/// <summary> Constructs a Grib1BinaryDataSection object from a gribStream.
        /// A bit map is defined.
        /// 
        /// </summary>
        /// <param gridTemplateName="gribStream">gribStream with BDS content
        /// </param>
        /// <param gridTemplateName="decimalscale">the exponent of the decimal scale
        /// </param>
        /// <param gridTemplateName="bms">bit map numberOfSection of GRIB record
        /// 
        /// </param>
        /// <throws>  NotSupportedException  if stream contains no valid GRIB file </throws>
        //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 Grib1BinaryDataSection(System.IO.Stream raf, int decimalscale, Grib1BitMapSection bms)
        {
            // octets 1-3 (numberOfSection lengthOfSection)
            length = (int)GribNumbers.uint3(raf);
            //System.out.println( "BDS lengthOfSection = " + lengthOfSection );

            // octet 4, 1st half (packing flag)
            int unusedbits = raf.ReadByte();

            // TODO Check this!!!
            if ((unusedbits & 192) != 0)
                throw new NGribCS.Helpers.GribNotSupportedException("BDS: (octet 4, 1st half) not grid point data and simple packing ");

            // octet 4, 2nd half (number of unused bits at end of this numberOfSection)
            unusedbits = unusedbits & 15;
            //System.out.println( "BDS unusedbits = " + unusedbits );

            // octets 5-6 (binary scale factor)
            int binscale = GribNumbers.int2(raf);

            // octets 7-10 (reference point = minimum value)
            float refvalue = GribNumbers.float4(raf);

            // octet 11 (number of bits per value)
            int numbits = raf.ReadByte();
            //System.out.println( "BDS numbits = " + numbits );
            if (numbits == 0)
                isConstant = true;
            //System.out.println( "BDS isConstant = " + isConstant );

            // *** read values *******************************************************

            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            float ref_Renamed = (float) (System.Math.Pow(10.0, - decimalscale) * refvalue);
            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            float scale = (float) (System.Math.Pow(10.0, - decimalscale) * System.Math.Pow(2.0, binscale));

            if (bms != null)
            {
                bool[] bitmap = bms.Bitmap;

                values = new float[bitmap.Length];
                for (int i = 0; i < bitmap.Length; i++)
                {
                    if (bitmap[i])
                    {
                        if (!isConstant)
                        {
                            values[i] = ref_Renamed + scale * bits2UInt(numbits, raf);
                        }
                        else
                        {
                            // rdg - added this to handle a constant valued parameter
                            values[i] = ref_Renamed;
                        }
                    }
                    else
                        values[i] = Grib1BinaryDataSection.UNDEFINED;
                }
            }
            else
            {
                // bms is null
                if (!isConstant)
                {
                    //System.out.println( "BDS values.size = " +
                    //(((lengthOfSection - 11) * 8 - unusedbits) /  numbits));
                    values = new float[((length - 11) * 8 - unusedbits) / numbits];

                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = ref_Renamed + scale * bits2UInt(numbits, raf);
                    }
                }
                else
                {
                    // constant valued - same min and max
                    int x = 0, y = 0;
                    raf.Seek(raf.Position - 53, System.IO.SeekOrigin.Begin); // return to start of GDS
                    length = (int)GribNumbers.uint3(raf);
                    if (length == 42)
                    {
                        // Lambert/Mercator offset
                        SupportClass.Skip(raf, 3);
                        x = GribNumbers.int2(raf);
                        y = GribNumbers.int2(raf);
                    }
                    else
                    {
                        SupportClass.Skip(raf, 7);
                        length = (int)GribNumbers.uint3(raf);
                        if (length == 32)
                        {
                            // Polar sterographic
                            SupportClass.Skip(raf, 3);
                            x = GribNumbers.int2(raf);
                            y = GribNumbers.int2(raf);
                        }
                        else
                        {
                            x = y = 1;
                            System.Console.Out.WriteLine("BDS constant value, can't determine array size");
                        }
                    }
                    values = new float[x * y];
                    for (int i = 0; i < values.Length; i++)
                        values[i] = ref_Renamed;
                }
            }
        }
Example #2
0
        /// <summary> Reads the Grib data 
        /// 
        /// </summary>
        /// <param gridTemplateName="offset"> offset into file.
        /// </param>
        /// <param gridTemplateName="DecimalScale">
        /// </param>
        /// <param gridTemplateName="bmsExists">
        /// </param>
        /// <throws>  NotSupportedException </throws>
        /// <returns> float[]
        /// </returns>
        public float[] getData(long offset, int DecimalScale, bool bmsExists)
        {
            long start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            raf.Seek(offset, System.IO.SeekOrigin.Begin);
            //System.out.println( "gribStream.Position=" + gribStream.Position );

            // Need numberOfSection 3 and 4 to read/interpet the data, numberOfSection 5
            // as a check that all data read and sections are correct

            Grib1BitMapSection bms = null;
            if (bmsExists)
            // read Bit Mapped Section 3
                bms = new Grib1BitMapSection(raf);

            // read Binary Data Section 4
            Grib1BinaryDataSection bds = new Grib1BinaryDataSection(raf, DecimalScale, bms);

            return bds.Values;
        }