Mohammad Riyaz
Mohammad Riyaz

Reputation: 1544

Getting Wrong values while Longitude has negative in Grib2 file

I'm working on a weather application for mobile, where I'm reading weather information form Grib2 files coming from NOAA server. Everything is working fine, I'm getting all weather information and plotting to the map.

Now, I have a scenario, where I'm getting Negative Longitude Values. as showing in image below:

Negative Value

Note:

I'm using GribCS library to extract weather information from Grib File which seems working perfect if Longitude value is 11 instead -11; I'm extracting values with following way:

    void GribTwo()
    {
        #region Grib 2 Code

        Grib2Input input = new Grib2Input(RandomAccessFile);

        if (!input.scan(false, false))
        {
            Console.WriteLine("Failed to successfully scan grib file");
            return;
        }
        Grib2Data data = new Grib2Data(RandomAccessFile);

        var records = input.Records;

        foreach (Grib2Record record in records)
        {
            IGrib2IndicatorSection iis = record.Is;
            IGrib2IdentificationSection id = record.ID;
            IGrib2ProductDefinitionSection pdsv = record.PDS;
            IGrib2GridDefinitionSection gdsv = record.GDS;

            float[] values = data.getData(record.getGdsOffset(), record.getPdsOffset());

            if ((iis.Discipline == 0) && (pdsv.ParameterCategory == 2) && (pdsv.ParameterNumber == 2))
            {
                // U-component_of_wind
                int c = 0;
                for (double lat = gdsv.La1; lat <= gdsv.La2; lat = lat - gdsv.Dy)
                {
                    //THIS is  gdsv.Lo1; always has wrong values. 349 value instead -11 whuile Lo2 has correct value 7.
                    for (double lon = gdsv.Lo1; lon <= gdsv.Lo2; lon = lon + gdsv.Dx)
                    {
                        Console.WriteLine("U-Wind " + lat + "\t" + lon + "\t" + values[c]);
                        c++;
                    }
                }
            }

        #endregion
    }

As I mentioned my comment at inner loop that starting point gdsv.Lo1; has wrong value in this case It has 349 instead -11 that cause an issue.

While digging into deep I found following method that basically reading values from stream and converting to readable form.

    public static int int4(System.IO.Stream raf)
    {
        int a = raf.ReadByte();
        int b = raf.ReadByte();
        int c = raf.ReadByte();
        int d = raf.ReadByte();

        // all bits set to ones
        if (a == 0xff && b == 0xff && c == 0xff && d == 0xff)
            return UNDEFINED;

        int i2 = (1 - ((a & 128) >> 6)) * ((a & 127) << 24 | b << 16 | c << 8 | d);
        return i2;
    }
    //This method is available under GribNumbers.cs in GribCS library.

I don't understand, is there anything that I'm putting/reading wrong?

Upvotes: 1

Views: 480

Answers (3)

Davide
Davide

Reputation: 306

There is no error in the library you are using. Indeed the WMO grib2 standard states that longitudes should be coded in the interval [0-360] and negative longitudes are not allowed. This may sound quite odd, because it makes natural to represent a grid across the 180° meridian and not across Greenwhich meridian, but that's more a matter of geopolitics, not of geography, so we have to accept this rule.

Having said this, it is up to the user program to e.g. subtract 360 from the westmost longitude in order to obtain two correctly ordered grid longitudinal boundaries. Moreover, whether the first point of the grid lies to the west or to the east of the last point (both choices are in principle possible) is stated by one of the so-called scanning-mode flags coded in the message.

Upvotes: 0

user5418394
user5418394

Reputation:

I'm using the same library GribApi.Net Its a bit buggy now because of under development, but yeah you can use it to verify data or find a better solution to fix your issue in your existing library.

Upvotes: 2

Manish
Manish

Reputation: 635

Try This I tired and It extracting those negative values as well.

Result

Cheers!!

Upvotes: 1

Related Questions