为了账号安全,请及时绑定邮箱和手机立即绑定

更改 python 中 netcdf 变量的单位

更改 python 中 netcdf 变量的单位

当年话下 2023-12-20 20:05:31
我在Python中有一个netCDF文件,其中时间变量的单位是days_since_Jan11900包无法读取的xarray。它抛出错误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_daysunlimited dimensions: current shape = (87600,)filling on, default _FillValue of 9.969209968386869e+36 used
查看完整描述

2 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

按照我的要求,我自己用 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。


查看完整回答
反对 回复 2023-12-20
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

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

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

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


查看完整回答
反对 回复 2023-12-20
  • 2 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信