【问题标题】:How to remove/delete dimension from a netcdf file without modifying other variables?如何在不修改其他变量的情况下从 netcdf 文件中删除/删除维度?
【发布时间】:2021-03-19 07:49:53
【问题描述】:

需要帮助。请查看并提出解决方案。请注意,我是初学者。

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" ;

我想删除变量expver(因为它会阻止与其他 *.nc 文件合并)。 即使在删除之后(使用nco 的命令ncks -C -O -x -v expver Lw_15_21.nc test.nc),expver 仍然保留在维度和变量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" ;

如何从维度和变量 str 中删除 expver,同时保持其他变量不变。我也尝试过ncwa -a,但出现分段错误(核心转储)错误。 这意味着我想得到以下输出。

假设文件名为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" ;

谢谢。

【问题讨论】:

  • 不清楚应该如何删除它。 expver 有两个值。这个维度必须被折叠。但是你想怎么做呢?平均?
  • 我可以使用 cdo splitlevel 拆分维度“expver”。即使我也这样做了,得到了两个输出,其中维度部分“expver”显示两个输出(t1.nc 和 t2.nc)的 1。现在下一步怎么办?我完全糊涂了。 --- 苏米克达斯
  • 在 CDO 中查找 reduce_dim 选项。这可以减少以摆脱只有一个值的维度
  • 能否请您提供reduce_dim的完整命令?谢谢。
  • cdo --reduce_dim -copy in.nc out.nc,应该可以工作

标签: python netcdf cdo-climate nco


【解决方案1】:

请注意,您也可以使用 python xarray 来删除坐标。 示例:

import xrray as xr

读取数据

data = xr.open_dataset("test.nc)

删除expver坐标

data = data.drop("expver")

如果expver坐标包含不同的值,也可以用dataarray.sel方法选择一个,例如:

data = data.sel(expver=1)

你可以使用前面的代码来删除expver坐标。

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2022-08-04
    • 2016-07-04
    相关资源
    最近更新 更多