'How to remove/delete dimension from a netcdf file without modifying other variables?

Need help. Please have a look and suggest a solution. Note that I'm a beginner.

I have a netcdf file named 'Lw_15_21.nc' 
netcdf Lw_15_21 {
dimensions:
        longitude = 135 ;
        latitude = 129 ;
        expver = 2 ;
        time = 54247 ;
variables:
        float longitude(longitude) ;
                longitude:units = "degrees_east" ;
                longitude:long_name = "longitude" ;
        float latitude(latitude) ;
                latitude:units = "degrees_north" ;
                latitude:long_name = "latitude" ;
        int expver(expver) ;
                expver:long_name = "expver" ;
        int time(time) ;
                time:units = "hours since 1900-01-01 00:00:00.0" ;
                time:long_name = "time" ;
                time:calendar = "gregorian" ;
        short str(time, expver, latitude, longitude) ;
                str:scale_factor = 22.4949414798651 ;
                str:add_offset = -574761.74747074 ;
                str:_FillValue = -32767s ;
                str:missing_value = -32767s ;
                str:units = "J m**-2" ;
                str:long_name = "Surface net thermal radiation" ;
                str:standard_name = "surface_net_upward_longwave_flux" ;

And I want to delete the variable expver (since it prevents merging with other *.nc files). Even after deleting (using nco's command ncks -C -O -x -v expver Lw_15_21.nc test.nc), the expver still remains in dimension and in varible str.

netcdf test {
dimensions:
        latitude = 129 ;
        longitude = 135 ;
        time = 54247 ;
        expver = 2 ;
variables:
        float latitude(latitude) ;
                latitude:units = "degrees_north" ;
                latitude:long_name = "latitude" ;
        float longitude(longitude) ;
                longitude:units = "degrees_east" ;
                longitude:long_name = "longitude" ;
        short str(time, expver, latitude, longitude) ;
                str:scale_factor = 22.4949414798651 ;
                str:add_offset = -574761.74747074 ;
                str:_FillValue = -32767s ;
                str:missing_value = -32767s ;
                str:units = "J m**-2" ;
                str:long_name = "Surface net thermal radiation" ;
                str:standard_name = "surface_net_upward_longwave_flux" ;
        int time(time) ;
                time:units = "hours since 1900-01-01 00:00:00.0" ;
                time:long_name = "time" ;
                time:calendar = "gregorian" ;

How to remove the expver from dimension and variable str while keeping others constant. I also tried ncwa -a but got a segmentation fault (core dumped) error. That means I would like to get the following output.

Assuming the file name test1.nc

netcdf test1 {
dimensions:
        latitude = 129 ;
        longitude = 135 ;
        time = 54247 ;
variables:
        float latitude(latitude) ;
                latitude:units = "degrees_north" ;
                latitude:long_name = "latitude" ;
        float longitude(longitude) ;
                longitude:units = "degrees_east" ;
                longitude:long_name = "longitude" ;
        short str(time, latitude, longitude) ;
                str:scale_factor = 22.4949414798651 ;
                str:add_offset = -574761.74747074 ;
                str:_FillValue = -32767s ;
                str:missing_value = -32767s ;
                str:units = "J m**-2" ;
                str:long_name = "Surface net thermal radiation" ;
                str:standard_name = "surface_net_upward_longwave_flux" ;
        int time(time) ;
                time:units = "hours since 1900-01-01 00:00:00.0" ;
                time:long_name = "time" ;
                time:calendar = "gregorian" ;

Thank you.



Solution 1:[1]

Note that you can also use python xarray to drop the coordinate. Example:

import xrray as xr

read the data

data = xr.open_dataset("test.nc)

drop the expver coordinate

data = data.drop("expver")

And if the expver coordinate contains different values, you can also select one with the datarray.sel method, example:

data = data.sel(expver=1)

And you can use the previous code to drop the expver coordinate.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1