コード例 #1
0
        private protected override IEnumerable <float> DoEnumerateDataValues(BufferedBinaryReader reader, DataSection dataSection, long dataPointsNumber)
        {
            IEnumerable <int> ReadPackedValues()
            {
                reader.NextUIntN();
                for (var i = 0; i < dataPointsNumber; i++)
                {
                    yield return(reader.ReadUIntN(NumberOfBits));
                }
            }

            return(Unpack(ReadPackedValues()));
        }
        private protected override IEnumerable <float> DoEnumerateDataValues(BufferedBinaryReader reader, DataSection dataSection, long dataPointsNumber)
        {
            var mvm = MissingValueManagement;

            float pmv = PrimaryMissingValue;

            int ng = (int)NumberOfGroups;

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

            reader.NextUIntN();
            int sign;

            // ds is number of bytes, convert to bits -1 for sign bit
            ds = ds * 8 - 1;
            if (os == SpatialDifferencingOrder.FirstOrder)
            {
                // first order spatial differencing g1 and gMin
                sign = reader.ReadUIntN(1);
                g1   = reader.ReadUIntN(ds);
                if (sign == 1)
                {
                    g1 *= (-1);
                }

                sign = reader.ReadUIntN(1);
                gMin = reader.ReadUIntN(ds);
                if (sign == 1)
                {
                    gMin *= (-1);
                }
            }
            else if (os == SpatialDifferencingOrder.SecondOrder)
            {
                //second order spatial differencing h1, h2, hMin
                sign = reader.ReadUIntN(1);
                h1   = reader.ReadUIntN(ds);
                if (sign == 1)
                {
                    h1 *= (-1);
                }

                sign = reader.ReadUIntN(1);
                h2   = reader.ReadUIntN(ds);
                if (sign == 1)
                {
                    h2 *= (-1);
                }

                sign = reader.ReadUIntN(1);
                hMin = reader.ReadUIntN(ds);
                if (sign == 1)
                {
                    hMin *= (-1);
                }
            }
            else
            {
                throw new BadGribFormatException("DS Error");
            }

            // [ww +1]-xx  Get reference values for groups (X1's)
            int[] x1     = new int[ng];
            int   nbBits = NumberOfBits;

            reader.NextUIntN();
            for (int i = 0; i < ng; i++)
            {
                x1[i] = reader.ReadUIntN(nbBits);
            }

            // [xx +1 ]-yy Get number of bits used to encode each group
            int[] nb = new int[ng];
            nbBits = BitsGroupWidths;

            reader.NextUIntN();
            for (int i = 0; i < ng; i++)
            {
                nb[i] = reader.ReadUIntN(nbBits);
            }

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

            int[] l        = new int[ng];
            int   countL   = 0;
            int   rgLength = (int)ReferenceGroupLength;

            int lenInc = LengthIncrement;

            nbBits = BitsScaledGroupLength;

            reader.NextUIntN();
            for (int i = 0; i < ng; i++)
            {
                // NG
                l[i] = rgLength + reader.ReadUIntN(nbBits) * lenInc;

                countL += l[i];
            }

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

            var data = new float[countL];

            //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[i] = (int)Math.Pow(2, i) - 1;
            }

            var count = 0;
            int x2;

            reader.NextUIntN();
            for (int i = 0; i < ng - 1; i++)
            {
                for (int j = 0; j < l[i]; j++)
                {
                    if (nb[i] == 0)
                    {
                        if (mvm == 0)
                        {
                            // X2 = 0
                            data[count++] = x1[i];
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            data[count++] = pmv;
                        }
                    }
                    else
                    {
                        x2 = reader.ReadUIntN(nb[i]);

                        if (mvm == 0)
                        {
                            data[count++] = x1[i] + x2;
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            // 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++] = x1[i] + x2;
                            }
                        }
                    }
                }         // end for j
            }             // end for i

            // process last group
            int last = (int)LastGroupLength;

            for (int j = 0; j < last; j++)
            {
                // last group
                if (nb[ng - 1] == 0)
                {
                    if (mvm == 0)
                    {
                        // X2 = 0
                        data[count++] = x1[ng - 1];
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        data[count++] = pmv;
                    }
                }
                else
                {
                    x2 = reader.ReadUIntN(nb[ng - 1]);
                    if (mvm == 0)
                    {
                        data[count++] = x1[ng - 1] + x2;
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        // 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++] = x1[ng - 1] + x2;
                        }
                    }
                }
            }             // end for j

            if (os == SpatialDifferencingOrder.FirstOrder)
            {
                // g1 and gMin this coding is a sort of guess, no doc
                float sum = 0;
                if (mvm == 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 = pmv;
                    // add the minimum back and set g1
                    int idx = 0;
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i] != pmv)
                        {
                            if (idx == 0)
                            {
                                // set g1
                                data[i] = g1;
                                lastOne = data[i];
                                idx     = i + 1;
                            }
                            else
                            {
                                data[i] += gMin;
                            }
                        }
                    }

                    if (lastOne == pmv)
                    {
                        throw new BadGribFormatException("DS bad spatial differencing data");
                    }

                    for (int i = idx; i < data.Length; i++)
                    {
                        if (data[i] != pmv)
                        {
                            sum    += data[i];
                            data[i] = lastOne + sum;
                            lastOne = data[i];
                        }
                    }
                }
            }
            else if (os == SpatialDifferencingOrder.SecondOrder)
            {
                //h1, h2, hMin
                float hDiff = h2 - h1;
                float sum   = 0;
                if (mvm == 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 = pmv;
                    // add the minimum back and set h1 and h2
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i] != pmv)
                        {
                            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 == pmv)
                    {
                        throw new BadGribFormatException("DS bad spatial differencing data");
                    }

                    for (int i = idx; i < data.Length; i++)
                    {
                        if (data[i] != pmv)
                        {
                            sum += data[i];

                            data[i] = lastOne + sum;
                            lastOne = data[i];
                        }
                    }
                }
            }             // end h1, h2, hMin

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

            var d = DecimalScaleFactor;

            var dd = Math.Pow(10, d);

            var r = ReferenceValue;

            var e = BinaryScaleFactor;

            var ee = Math.Pow(2.0, e);

            if (mvm == 0)
            {
                // no missing values
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = (float)((r + data[i] * ee) / dd);
                }
            }
            else
            {
                // missing value == 1
                for (int i = 0; i < data.Length; i++)
                {
                    if (data[i] != pmv)
                    {
                        data[i] = (float)((r + data[i] * ee) / dd);
                    }
                }
            }

            return(data);
        }
コード例 #3
0
        private protected override IEnumerable <float> DoEnumerateDataValues(BufferedBinaryReader reader, DataSection dataSection, long dataPointsNumber)
        {
            var mvm = MissingValueManagement;

            var pmv = PrimaryMissingValue;

            var ng = (int)NumberOfGroups;

            // 6-xx  Get reference values for groups (X1's)
            var x1     = new int[ng];
            var nbBits = NumberOfBits;

            for (var i = 0; i < ng; i++)
            {
                x1[i] = reader.ReadUIntN(nbBits);
            }

            // [xx + 1 ]-yy Get number of bits used to encode each group
            var nb = new int[ng];

            nbBits = BitsGroupWidths;

            reader.NextUIntN();
            for (var i = 0; i < ng; i++)
            {
                nb[i] = reader.ReadUIntN(nbBits);
            }

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

            var l        = new int[ng];
            var rgLength = (int)ReferenceGroupLength;

            var lenInc = LengthIncrement;

            nbBits = BitsScaledGroupLength;

            reader.NextUIntN();
            for (var i = 0; i < ng; i++)
            {
                // NG
                l[i] = rgLength + reader.ReadUIntN(nbBits) * lenInc;
            }

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

            var d = DecimalScaleFactor;

            var dd = Math.Pow(10, d);

            var r = ReferenceValue;

            var e = BinaryScaleFactor;

            var ee = Math.Pow(2.0, e);

            // used to check missing values when X2 is packed with all 1's
            var bitsmv1 = new int[31];

            for (var i = 0; i < 31; i++)
            {
                bitsmv1[i] = (int)Math.Pow(2, i) - 1;
            }

            int x2;

            reader.NextUIntN();
            for (var i = 0; i < ng - 1; i++)
            {
                for (var j = 0; j < l[i]; j++)
                {
                    if (nb[i] == 0)
                    {
                        if (mvm == 0)
                        {
                            // X2 = 0
                            yield return((float)((r + x1[i] * ee) / dd));
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            yield return(pmv);
                        }
                    }
                    else
                    {
                        x2 = reader.ReadUIntN(nb[i]);
                        if (mvm == 0)
                        {
                            yield return((float)((r + (x1[i] + x2) * ee) / dd));
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            // X2 is also set to missing value is all bits set to 1's
                            if (x2 == bitsmv1[nb[i]])
                            {
                                yield return(pmv);
                            }
                            else
                            {
                                yield return((float)((r + (x1[i] + x2) * ee) / dd));
                            }
                        }
                    }
                }         // end for j
            }             // end for i

            // process last group
            var last = (int)LastGroupLength;

            for (var j = 0; j < last; j++)
            {
                // last group
                if (nb[ng - 1] == 0)
                {
                    if (mvm == 0)
                    {
                        // X2 = 0
                        yield return((float)((r + x1[ng - 1] * ee) / dd));
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        yield return(pmv);
                    }
                }
                else
                {
                    x2 = reader.ReadUIntN(nb[ng - 1]);
                    if (mvm == 0)
                    {
                        yield return((float)((r + (x1[ng - 1] + x2) * ee) / dd));
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        // X2 is also set to missing value is all bits set to 1's
                        if (x2 == bitsmv1[nb[ng - 1]])
                        {
                            yield return(pmv);
                        }
                        else
                        {
                            yield return((float)((r + (x1[ng - 1] + x2) * ee) / dd));
                        }
                    }
                }
            }             // end for j
        }