wgrib2: Converting MERRA HDF file to GRIB-2
MERRA is a NASA reanalysis project that has created reanalyses in HDF format.
For some people, grib is a more convenient format. Here is our first attempt
to convert a MERRA HDF file into grib-2.
Surveying the situation we see that Lats or the simpler to use Lats4d can
convert HDF into grib-1. Grib-1 can be converted into grib-2 by the
cnvgrib utility except that lats creates a custom grib table. Therefore a
program would have to be written that converts the grib-1 file with the
custom grib table into a grib table using the standard NCEP table. Doable
but the program wouldn't be very generic. In addition the process would
take 3 steps (convert to grib1, translate grib1, convert to grib2).
Can we do better?
Looking at the programs that can read HDF, we see a familiar face, GrADS.
GrADS can read the HDF and write it out as a binary file. wgrib2 can then
read the binary file, a grib-2 template and metadata and then create a grib-2
1: The template
Grib-2 files include much metadata that is constant such as grid, center that created the
data, the process that created the data, etc. Making a template takes some knowledge
and we'll assume it has already been done. For the MERRA template, I used copygb/cnvgrib
to create a grib2 file with the proper grid definition and then used wgrib2 to fill
in some of the metadata (center). Probably should have filled in more of the metadata
but I didn't.
2: Writing the binary and meta data
The script g2write takes three arguements, the GrADS variable/expression,
the output file and some metadata (variable:level:forecast/analysis).
The script then creates the binary and metadata for one or more grids
depending whether t is varying. These two files along with the
template can be used to make a grib2 file.
3: All together
open the HDF file, sdfopen hdf-file
run the script, merra2grb2.gs
4: What does merra2grb2 do?
set the grid .. grads often uses an extra column .. 0..360 degrees
set the time
writes out ps with the metadata PRES:surface:anl
writes out slp with the metadata PRES:mean sea level:anl
loops over subset of available pressure levels
writes out h with the metadata HGT:xx mb:anl
writes out t with the metadata TMP:xx mb:anl
writes out u with the metadata UGRD:xx mb:anl
writes out v with the metadata VGRD:xx mb:anl
writes out qv with the metadata SPFH:xx mb:anl
writes out o3 with the metadata O3MR:xx mb:anl
after finishing looping over the vertical levels, the binary file are closed
the binary data is stored in test
the metadata is stored in test.inv, one line per field
finally wgrib2 is run which takes the binary data (test), inventory (test.inv) and template (hires-merra.template.grb)
and creates a grib2 file (test.grb2)