From: <js...@us...> - 2007-12-04 17:36:58
|
Revision: 4579 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4579&view=rev Author: jswhit Date: 2007-12-04 09:36:55 -0800 (Tue, 04 Dec 2007) Log Message: ----------- add date2num and num2date functions to basemap namespace. Modified Paths: -------------- trunk/toolkits/basemap/Changelog trunk/toolkits/basemap/MANIFEST.in trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py trunk/toolkits/basemap/setup.py Modified: trunk/toolkits/basemap/Changelog =================================================================== --- trunk/toolkits/basemap/Changelog 2007-12-04 16:06:20 UTC (rev 4578) +++ trunk/toolkits/basemap/Changelog 2007-12-04 17:36:55 UTC (rev 4579) @@ -1,3 +1,5 @@ + * added num2date and date2num functions, which use + included netcdftime module. version 0.9.8 (svn revision 4526) * fixes for filling continents in orthographic projection. * added 'maskandscale' kwarg to NetCDFFile to Modified: trunk/toolkits/basemap/MANIFEST.in =================================================================== --- trunk/toolkits/basemap/MANIFEST.in 2007-12-04 16:06:20 UTC (rev 4578) +++ trunk/toolkits/basemap/MANIFEST.in 2007-12-04 17:36:55 UTC (rev 4579) @@ -78,6 +78,7 @@ recursive-include lib/httplib2 * recursive-include lib/dbflib * recursive-include lib/shapelib * +recursive-include lib/netcdftime * include lib/matplotlib/toolkits/basemap/data/5minmask.bin include lib/matplotlib/toolkits/basemap/data/GL27 include lib/matplotlib/toolkits/basemap/data/countries_c.dat Modified: trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py =================================================================== --- trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py 2007-12-04 16:06:20 UTC (rev 4578) +++ trunk/toolkits/basemap/lib/matplotlib/toolkits/basemap/basemap.py 2007-12-04 17:36:55 UTC (rev 4579) @@ -28,7 +28,7 @@ from numpy import linspace, squeeze, ma from matplotlib.cbook import is_scalar, dedent from shapelib import ShapeFile -import _geos, pupynere +import _geos, pupynere, netcdftime # basemap data files now installed in lib/matplotlib/toolkits/basemap/data basemap_datadir = os.sep.join([os.path.dirname(__file__), 'data']) @@ -2857,3 +2857,80 @@ else: f = pupynere._LocalFile(file,maskandscale) return f + +def num2date(times,units,unit_format='%Y-%m-%d %H:%M:%S',calendar='standard'): + """ + Return datetime objects given numeric time values. The units + of the numeric time values are described by the units argument + and the unit_format and calendar keywords. + + Arguments: + + times - numeric time values. Maximum resolution is 1 second. + units - a string of the form '<time-units> since <reference time>' + describing the time units. <time-units> can be days, hours, minutes + or seconds. <reference-time> is the time origin, defined by the format + keyword (see below). For example, a valid choice would be + units='hours since 0001-01-01 00:00:00'. + + Keyword Arguments: + + format - a string describing a reference time. This string is converted + to a year,month,day,hour,minute,second tuple by strptime. The default + format is '%Y-%m-%d %H:%M:%S'. See the time.strptime docstring for other + valid formats. + + calendar - describes the calendar used in the time calculations. + All the values currently defined in the CF metadata convention + (http://cf-pcmdi.llnl.gov/documents/cf-conventions/) are supported. + Valid calendars 'standard', 'gregorian', 'proleptic_gregorian' + 'noleap', '365_day', '360_day', 'julian'. Default is 'standard'. + + Returns a datetime instance, or an array of datetime instances. + + The datetime instances returned are 'real' python datetime + objects if the date falls in the Gregorian calendar (i.e. + calendar='proleptic_gregorian', or calendar = 'standard' or 'gregorian' + and the date is after 1582-10-15). Otherwise, they are 'phony' datetime + objects which support some but not all the methods of 'real' python + datetime objects. This is because the python datetime module cannot + the weird dates in some calendars (such as '360_day' and 'all_leap' + which don't exist in any real world calendar. + """ + cdftime = netcdftime.utime(units,calendar=calendar,format=unit_format) + return cdftime.num2date(times) + +def date2num(dates,units,unit_format='%Y-%m-%d %H:%M:%S',calendar='standard'): + """ + Return numeric time values given datetime objects. The units + of the numeric time values are described by the units argument + and the unit_format and calendar keywords. + + Arguments: + + dates - A datetime object or a sequence of datetime objects. + units - a string of the form '<time-units> since <reference time>' + describing the time units. <time-units> can be days, hours, minutes + or seconds. <reference-time> is the time origin, defined by the format + keyword (see below). For example, a valid choice would be + units='hours since 0001-01-01 00:00:00'. + + Keyword Arguments: + + format - a string describing a reference time. This string is converted + to a year,month,day,hour,minute,second tuple by strptime. The default + format is '%Y-%m-%d %H:%M:%S'. See the time.strptime docstring for other + valid formats. + + calendar - describes the calendar used in the time calculations. + All the values currently defined in the CF metadata convention + (http://cf-pcmdi.llnl.gov/documents/cf-conventions/) are supported. + Valid calendars 'standard', 'gregorian', 'proleptic_gregorian' + 'noleap', '365_day', '360_day', 'julian'. Default is 'standard'. + + Returns a numeric time value, or an array of numeric time values. + + The maximum resolution of the numeric time values is 1 second. + """ + cdftime = netcdftime.utime(units,calendar=calendar,format=unit_format) + return cdftime.date2num(dates) Modified: trunk/toolkits/basemap/setup.py =================================================================== --- trunk/toolkits/basemap/setup.py 2007-12-04 16:06:20 UTC (rev 4578) +++ trunk/toolkits/basemap/setup.py 2007-12-04 17:36:55 UTC (rev 4579) @@ -114,6 +114,10 @@ packages = packages + ['httplib2'] package_dirs['httlib2'] = os.path.join('lib','httplib2') +# install netcdftime +packages = packages + ['netcdftime'] +package_dirs['httlib2'] = os.path.join('lib','netcdftime') + if 'setuptools' in sys.modules: # Are we running with setuptools? # if so, need to specify all the packages in heirarchy This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |