Convert a netcdf array to long format as data.table (raw version)
Source:R/nc_grid_to_dt_raw.R
nc_grid_to_dt_raw.Rd
Extracts a variable from netcdf, similar to nc_grid_to_dt
, but
in a raw version. Requires more manual info, does not convert time units
(like 'days since x'), and requires xy variables not dimensions (like a
curvilinear grid).
Usage
nc_grid_to_dt_raw(
filename,
variable,
var_t,
var_x,
var_y,
has_time = TRUE,
icell_raster_pkg = TRUE
)
Arguments
- filename
Complete path to .nc file.
- variable
Character, name of the variable to extract (required).
- var_t
Character, name of the time variable. (recommended if has_time=TRUE)
- var_x
(optional) Character, name of the x coordinate variable (not dimension!). If both var_x and var_y are supplied, results will have xy columns, too.
- var_y
(optional) Character, name of the y coordinate variable (not dimension!). If both var_x and var_y are supplied, results will have xy columns, too.
- has_time
Boolean, if
TRUE
(default), will read time information from file. UseFALSE
, if file has no time dimension/variable.- icell_raster_pkg
Boolean, if
TRUE
, cell indices will be ordered as if you were extracting the data with the raster package. See alsonc_grid_to_dt
Value
A data.table
with columns icell (cell
index), time (if has_time=T), value. Only if var_x and var_y supplied also
x and y. Column names except icell are variable names as in the netcdf
file.
Details
Names of netcdf variables can be inquired in a terminal with "ncdump -h", or
in R for example using ncdf4::nc_open(filename)
.
Example use cases: netcdf file has issues with dimensions (no dimvar), time variable is in non-standard format.
Examples
# example data
fn3 <- system.file("extdata", "test3.nc", package = "eurocordexr")
dat <- nc_grid_to_dt_raw(fn3, "tasmax", "Times")
str(dat)
#> Classes ‘data.table’ and 'data.frame': 51660 obs. of 3 variables:
#> $ icell : int 821 822 823 824 825 826 827 828 829 830 ...
#> $ Times : num 19790702 19790702 19790702 19790702 19790702 ...
#> $ tasmax: num 0.7883 0.6704 0.4309 0.0592 -0.1017 ...
#> - attr(*, ".internal.selfref")=<externalptr>