Time conversion in Iris

188 views
Skip to first unread message

Simon Tett

unread,
Nov 22, 2014, 1:16:40 PM11/22/14
to scitoo...@googlegroups.com
I've got a bunch of data with various different time coordinates which are not of the form days since YYYY-MM-DD HH:MM:SS and are of the form years since XX (or similar).

When I try to plot or convert I get errors of the form:
 ValueError: units must be one of 'seconds', 'minutes', 'hours' or 'days' (or singular version of these), got 'years'

How do I convert the time co-ordinate? I assume I'd explicitly change the points, bounds and units but wonder if there is a better way?

thanks

Simon

Andrew Dawson

unread,
Nov 24, 2014, 6:09:43 AM11/24/14
to scitoo...@googlegroups.com
What conversion did you try? I made up some dummy data and attempted to convert from "years since 1982" to "days since 1982-01-01 00:00:0.0":

import iris

unit0
= iris.unit.Unit('years since 1982', calendar='gregorian')
unit1
= iris.unit.Unit('days since 1982-01-01 00:00:0.0', calendar='gregorian')

time
= iris.coords.DimCoord([1.], units=unit0, standard_name='time')
time
.convert_units(unit1)
print time

Which prints: DimCoord([1983-01-01 05:48:46], standard_name='time', calendar='gregorian')
(1 year since 1982 == 365.24219878 days since 1982-01-01 00:00:0.0 in this case)

However, if you attempt to print the coordinate 'time' before converting the units you may hit an error:

ValueError: units must be one of 'seconds', 'minutes', 'hours' or 'days' (or singular version of these), got 'years'

It looks like this is in the netcdftime library, which I guess is not willing to support the unit 'years since', but by converting first you should be able to avoid this.

Andrew Dawson

unread,
Nov 24, 2014, 9:58:07 AM11/24/14
to scitoo...@googlegroups.com
I just realised I assumed a Gregorian calendar. If you have a non-Gregorian calendar even the conversion will fail, I guess this might be what is happening in your case. The error is caused because the netcdftime module won't parse this type of unit. The netcdftime module is required to process any dates/times with non-standard calendars.

Unfortunately this may indeed leave you needing to manually replace the times and the unit in order to work with the data set. As you said, you would need to change the time points and bounds as well as the units.
Reply all
Reply to author
Forward
0 new messages