使用 TERRA 包根据插值栅格计算长期平均月降雨量?
我在 R 中有一个名为 IDW3 的 SpatRaster 对象,使用 IDW 插值方法进行估计。我有 nlyr = 240
,包含 12 个月 x 20 年。我需要计算各图层的长期平均月降雨量,以便最终得到 nlyr = 12
,其中每一图层代表一个日历月(一月 - 十二月)。
我尝试使用下面的代码,按照这个线程 计算长期每日意味着来自 R 中的光栅,但我想验证我使用的代码。
有什么想法和意见吗?
idw3
#> class : SpatRaster
#> dimensions : 723, 449, 240 (nrow, ncol, nlyr)
#> resolution : 100, 100 (x, y)
#> extent : 624698.7, 669598.7, 640507.8, 712807.8 (xmin, xmax, ymin, ymax)
#> coord. ref. :
#> sources : May 1998_masked_idw3.asc
#> May 1999_masked_idw3.asc
#> May 2000_masked_idw3.asc
#> ... and 237 more source(s)
#> names : Jan 1998, Jan 1999, Jan 2000, Jan 2001, #> Jan 2002, Jan 2003, ...
#> min values : ? , ? , ? , ? , ? , ? , ...
#> max values : ? , ? , ? , ? , ? , ? , ...
## CALCULATE THE LONGTERM MONTHLY MEANS
# get the months substring
month.ltm <- substr(my, 1,3)
# calculate the ltm using tapp funtion in terra
idw3.ltm <- tapp(idw3, month.ltm, mean)
names(idw3.ltm)
#> [1] "May" "Apr" "Aug" "Jan" "Sep" "Jul" "Jun" "Feb" "Dec"
#> [10] "Nov" "Oct" "Mar"
I have a SpatRaster object in R called IDW3, estimated using IDW interpolation method. I have nlyr = 240
, containing 12 months x 20 years. I need to calculate the long-term mean monthly rainfall from the layers, so that I get nlyr = 12
at the end, in which each layer represents one calendar month (Jan - Dec).
I have tried using the code below, following this thread calculating long term daily means from a RASTER in R, but I want to verify the code I used.
Any thoughts and comments please?
idw3
#> class : SpatRaster
#> dimensions : 723, 449, 240 (nrow, ncol, nlyr)
#> resolution : 100, 100 (x, y)
#> extent : 624698.7, 669598.7, 640507.8, 712807.8 (xmin, xmax, ymin, ymax)
#> coord. ref. :
#> sources : May 1998_masked_idw3.asc
#> May 1999_masked_idw3.asc
#> May 2000_masked_idw3.asc
#> ... and 237 more source(s)
#> names : Jan 1998, Jan 1999, Jan 2000, Jan 2001, #> Jan 2002, Jan 2003, ...
#> min values : ? , ? , ? , ? , ? , ? , ...
#> max values : ? , ? , ? , ? , ? , ? , ...
## CALCULATE THE LONGTERM MONTHLY MEANS
# get the months substring
month.ltm <- substr(my, 1,3)
# calculate the ltm using tapp funtion in terra
idw3.ltm <- tapp(idw3, month.ltm, mean)
names(idw3.ltm)
#> [1] "May" "Apr" "Aug" "Jan" "Sep" "Jul" "Jun" "Feb" "Dec"
#> [10] "Nov" "Oct" "Mar"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
tapp
来实现。以下是一些示例数据如果数据按年排序,然后按月排序,您现在可以这样做。
在其他情况下,您可能必须创建另一个索引来匹配需要组合的图层。如果您的数据有时间戳,则有一些快捷方式。在这种情况下,您可以使用
index="months"
You can use
tapp
for that. Here are some example dataIf the data are ordered by year, and then by month, you can now do
In other cases you may have to create another index to match the layers that need to be combined. If your data has a time-stamp, there are some shortcuts. In this case you can use
index="months"
自从您发布此问题以来已经有一段时间了,但我会尝试在下一个问题中解决此问题。由于您的示例似乎无法完全重现,因此让我使用自己的数据来实现此目的。
我使用了 GPCC v2022 数据 - 过去二十年的月度数据准确地说,分辨率为 0.5° 的数据 - 来自德国气象局。
正如您所看到的,这个数据集至少在信息方面与您的数据集非常相似(除了 crs、范围和分辨率)。
nlyr = 240
包含每月降水数据的 SpatRaster 对象堆栈。最显着的不同是time
属性范围从 2001-01-01 到 2020-12-01。但是,基本上我解决了您的问题,使用
fun = Mean
构建适当的基于时间的索引向量作为tapp
的输入:从我的角度来看,结果似乎是合理的,但因为这是我第一次使用
tapp
,我想通过手动重新计算来确保该函数的行为符合预期:相同的结果,唷。
编辑:
昨天发生了一些今天无法重现的奇怪行为之后,我可以确认使用
index = Months.abb
会得到与使用index = "months 相同的结果“
(正如罗伯特在下面的评论中建议的那样):It's been some time since you posted this, but I'd try to solve this for the next one asking. Since your example does not seem to be fully reproducible, let my use my own data for this purpose.
I used GPCC v2022 data - the last two decades of monthly data at 0.5° resolution to be precise - from German Weather Service.
As you can see, this dataset is quite similar to yours in terms of information at least (except for crs, extent and resolution). A stack of SpatRaster objects with
nlyr = 240
containing monthly precipitation data. What differs most notably is thetime
attribute ranging from 2001-01-01 to 2020-12-01.However, basically I approached your issue constructing an appropriate time-based index vector as input to
tapp
usingfun = mean
:The result seems plausible from my point of view, but since this is the first time I used
tapp
, I want to make sure the function behaves as expected by re-calculating manually:Same results, phew.
Edit:
After some weird behaviour yesterday which cannot be reproduced today I can confirm that using
index = months.abb
gives you the same results as usingindex = "months"
(as suggested by Robert below in the comments):