如何在stars对象中添加时间维度并保留多属性
问题描述
已从CHELSA下载并导入1月、2月、3月的4个生物气候变量,为每个月份创建了SpatRaster栈(jan_stack、feb_stack、mar_stack),各栈结构一致:
> jan_stack class : SpatRaster dimensions : 146, 212, 4 (nrow, ncol, nlyr) resolution : 0.008333333, 0.008333333 (x, y) extent : 13.01653, 14.78319, 41.68319, 42.89986 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) sources : CHELSA_bio_01_1981-2010_V2.1_clipped.tif CHELSA_bio_07_1981-2010_V2.1_clipped.tif CHELSA_bio_13_1981-2010_V2.1_clipped.tif CHELSA_bio_14_1981-2010_V2.1_clipped.tif names : CHELSA_~10_V2.1, CHELSA_~10_V2.1, CHELSA_~10_V2.1, CHELSA_~10_V2.1 min values : -0.95, 16.2, 59.8, 18.9 max values : 16.65, 29.5, 241.7, 92.8
目标是创建包含时间维度的stars数据立方体,每个月份栈对应一个代表日期,同时保留4个生物气候属性。但尝试过程中遇到问题:
- 转换为stars对象后,属性被合并到
attributes维度,仅显示1个属性 - 使用
split()拆分后丢失维度,无法添加时间维度 - 手动添加时间维度后,无法恢复4个独立属性
期望得到的stars对象结构:
stars object with 3 dimensions and 4 attributes attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. CHELSA_bio_01_1981-2010_V2.1 -0.95 9.35 12.35 12.03118 15.45 16.65 CHELSA_bio_07_1981-2010_V2.1 16.20 25.30 27.20 25.69690 27.80 29.50 CHELSA_bio_13_1981-2010_V2.1 59.80 84.90 111.20 119.10602 147.40 241.70 CHELSA_bio_14_1981-2010_V2.1 18.90 33.20 43.90 44.37600 53.60 92.80 dimension(s): from to offset delta refsys point x/y x 1 212 13.02 0.008333 WGS 84 FALSE [x] y 1 146 42.9 -0.008333 WGS 84 FALSE [y] date 1 3 NA NA Date NA 2009-01-01, 2009-02-01, 2009-03-01
解决方案
步骤1:为单个月份数据添加时间维度
先拆分attributes维度为独立属性,再添加对应月份的时间维度:
# 加载所需包 library(stars) library(terra) # 定义每个月份的代表日期 dates <- as.Date(c("2009-01-01", "2009-02-01", "2009-03-01")) # 处理1月数据:拆分属性后添加时间维度 jan_stars_split <- split(st_as_stars(jan_stack)) jan_stars_time <- st_set_dimensions(jan_stars_split, which = 3, values = dates[1], names = "date") # 同理处理2月、3月数据 feb_stars_split <- split(st_as_stars(feb_stack)) feb_stars_time <- st_set_dimensions(feb_stars_split, which = 3, values = dates[2], names = "date") mar_stars_split <- split(st_as_stars(mar_stack)) mar_stars_time <- st_set_dimensions(mar_stars_split, which = 3, values = dates[3], names = "date")
步骤2:合并为完整数据立方体
使用c()函数按时间维度合并三个月份的stars对象:
# 合并为包含x、y、date三个维度+4个属性的数据立方体 climate_cube <- c(jan_stars_time, feb_stars_time, mar_stars_time) # 查看结果 climate_cube
关键说明
split()将attributes维度拆分为独立属性,此时对象为2个空间维度(x/y)+4个属性st_set_dimensions()添加单值时间维度(每个月份对应一个日期),此时对象变为3个维度(x/y/date)+4个属性- 合并后,时间维度会自动扩展为3个值,对应1-3月的日期,同时保留4个生物气候属性
验证结果
运行上述代码后,climate_cube的结构将符合预期:
> climate_cube stars object with 3 dimensions and 4 attributes attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. CHELSA_bio_01_1981-2010_V2.1 -0.95 9.35 12.35 12.03118 15.45 16.65 CHELSA_bio_07_1981-2010_V2.1 16.20 25.30 27.20 25.69690 27.80 29.50 CHELSA_bio_13_1981-2010_V2.1 59.80 84.90 111.20 119.10602 147.40 241.70 CHELSA_bio_14_1981-2010_V2.1 18.90 33.20 43.90 44.37600 53.60 92.80 dimension(s): from to offset delta refsys point values x/y x 1 212 13.02 0.008333 WGS 84 FALSE NULL [x] y 1 146 42.9 -0.008333 WGS 84 FALSE NULL [y] date 1 3 NA NA Date NA 2009-01-01,2009-02-01,2009-03-01
内容的提问来源于stack exchange,提问作者Rocío Beatriz Cortés
相关产品推荐
相关产品推荐

