eliocamp / rcdo

An R wrapper of the CDO command-line utility

Home Page:https://eliocamp.github.io/rcdo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rcdo

Lifecycle: experimental

rcdo is a wrapper around Climate Data Operators.

Installation

You can install the development version of rcdo from GitHub with:

# install.packages("pak")
pak::pak("eliocamp/rcdo")

Most operators are supported and are partially documented. The functions start with cdo_ an the name of the operator (e.g. the selname operator is the cdo_selname() function)

Example

library(rcdo)
ncep <- "hgt_ncep.nc"

The ymonmean operator computes monthly annual cycle. The rcdo function is cdo_ymonmean()

ncep |> 
  cdo_ymonmean() 
#> CDO command:
#>    cdo ymonmean [ 'hgt_ncep.nc' ] {{output}}

The output just prints the command with a place holder output. Use cdo_execute() to actually run the command. If no outpuf file is specified, then the result is saved in a tempfile.

ncep |> 
  cdo_ymonmean() |> 
  cdo_execute()
#> [1] "/tmp/Rtmp89RjCK/file71812ea48dcc"
#> attr(,"mtime")
#> [1] "2024-07-04 20:11:34 AEST"
#> attr(,"size")
#> [1] 8631345

Operators can be chained. Lets select just the Southern Hemisphere first.

ncep |> 
  cdo_sellonlatbox(lon1 = 0, lon2 = 360, lat1 = -90, lat2 = 0) |> 
  cdo_ymonmean() 
#> CDO command:
#>    cdo ymonmean [ -sellonlatbox,0,360,-90,0 [ 'hgt_ncep.nc' ] ] {{output}}

Now also select the 500 hPa level

ncep |> 
  cdo_sellonlatbox(lon1 = 0, lon2 = 360, lat1 = -90, lat2 = 0) |> 
  cdo_sellevel(level = 500) |> 
  cdo_ymonmean() 
#> CDO command:
#>    cdo ymonmean [ -sellevel,500 [ -sellonlatbox,0,360,-90,0 [ 'hgt_ncep.nc' ] ] ] {{output}}
ncep |> 
  cdo_sellonlatbox(lon1 = 0, lon2 = 360, lat1 = -90, lat2 = 0) |> 
  cdo_sellevel(level = 500) |> 
  cdo_ymonmean() 
#> CDO command:
#>    cdo ymonmean [ -sellevel,500 [ -sellonlatbox,0,360,-90,0 [ 'hgt_ncep.nc' ] ] ] {{output}}

Prior art

The ClimateOperators package also wrapps CDO, but it’s approach is different. Instead of wrapping each operator as its own function with parameters as arguments, it provides a generic cdo() function that runs the operators that the user needs to write as strings. Instead of

ncep |> 
  rcdo::cdo_sellonlatbox(lon1 = 0, lon2 = 360, lat1 = -90, lat2 = 0) 

one would write

ClimateOperators::cdo("sellonlatbox,0,360,-90,0", ncep, output_file)

About

An R wrapper of the CDO command-line utility

https://eliocamp.github.io/rcdo/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:R 64.2%Language:C++ 35.8%