1 | '''csml utils contains useful utility functions''' |
---|
2 | import csml |
---|
3 | |
---|
4 | def listify(item): |
---|
5 | ''' 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''' |
---|
6 | if type(item) is list: |
---|
7 | return item |
---|
8 | else: |
---|
9 | return [item] |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | #deal with longitude requests |
---|
14 | #if the request is in -ve,+ve eg (-30,30) but the data is in (0,360) need to handle this by changing the args. |
---|
15 | def fixLongitude(request, kwargs): |
---|
16 | def _anyitemlessthanzero(listofitems): |
---|
17 | for item in listofitems: |
---|
18 | if item <0: |
---|
19 | return True |
---|
20 | return False |
---|
21 | for key in request.keys(): |
---|
22 | if kwargs.has_key(key): |
---|
23 | if type(kwargs[key]) is not tuple: |
---|
24 | continue |
---|
25 | elif key == 'longitude': #how do we test if it is longitude properly? |
---|
26 | if _anyitemlessthanzero(request[key]): |
---|
27 | pass #no need to convert request if data is also -ve to +ve |
---|
28 | else: |
---|
29 | for val in request[key]: |
---|
30 | if val < 0: |
---|
31 | pass |
---|
32 | else: |
---|
33 | if kwargs[key][0] < 0: |
---|
34 | kwargs[key]=(kwargs[key][0]+360,kwargs[key][1]) |
---|
35 | if kwargs[key][1] < 0: |
---|
36 | kwargs[key]=(kwargs[key][0],kwargs[key][1]+360) |
---|
37 | return kwargs |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | def nudgeSingleValuesToAxisValues(value, axisValues): |
---|
42 | """ |
---|
43 | Takes a value and checks if it is in the axisValues array. If not, it nudges the |
---|
44 | value to the nearest neighbour in axis. It returns the new value twice along |
---|
45 | with a message describing the change. |
---|
46 | """ |
---|
47 | |
---|
48 | rtMessage="" |
---|
49 | newValue=None |
---|
50 | |
---|
51 | def __fixTimes(value, axisValues): |
---|
52 | newValue = csml.csmllibs.csmltime.getTimeAsValue(value) |
---|
53 | newAxisValues=[] |
---|
54 | for ax in axisValues: |
---|
55 | newAxisValues.append(csml.csmllibs.csmltime.getTimeAsValue(ax)) |
---|
56 | return newValue, newAxisValues |
---|
57 | |
---|
58 | if value in axisValues: |
---|
59 | newValue=value |
---|
60 | else: |
---|
61 | if str(value).find('T') != -1: |
---|
62 | value, axisValues=__fixTimes(value,axisValues) |
---|
63 | sortedAxis=[] |
---|
64 | for i in axisValues: |
---|
65 | sortedAxis.append(i) |
---|
66 | sortedAxis.sort() |
---|
67 | if value<sortedAxis[0]: |
---|
68 | newValue=sortedAxis[0] |
---|
69 | elif value>sortedAxis[-1]: |
---|
70 | newValue=sortedAxis[-1] |
---|
71 | else: |
---|
72 | for i in range(len(axisValues)): |
---|
73 | if i<(len(axisValues)-1): |
---|
74 | (current, nextone)=(axisValues[i], axisValues[i+1]) |
---|
75 | if current>nextone: |
---|
76 | tempc=nextone |
---|
77 | nextone=current |
---|
78 | current=tempc |
---|
79 | if value>current and value<nextone: |
---|
80 | lowergap=value-current |
---|
81 | uppergap=nextone-value |
---|
82 | if uppergap==lowergap: |
---|
83 | newValue=nextone |
---|
84 | elif uppergap>lowergap: |
---|
85 | newValue=current |
---|
86 | elif uppergap<lowergap: |
---|
87 | newValue=nextone |
---|
88 | break |
---|
89 | if newValue==None: |
---|
90 | axisType='unknown' |
---|
91 | rtMessage="%s axis selected value '%s' nudged to nearest value in real axis '%s' ;" % (axisType, value, newValue) |
---|
92 | # print rtMessag |
---|
93 | if type(newValue) is float: |
---|
94 | if len(str(int(newValue))) ==14: # it's a time |
---|
95 | newValue=csml.csmllibs.csmltime.convertValueToCSML(newValue) |
---|
96 | return (newValue) |
---|