D. Müller
D. Müller

Reputation: 3426

Java GRIB-Decoder: Extract data from GRIB2 files

I've downloaded some grib data files from here: ftp://data-portal.ecmwf.int/20160721000000/ (file type is .bin) and want to extract the data from this file in my Java application (I want to load the extracted data into a database later). I'm just trying with the file ftp://wmo:[email protected]/20160721000000/A_HWXE85ECEM210000_C_ECMF_20160721000000_24h_em_ws_850hPa_global_0p5deg_grib2.bin.

Therefore I've created a new Java project and added the two libraries grib-8.0.29.jar and netcdfAll-4.6.6.jar. Documentation for the grib API can be found here: http://www.unidata.ucar.edu/software/decoders/grib/javadoc/. I need to open the downloaded files to get the data. Retrieving some metadata via Grib2Dump seems to work (see below). Also the Grib2Input instance sais, that I have a valid GRIB file of version 2.

Here my working code for retrieving some metadata:

public static void main(String[] args) throws IOException, InterruptedException {
        File srcDir = new File("C://test//");
        File[] localFiles = srcDir.listFiles();

        for (File tempFile : localFiles) {
             RandomAccessFile raf = new RandomAccessFile(tempFile.getAbsolutePath(), "r");

             System.out.println("======= Grib2GDSVariables ==========");
             Grib2GDSVariables gdsVariables = new Grib2GDSVariables(raf.readBytes(raf.read())); 
             System.out.println("Gds key : " + gdsVariables.getGdsKey());

             System.out.println("======= Grib2Input ==========");
             Grib2Input input = new Grib2Input(raf);
             System.out.println(Grib2Input.isValidFile(raf));
             System.out.println("scan : " + input.scan(true, true));
             System.out.println("getGDSs.size: " + input.getGDSs().size());
             System.out.println("getProducts.size: " + input.getProducts().size());
             System.out.println("getRecords.size: " + input.getRecords().size());
             System.out.println("edition: " + input.getEdition());

             System.out.println("======= Grib2Dump ==========");
             Grib2Dump dump = new Grib2Dump(); 
             dump.gribDump(new String[] {tempFile.getAbsolutePath()});

             System.out.println("======= Grib2ExtractRawData ==========");
             Grib2ExtractRawData extractRawData = new
             Grib2ExtractRawData(raf); extractRawData.main(new String[] {tempFile.getAbsolutePath()});
        }

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

This produces the following output:

======= Grib2GDSVariables ==========
Gds key : -1732955898
======= Grib2Input ==========
true
scan : true
getGDSs.size: 0
getProducts.size: 0
getRecords.size: 0
edition: 2
======= Grib2Dump ==========
--------------------------------------------------------------------
                        Header : GRIB2
                    Discipline : 0 Meteorological products
                  GRIB Edition : 2
                   GRIB length : 113296
            Originating Center : 98 European Center for Medium-Range Weather Forecasts (RSMC)
        Originating Sub-Center : 0
Significance of Reference Time : 1 Start of forecast
                Reference Time : 2016-07-21T00:00:00Z
                Product Status : 0 Operational products
                  Product Type : 1 Forecast products
         Number of data points : 259920
                     Grid Name : 0 Latitude_Longitude
                     Grid Shape: 6 Earth spherical with radius of 6,371,229.0 m
Number of points along parallel: 720
Number of points along meridian: 361
                   Basic angle : 0
    Subdivisions of basic angle: -9999
  Latitude of first grid point : 90.0
 Longitude of first grid point : 0.0
  Resolution & Component flags : 48
                         Winds : True
   Latitude of last grid point : -90.0
  Longitude of last grid point : 359.5
         i direction increment : 0.5
         j direction increment : 0.5
                    Grid Units : degrees
                 Scanning mode : 0
            Product Definition : 2 Derived forecast on all ensemble members at a point in time
            Parameter Category : 2 Momentum
                Parameter Name : 1 Wind_speed
               Parameter Units : m s-1
       Generating Process Type : 4 Ensemble Forecast
                  ForecastTime : 24
            First Surface Type : 100 Isobaric surface
           First Surface value : 85000.0
           Second Surface Type : 255 Missing
          Second Surface value : -9.999E-252
======= Grib2ExtractRawData ==========
finished

I tried around for two days now but couldn't get it to work! I can't obtain the content data (lat, lon, value) from the file...

Can someone give an example in Java?

Upvotes: 1

Views: 3370

Answers (1)

DopplerShift
DopplerShift

Reputation: 5843

You shouldn't use the GRIB classes in netCDF-java directly. Instead, use

NetcdfFile.open()

That will give you access through the CDM, giving you a straightforward interface with variables and attributes. There's a tutorial here: https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfFile.html

Upvotes: 3

Related Questions