Monday, 19 August 2019

Retrieving info from GRIB2 file with JAVA?


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 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?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...