Good day all,

I have some very simple raster algebra that I need to implement
over/between two sets of ~10,000 raster layers (stored in a raster stack).
At the moment, my script is running for days (and I will likely need to do
some bootstrapping at some stage).

The raster layers have been generated and are sitting in a folder. I have
sped up the import process using the quickStack function proposed by
RobertH (
http://stackoverflow.com/questions/9929324/increase-performance-speed). So,
I am trying to figure out a way to speed up the following code (x and y are
raster stacks of ~10,000 layers):

xy <- x==1&y==1
xy.tot <- sum(xy)

I have included some sample code below which simulates my datasets and
problem. As the n.layers increase, the time required increases dramatically
(certainly not linearly).

library(raster)
fun <- function(ncol=100,nrow=100,n.layers=100) {
  res <- list()
  for (i in 1:n.layers) {
  r <- raster(ncol=100, nrow=100)
  vals <- ifelse(runif(ncell(r))>0.5,1,0)
  r <- setValues(r, vals)
  res[[i]] <- r
  }
  stack(res)
}

x <- fun(100,100,100)
y <- fun(100,100,100)

system.time(xy <- x==1&y==1)
system.time(xy.tot <- sum(xy))

x <- fun(100,100,1000)
y <- fun(100,100,1000)

system.time(xy <- x==1&y==1)
system.time(xy.tot <- sum(xy))

x <- fun(100,100,10000)
y <- fun(100,100,10000)

system.time(xy <- x==1&y==1)
system.time(xy.tot <- sum(xy))


Does anyone have any suggestions as to how I could speed up this process? I
have been looking at parallel processing with the foreach library - but I
am battling trying to figure out how to implement it in a useful way to
this problem. Plus increasing the number of processors with the current
functions is not going to offer me an incredible optimisation (I only have
a dual-core machine).

Any suggestions would be greatly appreciated.

Cheers,
Alastair

-- 
Dr. Alastair J. Potts
Claude Leon Postdoctoral Fellow
Botany Department
Nelson Mandela Metropolitan University

Cell #: 082 491-7275
Office #: 041 504-2398 (Tues, Thurs, Fri)
Fax #: 086 686-4251

	[[alternative HTML version deleted]]

