[R-sig-Geo] SpatialGridDataFrame to netCDF via ncdf on Windows

Robert J. Hijmans r.hijmans at gmail.com
Fri Jul 23 21:31:26 CEST 2010


Andy, this should work (it requires RNetCDF) :


sp = cbind(t1.sp, t2.sp)

library(raster)

b <- brick(sp)
nc = writeRaster(b, file='test.nc')



Robert


On Fri, Jul 23, 2010 at 12:17 PM, Andy Bunn <Andy.Bunn at wwu.edu> wrote:
> Hello, I have a series of a few dozen SpatialGridDataFrame objects that I would like to write as a single netCDF object - i.e., a 2D field (x,y) as a function of time (t) containing one time-varying variable (z). Given the example below I'd like to create one netCDF object containing t1 and t2. I do not have a netCDF driver in my gdalDrivers() list. Can this be done via ncdf?
>
> Thanks, Andy
>
>
>
> # example
> require(sp);require(rgdal);require(ncdf)
>
> nlong <- 20
> long.size <- 1
> long.min <- 0
> long <- seq(long.min+long.size/2,long.min+long.size*nlong,by=long.size)
>
> nlat <- 20
> lat.size <- 1
> lat.min <- 45
> lat <- seq(lat.min+lat.size/2,lat.min+lat.size*nlat,by=lat.size)
> prj.str <- 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
>
> t1 <- rnorm(nlong*nlat)
> t1.sp = data.frame(z = t1,
>    xc = rep(long,times=nlat),
>    yc = rep(lat,each=nlong))
> coordinates(t1.sp) = ~xc+yc
> proj4string(t1.sp)<- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
> gridded(t1.sp) = TRUE
> t1.sp = as(t1.sp, "SpatialGridDataFrame")
>
> t2 <- rnorm(nlong*nlat)
> t2.sp = data.frame(z = t2,
>    xc = rep(long,times=nlat),
>    yc = rep(lat,each=nlong))
> coordinates(t2.sp) = ~xc+yc
> proj4string(t2.sp)<- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
> gridded(t2.sp) = TRUE
> t2.sp = as(t2.sp, "SpatialGridDataFrame")
>
> # write netCDF: 2D field (x,y) as a function of time (t) containing one time-varying variable (z)
> # ??
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list