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

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

removing duplicate file opening

  • 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        DI=csmllibs.csmldataiface.DataInterface()
52        #grab any (first) file                 
53        DI=DI.getUnknownInterfaceType(filelist[0])
54        for i in range(0,len(filelist)):
55                DI.openFile(filelist[i])
56                try:
57                        DI.setAxis(timedim)
58                except:
59                        print "Could not set time axis"
60                        print "check you have the correct time axis (-t) set"
61                        sys.exit()
62                data =DI.getDataForAxis()
63                times=[]
64                for j in range(0,len(data)):
65                        times.append(data[j])
66                ftTuple = (times, filelist[i])
67                FileTimeList.append(ftTuple)
68                DI.closeFile()
69                if i % 20 == 0:
70                        print 'scanning file %s of %s'%(i,len(filelist))
71        #get calendar type:
72        DI.openFile(filelist[0])
73        try:
74            DI.setAxis(timedim)
75            cal = DI.getAxisAttribute('calendar')
76            setcdtimeCalendar(cal)
77            units=DI.getAxisAttribute('units')
78        except:
79            print "Could not get Calendar info"
80           
81        FileTimeList.sort()
82        #get the reference date for the time reference system.
83        refTime=DI.getTimeUnits()
84        DI.closeFile()
85        FormattedFileTimeList = []
86        for j in range (0,len(FileTimeList)):
87                timeList = FileTimeList[j][0]
88                #print 'timelist_____________________' +str(timeList)
89                for k in range(0, len(timeList)):
90                        relativetime= cdtime.reltime(timeList[k], refTime)
91                        uduTime=UDtimeToCSMLtime(relativetime.tocomp())
92                        ftTuple = (uduTime, filelist[j])
93                        FormattedFileTimeList.append(ftTuple)                   
94        return FormattedFileTimeList, cal,units
Note: See TracBrowser for help on using the repository browser.