R - 加速替换大型栅格堆栈中的值

时间:2015-09-04 19:58:40

标签: r geospatial multicore raster

最近,我无法加速我用于为某些卫星数据应用云掩码的代码。

有关加快大型rasterstacks值替换的提示吗?

1 个答案:

答案 0 :(得分:0)

以下代码设置了两个大型栅格堆栈,并使用一个来掩盖另一个栅格堆栈中的值。请注意,这些测试是在32核服务器上完成的。使用foreach的改进在很大程度上取决于高性能计算机的使用。

library(foreach)
library(doParallel)
library(raster)

xy = matrix(rnorm(1000^2),1000,1000)
# Turn the matrix into a raster
rast = raster(xy)
# Give it lat/lon coords for 36-37°E, 3-2°S
extent(rast) = c(36,37,-3,-2)
# ... and assign a projection
projection(rast) = CRS("+proj=longlat +datum=WGS84")

# create first random raster stack
raster_list = list()
for(i in 1:300){
  set.seed(i)
  rast[]= matrix(rnorm(1000^2),1000,1000)
  raster_list = c(raster_list,rast)
}

rast_stack = stack(raster_list)
backup_raster_stack = rast_stack # store a backup to reset 
plot(rast_stack[[3]])

# create second random raster stack
raster_list2 = list()
for(i in 1:300){
  set.seed(i+25)
  rast[]= matrix(rnorm(1000^2),1000,1000)
  raster_list2 = c(raster_list2,rast)
}

rast_stack2 = stack(raster_list2)
plot(rast_stack2[[3]])


##################
# do value replacement the normal way
ptm <- proc.time()
rast_stack[rast_stack2>1]=NA
proc.time() - ptm

用户系统已用完

426.872 23.734 462.304

# reset values
raster_stack = backup_raster_stack

#################
# do value replacement in calc
ptm <- proc.time()
calc( rast_stack , function(x) { rast_stack[ rast_stack2 > 1  ] = NA; return(x) } )
proc.time() - ptm

用户系统已用完

491.732 30.242 530.230

# reset values
raster_stack = backup_raster_stack

#################
# do value replacement in foreach loop
registerDoParallel(32)
ptm <- proc.time()
foreach(i=1:dim(rast_stack)[3]) %do% { rast_stack[[i]][rast_stack2[[i]]>1]=NA}
proc.time() - ptm

用户系统已用完

57.654 1.692 59.378