Skip to contents

Extraction of point like values from a data.table read with nc_get_variable.

Usage

nc_extract(
  mydata,
  name.x = "x",
  name.y = "y",
  name.z = NULL,
  name.time = "time",
  name.value = NULL,
  stations = NULL,
  st.x = "x",
  st.y = "y",
  st.z = NULL,
  st.code = "code",
  st.time = NULL,
  st.value = NULL,
  method = "simple",
  type = "horizontal",
  ncores = NULL
)

Arguments

mydata

A data.frame or data.table representing field values at x andy points of a grid. Column names must be specified with the following arguments. Optionally z and time can be provided.

name.x

Character vector with the name of the column representing x (default is "x").

name.y

Character vector with the name of the column representing y (default is "y").

name.z

Optional character vector with the name of the column representing z (default is "z").

name.time

Optional character vector with the name of the column representing time (default is "time").

name.value

Character vector with the name of the column with the field values to be extracted.

stations

A data.frame or data.table with the x and y coordinate where extraction has to be performed. It can optionally include a code label of the points (stations) and values measured at the points. If date time of measurements and values at the points are provided through st.time and st.value columns the function returns both extracted field values and observed data at the given points. This is useful to perform data validation.

st.x

Character vector with the name x column coordinates for the stations (default is "x").

st.y

Character vector with the name y column coordinates for the stations (default is "y").

st.z

Optional character vector with the name of the column with z coordinate for the stations (default is "z").

st.code

Character vector with the name of code column for the stations (default is "code"). It can be either a number or string labelling the given point.

st.time

Optional character vector with the name of the column with observed deadlines. This is required for automatically merging field data with date time specified in name.time.

st.value

Optional character vector with the name of the column with observed data. If st.value is present, values are merged in the output data.

method

Character vector to specify extraction method. It can be either 'simple' or 'bilinear'. If 'simple' values for the cell a point falls in are returned. If 'bilinear' the returned values are interpolated from the values of the four nearest raster cells.

type

A character string indicating the type of extraction. One of "horizontal" (default) or "vertical" (Note: "vertical" is not yet implemented.)

ncores

Integer to set the number of cores in parallel calculation. If missing it is set to 25% of available cores.

Value

A data.table with x, y, time, value and code columns.

Details

An input data.table is provided through mydata argument. A list of points where extraction has to be performed, is provided through the stations argument. Extraction is performed through the terra::extract function and two options are available: simple for nearest neighbour and bilinear for interpoloation of the four nearest raster cells.

Values in the stations data.table provided through the optional argument st.value, are automatically merged with the extracted values.

Examples

if (FALSE) {
# Read observed data from BRACE database:
stnsER <- import_meta_brace("~/data/BRACE", "NO2", 2015)
stnsER <- stnsER[regione == "Emilia-Romagna", ]

# Read NO2 model data from NetCDF archive
vals <- nc_get_variable("~/extdata/BO_conc_2015_hourly_07.nc",
                        variable = "c_NO2", tz = "Etc/GMT-1")
# Convert x and y coordinates from km to m
vals[, `:=`(x = x * 1000, y = y * 1000)]

# Extract temperature at sites included in the stnsER dataset.
extraction <- nc_extract(vals,
                         name.value = "c_NO2",
                         name.x = "x", name.y = "y", name.time = "time",
                         stations = stnsER,
                         st.value = "obs",
                         st.x = "x", st.y = "y", st.time = "datetime",
                         st.code = "cod_brace",
                         method = "simple")
}