Extract the nearest valid depth layer from a 3D netCDF variable
Source:R/extract_to_points.R
extract3d_nearest.RdFor each observation, this function reads the full vertical profile and
finds the valid, non-missing netCDF depth layer closest to the observation
depth. This avoids returning NA when the geometrically closest depth layer
is invalid at that grid cell, for example below the seabed.
Arguments
- data
Object containing observation points. Can be a data frame, tibble,
sfobject with POINT geometries, matrix with column names, or named list.- nc
netCDF source. Can be a file path, vector of file paths, list of file paths, opened
ncdf4object, list of opened objects, or a data frame with a file column.- var
Name of the variable to extract from the netCDF file. If
NULL, the function tries to detect the variable automatically from each file. This only works when each netCDF file contains one single variable.- ...
Additional arguments passed to
extract_to_point().
Value
The input observations with extracted values appended. Data frames,
tibbles and sf objects retain their structure; matrices and named lists
are returned as data frames.
Examples
nc_file <- system.file(
"extdata", "example_3d.nc",
package = "ocean3d"
)
observations <- data.frame(
lon = c(0, 1),
lat = c(40, 41),
depth = c(60, 100),
date = as.Date(c("2020-01-01", "2020-01-03"))
)
extract3d_nearest(
data = observations,
nc = nc_file,
var = "temp"
)
#> lon lat depth date temp
#> 1 0 40 60 2020-01-01 15.0
#> 2 1 41 100 2020-01-03 13.5