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

Thiago V. dos Santos thi_veloso at yahoo.com.br
Thu Jun 4 07:24:45 CEST 2015


Is there any news on this topic?
I am facing the same problems trying to preserve layer names (usually dates) when writing a netcdf file. This piece of information is critical when working with temporal series in netcdf files.
Perhaps a solution would be adding a "zname" argument to writeRaster (as Mark Payne suggested) which would use ncdf4's ncatt_put as a wrapper (as per from Dominik's suggestion ).
Would this be feasible to incorporate in the writeRaster function?

I am quite sure it can be done with ncdf4 but it would involve perhaps a dozen lines of code.  
Greetings,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323 9898 


     On Friday, February 6, 2015 11:34 AM, dschneiderch <Dominik.Schneider at colorado.edu> wrote:
   

 I was running into this problem and wondering if there was any progress?
As a workaround, I was trying to add an attribute to my variable using
ncput_att but it doesn't show more than 1 (no warning was shown that only 1
could be accepted and the min/max attributes have a value for each layer).

r=raster(matrix(runif(9),nrow=3))
r2=raster(matrix(runif(9),nrow=3))
s=stack(r,r2)
names(s)=c('X20120101','X20120108')

writeRaster(s,filename='~/Desktop/test.nc', format='CDF', varname='swe',
varunit='meters', xname='long', xunit='deg', yname='lat', yunit='deg',
zname='time', zunit='day', overwrite=T, NAflag=-99)

#Note the names of the written stack are generic.
#Now I try to add an attribute to the swe variable in the file so I can
retrieve later but it only lists the first name
n=nc_open('~/Desktop/test.nc',write=T)
ncatt_put(n,varid='swe',attname='dates',attval=names(s),prec='text')
nc_close(n)

nc_open('~/Desktop/test.nc',write=F)
File ~/Desktop/test.nc (NC_FORMAT_CLASSIC):

    1 variables (excluding dimension variables):
        float swe[long,lat,time]  
            units: meters
            _FillValue: -99
            missing_value: -99
            long_name: swe
            min: 0.0572868520393968
            min: 0.1845921901986
            max: 0.852122258627787
            max: 0.772868746891618
            dates: X20120101

    3 dimensions:
        long  Size:3
            units: degrees_east
            long_name: long
        lat  Size:3
            units: degrees_north
            long_name: lat
        time  Size:2  *** is unlimited ***
            units: day
            long_name: time

    3 global attributes:
        Conventions: CF-1.4
        created_by: R, packages ncdf and raster (version 2.3-24)
        date: 2015-02-06 10:18:51


Could that be a bug in ncatt_put? Or, are there any suggestions for storing
irregular spatial-temporal data (consistent spatial grid, but inconsistent
time slices that therefore require some annotation about the date)
 



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/writeRaster-does-not-preserve-names-when-writing-to-NetCDF-tp7586909p7587744.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


  
	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list