从 R 中的 RASTER 计算长期日均值 [英] calculating long term daily means from a RASTER in R

查看:74
本文介绍了从 R 中的 RASTER 计算长期日均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个名为 z500 的 RasterBrick 对象(500 hPa 等压面的等位势高度).它有以下描述:

I have a RasterBrick object in R called z500 (geopotential height of 500 hPa isobaric surface). Its got following description:

class      : RasterBrick 
dimensions : 221, 121, 26741, 59900  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25  (x, y)
extent     : 14.875, 45.125, 24.875, 80.125  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 
source     : C:/Users/Adam/AppData/Local/Temp/Rtmpct79pT/raster/r_tmp_2020-10-23_122821_43760_36114.grd 
names      : X1979.01.01.00.50.39, X1979.01.01.06.50.39, X1979.01.01.12.50.39, X1979.01.01.18.50.39, X1979.01.02.00.50.39, X1979.01.02.06.50.39, X1979.01.02.12.50.39, X1979.01.02.18.50.39, X1979.01.03.00.50.39, X1979.01.03.06.50.39, X1979.01.03.12.50.39, X1979.01.03.18.50.39, X1979.01.04.00.50.39, X1979.01.04.06.50.39, X1979.01.04.12.50.39, ... 
min values :             4769.189,             4746.817,             4732.399,             4728.390,             4753.844,             4795.936,             4853.804,             4893.073,             4947.256,             4990.038,             5002.388,             5007.603,             4996.633,             5000.276,             5014.048, ... 
max values :             5815.242,             5813.691,             5810.005,             5814.445,             5810.501,             5802.763,             5810.199,             5813.260,             5812.462,             5805.134,             5807.656,             5807.419,             5806.255,             5801.406,             5811.816, ... 

所以我得到了 59 900 个层,因为从 1979 年 1 月 1 日到 2019 年 12 月 31 日,我每天每 6 小时就有一次 z500 值.

So I got 59 900 layers, as I have values of z500 every 6 hours every day since 1.1.1979 till 31.12.2019.

现在,我想以某种方式对 RasterBrick 进行子集化,以便在每个栅格中,我在整个时间序列中都有来自同一日历日的值.所以它应该是一个子栅格中的 4*41 层(每天 4 个值,41 年).最后,我很容易就能找到 z500 的长期日均值,但上述步骤对我来说并不容易.

Now, I would like to subset the RasterBrick somehow, so that in each raster, I have values from the same calendar day throughout the whole time series. So it should be 4*41 layers in one sub-raster (4 values a day, 41 years). And finally, I would easily be able to find long term daily mean of z500, but the above described steps are not easy to do for me.

感谢每一个帮助,谢谢!

Every help appreciated, thanks!

推荐答案

您可以从 z500 的名称中提取日期并将其用作索引

You can extract the days from the names of z500 and use these as indices

# n <- names(z500)
# example data
n <- c("X1979.01.01.00.50.39", "X1979.01.01.06.50.39", "X1979.01.01.12.50.39", "X1979.01.02.00.50.39", "X1979.01.02.06.50.39", "X1979.02.01.12.50.39", "X1980.01.01.00.50.39", "X1980.01.01.06.50.39", "X1980.01.01.12.50.39", "X1980.01.02.00.50.39", "X1980.01.02.06.50.39", "X1980.02.01.12.50.39")

获取相关部分(月-日)

Get the relevant part (month-day)

md <- substr(n, 7, 11)
md 
# [1] "01.01" "01.01" "01.01" "01.02" "01.02" "02.01" "01.01" "01.01" "01.01"
#[10] "01.02" "01.02" "02.01"

并将其与 raster::stackApply

x <- stackApply(z500, md, mean)

你可以像这样子集(但为每一天创建一个对象是一个非常糟糕的主意).

You can subset like this (but creating an object for each day is a really bad idea).

jan1 <- z500[[md ==  "01.01"]]

这篇关于从 R 中的 RASTER 计算长期日均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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