读取大量 .asc-files,删除行并在 R 中另存为光栅
Read large number of .asc-files, delete rows and save as raster in R
我需要阅读大量的 .asc-files,删除行并通过 stack()
将它们转换为 raster-stack。 zip-packed 数据的来源在这里: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
我想知道是否有更好的方法将此数据转换为 raster-files。其他 .asc 文件只有 6 行作为 header ,如下所示: ftp://ftp.dwd.de/pub/CDC/grids_germany/monthly/precipitation/01_Jan/ 。我通过一个更简单的函数读取该数据,该函数仅使用 stack()
-function:
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)"
最后我通过使用 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)
我需要阅读大量的 .asc-files,删除行并通过 stack()
将它们转换为 raster-stack。 zip-packed 数据的来源在这里: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
我想知道是否有更好的方法将此数据转换为 raster-files。其他 .asc 文件只有 6 行作为 header ,如下所示: ftp://ftp.dwd.de/pub/CDC/grids_germany/monthly/precipitation/01_Jan/ 。我通过一个更简单的函数读取该数据,该函数仅使用 stack()
-function:
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)"
最后我通过使用 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)