Convert rotated-pole to regular lonlat grid
Source:vignettes/articles/rotpole-cdo.Rmd
rotpole-cdo.Rmd
This is a tutorial how to create the config file for remapping with CDO. It is based on the code from the TopoCLIM package. Remapping is done to grid centres, which is half a gridbox (res/2) is added.
First, we get the information from the input netcdf file (rotated-pole).
library(ncdf4)
file_nc <- system.file("extdata", "alps1.nc", package = "eurocordexr")
nc_obj <- nc_open(file_nc)
We can check that the resolution is what we expect:
# resolution
ncvar_get(nc_obj, "rlon") |> diff() |> round(3) |> table()
#>
#> 0.11
#> 99
ncvar_get(nc_obj, "rlat") |> diff() |> round(3) |> table()
#>
#> 0.11
#> 63
# -> res: 0.11
res <- 0.11
Optionally set a buffer:
buffer <- 0 # or 2*res or something else
For the extent, here I take the interior of the grid, which has
already been cropped (with cdo sellonlatbox
). Note that
remapping in CDO can crop at the same time, so probably you’d want to
manually set the extent and apply it directly to the whole domain files
(and not crop beforehand).
mat_lat <- ncvar_get(nc_obj, "lat")
mat_lon <- ncvar_get(nc_obj, "lon")
# this takes the interior only
latS <- mat_lat |> apply(1, min) |> max()
latN <- mat_lat |> apply(1, max) |> min()
lonW <- mat_lon |> apply(2, min) |> max()
lonE <- mat_lon |> apply(2, max) |> min()
Then write the config file.
file_content <- paste0(
"gridtype = lonlat\n",
"xsize = ", round((lonE - lonW + 2*buffer) / res), "\n",
"ysize = ", round((latN - latS + 2*buffer) / res), "\n",
"xfirst = ", (lonW - buffer + res / 2), "\n",
"xinc = ", res, "\n",
"yfirst = ", (latS - buffer + res / 2), "\n",
"yinc = ", res, "\n"
)
cat(file_content)
#> gridtype = lonlat
#> xsize = 132
#> ysize = 52
#> xfirst = 3.40486734390259
#> xinc = 0.11
#> yfirst = 42.8298985290527
#> yinc = 0.11
# to write directly to a file:
# cat(file_content, file = "coords4cdo.txt")
This can be used with CDO: