[R-sig-Geo] writeRaster does not preserve names when writing to NetCDF
Michael Sumner
mdsumner at gmail.com
Tue Aug 12 01:05:42 CEST 2014
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.
If you have a time series of RGB images, the three attributes Red,
Green and Blue do not belong in the 3rd dimension. In practice we see
separate files for a time series of multi-attribute "images". If you
explore the sp gridded and raster gridded data structures they are
stored as a 2D array with the first two dimensions of the grid
"flattened" down the columns. This raster brick image uses the 2nd
dimension of the storage for multiple attributes, but in another case
(time series SST) it uses this dimension for the 3rd dimension of the
grid.
That leaves no wriggle room for consistent extension to higher
dimensions, or even 3D-multiple-attribute data. It's not just for
colour images, the same problem exists for vector/velocity data
(storing horizontal and vertical components of velocity) and NetCDF
shares the limiation. Strictly I would say you store the Red, Green
and Blue data as separate variables in NetCDF as it would for time
series current velocity but still you need extra work applied to get
the "obvious" interpretation.
I think the only easy solution is to use raster's own native format.
I've seen discussions on this for R and for GDAL, which I'll include
if I can find them again. GDAL would store these names in its
auxiliary files, since many formats just don't have the ability to do
it. It's the same for mixing data types - sp's classes can do this as
it is a data.frame, but raster cannot (it's a matrix), and some GDAL
formats can but it's pretty obscure and generally frowned upon I
think.
Cheers, Mike.
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
More information about the R-sig-Geo
mailing list