R functions to extract environmental values from 2D and 3D netCDF files to point-based observations, supporting surface, bottom, nearest-depth and combined extraction workflows.
-
Extract values from:
- 2D netCDF variables
- 3D netCDF variables
- 4D netCDF variables including time
-
Extraction methods:
- Surface layer
- Bottom available layer
- Nearest valid depth layer
- Combined extraction
-
Supports:
- Single netCDF files
- Multiple netCDF files
- Lists of netCDF files
- Opened
ncdf4objects - Data frames containing file paths
-
Automatically:
- Detects dimension names
- Handles different longitude conventions
- Reads netCDF time metadata
- Matches nearest spatial and temporal cells
- Detects variables automatically when
var = NULL
| Function | Description |
|---|---|
extract2d() |
Extract values from 2D netCDF variables |
extract3d_surface() |
Extract the surface layer |
extract3d_bottom() |
Extract the deepest valid layer |
extract3d_nearest() |
Extract the nearest valid depth layer |
extract3d_all() |
Extract combined surface, nearest and bottom values |
extract_netcdf() |
General extraction interface |
Currently under development.
Clone the repository:
git clone https://github.com/your_username/extractNetCDF.gitOr source the functions directly:
source("extract_netcdf_package.R")ncdf4Optional:
dplyrextract3d_nearest() extracts the closest valid non-missing depth layer available in the vertical profile.
This behaviour is important in oceanographic products where some depth layers may be missing due to bathymetry.
extract3d_bottom() extracts the deepest non-missing value available in the profile rather than simply the deepest nominal depth layer.
If var = NULL, the package automatically detects the variable stored in the netCDF file.
This works when the file contains a single environmental variable.
If a netCDF file contains several variables, the user must specify var manually.
Observation tables should contain at minimum:
| Column | Required |
|---|---|
| Longitude | Yes |
| Latitude | Yes |
| Date | Only if netCDF contains time |
| Depth | Only for nearest-depth extraction |
The nc argument accepts:
# One file
nc = "file.nc"
# Several files
nc = c("file1.nc", "file2.nc")
# List of files
nc = list("file1.nc", "file2.nc")
# Opened ncdf4 object
nc = ncdf4::nc_open("file.nc")
# List of opened objects
nc = list(nc1, nc2)
# Data frame with file column
nc = data.frame(file = c("file1.nc", "file2.nc"))Potential future extensions include:
- Parallel extraction
- Polygon-based extraction
- Interpolation methods
David Ruiz García
MIT License