Converting NetCDF to GRIB2
Asked Answered
F

4

10

I know there is software like wgrib2 that will convert files in grib and grib2 format to NetCDF files, but I need to go the other way: from NetCDF to grib2, because the local weather offices here can only consume gridded data in grib2 format.

It appears that one solution could be in Python, using the NetCDF4-Python library (or other) to read the NetCDF files and using pygrib to write grib2.

Is there a better way?

Fokos answered 15/3, 2013 at 12:32 Comment(0)
F
10

After some more research, I ended up using the British Met Office "Iris" package (http://scitools.org.uk/iris/docs/latest/index.html) which can read NetCDF as well as OPeNDAP, GRIB and several other formats, and allows to save as NetCDF or GRIB.

Basically the code looks like:

import iris

cubes = iris.load('input.nc')       # each variable in the netcdf file is a cube
iris.save(cubes[0],'output.grib2')  # save a specific variable to grib 

But if your netcdf file doesn't contain sufficient metadata, you may need to add it, which you can also do with Iris. Here's a full working example:

https://github.com/rsignell-usgs/ipython-notebooks/blob/master/files/Iris_CFSR_wave_wind.ipynb

Fokos answered 2/5, 2013 at 11:37 Comment(0)
E
5

I know CDO is mentioned in one of the other answers, but I thought it would be useful to give the full command, you need copy and you specify the output format with the -f option

cdo -f grb2 copy in.nc out.grb
Eggshell answered 7/6, 2017 at 20:25 Comment(0)
B
4

One can also use climate data operators (cdo's) for the task -https://code.zmaw.de/projects/cdo/wiki

but need to install the software with all additional libraries.

Bekah answered 5/4, 2013 at 10:30 Comment(3)
Wow, that looks pretty extensive. Have you ever used it to convert NetCDF4 to GRIB2?Fokos
This is indeed THE best way to do it. When dealing with netCDF files, sooner or later CDO and/or NCO will be very useful. They are fast and reliable, they keep metadata, they are free and extremely versatile. Once you have learned CDO and NCO well any manipulation on gridded files can be performed. The two are by far the most complete solutions for working with gridded files without needing to code (R, Python are more versatile but usually slower).Baldheaded
In reply to Rich, GRIB2 output is possible by using the -f grb2 flag, see below for full commandEggshell
R
-1

ECMWF has a command line based tool to do just this: https://software.ecmwf.int/wiki/display/GRIB/grib_to_netcdf

Rameau answered 27/1, 2015 at 18:24 Comment(2)
the question was about netcdf to grib, not grib to netcdfFokos
True, but N1B4 is correct to point out that ECMWF has a neat toolset for handling grib/netcdf, although I believe grib_api has just been superseded again.Eggshell

© 2022 - 2024 — McMap. All rights reserved.