Menu

Deleting variable but retaining dimensions only associated with that variable

Help
2014-01-22
2014-05-20
  • Nick Papior

    Nick Papior - 2014-01-22

    Hi all,

    Say I have a NetCDF file with this CDL:
    netcdf in {
    dimensions:
    dim1 = 2 ;
    dim2 = 2 ;
    variables:
    double var1(dim1) ;
    double var2(dim2) ;
    data:
    var1 = 1., 2. ;
    var2 = 2., 3. ;
    }

    I then want to delete var2 but retain the dim2 dimension. The complete sequence is:
    ncgen in.cdl -o in.nc
    ncks -x -v var2 in.nc out.nc
    And then I arrive at (ncdump -h out.nc):
    netcdf out {
    dimensions:
    dim1 = 2 ;
    variables:
    double var1(dim1) ;

    // global attributes:
    :history = "Wed Jan 22 09:38:51 2014: ncks -x -v var2 in.nc out.nc" ;
    :NCO = "4.4.0" ;
    }

    How can I retain the dim2 dimension? I.e. I don't want to delete dim2 even though no variable is using it.

    ncks -h does not show any apparent solution.

    / Nick

     
  • Charlie Zender

    Charlie Zender - 2014-01-22

    Hi Nick,
    You're right, NCO/ncks cannot do this because NCO always removes non-referenced dimensions from the output file.
    You're the first person ever to ask for the capability to retain them.
    I will add this to the TODO list and post again when we have a solution.
    cz

     
  • Nick Papior

    Nick Papior - 2014-01-22

    Sorry, for the extra work. :)
    Thanks for this great software!

    Just for the record, the use case is as follows.
    If you have several files, with a lot of same dimensions. Then doing ncks to reduce size would mean that you loose the information that is attached to all those files. So when I still need the dimension size I can't get it (of course you could add attributes to designate run etc.). Furthermore retaining dimensions allows "easy" checks if one has a python script which reads them in and does things.

    / Nick

     
  • Jeremy

    Jeremy - 2014-04-07

    Note sure if this is still an issue for you, but I had the same problem and was able to get around it by adding a dummy variable: extract variable of the same size, rename the dimension, rename the variable, and append it back to the file.

    % ncks -d dim1,1,2 -v var1 in.nc tmp.nc # the -d part is not necessary unless dim2 is of smaller dimensions than dim1.
    % ncrename -d dim1,dim2 tmp.nc
    % ncrename -O -v var1,DUM tmp.nc
    % ncks -A tmp.nc out.nc

     
  • Charlie Zender

    Charlie Zender - 2014-04-09

    Thank you for your hint, Jeremy. Let me mention that the two ncrename
    commands can be combined into one command. The original poster
    may chafe at adding a variable just to retain the dimension and its size.
    Yet this is the best workaround available at the moment.
    cz

     
  • Charlie Zender

    Charlie Zender - 2014-04-15

    Good news. Pedro has implemented the desired capability. Documentation is forthcoming with the 4.4.4 release. For now it works in the ncks daily snapshot with the new --rad (retain all dimensions) switch illustrated here. Using --rad, ncks retains the lat dimension in foo2.nc:
    ncks -O -C -v lat,lon ~/nco/data/in.nc ~/foo.nc
    ncks --rad -O -x -v lat ~/foo.nc ~/foo2.nc
    NB: ncks does not yet print the lat dimension, as it is an orphan dimension. We will fix that soon.
    cz

     
  • Nick Papior

    Nick Papior - 2014-05-20

    Superb, thanks!

     

Log in to post a comment.