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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 11 03:03:03 CEST 2009


Author: rhijmans
Date: 2009-09-11 03:03:01 +0200 (Fri, 11 Sep 2009)
New Revision: 491

Modified:
   pkg/raster/R/rasterFromNetCDF.R
   pkg/raster/man/raster.Rd
Log:


Modified: pkg/raster/R/rasterFromNetCDF.R
===================================================================
--- pkg/raster/R/rasterFromNetCDF.R	2009-09-11 00:26:42 UTC (rev 490)
+++ pkg/raster/R/rasterFromNetCDF.R	2009-09-11 01:03:01 UTC (rev 491)
@@ -55,7 +55,7 @@
 }
 
 
-.rasterCDF <- function(filename, xvar='', yvar='', zvar='', time=1) {
+.rasterCDF <- function(filename, xvar='', yvar='', zvar='', time=NA) {
 # to be improved for large files (i.e. do not read all data from file...)
 	if (!require(RNetCDF)) { stop() }
 	nc <- open.nc(filename)
@@ -65,19 +65,25 @@
 	zvar <- .getzvar(zvar, vars) 
 	r <- .getraster(nc, vars, xvar, yvar)
 	
-	d <- var.get.nc(nc, zvar)
-    close.nc(nc)
-    
-	dims <- dim(d)
-
-	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]))
+	if (is.na(time) ) {
+		d <- var.get.nc(nc, variable=zvar)
+		dims <- dim(d)
+		if (length(dims)== 1) { 
+			stop('zvar only has a single dimension')
+		} else if (length(dims)== 2) { 
+			d <- as.vector(d)
+		} else if (length(dims)== 3) { 
+			stop('zvar has three dimensions, provide a value for "time"')
+		} else if (length(dims)== 4) { 
+			stop('zvar has four dimensions, I cannot process that')
 		}
-		d <- as.vector(d[,,time])
-	} else { stop(paste('data has an unexpected number of dimensions', dims)) }
+		
+	} else {
+		start <- c(1, 1, time)
+		count <- c(ncol(r), nrow(r), 1)
+		d <- as.vector ( var.get.nc(nc, variable=zvar, start=start, count=count) )
+	} 
+	close.nc(nc)
 
 # y needs to go from big to small
 	d <- matrix(d, ncol=ncol(r), nrow=nrow(r), byrow=TRUE)

Modified: pkg/raster/man/raster.Rd
===================================================================
--- pkg/raster/man/raster.Rd	2009-09-11 00:26:42 UTC (rev 490)
+++ pkg/raster/man/raster.Rd	2009-09-11 01:03:01 UTC (rev 491)
@@ -45,12 +45,13 @@
 \tab \code{xvar} \tab character. For netCDF files only: identify the x variable (e.g. 'longitude' or 'x') \cr
 \tab \code{yvar} \tab character. For netCDF files only: identify the y variable (e.g. 'latitude' or 'y')\cr
 \tab \code{zvar} \tab character. For netCDF files only: identify the z variable (e.g. 'altitide' or 'precipitation')\cr
-\tab \code{time} \tab integer > 0. For netCDF files only: identify which of the time variables you want (default=1)\cr
+\tab \code{time} \tab integer > 0. For netCDF files only: identify which of the time variables you want (default=NA)\cr
 }
 
 If \code{x} is a character value, it should be a filename of a file that the raster package can read. Supported file types are the 'native' raster package format and those that can be read via rgdal. See \code{\link[rgdal]{readGDAL}} help for supported file types.
 
-For netCDF files, the function will try to guess values for the \code{xvar}, \code{yvar}, and \code{zvar} if they are not supplied. For this function to work, the RNetCDF package needs to have been installed. Note that currently, a raster object made from a netCDF file will load all values from file at creation (unlike other raster objects), and that 'read' functions are therefore not applicable. 
+For netCDF files, the function will try to guess values for the \code{xvar}, \code{yvar}, and \code{zvar} if they are not supplied. For this function to work, the RNetCDF package needs to have been installed. Note that currently, a raster object made from a netCDF file will load all values from file at creation (unlike other raster objects), and that \code{read*} functions are therefore not applicable. 
+netCDF data can only be read if the data are stored in 2 (x and y) or 3 (x, y, and time) dimensions. If the data has three dimensions, a value must be supplied for \code{time}
 
 
 2) Create a RasterLayer object from scratch



More information about the Raster-commits mailing list