Option to display time coordinates in human-readable format

614 views
Skip to first unread message

pbentley

unread,
Nov 6, 2012, 4:59:51 AM11/6/12
to scitoo...@googlegroups.com
Hi,

When I display/print a DimCoord object representing time, the time coordinates and bounds are displayed as 'opaque' raw values, as the example below illustrates:

>>> t = sst.coord('time')
>>> type(t)
<class 'iris.coords.DimCoord'>
>>> print t
DimCoord(array([  7.47792000e+08]), bounds=array([[  7.46496000e+08,   7.49088000e+08]]), standard_name=u'time', units=Unit('seconds since 1978-09-01 00:00:00', calendar='360_day'), long_name=u'Time axis', attributes={'title': 'Time', 'time_origin': ' 1978-SEP-01 00:00:00'})

This is to be expected - it's just printing the values stored in the underlying numpy array.

However, it would be really neat if there was an equivalent to ncdump's -t option to display time coordinates in human-readable format. I presume this would need to be coded up as an extra method on the DimCoord class (or a subclass of it).

Phil

RHattersley

unread,
Nov 6, 2012, 6:16:28 AM11/6/12
to scitoo...@googlegroups.com
Hi,

Agreed. Prior to making Iris open source we were busy working on storing times as "time-values", not just numbers. That work is something we're planning on dusting off. But perhaps there's benefit in an interim solution which just provides human-reabable display? In part it depends on how many API changes it would make which would end up eventually being deprecated/removed.

Regards,
Richard

pbentley

unread,
Nov 6, 2012, 6:50:12 AM11/6/12
to scitoo...@googlegroups.com
Hi Richard,

That's great. My mental faculty for converting seemingly random large numbers to dates is pathetic!

Phil

Bernd Becker

unread,
Mar 4, 2013, 7:09:22 AM3/4/13
to scitoo...@googlegroups.com
Any news on  printing time in human readable format?
I get :
print z500.coords('time')
[DimCoord(array([ 344868.,  344892.,  344916.,  344940.,  344964.,  344988.,
        345012.,  345036.,  345060.,  345084.,  345108.,  345132.,
        345156.,  345180.,  345204.,  345228.,  345252.,  345276.,
        345300.,  345324.,  345348.,  345372.,  345396.,  345420.,
        345444.,  345468.,  345492.,  345516.,  345540.,  345564.,
        345588.,  345612.]), bounds=array([[ 344856.,  344880.],

What is the magic word to convert time units into  a comon date format or yyyy mm dd?

Rutger Dankers

unread,
Mar 4, 2013, 11:13:33 AM3/4/13
to scitoo...@googlegroups.com
Hi Bernd

Try something like this:

tcoord = mycube.coord('time')
firstdate
= tcoord.units.num2date(tcoord.points[0]) # e.g., datetime.datetime(2004, 1, 1, 0, 0)
print firstdate.year # e.g., 2004


HTH
Rutger

RHattersley

unread,
Mar 5, 2013, 5:28:59 AM3/5/13
to scitoo...@googlegroups.com
Hi Bernd,

Human-readable dates were included in the v1.2 release which happened a few days ago. As of this morning, this release is now the default within the Met Office.

So now you''ll see something like:

>>> print c.coord('time')
DimCoord([2009-05-05 12:00:00, 2009-05-06 12:00:00], standard_name='time', calendar='gregorian')

Richard

LeonH

unread,
Mar 19, 2013, 6:55:59 AM3/19/13
to scitoo...@googlegroups.com
The new human readable format  looks great. It is a little confusing though, as it implies that I can access calendar, as I can all the other variables. As an example:
>>> print cube.coord('Time axis')
DimCoord([2005-03-16 00:00:00], standard_name=None, calendar=u'360_day', long_name=u'Time axis', var_name='time_counter', attributes={'time_origin': '2005-MAR-01 00:00:00', 'title': 'Time'})
>>> print cube.coord('Time axis').standard_name
None
>>> print cube.coord('Time axis').long_name   
Time axis
>>> print cube.coord('Time axis').var_name
time_counter
>>> print cube.coord('Time axis').attributes
{'time_origin': '2005-MAR-01 00:00:00', 'title': 'Time'}
>>> print cube.coord('Time axis').calendar 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'DimCoord' object has no attribute 'calendar'

I haven't actually worked out how to get the calendar string yet, but it appears to be a different beast than the other variables. Shouldn't that be reflected in the output of print somehow?

pp-mo

unread,
Mar 19, 2013, 7:31:39 AM3/19/13
to scitoo...@googlegroups.com
>> I haven't actually worked out how to get the calendar string yet, but it appears to be a different beast than the other variables.

The calendar is actually a property of the coord 'units' attribute, not of the coord itself. Like this ...
    >>> coord = iris.coords.DimCoord([1,2],long_name='t', units=iris.unit.Unit('hours since 1970-01-01'))
    >>> coord.units.calendar
    'gregorian'



>> Shouldn't that be reflected in the output of print somehow?
Print formatting is a matter of programmer choice and convenience (think about how a cube prints out).
You can see what the construction 'really' is by using 'repr()' instead of 'str()'
 - see http://www-avd/sci/python/library/functions.html?highlight=repr#repr
 - and http://www-avd/sci/python/library/functions.html?highlight=repr#str

    >>> print repr(coord)
    DimCoord(array([1, 2]), standard_name=None, units=Unit('hours since 1970-01-01', calendar='gregorian'), long_name='t')

Reply all
Reply to author
Forward
0 new messages