[R-sig-Geo] writeRaster does not preserve names when writing to NetCDF
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Tue Aug 12 17:26:50 CEST 2014
This chunk of code writes a character vector to a file. It needs two
dimensions, one that has to be big enough for the longest character
string, and one which has to be the length of the vector.
writeChars <- function(fn){
nameDim = dim.def.ncdf("NamesDim","", 1:3)
charDim = dim.def.ncdf("CharsDim","", 1:10)
nameVar = var.def.ncdf("Names","",list(charDim,nameDim),"NA")
nc = create.ncdf(fn, list(nameVar))
v = c("red ","green","blue ")
for(i in 1:length(v)){
put.var.ncdf(nc, nameVar, v[i], start=c(1,i), count=c(-1,1))
}
close.ncdf(nc)
}
Dumping the NetCDF gives:
data:
CharsDim = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
NamesDim = 1, 2, 3 ;
Names =
"red ",
"green",
"blue " ;
}
Whether this is a good idea or not... Hmmmm....
I've noticed if you use `writeRaster` with format="CDF" you get the
data in one variable called "value", but if you use format="netCDF"
you get one named variable for each layer. I'm pretty sure these names
could be set and recovered, but currently they seem to be called Band1
etc. I think thats the GDAL driver at work:
"""
This driver supports creation of netCDF file following the CF-1
convention. You may create set of 2D
datasets. Each variable array is named Band1, Band2, ... BandN.
""" http://www.gdal.org/frmt_netcdf.html
Also, this section in writeRaster:
NetCDF files have the following additional, optional,
arguments: ‘varname’, ‘varunit’, ‘longname’, ‘xname’,
‘yname’, ‘zname’, ‘zunit’
only seems to apply to format="CDF" and not "NetCDF", which seems to
ignore at least the ones I've tried. The "NetCDF" format seems to be
done via GDAL.
Is it worth putting any effort into this? I mean, is it worth *Robert*
putting any effort into this?
Barry
More information about the R-sig-Geo
mailing list