0

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?

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I have monthly data, how can I add months?
github.com/Unidata/cftime/blob/master/cftime/_cftime.pyx#L172 units: a string of the form <time units> since <reference time> describing the time units. <time units> can be days, hours, minutes, seconds, milliseconds or microseconds. <reference time> is the time origin. months_since is allowed only for the 360_day calendar.
Example: nc.date2num(dates, 'months since 2020-08-01', '360_day')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.