[Raster-commits] r476 - in pkg/raster: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 5 01:45:35 CEST 2009


Author: rhijmans
Date: 2009-09-05 01:45:34 +0200 (Sat, 05 Sep 2009)
New Revision: 476

Added:
   pkg/raster/man/rasterCDF.Rd
Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/R/raster.R
   pkg/raster/R/rasterFromNetCDF.R
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-09-03 22:14:26 UTC (rev 475)
+++ pkg/raster/DESCRIPTION	2009-09-04 23:45:34 UTC (rev 476)
@@ -1,7 +1,7 @@
 Package: raster
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
-Version: 0.9.2
+Version: 0.9.3
 Date: 2-September-2009
 Depends: methods, sp, R (>= 2.8.0)
 Suggests: rgdal (>= 0.5-33), RNetCDF

Modified: pkg/raster/R/raster.R
===================================================================
--- pkg/raster/R/raster.R	2009-09-03 22:14:26 UTC (rev 475)
+++ pkg/raster/R/raster.R	2009-09-04 23:45:34 UTC (rev 476)
@@ -47,7 +47,7 @@
 						w <- readBin(fcon, what='character', n=1)
 						close(fcon)
 						if (substr(w, 1, 3) == "CDF") { 
-							r <- .rasterFromNetCDF(x, ...) 
+							r <- rasterCDF(x, ...) 
 						} else {
 						# perhaps a surfer grid...
 							r <- .rasterFromGDAL(x, band) 
@@ -61,6 +61,9 @@
 				r <- .rasterFromGDAL(x, band) 
 			}
 		} else if (file.exists( x )){
+		    if (fileext == '.NC') {
+				r <- rasterCDF(x, ...) 
+			}
 			r <- .rasterFromGDAL(x, band) 
 		} else {
 			stop(paste('file', x, 'does not exist'))

Modified: pkg/raster/R/rasterFromNetCDF.R
===================================================================
--- pkg/raster/R/rasterFromNetCDF.R	2009-09-03 22:14:26 UTC (rev 475)
+++ pkg/raster/R/rasterFromNetCDF.R	2009-09-04 23:45:34 UTC (rev 476)
@@ -4,61 +4,110 @@
 # Licence GPL v3
 
 
-.rasterFromNetCDF <- function(x, band, xvar='', yvar='', zvar='') {
+.getxvar <- function(xvar, vars) {
+	if (xvar == '') {
+		if ('x' %in% vars) { xvar <- 'x'
+		} else if ('lon' %in% vars) { xvar <- 'lon' 
+		} else if ('long' %in% vars) { xvar <- 'long' 
+		} else if ('longitude' %in% vars) { xvar <- 'longitude' 
+		} else { stop('cannot find xvar in file') 
+		}
+	} else if (!(xvar %in% vars)) { stop('cannot find xvar in file') }	
+	return(xvar)
+}
+
+.getyvar <- function(yvar, vars) {
+	if (yvar == '') { if ('y' %in% vars){ yvar <- 'y'
+		} else if ('lat' %in% vars) { yvar <- 'lat' 
+		} else if ('latitude' %in% vars) { yvar <- 'latitude' 
+		} else { stop('cannot find yvar in file') 
+		}
+	} else if (!(yvar %in% vars)) { stop('cannot find yvar in file') }	
+	return(yvar)
+}
+
+.getzvar <- function(zvar, vars) {
+	if (zvar == '') { zvar <- 'z' }
+	if (!(zvar %in% vars)) { stop('cannot find zvar in file') }
+	return(zvar)
+}
+
+.getraster <- function(nc, xvar, yvar, zvar) {
+	ncols <- dim.inq.nc(nc, xvar)$length
+	nrows <- dim.inq.nc(nc, yvar)$length
+	xx <- as.vector(var.get.nc(nc, xvar))
+	xrange <- c(min(xx), max(xx))
+	rm(xx)
+	yy <- as.vector(var.get.nc(nc, yvar))
+	yrange <- c(min(yy), max(yy))
+	rm(yy)
+    r <- raster(xmn=xrange[1], xmx=xrange[2], ymn=yrange[1], ymx=yrange[2], ncols=ncols, nrows=nrows)
+    return(r)
+}
+
+rasterCDF <- function(filename, xvar='', yvar='', zvar='', time=1) {
 # to be improved for large files (i.e. do not read all data from file...)
 	if (!require(RNetCDF)) { stop() }
-	nc <- open.nc(x)
+	nc <- open.nc(filename)
 # should do some checks if x, y and z are present. Or perhaps lat and lon in stead of x	
 
 	nv <- file.inq.nc(nc)$nvars
     vars <- vector()
 	for (i in 1:nv) { vars <- c(var.inq.nc(nc,i-1)$name, vars) }
      
-	if (xvar == '') {
-		if ('x' %in% vars) { 
-			xvar <- 'x'
-		} else if ('lon' %in% vars) { 
-			xvar <- 'lon' 
-		} else {
-			stop('cannot find xvar in file')
-		}
-	} else {
-		if (!(xvar %in% vars)) { 
-			stop('cannot find xvar in file')
-		}
-	}
-	if (yvar == '') {
-		if ('y' %in% vars){ 
-			yvar <- 'y'
-		} else if ('lat' %in% vars) { 
-			yvar <- 'lat' 
-		} else {
-			stop('cannot find yvar in file')
-		}
-	} else {
-		if (!(yvar %in% vars)) { 
-			stop('cannot find yvar in file')
-		}
-	}
+	xvar <- .getxvar(xvar, vars) 
+	yvar <- .getyvar(yvar, vars) 
+	zvar <- .getzvar(zvar, vars) 
+	r <- .getraster(nc, xvar, yvar, zvar)
 	
-	if (zvar == '') { zvar <- 'z' }
-	if (!(zvar %in% vars)) { 
-		stop('cannot find zvar in file')
-	}
+	d <- var.get.nc(nc, zvar)
+	d <- d[,,time]
 	
-	ncols <- dim.inq.nc(nc, xvar)$length
-	nrows <- dim.inq.nc(nc, yvar)$length
-	xrange <- att.get.nc(nc, xvar, 1)
-	yrange <- att.get.nc(nc, yvar, 1)
-	d <- as.vector(var.get.nc(nc, zvar))
-	
-     close.nc(nc)
-    r <- raster(xmn=xrange[1], xmx=xrange[2], ymn=yrange[1], ymx=yrange[2], ncols=ncols, nrows=nrows)
+    close.nc(nc)
 # y needs to go from big to small
-	d <- matrix(d, ncol=ncols, nrow=nrows, byrow=TRUE)
-	d <- as.vector( t( d[nrows:1,] ) )
+	d <- matrix(d, ncol=ncol(r), nrow=nrow(r), byrow=TRUE)
+	d <- as.vector( t( d[nrow(r):1,] ) )
 	
 	r <- setValues(r, d)
 	return(r)
 }
 
+
+
+
+stackCDF <- function(filename, xvar='', yvar='', zvar='', time=1) {
+# to be improved for large files (i.e. do not read all data from file...)
+	if (!require(RNetCDF)) { stop() }
+	nc <- open.nc(filename)
+# should do some checks if x, y and z are present. Or perhaps lat and lon in stead of x	
+
+	nv <- file.inq.nc(nc)$nvars
+    vars <- vector()
+	for (i in 1:nv) { vars <- c(var.inq.nc(nc,i-1)$name, vars) }
+     
+	xvar <- .getxvar(xvar, vars) 
+	yvar <- .getyvar(yvar, vars) 
+	zvar <- .getzvar(zvar, vars) 
+	r <- .getraster(nc, xvar, yvar, zvar)
+	d <- var.get.nc(nc, zvar)
+    close.nc(nc)
+	
+	dims <- dim(d)
+	if (length(dims)== 3) { tsteps <- dims[3] 
+	} else if (length(dims)== 3) { tsteps <- 1
+	} else if (length(dims)== 2) { 
+		return(stack(rasterCDF(filename, xvar, yvar, zvar)))
+	} else { stop('data has unexpected dimensions') }
+	
+
+	for (i in 1:tsteps) {
+		d <- d[,,i]
+# y needs to go from big to small
+		d <- matrix(d, ncol=ncol(r), nrow=nrow(r), byrow=TRUE)
+		d <- as.vector( t( d[nrow(r):1,] ) )
+		r <- setValues(r, d)
+		if (i == 1) { stk <- stack(r) 
+		} else { stk <- addLayer(stk, r) }
+	}
+	return(stk)
+}

Added: pkg/raster/man/rasterCDF.Rd
===================================================================
--- pkg/raster/man/rasterCDF.Rd	                        (rev 0)
+++ pkg/raster/man/rasterCDF.Rd	2009-09-04 23:45:34 UTC (rev 476)
@@ -0,0 +1,40 @@
+\name{rasterCDF}
+
+\alias{rasterCDF}
+\alias{stackCDF}
+
+\title{Read a netCDF file}
+
+\description{Read the contents of a netCDF file into a RasterLayer or RasterStack object. Currently all the data are loaded into memory (unlike with other formats), but this will likely change in the future. 
+}
+
+\usage{
+rasterCDF(filename, xvar='', yvar='', zvar='', time=1)
+}
+
+\arguments{
+\item{filename}{filename}
+\item{xvar}{x variable in the file (e.g. 'longitude')}
+\item{yvar}{y variable in the file (e.g. 'latitude')}
+\item{zvar}{z variable in the file (e.g. 'altitude', or 'precipitation)}
+\item{time}{numer of time steps in the file (default is 1)} 
+}
+
+\value{
+a Raster* object
+}
+
+\details{
+  The function will try to guess values for the xvar, yvar, and zvar if they are not supplied.
+}
+
+\author{Robert J. Hijmans}
+
+\seealso{ \code{\link[raster]{raster},  \link[raster]{stack} } }
+
+\examples{
+}
+
+\keyword{methods}
+\keyword{spatial}
+



More information about the Raster-commits mailing list