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

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

added EnvelopeWithTimePeriod?, also, more on subsetting

  • 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
3import string
4import csml.parser
5
6def getRandomID():
7    #returns a random 8 character alphanumeric string that can be used as an internal identifier in a CSML document.
8    #the ID only needs to be unique within the document that it occurs, so this is sufficiently random
9    #to make a clash extremely unlikely. The string is alphanumeric but always begins with a letter. 
10    #Note: tested with 50000 ids and all were different
11    randomID=random.choice(string.letters)
12    for i in range(7):
13        randomID=randomID+random.choice(string.letters+string.digits)
14    return randomID
15   
16
17def addEnvelope(fc):
18    #adds EnvelopeWithTimePeriod to feature collection, based on the spatial extent of the features within.
19    #TO DO - this calculation, placeholder for now
20    env=csml.parser.EnvelopeWithTimePeriod()
21    env.lowerCorner=csml.parser.csString('lc')
22    env.upperCorner=csml.parser.csString('uc')
23    env.timePosition1=csml.parser.csString('tp1')
24    env.timePosition2=csml.parser.csString('tp2')
25    fc.boundedBy=env
26    return fc
27
28def getSeqRule(nDims):
29        #returns a sequenceRule
30        #*****************very simplified version*****************
31        #This really needs more work to see if the +s and -s are correct. and the x/y/z s in right order
32        #nDims = spatial dimensions + time dimension
33       
34        #TODO CSML2 - rewrite this
35        if nDims == 2:
36                seq = "Linear"
37        elif nDims ==3:
38                seq = "+x+y"
39        elif nDims ==4:
40                seq = "+x+y+z"
41        else:
42                seq="not sure"
43        return seq
44       
45       
46def getMappingRule(nDims):
47        #TODO CSML2 - rewrite this
48        if nDims == 2:
49                mr = "+ gridI + series"
50        elif nDims ==3:
51                mr = "+ gridI + gridJ +series"
52        elif nDims ==4:
53                mr = "+ gridI + gridJ +gridK +series"
54        else:
55                mr="not sure"
56        return mr
57       
58       
59# a couple of string cleaning functions:
60#   don't think these are used now
61def cleanString1(messystring):
62        #removes outer brackets and changes commas to spaces
63        cleanstring = messystring[1:-1]
64        cleanstring = cleanstring.replace(',',' ')
65        #strip off first (time) dimension.
66        #note, this might need rethinking for other datasets
67        cleanstring = cleanstring[3:]
68        return cleanstring
69#       
70def cleanString(messystring):
71   # removes outer brackets and 's, but leaves commas.
72    cleanstring = messystring[1:-1]
73    cleanstring = cleanstring.replace("'",'')
74    #remove any carriage returns
75    cleanstring = cleanstring.replace("\n",'')
76    return cleanstring
77       
78def listify(item):
79    ''' 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'''
80    if type(item) is list:
81        return item
82    else:
83        return [item]       
84
85def main():
86    #test to check randomness of getRandomID()
87    idlist=[]
88    for j in range (500):
89        for i in range(100):
90            newid= getRandomID()
91            if newid in idlist:
92                print 'ID matched!!!'               
93                sys.exit()
94            else:               
95                idlist.append(newid)
96        print i * j
97    print 'done'
98   
99if __name__=="__main__":
100    main()
Note: See TracBrowser for help on using the repository browser.