This function reads a set of NetCDF files (one per variable) and extracts data for a set of NetCDF cells that are specified using a boundary box (in lon/lat format) or a set of (x,y) grid indices.

extractNetCDF(ncdf_files, bbox = NULL, offset = 0, cells = NULL, export = TRUE,
exportDir = getwd(), exportFormat = "meteoland/txt", mpfilename = "MP.txt")

## Arguments

ncdf_files

Character vector containing files to read

bbox

Boundary box (2 x 2 matrix) specifying the limit coordinates of a study area (in lon/lat format).

offset

A buffer to include NetCDF cells that are at a certain distance around the boundary box.

cells

A (n x 2) matrix specifying the x and y indices of n cells in a grid.

export

If export = FALSE the extracted data is stored in memory. Otherwise the result is written in the disk (using the format specified in exportFormat).

exportFormat

Export format for meteorological data (see writemeteorologypoint).

exportDir

Output directory for extracted meteorology.

mpfilename

The name of the file that will store the meta data describing all written files.

## Details

Function extractNetCDF first identifies which cells in NetCDF data should be extracted according to bbox (or the cells are indicated by the user using cells), and the overall period (days). If neither bbox or cells is supplied, then all NetCDF cells will be processed. For each cell to be processed, the function loops over all files (which can describe different variables and time periods) and extracts the corresponding data. The function transforms units to the units used in meteoland. If specific humidity and mean temperature are available, the function calculates mean relative humidity.

Extracted meteorological data (a data frame with days in rows and meteorological variables in columns) can be stored in an object SpatialPointsMeteorology-class or it can be written in the disk (one file per cell). In the latter case, the output format can be chosen and the function also writes a supplementary file containing the meta data (i.e. the coordinates and filename of each file).

Humidity in climate model files is given as specific humidity. This is converted to relative humidity and the conversion may produce values above saturation (>100%) (see also defaultCorrectionParams for the same issue when performing bias correction).

## Value

If export = FALSE, the function returns an object of class SpatialPointsMeteorology-class with the meteorological series for each cell (represented by a spatial point). Otherwise the function returns an object of class SpatialPointsDataFrame-class containing the meta data of the files written in the disk.

## Author

Miquel De Cáceres Ainsa, CREAF

Nicolas Martin, INRA-Avignon

correctionpoints, writemeteorologypointfiles, SpatialPointsMeteorology-class