示例#1
0
        private int Xlength; // lengthOfSection of the x axis

        #endregion Fields

        #region Constructors

        // *** constructors *******************************************************
        /// <summary> Constructor for a Grib2 Data Section.</summary>
        /// <param gridTemplateName="getData">
        /// </param>
        /// <param gridTemplateName="gribStream">
        /// </param>
        /// <param gridTemplateName="gds">
        /// </param>
        /// <param gridTemplateName="drs">
        /// </param>
        /// <param gridTemplateName="bms">
        /// </param>
        /// <throws>  IOException </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 Grib2DataSection(bool getData, System.IO.Stream raf, Grib2GridDefinitionSection gds, Grib2DataRepresentationSection drs, Grib2BitMapSection bms)
        {
            //System.out.println( "gribStream.FilePointer=" + gribStream.FilePointer() );
            // octets 1-4 (Length of DS)
            length = GribNumbers.int4(raf);
            //System.out.println( "DS lengthOfSection=" + lengthOfSection );
            //System.out.println( "DS calculated end=" + ( gribStream.getFilePointer() + lengthOfSection -4 ));
            // octet 5  numberOfSection 7
            section = raf.ReadByte();
            //System.out.println( "DS is 7, numberOfSection=" + numberOfSection );
            if (!getData)
            {
                // skip data read
                //System.out.println( "gribStream.position before reposition="+gribStream.getFilePointer());
                //System.out.println( "gribStream.lengthOfSection=" + gribStream.lengthOfSection() );
                // sanity check for erronous descriptorSpatial lengthOfSection
                if (length > 0 && length < raf.Length)
                {
                    SupportClass.Skip(raf, length - 5);
                    //System.out.println( "gribStream.skipBytes = " + (lengthOfSection -5) );
                }
                else
                {
                    length = 5; // only read lengthOfSection and numberOfSection
                }
                //System.out.println( "gribStream.position after skip=" + gribStream.getFilePointer() );
                return ;
            }
            int dtn = drs.DataTemplateNumber;
            //System.out.println( "DS dtn=" + dtn );
            if (dtn == 0 || dtn == 1)
            {
                // 0: Grid point data - simple packing
                // 1: Matrix values - simple packing
                simpleUnpacking(raf, gds, drs, bms);
            }
            else if (dtn == 2)
            {
                // 2:Grid point data - complex packing
                complexUnpacking(raf, gds, drs, bms);
            }
            else if (dtn == 3)
            {
                // 3: complex packing with spatial differencing
                complexUnpackingWithSpatial(raf, gds, drs, bms);
            }
            else if (dtn == 40 || dtn == 40000)
            {
                // JPEG 2000 Stream Format
                jpeg2000Unpacking(raf, gds, drs, bms);
            }
        }
示例#2
0
        /// <summary> Jpeg2000 unpacking method for Grib2 data.</summary>
        /// <param gridTemplateName="gribStream">
        /// </param>
        /// <param gridTemplateName="gds">
        /// </param>
        /// <param gridTemplateName="drs">
        /// </param>
        /// <param gridTemplateName="bms">bit-map numberOfSection object
        /// </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'"
        private void jpeg2000Unpacking(System.IO.Stream raf, Grib2GridDefinitionSection gds, Grib2DataRepresentationSection drs, Grib2BitMapSection bms)
        {
            if (bms.BitMapIndicator != 255 && bms.BitMapIndicator != 0)
                throw new NotImplementedException("jpeg2000 unpacking with a bitmap indicator other than 0 or 255 is currently not supported");
            // 6-xx  jpeg2000 data block to decode

            // dataPoints are number of points encoded, it could be less than the
            // numberOfDataPoints in the grid record if bitMap is used, otherwise equal
            int dataPoints = drs.DataPoints;
            //System.out.println( "DS DRS dataPoints=" + drs.getDataPoints() );
            //System.out.println( "DS lengthOfSection=" + lengthOfSection );

            float pmv = drs.PrimaryMissingValue;
            //System.out.println( "DS pmv=" + pmv );
            int nb = drs.NumberOfBits;
            //System.out.println( "DS numberOfBits = " + numberOfBits );

            int D = drs.DecimalScaleFactor;
            //System.out.println( "DS D=" + D );
            //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 DD = (float) System.Math.Pow((double) 10, (double) D);
            //System.out.println( "DS DD=" + DD );

            float R = drs.ReferenceValue;
            //System.out.println( "DS R=" + R );

            int E = drs.BinaryScaleFactor;
            //System.out.println( "DS E=" + E );
            //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 EE = (float) System.Math.Pow((double) 2.0, (double) E);
            //System.out.println( "DS EE=" + EE );

            /*Grib2JpegDecoder g2j = null;
            if (numberOfBits != 0)
            {
                // there's data to decode
                System.String[] argv = new System.String[4];
                argv[0] = "-rate";
                argv[1] = System.Convert.ToString(numberOfBits);
                argv[2] = "-verbose";
                argv[3] = "off";
                //argv[ 2 ] = "-nocolorspace" ;
                //argv[ 3 ] = "-Rno_roi" ;
                //argv[ 4 ] = "-cdstr_info" ;
                //argv[ 5 ] = "-verbose" ;
                //argv[ 6 ] = "-debug" ;
                g2j = new Grib2JpegDecoder(argv);
                g2j.decode(gribStream, lengthOfSection - 5);
            }
             */

            Jpeg2000Decoder decoder = new Jpeg2000Decoder();
            byte[] buf = new byte[length-5];
            // TODO Check cast
            int res = raf.Read(buf, 0, length-5);
            int[] values = null;
            if( res == length-5 )
            {
                values = decoder.Decode(buf, dataPoints);
            }
            else
            {
                // TODO Error handling
            }

            int numberPoints = gds.NumberOfDataPoints;
            //System.out.println( "DS GDS NumberOfDataPoints=" +  gds.getNumberPoints() );
            data = new float[numberPoints];
            bool[] bitmap = bms.Bitmap;

            if (bitmap == null)
            {
                if (nb == 0)
                {
                    // no data decoded, set to primaryMissingValue
                    for (int i = 0; i < numberPoints; i++)
                    {
                        data[i] = pmv;
                    }
                }
                else
                {
                    //System.out.println( "DS jpeg data lengthOfSection ="+ g2j.data.lengthOfSection );
                    // record has missing bitmap
                    if (values.Length != numberPoints)
                    {
                        data = null;
                        return ;
                    }
                    for (int i = 0; i < numberPoints; i++)
                    {
                        //Y = (R + ( 0 + X2) * EE)/DD ;
                        data[i] = (R + values[i] * EE) / DD;
                        //System.out.println( "DS data[ " + pInvItem +"  ]=" + data[ pInvItem ] );
                    }
                }
            }
            else
            {
                for (int i = 0, j = 0; i < bitmap.Length; i++)
                {
                    if (bitmap[i])
                    {
                        data[i] = (R + values[j++] * EE) / DD;
                    }
                    else
                    {
                        data[i] = pmv;
                    }
                }
            }
            scanMode = gds.ScanMode;
            scanningModeCheck();
        }
示例#3
0
        /// <summary> complex unpacking method for spatial data.</summary>
        /// <param gridTemplateName="gribStream">
        /// </param>
        /// <param gridTemplateName="gds">
        /// </param>
        /// <param gridTemplateName="drs">
        /// </param>
        /// <throws>  IOException </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'"
        private void complexUnpackingWithSpatial(System.IO.Stream raf, Grib2GridDefinitionSection gds, Grib2DataRepresentationSection drs, Grib2BitMapSection bms)
        {
            if (bms.BitMapIndicator != 255)
                throw new NotImplementedException("Complex unpacking with spatial and a bitmap is currently not supported");

             /*
             * 0 - No explicit missing values included within the data values
             * 1 - Primary missing values included within the data values
             * 2 - Primary and secondary missing values included within the data values
             */
            int missingValManagement = drs.MissingValueManagement;

            float primaryMissingValue = drs.PrimaryMissingValue;

            int numGroups = drs.NumberOfGroups;
            //System.out.println( "DS numGroups=" + numGroups );

            int g1 = 0, gMin = 0, h1 = 0, h2 = 0, hMin = 0;
            // [6-ww]   1st values of undifferenced scaled values and minimums

            // 1 - First-Order Spatial Differencing
            // 2 - Second-Order Spatial Differencing
            int orderSpatial = drs.OrderSpatial;

            // Number of octets required in the data numberOfSection to specify extra descriptors needed for spatial differencing
            int descriptorSpatial = drs.DescriptorSpatial;
            //System.out.println( "DS orderSpatial=" + orderSpatial +" descriptorSpatial =" + descriptorSpatial );
            bitPos = 0;
            bitBuf = 0;
            int sign;
            // descriptorSpatial is number of bytes, convert to bits -1 for sign bit
            descriptorSpatial = descriptorSpatial * 8 - 1;
            if (orderSpatial == 1)
            {
                // first order spatial differencing g1 and gMin
                sign = bits2UInt(1, raf);
                g1 = bits2UInt(descriptorSpatial, raf);
                if (sign == 1)
                {
                    g1 *= (- 1);
                }
                sign = bits2UInt(1, raf);
                gMin = bits2UInt(descriptorSpatial, raf);
                if (sign == 1)
                {
                    gMin *= (- 1);
                }
            }
            else if (orderSpatial == 2)
            {
                //second order spatial differencing h1, h2, hMin
                sign = bits2UInt(1, raf);
                h1 = bits2UInt(descriptorSpatial, raf);
                if (sign == 1)
                {
                    h1 *= (- 1);
                }
                sign = bits2UInt(1, raf);
                h2 = bits2UInt(descriptorSpatial, raf);
                if (sign == 1)
                {
                    h2 *= (- 1);
                }
                sign = bits2UInt(1, raf);
                hMin = bits2UInt(descriptorSpatial, raf);
                if (sign == 1)
                {
                    hMin *= (- 1);
                }
                //System.out.println( "DS descriptorSpatial ="+ descriptorSpatial +" h1=" + h1 +" h2 =" + h2 + " hMin=" + hMin );
            }
            else
            {
                System.Console.Out.WriteLine("DS error os=" + orderSpatial + " ds =" + descriptorSpatial);
                return ;
            }

            // [ww +1]-xx  Get reference values for groups (X1's)
            int[] X1 = new int[numGroups];
            int numberOfBits = drs.NumberOfBits;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < numGroups; i++)
            {
                X1[i] = bits2UInt(numberOfBits, raf);
                //System.out.println( "DS X1[ pInvItem ]=" + X1[ pInvItem ] );
            }

            // [xx +1 ]-yy Get number of bits used to encode each group
            int[] numBitsEncodingEachGroup = new int[numGroups];
            numberOfBits = drs.BitsGroupWidths;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < numGroups; i++)
            {
                numBitsEncodingEachGroup[i] = bits2UInt(numberOfBits, raf);
            }

            // [yy +1 ]-zz Get the scaled group lengths using formula
            //     Ln = ref + Kn * lengthIncrement, where n = 1-numGroups,
            //          ref = referenceGroupLength, and  lengthIncrement = lengthIncrement

            int[] L = new int[numGroups];
            int countL = 0;
            int referenceGroupLength = drs.ReferenceGroupLength;
            //System.out.println( "DS ref=" + ref );
            int lengthIncrement = drs.LengthIncrement;
            //System.out.println( "DS lengthIncrement=" + lengthIncrement );
            numberOfBits = drs.BitsScaledGroupLength;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < numGroups; i++)
            {
                // numGroups
                L[i] = referenceGroupLength + (bits2UInt(numberOfBits, raf) * lengthIncrement);
                //System.out.println( "DS L[ pInvItem ]=" + L[ pInvItem ] );
                countL += L[i];
            }
            //System.out.println( "DS countL=" + countL );

            // [zz +1 ]-nn get X2 values and add X1[ pInvItem ] + X2

            countL += drs.LengthLastGroup;

                data = new float[countL];

            //System.out.println( "DS countL=" + countL + " dataPoints=" +
            //gds.getNumberPoints() );
            // used to check missing values when X2 is packed with all 1's
            int[] bitsmv1 = new int[31];
            //int bitsmv2[] = new int[ 31 ]; didn't code cuz number larger the # of bits
            for (int i = 0; i < 31; i++)
            {
                //bitsmv1[ pInvItem ] = ( bitsmv1[ pInvItem -1 ] +1 ) *2 -1;
                //bitsmv2[ pInvItem ] = ( bitsmv2[ pInvItem -1 ] +2 ) *2 -2;
                bitsmv1[i] = (int) System.Math.Pow((double) 2, (double) i) - 1;
                //bitsmv2[ pInvItem ] = (int) java.lang.Math.pow( (double)2, (double)pInvItem +1) -2;
                //System.out.println( "DS bitsmv1[ "+ pInvItem +" ] =" + bitsmv1[ pInvItem ] );
                //System.out.println( "DS bitsmv2[ "+ pInvItem +" ] =" + bitsmv2[ pInvItem ] );
            }
            count = 0;
            Xlength = gds.Nx; // needs some smarts for different type Grids
            int X2;
            bitPos = 0;
            bitBuf = 0;

            for (int i = 0; i < numGroups - 1; i++)
            {
                for (int j = 0; j < L[i]; j++)
                {

                    if (numBitsEncodingEachGroup[i] == 0)
                    {
                        if (missingValManagement == 0)
                        {
                            // X2 = 0
                            data[count++] = X1[i];
                        }
                        else if (missingValManagement == 1)
                        {
                            data[count++] = primaryMissingValue;
                        }
                    }
                    else
                    {
                        X2 = bits2UInt(numBitsEncodingEachGroup[i], raf);

                        if (missingValManagement == 0)
                        {
                            data[count++] = X1[i] + X2;
                        }
                        else if (missingValManagement == 1)
                        {
                            // X2 is also set to missing value is all bits set to 1's
                            if (X2 == bitsmv1[numBitsEncodingEachGroup[i]])
                            {
                                data[count++] = primaryMissingValue;
                            }
                            else
                            {
                                data[count++] = X1[i] + X2;
                            }
                        }
                        //if( count > 1235 && count < 1275 ) {
                        //   System.out.println( "DS count=" + count );
                        //   System.out.println( "DS numBitsEncodingEachGroup[ "+ pInvItem +" ]=" + numBitsEncodingEachGroup[ pInvItem ] );
                        //   System.out.println( "DS X1[ "+ pInvItem +" ]=" + X1[ pInvItem ] );
                        //   System.out.println( "DS X2 =" +X2 );
                        //   System.out.println( "DS X1[ pInvItem ] + X2 ="+(X1[ pInvItem ]+X2) );
                        //}
                    }
                } // end for j
            } // end for pInvItem
            // process last group
            int last = drs.LengthLastGroup;
            //System.out.println( "DS last=" + last );
            for (int j = 0; j < last; j++)
            {

                // last group
                if (numBitsEncodingEachGroup[numGroups - 1] == 0)
                {
                    if (missingValManagement == 0)
                    {
                        // X2 = 0

                        data[count++] = X1[numGroups - 1];
                    }
                    else if (missingValManagement == 1)
                    {
                        data[count++] = primaryMissingValue;
                    }
                }
                else
                {
                    X2 = bits2UInt(numBitsEncodingEachGroup[numGroups - 1], raf);
                    if (missingValManagement == 0)
                    {
                        data[count++] = X1[numGroups - 1] + X2;
                    }
                    else if (missingValManagement == 1)
                    {
                        // X2 is also set to missing value is all bits set to 1's
                        if (X2 == bitsmv1[numBitsEncodingEachGroup[numGroups - 1]])
                        {
                            data[count++] = primaryMissingValue;
                        }
                        else
                        {
                            data[count++] = X1[numGroups - 1] + X2;
                        }
                    }
                }
            } // end for j

            //System.out.println( "DS mvm =" + mvm );
            if (orderSpatial == 1)
            {
                // g1 and gMin this coding is a sort of guess, no doc
                float sum = 0;
                if (missingValManagement == 0)
                {
                    // no missing values
                    for (int i = 1; i < data.Length; i++)
                    {
                        data[i] += gMin; // add minimum back
                    }
                    data[0] = g1;
                    for (int i = 1; i < data.Length; i++)
                    {
                        sum += data[i];
                        data[i] = data[i - 1] + sum;
                    }
                }
                else
                {
                    // contains missing values
                    float lastOne = primaryMissingValue;
                    // add the minimum back and set g1
                    int idx = 0;
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i] != primaryMissingValue)
                        {
                            if (idx == 0)
                            {
                                // set g1
                                data[i] = g1;
                                lastOne = data[i];
                                idx = i + 1;
                            }
                            else
                            {
                                data[i] += gMin;
                            }
                        }
                    }
                    //System.out.println( "DS data[ 0 ] ="+ data[ 0 ] );
                    if (lastOne == primaryMissingValue)
                    {
                        System.Console.Out.WriteLine("DS bad spatial differencing data");
                        return ;
                    }
                    for (int i = idx; i < data.Length; i++)
                    {
                        if (data[i] != primaryMissingValue)
                        {
                            //System.out.println( "DS pInvItem=" + pInvItem + " sum =" + sum );
                            sum += data[i];
                            data[i] = lastOne + sum;
                            lastOne = data[i];
                            //System.out.println( "DS data[ "+ pInvItem +" ] =" + data[ pInvItem ] );
                        }
                    }
                }
            }
            else if (orderSpatial == 2)
            {
                //h1, h2, hMin
                float hDiff = h2 - h1;
                float sum = 0;
                if (missingValManagement == 0)
                {
                    // no missing values
                    for (int i = 2; i < data.Length; i++)
                    {
                        data[i] += hMin; // add minimum back
                    }
                    data[0] = h1;
                    data[1] = h2;
                    sum = hDiff;
                    for (int i = 2; i < data.Length; i++)
                    {
                        sum += data[i];
                        data[i] = data[i - 1] + sum;
                    }
                }
                else
                {
                    // contains missing values
                    int idx = 0;
                    float lastOne = primaryMissingValue;
                    // add the minimum back and set h1 and h2
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i] != primaryMissingValue)
                        {
                            if (idx == 0)
                            {
                                // set h1
                                data[i] = h1;
                                sum = 0;
                                lastOne = data[i];
                                idx++;
                            }
                            else if (idx == 1)
                            {
                                // set h2
                                data[i] = h1 + hDiff;
                                sum = hDiff;
                                lastOne = data[i];
                                idx = i + 1;
                            }
                            else
                            {
                                data[i] += hMin;
                            }
                        }
                    }
                    if (lastOne == primaryMissingValue)
                    {
                        System.Console.Out.WriteLine("DS bad spatial differencing data");
                        return ;
                    }
                    for (int i = idx; i < data.Length; i++)
                    {
                        if (data[i] != primaryMissingValue)
                        {
                            //System.out.println( "DS pInvItem=" + pInvItem + " sum =" + sum );
                            sum += data[i];
                            //System.out.println( "DS before data[ "+ pInvItem +" ] =" + data[ pInvItem ] );
                            data[i] = lastOne + sum;
                            lastOne = data[i];
                            //System.out.println( "DS after data[ "+ pInvItem +" ] =" + data[ pInvItem ] );
                        }
                    }
                }
            } // end h1, h2, hMin

            // formula used to create values,  Y * 10**D = R + (X1 + X2) * 2**E

            int D = drs.DecimalScaleFactor;
            //System.out.println( "DS D=" + D );
            //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 DD = (float) System.Math.Pow((double) 10, (double) D);
            //System.out.println( "DS DD=" + DD );

            float R = drs.ReferenceValue;
            //System.out.println( "DS R=" + R );

            int E = drs.BinaryScaleFactor;
            //System.out.println( "DS E=" + E );
            //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 EE = (float) System.Math.Pow((double) 2.0, (double) E);
            //System.out.println( "DS EE=" + EE );

            if (missingValManagement == 0)
            {
                // no missing values
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = (R + data[i] * EE) / DD;
                }
            }
            else
            {
                // missing value == 1
                for (int i = 0; i < data.Length; i++)
                {
                    if (data[i] != primaryMissingValue)
                    {
                        data[i] = (R + data[i] * EE) / DD;
                    }
                }
            }
            scanMode = gds.ScanMode;
            scanningModeCheck();
            //System.out.println( "DS true end =" + gribStream.position() );
        }
示例#4
0
        /// <summary> complex unpacking of Grib2 data.</summary>
        /// <param gridTemplateName="gribStream">
        /// </param>
        /// <param gridTemplateName="gds">
        /// </param>
        /// <param gridTemplateName="drs">
        /// </param>
        /// <throws>  IOException </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'"
        private void complexUnpacking(System.IO.Stream raf, Grib2GridDefinitionSection gds, Grib2DataRepresentationSection drs, Grib2BitMapSection bms)
        {
            if (bms.BitMapIndicator != 255)
                throw new NotImplementedException("Complex unpacking with a bitmap is currently not supported");

            int mvm = drs.MissingValueManagement;
            //System.out.println( "DS mvm=" + mvm );

            float pmv = drs.PrimaryMissingValue;
            //System.out.println( "DS pmv=" + pmv );

            int NG = drs.NumberOfGroups;
            //System.out.println( "DS numGroups=" + numGroups );

            // 6-xx  Get reference values for groups (X1's)
            int[] X1 = new int[NG];
            int nb = drs.NumberOfBits;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < NG; i++)
            {
                X1[i] = bits2UInt(nb, raf);
                //System.out.println( "DS X1[ pInvItem ]=" + X1[ pInvItem ] );
            }

            // [xx +1 ]-yy Get number of bits used to encode each group
            int[] NB = new int[NG];
            nb = drs.BitsGroupWidths;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < NG; i++)
            {
                NB[i] = bits2UInt(nb, raf);
                //System.out.println( "DS numBitsEncodingEachGroup[ pInvItem ]=" + numBitsEncodingEachGroup[ pInvItem ] );
            }

            // [yy +1 ]-zz Get the scaled group lengths using formula
            //     Ln = ref + Kn * lengthIncrement, where n = 1-numGroups,
            //          ref = referenceGroupLength, and  lengthIncrement = lengthIncrement

            int[] L = new int[NG];
            int countL = 0;
            int ref_Renamed = drs.ReferenceGroupLength;
            //System.out.println( "DS ref=" + ref );
            int len_inc = drs.LengthIncrement;
            //System.out.println( "DS lengthIncrement=" + lengthIncrement );
            nb = drs.BitsScaledGroupLength;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < NG; i++)
            {
                // numGroups
                L[i] = ref_Renamed + (bits2UInt(nb, raf) * len_inc);
                //System.out.println( "DS L[ pInvItem ]=" + L[ pInvItem ] );
                countL += L[i];
            }
            //System.out.println( "DS countL=" + countL );

            // [zz +1 ]-nn get X2 values and calculate the results Y using formula
            //                Y * 10**D = R + (X1 + X2) * 2**E

            int D = drs.DecimalScaleFactor;
            //System.out.println( "DS D=" + D );
            //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 DD = (float) System.Math.Pow((double) 10, (double) D);
            //System.out.println( "DS DD=" + DD );

            float R = drs.ReferenceValue;
            //System.out.println( "DS R=" + R );

            int E = drs.BinaryScaleFactor;
            //System.out.println( "DS E=" + E );
            //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 EE = (float) System.Math.Pow((double) 2.0, (double) E);
            //System.out.println( "DS EE=" + EE );

            data = new float[countL];
            //System.out.println( "DS countL=" + countL + " dataPoints=" +
            //gds.getNumberPoints() );
            count = 0;
            Xlength = gds.Nx; // needs some smarts for different type Grids
            // used to check missing values when X2 is packed with all 1's
            int[] bitsmv1 = new int[31];
            //int bitsmv2[] = new int[ 31 ]; didn't code cuz number larger the # of bits
            for (int i = 0; i < 31; i++)
            {
                //bitsmv1[ pInvItem ] = ( bitsmv1[ pInvItem -1 ] +1 ) *2 -1;
                //bitsmv2[ pInvItem ] = ( bitsmv2[ pInvItem -1 ] +2 ) *2 -2;
                //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'"
                bitsmv1[i] = (int) System.Math.Pow((double) 2, (double) i) - 1;
                //bitsmv2[ pInvItem ] = (int) java.lang.Math.pow( (double)2, (double)pInvItem +1) -2;
                //System.out.println( "DS bitsmv1[ "+ pInvItem +" ] =" + bitsmv1[ pInvItem ] );
                //System.out.println( "DS bitsmv2[ "+ pInvItem +" ] =" + bitsmv2[ pInvItem ] );
            }
            int X2;
            bitPos = 0;
            bitBuf = 0;
            for (int i = 0; i < NG - 1; i++)
            {
                //System.out.println( "DS numBitsEncodingEachGroup[ pInvItem ]=" + numBitsEncodingEachGroup[ pInvItem ] );
                //System.out.println( "DS L[ pInvItem ]=" + L[ pInvItem ] );
                //System.out.println( "DS X1[ pInvItem ]=" + X1[ pInvItem ] );
                for (int j = 0; j < L[i]; j++)
                {
                    if (NB[i] == 0)
                    {
                        if (mvm == 0)
                        {
                            // X2 = 0
                            data[count++] = (R + X1[i] * EE) / DD;
                        }
                        else if (mvm == 1)
                        {
                            data[count++] = pmv;
                        }
                    }
                    else
                    {
                        X2 = bits2UInt(NB[i], raf);
                        if (mvm == 0)
                        {
                            data[count++] = (R + (X1[i] + X2) * EE) / DD;
                        }
                        else if (mvm == 1)
                        {
                            // X2 is also set to missing value is all bits set to 1's
                            if (X2 == bitsmv1[NB[i]])
                            {
                                data[count++] = pmv;
                            }
                            else
                            {
                                data[count++] = (R + (X1[i] + X2) * EE) / DD;
                            }
                        }
                        //System.out.println( "DS count=" + count );
                        //System.out.println( "DS numBitsEncodingEachGroup[ "+ pInvItem +" ]=" + numBitsEncodingEachGroup[ pInvItem ] );
                        //System.out.println( "DS X1[ "+ pInvItem +" ]=" + X1[ pInvItem ] );
                        //System.out.println( "DS X2 =" +X2 );
                        //System.out.println( "DS X1[ pInvItem ] + X2 ="+(X1[ pInvItem ]+X2) );
                    }
                } // end for j
            } // end for pInvItem
            // process last group
            int last = drs.LengthLastGroup;
            //System.out.println( "DS last=" + last );
            for (int j = 0; j < last; j++)
            {
                // last group
                if (NB[NG - 1] == 0)
                {
                    if (mvm == 0)
                    {
                        // X2 = 0
                        data[count++] = (R + X1[NG - 1] * EE) / DD;
                    }
                    else if (mvm == 1)
                    {
                        data[count++] = pmv;
                    }
                }
                else
                {
                    X2 = bits2UInt(NB[NG - 1], raf);
                    if (mvm == 0)
                    {
                        data[count++] = (R + (X1[NG - 1] + X2) * EE) / DD;
                    }
                    else if (mvm == 1)
                    {
                        // X2 is also set to missing value is all bits set to 1's
                        if (X2 == bitsmv1[NB[NG - 1]])
                        {
                            data[count++] = pmv;
                        }
                        else
                        {
                            data[count++] = (R + (X1[NG - 1] + X2) * EE) / DD;
                        }
                    }
                }
            } // end for j
            scanMode = gds.ScanMode;
            scanningModeCheck();
            //System.out.println( "DS true end =" + gribStream.position() );
        }
示例#5
0
        /// <summary> simple Unpacking method for Grib2 data.</summary>
        /// <param gridTemplateName="gribStream">
        /// </param>
        /// <param gridTemplateName="gds">
        /// </param>
        /// <param gridTemplateName="drs">
        /// </param>
        /// <param gridTemplateName="bms">
        /// </param>
        /// <throws>  IOException </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'"
        private void simpleUnpacking(System.IO.Stream raf, Grib2GridDefinitionSection gds, Grib2DataRepresentationSection drs, Grib2BitMapSection bms)
        {
            if (bms.BitMapIndicator != 255 && bms.BitMapIndicator != 0)
                throw new NotImplementedException("Simple unpacking with a bitmap indicator other than 0 or 255 is currently not supported");

            int dtn = drs.DataTemplateNumber;
            //System.out.println( "DS dtn=" + dtn );

            if (dtn == 1)
            {
                // Matrix values
                System.Console.Out.WriteLine("DS Matrix values not supported yet");
                return ;
            }
            // dataPoints are number of points encoded, it could be less than the
            // numberOfDataPoints in the grid record if bitMap is used, otherwise equal
            int dataPoints = drs.DataPoints;
            //System.out.println( "DS DRS dataPoints=" + drs.getDataPoints() );
            float pmv = drs.PrimaryMissingValue;
            //System.out.println( "DS pmv=" + pmv );
            int nb = drs.NumberOfBits;
            //System.out.println( "DS numberOfBits=" + numberOfBits );
            int D = drs.DecimalScaleFactor;
            //System.out.println( "DS D=" + D );
            //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 DD = (float) System.Math.Pow((double) 10, (double) D);
            //System.out.println( "DS DD=" + DD );
            float R = drs.ReferenceValue;
            //System.out.println( "DS R=" + R );
            int E = drs.BinaryScaleFactor;
            //System.out.println( "DS E=" + E );
            //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 EE = (float) System.Math.Pow((double) 2.0, (double) E);
            //System.out.println( "DS EE=" + EE );

            int numberPoints = gds.NumberOfDataPoints;
            //System.out.println( "DS GDS NumberOfDataPoints=" +  gds.getNumberPoints() );
            data = new float[numberPoints];

            bool[] bitmap = bms.Bitmap;

            //  Y * 10**D = R + (X1 + X2) * 2**E
            //   E = binary scale factor
            //   D = decimal scale factor
            //   R = reference value
            //   X1 = 0
            //   X2 = scaled encoded value
            //   data[ pInvItem ] = (R + ( X1 + X2) * EE)/DD ;

            if (bitmap == null)
            {
                for (int i = 0; i < numberPoints; i++)
                {
                    //data[ pInvItem ] = (R + ( X1 + X2) * EE)/DD ;
                    data[i] = (R + bits2UInt(nb, raf) * EE) / DD;
                }
            }
            else
            {
                bitPos = 0;
                bitBuf = 0;
                for (int i = 0; i < bitmap.Length; i++)
                {
                    if (bitmap[i])
                    {
                        //data[ pInvItem ] = (R + ( X1 + X2) * EE)/DD ;
                        data[i] = (R + bits2UInt(nb, raf) * EE) / DD;
                    }
                    else
                    {
                        data[i] = pmv;
                    }
                }
            }
            scanMode = gds.ScanMode;
            Xlength = gds.Nx; // needs some smarts for different type Grids
            //scanningModeCheck();
        }
示例#6
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;
        }
示例#7
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;
 }
示例#8
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;
        }