Skip to content
Prev 21489 / 29559 Next

writeRaster does not preserve names when writing to NetCDF

On Tue, Aug 12, 2014 at 12:05 AM, Michael Sumner <mdsumner at gmail.com> wrote:
Ummmm....

 NetCDF can store just about anything. Its that flexibility that makes
it hard for one piece of software that writes a raster brick totally
compatible with another piece of software that writes a raster brick.
The NetCDF format itself has no convention for how to store it.

 So people have developed conventions!

http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html
http://www.unidata.ucar.edu/software/netcdf/conventions.html
http://cfconventions.org/Data/cf-convetions/cf-conventions-1.7/build/cf-conventions-multi.html

[yes there's a typo in that last one - "convetions" - that's how it is.]

 You can see which convention a NetCDF file claims to be by dumping it
with ncdump on the command line:

 // global attributes:
        :Conventions = "CF-1.4" ;
        :created_by = "R, packages ncdf and raster (version 2.1-49)" ;
        :date = "2014-08-11 17:17:01" ;

[note this also dumps the data, you might want to pipe it to | more]

The exact convention used by raster and rgdal seems to vary depending
on whether ncdf is loaded when raster saves it, or something. At least
in my blurry morning fumblings I seem to have reached contradictory
examples which need clearing up once the coffee IV hits. The varname
argument to writeRaster doesn't seem to do anything, but I'm probably
looking at the wrong file or have an old package:raster or something.

Anyway, the two main ways of storing multi-layer raster appear to be,
and these are files with CF-1.4/5 convention markers, as a 3d cube
with dimension (Nx,Ny,Nz) or as a set of 2d named variables. The names
of these 2d variables should be settable at write time and recoverable
at read-time. However, If the data is a 3d cube then I don't think the
dimensions can have names, although I've not really understood the CF
standards yet (and that page has links to some awful documentation and
missing links...).

It would be quite simple to add another variable, of characters, to
the NetCDF file, but I suspect keeping to a given NetCDF Convention
would be a good idea assuming it can do it.

End-of-ramble...

Barry