HadGEM2-ES NetCDF文件中的时间问题

时间:2018-10-16 17:36:30

标签: python numpy python-xarray

我有文件:

tasmin_day_HadGEM2-ES_rcp45_r4i1p1_20151201-20251130.nc

当我运行代码时:

import xarray as xr
import matplotlib.pyplot as plt
path = r'C:\Users\alexandre\Downloads'
df = xr.open_mfdataset(path + r'\tasmin_day_HadGEM2-ES_rcp45_r4i1p1_20151201-20251130.nc')
plt.figure()
df['tasmin'].isel(lat=100, lon=100).plot(label='plot1')
print(df['tasmin'].time.min())
df['tasmin'].isel(lat=100, lon=100).plot(label='plot2')
print(df['tasmin'].time.min())
plt.legend()

我总是有不同的情节,例如:

enter image description here

如果我查看日期,可以从打印命令中找到:

    <xarray.DataArray 'time' ()>
array('1970-01-01T00:00:00.000000000', dtype='datetime64[ns]')
Coordinates:
    height   float64 ...
<xarray.DataArray 'time' ()>
array('2015-12-01T12:00:00.000000000', dtype='datetime64[ns]')
Coordinates:
    height   float64 ...

真正奇怪的是,数据始于2015年12月1日!

感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

经过一番搜索,我在xarray documentation 上找到了答案:

与许多实际数据集一样,该数据集并不完全遵循CF约定。意外的格式通常会导致xarray的自动解码失败。解决此问题的方法是在open_dataset中设置encode_cf = False以关闭所有对CF约定的使用,或者仅禁用麻烦的解析器。在这种情况下,我们将解码时间设置为False,因为此处的时间轴以xarray不需要的格式(整数360而不是像'360_day'这样的字符串)提供了日历属性。

因此,只需更改以下行:

df = xr.open_mfdataset(path + r'\tasmin_day_HadGEM2-ES_rcp45_r4i1p1_20151201-20251130.nc'

df = xr.open_mfdataset(path + r'\tasmin_day_HadGEM2-ES_rcp45_r4i1p1_20151201-20251130.nc', decode_times=False)