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