ncks copy variable attributes

  • Hi,

    I'm trying to use ncks to copy the attributes of a variable to the same variable name in another file. I would like to only copy the attributes and not the values of the variable or any of the associated coordinate variables.

    From what I can understand from the documentation, ncks normally changes the variable and associated coordinate variable values when the input and output names are the same. Is there a way to get around this?


  • Charlie Zender
    Charlie Zender

    your understanding of ncks is correct. there is no elegant way to do what you wish with ncks.
    i have added this feature request as TODO nco1105.
    can anyone think of a quick way to do what Andrew wishes?

  • Russell K. Rew
    Russell K. Rew

    There's this function in the netCDF public C API:
           int nc_copy_att(int ncid, int varid_in, const char name, int ncid_out, int varid_out)


  • Thanks Charlie and Russ!

  • Charlie Zender
    Charlie Zender

    I just committed some changes to ncks that enable your request.
    The switches were there, it was just a matter of connecting them.
    When copying/subsetting/appending files (as opposed to printing them), the
    copying of data, variable metadata, and global/group metadata are now turned OFF by -H, -m, and -M, respectively.
    This is the opposite sense in which these switches work when _printing_ a file.
    To prove/demonstrate their use:

    # Extract naked (data-only) copies of two variables
    ncks -h -M -m -O -C -v one,three_dmn_rec_var ~/nco/data/ ~/
    # Change values to be sure original values are not copied in following step
    ncap2 -O -v -s 'one=2*one;three_dmn_rec_var=73+0.*three_dmn_rec_var' ~/nco/data/ ~/
    # Copy metadata but not data to
    ncks -A -C -H -v one,three_dmn_rec_var ~/ ~/

    Variables in now contain data from and metadata from

    This functionality will be in NCO 4.3.7.
    If you can, I would appreciate it if you would install the current snapshot
    and let us know if it works for you.


  • Thanks so much Charlie.

    I'm using NCO from macports, which is currently at version 4.3.6.
    Is there another way that I could install the snapshot?