如何循环遍历多个RasterBrick来创建一个大型RasterBrick?

时间:2016-10-17 11:14:25

标签: r loops netcdf r-raster

我的问题进一步建立在How to extract data from a RasterBrick?之前的帖子上。我有多个netCDF文件,温度数据为纬度,长度和深度,分别为不同的时间段(每个6个月)。我也有一个lat,long和time的数据框,我想从netcdf文件中提取海面临时数据。在我提取这些数据之前,我想将所有netcdf文件转换并合并到一个RaterBrick(这更容易使用)。当我使用循环来“砖头”时和'合并'它创建一个RasterLayer对象的文件只有一层而不是我期待的栅格砖。我已经在互联网上搜索,但到目前为止,我无法找到解决此问题的正确方法。

我退出了使用循环的新功能,所以如果我提出一个非常简单的答案但我真的可以使用一些建议,我会提前抱歉。

这是我到目前为止所做的:

创建所有netcdf文件的文件列表。

#Open all files, creats a list of 12 files
    files= list.files('copernicus/daily_temp/',pattern='*.nc', full.names=TRUE)

尝试遍历文件列表以创建砖块。将这些多层砖合并在一起,形成一个每天具有海面(水平= 1)温度数据的大型RasterBrick。

# Loop over files to creat a RasterBrick of temp at SST
for(i in 1:length(files)) {
  temp <- brick(files[i], varname="votemper",level=1)
  temp.brick<-merge(temp)}

# Look what one brick is build out of
> temp
class       : RasterBrick 
dimensions  : 375, 297, 111375, 184  (nrow, ncol, ncell, nlayers)
resolution  : 0.11111, 0.06667  (x, y)
extent      : -19.94444, 13.05523, 40.03333, 65.03459  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\PFA\Dropbox\HOM\copernicus\daily_temp\daily_temp_2003_2.nc 
names       : X2003.07.01, X2003.07.02, X2003.07.03, X2003.07.04, X2003.07.05, X2003.07.06, X2003.07.07, X2003.07.08, X2003.07.09, X2003.07.10, X2003.07.11, X2003.07.12, X2003.07.13, X2003.07.14, X2003.07.15, ... 
Date        : 2003-07-01, 2003-12-31 (min, max)
varname     : votemper 
level       : 1 

   # Look at what the merged bricks are build out of 
class       : RasterLayer 
dimensions  : 375, 297, 111375  (nrow, ncol, ncell)
resolution  : 0.11111, 0.06667  (x, y)
extent      : -19.94444, 13.05523, 40.03333, 65.03459  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 280.677, 297.669  (min, max)

1 个答案:

答案 0 :(得分:0)

您可以尝试手动创建第一块砖,然后循环浏览其余文件,合并它们。如果这不适合您的RAM,您可能需要切换到使用硬盘驱动器上的文件。请参阅上一个?merge示例,了解如何执行此操作。

tmp <- brick(files[1], ...) # create first brick
for (i in 2:length(files)) {
  newbrick <- brick(files[i], ...) # this will be the second file in first iteration
  # on first iteration, it will merge file 1 and 2
  # on second interation, merged file (1,2) and file 3
  # on third iteration, merged file (1,2,3) and file 4
  # ...
  mergedbrick <- merge(tmp, newbrick) 

}