1 | """Date Request Scoping module |
---|
2 | --------------------------- |
---|
3 | The scope.py module contains the dreqQuery class and a set of ancilliary functions. The dreqQuery class contains methods for analysing the data request. |
---|
4 | """ |
---|
5 | try: |
---|
6 | import dreq |
---|
7 | from utilities import cmvFilter |
---|
8 | import makeTables |
---|
9 | except: |
---|
10 | import dreqPy.dreq as dreq |
---|
11 | from dreqPy.utilities import cmvFilter |
---|
12 | import dreqPy.makeTables as makeTables |
---|
13 | |
---|
14 | import collections, string, operator |
---|
15 | import sys, os |
---|
16 | |
---|
17 | python2 = True |
---|
18 | if sys.version_info[0] == 3: |
---|
19 | python2 = False |
---|
20 | from functools import reduce |
---|
21 | try: |
---|
22 | from utilP3 import mlog3 |
---|
23 | except: |
---|
24 | from dreqPy.utilP3 import mlog3 |
---|
25 | mlg = mlog3() |
---|
26 | else: |
---|
27 | from utilP2 import mlog |
---|
28 | mlg = mlog() |
---|
29 | |
---|
30 | class c1(object): |
---|
31 | def __init__(self): |
---|
32 | self.a = collections.defaultdict( int ) |
---|
33 | class c1s(object): |
---|
34 | def __init__(self): |
---|
35 | self.a = collections.defaultdict( set ) |
---|
36 | |
---|
37 | NT_txtopts = collections.namedtuple( 'txtopts', ['mode'] ) |
---|
38 | |
---|
39 | class baseException(Exception): |
---|
40 | """Basic exception for general use in code.""" |
---|
41 | |
---|
42 | def __init__(self,msg): |
---|
43 | self.msg = 'scope:: %s' % msg |
---|
44 | |
---|
45 | def __str__(self): |
---|
46 | return repr( self.msg ) |
---|
47 | |
---|
48 | def __repr__(self): |
---|
49 | return self.msg |
---|
50 | |
---|
51 | nt_mcfg = collections.namedtuple( 'mcfg', ['nho','nlo','nha','nla','nlas','nls','nh1'] ) |
---|
52 | class cmpd(object): |
---|
53 | def __init__(self,dct): |
---|
54 | self.d = dct |
---|
55 | def cmp(self,x,y,): |
---|
56 | return cmp( self.d[x], self.d[y] ) |
---|
57 | |
---|
58 | |
---|
59 | def filter1( a, b ): |
---|
60 | if b < 0: |
---|
61 | return a |
---|
62 | else: |
---|
63 | return min( [a,b] ) |
---|
64 | |
---|
65 | def filter2( a, b, tt, tm ): |
---|
66 | ## largest tier less than or equal to tm |
---|
67 | t1 = [t for t in tt if t <= tm][-1] |
---|
68 | it1 = tt.index(t1) |
---|
69 | aa = a[it1] |
---|
70 | if b < 0: |
---|
71 | return aa |
---|
72 | else: |
---|
73 | return min( [aa,b] ) |
---|
74 | |
---|
75 | npy = {'1hrClimMon':24*12, 'daily':365, u'Annual':1, u'fx':0.01, u'1hr':24*365, u'3hr':8*365, |
---|
76 | u'monClim':12, u'Timestep':100, u'6hr':4*365, u'day':365, u'1day':365, u'mon':12, u'yr':1, |
---|
77 | u'1mon':12, 'month':12, 'year':1, 'monthly':12, 'hr':24*365, 'other':24*365, |
---|
78 | 'subhr':24*365, 'Day':365, '6h':4*365, '3 hourly':8*365, '':1 } |
---|
79 | |
---|
80 | ## There are 4 cmor variables with blank frequency .... |
---|
81 | |
---|
82 | def vol01( sz, v, npy, freq, inx ): |
---|
83 | n1 = npy[freq] |
---|
84 | s = sz[inx.uid[v].stid] |
---|
85 | assert type(s) == type(1), 'Non-integer size found for %s' % v |
---|
86 | assert type(n1) in (type(1),type(0.)), 'Non-number "npy" found for %s, [%s]' % (v,freq) |
---|
87 | return s*n1 |
---|
88 | |
---|
89 | class col_list(object): |
---|
90 | def __init__(self): |
---|
91 | self.a = collections.defaultdict(list) |
---|
92 | |
---|
93 | class col_count(object): |
---|
94 | def __init__(self): |
---|
95 | self.a = collections.defaultdict(int) |
---|
96 | |
---|
97 | class dreqQuery(object): |
---|
98 | __doc__ = """Methods to analyse the data request, including data volume estimates""" |
---|
99 | def __init__(self,dq=None,tierMax=1): |
---|
100 | if dq == None: |
---|
101 | self.dq = dreq.loadDreq() |
---|
102 | else: |
---|
103 | self.dq=dq |
---|
104 | self.rlu = {} |
---|
105 | for i in self.dq.coll['objective'].items: |
---|
106 | k = '%s.%s' % (i.mip,i.label) |
---|
107 | assert not k in self.rlu, 'Duplicate label in objectives: %s' % k |
---|
108 | self.rlu[k] = i.uid |
---|
109 | |
---|
110 | self.cmvFilter = cmvFilter( self ) |
---|
111 | self.tierMax = tierMax |
---|
112 | |
---|
113 | self.mips = set( [x.label for x in self.dq.coll['mip'].items ] ) |
---|
114 | self.mips = ['AerChemMIP', 'C4MIP', 'CFMIP', 'DAMIP', 'DCPP', 'FAFMIP', 'GeoMIP', 'GMMIP', 'HighResMIP', 'ISMIP6', 'LS3MIP', 'LUMIP', 'OMIP', 'PMIP', 'RFMIP', 'ScenarioMIP', 'VolMIP', 'CORDEX', 'DynVar', 'SIMIP', 'VIACSAB'] |
---|
115 | self.mipsp = ['DECK','CMIP6',] + self.mips[:-4] |
---|
116 | |
---|
117 | self.experiments = set( [x.uid for x in self.dq.coll['experiment'].items ] ) |
---|
118 | self.exptByLabel = {} |
---|
119 | for x in self.dq.coll['experiment'].items: |
---|
120 | if x.label in self.exptByLabel: |
---|
121 | print ( 'ERROR: experiment label duplicated: %s' % x.label ) |
---|
122 | self.exptByLabel[x.label] = x.uid |
---|
123 | self.mipls = sorted( list( self.mips ) ) |
---|
124 | |
---|
125 | self.default_mcfg = nt_mcfg._make( [259200,60,64800,40,20,5,100] ) |
---|
126 | self.mcfg = self.default_mcfg._asdict() |
---|
127 | self.mcfgNote = None |
---|
128 | self.szcfg() |
---|
129 | self.requestItemExpAll( ) |
---|
130 | |
---|
131 | def setMcfg(self, ll, msg=None ): |
---|
132 | assert len(ll) == 7, 'Model config must be of length 7: %s' % str(ll) |
---|
133 | assert all( [type(x) == type(1) for x in ll] ) |
---|
134 | self.mcfg = nt_mcfg._make( ll )._asdict() |
---|
135 | if msg == None: |
---|
136 | self.mcfgNote = 'User supplied model configuration: %s' % str(ll) |
---|
137 | else: |
---|
138 | self.mcfgNote = msg |
---|
139 | self.szcfg() |
---|
140 | |
---|
141 | def szcfg(self): |
---|
142 | szr = {'100km':64800, '1deg':64800, '2deg':16200 } |
---|
143 | self.szss = {} |
---|
144 | self.sz = {} |
---|
145 | self.szg = collections.defaultdict( dict ) |
---|
146 | self.szgss = collections.defaultdict( dict ) |
---|
147 | for i in self.dq.coll['spatialShape'].items: |
---|
148 | type = 'a' |
---|
149 | if i.levelFlag == False: |
---|
150 | ds = i.dimensions.split( '|' ) |
---|
151 | if ds[-1] in ['site', 'basin']: |
---|
152 | vd = ds[-2] |
---|
153 | else: |
---|
154 | vd = ds[-1] |
---|
155 | |
---|
156 | if vd[:4] == 'olev' or vd == 'rho': |
---|
157 | type = 'o' |
---|
158 | nz = self.mcfg['nlo'] |
---|
159 | elif vd[:4] == 'alev': |
---|
160 | nz = self.mcfg['nla'] |
---|
161 | elif vd in ['slevel','sdepth']: |
---|
162 | nz = self.mcfg['nls'] |
---|
163 | elif vd == 'aslevel': |
---|
164 | nz = self.mcfg['nlas'] |
---|
165 | else: |
---|
166 | mlg.prnt( 'Failed to parse dimensions %s' % i.dimensions ) |
---|
167 | raise |
---|
168 | else: |
---|
169 | nz = i.levels |
---|
170 | |
---|
171 | dims = set( i.dimensions.split( '|' ) ) |
---|
172 | if 'latitude' in dims and 'longitude' in dims: |
---|
173 | if type == 'o': |
---|
174 | nh = self.mcfg['nho'] |
---|
175 | else: |
---|
176 | nh = self.mcfg['nha'] |
---|
177 | else: |
---|
178 | nh = 10 |
---|
179 | |
---|
180 | self.szss[i.uid] = nh*nz |
---|
181 | for k in szr: |
---|
182 | self.szgss[k][i.uid] = szr[k]*nz |
---|
183 | for i in self.dq.coll['structure'].items: |
---|
184 | s = 1 |
---|
185 | if i.odims != '': |
---|
186 | s = s*5 |
---|
187 | self.sz[i.uid] = self.szss[i.spid]*s |
---|
188 | for k in szr: |
---|
189 | self.szg[k][i.uid] = self.szgss[k][i.spid]*s |
---|
190 | |
---|
191 | def getRequestLinkByMip( self, mipSel ): |
---|
192 | """Return the set of request links which are associated with specified MIP""" |
---|
193 | |
---|
194 | if type(mipSel) == type( {} ): |
---|
195 | return self.getRequestLinkByMipObjective(self,mipSel) |
---|
196 | |
---|
197 | if type(mipSel) == type(''): |
---|
198 | t1 = lambda x: x == mipSel |
---|
199 | elif type(mipSel) == type(set()): |
---|
200 | t1 = lambda x: x in mipSel |
---|
201 | |
---|
202 | s = set() |
---|
203 | for i in self.dq.coll['objectiveLink'].items: |
---|
204 | if t1(i.label): |
---|
205 | s.add( self.dq.inx.uid[i.rid] ) |
---|
206 | |
---|
207 | ##self.rqs = list({self.dq.inx.uid[i.rid] for i in self.dq.coll['objectiveLink'].items if t1(i.label) }) |
---|
208 | self.rqs = list( s ) |
---|
209 | return self.rqs |
---|
210 | |
---|
211 | def getRequestLinkByMipObjective( self, mipSel ): |
---|
212 | """Return the set of request links which are associated with specified MIP and its objectives""" |
---|
213 | |
---|
214 | assert type(mipSel) == type( {} ),'Argument must be a dictionary, listing objectives for each MIP' |
---|
215 | |
---|
216 | s = set() |
---|
217 | for i in self.dq.coll['objectiveLink'].items: |
---|
218 | if i.label in mipSel: |
---|
219 | if len(mipSel[i]) == 0 or self.dq.inx.uid[i.oid].label in mipSel[i]: |
---|
220 | s.add( self.dq.inx.uid[i.rid] ) |
---|
221 | ##self.rqs = list({self.dq.inx.uid[i.rid] for i in self.dq.coll['objectiveLink'].items if t1(i.label) }) |
---|
222 | self.rqs = list( s ) |
---|
223 | return self.rqs |
---|
224 | |
---|
225 | def getRequestLinkByObjective( self, objSel ): |
---|
226 | """Return the set of request links which are associated with specified objectives""" |
---|
227 | if type(objSel) == type(''): |
---|
228 | t1 = lambda x: x == self.rlu[objSel] |
---|
229 | elif type(objSel) == type(set()): |
---|
230 | t1 = lambda x: x in [self.rlu[i] for i in objSel] |
---|
231 | |
---|
232 | s = set() |
---|
233 | for i in self.dq.coll['objectiveLink'].items: |
---|
234 | if t1(i.label): |
---|
235 | s.add( self.dq.inx.uid[i.oid] ) |
---|
236 | ## |
---|
237 | self.rqs = list( s ) |
---|
238 | ##self.rqs = list({self.dq.inx.uid[i.rid] for i in self.dq.coll['objectiveLink'].items if t1(i.oid) }) |
---|
239 | return self.rqs |
---|
240 | |
---|
241 | def varGroupXexpt(self, rqList ): |
---|
242 | """For a list of request links, return a list of variable group IDs for each experiment""" |
---|
243 | self.cc = collections.defaultdict( list ) |
---|
244 | ## dummy = {self.cc[i.expt].append(i.rlid) for i in self.dq.coll['requestItem'].items if i.rlid in {j.uid for j in rqList} } |
---|
245 | return self.cc |
---|
246 | |
---|
247 | def yearsInRequest(self, rql ): |
---|
248 | self.ntot = sum( [i.ny for i in self.dq.coll['requestItem'].items if i.rlid == rql.uid] ) |
---|
249 | return self.ntot |
---|
250 | |
---|
251 | def rqlByExpt( self, l1, ex, pmax=2, expFullEx=False ): |
---|
252 | """rqlByExpt: return a set of request links for an experiment""" |
---|
253 | ## |
---|
254 | inx = self.dq.inx |
---|
255 | |
---|
256 | if ex != None: |
---|
257 | |
---|
258 | exi = self.dq.inx.uid[ex] |
---|
259 | if exi._h.label == 'experiment': |
---|
260 | exset = set( [ex,exi.egid,exi.mip] ) |
---|
261 | else: |
---|
262 | exset = set( self.esid_to_exptList(ex,deref=False,full=expFullEx) ) |
---|
263 | ## |
---|
264 | ## rql is the set of all request links which are associated with a request item for this experiment set |
---|
265 | ## |
---|
266 | |
---|
267 | l1p = set() |
---|
268 | for i in l1: |
---|
269 | if i.preset < 0 or i.preset <= pmax: |
---|
270 | if i.esid in exset: |
---|
271 | l1p.add(i) |
---|
272 | else: |
---|
273 | exset = None |
---|
274 | l1p = l1 |
---|
275 | |
---|
276 | rql0 = set() |
---|
277 | for i in l1p: |
---|
278 | rql0.add(i.rlid) |
---|
279 | |
---|
280 | rqlInv = set() |
---|
281 | for u in rql0: |
---|
282 | if inx.uid[u]._h.label == 'remarks': |
---|
283 | rqlInv.add( u ) |
---|
284 | if len(rqlInv) != 0: |
---|
285 | mlg.prnt ( 'WARNING.001.00002: %s invalid request links from request items ...' % len(rqlInv) ) |
---|
286 | rql = set() |
---|
287 | for u in rql0: |
---|
288 | if inx.uid[u]._h.label != 'remarks': |
---|
289 | rql.add( u ) |
---|
290 | |
---|
291 | return rql, l1p, exset |
---|
292 | |
---|
293 | def varsByRql( self, rql, pmax=2, intersection=False, asDict=False): |
---|
294 | """The complete set of variables associated with a set of request links.""" |
---|
295 | inx = self.dq.inx |
---|
296 | cc1 = collections.defaultdict( set ) |
---|
297 | for i in rql: |
---|
298 | o = inx.uid[i] |
---|
299 | if o.opt == 'priority': |
---|
300 | p = int( float( o.opar ) ) |
---|
301 | assert p in [1,2,3], 'Priority incorrectly set .. %s, %s, %s' % (o.label,o.title, o.uid) |
---|
302 | cc1[inx.uid[i].mip].add( (inx.uid[i].refid,p) ) |
---|
303 | else: |
---|
304 | cc1[inx.uid[i].mip].add( inx.uid[i].refid ) |
---|
305 | |
---|
306 | if intersection: |
---|
307 | ccv = {} |
---|
308 | # |
---|
309 | # set of request variables for each MIP |
---|
310 | # |
---|
311 | for k in cc1: |
---|
312 | thisc = reduce( operator.or_, [set( inx.iref_by_sect[vg].a['requestVar'] ) for vg in cc1[k] ] ) |
---|
313 | rqvgs = collections.defaultdict( set ) |
---|
314 | for x in cc1[k]: |
---|
315 | if type(x) == type( () ): |
---|
316 | rqvgs[x[0]].add( x[1] ) |
---|
317 | else: |
---|
318 | rqvgs[x].add( 3 ) |
---|
319 | |
---|
320 | s = set() |
---|
321 | for vg in rqvgs: |
---|
322 | for l in inx.iref_by_sect[vg].a['requestVar']: |
---|
323 | if inx.uid[l].priority <= min(pmax,max(rqvgs[vg])): |
---|
324 | s.add( inx.uid[l].vid ) |
---|
325 | ccv[k] = s |
---|
326 | |
---|
327 | if len( ccv.keys() ) < len( list(imips) ): |
---|
328 | vars = set() |
---|
329 | else: |
---|
330 | vars = reduce( operator.and_, [ccv[k] for k in ccv] ) |
---|
331 | else: |
---|
332 | rqvgs = collections.defaultdict( set ) |
---|
333 | for k in cc1: |
---|
334 | for x in cc1[k]: |
---|
335 | if type(x) == type( () ): |
---|
336 | rqvgs[x[0]].add( x[1] ) |
---|
337 | else: |
---|
338 | rqvgs[x].add( 3 ) |
---|
339 | |
---|
340 | ###To obtain a set of variables associated with this collection of variable groups: |
---|
341 | |
---|
342 | if asDict: |
---|
343 | vars = collections.defaultdict( list ) |
---|
344 | else: |
---|
345 | vars = set() |
---|
346 | for vg in rqvgs: |
---|
347 | for l in inx.iref_by_sect[vg].a['requestVar']: |
---|
348 | if inx.uid[l].priority <= min(pmax,max(rqvgs[vg])): |
---|
349 | if asDict: |
---|
350 | vars[inx.uid[l].vid].append( vg ) |
---|
351 | else: |
---|
352 | vars.add(inx.uid[l].vid) |
---|
353 | |
---|
354 | ##col1 = reduce( operator.or_, [set( inx.iref_by_sect[vg].a['requestVar'] ) for vg in rqvg ] ) |
---|
355 | ### filter out cases where the request does not point to a CMOR variable. |
---|
356 | ##vars = {vid for vid in vars if inx.uid[vid][0] == u'CMORvar'} |
---|
357 | |
---|
358 | if asDict: |
---|
359 | thisvars = {} |
---|
360 | for vid in vars: |
---|
361 | if inx.uid[vid]._h.label == u'CMORvar': |
---|
362 | thisvars[vid] = vars[vid] |
---|
363 | else: |
---|
364 | thisvars = set() |
---|
365 | for vid in vars: |
---|
366 | if inx.uid[vid]._h.label == u'CMORvar': |
---|
367 | thisvars.add(vid) |
---|
368 | |
---|
369 | return thisvars |
---|
370 | |
---|
371 | def volByExpt( self, l1, ex, pmax=1, cc=None, retainRedundantRank=False, intersection=False,expFullEx=False, adsCount=False ): |
---|
372 | """volByExpt: calculates the total data volume associated with an experiment/experiment group and a list of request items. |
---|
373 | The calculation has some approximations concerning the number of years in each experiment group. |
---|
374 | cc: an optional collector, to accumulate indexed volumes. """ |
---|
375 | ## |
---|
376 | inx = self.dq.inx |
---|
377 | imips = set() |
---|
378 | for i in l1: |
---|
379 | imips.add(i.mip) |
---|
380 | |
---|
381 | rql, l1p, exset = self.rqlByExpt( l1, ex, pmax=pmax, expFullEx=expFullEx ) |
---|
382 | verbose = False |
---|
383 | if verbose: |
---|
384 | for i in rql: |
---|
385 | r = inx.uid[i] |
---|
386 | print ( '%s, %s, %s' % (r.label, r.title, r.uid) ) |
---|
387 | |
---|
388 | dn = False |
---|
389 | if dn: |
---|
390 | ## obsolete code deleted here |
---|
391 | pass |
---|
392 | elif ex != None: |
---|
393 | |
---|
394 | exi = self.dq.inx.uid[ex] |
---|
395 | if exi._h.label == 'experiment': |
---|
396 | exset = set( [ex,exi.egid,exi.mip] ) |
---|
397 | ##### |
---|
398 | if len( rql ) == 0: |
---|
399 | self.vars = set() |
---|
400 | return (0,{},{} ) |
---|
401 | |
---|
402 | ## The complete set of variables associated with these requests: |
---|
403 | vars = self.varsByRql( rql, pmax=pmax, intersection=intersection, asDict=True) |
---|
404 | tm = 3 |
---|
405 | if tm == 0: |
---|
406 | pass |
---|
407 | elif tm == 1: |
---|
408 | pass |
---|
409 | ## |
---|
410 | ## filter by configuration option and rank |
---|
411 | ## |
---|
412 | if not retainRedundantRank: |
---|
413 | len1 = len(vars.keys()) |
---|
414 | cmv = self.cmvFilter.filterByChoiceRank(cmv=vars.keys()) |
---|
415 | |
---|
416 | vars = cmv |
---|
417 | |
---|
418 | self.vars = vars |
---|
419 | |
---|
420 | e = {} |
---|
421 | for u in rql: |
---|
422 | ### for request variables which reference the variable group attached to the link, add the associate CMOR variables, subject to priority |
---|
423 | i = inx.uid[u] |
---|
424 | e[i.uid] = set() |
---|
425 | si = collections.defaultdict( list ) |
---|
426 | for x in inx.iref_by_sect[i.refid].a['requestVar']: |
---|
427 | if inx.uid[x].priority <= pmax: |
---|
428 | e[i.uid].add( inx.uid[x].vid ) |
---|
429 | |
---|
430 | if verbose: |
---|
431 | cmv = inx.uid[inx.uid[x].vid] |
---|
432 | if cmv._h.label == 'CMORvar': |
---|
433 | si[ cmv.mipTable ].append( inx.uid[x].label ) |
---|
434 | # |
---|
435 | # for each variable, calculate the maximum number of years across all the request links which reference that variable. |
---|
436 | ## |
---|
437 | ## for each request item we have nymax, nenmax, nexmax. |
---|
438 | ## |
---|
439 | nymg = collections.defaultdict( dict ) |
---|
440 | ## |
---|
441 | ## if dataset count rather than volume is wanted, use item 3 from rqiExp tuple. |
---|
442 | if adsCount: |
---|
443 | irqi = 3 |
---|
444 | else: |
---|
445 | irqi = 2 |
---|
446 | |
---|
447 | sgg = set() |
---|
448 | for v in vars: |
---|
449 | s = set() |
---|
450 | sg = collections.defaultdict( set ) |
---|
451 | cc2 = collections.defaultdict( set ) |
---|
452 | cc2s = collections.defaultdict( c1s ) |
---|
453 | for i in l1p: |
---|
454 | ################## |
---|
455 | if (exset == None or i.esid in exset) and v in e[i.rlid]: |
---|
456 | ix = inx.uid[i.esid] |
---|
457 | rl = inx.uid[i.rlid] |
---|
458 | sgg.add( rl.grid ) |
---|
459 | if rl.grid in ['100km','1deg','2deg']: |
---|
460 | grd = rl.grid |
---|
461 | else: |
---|
462 | grd = 'native' |
---|
463 | |
---|
464 | this = None |
---|
465 | if exset == None: |
---|
466 | thisz = 100 |
---|
467 | ## |
---|
468 | ## for a single experiment, look up n years, and n ensemble. |
---|
469 | ## should have nstart???? |
---|
470 | ## |
---|
471 | elif exi._h.label == 'experiment' or ix._h.label == 'experiment': |
---|
472 | this = None |
---|
473 | if ex in self.rqiExp[i.uid][1]: |
---|
474 | this = self.rqiExp[i.uid][1][ex] |
---|
475 | elif ix.uid in self.rqiExp[i.uid][1]: |
---|
476 | this = self.rqiExp[i.uid][1][ix.uid] |
---|
477 | if this != None: |
---|
478 | thisns = this[-3] |
---|
479 | thisny = this[-2] |
---|
480 | thisne = this[-1] |
---|
481 | cc2s[grd].a[u].add( filter1( thisns*thisny*thisne, i.nymax) ) |
---|
482 | else: |
---|
483 | thisz = None |
---|
484 | if 'experiment' in inx.iref_by_sect[i.esid].a: |
---|
485 | for u in inx.iref_by_sect[i.esid].a['experiment']: |
---|
486 | if u in self.rqiExp[i.uid][1]: |
---|
487 | this = self.rqiExp[i.uid][1][u] |
---|
488 | thisns = this[-3] |
---|
489 | thisny = this[-2] |
---|
490 | thisne = this[-1] |
---|
491 | cc2s[grd].a[u].add( filter1( thisns*thisny*thisne, i.nymax) ) |
---|
492 | |
---|
493 | ##if thisny != None and thisne != None: |
---|
494 | ##cc2s[grd].a[i.esid].add( thisny*thisne ) |
---|
495 | |
---|
496 | if exset != None: |
---|
497 | sg[grd].add( self.rqiExp[i.uid][irqi] ) |
---|
498 | |
---|
499 | ##if len(s) == 0: |
---|
500 | ##nym[v] = 0 |
---|
501 | ##else: |
---|
502 | ### |
---|
503 | ### sum over experiments of maximum within each experiment |
---|
504 | ### |
---|
505 | ##nym[v] = sum( [max( cc2[k] ) for k in cc2] ) |
---|
506 | for g in sg: |
---|
507 | nymg[v][g] = sum( [max( cc2s[g].a[k] ) for k in cc2s[g].a] ) |
---|
508 | |
---|
509 | szv = {} |
---|
510 | ov = [] |
---|
511 | for v in vars: |
---|
512 | if 'requestVar' not in inx.iref_by_sect[v].a: |
---|
513 | print ( 'Variable with no request ....: %s, %s' % (inx.uid[v].label, inx.uid[v].mipTable) ) |
---|
514 | szv[v] = self.sz[inx.uid[v].stid]*npy[inx.uid[v].frequency] |
---|
515 | ov.append( self.dq.inx.uid[v] ) |
---|
516 | ee = self.listIndexDual( ov, 'mipTable', 'label', acount=None, alist=None, cdict=szv, cc=cc ) |
---|
517 | |
---|
518 | ff = {} |
---|
519 | for v in vars: |
---|
520 | if adsCount: |
---|
521 | ff[v] = 1 |
---|
522 | else: |
---|
523 | if 'native' in nymg[v]: |
---|
524 | ff[v] = self.sz[ inx.uid[v].stid ] * npy[inx.uid[v].frequency] |
---|
525 | ny = nymg[v]['native'] |
---|
526 | else: |
---|
527 | if len( nymg[v] ) > 1: |
---|
528 | print ( '########### Selecting first in list .............' ) |
---|
529 | ks0 = nymg[v].keys() |
---|
530 | if len(ks0) == 0: |
---|
531 | ##print 'WARN: no nymg entry for %s [%s]' % (v,ex) |
---|
532 | ff[v] = 0. |
---|
533 | ny = 0. |
---|
534 | else: |
---|
535 | ks = list( nymg[v].keys() )[0] |
---|
536 | ny = nymg[v][ks] |
---|
537 | ff[v] = self.szg[ks][ inx.uid[v].stid ] * npy[inx.uid[v].frequency] |
---|
538 | |
---|
539 | if inx.uid[v].frequency != 'monClim': |
---|
540 | ff[v] = ff[v]*ny |
---|
541 | self.ngptot = sum( [ ff[v] for v in vars] ) |
---|
542 | return (self.ngptot, ee, ff ) |
---|
543 | |
---|
544 | def esid_to_exptList(self,esid,deref=False,full=False): |
---|
545 | if not esid in self.dq.inx.uid: |
---|
546 | mlg.prnt ( 'Attempt to dereferece invalid uid: %s' % esid ) |
---|
547 | raise |
---|
548 | |
---|
549 | if self.dq.inx.uid[esid]._h.label == 'experiment': |
---|
550 | expts = [esid,] |
---|
551 | elif self.dq.inx.uid[esid]._h.label != 'remarks': |
---|
552 | if esid in self.dq.inx.iref_by_sect and 'experiment' in self.dq.inx.iref_by_sect[esid].a: |
---|
553 | expts = list( self.dq.inx.iref_by_sect[esid].a['experiment'][:] ) |
---|
554 | else: |
---|
555 | expts = [] |
---|
556 | |
---|
557 | ## add in groups and mips for completeness |
---|
558 | ## |
---|
559 | if full: |
---|
560 | if self.dq.inx.uid[esid]._h.label == 'mip': |
---|
561 | s = set() |
---|
562 | for e in expts: |
---|
563 | if self.dq.inx.uid[e]._h.label != 'experiment': |
---|
564 | mlg.prnt ( 'ERROR: %s, %s, %s ' % (esid,e, self.dq.inx.uid[e].title ) ) |
---|
565 | s.add( self.dq.inx.uid[e].egid ) |
---|
566 | for i in s: |
---|
567 | expts.append( i ) |
---|
568 | expts.append( esid ) |
---|
569 | else: |
---|
570 | ##print ( 'WARNING: request link not associated with valid experiment group' ) |
---|
571 | ##raise |
---|
572 | return None |
---|
573 | |
---|
574 | if self.tierMax > 0: |
---|
575 | expts1 = [] |
---|
576 | for i in expts: |
---|
577 | if self.dq.inx.uid[i]._h.label == 'experiment': |
---|
578 | if self.dq.inx.uid[i].tier[0] <= self.tierMax: |
---|
579 | expts1.append( i ) |
---|
580 | elif self.dq.inx.uid[i]._h.label == 'exptgroup': |
---|
581 | if self.dq.inx.uid[i].tierMin <= self.tierMax: |
---|
582 | expts1.append( i ) |
---|
583 | else: |
---|
584 | expts1.append( i ) |
---|
585 | else: |
---|
586 | expts1 = expts |
---|
587 | |
---|
588 | if deref: |
---|
589 | return [self.dq.inx.uid[e] for e in expts1] |
---|
590 | else: |
---|
591 | return expts1 |
---|
592 | |
---|
593 | ## |
---|
594 | ## need to call this on load |
---|
595 | ## then use instead of i.ny etc below |
---|
596 | ## |
---|
597 | def requestItemExpAll( self ): |
---|
598 | self.rqiExp = {} |
---|
599 | for rqi in self.dq.coll['requestItem'].items: |
---|
600 | a,b,c,d = self.requestItemExp( rqi ) |
---|
601 | if a != None: |
---|
602 | self.rqiExp[rqi.uid] = (a,b,c,d) |
---|
603 | |
---|
604 | def requestItemExp( self, rqi ): |
---|
605 | assert rqi._h.label == "requestItem", 'Argument to requestItemExp must be a requestItem' |
---|
606 | u = rqi.esid |
---|
607 | if self.dq.inx.uid[u]._h.label == 'experiment': |
---|
608 | expts = [u,] |
---|
609 | elif self.dq.inx.uid[u]._h.label != 'remarks': |
---|
610 | if u in self.dq.inx.iref_by_sect and 'experiment' in self.dq.inx.iref_by_sect[u].a: |
---|
611 | expts = self.dq.inx.iref_by_sect[u].a['experiment'] |
---|
612 | else: |
---|
613 | expts = [] |
---|
614 | else: |
---|
615 | # print ( 'WARNING: request link not associated with valid experiment group' ) |
---|
616 | ##rqi.__info__() |
---|
617 | ##raise |
---|
618 | return (None, None, None, None) |
---|
619 | |
---|
620 | if self.tierMax > 0: |
---|
621 | expts = [i for i in expts if self.dq.inx.uid[i].tier[0] <= self.tierMax] |
---|
622 | |
---|
623 | self.multiTierOnly = False |
---|
624 | if self.multiTierOnly: |
---|
625 | expts = [i for i in expts if len(self.dq.inx.uid[i].tier) > 1] |
---|
626 | print ('Len expts: %s' % len(expts) ) |
---|
627 | |
---|
628 | if len(expts) > 0: |
---|
629 | e = [self.dq.inx.uid[i] for i in expts] |
---|
630 | for i in e: |
---|
631 | if i._h.label != 'experiment': |
---|
632 | mlg.prnt ( 'ERROR: %s, %s, %s ' % ( u,i._h.label, i.label, i.title ) ) |
---|
633 | ##dat = [ (i.ntot, i.yps, i.ensz, i.tier, i.nstart, filter1(i.yps,rqi.nymax), filter2(i.ensz,rqi.nenmax,i.tier,self.tierMax) ) for i in e] |
---|
634 | dat2 = {} |
---|
635 | for i in e: |
---|
636 | dat2[i.uid] = (i.ntot, i.yps, i.ensz, i.tier, i.nstart, filter1(i.yps,rqi.nymax), filter2(i.ensz,rqi.nenmax,i.tier,self.tierMax) ) |
---|
637 | ##print i.label, rqi.title, dat2[i.uid] |
---|
638 | ### number of |
---|
639 | nytot = sum( [dat2[x][-2]*dat2[x][-3] for x in dat2 ] ) |
---|
640 | netot = sum( [dat2[x][-1] for x in dat2 ] ) |
---|
641 | ##print 'debug1:: ',dat, nytot, netot |
---|
642 | else: |
---|
643 | dat2 = {} |
---|
644 | nytot = 0 |
---|
645 | netot = 0 |
---|
646 | |
---|
647 | return (expts, dat2, nytot, netot ) |
---|
648 | |
---|
649 | def setTierMax( self, tierMax ): |
---|
650 | """Set the maxium tier and recompute request sizes""" |
---|
651 | if tierMax != self.tierMax: |
---|
652 | self.tierMax = tierMax |
---|
653 | self.requestItemExpAll( ) |
---|
654 | |
---|
655 | def summaryByMip( self, pmax=1 ): |
---|
656 | bytesPerFloat = 2. |
---|
657 | for m in self.mipls: |
---|
658 | v = self.volByMip( m, pmax=pmax ) |
---|
659 | mlg.prnt ( '%12.12s: %6.2fTb' % (m,v*bytesPerFloat*1.e-12) ) |
---|
660 | |
---|
661 | def rqiByMip( self, mip): |
---|
662 | |
---|
663 | if mip == 'TOTAL': |
---|
664 | mip = self.mips |
---|
665 | if type(mip) in [type( '' ),type( u'') ]: |
---|
666 | if mip not in self.mips: |
---|
667 | mlg.prnt ( self.mips ) |
---|
668 | raise baseException( 'rqiByMip: Name of mip not recognised: %s' % mip ) |
---|
669 | l1 = [i for i in self.dq.coll['requestItem'].items if i.mip == mip] |
---|
670 | elif type(mip) in [ type( set()), type( [] ) ]: |
---|
671 | nf = [ m for m in mip if m not in self.mips] |
---|
672 | if len(nf) > 0: |
---|
673 | raise baseException( 'rqiByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
674 | l1 = [i for i in self.dq.coll['requestItem'].items if i.mip in mip] |
---|
675 | elif type(mip) == type( dict()): |
---|
676 | nf = [ m for m in mip if m not in self.mips] |
---|
677 | if len(nf) > 0: |
---|
678 | raise baseException( 'rqiByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
679 | l1 = [] |
---|
680 | for i in self.dq.coll['requestLink'].items: |
---|
681 | if i.mip in mip: |
---|
682 | ok = False |
---|
683 | if len( mip[i.mip] ) == 0: |
---|
684 | ok = True |
---|
685 | else: |
---|
686 | for ol in self.dq.inx.iref_by_sect[i.uid].a['objectiveLink']: |
---|
687 | o = self.dq.inx.uid[ol] |
---|
688 | if self.dq.inx.uid[o.oid].label in mip[i.mip]: |
---|
689 | ok = True |
---|
690 | if ok: |
---|
691 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
692 | for u in self.dq.inx.iref_by_sect[i.uid].a['requestItem']: |
---|
693 | l1.append( self.dq.inx.uid[u] ) |
---|
694 | else: |
---|
695 | raise baseException( 'rqiByMip: "mip" (1st explicit argument) should be type string or set: %s -- %s' % (mip, type(mip)) ) |
---|
696 | |
---|
697 | return l1 |
---|
698 | |
---|
699 | def checkDir(self,odir,msg): |
---|
700 | if not os.path.isdir( odir ): |
---|
701 | try: |
---|
702 | os.mkdir( odir ) |
---|
703 | except: |
---|
704 | print ('\n\nFailed to make directory "%s" for: %s: make necessary subdirectories or run where you have write access' % (odir,msg) ) |
---|
705 | print ( '\n\n' ) |
---|
706 | raise |
---|
707 | print ('Created directory %s for: %s' % (odir,msg) ) |
---|
708 | |
---|
709 | |
---|
710 | def xlsByMipExpt(self,m,ex,pmax,odir='xls',xls=True,txt=False,txtOpts=None): |
---|
711 | import scope_utils |
---|
712 | mxls = scope_utils.xlsTabs(self,tiermax=self.tierMax,pmax=pmax,xls=xls, txt=txt, txtOpts=txtOpts,odir=odir) |
---|
713 | |
---|
714 | mlab = makeTables.setMlab( m ) |
---|
715 | |
---|
716 | mxls.run( m, mlab=mlab ) |
---|
717 | |
---|
718 | |
---|
719 | def volByMip( self, mip, pmax=2, retainRedundantRank=False, intersection=False, adsCount=False, exptid=None): |
---|
720 | |
---|
721 | l1 = self.rqiByMip( mip ) |
---|
722 | |
---|
723 | #### The set of experiments/experiment groups: |
---|
724 | if exptid == None: |
---|
725 | ##exps = self.mips |
---|
726 | exps = self.experiments |
---|
727 | else: |
---|
728 | exps = set( [exptid,] ) |
---|
729 | ##print exptid, exps |
---|
730 | |
---|
731 | self.volByE = {} |
---|
732 | vtot = 0 |
---|
733 | cc = collections.defaultdict( col_count ) |
---|
734 | self.allVars = set() |
---|
735 | for e in exps: |
---|
736 | expts = self.esid_to_exptList(e,deref=True,full=False) |
---|
737 | if expts not in [None,[]]: |
---|
738 | ###print 'EXPTS: ',e,len(expts), list( expts )[0].label |
---|
739 | for ei in expts: |
---|
740 | self.volByE[ei.label] = self.volByExpt( l1, ei.uid, pmax=pmax, cc=cc, retainRedundantRank=retainRedundantRank, intersection=intersection, adsCount=adsCount ) |
---|
741 | vtot += self.volByE[ei.label][0] |
---|
742 | self.allVars = self.allVars.union( self.vars ) |
---|
743 | ##else: |
---|
744 | ##print 'No expts found: ',e |
---|
745 | self.indexedVol = cc |
---|
746 | |
---|
747 | return vtot |
---|
748 | |
---|
749 | def listIndexDual(self, ll, a1, a2, acount=None, alist=None, cdict=None, cc=None ): |
---|
750 | do_count = acount != None |
---|
751 | do_list = alist != None |
---|
752 | assert not (do_count and do_list), 'It is an error to request both list and count' |
---|
753 | if not (do_count or do_list): |
---|
754 | acount = '__number__' |
---|
755 | do_count = True |
---|
756 | |
---|
757 | if cc == None: |
---|
758 | if do_count: |
---|
759 | cc = collections.defaultdict( col_count ) |
---|
760 | elif do_list: |
---|
761 | cc = collections.defaultdict( col_list ) |
---|
762 | |
---|
763 | if do_count: |
---|
764 | for l in ll: |
---|
765 | if cdict != None: |
---|
766 | v = cdict[l.uid] |
---|
767 | elif acount == '__number__': |
---|
768 | v = 1 |
---|
769 | else: |
---|
770 | v = l.__dict__[acount] |
---|
771 | |
---|
772 | cc[ l.__dict__[a1] ].a[ l.__dict__[a2] ] += v |
---|
773 | elif do_list: |
---|
774 | for l in ll: |
---|
775 | if cdict != None: |
---|
776 | v = cdict[l.uid] |
---|
777 | elif alist == '__item__': |
---|
778 | v = l |
---|
779 | else: |
---|
780 | v = l.__dict__[alist] |
---|
781 | cc[ l.__dict__[a1] ].a[ l.__dict__[a2] ].append( v ) |
---|
782 | |
---|
783 | od = {} |
---|
784 | for k in cc.keys(): |
---|
785 | d2 = {} |
---|
786 | for k2 in cc[k].a.keys(): |
---|
787 | d2[k2] = cc[k].a[k2] |
---|
788 | od[k] = d2 |
---|
789 | return od |
---|
790 | |
---|
791 | class dreqUI(object): |
---|
792 | """Data Request Command line. |
---|
793 | ------------------------- |
---|
794 | -v : print version and exit; |
---|
795 | --unitTest : run some simple tests; |
---|
796 | -m <mip>: MIP of list of MIPs (comma separated; for objective selection see note [1] below); |
---|
797 | -l <options>: List for options: |
---|
798 | o: objectives |
---|
799 | e: experiments |
---|
800 | -h : help: print help text; |
---|
801 | -e <expt>: experiment; |
---|
802 | -t <tier> maxmum tier; |
---|
803 | -p <priority> maximum priority; |
---|
804 | --xls : Create Excel file with requested variables; |
---|
805 | --txt : Create text file with requested variables; |
---|
806 | --mcfg : Model configuration: 7 integers, comma separated, 'nho','nlo','nha','nla','nlas','nls','nh1' |
---|
807 | default: 259200,60,64800,40,20,5,100 |
---|
808 | --txtOpts : options for content of text file: (v|c)[(+|-)att1[,att2[...]]] |
---|
809 | --xlsDir <directory> : Directory in which to place variable listing [xls]; |
---|
810 | --printLinesMax <n>: Maximum number of lines to be printed (default 20) |
---|
811 | --printVars : If present, a summary of the variables (see --printLinesMax) fitting the selection options will be printed |
---|
812 | --intersection : Analyse the intersection of requests rather than union. |
---|
813 | |
---|
814 | NOTES |
---|
815 | ----- |
---|
816 | [1] A set of objectives within a MIP can be specified in the command line. The extended syntax of the "-m" argument is: |
---|
817 | -m <mip>[:objective[.obj2[.obj3 ...]]][,<mip2]...] |
---|
818 | |
---|
819 | e.g. |
---|
820 | drq -m HighResMIP:Ocean.DiurnalCycle |
---|
821 | """ |
---|
822 | def __init__(self,args): |
---|
823 | self.adict = {} |
---|
824 | self.knownargs = {'-m':('m',True), '-p':('p',True), '-e':('e',True), '-t':('t',True), \ |
---|
825 | '-h':('h',False), '--printLinesMax':('plm',True), \ |
---|
826 | '-l':('l',True), |
---|
827 | '--printVars':('vars',False), '--intersection':('intersection',False), \ |
---|
828 | '--count':('count',False), \ |
---|
829 | '--txt':('txt',False), \ |
---|
830 | '--mcfg':('mcfg',True), \ |
---|
831 | '--txtOpts':('txtOpts',True), \ |
---|
832 | '--xlsDir':('xlsdir',True), '--xls':('xls',False) \ |
---|
833 | } |
---|
834 | aa = args[:] |
---|
835 | notKnownArgs = [] |
---|
836 | while len(aa) > 0: |
---|
837 | a = aa.pop(0) |
---|
838 | if a in self.knownargs: |
---|
839 | b = self.knownargs[a][0] |
---|
840 | if self.knownargs[a][1]: |
---|
841 | v = aa.pop(0) |
---|
842 | self.adict[b] = v |
---|
843 | else: |
---|
844 | self.adict[b] = True |
---|
845 | else: |
---|
846 | notKnownArgs.append(a) |
---|
847 | |
---|
848 | assert self.checkArgs( notKnownArgs ), 'FATAL ERROR 001: Arguments not recognised: %s' % (str(notKnownArgs) ) |
---|
849 | |
---|
850 | if 'm' in self.adict: |
---|
851 | if self.adict['m'].find( ':' ) != -1: |
---|
852 | ee = {} |
---|
853 | for i in self.adict['m'].split(','): |
---|
854 | bits = i.split( ':' ) |
---|
855 | if len( bits ) == 1: |
---|
856 | ee[bits[0]] = [] |
---|
857 | else: |
---|
858 | assert len(bits) == 2, 'Cannot parse %s' % self.adict['m'] |
---|
859 | ee[bits[0]] = bits[1].split( '.' ) |
---|
860 | self.adict['m'] = ee |
---|
861 | else: |
---|
862 | self.adict['m'] = set(self.adict['m'].split(',') ) |
---|
863 | |
---|
864 | integerArgs = set( ['p','t','plm'] ) |
---|
865 | for i in integerArgs.intersection( self.adict ): |
---|
866 | self.adict[i] = int( self.adict[i] ) |
---|
867 | |
---|
868 | self.intersection = self.adict.get( 'intersection', False ) |
---|
869 | |
---|
870 | |
---|
871 | def checkArgs( self, notKnownArgs ): |
---|
872 | if len( notKnownArgs ) == 0: |
---|
873 | return True |
---|
874 | print ('--------------------------------------') |
---|
875 | print ('------------ %s Arguments Not Recognised ------------' % len(notKnownArgs) ) |
---|
876 | k = 0 |
---|
877 | for x in notKnownArgs: |
---|
878 | k += 1 |
---|
879 | if x[1:] in self.knownargs: |
---|
880 | print ( '%s PERHAPS %s instead of %s' % (k, x[1:],x) ) |
---|
881 | elif '-%s' % x in self.knownargs: |
---|
882 | print ( '%s PERHAPS -%s instead of %s' % (k, x,x) ) |
---|
883 | elif x[0] == '\xe2': |
---|
884 | print ( '%s POSSIBLY -- (double hyphen) instead of long dash in %s' % (k, x) ) |
---|
885 | print ('--------------------------------------') |
---|
886 | |
---|
887 | return len( notKnownArgs ) == 0 |
---|
888 | |
---|
889 | def run(self, dq=None): |
---|
890 | if 'h' in self.adict: |
---|
891 | mlg.prnt ( self.__doc__ ) |
---|
892 | return |
---|
893 | |
---|
894 | if not 'm' in self.adict: |
---|
895 | mlg.prnt ( 'Current version requires -m argument' ) |
---|
896 | mlg.prnt ( self.__doc__ ) |
---|
897 | sys.exit(0) |
---|
898 | |
---|
899 | if dq == None: |
---|
900 | self.dq = dreq.loadDreq() |
---|
901 | else: |
---|
902 | self.dq = dq |
---|
903 | |
---|
904 | if 'l' in self.adict: |
---|
905 | self.printList() |
---|
906 | return |
---|
907 | |
---|
908 | if 'mcfg' in self.adict: |
---|
909 | ll = string.split( self.adict['mcfg'], ',' ) |
---|
910 | assert len(ll) == 7, 'Length of model configuration argument must be 7 comma separated integers: %s' % self.adict['mcfg'] |
---|
911 | lli = [ int(x) for x in ll] |
---|
912 | |
---|
913 | self.sc = dreqQuery( dq=self.dq ) |
---|
914 | if 'mcfg' in self.adict: |
---|
915 | self.sc.setMcfg( lli ) |
---|
916 | |
---|
917 | ok = True |
---|
918 | for i in self.adict['m']: |
---|
919 | if i not in self.sc.mips: |
---|
920 | ok = False |
---|
921 | mlg.prnt ( 'NOT FOUND: %s' % i ) |
---|
922 | |
---|
923 | eid = None |
---|
924 | ex = None |
---|
925 | if 'e' in self.adict: |
---|
926 | ex = self.adict['e'] |
---|
927 | for i in self.dq.coll['experiment'].items: |
---|
928 | if i.label == self.adict['e']: |
---|
929 | eid = i.uid |
---|
930 | assert eid != None, 'Experiment %s not found' % self.adict['e'] |
---|
931 | |
---|
932 | assert ok,'Available MIPs: %s' % str(self.sc.mips) |
---|
933 | adsCount = self.adict.get( 'count', False ) |
---|
934 | |
---|
935 | tierMax = self.adict.get( 't', 1 ) |
---|
936 | self.sc.setTierMax( tierMax ) |
---|
937 | pmax = self.adict.get( 'p', 1 ) |
---|
938 | self.getVolByMip(pmax,eid,adsCount) |
---|
939 | makeXls = self.adict.get( 'xls', False ) |
---|
940 | makeTxt = self.adict.get( 'txt', False ) |
---|
941 | if makeXls or makeTxt: |
---|
942 | mips = self.adict['m'] |
---|
943 | odir = self.adict.get( 'xlsdir', 'xls' ) |
---|
944 | self.sc.checkDir( odir, 'xls files' ) |
---|
945 | |
---|
946 | if 'txtOpts' in self.adict: |
---|
947 | if self.adict['txtOpts'][0] == 'v': |
---|
948 | txtOpts = NT_txtopts( 'var' ) |
---|
949 | else: |
---|
950 | txtOpts = NT_txtopts( 'cmv' ) |
---|
951 | else: |
---|
952 | txtOpts=None |
---|
953 | |
---|
954 | self.sc.xlsByMipExpt(mips,eid,pmax,odir=odir,xls=makeXls,txt=makeTxt,txtOpts=txtOpts) |
---|
955 | |
---|
956 | def printList(self): |
---|
957 | mips = self.adict['m'] |
---|
958 | ee = {} |
---|
959 | for i in self.dq.coll['mip'].items: |
---|
960 | if i.label in mips: |
---|
961 | ee[i.label] = i |
---|
962 | if self.adict['l'] in ['o','e']: |
---|
963 | targ = {'o':'objective', 'e':'experiment' }[self.adict['l']] |
---|
964 | for k in sorted( ee.keys() ): |
---|
965 | if targ in self.dq.inx.iref_by_sect[ee[k].uid].a: |
---|
966 | for u in self.dq.inx.iref_by_sect[ee[k].uid].a[targ]: |
---|
967 | print ( '%s: %s' % (ee[k].label, self.dq.inx.uid[u].label) ) |
---|
968 | else: |
---|
969 | print ('list objective *%s* not recognised (should be e or o)' % self.adict['l'] ) |
---|
970 | |
---|
971 | def getVolByMip(self,pmax,eid,adsCount): |
---|
972 | |
---|
973 | v0 = self.sc.volByMip( self.adict['m'], pmax=pmax, intersection=self.intersection, adsCount=adsCount, exptid=eid ) |
---|
974 | #mlg.prnt ( '%7.2fTb' % (v0*2.*1.e-12) ) |
---|
975 | mlg.prnt ( 'getVolByMip: %s [%s]' % (v0,makeTables.vfmt(v0*2.)) ) |
---|
976 | cc = collections.defaultdict( int ) |
---|
977 | for e in self.sc.volByE: |
---|
978 | for v in self.sc.volByE[e][2]: |
---|
979 | cc[v] += self.sc.volByE[e][2][v] |
---|
980 | x = 0 |
---|
981 | for v in cc: |
---|
982 | x += cc[v] |
---|
983 | |
---|
984 | if python2: |
---|
985 | vl = sorted( cc.keys(), cmp=cmpd(cc).cmp, reverse=True ) |
---|
986 | else: |
---|
987 | vl = sorted( cc.keys(), key=lambda x: cc[x], reverse=True ) |
---|
988 | if self.adict.get( 'vars', False ): |
---|
989 | printLinesMax = self.adict.get( 'plm', 20 ) |
---|
990 | if printLinesMax > 0: |
---|
991 | mx = min( [printLinesMax,len(vl)] ) |
---|
992 | else: |
---|
993 | mx = len(vl) |
---|
994 | |
---|
995 | for v in vl[:mx]: |
---|
996 | mlg.prnt ( '%s: %7.2fTb' % (self.dq.inx.uid[v].label, cc[v]*2.*1.e-12) ) |
---|
997 | if mx < len(vl): |
---|
998 | mlg.prnt ( '%s variables not listed (use --printLinesMax to print more)' % (len(vl)-mx) ) |
---|
999 | |
---|