迭代一个函数以将许多栅格堆栈合并为一个 [英] Iterating over a function to combine many raster stacks into one

查看:44
本文介绍了迭代一个函数以将许多栅格堆栈合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上坚持了一段时间.到处寻找答案,但我似乎在 Stack 上找不到任何东西.大家可以提供的任何帮助将不胜感激.

Been stuck on this for a while now. Looked everywhere for an answer, but I can't seem to find anything on Stack. Any help you all can give that would be very appreciated.

我的主要问题是我需要导入很多很多 netcdf4 文件,为每个文件创建光栅砖,然后将许多砖组合起来为每个变量制作一个主砖".给你一个更清楚的例子,我有 40 年(netcdf = 40)的许多气候变量(n = 15),它们每天都在解析.目标是每月汇总,但首先我必须得到这个函数,该函数可以读取所有年份的 netcdf 以将一个变量放入一个大堆栈中.

My main issue is that I need to import many, many netcdf4 files, create raster bricks of each, then combine many bricks to make a "master brick" per variable. To give you a clearer example, I have 40 years (netcdf = 40) of many climate variables (n = 15) that are at a daily resolution. The goal is to aggregate to monthly, but first I have to get this function that reads all years of netcdf's for one variable in and into one large stack.

我现在的内容如下:

# Libraries --------------------------------------------------------------
library(raster)
library(ncdf4)

# Directories -------------------------------------------------------------

tmp_dl <- list.files("/Users/NateM", pattern = "nc",
                 full.names = TRUE)
# Functions ---------------------------------------------------------------
rstlist = stack()

netcdf_import <- function(file) {
   nc <- nc_open(file)
   nc_att <- attributes(nc$var)$names
   ncvar <- ncvar_get(nc, nc_att)
   rm(nc)
   proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
   rbrck <- brick(ncvar, crs= proj)
   rm(ncvar)
   extent(rbrck) <- c(-124.772163391113, -67.06383005778, 25.0626894632975, 
                       49.3960227966309) 
   }      

t <- for(i in 1:length(tmp_dl)) {
         x <- netcdf_import(tmp_dl[i])
         rstlist <- stack(rstlist, x)
      }

allyears <- stack(t)

两年的数据可以在这里找到:

Two years of the data can be found here:

https://www.northwestknowledge.net/metdata/data/pdsi_2016.nchttps://www.northwestknowledge.net/metdata/data/pdsi_2015.nc

欢迎任何帮助.在此先感谢大家,如果这是重复的帖子,我深表歉意;我四处张望却无济于事.

Any help would be most welcomed. Thank you all in advance, and if this is a duplicate post I apologize; I looked far and wide to no avail.

推荐答案

你的代码没问题,你只需要从你的函数中return加载的brick rbrck,否则你会得到程度.

Your code is fine, you just need to return the loaded brick rbrck from your function, otherwise you'll get the extent.

至于加载和堆叠,我建议使用 lapply 将函数应用于每个数据文件.这会给你一个整洁的清单,每个项目都有一年.在那里你可以做一些更多的处理,最后只需调用列表中的 stack 来生成你的主砖".

As for loading and stacking, I'd suggest using lapply to apply the function to each datafile. This will give you a neat list with a year per item. There you could do some more processing and finally just call stack on the list to produce your "master brick".

请注意,我只使用了两个文件,所以当您使用 40 个文件时,我不确定整个文件的大小.

Mind that I only did this with two files, so I'm not sure about the size of the whole thing when you do it with 40.

这是您修改后的代码:

# Libraries --------------------------------------------------------------
library(raster)
library(ncdf4)

# Directories -------------------------------------------------------------

tmp_dl <- list.files("/Users/NateM", pattern = "nc",
                     full.names = TRUE)
# Functions ---------------------------------------------------------------

netcdf_import <- function(file) {
  nc <- nc_open(file)
  nc_att <- attributes(nc$var)$names
  ncvar <- ncvar_get(nc, nc_att)
  rm(nc)
  proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
  rbrck <- brick(ncvar, crs= proj)
  rm(ncvar)
  extent(rbrck) <- c(-124.772163391113, -67.06383005778, 25.0626894632975, 
                     49.3960227966309) 
  return(rbrck)
}      

# apply function
allyrs <- lapply(tmp_dl,netcdf_import)

# stack to master brick
allyrs <- do.call(stack,allyrs)

HTH

这篇关于迭代一个函数以将许多栅格堆栈合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆