writeNetCDF-methods: writeNetCDF methods

writeNetCDF-methodsR Documentation

writeNetCDF methods

Description

Methods for writing DGVMTools::Field and raster::Raster* objects to disk as netCDF files. Uses the ncdf4 R-package behind the scenes. NOTE: when using the function on a raster::Raster object, you cannot provide both multiple variables and multiple time periods, you can only do one or the other.

Usage

writeNetCDF(
  x,
  filename,
  start.date = NULL,
  verbose = TRUE,
  quantity = NULL,
  source = NULL,
  layer.names = NULL,
  layer.dim.name = NULL,
  layer.dim.values = NULL,
  lat.dim.name = "lat",
  lon.dim.name = "lon",
  time.dim.name = "time",
  time.values = NULL,
  calendar = "365_day",
  global.extent = FALSE,
  add.missing.cols = TRUE,
  add.missing.rows = TRUE,
  nc.verbose = FALSE,
  .sta.info = NULL,
  ...
)

## S4 method for signature 'Field,character'
writeNetCDF(
  x,
  filename,
  start.date = NULL,
  verbose = TRUE,
  quantity = NULL,
  source = NULL,
  layer.names = NULL,
  layer.dim.name = NULL,
  layer.dim.values = NULL,
  lat.dim.name = "lat",
  lon.dim.name = "lon",
  time.dim.name = "time",
  time.values = NULL,
  calendar = "365_day",
  global.extent = FALSE,
  add.missing.cols = TRUE,
  add.missing.rows = TRUE,
  nc.verbose = FALSE,
  .sta.info = NULL,
  ...
)

## S4 method for signature 'Raster,character'
writeNetCDF(
  x,
  filename,
  start.date = NULL,
  verbose = TRUE,
  quantity = NULL,
  source = NULL,
  layer.names = NULL,
  layer.dim.name = NULL,
  layer.dim.values = NULL,
  lat.dim.name = "lat",
  lon.dim.name = "lon",
  time.dim.name = "time",
  time.values = NULL,
  calendar = "365_day",
  global.extent = FALSE,
  add.missing.cols = TRUE,
  add.missing.rows = TRUE,
  nc.verbose = FALSE,
  .sta.info = NULL,
  ...
)

## S4 method for signature 'SpatRaster,character'
writeNetCDF(
  x,
  filename,
  start.date = NULL,
  verbose = TRUE,
  quantity = NULL,
  source = NULL,
  layer.names = NULL,
  layer.dim.name = NULL,
  layer.dim.values = NULL,
  lat.dim.name = "lat",
  lon.dim.name = "lon",
  time.dim.name = "time",
  time.values = NULL,
  calendar = "365_day",
  global.extent = FALSE,
  add.missing.cols = TRUE,
  add.missing.rows = TRUE,
  nc.verbose = FALSE,
  .sta.info = NULL,
  ...
)

## S4 method for signature 'list,character'
writeNetCDF(
  x,
  filename,
  start.date = NULL,
  verbose = TRUE,
  quantity = NULL,
  source = NULL,
  layer.names = NULL,
  layer.dim.name = NULL,
  layer.dim.values = NULL,
  lat.dim.name = "lat",
  lon.dim.name = "lon",
  time.dim.name = "time",
  time.values = NULL,
  calendar = "365_day",
  global.extent = FALSE,
  add.missing.cols = TRUE,
  add.missing.rows = TRUE,
  nc.verbose = FALSE,
  .sta.info = NULL,
  ...
)

Arguments

x

A Field or Raster* object to be written as a netCDF file. The method can also handle a list of arrays (each array in the list represents one layer) but this is more of a technical, internal use of this method.

filename

A character specifying the name of the netCDF file to be written.

start.date

A date (as a Date object) for the start of the time dimension. Can be omitted for a Field, must be included if you want to write a RasterStack or RasterBrick with a time dimension.

verbose

Logical, if TRUE print progress and debug output (from DGVMTools not from netCDF)

quantity

A DGVMTools::Quantity object. This is to provide meta-data (units, names) if saving a Raster* object, it is ignored in the case of a Field (but note that if you want to use different meta-data for a Field just alter the Quantity object in the Field object before you callwriteNetCDF)

source

A DGVMTools::Source object. This is to provide meta-data about the source of the data (model run/dataset, climate forcing, contact person etc) if saving a Raster* object. It is ignored in the case of a Field (but note that if you want to use different meta-data for a Field just alter the Source object in the Field object before you call writeNetCDF)

layer.names

A vector of characters to specify the names of the variables in the netCDF file, ignored for a Field. Should be length 1 or the number of layers in the input raster.

layer.dim.name

A character string specifying the name of the fourth 'layers' dimension (ie not lon, lat or time). If not specified (or NULL) then no fourth dimension is created and each layer gets its own variable in the netCDF. If it is specified, the layers are 'collapsed' on to elements on this fourth, layers, dimension.

layer.dim.values

A named vector of numerics which will be used if writing a 'layer' dimension. The number corresponds to the value on the dimension, the name corresponds to the layer name. Can be omitted in which case every layer will be written in the order they appear in the Field.

lat.dim.name

Character, the latitude dimension name. Defaults to "lat".

lon.dim.name

Character, the longitude dimension name. Defaults to "lon".

time.dim.name

Character, the time dimension name. Defaults to "time".

time.values

The values along the time dimension (in the case of Raster*, is ignored for a Field) defined as 'days since' the 'start.date' argument. If not supplied, data is assumed not to have a time axis. In such case if the raster has multiple layers they will be interpreted

calendar

Character, describing the calendar to be used (required for a Raster*, optional for a Field), can currently be "standard" or "365_day" (default),

global.extent

logical, if TRUE extend the netCDF file to the entire global extent, default is FALSE.

add.missing.cols, add.missing.rows

Logical, if TRUE (default), fill missing columns of longitude/rows of latitude. For guassian/spectral grids with non-equal latitude spacing, set add.missing.rows to FALSE.

nc.verbose

Logical, if TRUE print progress and debug output from the NetCDF function calls. This can be rather a lot, so is controlled separately to the DGVMTools output.

.sta.info

Advanced. An object of STAInfo for tracking spatio-temporal metadata. IGNORED for for a Field as it is handled automatically when writing a Field. But can be added manually for a raster if you wish.

...

Other arguments that can usefully be passed to ncdf4::ncvar_def, in particular:

  • compression Integer to define compression level when define netCDF variables (1 = a little compression, 9 = a lot of compression). Set to NA (default) for no compression. Using compression forces netCDF version 4.

  • missval Numeric, for the missing value. Default is NA which gives NaN as the missing value. NULL can be used to specify "no missing value" although it is not clear what that does in the resultant netcdf file in practice. TRENDY/GCP likes -99999.0 for missing values.

  • prec Character, the output precision (although that is confusing terminology, 'type' would be more descriptive) to use in the netCDF file. See the 'prec' argument of ncdf4::ncvar_def, can be 'short', 'integer', 'float', 'double', 'char', 'byte'). Default is 'float'.

  • shuffle Logical, if TRUE turn on the shuffle filter see netCDF docs and ncdf4::ncvar_def for details

  • chunksize If set, this must be a vector of integers with a length equal to the number of dimensions in the variable. Potentially very useful to optimise the read and write time, but rather advanced, see netCDF docs and ncdf4::ncvar_def for details

These methods offers two very convenient things. Firstly, it allows the exporting of a DGVMTools::Field object as a standard netCDF (CF) file. Secondly, it provides a more convenient way to write Raster* objects as netCDF objects than writeRaster (at least in the eye's of the author). This because is allows specification a time axis, writes additional metadata, allows some flexibility in the formatting, doesn't invert the latitudes, and should generally write CF-standard compliant files.

Value

Nothing

Author(s)

Matthew Forrest matthew.forrest@senckenberg.de


MagicForrest/DGVMTools documentation built on Aug. 23, 2024, 8:05 a.m.