在netCDF4 Python中使用date2num转换日期时间时出错

2024-06-11 20:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python中的netCDF4编写netcdf文件。为此,我创建了一个时间变量。我使用datetime创建了时间数据。当我尝试使用date2num将日期时间数据转换为netcdf格式时,我遇到了一个错误

dates=[datetime.datetime(1996,1,1) + relativedelta(months=mon) for mon in range(120)]

上面的命令为我提供了如下日期列表:

[datetime.datetime(1996, 1, 1, 0, 0),
 datetime.datetime(1996, 2, 1, 0, 0),
 datetime.datetime(1996, 3, 1, 0, 0),
 datetime.datetime(1996, 4, 1, 0, 0),
 datetime.datetime(1996, 5, 1, 0, 0),
 ...
 datetime.datetime(2005, 10, 1, 0, 0),
 datetime.datetime(2005, 11, 1, 0, 0),
 datetime.datetime(2005, 12, 1, 0, 0)]

我正在使用以下命令将日期转换为netCDF4日期:

dates_nc=nc.date2num(dates,time.units)

这里time.unitssince 1991-01-01(我不知道time.units是如何工作的)

nc.date2num命令给出以下错误:

ValueError                                Traceback (most recent call last)
cftime\_cftime.pyx in cftime._cftime._datesplit()

ValueError: need more than 2 values to unpack

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-154-97a05959ce3c> in <module>
----> 1 dates_nc=nc.date2num(dates,time.units)

cftime\_cftime.pyx in cftime._cftime.date2num()

cftime\_cftime.pyx in cftime._cftime._dateparse()

cftime\_cftime.pyx in cftime._cftime._datesplit()

ValueError: Incorrectly formatted CF date-time unit_string

错误的原因可能是什么


Tags: in命令datetimetime错误时间netcdfnetcdf4