- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在大型 RasterBrick
对象中有德国历史每日温度观测(15 年的历史每日平均温度)的栅格网格化数据。这是我的栅格网格化数据的样子:
> Temperature_rasterData
class : RasterBrick
dimensions : 31, 37, 1147, 5479 (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25 (x, y)
extent : 5.75, 15, 47.25, 55 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : X1980.01.01, X1980.01.02, X1980.01.03, X1980.01.04, X1980.01.05, X1980.01.06, X1980.01.07, X1980.01.08, X1980.01.09, X1980.01.10, X1980.01.11, X1980.01.12, X1980.01.13, X1980.01.14, X1980.01.15, ...
min values : -9.24, -11.32, -12.05, -14.12, -7.91, -6.35, -6.74, -7.77, -9.79, -10.17, -12.20, -14.90, -15.68, -15.61, -15.22, ...
max values : 2.19, 0.68, 0.30, 2.91, 5.25, 5.03, 4.33, 3.40, 1.52, 0.33, -1.10, -1.61, -3.55, -0.12, 0.19, ...
但是,我打算将每日温度的年度分布离散化到一组固定的温度分箱中(我每年总共需要10个分箱),您可以在这里找到详细的方法:Temperature Effects on Productivity and Factor Reallocation .为此,我需要从所有这些多层栅格网格化数据中找到最高和最低温度值。之所以找一个温度范围是因为我需要根据MAX/MIN
温度值来划分每个网格中每日温度的年分布。
不幸的是,我无法在 R
中重现这些多层 RaterBrick
数据,因为原始栅格网格化数据非常大,难以重现小栅格。我希望 SO
社区能够理解这种情况。这是可重复使用的较小栅格数据:please give it try smallest example raster data这是我的 R
脚本,用于处理下载的栅格数据:
temp_raster <- raster::stack('~/tg_day_2017_grid_ensmean.nc')
data(wrld_simpl)
Germany <- wrld_simpl[wrld_simpl@data$NAME == "Germany",]
deu_ext <- extent(Germany)
Deu_crop <- crop(temp_raster ,deu_ext)
为了获得这些多个栅格后期数据的温度范围,我尝试了以下方法,但它并不聪明,因为我需要一个更简化的解决方案。这是我在 R
中的尝试:
nms <- names(Deu_crop)
yrs <- unique(sub('X(\\d+).+','\\1',nms))
getRange <- lapply(yrs,function(x) {
range(Deu_crop[[grep(x,nms)]],na.rm=TRUE)
})
我真的不知道如何离散化大型 RasterBrick
对象中的数据。特别是,我不太清楚如何操作 raster
数据以实现离散化目的,因为此 raster
数据具有多个层,具有大量的每日平均温度观测值。我怎样才能在 R 中实现这一点?是否可以操纵多层 raster
数据进行离散化?任何的想法?
如果有更简单的方法来处理大型 raster
数据,我如何离散化每日温度的年度分布并为每年制作条形图?在 R 中完成此操作的任何最简单方法?提前致谢!
这是我想从多层 raster
数据制作的可能的条形图:
更新:
我将离散化每个德国地区(AKA,多边形)每年每日温度观测的年度分布,这是动态的德国 NUTS 地区:Germany' shapefile .
最佳答案
这是一个解决方案(包括一个可重现的示例):
library(raster)
library(lubridate)
library(tidyverse)
# creating some fake temperature data which matches your rasterstack
# create template raster
r <- raster(xmn=5.75, xmx= 15, ymn = 47.25, ymx =55,res=c(0.25,0.25))
# add fake temperature values
Deu_crop <- do.call(stack,lapply(1:5479,function(i) setValues(r,round(runif(n = ncell(r),min = -10,max = 25)))))
# add layer names
names(Deu_crop) <- paste0('X',gsub('-','.',ymd('1980.01.01') + days(1:5479)))
# check rasterstack
Deu_crop
# output
#
# class : RasterStack
# dimensions : 31, 37, 1147, 5479 (nrow, ncol, ncell, nlayers)
# resolution : 0.25, 0.25 (x, y)
# extent : 5.75, 15, 47.25, 55 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# names : X1980.01.02, X1980.01.03, X1980.01.04, X1980.01.05, X1980.01.06, X1980.01.07, ...
# min values : -10, -10, -10, -10, -10, -10, ...
# max values : 25, 25, 25, 25, 25, 25, ...
因此 Deu_crop
应该在结构方面与您的数据具有可比性,当然还有随机温度值。
shapefile 不容易重现,所以我下载了你的文件并使用了它。正如我已经提到的,一些多边形对于提取来说有点小。
最快的方法是栅格化 shapefile 以匹配您的数据栅格,但有些多边形不会被转换,而其他多边形可能会转换为错误的单元格......所以在这种情况下,最好使用raster::extract
直接使用 shapefile,尽管速度有点慢。但是,如果您只需要几次,那是可以忍受的 - 同时喝杯咖啡。
shp <- shapefile('eurostat_NUTS3_29-May-18/deu_adm_2006.shp')
# coffee time
e <- extract(Deu_crop,shp)
# add NUTS_ID as names to list
names(e) <- shp$NUTS_ID
为了计算每个 bin 每年的天数,我创建了一个函数,它使用 tidiverse
功能并使用 lapply
迭代整个提取列表(一个列表项目对应一个多边形):
# define bins
bins <- seq(-10,25,length.out = 5)
myfun <- function(ix){
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)) %>% # get years
select(- colname) %>% # drop colname column
mutate(bin1= (temp <= bins[1]) * 1) %>% # bin1
mutate(bin2= (temp > bins[1] & temp <= bins[2]) * 1) %>% # bin2
mutate(bin3= (temp > bins[2] & temp <= bins[3]) * 1) %>% # bin3
mutate(bin4= (temp > bins[3] & temp <= bins[4]) * 1) %>% # bin4
mutate(bin5= (temp > bins[4] & temp <= bins[5]) * 1) %>% # bin5
mutate(bin6= (temp > bins[5]) * 1) %>% select(-temp) %>% # bin6
group_by(year) %>% summarise_all(funs(sum)) %>% mutate(NUTS_ID = names(e)[ix]) # drop year, calculate occurences and add NUTS_ID
}
# create single dataframe
result <- do.call(rbind,lapply(1:length(e),function(ix) myfun(ix)))
快速浏览一下 result
变量:
result
# output:
#
# # A tibble: 6,864 x 8
# year bin1 bin2 bin3 bin4 bin5 bin6 NUTS_ID
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
# 1 1980 12 85 91 92 85 0 DEA54
# 2 1981 3 64 99 113 86 0 DEA54
# 3 1982 3 80 113 86 83 0 DEA54
# 4 1983 6 84 90 85 100 0 DEA54
# 5 1984 8 90 92 86 90 0 DEA54
# 6 1985 5 86 85 95 94 0 DEA54
# 7 1986 6 74 97 108 80 0 DEA54
# 8 1987 4 82 99 94 86 0 DEA54
# 9 1988 3 89 87 91 96 0 DEA54
#10 1989 8 103 92 73 89 0 DEA54
# # ... with 6,854 more rows
为了处理 bin,我首先根据整个数据的最小值和最大值计算 bin,然后使用新函数 createBins
将它们添加到每个多边形的提取物中。这将替换我原来解决方案中的 myfun
部分。
# new function
createBins <- function(df,bins_mat){
for (i in 1:nrow(bins_mat)){
bin <- sprintf('Bin%s;%s;%s',bins_mat[i,1],bins_mat[i,2],bins_mat[i,3])
if (i ==1) df <- df %>% mutate(!!bin := (temp >= bins_mat[i,2] & temp <= bins_mat[i,3])*1)
else df <- df %>% mutate(!!bin := (temp > bins_mat[i,2] & temp <= bins_mat[i,3])*1)
}
return(df)
}
# new version of myfun
myfun2 <- function(ix,bins_mat){
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)) %>% # get years
select(- colname) %>% # drop colname column
createBins(.,bins_mat) %>% select(-temp) %>%
group_by(year) %>% summarise_all(funs(sum)) %>% mutate(NUTS_ID = names(e)[ix])
}
# 11 values to create 10 interval bins
bins <- seq(min(cellStats(Deu_crop,'min')),min(cellStats(Deu_crop,'max')),length.out = 11)
# create a bin matrix (number, bin_minimum, bin_maximum) for later function
bins_mat <- cbind(1:10,bins[1:10],bins[2:11])
# create new result
result <- do.call(rbind,lapply(1:length(e),function(ix) myfun2(ix,binsmat)))
关于r - 处理栅格数据以离散化每日温度年度分布的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571015/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!