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

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

bounding box working for profileseries

  • 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       
109       
110       
111
112def addEnvelope(fc, ffmap):
113    #adds EnvelopeWithTimePeriod to feature collection, based on the spatial extent of the features within.
114   
115    #process:   
116    #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.
117   
118    #TODO, need to use the CRS catalogue to get these values
119    lonname='longitude'
120    latname='latitude'
121    timename='time'
122    srs='ndg:crs:xyt'
123   
124       
125    aggregator=None
126    for feature in fc.featureMembers:
127        if type(feature) is csml.parser.GridSeriesFeature:
128            tmax=None
129            tmin=None
130            for ord in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
131                if ord.coordAxisLabel.CONTENT==timename:
132                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
133                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
134                    if tmin ==None:
135                        tmin=tnewmin
136                    if tmax == None:
137                        tmax=tnewmax
138                    if tnewmin < tmin:
139                        tmin=tnewmin                   
140                    if tnewmax > tmax:
141                        tmax=tnewmax                     
142        if type(feature) is csml.parser.ProfileSeriesFeature:
143            tmax=None
144            tmin=None
145            for ord in feature.value.profileSeriesDomain.coordTransformTable.gridOrdinates:
146                if ord.coordAxisLabel.CONTENT==timename:
147                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
148                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
149                    if tmin ==None:
150                        tmin=tnewmin
151                    if tmax == None:
152                        tmax=tnewmax
153                    if tnewmin < tmin:
154                        tmin=tnewmin                   
155                    if tnewmax > tmax:
156                        tmax=tnewmax                     
157                       
158    for repfile in ffmap.getRepresentativeFiles():
159        minlon=None
160        maxlon=None
161        minlat=None
162        maxlat=None
163        f=repfile.getRepresentativeFileName()
164        DI=csml.csmllibs.csmldataiface.DataInterface()
165        DI=DI.getUnknownInterfaceType(f)
166        DI.openFile(f)
167        tmpDims=DI.getListOfAxes()
168        if lonname in tmpDims:
169            DI.setAxis(lonname)
170            vals=DI.getDataForAxis()
171            minlon=str(min(vals))
172            maxlon= str(max(vals))
173            if latname in tmpDims:
174                DI.setAxis(latname)
175                vals=DI.getDataForAxis()
176                minlat=str(min(vals))
177                maxlat= str(max(vals))                         
178        else:
179            tmpVars=DI.getListofVariables()
180            if lonname in tmpVars:
181                DI.setVariable(lonname)
182                vals=DI.getDataForVar()
183                minlon=str(min(vals))
184                maxlon= str(max(vals))
185                if latname in tmpVars:
186                    DI.setVariable(latname)
187                    vals=DI.getDataForVar()
188                    minlat=str(min(vals))
189                    maxlat= str(max(vals))
190           
191        env=csml.parser.EnvelopeWithTimePeriod()
192        env.lowerCorner=csml.parser.csString(minlon + ' ' + minlat)
193        env.upperCorner=csml.parser.csString(maxlon + ' ' + maxlat)
194        env.timePosition=[csml.parser.csString(tmin), csml.parser.csString(tmax)]
195        env.srsName='srs tba'
196        if aggregator is None:
197            aggregator=EnvelopeAggregator(env)
198        else:
199            aggregator.compareEnvelope(env)
200    fcEnvelope=aggregator.getAggregatedEnvelope()
201    fcEnvelope.srsName=env.srsName 
202    fc.boundedBy=fcEnvelope
203    fc.boundedBy=env
204    return fc
205       
206       
207       
208       
209# a couple of string cleaning functions:
210#   don't think these are used now
211def cleanString1(messystring):
212        #removes outer brackets and changes commas to spaces
213        cleanstring = messystring[1:-1]
214        cleanstring = cleanstring.replace(',',' ')
215        #strip off first (time) dimension.
216        #note, this might need rethinking for other datasets
217        cleanstring = cleanstring[3:]
218        return cleanstring
219#       
220def cleanString(messystring):
221   # removes outer brackets and 's, but leaves commas.
222    cleanstring = messystring[1:-1]
223    cleanstring = cleanstring.replace("'",'')
224    #remove any carriage returns
225    cleanstring = cleanstring.replace("\n",'')
226    return cleanstring
227       
228def listify(item):
229    ''' 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'''
230    if type(item) is list:
231        return item
232    else:
233        return [item]       
234
235def main():
236    #test to check randomness of getRandomID()
237    idlist=[]
238    for j in range (500):
239        for i in range(100):
240            newid= getRandomID()
241            if newid in idlist:
242                print 'ID matched!!!'               
243                sys.exit()
244            else:               
245                idlist.append(newid)
246        print i * j
247    print 'done'
248   
249if __name__=="__main__":
250    main()
Note: See TracBrowser for help on using the repository browser.