Extracts a variable from netcdf, and returns a
data.table
with cell index, date, values, and
optionally: coordinates.
Usage
nc_grid_to_dt(
filename,
variable,
icell_raster_pkg = TRUE,
add_xy = FALSE,
interpolate_to_standard_calendar = FALSE,
date_range,
verbose = FALSE
)
Arguments
- filename
Complete path to .nc file.
- variable
Name of the variable to extract from
filename
(character).- icell_raster_pkg
Boolean, if
TRUE
, cell indices will be ordered as if you were extracting the data with the raster package.- add_xy
Boolean, if
TRUE
, adds columns with x and y coordinates.- interpolate_to_standard_calendar
Boolean, if
TRUE
will usemap_non_standard_calendar
to interpolate values to a standard calendar.- date_range
(optional) two-element vector of class Date (min, max), which will be used to extract only parts of the netcdf file
- verbose
Boolean, if
TRUE
, prints more information.
Value
A data.table
with columns:
date: Date of class
Date
, if file has a standard calendar. Date as character, if it has a non-standard calendar (360, noleap) and ifinterpolate_to_standard_calendar
is set toFALSE
. Ifinterpolate_to_standard_calendar
isTRUE
, it's always of classDate
.variable: Values, column is renamed to input
variable
(optional) x,y: Coordinates of netcdf dimensions, will be renamed to dimension names found in array named after input
variable
Details
Coordinates are usually not put in the result, because it saves space. It is
recommended to merge them after the final operations. The unique cell index
is more efficient. However, if you plan to merge to data extracted with the
raster package (assuming the same grid), then cell indices might differ. Set
icell_raster_pkg
to TRUE
, to have the same cell indices. Note
that raster and ncdf4 have different concepts of coordinates (cell corner vs.
cell center), so merging based on coordinates can produce arbitrary results
(besides rounding issues).
Warning
Netcdf files can be huge, so loading everything in memory can rapidly crash your R session. Think first about subsetting or aggregating (e.g. using CDO: https://code.mpimet.mpg.de/projects/cdo/).
See also
The raster and terra packages can also open netcdf files and create
data.frames with raster::as.data.frame
or terra::as.data.frame
. But, it does
not handle non-standard calendars, and returns a data.frame, which is
slower than data.table.
Examples
# example data from EURO-CORDEX (cropped for size)
fn1 <- system.file("extdata", "test1.nc", package = "eurocordexr")
dat <- nc_grid_to_dt(fn1)
str(dat)
#> Classes ‘data.table’ and 'data.frame': 25550 obs. of 3 variables:
#> $ icell : int 64 65 66 67 68 69 70 57 58 59 ...
#> $ date : Date, format: "1950-01-01" "1950-01-01" ...
#> $ tasmin: num 269 269 271 271 272 ...
#> - attr(*, ".internal.selfref")=<externalptr>