Converting grads files to NetCDF from within R using CDO

A while back I wrote a blog post on the R package I wrote for reading and manipulating grads data. The library is quite basic, and does not support a number of grads data file options:

  • Using one ctl file, and multiple binary files
  • Some variables have only one vertical level

When I faced data that had these properties, I had a choice to either expand my readGrads library, or convert the data to some other format which was easier to read into R. I chose to convert the data to NetCDF and then reading it using the excellent ncdf library. Luckily I did not have to write a grads to NetCDF conversion tool myself, but I used the cdo tool. This program is “a collection of command line Operators to manipulate and analyse Climate and NWP model Data” (quote from the website). Converting a grads file to NetCDF can be done using the following command:

As I wanted to perform this conversion from within R, I created a function which uses cdo through a system call. The grads2nc function looks like:

With this function loaded into the R session, the conversion can be performed using:

Tagged with: , , ,
Posted in R stuff
5 Comments » for Converting grads files to NetCDF from within R using CDO
  1. wanderer says:

    Hi Paul

    So the command is:
    cdo -f nc import_binary in_grads.ctl

    But we are not specifying the actual binary data file. So for example I have data.grd and metadata.ctl file. How do I convert the data.grd file to netcdf format.

    Thanks in anticipation.

  2. Roberto Garcia says:

    Tks for the clarification Paul.

  3. Elham says:

    Is it possible to do any cdo function in R using this method? I don’t really understand how you connected cdo and R here. Could you explain it in easy terms please?


    • Paul Hiemstra says:

      You can indeed run cdo command. The important command is system, which allows you to run any command line tool such as cdo. I build a string with the appropriate cdo command, and execute it using system. To run another cdo command you need to change the string.

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see