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

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

default calendar attribute added, repitition in fileList removed, gml:description fixed

  • 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
16
17def CSMLtimeToUDtime(time):
18        #time string manipulation  - convert  2783-5-22T0:0:0.0 to 2783-5-22 0:0:0.0
19    return str(time).replace ('T', ' ')
20
21def UDtimeToCSMLtime(time):
22    #time string manipulation  - convert 2783-5-22 0:0:0.0 to 2783-5-22T0:0:0.0
23    return str(time).replace (' ', 'T')
24               
25def setcdtimeCalendar(cal):
26    if cal == 'standard':
27        cdtime.DefaultCalendar= cdtime.MixedCalendar
28    elif cal == 'gregorian':
29        cdtime.DefaultCalendar= cdtime.MixedCalendar
30    elif cal == 'proleptic_gregorian':
31        cdtime.DefaultCalendar= cdtime.GregorianCalendar
32    elif cal=='360_day':
33        cdtime.DefaultCalendar=cdtime.Calendar360
34    elif cal=='noleap':
35        cdtime.DefaultCalendar=  cdtime.NoLeapCalendar
36    elif cal=='365_day':
37        cdtime.DefaultCalendar=  cdtime.NoLeapCalendar
38    elif cal =='julian':
39        cdtime.DefaultCalendar=  cdtime.JulianCalendar
40
41def getFileTimeList(filelist, timedim):
42        #****************************************************************************************
43        #Given a list of files and a name for the time dimension this function will return a list containing
44        # filename/time pairs eg tuple [(ta,fa),(tb,fb),(tc,fc)] in the correct temporal order.
45        #if files contain more than one time, then the tuple will be of the form:
46        #[(ta1,ta2,ta3,fa),(tb1,tb2,tb3,fb),(tc1,tc2,tc3,fc)]
47        #*****************************************************************************************
48        FileTimeList = []  #list to hold the filesnames and corrsponding times
49        DI=csml.csmllibs.csmldataiface.DataInterface()
50        #grab any (first) file                 
51        DI=DI.getUnknownInterfaceType(filelist[0])
52        count=0
53        for file in filelist:
54                count=count +1
55                DI.openFile(file)
56                try:
57                        DI.setAxis(timedim)
58                        data =DI.getDataForAxis()
59                except:
60                        try:
61                            data=DI.getTimes()
62                        except:
63                            print 'tried alternative method to get time axis and failed'
64                            print "check you have the correct time axis (-t) set"
65                            sys.exit()
66                times=[]
67                for j in range(0,len(data)):
68                        times.append(data[j])
69                refTime=DI.getTimeUnits()   #maybe this can be optional?
70                ftTuple = (times, file,refTime)
71                FileTimeList.append(ftTuple)
72                DI.closeFile()
73                if count % 20 == 0:
74                        print 'scanning file %s of %s'%(count,len(filelist))
75        #get calendar type:
76        DI.openFile(filelist[0])
77        try:
78            DI.setAxis(timedim)
79            cal = DI.getAxisAttribute('calendar')
80            setcdtimeCalendar(cal)
81        except:
82            print 'No calendar attribute, defaulting to CF standard mixed calendar'
83            cal='standard'
84            setcdtimeCalendar(cal)
85        try:
86            units=DI.getAxisAttribute('units')
87        except:
88            print "Could not get time unit info"
89            units =''
90        FileTimeList.sort()
91        #get the reference date for the time reference system.
92        #refTime=DI.getTimeUnits()
93        DI.closeFile()
94        FormattedFileTimeList = []
95        for j in range (0,len(FileTimeList)):
96                timeList = FileTimeList[j][0]
97                refTime=FileTimeList[j][2]
98                #print 'timelist_____________________' +str(timeList)
99                for k in range(0, len(timeList)):
100                        relativetime= cdtime.reltime(timeList[k], refTime)
101                        uduTime=UDtimeToCSMLtime(relativetime.tocomp())
102                        ftTuple = (uduTime, filelist[j])
103                        FormattedFileTimeList.append(ftTuple)                   
104        print  'finished time list'
105        return FormattedFileTimeList, cal,units
Note: See TracBrowser for help on using the repository browser.