source: TI02-CSML/trunk/Scanner/csmllibs/csmltime.py @ 1445

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/Scanner/csmllibs/csmltime.py@1445
Revision 1445, 3.4 KB checked in by domlowe, 13 years ago (diff)

removing old time functions, CDtime used throughout now

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/env python
2
3#**************************************************************************************
4#csmltime.py
5#contains time related functions for creating CSML
6#v0.5 split off 11th November 2005
7#v0.51 updated to use datainterface 6th Dec 05
8#!!!!!!!!!!!!!!! NEEDS TIDYING UP!!!!!!!!!!!!!!!!!!!!!!!!!!!
9#Dominic Lowe, BADC
10#**************************************************************************************
11
12#import cdms
13import csmllibs
14import datetime
15import sys
16import cdtime
17
18def CSMLtimeToUDtime(time):
19        #time string manipulation  - convert  2783-5-22T0:0:0.0 to 2783-5-22 0:0:0.0
20    return str(time).replace ('T', ' ')
21
22def UDtimeToCSMLtime(time):
23    #time string manipulation  - convert 2783-5-22 0:0:0.0 to 2783-5-22T0:0:0.0
24    return str(time).replace (' ', 'T')
25               
26def setcdtimeCalendar(cal):
27    if cal == 'standard':
28        cdtime.DefaultCalendar= cdtime.MixedCalendar
29    elif cal == 'gregorian':
30        cdtime.DefaultCalendar= cdtime.MixedCalendar
31    elif cal == 'proleptic_gregorian':
32        cdtime.DefaultCalendar= cdtime.GregorianCalendar
33    elif cal=='360_day':
34        cdtime.DefaultCalendar=cdtime.Calendar360
35    elif cal=='noleap':
36        cdtime.DefaultCalendar=  cdtime.NoLeapCalendar
37    elif cal=='365_day':
38        cdtime.DefaultCalendar=  cdtime.NoLeapCalendar
39    elif cal =='julian':
40        cdtime.DefaultCalendar=  cdtime.JulianCalendar
41
42def getFileTimeList(filelist, timedim):
43   
44        #****************************************************************************************
45        #Given a list of files and a name for the time dimension this function will return a list containing
46        # filename/time pairs eg tuple [(ta,fa),(tb,fb),(tc,fc)] in the correct temporal order.
47        #if files contain more than one time, then the tuple will be of the form:
48        #[(ta1,ta2,ta3,fa),(tb1,tb2,tb3,fb),(tc1,tc2,tc3,fc)]
49        #*****************************************************************************************
50        FileTimeList = []
51        print "len of filelist = " + str(len(filelist))
52        DI=csmllibs.csmldataiface.DataInterface()
53        #grab any (first) file                 
54       
55        DI=DI.getUnknownInterfaceType(filelist[0])
56        for i in range(0,len(filelist)):
57                DI.openFile(filelist[i])
58                try:
59                        DI.setAxis(timedim)
60                except:
61                        print "Could not set time axis"
62                        print "check you have the correct time axis (-t) set"
63                        sys.exit()
64                data =DI.getDataForAxis()
65                times=[]
66                for j in range(0,len(data)):
67                        times.append(data[j])
68                ftTuple = (times, filelist[i])
69                FileTimeList.append(ftTuple)
70                DI.closeFile()
71                if i % 20 == 0:
72                        print i
73        #get calendar type:
74        DI.openFile(filelist[0])
75        try:
76            DI.setAxis(timedim)
77            cal = DI.getAxisAttribute('calendar')
78            setcdtimeCalendar(cal)
79            units=DI.getAxisAttribute('units')
80        except:
81            print "Could not get Calendar info"
82           
83        FileTimeList.sort()
84        DI.openFile(filelist[i])
85        #get the reference date for the time reference system.
86        DI.setAxis(timedim)
87        refTime=DI.getTimeUnits()
88        FormattedFileTimeList = []
89        for j in range (0,len(FileTimeList)):
90                timeList = FileTimeList[j][0]
91                #print 'timelist_____________________' +str(timeList)
92                for k in range(0, len(timeList)):
93                        relativetime= cdtime.reltime(timeList[k], refTime)
94                        uduTime=UDtimeToCSMLtime(relativetime.tocomp())
95                        ftTuple = (uduTime, filelist[j])
96                        FormattedFileTimeList.append(ftTuple)                   
97        return FormattedFileTimeList, cal,units
Note: See TracBrowser for help on using the repository browser.