Search code examples
arraysrmultidimensional-arraynetcdfnetcdf4

How to invert a multidimensional array stored in NetCDF along 1 dimension?


I have netCDF containing multidimensional array of following shape:

[1:424, 1:412, 1:3, 1:130]

..and I would like to invert along 2nd dimension and get:

[1:424, 412:1, 1:3, 1:130]

I tried:

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

nc_close( test_object )

..but this does not invert the object and also I am not getting any error.


Solution

  • In your code you read the data into your R workspace and inverted the data, but you never wrote it back to the NetCDF-file. Assuming that your variable is called "hwmid" in the NetCDF-file, you can write to the file after inverting using:

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