Re: time

From: Jeff Whitaker <jswhit_at_nyahnyahspammersnyahnyah>
Date: Fri, 29 Sep 2006 10:46:42 -0600

Wendi Madsen wrote:
> Good Morning
> NCEP recently changed the format of their ensemble data, and this change
> required changes in the way we get that data from them. I started with
> a Perl script to get the data and then let the old processes take care
> of it from there, however there were missing ensembles in the netcdf
> files. In an effort to fix this problem I wrote a python script using
> Nio to read the NCEP ensemble grib files and write the data to netcdf
> files directly. This seems to work except for the times that I put into
> the file. I am using the python datetime module to convert a date,
> year, month and day, to hours since 1/1/1 at 00:00:00. When I compare
> the python generated files to the files generated using a Fortran
> program, with udunits calls for time conversion, the python times are 24
> hours less than the Fortran times. Are there any times functions,
> possibly using udunits functionality, that are part of Pyngl/nio?? Or
> is there some other python module that will correct this problem. I
> have kludged it for the time being by adding 1 to the number of days
> since 1/1/1 before the multiply by 24, and then decode it in the reverse.
> Thanks for your assistance.
> Wendi Madsen

Wendi: This is happening because the python datetime module uses a
different calendar than udunits. It's a 'proleptic gregorian' calendar
instead of a 'mixed Julian/Gregorian' calendar. I've written a python
module that deals with this problem - it's called netcdftime and it's
part of my netcdf4 package (,
but I can show you how to install it separately offline. Here's an
example of how to use it:

>>> from netcdftime import utime
>>> from datetime import datetime
>>> cdftime = utime('hours since 0001-01-01 00:00:00')
>>> print cdftime.units,'since',cdftime.origin
>>> print cdftime.calendar,'calendar'
>>> d = datetime(2006,9,29,12)
>>> t1 = cdftime.date2num(d)
>>> print t1
>>> d2 = cdftime.num2date(t1)
>>> print d2
hours since 1-01-01 00:00:00
standard calendar
2006-09-29 12:00:00

Docs are at



Jeffrey S. Whitaker         Phone  : (303)497-6313
Meteorologist               FAX    : (303)497-6449
NOAA/OAR/PSD  R/PSD1        Email  :
325 Broadway                Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web    :
pyngl-talk mailing list
Received on Fri Sep 29 2006 - 10:46:42 MDT

This archive was generated by hypermail 2.2.0 : Mon Oct 23 2006 - 15:54:12 MDT