grib2fimex - tnipen/NWPdocs GitHub Wiki

Reading GRIB2 using Fimex

Download a GRIB2 file (from DWD)

  ### ...get timestamp and url/file components...
  dtg=`date +"%Y%m%d"`;
  hh="00";
  loc="https://opendata.dwd.de/weather/nwp/cosmo-d2/grib/${hh}/";
  pre='cosmo-d2_germany_regular-lat-lon_single-level_'
  parl="t_2m"
  paru="T_2M"
  lead="000"
  suff='.grib2.bz2'
  final='.grib2;
  ### ...make url and output paths
  url=${loc}${parl}/${pre}${dtg}${hh}_${lead}_${paru}${suff}
  file=cosmo2-d2_${parl}_${dtg}${hh}_${lead}
  raw=${file}${suff}
  grib=${file}${final}
  echo "Fetching ${url} => ${out}";
  ### ...download grib-file from dwd
  wget ${url} -o dwd.log -O ${raw}
  ### ...uncompress file (creates ${grib})
  bunzip2 ${raw}

Install fimex-binary on Ubuntu (bash)

  sudo add-apt-repository ppa:met-norway/fimex
  sudo apt-get update
  sudo apt-get install fimex-bin

Examply using the fimex binary

This example interpolates a GRIB2-file to specified locations using the fimex binary which creates a NetCDF-file. The NetCDF-file is printed using ncdump, a binary from the NCO-suite.

  ### Set which GRIB2 file you are using (if not already set)
  grib="cosmo2-d2_t_2m_2020082100_000.grib2"
  ### Interpolate using fimex
  fimex --input.file $grib --input.config cdmGribReaderConfig.xml --interpolate.latitudeValues 50.0,50.2,50.4,50.6 --interpolate.longitudeValues 10.0,10.2,10.4,10.6 --output.file test.nc
  ### And finally print content of NetCDF file to stdout... [~20ms?]
  ncdump test.nc

Note that cdmGribReaderConfig.xml is available from http://fimex.met.no/doc/gribReaderDoc.html.

Installation of fimex-python library (for the Python interface)

  sudo apt-get install python3-pyfimex0-1.5

Example running fimex-python

This example shows a python-script for interpolating a GRIB2 file using python-fimex returning a Python-numpy array.

  import pyfimex0
  import numpy
  import os.path
  import unittest
  #test_srcdir = os.path.join(os.path.dirname(__file__), "..", "..", "test")
  test_srcdir=""

  class TestInterpolator(unittest.TestCase):

      def test_LonLatPoints(self):
          test_ncfile = 'cosmo2-d2_t_2m_2020082100_000.grib2';
          configFile='cdmGribReaderConfig.xml';
          r = pyfimex0.createFileReader('grib2', test_ncfile, configFile)

          inter_ll = pyfimex0.createInterpolator(r)

          lats = [50.109, 50.052]
          lons = [10.965, 10.13]
          inter_ll.changeProjection(pyfimex0.InterpolationMethod.BILINEAR,lons, lats)

          i_cdm = inter_ll.getCDM()
          self.assertEqual(len(lons), i_cdm.getDimension('x').getLength())
          self.assertEqual(1, i_cdm.getDimension('y').getLength())

          for v in inter_ll.getDataSlice('air_temperature_2m',0).values():
              print(v);
              self.assertTrue((v > 250) and (v < 300))

  if __name__ == '__main__':
      unittest.main()

Tips and tricks

Note that multiple GRIB-files can be concattenated:

  cat fileA.grib2 fileB.grib2 > file.grib2

which improves performance compared to reading one file at a time.

Note also that fimex may read multiple grib-files using --input.file=glob:*.grib2.