关于这个'for'循环的plyr-up的建议

时间:2013-03-28 18:16:15

标签: r parallel-processing plyr netcdf

尽我所能,我无法弄清楚如何让普莱尔工作。非常感谢您对此具体示例的任何帮助,以及对您的示例工作原理的任何解释的奖励积分。

数据为here,以防有人想让这个例子“可行”

设定:

library(ncdf)
library(plyr)
longs = seq.int(from=1,to=4320,by=6)
lats = seq.int(from=1,to=2160,by=6)
blocksize = 6
maize.nc = open.ncdf('maize_5min.nc')

这个for循环有效,但速度慢而且不平行:

latlongcoords = NULL
for (x in 1:length(longs))  {
for (y in 1:length(lats))   {   
    lat = mean(get.var.ncdf(maize.nc,varid="latitude",start = lats[x],count = blocksize))
    lon = mean(get.var.ncdf(maize.nc,varid="longitude",start = longs[y],count = blocksize))
    rw = c(lat,lon)
    latlongcoords = rbind(latlongcoords,rw)
    }
    #print(x)
    }
save(latlongcoords,file="latlongcoords")

我想做这样的事情:

require(doMC)
registerDoMC(32)
x = seq(1:10)  #shorten it for testing purposes
y = seq(1:10)  #shorten it for testing purposes
makecoords = function(x,y){
    lat = mean(get.var.ncdf(maize.nc,varid="latitude",start = lats[x],count = blocksize))
    lon = mean(get.var.ncdf(maize.nc,varid="longitude",start = longs[y],count = blocksize))
    c(lat,lon)
    }
latlongcoords = NULL
latlongcoords = aaply(.data = cbind(x,y), .margins=2, .fun=makecoords(x,y),.parallel=TRUE)

当我运行它时,我收到此错误消息:

Error in get.var.ncdf(maize.nc, varid = "latitude", start = lats[x], count = blocksize) : 
  Error: variable has 1 dims, but start has 10 entries.  They must match!

看起来plyr将整个向量传递给函数,而不是单个值!关于如何使这项工作的建议,以及对你的修复工作原因的解释,我们非常感激!

提前致谢!

1 个答案:

答案 0 :(得分:1)

试试这个。我想你要在这里传递.margin=1而不是2。也就是说,如果您想传递值按行,那么.margin = 1x[1]y[1]首先传递给w。因此,我们将使用w[1]w[2]访问它们,并将其传递给您的makecoords函数。我希望这是你所期待的。如果没有,请随意在评论中写下出现了什么问题。

latlongcoords <- aaply(cbind(x,y), 1, function(w) 
                 makecoords(w[1], w[2]), .parallel=TRUE)

我可以使用.parallel = FALSE。现在无法测试.parallel=TRUE

head(latlongcoords)
# X1              1            2
#   1    89.7500025 -179.7499949
#   2    86.7500025 -176.7499949
#   3    83.7500025 -173.7499949
#   4    80.7500025 -170.7499949
#   5    77.7500025 -167.7499949
#   6    74.7500025 -164.7499949