source: TI02-CSML/trunk/csml/csmllibs/csmltime.py @ 2176

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

Numeric.array is now the standard data container

  • 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
12import csml.csmllibs
13import datetime
14import sys
15import cdtime
16import pdb
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       
42       
43def identifyTimeAxis(self,listToCheck): 
44    #given a list of possible axes, find the one which is most probably the time axis.
45    for name in listToCheck:
46        if string.upper(name) in ['T', 'TIME']:
47            tname =name
48    return tname
49
50def compareTimes(timemin, timecheck, timemax):
51    timeok=0
52    try:
53        year=int(timecheck.split('-')[0])
54        month=int(timecheck.split('-')[1])
55        day=int(timecheck.split('-')[2].split('T')[0])
56        tcheck=cdtime.comptime(year,month,day)
57        timeok=0
58        if tcheck.cmp(timemin) >= 0:
59            if tcheck.cmp(timemax) < 1:
60                timeok = 1
61        print 'TMIN time  %s'%timemin
62        print 'CHECK time  %s'%tcheck
63        print 'TMAX time  %s'%timemax
64    except:
65        #if it's not a proper internet time e.g. time = '6' then just compare numerically
66        timeok=0
67        if timecheck >=timemin:
68            if timecheck <=timemax:
69                timeok = 1
70    return timeok
71
72def getCDtime(time): 
73    year=int(time.split('-')[0])
74    month=int(time.split('-')[1])
75    day=int(time.split('-')[2].split('T')[0])
76    hour=int(time.split('-')[2].split('T')[1].split(':')[0])
77    minute=int(time.split('-')[2].split('T')[1].split(':')[1])
78    second=float(time.split('-')[2].split('T')[1].split(':')[2])
79    cdTimeVal=cdtime.comptime(year,month,day,hour,minute,second)
80    return cdTimeVal
81
82def getFileTimeList(filelist, timedim):
83        #****************************************************************************************
84        #Given a list of files and a name for the time dimension this function will return a list containing
85        # filename/time pairs eg tuple [(ta,fa),(tb,fb),(tc,fc)] in the correct temporal order.
86        #if files contain more than one time, then the tuple will be of the form:
87        #[(ta1,ta2,ta3,fa),(tb1,tb2,tb3,fb),(tc1,tc2,tc3,fc)]
88        #*****************************************************************************************
89        FileTimeList = []  #list to hold the filesnames and corrsponding times
90        DI=csml.csmllibs.csmldataiface.DataInterface()
91        #grab any (first) file                 
92        DI=DI.getUnknownInterfaceType(filelist[0])
93        count=0
94        for file in filelist:
95                count=count +1
96                DI.openFile(file)
97                try:
98                        DI.setAxis(timedim)
99                        data =DI.getDataForAxis()
100                        print '?'
101                except:
102                        try:
103                            data=DI.getTimes()
104                        except:
105                            print 'tried alternative method to get time axis and failed'
106                            print "check you have the correct time axis (-t) set"
107                            sys.exit()
108                times=[]
109                for j in range(0,len(data)):
110                        times.append(data[j])
111                refTime=DI.getTimeUnits()   #maybe this can be optional?
112                ftTuple = (times, file,refTime)
113                FileTimeList.append(ftTuple)
114                DI.closeFile()
115                if count % 20 == 0:
116                        print 'scanning file %s of %s'%(count,len(filelist))
117        #get calendar type:
118        DI.openFile(filelist[0])
119        try:
120            DI.setAxis(timedim)
121            cal = DI.getAxisAttribute('calendar')
122            setcdtimeCalendar(cal)
123        except:
124            print 'No calendar attribute, defaulting to CF standard mixed calendar'
125            cal='standard'
126            setcdtimeCalendar(cal)
127        try:
128            units=DI.getAxisAttribute('units')
129        except:
130            print "Could not get time unit info"
131            units =''
132        FileTimeList.sort()
133        #get the reference date for the time reference system.
134        #refTime=DI.getTimeUnits()
135        DI.closeFile()
136        FormattedFileTimeList = []
137        for j in range (0,len(FileTimeList)):
138                timeList = FileTimeList[j][0]
139                refTime=FileTimeList[j][2]
140                #print 'timelist_____________________' +str(timeList)
141                for k in range(0, len(timeList)):
142                        relativetime= cdtime.reltime(timeList[k], refTime)
143                        uduTime=UDtimeToCSMLtime(relativetime.tocomp())
144                        ftTuple = (uduTime, filelist[j])
145                        FormattedFileTimeList.append(ftTuple)                   
146        print  'finished time list'
147        return FormattedFileTimeList, cal,units
Note: See TracBrowser for help on using the repository browser.