source: TI02-CSML/trunk/csml/csmllibs/csmlextra.py @ 2215

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

adding online parser to svn

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#Stuff with nowhere better to go
2import random, datetime
3import string
4import csml.parser
5import sys
6
7def getRandomID():
8    #returns a random 8 character alphanumeric string that can be used as an internal identifier in a CSML document.
9    #the ID only needs to be unique within the document that it occurs, so this is sufficiently random
10    #to make a clash extremely unlikely. The string is alphanumeric but always begins with a letter. 
11    #Note: tested with 50000 ids and all were different
12    randomID=random.choice(string.letters)
13    for i in range(7):
14        randomID=randomID+random.choice(string.letters+string.digits)
15    return randomID
16
17def strToDate(ymd):
18    '''given a string in form '2006-01-01' returns a datetime object'''
19    return datetime.date(*map(int, ymd.split('-')))
20
21class EnvelopeAggregator(object):
22    def __init__(self,envelope):
23        '''start with aggregated envelope equal to the initialising envelope
24        envelope must be of type csml.parser.EnvelopeWithTimePeriod'''
25        self.envelope=envelope
26        self.minX=self.envelope.lowerCorner.CONTENT.split()[0]
27        self.minY=self.envelope.lowerCorner.CONTENT.split()[1]
28        self.maxX=self.envelope.upperCorner.CONTENT.split()[0]
29        self.maxY=self.envelope.upperCorner.CONTENT.split()[1]
30        self.t1= strToDate(self.envelope.timePosition[0].CONTENT)
31        self.t2= strToDate(self.envelope.timePosition[1].CONTENT)
32 
33    def _compareLowerCorners(self,lowerCorner):
34        minX,minY=lowerCorner.CONTENT.split()[0],lowerCorner.CONTENT.split()[1]
35        if float(minX) < float(self.minX):
36            self.envelope.lowerCorner.CONTENT=str(minX +' '+ self.minY)
37            self.minX=minX
38        if float(minY) < float(self.minY):
39            self.envelope.lowerCorner.CONTENT=str(self.minX +' '+ minY)
40            self.minY=minY
41           
42    def _compareUpperCorners(self,upperCorner):
43        maxX,maxY=upperCorner.CONTENT.split()[0],upperCorner.CONTENT.split()[1]
44        if float(maxX) > float(self.maxX):
45            self.envelope.upperCorner.CONTENT=str(maxX +' '+ self.maxY)
46            self.maxX=maxX
47        if float(maxY) > float(self.maxY):
48            self.envelope.upperCorner.CONTENT=str(self.maxX +' '+ maxY)
49            self.maxY=maxY
50           
51    def _compareLowerTimes(self,timepos):
52        t=strToDate(timepos)
53        if t<self.t1:
54            self.t1=t
55       
56    def _compareUpperTimes(self,timepos):
57        t=strToDate(timepos)
58        if t>self.t2:
59            self.t2=t
60   
61    def compareEnvelope(self,envtocheck):
62        '''compares new envelope, and if necessary changes the aggregated envelope.'''
63        if envtocheck.srsName!=self.envelope.srsName:
64            print 'Can currently only perform aggregation within the same spatio-temporal reference system.'
65            sys.exit()
66        else: 
67            self._compareLowerCorners(envtocheck.lowerCorner)
68            self._compareUpperCorners(envtocheck.upperCorner)
69            self._compareLowerTimes(envtocheck.timePosition[0].CONTENT)
70            self._compareUpperTimes(envtocheck.timePosition[1].CONTENT)
71   
72    def getAggregatedEnvelope(self):
73        self.envelope.timePosition=[csml.parser.csString(str(self.t1)), csml.parser.csString(str(self.t2))] #convert from datetime types
74        return self.envelope
75
76
77
78def getSeqRule(nDims):
79        #returns a sequenceRule
80        #*****************very simplified version*****************
81        #This really needs more work to see if the +s and -s are correct. and the x/y/z s in right order
82        #nDims = spatial dimensions + time dimension
83       
84        #TODO CSML2 - rewrite this
85        if nDims == 2:
86                seq = "Linear"
87        elif nDims ==3:
88                seq = "+x+y"
89        elif nDims ==4:
90                seq = "+x+y+z"
91        else:
92                seq="not sure"
93        return seq
94       
95       
96def getMappingRule(nDims):
97        #TODO CSML2 - rewrite this
98        if nDims == 2:
99                mr = "+ gridI + series"
100        elif nDims ==3:
101                mr = "+ gridI + gridJ +series"
102        elif nDims ==4:
103                mr = "+ gridI + gridJ +gridK +series"
104        else:
105                mr="not sure"
106        return mr
107       
108
109def addEnvelope(fc, ffmap):
110    #adds EnvelopeWithTimePeriod to feature collection, based on the spatial extent of the features within.
111   
112    #process:   
113    #Extract the lat/lon and time extents from each representative file.    Create a temporary EnvelopeWithTimePeriod object, then use the EnvelopeAggregator class to aggregate into an envelope for the whole feature collection.
114   
115    #TODO, need to use the CRS catalogue to get these values
116    lonname='longitude'
117    latname='latitude'
118    timename='time'
119    srs='ndg:crs:xyt'
120   
121       
122    aggregator=None
123    for feature in fc.featureMembers:
124        if type(feature) is csml.parser.GridSeriesFeature:
125            tmax=None
126            tmin=None
127            for ord in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
128                if ord.coordAxisLabel.CONTENT==timename:
129                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
130                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
131                    if tmin ==None:
132                        tmin=tnewmin
133                    if tmax == None:
134                        tmax=tnewmax
135                    if tnewmin < tmin:
136                        tmin=tnewmin                   
137                    if tnewmax > tmax:
138                        tmax=tnewmax                     
139        if type(feature) is csml.parser.ProfileSeriesFeature:
140            tmax=None
141            tmin=None
142            for ord in feature.value.profileSeriesDomain.coordTransformTable.gridOrdinates:
143                if ord.coordAxisLabel.CONTENT==timename:
144                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
145                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
146                    if tmin ==None:
147                        tmin=tnewmin
148                    if tmax == None:
149                        tmax=tnewmax
150                    if tnewmin < tmin:
151                        tmin=tnewmin                   
152                    if tnewmax > tmax:
153                        tmax=tnewmax                     
154                       
155    for repfile in ffmap.getRepresentativeFiles():
156        minlon=None
157        maxlon=None
158        minlat=None
159        maxlat=None
160        f=repfile.getRepresentativeFileName()
161        DI=csml.csmllibs.csmldataiface.DataInterface()
162        DI=DI.getUnknownInterfaceType(f)
163        DI.openFile(f)
164        tmpDims=DI.getListOfAxes()
165        if lonname in tmpDims:
166            DI.setAxis(lonname)
167            vals=DI.getDataForAxis()
168            minlon=str(min(vals))
169            maxlon= str(max(vals))
170            if latname in tmpDims:
171                DI.setAxis(latname)
172                vals=DI.getDataForAxis()
173                minlat=str(min(vals))
174                maxlat= str(max(vals))                         
175        else:
176            tmpVars=DI.getListofVariables()
177            if lonname in tmpVars:
178                DI.setVariable(lonname)
179                vals=DI.getDataForVar()
180                minlon=str(min(vals))
181                maxlon= str(max(vals))
182                if latname in tmpVars:
183                    DI.setVariable(latname)
184                    vals=DI.getDataForVar()
185                    minlat=str(min(vals))
186                    maxlat= str(max(vals))
187           
188        env=csml.parser.EnvelopeWithTimePeriod()
189        env.lowerCorner=csml.parser.csString(minlon + ' ' + minlat)
190        env.upperCorner=csml.parser.csString(maxlon + ' ' + maxlat)
191        env.timePosition=[csml.parser.csString(tmin), csml.parser.csString(tmax)]
192        env.srsName='srs tba'
193        if aggregator is None:
194            aggregator=EnvelopeAggregator(env)
195        else:
196            aggregator.compareEnvelope(env)
197    fcEnvelope=aggregator.getAggregatedEnvelope()
198    fcEnvelope.srsName=env.srsName 
199    fc.boundedBy=fcEnvelope
200    fc.boundedBy=env
201    return fc
202       
203       
204       
205       
206# a couple of string cleaning functions:
207#   don't think these are used now
208def cleanString1(messystring):
209        #removes outer brackets and changes commas to spaces
210        cleanstring = messystring[1:-1]
211        cleanstring = cleanstring.replace(',',' ')
212        #strip off first (time) dimension.
213        #note, this might need rethinking for other datasets
214        cleanstring = cleanstring[3:]
215        return cleanstring
216#       
217def cleanString(messystring):
218   # removes outer brackets and 's, but leaves commas.
219    cleanstring = messystring[1:-1]
220    cleanstring = cleanstring.replace("'",'')
221    #remove any carriage returns
222    cleanstring = cleanstring.replace("\n",'')
223    return cleanstring
224       
225def listify(item):
226    ''' listify checks if an item is a list, if it isn't it puts it inside a list and returns it. Always returns a list object'''
227    if type(item) is list:
228        return item
229    else:
230        return [item]       
231
232def main():
233    #test to check randomness of getRandomID()
234    idlist=[]
235    for j in range (500):
236        for i in range(100):
237            newid= getRandomID()
238            if newid in idlist:
239                print 'ID matched!!!'               
240                sys.exit()
241            else:               
242                idlist.append(newid)
243        print i * j
244    print 'done'
245   
246if __name__=="__main__":
247    main()
Note: See TracBrowser for help on using the repository browser.