Project

General

Profile

Remove a Variable from a nc file

Added by Jeisson Javier Leal Rojas almost 2 years ago

Hi all.

I have two nc files: precipitation (pre.nc) and temperature(temp.nc).

My temp.nc files has a extra variable that I do not need.

This is how my pre.nc file looks like when doing ncdump -h pre.nc:

dimensions:
        time = UNLIMITED ; // (750 currently)
        lon = 3600 ;
        lat = 1400 ;
variables:
        int time(time) ;
                time:standard_name = "time" ;
                time:units = "minutes since 2020-1-20 00:00:00" ;
                time:calendar = "proleptic_gregorian" ;
                time:axis = "T" ;
        double lon(lon) ;
                lon:standard_name = "longitude" ;
                lon:long_name = "longitude" ;
                lon:units = "degrees_east" ;
                lon:axis = "X" ;
        double lat(lat) ;
                lat:standard_name = "latitude" ;
                lat:long_name = "latitude" ;
                lat:units = "degrees_north" ;
                lat:axis = "Y" ;
        double pre(time, lat, lon) ;
                pre:standard_name = "pr" ;
                pre:long_name = "precipitation flux" ;
                pre:units = "kg m-2 s-1" ;
                pre:param = "52.1.0" ;
                pre:_FillValue = -9999. ;
                pre:missing_value = -9999. ;
                pre:level_type = "atmosphere" ;
                pre:cell_methods = "time: mean" ;

And this is how my temp.nc file looks like when doing ncdump -h temp.nc:

dimensions:
        time = UNLIMITED ; // (750 currently)
        lon = 3600 ;
        lat = 1400 ;
        height = 1 ;
variables:
        int time(time) ;
                time:standard_name = "time" ;
                time:units = "minutes since 2020-1-20 00:00:00" ;
                time:calendar = "proleptic_gregorian" ;
                time:axis = "T" ;
        double lon(lon) ;
                lon:standard_name = "longitude" ;
                lon:long_name = "longitude" ;
                lon:units = "degrees_east" ;
                lon:axis = "X" ;
        double lat(lat) ;
                lat:standard_name = "latitude" ;
                lat:long_name = "latitude" ;
                lat:units = "degrees_north" ;
                lat:axis = "Y" ;
        double height(height) ;
                height:standard_name = "height" ;
                height:long_name = "height" ;
                height:units = "m" ;
                height:positive = "up" ;
                height:axis = "Z" ;
        double tavg(time, height, lat, lon) ;
                tavg:standard_name = "tas" ;
                tavg:long_name = "temperature in 2m" ;
                tavg:units = "K" ;
                tavg:param = "0.0.0" ;
                tavg:_FillValue = -9999. ;
                tavg:missing_value = -9999. ;
                tavg:cell_methods = "time: mean" ;

As you can see for temp.nc I have this double height(height) variable I want to remove so temp.nc will look like pre.nc.

I've checked previous posts such as https://code.mpimet.mpg.de/boards/2/topics/12103, but that one in particular uses sellevel to remove one value of a variable, and not the variable itself.

Thanks in advance


Replies (5)

RE: Remove a Variable from a nc file - Added by Estanislao Gavilan almost 2 years ago

Hi,

you can either use the command "-selname" or "-delname". The first selects a variable, and the second removes it.

Regards,

Estanislao

RE: Remove a Variable from a nc file - Added by Karin Meier-Fleischer almost 2 years ago

Hi Jeisson,

another approach is to reduce the 1-size height dimension:

cdo --reduce_dim -copy temp.nc temp_1.nc

See Options https://code.mpimet.mpg.de/projects/cdo/embedded/cdo.pdf#subsection.1.2.1

RE: Remove a Variable from a nc file - Added by Jeisson Javier Leal Rojas almost 2 years ago

Dear Estanislao,

I tried before the delname and tried your suggestion with selname, but it seems that height is not a valid Variable name:

cdo selname,height temp.nc temp_out.nc

propt:

cdo    selname (Warning): Variable name height not found!

Dear Karin,

I tried your suggestion, and it seems to work!

cdo --reduce_dim -copy temp.nc temp_out.nc

And this is how temp_out looks like:

dimensions:
        time = UNLIMITED ; // (750 currently)
        lon = 3600 ;
        lat = 1400 ;
variables:
        int time(time) ;
                time:standard_name = "time" ;
                time:units = "minutes since 2020-1-20 00:00:00" ;
                time:calendar = "proleptic_gregorian" ;
                time:axis = "T" ;
        double lon(lon) ;
                lon:standard_name = "longitude" ;
                lon:long_name = "longitude" ;
                lon:units = "degrees_east" ;
                lon:axis = "X" ;
        double lat(lat) ;
                lat:standard_name = "latitude" ;
                lat:long_name = "latitude" ;
                lat:units = "degrees_north" ;
                lat:axis = "Y" ;
        double tavg(time, lat, lon) ;
                tavg:standard_name = "tas" ;
                tavg:long_name = "temperature in 2m" ;
                tavg:units = "K" ;
                tavg:param = "0.0.0" ;
                tavg:_FillValue = -9999. ;
                tavg:missing_value = -9999. ;
                tavg:cell_methods = "time: mean" ;

Do you happen to know why delname or selname did not work?

Do you mind to explain me a bit more what cdo --reduce_dim -copy does on the background?

Thank you all,

Jeisson Leal.

RE: Remove a Variable from a nc file - Added by Ralf Mueller almost 2 years ago

Jeisson Javier Leal Rojas wrote in RE: Remove a Variable from a nc file:

Dear Estanislao,

[...]

Do you happen to know why delname or selname did not work?

selname and delname like most other operators work on data variables. heights is a coordinate variable (according to CF-convention), so CDO does not access them directly - only via coordinate related operations, e.g. intlevel or so.

Do you mind to explain me a bit more what cdo --reduce_dim -copy does on the background?

there is nothing much to tell:
  • copy performs a copy
  • reduce_dim deletes dimensions of size 1

you need both here, because --reduce_dim is an option that can be given to every operators. In order to apply it to your data, you need a simple operators, which lets your data untouched. hence: copy

Thank you all,

Jeisson Leal.

    (1-5/5)