1 | import re |
---|
2 | |
---|
3 | class markupHtml(object): |
---|
4 | def __init__(self,base): |
---|
5 | self.resect = re.compile( '(\{[a-zA-Z]*\})' ) |
---|
6 | self.relink = re.compile( '(\[http\S*.*\])' ) |
---|
7 | self.base = base |
---|
8 | |
---|
9 | def parse(self,ss): |
---|
10 | for x in self.resect.findall( ss ): |
---|
11 | ss = ss.replace( x, self.sectionlink(x) ) |
---|
12 | for x in self.relink.findall( ss ): |
---|
13 | ss = ss.replace( x, self.linklink(x) ) |
---|
14 | return ss |
---|
15 | |
---|
16 | def sectionlink( self, x ): |
---|
17 | x1 = x[1:-1] |
---|
18 | return '<a href=%s%s.html>%s</a>' % (self.base,x1,x1) |
---|
19 | def linklink( self, x ): |
---|
20 | a,b = x[1:-1].split( None, 1 ) |
---|
21 | return '<a href=%s>%s</a>' % (a,b) |
---|
22 | |
---|
23 | |
---|
24 | class gridOptionSort(object): |
---|
25 | def __init__(self,oldpy=True): |
---|
26 | self.od = {'1deg':'1001','100km':'1001','2deg':'1002','native':'0102','DEF':'9000','':'9001','native:01':'0101'} |
---|
27 | self.oldpy = oldpy |
---|
28 | |
---|
29 | def cmp(self,x,y): |
---|
30 | return cmp( self.od[x], self.od[y] ) |
---|
31 | |
---|
32 | def sort(self,ll): |
---|
33 | if self.oldpy: |
---|
34 | return sorted( ll, cmp=self.cmp ) |
---|
35 | else: |
---|
36 | return sorted( ll, key=lambda x: self.od[x] ) |
---|
37 | |
---|
38 | class cmvFilter(object): |
---|
39 | """Class to filter list of CMOR variables by rank. |
---|
40 | dq: data request object, as returned by dreq.loadDreq()""" |
---|
41 | |
---|
42 | def __init__(self,sc,dq=None): |
---|
43 | self.sc = sc |
---|
44 | if dq == None: |
---|
45 | self.dq = self.sc.dq |
---|
46 | else: |
---|
47 | self.dq = dq |
---|
48 | |
---|
49 | self.defaultPriority = 3 |
---|
50 | |
---|
51 | def __test__(self,silentFail=False,silentPass=True): |
---|
52 | """Test the class against default request document. |
---|
53 | Returns True if test is passed, False otherwise. |
---|
54 | silentFail: return without printing a message if test fails. |
---|
55 | silentPass: return without printing a message if test passes. |
---|
56 | """ |
---|
57 | cmv = self.filterByChoiceRank() |
---|
58 | self.cmv = cmv |
---|
59 | vidt = [i.uid for i in self.dq.coll['var'].items if i.label == 'ta'] |
---|
60 | if len(vidt) != 1: |
---|
61 | if not silentFail: |
---|
62 | print ('FAIL: could not identify ta in "var" section' ) |
---|
63 | return False |
---|
64 | |
---|
65 | vidt = vidt[0] |
---|
66 | taf = [i for i in cmv if self.dq.inx.uid[i].vid == vidt and self.dq.inx.uid[i].frequency in ['3hr','6hr']] |
---|
67 | tau = [i for i in self.sc.allVars if self.dq.inx.uid[i].vid == vidt and self.dq.inx.uid[i].frequency in ['3hr','6hr']] |
---|
68 | ## |
---|
69 | ## NB not easy to separate pressure level from model level variables here |
---|
70 | ## at 01.beta.06 do not filter CMIP5 6hrPlev and 27 level HighResMIP .... |
---|
71 | ## |
---|
72 | if len(taf) == 5 and len(tau) == 6: |
---|
73 | if not silentPass: |
---|
74 | print ('OK: tests passed') |
---|
75 | return True |
---|
76 | else: |
---|
77 | if not silentFail: |
---|
78 | print ( 'FAIL: tests failed: len filtered=%s, unfiltered=%s' % (len(taf),len(tau)) ) |
---|
79 | return False |
---|
80 | |
---|
81 | def filterByChoiceCfg(self,cmv=None,cfg={}): |
---|
82 | """Filter a set of CMOR variable identifiers by configuration as specified in varChoiceLinkC section of the data request. |
---|
83 | cmv: set of CMOR variable identifiers. |
---|
84 | |
---|
85 | Returns the filetered set. The items removed are available in self.rejected.""" |
---|
86 | ## |
---|
87 | ## cmv is a set of CMORvar ids |
---|
88 | ## |
---|
89 | if cmv == None: |
---|
90 | self.sc.volByMip( 'HighResMIP', pmax=self.defaultPriority ) |
---|
91 | cmv = self.sc.allVars.copy() |
---|
92 | |
---|
93 | ## set of vids associated with choices |
---|
94 | s = set() |
---|
95 | for i in self.dq.coll['varChoiceLinkC'].items: |
---|
96 | s.add( i.vid ) |
---|
97 | |
---|
98 | ## set of variables in current selection associated with choices |
---|
99 | v1 = set( [ u for u in cmv if u in s ] ) |
---|
100 | if len(v1) == 0: |
---|
101 | ## print 'Nothing to do' |
---|
102 | return cmv |
---|
103 | |
---|
104 | ## set of "rank" choice groups relevant to current selection |
---|
105 | s1 = set( [i.cid for i in self.dq.coll['varChoiceLinkC'].items if i.vid in v1] ) |
---|
106 | |
---|
107 | self.rejected = set() |
---|
108 | for s in s1: |
---|
109 | imr = set() |
---|
110 | ## |
---|
111 | ## set of choice links in group s which relate to a variable in current selection |
---|
112 | ## |
---|
113 | this = set() |
---|
114 | for i in self.dq.coll['varChoiceLinkC'].items: |
---|
115 | if i.vid in v1 and i.cid == s: |
---|
116 | set.add(i) |
---|
117 | ## |
---|
118 | ## value of configuration option (defaults to True here). |
---|
119 | ## |
---|
120 | testval = cfg.get( s, True ) |
---|
121 | for i in this: |
---|
122 | if i.cfg != testval: |
---|
123 | l1 = len(cmv) |
---|
124 | cmv.remove( i.vid ) |
---|
125 | if len(cmv) == l1: |
---|
126 | print ( 'Failed to remove i.vid=%s' % i.vid ) |
---|
127 | self.rejected.add( i.vid ) |
---|
128 | else: |
---|
129 | imr.add( i ) |
---|
130 | ## print self.dq.inx.uid[s].label, len(this), len(imr) |
---|
131 | |
---|
132 | return cmv |
---|
133 | |
---|
134 | def filterByChoiceRank(self,cmv=None,asDict=False): |
---|
135 | """Filter a set of CMOR variable identifiers by rank as specified in varChoiceLinkR section of the data request. |
---|
136 | cmv: set of CMOR variable identifiers. |
---|
137 | |
---|
138 | Returns the filetered set. The items removed are available in self.rejected.""" |
---|
139 | if asDict: |
---|
140 | assert cmv != None, 'Cannot have empty cmv argument if asDict is True' |
---|
141 | ## |
---|
142 | ## cmv is a set of CMORvar ids |
---|
143 | ## |
---|
144 | if cmv == None: |
---|
145 | self.sc.volByMip( 'HighResMIP', pmax=self.defaultPriority ) |
---|
146 | cmv = self.sc.allVars.copy() |
---|
147 | |
---|
148 | ## set of vids associated with choices |
---|
149 | s = set( [i.vid for i in self.dq.coll['varChoiceLinkR'].items] ) |
---|
150 | |
---|
151 | ## set of variables in current selection associated with choices |
---|
152 | v1 = set( [ u for u in cmv if u in s ] ) |
---|
153 | if len(v1) == 0: |
---|
154 | ## print 'Nothing to do' |
---|
155 | if asDict: |
---|
156 | return |
---|
157 | else: |
---|
158 | return cmv |
---|
159 | |
---|
160 | ## set of "rank" choice groups relevant to current selection |
---|
161 | s1 = set( [i.cid for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1] ) |
---|
162 | |
---|
163 | self.rejected = set() |
---|
164 | for s in s1: |
---|
165 | imr = set() |
---|
166 | ## |
---|
167 | ## set of choice links in group s which relate to a variable in current selection |
---|
168 | ## |
---|
169 | this = set( [i for i in self.dq.coll['varChoiceLinkR'].items if i.vid in v1 and i.cid == s] ) |
---|
170 | if len(this) > 1: |
---|
171 | mr = max( [i.rank for i in this ] ) |
---|
172 | for i in this: |
---|
173 | if i.rank < mr: |
---|
174 | l1 = len(cmv) |
---|
175 | if asDict: |
---|
176 | cmv.pop( i.vid ) |
---|
177 | else: |
---|
178 | cmv.remove( i.vid ) |
---|
179 | if len(cmv) == l1: |
---|
180 | print ( 'Failed to remove i.vid=%s' % i.vid ) |
---|
181 | self.rejected.add( i.vid ) |
---|
182 | else: |
---|
183 | imr.add( i ) |
---|
184 | ## print self.dq.inx.uid[s].label, len(this), len(imr), mr |
---|
185 | else: |
---|
186 | ## print self.dq.inx.uid[s].label, len(this) |
---|
187 | pass |
---|
188 | |
---|
189 | if asDict: |
---|
190 | return |
---|
191 | else: |
---|
192 | return cmv |
---|
193 | |
---|
194 | |
---|
195 | test = cmvFilter |
---|