简体   繁体   中英

Error converting datetime using date2num in netCDF4 Python

I am trying to write a netcdf file using netCDF4 in Python. For that, I am creating a time variable. I created the time data using datetime . I am getting an error when I try to convert the datetime data to netcdf formats using date2num .

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:

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

Here time.units is since 1991-01-01 (I am not sure how time.units works)

The nc.date2num command gives the following error:

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

Can be days, hours, minutes, seconds, milliseconds or microseconds. https://unidata.github.io/cftime/api.html#cftime.date2num

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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