title: Java GRIB-Decoder: Extract data from GRIB2 files
body: <p>I've downloaded some grib data files from here: <a href="ftp://data-portal.ecmwf.int/20160721000000/" rel="nofollow">ftp://data-portal.ecmwf.int/20160721000000/</a> <strong>(file type is <code>.bin</code>)</strong> 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 <a href="ftp://wmo:essential@data-portal.ecmwf.int/20160721000000/A_HWXE85ECEM210000_C_ECMF_20160721000000_24h_em_ws_850hPa_global_0p5deg_grib2.bin" rel="nofollow">ftp://wmo:essential@data-portal.ecmwf.int/20160721000000/A_HWXE85ECEM210000_C_ECMF_20160721000000_24h_em_ws_850hPa_global_0p5deg_grib2.bin</a>.</p>
  
  <p>Therefore I've created a new Java project and added the two libraries <code>grib-8.0.29.jar</code> and <code>netcdfAll-4.6.6.jar</code>. Documentation for the <code>grib</code> API can be found here: <a href="http://www.unidata.ucar.edu/software/decoders/grib/javadoc/" rel="nofollow">http://www.unidata.ucar.edu/software/decoders/grib/javadoc/</a>. I need to open the downloaded files to get the data. Retrieving some metadata via <code>Grib2Dump</code> seems to work (see below). Also the <code>Grib2Input</code> instance sais, that I have a valid GRIB file of version 2. </p>
  
  <p>Here my working code for retrieving some metadata:</p>
  
  <pre><code>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");
  }
  </code></pre>
  
  <p>This produces the following output:</p>
  
  <pre><code>======= 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 &amp; 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
  </code></pre>
  
  <p>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...</p>
  
  <p>Can someone give an example in Java?</p>