Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

writeCDF

Write raster data to a NetCDF file


Description

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

Usage

## S4 method for signature 'SpatRaster'
writeCDF(x, filename, varname, longname="", unit="", ...)

## S4 method for signature 'SpatRasterDataset'
writeCDF(x, filename, overwrite=FALSE, zname="time",
    missval=-9999, prec="float", compression=NA, ...)

Arguments

x

SpatRaster or SpatRasterDataset

filename

character. Output filename

varname

character. Name of the dataset

longname

character. Long name of the dataset

unit

character. Unit of the data

overwrite

logical. If TRUE, filename is overwritten

zname

character. The name of the "time" dimension

missval

numeric, the number used to indicate missing values

prec

character. One of "double", "float", "integer", "short", "byte" or "char"

compression

Can be set to an integer between 1 (least compression) and 9 (most compression)

...

additional arguments passed on to ncvar_def

Value

SpatRaster or SpatDataSet

See Also

see writeRaster for writing other file formats

Examples

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(ncol=5, nrow=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)", "precicpiation (mm)")
units(s) <- c("C", "mm")
ss <- writeCDF(s, fname, overwrite=TRUE)

# for CRAN
file.remove(fname)

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.