writeCDF.Rd
Write a SpatRaster or SpatRasterDataset to a NetCDF file.
When using a SpatRasterDataset, the varname, longname, and unit should be set in the object (see examples).
Always use the ".nc"
or ".cdf"
file extension to assure that the file can be properly read again by GDAL
# S4 method for SpatRaster
writeCDF(x, filename, varname, longname="", unit="", split=FALSE, ...)
# S4 method for SpatRasterDataset
writeCDF(x, filename, overwrite=FALSE, zname="time", atts="",
gridmap="", prec="float", compression=NA, missval, ...)
SpatRaster or SpatRasterDataset
character. Output filename
character. Name of the dataset
character. Long name of the dataset
character. Unit of the data
logical. If TRUE
each layer of x
is treated as a sub-dataset
character. A vector of additional global attributes to write. The must be formatted like c("x=a value", "y=abc")
character. The crs is always writting to the file in standard formats. With this argument you can also write the format commonly used in netcdf files. Something like c("grid_mapping_name=lambert_azimuthal_equal_area", "longitude_of_projection_origin=10", "latitude_of_projection_origin=52", "false_easting=4321000", "false_northing=3210000")
logical. If TRUE
, filename
is overwritten
character. The name of the "time" dimension
character. One of "double", "float", "integer", "short", "byte" or "char"
Can be set to an integer between 1 (least compression) and 9 (most compression)
numeric, the number used to indicate missing values
additional arguments passed on to the SpatRasterDataset method, and from there possibly to ncvar_def
SpatRaster or SpatDataSet
see writeRaster
for writing other file formats
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
fname <- paste0(tempfile(), ".nc")
#rr <- writeCDF(r, fname, overwrite=TRUE, varname="alt",
# longname="elevation in m above sea level", unit="m")
a <- rast(ncols=5, nrows=5, nl=50)
values(a) <- 1:prod(dim(a))
time(a) <- as.Date("2020-12-31") + 1:nlyr(a)
#aa <- writeCDF(a, fname, overwrite=TRUE, varname="power",
# longname="my nice data", unit="U/Pa")
b <- sqrt(a)
s <- sds(a, b)
names(s) <- c("temp", "prec")
longnames(s) <- c("temperature (C)", "precipitation (mm)")
units(s) <- c("C", "mm")
#ss <- writeCDF(s, fname, overwrite=TRUE)
# for CRAN
#file.remove(fname)