【问题标题】:Change unit in a netcdf variable in python在python中更改netcdf变量中的单位
【发布时间】:2023-04-03 10:30:01
【问题描述】:

我在 Python 中有一个 netCDF 文件,其中时间变量的单位是 days_since_Jan11900xarray 包无法读取该文件。它抛出错误

unable to decode time units 'days_since_Jan11900' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.

我不想使用decode_times,而只是将其更改为days_since_0。距 0000 年 1 月 1 日已过去了几天。

如何将 netCDF 中的单位更改为可读的单位?

<class 'netCDF4._netCDF4.Variable'>
float64 days(time)
    units: days_since_Jan11900
    long_name: calendar_days
unlimited dimensions: 
current shape = (87600,)
filling on, default _FillValue of 9.969209968386869e+36 used

【问题讨论】:

标签: python spatial netcdf


【解决方案1】:

如果您使用的是 Unix,您可以尝试使用 CDO 更改日历。以下应该可以解决您的问题:

cdo -L  -setreftime,0001-01-01  -settaxis,0001-01-01,0:00:00,1day  -setcalendar,standard infile.nc outfile.nc 

我已将时间设置为第 1 年,因为我认为任何有效日历中都没有第 0 年。

【讨论】:

  • 我正在使用标题中所述的python。
【解决方案2】:

按照我的要求,我自己在 Python 中解决了这个问题,而不是通过 cdo。这是我的做法,很简单:

ds['days'] = ds.days - 730966
ds.days.attrs['units'] = 'days since 1860-01-01'
ds.to_netcdf(path + 'name_of_file.nc')

730966 是我在days 数组中的起始值,因此我将其减去并将我的单位更改为适当的单位,在我的情况下为days since 1860-01-01

【讨论】:

    猜你喜欢
    • 2022-10-06
    • 2021-02-07
    • 2017-07-07
    • 1970-01-01
    • 2022-01-09
    • 2021-05-19
    • 2016-03-13
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多