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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 11 02:26:44 CEST 2009


Author: rhijmans
Date: 2009-09-11 02:26:42 +0200 (Fri, 11 Sep 2009)
New Revision: 490

Added:
   pkg/raster/R/rotate.R
   pkg/raster/R/stackFromNetCDF.R
   pkg/raster/man/rotate.Rd
Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/R/rasterFromNetCDF.R
   pkg/raster/R/stackAdd.R
   pkg/raster/R/stackRead.R
   pkg/raster/R/xyValues.R
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-09-10 06:28:52 UTC (rev 489)
+++ pkg/raster/DESCRIPTION	2009-09-11 00:26:42 UTC (rev 490)
@@ -1,8 +1,8 @@
 Package: raster
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
-Version: 0.9.4
-Date: 8-September-2009
+Version: 0.9.5
+Date: 10-September-2009
 Depends: methods, sp, R (>= 2.8.0)
 Suggests: rgdal (>= 0.5-33), RNetCDF
 Author: Robert J. Hijmans & Jacob van Etten

Modified: pkg/raster/R/rasterFromNetCDF.R
===================================================================
--- pkg/raster/R/rasterFromNetCDF.R	2009-09-10 06:28:52 UTC (rev 489)
+++ pkg/raster/R/rasterFromNetCDF.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -31,7 +31,11 @@
 	return(zvar)
 }
 
-.getraster <- function(nc, xvar, yvar, zvar) {
+.getraster <- function(nc, vars, xvar, yvar) {
+	xvar <- .getxvar(xvar, vars) 
+	yvar <- .getyvar(yvar, vars) 
+	# to do: also consider "lat_bnds" and "lat_bnds"
+	
 	ncols <- dim.inq.nc(nc, xvar)$length
 	nrows <- dim.inq.nc(nc, yvar)$length
 	xx <- as.vector(var.get.nc(nc, xvar))
@@ -58,10 +62,9 @@
 	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)
+	r <- .getraster(nc, vars, xvar, yvar)
+	
 	d <- var.get.nc(nc, zvar)
     close.nc(nc)
     
@@ -70,6 +73,9 @@
 	if (length(dims)== 2) { 
 		d <- as.vector(d)
 	} else if (length(dims)== 3) { 
+	    if (time > dims[3] | time < 1) {
+			stop(paste('time should be >= 1 and <=', dims[3]))
+		}
 		d <- as.vector(d[,,time])
 	} else { stop(paste('data has an unexpected number of dimensions', dims)) }
 
@@ -81,46 +87,3 @@
 }
 
 
-
-.stackCDF <- function(filename, xvar='', yvar='', zvar='', time='') {
-# to be improved for large files (i.e. do not read all data from file...)
-	if (!require(RNetCDF)) { stop() }
-	nc <- open.nc(filename)
-
-	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)
-	dd <- var.get.nc(nc, zvar)
-    close.nc(nc)
-	
-	dims <- dim(dd)
-	if (length(dims)== 3) { 
-		if (is.numeric(time)) { 
-			tsteps <- time	
-		} else { 
-			tsteps <- 1:dims[3] 
-		}
-	} else if (length(dims)== 3) { tsteps <- 1
-	} else if (length(dims)== 2) { 
-		return(stack(.rasterCDF(filename, xvar, yvar, zvar)))
-	} else { stop(paste('data has an unexpected number of dimensions', dims)) }
-	
-
-	for (i in tsteps) {
-		d <- dd[,,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/R/rotate.R
===================================================================
--- pkg/raster/R/rotate.R	                        (rev 0)
+++ pkg/raster/R/rotate.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -0,0 +1,12 @@
+
+rotate <- function(raster) {
+    xr <- xmax(raster) - xmin(raster)
+	hx <- xr / 2
+	r1 <- crop(raster, newExtent(xmin(raster), hx, ymin(raster), ymax(raster)))
+	r2 <- crop(raster, newExtent(hx, xmax(raster), ymin(raster), ymax(raster)))
+	xmax(r2) <- xmax(r2) - xr
+	xmin(r2) <- xmin(r2) - xr
+    m <- merge(r1, r2)	
+    return(m)
+}
+

Modified: pkg/raster/R/stackAdd.R
===================================================================
--- pkg/raster/R/stackAdd.R	2009-09-10 06:28:52 UTC (rev 489)
+++ pkg/raster/R/stackAdd.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -1,7 +1,6 @@
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
 # Date : June 2008
-# Version 0.8
+# Version 0.9
 # Licence GPL v3
 
 
@@ -43,10 +42,6 @@
 	for (i in 1:length(rasters)) { 
 		raster <- rasters[[i]]
 
-		if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
-			stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
-		}
-
 		if (nlayers(rstack) == 0) {
 			if (class(raster) == 'RasterStack') {
 				rstack <- raster
@@ -55,28 +50,27 @@
 				rstack <- setExtent(rstack, raster, snap=FALSE)
 				projection(rstack) <- projection(raster)
 
-				nl <- 1
-				rstack at data@nlayers <- as.integer(nl)
-				rstack at layers[nl] <- raster 
-				rstack at data@min[nl] <- raster at data@min
-				rstack at data@max[nl] <- raster at data@max		
-
-				if (trim(raster at file@shortname) != "") {
-					cname <- trim(raster at file@shortname)
+				if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
+					nl <- 0
 				} else {
-					cname <- "layer1"
-				}
-				rstack at data@colnames[1] <- cname
-				if (dataContent(raster) == 'all') {
-					rstack at data@values <- as.matrix(values(raster))
-					rstack at data@content <- 'all'
-					raster <- clearValues(raster)
-				} else {
-					if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
-						stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
-					}
-				}
+					nl <- 1
+					rstack at data@nlayers <- as.integer(nl)
+					rstack at data@min[nl] <- raster at data@min
+					rstack at data@max[nl] <- raster at data@max		
 
+					if (trim(raster at file@shortname) != "") {
+						cname <- trim(raster at file@shortname)
+					} else {
+						cname <- "layer1"
+					}
+					rstack at data@colnames[1] <- cname
+					if (dataContent(raster) == 'all') {
+						rstack at data@values <- as.matrix(values(raster))
+						rstack at data@content <- 'all'
+						raster <- clearValues(raster)
+					}
+					rstack at layers[nl] <- raster 
+				} 
 			}
 			
 		} else {
@@ -90,11 +84,15 @@
 			for (k in 1:length(rasterlist)) {
 				nl <- as.integer( rstack at data@nlayers + 1 )
 				rstack at data@nlayers <- nl
-				rstack at layers[nl] <- raster 
 				rstack at data@min[nl] <- raster at data@min
 				rstack at data@max[nl] <- raster at data@max		
 
 				raster <- rasterlist[[k]]
+				
+				if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
+					stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+				}
+
 				if (!compare(c(rstack, raster))) { 
 					stop(paste("could not add raster:", filename(raster))) 
 				}
@@ -122,7 +120,7 @@
 						raster <- clearValues(raster)
 					}
 				}
-
+				rstack at layers[nl] <- raster 
 			}	
 		}
 	}	

Added: pkg/raster/R/stackFromNetCDF.R
===================================================================
--- pkg/raster/R/stackFromNetCDF.R	                        (rev 0)
+++ pkg/raster/R/stackFromNetCDF.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -0,0 +1,48 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# Date: Sept 2009
+# Version 0.9
+# Licence GPL v3
+
+
+.stackCDF <- function(filename, xvar='', yvar='', zvar='', time='') {
+# to be improved for large files (i.e. do not read all data from file...)
+	if (!require(RNetCDF)) { stop() }
+	nc <- open.nc(filename)
+
+	nv <- file.inq.nc(nc)$nvars
+    vars <- vector()
+	for (i in 1:nv) { vars <- c(var.inq.nc(nc,i-1)$name, vars) }
+     
+	r <- .getraster(nc, vars, xvar, yvar) 
+	stk <- stack( r )
+	
+	zvar <- .getzvar(zvar, vars) 
+	
+	dd <- var.get.nc(nc, zvar)
+    close.nc(nc)
+	
+	dims <- dim(dd)
+	if (length(dims)== 3) { 
+		if (is.numeric(time)) { 
+			tsteps <- time	
+		} else { 
+			tsteps <- 1:dims[3] 
+		}
+	} else if (length(dims)== 3) { tsteps <- 1
+	} else if (length(dims)== 2) { 
+		return(stack(.rasterCDF(filename, xvar, yvar, zvar)))
+	} else { stop(paste('data has an unexpected number of dimensions', dims)) }
+	
+
+#	for (i in tsteps) {
+	d <- dd[,,tsteps]
+	dims <- dim(d)
+	for (i in 1:dims[3]) {
+		x <- t(d[,,i])
+		x <- x[nrow(x):1, ]
+		r[] <- as.vector(t(x))
+		stk <- addLayer(stk, r)
+	}
+	return(stk)
+}
+

Modified: pkg/raster/R/stackRead.R
===================================================================
--- pkg/raster/R/stackRead.R	2009-09-10 06:28:52 UTC (rev 489)
+++ pkg/raster/R/stackRead.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -1,8 +1,7 @@
 # R code for reading raster (grid) data
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date : June 2008
-# Version 0.8
+# Date: Sept 2008
+# Version 0.9
 # Licence GPL v3
 
 
@@ -31,7 +30,7 @@
 
 .stackReadCells <- function(object, cells) {
 		for (i in seq(nlayers(object))) {
-			v <- .rasterReadCells(object at layers[[i]], cells)
+			v <- .rasterReadCells( raster(object, i), cells)
 			if (i == 1) {
 				result <- v
 			} else {

Modified: pkg/raster/R/xyValues.R
===================================================================
--- pkg/raster/R/xyValues.R	2009-09-10 06:28:52 UTC (rev 489)
+++ pkg/raster/R/xyValues.R	2009-09-11 00:26:42 UTC (rev 490)
@@ -2,7 +2,7 @@
 # International Rice Research Institute. Philippines
 # contact: r.hijmans at gmail.com
 # Date : November 2008
-# Version 0.8
+# Version 0.9
 # Licence GPL v3
 
 

Added: pkg/raster/man/rotate.Rd
===================================================================
--- pkg/raster/man/rotate.Rd	                        (rev 0)
+++ pkg/raster/man/rotate.Rd	2009-09-11 00:26:42 UTC (rev 490)
@@ -0,0 +1,34 @@
+\name{rotate}
+
+\alias{rotate}
+  
+\title{rotate}
+
+\description{
+Rotate a RasterLayer that has x coordinates (longitude) from 0 to 360, to standard coordinates between -180 and 180 degrees. Longitude between 0 and 360 is frequently used in climate data (e.g. IPCC climate change data)
+}
+
+\usage{
+rotate(raster)
+}
+
+\arguments{
+  \item{raster}{a RasterLayer object}
+}
+
+\value{
+a RasterLayer
+}
+
+\author{Robert J. Hijmans }
+\examples{
+
+r <- raster(nrow=18, ncol=36)
+m <- matrix(1:ncell(r), nrow=18)
+r[] <- as.vector(t(m))
+extent(r) <- newExtent(0, 360, -90, 90)
+rr <- rotate(r)
+
+}
+
+\keyword{spatial}



More information about the Raster-commits mailing list