【问题标题】:Reordering an aggregated stars object重新排序聚合的星星对象
【发布时间】:2021-06-21 14:40:57
【问题描述】:

我正在使用 R 中的 stars 包对 netcdf 栅格执行时间聚合。通常对象具有 (X, Y, Time) 维度,并且在进行时间平均后,得到一个维度排序为 (Time, X , Y)。如何将维度的顺序改回原来的顺序(X、Y、T)?

我一直在搜索包 vignettes 和可能的函数示例以查找方法,但没有任何运气。我觉得我可能遗漏了一些简单而明显的东西......

这是一个代表:

library(stars)
#> Loading required package: abind
#> Warning: package 'abind' was built under R version 4.0.3
#> Loading required package: sf
#> Warning: package 'sf' was built under R version 4.0.4
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1

tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(c(tif, tif, tif), along = "band")
x
#> stars object with 3 dimensions and 1 attribute
#> attribute(s), summary of first 1e+05 cells:
#>   L7_ETMs.tif    
#>  Min.   : 47.00  
#>  1st Qu.: 65.00  
#>  Median : 76.00  
#>  Mean   : 77.34  
#>  3rd Qu.: 87.00  
#>  Max.   :255.00  
#> dimension(s):
#>      from  to  offset delta                       refsys point values x/y
#> x       1 349  288776  28.5 UTM Zone 25, Southern Hem... FALSE   NULL [x]
#> y       1 352 9120761 -28.5 UTM Zone 25, Southern Hem... FALSE   NULL [y]
#> band    1  18      NA    NA                           NA    NA   NULL

time = as.Date("2021-03-24") + 1:18
x = st_set_dimensions(x, "band", values = time)

y = aggregate(x, by = "3 days", "mean")
y
#> stars object with 3 dimensions and 1 attribute
#> attribute(s):
#>   L7_ETMs.tif    
#>  Min.   :  5.00  
#>  1st Qu.: 56.67  
#>  Median : 71.67  
#>  Mean   : 68.91  
#>  3rd Qu.: 84.00  
#>  Max.   :255.00  
#> dimension(s):
#>      from  to     offset  delta                       refsys point values x/y
#> time    1   6 2021-03-25 3 days                         Date    NA   NULL    
#> x       1 349     288776   28.5 UTM Zone 25, Southern Hem... FALSE   NULL [x]
#> y       1 352    9120761  -28.5 UTM Zone 25, Southern Hem... FALSE   NULL [y]

reprex package (v0.3.0) 于 2021 年 3 月 24 日创建

【问题讨论】:

    标签: r gis spatial


    【解决方案1】:

    答案很简单,我应该知道的!由于 stars 对象在一定程度上是一个数组列表,因此可以使用 aperm 对维度进行重新排序,就像使用普通的 n 维数组一样。

    aperm(y, c(2, 3, 1))
    #> stars object with 3 dimensions and 1 attribute
    #> attribute(s):
    #>   L7_ETMs.tif    
    #>  Min.   :  5.00  
    #>  1st Qu.: 56.67  
    #>  Median : 71.67  
    #>  Mean   : 68.91  
    #>  3rd Qu.: 84.00  
    #>  Max.   :255.00  
    #> dimension(s):
    #>      from  to     offset  delta                       refsys point values x/y
    #> x       1 349     288776   28.5 UTM Zone 25, Southern Hem... FALSE   NULL [x]
    #> y       1 352    9120761  -28.5 UTM Zone 25, Southern Hem... FALSE   NULL [y]
    #> time    1   6 2021-03-25 3 days                         Date    NA   NULL
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多