读取大量.asc文件,删除行并在R

时间:2016-12-14 15:35:43

标签: r raster large-files

我需要读取大量的.asc文件,删除行并将它们转换为stack()的栅格堆栈。压缩数据的来源如下:ftp://ftp.dwd.de/pub/CDC/grids_germany/monthly/radiation_global/ 我已经解压缩了文件。但是现在我写的代码真的很慢,我的电脑无法完成它:

files <- list.files("mydirectory", pattern="\\.asc$", 
                    full.names=TRUE, recursive=TRUE)
i <- lapply(files, readLines) #read data
i <- lapply(i, function(x){x[-(1:28)]}) #delete rows
i <- lapply(i, function(x){gsub('-999', ' NA ', x, fixed = TRUE)}) #convert '-999' to NA
i <- lapply(i, function(x){scan(textConnection(x), what = double(), n = 866*654)}) #convert to numeric
i <- lapply(i, function(x){matrix(x, nrow = 866, ncol = 654, byrow = TRUE)}) #convert to matrix
r <- lapply(i, function(x){raster(x)}) #rasterize data
st <- stack(r) #convert to stack-raster

我想知道是否有更好的方法将此数据转换为栅格文件。其他.asc文件只有6行作为标题,如下所示:ftp://ftp.dwd.de/pub/CDC/grids_germany/monthly/precipitation/01_Jan/。我通过一个更简单的函数读取数据,该函数仅使用stack() - 函数:

loadRaster <- function(directory, output, clipping){
  files <- list.files(path=directory, pattern="\\.asc$", 
                     full.names=TRUE, recursive=TRUE)
  stc <- stack(files)
  crs(stc) <- gk3
  stcC <- crop(stc, extent(clipping))
  writeRaster(stcC, filename=output)
} 
#You can ignore the code below "stc <-stack(files)"

1 个答案:

答案 0 :(得分:0)

最后我通过逐步使用textConnection()(再次打开和关闭)得到它,因为 - 并且可能导致缓慢 - 打开的连接存在限制。

files <- list.files(path="mydirectory", pattern="\\.asc$", 
                    full.names=TRUE, recursive=TRUE)
i <- lapply(files, readLines)
i <- lapply(i, function(x){x[-(1:28)]})
i <- lapply(i, function(x){gsub('-999', ' NA ', x, fixed = TRUE)})
names(i) <- substr(files, 92,97)
i1 <- lapply(i[1:100], function(x){scan(textConnection(x), what = double(), n = 866*654)})
closeAllConnections()
i2 <- lapply(i[101:200], function(x){scan(textConnection(x), what = double(), n = 866*654)})
closeAllConnections()
i3 <- lapply(i[201:300], function(x){scan(textConnection(x), what = double(), n = 866*654)})
closeAllConnections()
i4 <- lapply(i[301:length(i)], function(x){scan(textConnection(x), what = double(), n = 866*654)})
closeAllConnections()
i <- c(i1, i2, i3, i4)
m <- lapply(i, function(x){matrix(x, nrow = 866, ncol = 654, byrow = TRUE)})
r <- lapply(m, function(x){raster(x)})
stc <- stack(r)