[R-sig-Geo] writeRaster does not preserve names when writing to NetCDF

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Aug 12 09:53:49 CEST 2014


On Tue, Aug 12, 2014 at 12:05 AM, Michael Sumner <mdsumner at gmail.com> wrote:
> Hi,
>
> I don't think that NetCDF has the capacity to store these names in a
> natural way. Internally your brick is stored as a NetCDF variable, in
> this case a 3d array. To do this it would need to define a 'dimension"
> and include a lookup map to the names. A NetCDF dimension variable
> must be populated with numbers I believe, so you would need another
> variable to store those and then add in features to raster to use
> them.
>
> There's at least two blockers here:
> - the NetCDF model is not designed for storing tabular data (a RGB
> image is a table of records in one sense, not a 3d array of integers)
> - gridded data storage usually conflates "dimension" with
> "attributes", which is unfortunate.

 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





>
>
>
> On Tue, Aug 12, 2014 at 12:26 AM, Mark Payne <markpayneatwork at gmail.com> wrote:
>> Hi,
>>
>> I have a problem with writeRaster when writing a brick to NetCDF - the
>> names of the layers are not preserved. Here is a minimum example to
>> demonstrate the point:
>>
>> #Create an arbitrary brick and write it out
>> b <- brick(system.file("external/rlogo.grd", package="raster"))
>> fname <- file.path( tempdir(),"test.nc")
>> dmp <- writeRaster(b,file=fname)
>>
>> #Now read it back in
>> a <- brick(fname)
>>
>> #However, names are not preserved
>>> names(b)
>> [1] "red"   "green" "blue"
>>> names(a)
>> [1] "X1" "X2" "X3"
>>>
>>
>> I realise this is a touch tricky, as NetCDF isn't exactly made for storing
>> character strings, which is how raster stores the layer names. However,
>> there are many cases where the string is just a representation of a number
>> anyway.... e.g. when working with time or depth levels as the layers in the
>> brick. One solution could therefore be to allow the writeRaster function to
>> take an argument "zvals", in the same way that it takes "zname" and "zunit"
>> when working with netcdf files - this argument would let you specify the
>> numeric values of the z-axis. Everything else would then work ok I think,
>> as brick() picks up the values of the z-axis, converts them to strings and
>> assigns them to layer names.
>>
>> Cheers,
>>
>> Mark
>>
>> raster version: 2.2-31
>> R version 3.0.3
>> ncdf4 version: 1.10
>> Platform: Linux Mint 16, 64 bit
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> --
> Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> Hobart, Australia
> e-mail: mdsumner at gmail.com
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list