xarray:保存后时间错误
xarray: wrong time after saving
任何人都可以重现 xarray 在使用大值节省时间时的这种行为吗?我对这里发生的事情有点不知所措。
编辑:如果 "time" 的数值超过某个阈值,xarray 似乎做错了什么。请注意,这只会发生在 "days since" 上,而不是 "seconds since" 上。
我正在使用 Python 3 和 xarray 版本 0.10.7。
import numpy as np
import xarray as xr
# print('xarray version: {}'.format(xr.__version__))
ds = xr.Dataset(coords={'time': (
'time',
np.arange(106300.5, 106665.5+5*365, 365),
{'units': 'days since 1800-01-01 00:00:00'})})
# print(ds.time)
ds = xr.decode_cf(ds)
# print(ds.time)
ds.to_netcdf('./test.nc')
ds = xr.open_dataset('./test.nc', decode_cf=False)
print(ds.time)
输出:
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5 , 106665.5 , -106473.482335, -106108.482335,
-105743.482335, -105378.482335])
Coordinates:
* time (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
_FillValue: nan
units: days since 1800-01-01
calendar: proleptic_gregorian
编辑:这是带有 ncdump 的文件内容:
netcdf test {
dimensions:
time = 6 ;
variables:
double time(time) ;
time:_FillValue = NaN ;
time:units = "days since 1800-01-01" ;
time:calendar = "proleptic_gregorian" ;
// global attributes:
:_NCProperties = "version=1|netcdflibversion=4.4.1.1|hdf5libversion=1.10.1" ;
data:
time = 106300.5, 106665.5, -106473.482334601, -106108.482334601,
-105743.482334601, -105378.482334601 ;
}
是的,我可以重现这个。这可以被认为是 xarray 中的一个错误;您可能会考虑在 GitHub.
上提出问题
保存文件时,在后台 xarray 正在获取解码日期并将它们转换为自参考日期以来的时间增量。问题是示例数据集中的日期跨越了比提供的参考日期 (1800-01-01) 晚 292 年的边界线。
In [1]: import numpy as np
In [2]: import xarray as xr
In [3]: ds = xr.Dataset(coords={'time': (
...: 'time',
...: np.arange(106300.5, 106665.5+5*365, 365),
...: {'units': 'days since 1800-01-01 00:00:00'})})
...:
In [4]: ds = xr.decode_cf(ds)
In [5]: ds.time
Out[5]:
<xarray.DataArray 'time' (time: 6)>
array(['2091-01-15T12:00:00.000000000', '2092-01-15T12:00:00.000000000',
'2093-01-14T12:00:00.000000000', '2094-01-14T12:00:00.000000000',
'2095-01-14T12:00:00.000000000', '2096-01-14T12:00:00.000000000'],
dtype='datetime64[ns]')
Coordinates:
* time (time) datetime64[ns] 2091-01-15T12:00:00 2092-01-15T12:00:00 ...
In [6]: ds.to_netcdf('so.nc')
In [7]: xr.open_dataset('so.nc', decode_times=False).time
so.nc
Out[7]:
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5 , 106665.5 , -106473.482335, -106108.482335,
-105743.482335, -105378.482335])
Coordinates:
* time (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
units: days since 1800-01-01
calendar: proleptic_gregorian
292 年是具有纳秒精度的 np.timedelta64
对象可以表示的最大时间长度(请参阅文档中的 here);再多的话,你就会 运行 溢出(这是负值的原因)。
您可以使用的解决方法是用新值覆盖数据集中与时间关联的单位编码:
In [8]: ds.time.encoding['units'] = 'days since 1970-01-01'
In [9]: ds.to_netcdf('so-workaround.nc')
In [10]: xr.open_dataset('so-workaround.nc', decode_times=False).time
Out[10]:
<xarray.DataArray 'time' (time: 6)>
array([44209.5, 44574.5, 44939.5, 45304.5, 45669.5, 46034.5])
Coordinates:
* time (time) float64 4.421e+04 4.457e+04 4.494e+04 4.53e+04 4.567e+04 ...
Attributes:
units: days since 1970-01-01
calendar: proleptic_gregorian
这里我特意选择了 'days since 1970-01-01'
,因为这是 np.datetime64
对象在 NumPy 中的中心。
任何人都可以重现 xarray 在使用大值节省时间时的这种行为吗?我对这里发生的事情有点不知所措。
编辑:如果 "time" 的数值超过某个阈值,xarray 似乎做错了什么。请注意,这只会发生在 "days since" 上,而不是 "seconds since" 上。
我正在使用 Python 3 和 xarray 版本 0.10.7。
import numpy as np
import xarray as xr
# print('xarray version: {}'.format(xr.__version__))
ds = xr.Dataset(coords={'time': (
'time',
np.arange(106300.5, 106665.5+5*365, 365),
{'units': 'days since 1800-01-01 00:00:00'})})
# print(ds.time)
ds = xr.decode_cf(ds)
# print(ds.time)
ds.to_netcdf('./test.nc')
ds = xr.open_dataset('./test.nc', decode_cf=False)
print(ds.time)
输出:
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5 , 106665.5 , -106473.482335, -106108.482335,
-105743.482335, -105378.482335])
Coordinates:
* time (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
_FillValue: nan
units: days since 1800-01-01
calendar: proleptic_gregorian
编辑:这是带有 ncdump 的文件内容:
netcdf test {
dimensions:
time = 6 ;
variables:
double time(time) ;
time:_FillValue = NaN ;
time:units = "days since 1800-01-01" ;
time:calendar = "proleptic_gregorian" ;
// global attributes:
:_NCProperties = "version=1|netcdflibversion=4.4.1.1|hdf5libversion=1.10.1" ;
data:
time = 106300.5, 106665.5, -106473.482334601, -106108.482334601,
-105743.482334601, -105378.482334601 ;
}
是的,我可以重现这个。这可以被认为是 xarray 中的一个错误;您可能会考虑在 GitHub.
上提出问题保存文件时,在后台 xarray 正在获取解码日期并将它们转换为自参考日期以来的时间增量。问题是示例数据集中的日期跨越了比提供的参考日期 (1800-01-01) 晚 292 年的边界线。
In [1]: import numpy as np
In [2]: import xarray as xr
In [3]: ds = xr.Dataset(coords={'time': (
...: 'time',
...: np.arange(106300.5, 106665.5+5*365, 365),
...: {'units': 'days since 1800-01-01 00:00:00'})})
...:
In [4]: ds = xr.decode_cf(ds)
In [5]: ds.time
Out[5]:
<xarray.DataArray 'time' (time: 6)>
array(['2091-01-15T12:00:00.000000000', '2092-01-15T12:00:00.000000000',
'2093-01-14T12:00:00.000000000', '2094-01-14T12:00:00.000000000',
'2095-01-14T12:00:00.000000000', '2096-01-14T12:00:00.000000000'],
dtype='datetime64[ns]')
Coordinates:
* time (time) datetime64[ns] 2091-01-15T12:00:00 2092-01-15T12:00:00 ...
In [6]: ds.to_netcdf('so.nc')
In [7]: xr.open_dataset('so.nc', decode_times=False).time
so.nc
Out[7]:
<xarray.DataArray 'time' (time: 6)>
array([ 106300.5 , 106665.5 , -106473.482335, -106108.482335,
-105743.482335, -105378.482335])
Coordinates:
* time (time) float64 1.063e+05 1.067e+05 -1.065e+05 -1.061e+05 ...
Attributes:
units: days since 1800-01-01
calendar: proleptic_gregorian
292 年是具有纳秒精度的 np.timedelta64
对象可以表示的最大时间长度(请参阅文档中的 here);再多的话,你就会 运行 溢出(这是负值的原因)。
您可以使用的解决方法是用新值覆盖数据集中与时间关联的单位编码:
In [8]: ds.time.encoding['units'] = 'days since 1970-01-01'
In [9]: ds.to_netcdf('so-workaround.nc')
In [10]: xr.open_dataset('so-workaround.nc', decode_times=False).time
Out[10]:
<xarray.DataArray 'time' (time: 6)>
array([44209.5, 44574.5, 44939.5, 45304.5, 45669.5, 46034.5])
Coordinates:
* time (time) float64 4.421e+04 4.457e+04 4.494e+04 4.53e+04 4.567e+04 ...
Attributes:
units: days since 1970-01-01
calendar: proleptic_gregorian
这里我特意选择了 'days since 1970-01-01'
,因为这是 np.datetime64
对象在 NumPy 中的中心。