how to convert Grib1 to Netcdf using R?

2.9k views Asked by At

I have just downloaded some climate data in grib format. I want to use "R" to convert it to NetCDF format.

Furthermore, as the file consists of different variables, I would like to extract one variable at a time into individual files.

3

There are 3 answers

0
vitale232 On BEST ANSWER

It's hard to answer this without your specific file. You should look into producing reproducible examples, especially if you're posting to the R board.

For R, check out library(raster) and library(ncdf4). I just grabbed the first grib1 file I saw, and put together a quick example.

library(raster)
library(ncdf4)

download.file(url = 'ftp://ftp.hpc.ncep.noaa.gov/grib/20130815/p06m_2013081500f030.grb', destfile = 'test.grb')

(r <- raster('test.grb'))

n <- writeRaster(r, filename = 'netcdf_in_youR_comp.nc', overwrite = TRUE)
0
ClimateUnboxed On

1. RNOMADS

The package Rnomads has a function readgrib providing wrappers to external libraries allowing one to read grib files

2. converting to netcdf

If the GRIB data is on a regular lat-lon grid, then probably an easier way is to convert to netcdf as the support for reading that is more developed (and you are probably already used to using it)

You can convert grib in several ways, two of the easiest are

CDO:

cdo -f nc copy test.grb test.nc 

Use "-f nc4" if you want netcdf4 conventions.

ECCODES (on a mac install with brew install eccodes)

grib_to_netcdf -o test.nc test.grb
0
TOPMAD On

you can use ncl installed on you computer

library(ncdf)
 system(ncl_convert2nc  xxxx.grb, internal = TRUE)
 my.nc <- open.ncdf("result.nc")
 print(my.nc)