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