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

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

Numeric.array is now the standard data container

  • 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           
129            tmax=None
130            tmin=None
131            for ord in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
132                if ord.coordAxisLabel.CONTENT==timename:
133                    tnewmin= strToDate(ord.coordAxisValues.coordinateList.CONTENT.split()[0].split('T')[0])
134                    tnewmax=strToDate( ord.coordAxisValues.coordinateList.CONTENT.split()[len(ord.coordAxisValues.coordinateList.CONTENT.split())-1].split('T')[0])
135                    if tmin ==None:
136                        tmin=tnewmin
137                    if tmax == None:
138                        tmax=tnewmax
139                    if tnewmin < tmin:
140                        tmin=tnewmin                   
141                    if tnewmax > tmax:
142                        tmax=tnewmax                     
143 
144    for repfile in ffmap.getRepresentativeFiles():
145        minlon=None
146        maxlon=None
147        minlat=None
148        maxlat=None
149        f=repfile.getRepresentativeFileName()
150        DI=csml.csmllibs.csmldataiface.DataInterface()
151        DI=DI.getUnknownInterfaceType(f)
152        DI.openFile(f)
153        tmpDims=DI.getListOfAxes()
154        if lonname in tmpDims:
155            DI.setAxis(lonname)
156            vals=DI.getDataForAxis()
157            minlon=str(min(vals))
158            maxlon= str(max(vals))
159        if latname in tmpDims:
160            DI.setAxis(latname)
161            vals=DI.getDataForAxis()
162            minlat=str(min(vals))
163            maxlat= str(max(vals))                         
164        env=csml.parser.EnvelopeWithTimePeriod()
165        env.lowerCorner=csml.parser.csString(minlon + ' ' + minlat)
166        env.upperCorner=csml.parser.csString(maxlon + ' ' + maxlat)
167        env.timePosition=[csml.parser.csString(tmin), csml.parser.csString(tmax)]
168        env.srsName='srs tba'
169        if aggregator is None:
170            aggregator=EnvelopeAggregator(env)
171        else:
172            aggregator.compareEnvelope(env)
173    fcEnvelope=aggregator.getAggregatedEnvelope()
174    fcEnvelope.srsName=env.srsName 
175    fc.boundedBy=fcEnvelope
176    fc.boundedBy=env
177    return fc
178       
179       
180       
181       
182# a couple of string cleaning functions:
183#   don't think these are used now
184def cleanString1(messystring):
185        #removes outer brackets and changes commas to spaces
186        cleanstring = messystring[1:-1]
187        cleanstring = cleanstring.replace(',',' ')
188        #strip off first (time) dimension.
189        #note, this might need rethinking for other datasets
190        cleanstring = cleanstring[3:]
191        return cleanstring
192#       
193def cleanString(messystring):
194   # removes outer brackets and 's, but leaves commas.
195    cleanstring = messystring[1:-1]
196    cleanstring = cleanstring.replace("'",'')
197    #remove any carriage returns
198    cleanstring = cleanstring.replace("\n",'')
199    return cleanstring
200       
201def listify(item):
202    ''' 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'''
203    if type(item) is list:
204        return item
205    else:
206        return [item]       
207
208def main():
209    #test to check randomness of getRandomID()
210    idlist=[]
211    for j in range (500):
212        for i in range(100):
213            newid= getRandomID()
214            if newid in idlist:
215                print 'ID matched!!!'               
216                sys.exit()
217            else:               
218                idlist.append(newid)
219        print i * j
220    print 'done'
221   
222if __name__=="__main__":
223    main()
Note: See TracBrowser for help on using the repository browser.