Search code examples
netcdfnetcatnetcdf4nco

How can I swap the dimensions of a netCDF file?


I have a

dimensions:
    time = 1000 ;
    xr = 100 ;
variables:
    int time(time) ;
        time:long_name = "time since time reference (instant)" ;
        time:units = "hours since 2010-05-01" ;
    double v1(xr, time) ;
        v1:long_name = "V1" ;
        averageInstantRunoff:units = "m s-1" ;
    double v1(time, xr) ;
        v1:long_name = "V1" ;
        averageInstantRunoff:units = "m s-1" ;
    int xr(xr) ;
        xr:long_name = "xr index" ;
        xr:units = "-" ;

Here, I want to make variable v1 to have dimensions of (time, xr) instead of (xr,time).

I do this using:

ncpdq -v v1 -a time,xr test.nc test2.nc

But ofcourse, it doesn't copy v2. I do it over large files with large number of variables and I only want to do it for a single variable.

How can I do this?


Solution

  • You can also the use ncap2 permute() method:

    ncap2 -s 'v1_prm=v1.permute($time,$xr)' in.nc out.nc
    

    but then your output will have a v1 and a v1_prm. Either way it's a two line process with NCO because you need a second step to append (ncpdq method) or remove the extra variable (ncap2 method).