R语言 如何将NetCDF中存储的多维数组沿沿着反转?

8yoxcaq7  于 7个月前  发布在  Etcd
关注(0)|答案(2)|浏览(72)

我有netCDF包含以下形状的多维数组:
[1:424,1:412,1:3,1:130]
..我想沿着沿着二维反转并得到:
[1:424,412:1,1:3,1:130]
我试过了:

test_object <- nc_open("~/work/macro/COOR_2_INDICES/test.nc")
hwmid <- ncvar_get(test_object)
    
hwmid<-hwmid[,412:1,,]

nc_close( test_object )

字符串
..但这并没有反转对象,我也没有得到任何错误。

bf1o4zei

bf1o4zei1#

假设对象是数组类型,在下面的三维示例中,切口的顺序可以改变如下:

> three_d_array <- array(
+   1:8,
+   dim = c(2, 2, 2),
+   dimnames = list(
+     c("one", "two"),
+     c("ein", "zwei"),
+     c("un", "deux")
+   )
+ )
> three_d_array[,1:2,]
, , un

    ein zwei
one   1    3
two   2    4

, , deux

    ein zwei
one   5    7
two   6    8

> three_d_array <- three_d_array[,2:1,]
> three_d_array
, , un

    zwei ein
one    3   1
two    4   2

, , deux

    zwei ein
one    7   5
two    8   6

字符串
类似的行为在更高的维度中也应该发生。

abithluo

abithluo2#

在您的代码中,您将数据读入R工作区并反转数据,但您从未将其写回NetCDF文件。假设您的变量在NetCDF文件中称为“hwmid”,您可以在反转后使用以下命令写入文件:

ncvar_put(nc = test_object,
    varid = "hwmid",
    vals = hwmid,
    start = c(1,1,1,1),
    count = c(424, 412, 3, 130))

字符串

相关问题