在 R 中构建多层栅格温度分布的任何解决方法

Any workaround to construct temperature distribution over multi-layers raster in R

在这里我发现了一个非常有趣的博客:critical threshold in temperature effects and empirical approach is very interesting, so I want to implement its idea in R. However, I have multi-layer raster data of German' historical daily temperatures (15 years' historical daily mean temperature) in large RasterBrick object. According to the empirical approach that discussed in inspired post,我需要从我的多层栅格数据中构建温度分布。

更新 2:可重现的 shapefile

我知道从 3rd 方网站下载 shapefile 是不切实际的,所以在这里我想出了可复制的 shapefile 来试试:

library(sf)
library(maps)
library(rgeos)
library(mapdata)

germany <- st_as_sf(map("Germany", plot = FALSE, fill = TRUE))
write_sf(germany, "germany.shp")

为了轻松跟进我的 post,我创建了可重现的栅格数据以在 R 中使用。我还提供了从 eurostat 网站获取的德国 shapefile;这是动态的 shapefile(我可以保证 link 非常安全并且文件非常小):eurostat' shapefile 这是方便的可重现栅格数据:

可重现数据

library(raster)
library(lubridate)
library(tidyverse)

r <- raster(xmn=5.75, xmx= 15, ymn = 47.25, ymx =55,res=c(0.25,0.25))
Deu_crop <- do.call(stack,lapply(1:5479,function(i) setValues(r,round(runif(n = ncell(r),min = -10,max = 25)))))
names(Deu_crop) <- paste0('X',gsub('-','.',ymd('1980.01.01') + days(1:5479)))
shp <- shapefile('eurostat_NUTS3/deu_adm_2006.shp')
e <- raster::extract(Deu_crop,shp)
names(e) <- shp$NUTS_ID

所以要测试 inspired post, I need to design several global variables which will be used helper functions that presented in here. But I don't understand how to design some critical global variables that used to accomplish its workflow; It is recommended to define global variable like: w - weather data; tempDat: particular aggregated weather data; Trows: span aggregated grid data; and T: vector of integer temperature (details can be found here: details) 中介绍的工作流程。

我想根据网格化的每日天气数据估计温度随时间的分布。但是我很难测试这个inspired post中给出的经验步骤,因为它没有提到处理多层栅格数据的情况的解决方案,所以我不知道如何采纳它的奇思妙想我自己在 R.

这是我在 inspired post:

中使用辅助函数之前从 shapefile (eurostat' shapefile) 为每个多边形聚合多层栅格数据的方法

初次尝试操纵多层栅格:

rasterHelper <- function(ix,e){
  gather(data.frame(e[[ix]],stringsAsFactors = F),'colname','temp') %>% 
    group_by(colname) %>% summarise(temp = mean(temp)) %>% ungroup() %>% # spatial mean
    mutate(year = sub('X(\d{4}).+','\1',colname)) %>% 
    group_by(year) %>% summarise_all(funs(sum)) %>% mutate(NUTS_ID = names(e)[ix])
}
do.call(rbind,lapply(1:length(e),function(ix) rasterHelper(ix)))

但是我上面的尝试没有用;在我的尝试中,我打算聚合每个多边形的温度栅格数据。 inspired post 的实现非常有用,但处理多层栅格数据仍然很难跟进。我想我应该在每个栅格层上工作并构建随时间变化的温度分布,但我真的不知道如何在 R 中做。有什么想法吗?

更新:

这是我从中得到灵感的论文:nonlinear temperature effect ..., but implementing the proposed method is still challenging for me even I followed the workflow that presented in the respective blog: searching critical threshold in temperature effect

有没有人指出如何在 R 中对多层栅格数据采用其经验方法?如何估计温度随时间的分布?我怎样才能在 R 中实现这一点?还有什么想法吗?谢谢

我不太确定你想做什么。

设置一个更小的例子:

library(raster)
lux <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(lux)
s <- stack(lapply(1:12, function(i) setValues(r, 1:ncell(r))))

e <- extract(s, lux)

现在你说你要聚合---这有点含糊,但也许你想要的是

x <- lapply(e, function(i) apply(i,2,mean))

相当于

y <- extract(s, lux, fun='mean')