简体   繁体   English

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

[英]Error converting datetime using date2num in netCDF4 Python

I am trying to write a netcdf file using netCDF4 in Python.我正在尝试在 Python 中使用 netCDF4 编写 netcdf 文件。 For that, I am creating a time variable.为此,我正在创建一个时间变量。 I created the time data using datetime .我使用datetime创建了时间数据。 I am getting an error when I try to convert the datetime data to netcdf formats using date2num .当我尝试使用date2num将日期时间数据转换为 netcdf 格式时出现错误。

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

The above command gives me list of dates that look like-上面的命令给了我看起来像的日期列表 -

[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)]

I am using the following command to convert the date to netCDF4 dates:我正在使用以下命令将日期转换为 netCDF4 日期:

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

Here time.units is since 1991-01-01 (I am not sure how time.units works)这里time.unitssince 1991-01-01 1991-01-01 开始的(我不确定 time.units 是如何工作的)

The nc.date2num command gives the following error: 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

What could be the reason for the error?错误的原因可能是什么?

You need add the time units example: days since 1991-01-01您需要添加时间单位示例: days since 1991-01-01

Can be days, hours, minutes, seconds, milliseconds or microseconds.可以是天、小时、分钟、秒、毫秒或微秒。 https://unidata.github.io/cftime/api.html#cftime.date2num https://unidata.github.io/cftime/api.html#cftime.date2num

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM