Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Xarray将时间更新为NetCDF中的前置公历_Python 3.x_Netcdf_Python Xarray - Fatal编程技术网

Python 3.x 使用Xarray将时间更新为NetCDF中的前置公历

Python 3.x 使用Xarray将时间更新为NetCDF中的前置公历,python-3.x,netcdf,python-xarray,Python 3.x,Netcdf,Python Xarray,我有每年6小时的NetCDF文件,但时间值已损坏,即:;每个文件的timedelta64[ns]00:00:00。我的目标是纠正这一点,并使用xarrays将其合并到一个组合文件中。该文件有一个CF约定,时间以公历开头 我试图将时间轴值存储为时间戳,但在将其写回.nc格式时出错,该格式表示ValueError:无法防止在变量“time”上覆盖attrs中的现有键日历 我正在尝试从1970年1月1日起将时间值更新为秒。它可以工作,但问题是,当我读回保存的文件时,我希望实际的yyyy-mm-dd中显

我有每年6小时的NetCDF文件,但时间值已损坏,即:;每个文件的timedelta64[ns]00:00:00。我的目标是纠正这一点,并使用xarrays将其合并到一个组合文件中。该文件有一个CF约定,时间以公历开头

我试图将时间轴值存储为时间戳,但在将其写回.nc格式时出错,该格式表示ValueError:无法防止在变量“time”上覆盖attrs中的现有键日历

我正在尝试从1970年1月1日起将时间值更新为秒。它可以工作,但问题是,当我读回保存的文件时,我希望实际的yyyy-mm-dd中显示的时间,以便我可以使用.sel方法快速浏览数据

是否有更好的方法来编写输出文件,同时保持Proleptic Gregorian的标准约定,以便在我读回时,我的时间轴自动采用yyyy mm dd格式,而不是每次读取后将其从秒转换回yyyy mm dd格式

import xarray as xr
big_ds = xr.open_mfdataset('path/*',   autoclose=True, concat_dim='time')
dt = pd.date_range(start=datetime(2000,1,1), end=datetime(2000,12,31,18), freq='6H')
big_ds.time.values = (dt - std_time).total_seconds()
big_ds.to_netcdf('outfile.nc')

test_ds = xr.open_dataset('outfile.nc')
test_ds.time
>><xarray.DataArray 'time' (time: 1464)>
array([9.466848e+08, 9.467064e+08, 9.467280e+08, ..., 9.782424e+08,
   9.782640e+08, 9.782856e+08])
Coordinates:
* time     (time) float64 9.467e+08 9.467e+08 ... 9.783e+08 9.783e+08
Attributes:
calendar:  proleptic_gregorian
将xarray作为xr导入
big\u ds=xr.open\u mfdataset('path/*',autoclose=True,concat\u dim='time'))
dt=pd.date_范围(开始=datetime(2000,1,1),结束=datetime(2000,12,31,18),频率=6H')
big_ds.time.values=(dt-std_time).total_seconds()
大数据到网络CDF('outfile.nc')
测试\u ds=xr.open\u数据集('outfile.nc'))
测试时间
>>
阵列([9.466848e+08,9.467064e+08,9.467280e+08,…,9.7824E+08,
9.782640e+08,9.782856e+08])
协调:
*时间(时间)浮动64 9.467e+08 9.467e+08。。。9.783e+08 9.783e+08
属性:
日历:前公历

短修复方法是将单位添加到时间属性中

big_ds.time.attrs['units'] = 'Seconds since 01/01/1970 00:00:00 UTC'

因此,在回读数据时,xarray足够智能,可以看到这一点,并将以秒为单位存储的时间值转换为
datetime64[ns]
,这样就可以作为
yyyy-mm-dd

来访问它们。短修复方法是将单位添加到时间属性中

big_ds.time.attrs['units'] = 'Seconds since 01/01/1970 00:00:00 UTC'
因此,在回读数据时,xarray足够智能,可以看到这一点,并将以秒为单位存储的时间值转换为
datetime64[ns]
,这样就可以作为
yyyy-mm-dd