1 | """This module has a class which will analyse the usage of variables in the data request""" |
---|
2 | import operator |
---|
3 | import collections |
---|
4 | |
---|
5 | class checkVar(object): |
---|
6 | """checkVar |
---|
7 | -------- |
---|
8 | Class to analyse the usage of variables in the data request. |
---|
9 | """ |
---|
10 | def __init__(self,dq): |
---|
11 | self.dq = dq |
---|
12 | self.mips = set( [i.label for i in dq.coll['mip'].items] ) |
---|
13 | for i in ['PDRMIP', 'DECK', 'VIACSAB', 'SolarMIP', 'CMIP6' ]: |
---|
14 | self.mips.discard(i) |
---|
15 | |
---|
16 | def chk2(self,vn,byExpt=False, byBoth=False): |
---|
17 | dq = self.dq |
---|
18 | ks = [i for i in dq.coll['var'].items if i.label == vn ] |
---|
19 | |
---|
20 | v = ks[0] |
---|
21 | cc = {} |
---|
22 | l = dq.inx.iref_by_sect[v.uid].a['CMORvar'] |
---|
23 | for i in l: |
---|
24 | r = dq.inx.uid[i] |
---|
25 | kk = '%s.%s' % (r.mipTable, r.label ) |
---|
26 | cc[i] = (kk,self.chkCmv( i, byExpt=byExpt, byBoth=byBoth ) ) |
---|
27 | |
---|
28 | return cc |
---|
29 | |
---|
30 | def chk(self,vn): |
---|
31 | ks = [i for i in dq.coll['var'].items if i.label == vn ] |
---|
32 | |
---|
33 | v = ks[0] |
---|
34 | l = dq.inx.iref_by_sect[v.uid].a['CMORvar'] |
---|
35 | |
---|
36 | ## set of all request variables |
---|
37 | s = set() |
---|
38 | for i in l: |
---|
39 | for j in dq.inx.iref_by_sect[i].a['requestVar']: |
---|
40 | s.add(j) |
---|
41 | |
---|
42 | ## filter out the ones which link to a remark |
---|
43 | s0 = set( [i for i in s if dq.inx.uid[dq.inx.uid[i].vgid]._h.label != 'remarks' ] ) |
---|
44 | |
---|
45 | ## set of request groups |
---|
46 | |
---|
47 | s1 = set( [dq.inx.uid[i].vgid for i in s0 ] ) |
---|
48 | |
---|
49 | #s2 = set() |
---|
50 | #for i in s1: |
---|
51 | #for j in dq.inx.iref_by_sect[i].a['requestLink']: |
---|
52 | #s2.add(j) |
---|
53 | s2 = reduce( operator.or_, [set(dq.inx.iref_by_sect[i].a['requestLink']) for i in s1 if dq.inx.iref_by_sect[i].a.has_key('requestLink')] ) |
---|
54 | |
---|
55 | mips = set( [dq.inx.uid[i].mip for i in s2 ] ) |
---|
56 | self.missing = self.mips.difference( mips ) |
---|
57 | self.inc = mips |
---|
58 | |
---|
59 | ############# |
---|
60 | def chkCmv(self,cmvid, byExpt=False, byBoth=False): |
---|
61 | dq = self.dq |
---|
62 | s = set( dq.inx.iref_by_sect[cmvid].a['requestVar'] ) |
---|
63 | |
---|
64 | ## filter out the ones whch link to a remark |
---|
65 | |
---|
66 | # s0: set of requestVars |
---|
67 | |
---|
68 | s0 = set( [i for i in s if dq.inx.uid[dq.inx.uid[i].vgid]._h.label != 'remarks' ] ) |
---|
69 | |
---|
70 | ## set of request groups |
---|
71 | ## dictionary, keyed on variable group uid, with values set to priority of variable |
---|
72 | ## |
---|
73 | |
---|
74 | cc1 = collections.defaultdict( set ) |
---|
75 | for i in s0: |
---|
76 | cc1[ dq.inx.uid[i].vgid ].add( dq.inx.uid[i].priority ) |
---|
77 | ##s1 = set( [dq.inx.uid[i].vgid for i in s0 ] ) |
---|
78 | |
---|
79 | s2 = set() |
---|
80 | for i in cc1: |
---|
81 | if dq.inx.iref_by_sect[i].a.has_key('requestLink'): |
---|
82 | for l in dq.inx.iref_by_sect[i].a['requestLink']: |
---|
83 | lr = dq.inx.uid[l] |
---|
84 | if lr.opt == 'priority': |
---|
85 | p = int( float( lr.opar ) ) |
---|
86 | if max( cc1[i] ) <= p: |
---|
87 | s2.add(l) |
---|
88 | else: |
---|
89 | s2.add( l ) |
---|
90 | ##ll = [set(dq.inx.iref_by_sect[i].a['requestLink']) for i in cc1 if dq.inx.iref_by_sect[i].a.has_key('requestLink')] |
---|
91 | ##if len(ll) == 0: |
---|
92 | ##return set() |
---|
93 | ## |
---|
94 | ##s2 = reduce( operator.or_, ll) |
---|
95 | if len( s2 ) == 0: |
---|
96 | if byBoth: |
---|
97 | return (set(),set()) |
---|
98 | else: |
---|
99 | return s2 |
---|
100 | |
---|
101 | if byBoth or not byExpt: |
---|
102 | mips0 = set( [dq.inx.uid[i].mip for i in s2] ) |
---|
103 | if byExpt or byBoth: |
---|
104 | |
---|
105 | ## set of esid values |
---|
106 | esids = set() |
---|
107 | for i in s2: |
---|
108 | for u in dq.inx.iref_by_sect[i].a['requestItem']: |
---|
109 | esids.add( dq.inx.uid[u].esid ) |
---|
110 | mips = set() |
---|
111 | for e in esids: |
---|
112 | if e == '': |
---|
113 | ##print 'WARNING: empty esid encountered' |
---|
114 | pass |
---|
115 | else: |
---|
116 | r = dq.inx.uid[e] |
---|
117 | if r._h.label == 'mip': |
---|
118 | mips.add(e) |
---|
119 | else: |
---|
120 | if r._h.label == 'exptgroup': |
---|
121 | if 'experiment' in dq.inx.iref_by_sect[e].a: |
---|
122 | r = dq.inx.uid[ dq.inx.iref_by_sect[e].a['experiment'][0] ] |
---|
123 | else: |
---|
124 | ei = dq.inx.uid[e] |
---|
125 | print ( 'ERROR.exptgroup.00001: empty experiment group: %s: %s' % (ei.label, ei.title) ) |
---|
126 | if r._h.label in [ 'remarks','exptgroup']: |
---|
127 | ##print 'WARNING: link to remarks encountered' |
---|
128 | pass |
---|
129 | else: |
---|
130 | assert r._h.label == 'experiment', 'LOGIC ERROR ... should have an experiment record here: %s' % r._h.label |
---|
131 | mips.add(r.mip) |
---|
132 | if byBoth: |
---|
133 | return (mips0,mips) |
---|
134 | else: |
---|
135 | return mips |
---|
136 | else: |
---|
137 | return mips0 |
---|
138 | |
---|
139 | if __name__ == '__main__': |
---|
140 | try: |
---|
141 | import dreq |
---|
142 | except: |
---|
143 | import dreqPy.dreq as dreq |
---|
144 | dq = dreq.loadDreq() |
---|
145 | c = checkVar(dq) |
---|
146 | c.chk( 'tas' ) |
---|
147 | print ( '%s, %s' % (c.inc, c.missing)) |
---|