Converting grib to netcdf

Purpose

Some reanalysis data comes in grib format but I have a personal preference for NetCdf data because it is usually easier to load into your favourite programming or scripting language. An easy solution is to convert grib to netcdf. The following bash script uses NCL in a loop to convert every grib files in a given folder into netcdf files.

Code

#!/bin/bash
#the script must be placed outside of the folder where the data is
#therefore script.sh folder_grib and folder_netcdf are in the same location
folderin='folder_grib' #folder where the grib are
folderout='folder_netcdf' #folder to store netcdf
cd $folderin # cd to that folder
#now loop through all the grib files in there
for file in *.grib
do
echo 'test '$file #display the current selected file
ncl_convert2nc $file -o "../"$folderout -e grb #convert it using ncl
done

17 thoughts on “Converting grib to netcdf

  1. Dr. D. Murambadoro

    Would you have a Python version of your code that can extract specific message (parameters) from grib files?
    Thanks.

    Reply
    1. admin Post author

      Unfortunately not,
      I usually convert everything in the grib file or a subset of variables to netcdf and do all the reading with Matlab.

      Reply
      1. Manoj

        I need to convert .grb format file to any raster file readable by ArcMap.
        Do you have any quick procedure.

        Thanks,

        Manoj

        Reply
    1. Patrick Martineau Post author

      Hi Anil,
      I have never done that. However, a quick search on Google indicates that CDO (Climate Data Operators) can do that with a command similar to : cdo -f nc copy file.nc file.grb. If you are successful converting NetCDF to Grib2 using CDO, please let me know. CDO

      Reply
    1. Patrick Martineau Post author

      I think the same script will work with Grib2, please tell me if I am wrong.

      Reply
      1. Mireya

        Hello, I’ve tried to use this code for Grib2 data and I got this error
        fatal:NclGRIB2: Invalid Product Definition Template.
        Segmentation fault (core dumped)
        And no matter what program I use (CDO or wgrib2) it seems to be a problem with the template, do you have any idea of what can I do?

        Thank you in advance!

        Reply
  2. S

    Hello, Patrick !

    I am trying to use your script and am getting the following error:

    0 [main] ncl 5344 child_info_fork::abort: C:\cygwin\bin\cygiconv-2.dll: Loaded to different address: parent(0x2E0000) != child(0x220000)
    fatal:systemfunc: cannot create child process:[errno=11]
    fatal:[“Execute.c”:8567]:Execute: Error occurred at or near line 270 in file /tmp/tmp616-538483e4.ncl

    Just wanted to ask what could be the reason of this error ?

    Reply
    1. Patrick Martineau Post author

      Unfortunately, I do not understand this error message. I see from the error message that you are running this on windows with cygwin? I only executed this script on native Linux machines.
      I hope you can find a workaround. Perhaps you could use ncl_convert2nc with fewer options to see if it works? Perhaps you could try a simple function like ncl_filedump to see if basic ncl functionality work well.

      Reply
      1. S

        Thanks for the quick reply.

        If I run ncl_convert2nc with fewer options
        ncl_convert2nc /home/user/data/grib/111.grib
        I get following results:

        warning: Entry (98) not found in code table file /usr/local/lib/ncarg/grib2_codetables/ecmwf/4/4.2.3.1.table
        warning: Entry (99) not found in code table file /usr/local/lib/ncarg/grib2_codetables/ecmwf/4/4.2.3.1.table
        warning:NCL does not yet fully support GRIB2 space view perspective grid (template 90), no coordinate variables will be supplied for this grid
        0 [main] ncl 5132 child_info_fork::abort: C:\cygwin\bin\cygiconv-2.dll: Loaded to different address: parent(0x2930000) != child(0x260000)
        fatal:systemfunc: cannot create child process:[errno=11]
        fatal:[“Execute.c”:8567]:Execute: Error occurred at or near line 270 in file /tmp/tmp6080-5384f204.ncl
        ncl_filedump works as expected.

        Reply
        1. Patrick Martineau Post author

          I can’t be of much help since I never ran into those errors before. Perhaps CDO could be useful here.

          Reply
  3. Gabriel B N

    Hi,
    I really need to learn how to use grib1 files in R. I cannot find any help online so far and I’m running out of time. I do have Ubuntu installed but I am far more secure using Windows 7. I have RStudio installed here on Windows. And I can also do a little of Matlab. Is there any way to access grib1 data from R? Can I convert grib1 files to ncdf from within R? Kindly help.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *