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

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

added EnvelopeAggregator? class and code to create an EnvelopeWithTimePeriod? for the feature collection

  • 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.timePosition1.CONTENT)
31        self.t2= strToDate(self.envelope.timePosition2.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.timePosition1.CONTENT)
70            self._compareUpperTimes(envtocheck.timePosition2.CONTENT)
71   
72    def getAggregatedEnvelope(self):
73        self.envelope.timePosition=str(self.t1)  #convert from datetime types
74        self.envelope.timePosition2=str(self.t2)
75        return self.envelope
76
77
78
79def getSeqRule(nDims):
80        #returns a sequenceRule
81        #*****************very simplified version*****************
82        #This really needs more work to see if the +s and -s are correct. and the x/y/z s in right order
83        #nDims = spatial dimensions + time dimension
84       
85        #TODO CSML2 - rewrite this
86        if nDims == 2:
87                seq = "Linear"
88        elif nDims ==3:
89                seq = "+x+y"
90        elif nDims ==4:
91                seq = "+x+y+z"
92        else:
93                seq="not sure"
94        return seq
95       
96       
97def getMappingRule(nDims):
98        #TODO CSML2 - rewrite this
99        if nDims == 2:
100                mr = "+ gridI + series"
101        elif nDims ==3:
102                mr = "+ gridI + gridJ +series"
103        elif nDims ==4:
104                mr = "+ gridI + gridJ +gridK +series"
105        else:
106                mr="not sure"
107        return mr
108       
109       
110       
111       
112
113def addEnvelope(fc, ffmap):
114    #adds EnvelopeWithTimePeriod to feature collection, based on the spatial extent of the features within.
115   
116    #process:   
117    #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.
118   
119    #TODO, need to use the CRS catalogue to get these values
120    lonname='longitude'
121    latname='latitude'
122    timename='time'
123    srs='ndg:crs:xyt'
124   
125    import sys
126   
127    aggregator=None
128    for feature in fc.featureMembers:
129        if type(feature) is csml.parser.GridSeriesFeature:
130           
131            tmax=None
132            tmin=None
133            for ord in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
134                if ord.coordAxisLabel.CONTENT==timename:
135                    #print ord.coordAxisValues.coordinateList.CONTENT                               
136                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
137                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
138                    if tmin ==None:
139                        tmin=tnewmin
140                    if tmax == None:
141                        tmax=tnewmax
142                    if tnewmin < tmin:
143                        tmin=tnewmin                   
144                    if tnewmax > tmax:
145                        tmax=tnewmax                     
146 
147    for repfile in ffmap.getRepresentativeFiles():
148        minlon=None
149        maxlon=None
150        minlat=None
151        maxlat=None
152        f=repfile.getRepresentativeFileName()
153        DI=csml.csmllibs.csmldataiface.DataInterface()
154        DI=DI.getUnknownInterfaceType(f)
155        DI.openFile(f)
156        tmpDims=DI.getListOfAxes()
157        if lonname in tmpDims:
158            DI.setAxis(lonname)
159            vals=DI.getDataForAxis()
160            minlon=str(min(vals))
161            maxlon= str(max(vals))
162        if latname in tmpDims:
163            DI.setAxis(latname)
164            vals=DI.getDataForAxis()
165            minlat=str(min(vals))
166            maxlat= str(max(vals))                         
167        env=csml.parser.EnvelopeWithTimePeriod()
168        env.lowerCorner=csml.parser.csString(minlon + ' ' + minlat)
169        env.upperCorner=csml.parser.csString(maxlon + ' ' + maxlat)
170        env.timePosition1=csml.parser.csString(tmin)
171        env.timePosition2=csml.parser.csString(tmax)
172        env.srsName='srs tba'
173        if aggregator is None:
174            aggregator=EnvelopeAggregator(env)
175        else:
176            aggregator.compareEnvelope(env)
177    fcEnvelope=aggregator.getAggregatedEnvelope()
178    print type(fcEnvelope.timePosition1)
179    fcEnvelope.srsName=env.srsName 
180    fc.boundedBy=fcEnvelope
181    #sys.exit()
182    fc.boundedBy=env
183    return fc
184       
185       
186       
187       
188# a couple of string cleaning functions:
189#   don't think these are used now
190def cleanString1(messystring):
191        #removes outer brackets and changes commas to spaces
192        cleanstring = messystring[1:-1]
193        cleanstring = cleanstring.replace(',',' ')
194        #strip off first (time) dimension.
195        #note, this might need rethinking for other datasets
196        cleanstring = cleanstring[3:]
197        return cleanstring
198#       
199def cleanString(messystring):
200   # removes outer brackets and 's, but leaves commas.
201    cleanstring = messystring[1:-1]
202    cleanstring = cleanstring.replace("'",'')
203    #remove any carriage returns
204    cleanstring = cleanstring.replace("\n",'')
205    return cleanstring
206       
207def listify(item):
208    ''' 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'''
209    if type(item) is list:
210        return item
211    else:
212        return [item]       
213
214def main():
215    #test to check randomness of getRandomID()
216    idlist=[]
217    for j in range (500):
218        for i in range(100):
219            newid= getRandomID()
220            if newid in idlist:
221                print 'ID matched!!!'               
222                sys.exit()
223            else:               
224                idlist.append(newid)
225        print i * j
226    print 'done'
227   
228if __name__=="__main__":
229    main()
Note: See TracBrowser for help on using the repository browser.