I want to write compressed netCDF4 files using the raster
package.
ncdf4
and rgdal
are installed correctly.
The following, however, fails to return a netCDF4 compressed file:
library(raster)
r <- raster() <- runif(86400)
r[] <- <- runif(86400)
writeRaster(r, "test.nc", options=c("COMPRESS=DEFLATE", "FORMAT=NC4"))
In fact it just returns a standard netCDF file:
bash $ > cdo sinfo test.nc
File format : netCDF
Not only uncompressed, but not even netCDF4.
According to the GDAL documentation, these options should work (see here, under "Creation options").
According to the raster manual, under writeRaster
:
options: Character. File format specific GDAL options. E.g., when writing a geotiff file you can use: options=c("COMPRESS=NONE", "TFW=YES")
What could be wrong?
ncdf files are not written via GDAL because the rgdal package (at least the binary version on windows) does not come with the ncdf driver. Instead, writeRaster uses package ncdf
or (preferably) ncdf4
, so you would have to use arguments provided by the ncdf4 package (in the ncvar_def
function). That is, you would do e.g., compression=7
, which would automatically turn the file into ncdf4 format. However, the current version of raster
would ignore these arguments. I have fixed that in the development version on R-Forge (version 2.4-7), such that you can do:
writeRaster(r, "test.nc", datatype='INT2S', force_v4=TRUE, compression=7)
(The force_v4
argument is passed to nc_create
).