1

I've just started parsing some info inside GRIB2 file with NOAA weather forecast, and I am realizing this task is not as easy as I could think 'a priori'. I need to process some info inside JAVA routines, and until now I am experiencing with library from Unidata.ucar.edu.

The problem is that accessing any specific data inside the file is a little pain in the ass, because of I had never seen a weather interchange file format until now. Therefore, while I read up documentation about GRIB2 file format, if any of you know some JAVA working examples that extract concrete information it would be very appreciated (e.g. retrieve temperature at some level in specific lat/lon)?

I found this library's API just here, but no examples are provided so in the meanwhile it's time to study.


I create a Grib2Input object with a RandomAccessFile as param like this:

Grib2Input grib2Input;
String pathfile = "c:/gfs.t06z.pgrb2f03";

//Create RandomAccessFile
RandomAccessFile gribfile = null;
try {
  gribfile = new ucar.unidata.io.RandomAccessFile(pathfile2, "rw");
} catch (IOException e) {
  e.printStackTrace();
}

//Create grib2Input file
grib2Input = new Grib2Input(gribfile);      
try {
  grib2Input.scan(false,  false);       
} catch (IOException e) {
  e.printStackTrace();
}

And here I can get info about GRIB2 file as Product Status, Grid Name, Grid Units, and so foth by getting Grib2GridDefinitionSection of my grib2Input object and then Grib2GDSVariables with getGdsVars() getter on it .

Now I suppose (maybe i'm wrong) each record is compound ofall sections (1, 2, 3... of GRIB2 spec) First of all: Is this righg? For example, record 1 has its GridDefinitionSection, its ProductDefinitionSection, its DataRepresentationSection, and so foth.

So, then I try to get one particular record in this way:

List<Grib2Record> records = grib2Input.getRecords();
Grib2Record myRecord = records.get(15); //Get record number 15. 

How can then retrieve information just about this record as Parameter Name, Parameter Units and of course, its data??

The hole file has information about Temperature, Wind, Clouds, and much more, and my problem comes in what I'm seeing that information for each record is the same (no matter how many records I get). Of course, I'm doing something wrong or I don't know how the file is format yet.


Here are my thoughts:

1) With my RandomAccessFile, I can create a Grid2Data:

Grib2Data gd = new Grib2Data(gribfile);

2) Then I can get data in this way:

float[] data = null;
for (int i = 0; i < records.size(); i++)
{
    //First get record information   
    System.out.println("record " + i +  "-> Param. Categ.: " + records2.get(i).getPDS().getPdsVars().getParameterCategory());
    System.out.println("record " + i +  "-> Param. Number: " + records2.get(i).getPDS().getPdsVars().getParameterNumber());
    try 
    {
        data = gd.getData(
                records.get(i).getGdsOffset(),
                records.get(i).getPdsOffset(), 
                records.get(i).getId().getRefTime() //This param must be new, because it 
                                                    //is not documented in API
               );
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    //Second print some data
    for (int j = 0; j < 50000; j+=5000)
    {
        System.out.println("data[" + j + "] -> " + data[j]);
    }

    System.out.println("--");
}

Key question:

If I open GRIB2 file with some software like tkdegrib then I can see information about a number of records (pressure, temperature, etc) that not match with the number of records I can see with the routine above. For example above, with a GRIB2 from NOAA about weather forecast, tkdegrib shows information about 319 records and I can print information of 315 records ¿? So I can't match my printed data with the extracted with tkdegrib to test this information is the right one.

Any suggestion?

Am I doing something wrong?

4
  • There are some usage examples in the unit tests at sub.unidata.ucar.edu/common/thredds/branch/grib/RB-GRIB-8.5/… , but they are not very comprehensible. Also, the library/API seems to not be very readable; so yeah, you basically are down to that PITA...Sorry!
    – til_b
    Commented Apr 10, 2013 at 11:36
  • What exactly is your question? Commented Apr 10, 2013 at 11:49
  • Thank you for your responses! I've updated the post. In the meanwhile, I'll see these examples.
    – elguime
    Commented Apr 10, 2013 at 13:10
  • anyone decode GRIB2 with this JAVA library?
    – elguime
    Commented Apr 11, 2013 at 14:24

1 Answer 1

1

I made same experiences when looking for Java libraries, finding that there are not many, and most of them difficult to use.

As an alternative. you could try the GRIB2Tools library at https://github.com/philippphb/GRIB2Tools, maybe it will work for you. It is rather basic but easy to use and can be extended easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.