【问题标题】:Rename netCDF dimensions重命名 netCDF 维度
【发布时间】:2022-01-03 15:56:53
【问题描述】:

我想重新命名 .nc 文件的尺寸,从“纬度”到“纬度”,从“经度”到“经度”,以便以后能够将其与另一个 .nc 数据集结合起来。这是我的 .nc 文件的样子:

Dimensions:    (time: 1, latitude: 1037, longitude: 1345)
Coordinates:
  * latitude   (latitude) float32 37.7 37.7 37.69 37.69 ... 35.01 35.0 35.0
  * time       (time) datetime64[ns] 2021-11-23
  * longitude  (longitude) float32 -9.001 -8.999 -8.996 ... -5.507 -5.504 -5.501

Data variables:
    CHL        (time, latitude, longitude) float32 ...

Attributes: (12/38)
    FROM_ORIGINAL_FILE__Metadata_Conventions:  Unidata Dataset Discovery v1.0
    keywords:                                  satellite,observation,ocean
    summary:                                   These data are Level-3 satelli...
    history:                                   1637676190 Created by composit...
    netcdf_file_type:                          NETCDF4_CLASSIC
    contact:                                   email: cmems@pml.ac.uk
    ...                                        ...
    start_date:                                2021-11-23
    start_time:                                00:00:00 UTC
    stop_date:                                 2021-11-23
    stop_time:                                 23:59:00 UTC
    _CoordSysBuilder:                          ucar.nc2.dataset.conv.CF1Conve...
    source:      

我厌倦了使用此处找到的以下代码:

from netCDF4 import Dataset
path='dataset-oc-atl-chl-olci-l3-chl_300m_daily-rt_1637778183019.nc'
f=Dataset(path,'r+')
f.renameDimension(u'longitude',u'lon')
f.renameVariable(u'longitude',u'lon')
f.renameDimension(u'latitude',u'lat')
f.renameVariable(u'latitude',u'lat')
f.close()

但是弹出如下错误:

KeyError: '经度不是有效的维度名称'

有什么想法吗?谢谢!

【问题讨论】:

    标签: python netcdf dimensions nco


    【解决方案1】:

    NCO 的ncrename

    ncrename -d latitude,lat -d longitude,lon in.nc out.nc
    

    【讨论】:

    【解决方案2】:

    通过打印数据集的维度来检查维度u'longitude'是否实际存在:

    print(f.dimensions)
    

    或打印f.dimensions 的键,因为它是字典:

    print(f.dimensions.keys())
    

    (见:Dimensions in a netcdf4 file

    【讨论】:

    • 不,字典好像是空的...
    【解决方案3】:

    首先,感谢@user2314737,我发现我试图重命名的文件之一已损坏,并且包含尺寸的字典是空的。我更新了文件并找到了这个简单的解决方案来重命名尺寸:

    ds1 = xr.open_dataset("dataset-oc-atl-chl-olci-l3-chl_300m_daily-rt_1637844102280.nc")
    ds1
    

    [输出]

    <xarray.Dataset>
    Dimensions:  (time: 1, latitude: 1037, longitude: 1345)
    Coordinates:
      * latitude      (latitude) float32 37.7 37.7 37.69 37.69 37.69 ... 35.01 35.01 35.0 35.0
      * time     (time) datetime64[ns] 2021-11-23
      * longitude      (longitude) float32 -9.001 -8.999 -8.996 -8.993 ... -5.507 -5.504 -5.501
    Data variables:
        CHL      (time, lat, lon) float32 ...
    Attributes: (12/38)
    

    解决办法是:

    ds1 = ds1.rename({'latitude': 'lat','longitude': 'lon'})
    

    [输出]

    <xarray.Dataset>
    Dimensions:  (time: 1, lat: 1037, lon: 1345)
    Coordinates:
      * lat      (lat) float32 37.7 37.7 37.69 37.69 37.69 ... 35.01 35.01 35.0 35.0
      * time     (time) datetime64[ns] 2021-11-23
      * lon      (lon) float32 -9.001 -8.999 -8.996 -8.993 ... -5.507 -5.504 -5.501
    Data variables:
        CHL      (time, lat, lon) float32 ...
    Attributes: (12/38)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2015-03-03
      • 2015-02-05
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多