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 | |
---|
6 | try: |
---|
7 | import dreq |
---|
8 | imm=1 |
---|
9 | except: |
---|
10 | import dreqPy.dreq as dreq |
---|
11 | imm=2 |
---|
12 | |
---|
13 | if imm == 1: |
---|
14 | from utilities import cmvFilter |
---|
15 | import makeTables |
---|
16 | import fgrid |
---|
17 | else: |
---|
18 | import dreqPy.fgrid as fgrid |
---|
19 | from dreqPy.utilities import cmvFilter |
---|
20 | import dreqPy.makeTables as makeTables |
---|
21 | |
---|
22 | import collections, string, operator |
---|
23 | import sys, os |
---|
24 | |
---|
25 | odsz = {'landUse':(5,'free'), 'tau':7, 'scatratio':15, 'effectRadLi|tau':(28,'query pending'), 'vegtype':(8,'free'), 'sza5':5, 'site':(119,'73 for aquaplanet .. '), 'iceband':(5,'free'), 'dbze':15, 'spectband':(10,'free'), 'misrBands':(7,'query pending'), 'effectRadIc|tau':(28,'query pending')} |
---|
26 | |
---|
27 | python2 = True |
---|
28 | if sys.version_info[0] == 3: |
---|
29 | python2 = False |
---|
30 | from functools import reduce |
---|
31 | try: |
---|
32 | from utilP3 import mlog3 |
---|
33 | except: |
---|
34 | from dreqPy.utilP3 import mlog3 |
---|
35 | mlg = mlog3() |
---|
36 | else: |
---|
37 | from utilP2 import util |
---|
38 | mlg = util.mlog() |
---|
39 | |
---|
40 | class c1(object): |
---|
41 | def __init__(self): |
---|
42 | self.a = collections.defaultdict( int ) |
---|
43 | class c1s(object): |
---|
44 | def __init__(self): |
---|
45 | self.a = collections.defaultdict( set ) |
---|
46 | |
---|
47 | NT_txtopts = collections.namedtuple( 'txtopts', ['mode'] ) |
---|
48 | |
---|
49 | def vfmt(ss): |
---|
50 | stb = ss*1.e-12 |
---|
51 | if stb < .099: |
---|
52 | return '%7.2fGb' % (stb*100) |
---|
53 | else: |
---|
54 | return '%7.2fTb' % stb |
---|
55 | |
---|
56 | class baseException(Exception): |
---|
57 | """Basic exception for general use in code.""" |
---|
58 | |
---|
59 | def __init__(self,msg): |
---|
60 | self.msg = 'scope:: %s' % msg |
---|
61 | |
---|
62 | def __str__(self): |
---|
63 | return repr( self.msg ) |
---|
64 | |
---|
65 | def __repr__(self): |
---|
66 | return self.msg |
---|
67 | |
---|
68 | nt_mcfg = collections.namedtuple( 'mcfg', ['nho','nlo','nha','nla','nlas','nls','nh1'] ) |
---|
69 | class cmpd(object): |
---|
70 | def __init__(self,dct): |
---|
71 | self.d = dct |
---|
72 | def cmp(self,x,y,): |
---|
73 | return cmp( self.d[x], self.d[y] ) |
---|
74 | |
---|
75 | |
---|
76 | def filter1( a, b ): |
---|
77 | if b < 0: |
---|
78 | return a |
---|
79 | else: |
---|
80 | return min( [a,b] ) |
---|
81 | |
---|
82 | def filter2( a, b, tt, tm ): |
---|
83 | ## largest tier less than or equal to tm |
---|
84 | ll = [t for t in tt if t <= tm] |
---|
85 | if len( ll ) > 0: |
---|
86 | t1 = [t for t in tt if t <= tm][-1] |
---|
87 | it1 = tt.index(t1) |
---|
88 | aa = a[it1] |
---|
89 | if b < 0: |
---|
90 | return aa |
---|
91 | else: |
---|
92 | return min( [aa,b] ) |
---|
93 | else: |
---|
94 | return 0 |
---|
95 | |
---|
96 | npy = {'1hrClimMon':24*12, 'daily':365, u'Annual':1, u'fx':0.01, u'1hr':24*365, u'3hr':8*365, |
---|
97 | u'monClim':12, u'Timestep':100, u'6hr':4*365, u'day':365, u'1day':365, u'mon':12, u'yr':1, |
---|
98 | u'1mon':12, 'month':12, 'year':1, 'monthly':12, 'hr':24*365, 'other':24*365, |
---|
99 | 'subhr':24*365, 'Day':365, '6h':4*365, '3 hourly':8*365, '':1 } |
---|
100 | |
---|
101 | ## There are 4 cmor variables with blank frequency .... |
---|
102 | |
---|
103 | def vol01( sz, v, npy, freq, inx ): |
---|
104 | n1 = npy[freq] |
---|
105 | s = sz[inx.uid[v].stid] |
---|
106 | assert type(s) == type(1), 'Non-integer size found for %s' % v |
---|
107 | assert type(n1) in (type(1),type(0.)), 'Non-number "npy" found for %s, [%s]' % (v,freq) |
---|
108 | return s*n1 |
---|
109 | |
---|
110 | class col_list(object): |
---|
111 | def __init__(self): |
---|
112 | self.a = collections.defaultdict(list) |
---|
113 | |
---|
114 | class col_count(object): |
---|
115 | def __init__(self): |
---|
116 | self.a = collections.defaultdict(int) |
---|
117 | |
---|
118 | class dreqQuery(object): |
---|
119 | __doc__ = """Methods to analyse the data request, including data volume estimates""" |
---|
120 | def __init__(self,dq=None,tierMax=1): |
---|
121 | if dq == None: |
---|
122 | self.dq = dreq.loadDreq() |
---|
123 | else: |
---|
124 | self.dq=dq |
---|
125 | self.rlu = {} |
---|
126 | for i in self.dq.coll['objective'].items: |
---|
127 | k = '%s.%s' % (i.mip,i.label) |
---|
128 | assert not k in self.rlu, 'Duplicate label in objectives: %s' % k |
---|
129 | self.rlu[k] = i.uid |
---|
130 | |
---|
131 | self.odsz = odsz |
---|
132 | self.npy = npy |
---|
133 | self.strSz = dict() |
---|
134 | self.cmvFilter = cmvFilter( self ) |
---|
135 | self.tierMax = tierMax |
---|
136 | self.gridPolicyDefaultNative = False |
---|
137 | self.gridPolicyTopOnly = True |
---|
138 | self.exptFilter = None |
---|
139 | self.uniqueRequest = False |
---|
140 | |
---|
141 | self.mips = set( [x.label for x in self.dq.coll['mip'].items ] ) |
---|
142 | self.mips = ['AerChemMIP', 'C4MIP', 'CFMIP', 'DAMIP', 'DCPP', 'FAFMIP', 'GeoMIP', 'GMMIP', 'HighResMIP', 'ISMIP6', 'LS3MIP', 'LUMIP', 'OMIP', 'PMIP', 'RFMIP', 'ScenarioMIP', 'VolMIP', 'CORDEX', 'DynVar', 'SIMIP', 'VIACSAB'] |
---|
143 | self.mipsp = ['DECK','CMIP6',] + self.mips[:-4] |
---|
144 | self.cmvGridId, i4 = fgrid.fgrid( self.dq ) |
---|
145 | assert len(i4) == 0 |
---|
146 | |
---|
147 | self.experiments = set( [x.uid for x in self.dq.coll['experiment'].items ] ) |
---|
148 | self.exptByLabel = {} |
---|
149 | for x in self.dq.coll['experiment'].items: |
---|
150 | if x.label in self.exptByLabel: |
---|
151 | print ( 'ERROR: experiment label duplicated: %s' % x.label ) |
---|
152 | self.exptByLabel[x.label] = x.uid |
---|
153 | self.mipls = sorted( list( self.mips ) ) |
---|
154 | |
---|
155 | self.default_mcfg = nt_mcfg._make( [259200,60,64800,40,20,5,100] ) |
---|
156 | self.mcfg = self.default_mcfg._asdict() |
---|
157 | self.mcfgNote = None |
---|
158 | self.szcfg() |
---|
159 | self.requestItemExpAll( ) |
---|
160 | |
---|
161 | def setMcfg(self, ll, msg=None ): |
---|
162 | assert len(ll) == 7, 'Model config must be of length 7: %s' % str(ll) |
---|
163 | assert all( [type(x) == type(1) for x in ll] ) |
---|
164 | self.mcfg = nt_mcfg._make( ll )._asdict() |
---|
165 | if msg == None: |
---|
166 | self.mcfgNote = 'User supplied model configuration: %s' % str(ll) |
---|
167 | else: |
---|
168 | self.mcfgNote = msg |
---|
169 | self.szcfg() |
---|
170 | |
---|
171 | def szcfg(self): |
---|
172 | szr = {'100km':64800, '1deg':64800, '2deg':16200 } |
---|
173 | self.szss = {} |
---|
174 | self.sz = {} |
---|
175 | self.szg = collections.defaultdict( dict ) |
---|
176 | self.szgss = collections.defaultdict( dict ) |
---|
177 | self.isLatLon = {} |
---|
178 | self.szSrf = collections.defaultdict( dict ) |
---|
179 | self.szssSrf = collections.defaultdict( dict ) |
---|
180 | for i in self.dq.coll['spatialShape'].items: |
---|
181 | gtype = 'a' |
---|
182 | if i.levelFlag == False: |
---|
183 | ds = i.dimensions.split( '|' ) |
---|
184 | if ds[-1] in ['site', 'basin']: |
---|
185 | vd = ds[-2] |
---|
186 | else: |
---|
187 | vd = ds[-1] |
---|
188 | |
---|
189 | if vd[:4] == 'olev' or vd == 'rho': |
---|
190 | gtype = 'o' |
---|
191 | nz = self.mcfg['nlo'] |
---|
192 | elif vd[:4] == 'alev': |
---|
193 | nz = self.mcfg['nla'] |
---|
194 | elif vd in ['slevel','sdepth']: |
---|
195 | nz = self.mcfg['nls'] |
---|
196 | elif vd == 'aslevel': |
---|
197 | nz = self.mcfg['nlas'] |
---|
198 | else: |
---|
199 | mlg.prnt( 'Failed to parse dimensions %s' % i.dimensions ) |
---|
200 | raise |
---|
201 | else: |
---|
202 | nz = i.levels |
---|
203 | |
---|
204 | dims = set( i.dimensions.split( '|' ) ) |
---|
205 | if 'latitude' in dims and 'longitude' in dims: |
---|
206 | if gtype == 'o': |
---|
207 | nh = self.mcfg['nho'] |
---|
208 | self.isLatLon[i.uid] = 'o' |
---|
209 | else: |
---|
210 | nh = self.mcfg['nha'] |
---|
211 | self.isLatLon[i.uid] = 'a' |
---|
212 | else: |
---|
213 | nh = 10 |
---|
214 | self.isLatLon[i.uid] = False |
---|
215 | |
---|
216 | self.szss[i.uid] = nh*nz |
---|
217 | if self.isLatLon[i.uid] != False and len(dims) == 2: |
---|
218 | self.szssSrf[i.uid] = { 'a':self.mcfg['nha']*nz, 'o':self.mcfg['nho']*nz } |
---|
219 | |
---|
220 | for k in szr: |
---|
221 | if self.isLatLon[i.uid] != False: |
---|
222 | self.szgss[k][i.uid] = szr[k]*nz |
---|
223 | else: |
---|
224 | self.szgss[k][i.uid] = nh*nz |
---|
225 | |
---|
226 | for i in self.dq.coll['structure'].items: |
---|
227 | s = 1 |
---|
228 | knownAtmos = False |
---|
229 | if i.odims != '': |
---|
230 | if i.odims in odsz: |
---|
231 | sf = odsz[i.odims] |
---|
232 | else: |
---|
233 | ## print 'SEVERE.odims.00001: no information on dimension size: %s' % i.odims |
---|
234 | sf = 5 |
---|
235 | if type( sf ) == type( () ): |
---|
236 | sf = sf[0] |
---|
237 | s = s*sf |
---|
238 | if i.odims not in ['iceband']: |
---|
239 | knownAtmos = True |
---|
240 | if i.spid in self.szss: |
---|
241 | self.sz[i.uid] = self.szss[i.spid]*s |
---|
242 | |
---|
243 | if i.uid in self.szssSrf: |
---|
244 | if knownAtmos: |
---|
245 | self.sz[i.uid] = self.szssSrf[i.spid]['a']*s |
---|
246 | else: |
---|
247 | for k in ['a','o']: |
---|
248 | self.szSrf[i.uid][k] = self.szssSrf[i.spid][k]*s |
---|
249 | |
---|
250 | for k in szr: |
---|
251 | self.szg[k][i.uid] = self.szgss[k][i.spid]*s |
---|
252 | else: |
---|
253 | print ('WARNING: spid has no size info: %s' % i.spid ) |
---|
254 | self.sz[i.uid] = 0. |
---|
255 | for k in szr: |
---|
256 | self.szg[k][i.uid] = 0. |
---|
257 | |
---|
258 | def getRequestLinkByMip( self, mipSel ): |
---|
259 | """Return the set of request links which are associated with specified MIP""" |
---|
260 | |
---|
261 | if type(mipSel) == type( {} ): |
---|
262 | return self.getRequestLinkByMipObjective(self,mipSel) |
---|
263 | |
---|
264 | if type(mipSel) == type(''): |
---|
265 | t1 = lambda x: x == mipSel |
---|
266 | elif type(mipSel) == type(set()): |
---|
267 | t1 = lambda x: x in mipSel |
---|
268 | |
---|
269 | s = set() |
---|
270 | for i in self.dq.coll['requestLink'].items: |
---|
271 | if t1(i.mip): |
---|
272 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
273 | if any( [ self.rqiExp[x][-1] > 0 for x in self.dq.inx.iref_by_sect[i.uid].a['requestItem'] if x in self.rqiExp ] ): |
---|
274 | s.add( i ) |
---|
275 | |
---|
276 | self.rqs = list( s ) |
---|
277 | return self.rqs |
---|
278 | |
---|
279 | def getRequestLinkByMipObjective( self, mipSel ): |
---|
280 | """Return the set of request links which are associated with specified MIP and its objectives""" |
---|
281 | |
---|
282 | assert type(mipSel) == type( {} ),'Argument must be a dictionary, listing objectives for each MIP' |
---|
283 | |
---|
284 | s = set() |
---|
285 | for i in self.dq.coll['requestLink'].items: |
---|
286 | if i.mip in mipSel: |
---|
287 | if len(mipSel[i.mip]) == 0: |
---|
288 | s.add( i ) |
---|
289 | elif 'objectiveLink' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
290 | ss = set( [self.dq.inx.uid[k].label for k in self.dq.inx.iref_by_sect[i.uid].a['objectiveLink'] ] ) |
---|
291 | if any( [x in mipSel[i.mip] for x in ss] ): |
---|
292 | s.add( i ) |
---|
293 | ## |
---|
294 | ## filter requestLinks by tierMax: check to see whether they link to experiments with tier below or equal to tiermax. |
---|
295 | ## |
---|
296 | s1 = set() |
---|
297 | for i in s: |
---|
298 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
299 | if any( [ self.rqiExp[x][-1] > 0 for x in self.dq.inx.iref_by_sect[i.uid].a['requestItem'] if x in self.rqiExp ] ): |
---|
300 | s1.add( i ) |
---|
301 | |
---|
302 | self.rqs = list( s1 ) |
---|
303 | return self.rqs |
---|
304 | |
---|
305 | def varGroupXexpt(self, rqList ): |
---|
306 | """For a list of request links, return a list of variable group IDs for each experiment""" |
---|
307 | self.cc = collections.defaultdict( list ) |
---|
308 | ## 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} } |
---|
309 | return self.cc |
---|
310 | |
---|
311 | def yearsInRequest(self, rql ): |
---|
312 | self.ntot = sum( [i.ny for i in self.dq.coll['requestItem'].items if i.rlid == rql.uid] ) |
---|
313 | return self.ntot |
---|
314 | |
---|
315 | def rqlByExpt( self, l1, ex, pmax=2, expFullEx=False ): |
---|
316 | """rqlByExpt: return a set of request links for an experiment""" |
---|
317 | ## |
---|
318 | inx = self.dq.inx |
---|
319 | |
---|
320 | if ex != None: |
---|
321 | |
---|
322 | exi = self.dq.inx.uid[ex] |
---|
323 | if exi._h.label == 'experiment': |
---|
324 | exset = set( [ex,exi.egid,exi.mip] ) |
---|
325 | else: |
---|
326 | exset = set( self.esid_to_exptList(ex,deref=False,full=expFullEx) ) |
---|
327 | ## |
---|
328 | ## rql is the set of all request links which are associated with a request item for this experiment set |
---|
329 | ## |
---|
330 | |
---|
331 | l1p = set() |
---|
332 | for i in l1: |
---|
333 | if i.preset < 0 or i.preset <= pmax: |
---|
334 | if i.esid in exset: |
---|
335 | l1p.add(i) |
---|
336 | else: |
---|
337 | exset = None |
---|
338 | l1p = l1 |
---|
339 | |
---|
340 | rql0 = set() |
---|
341 | for i in l1p: |
---|
342 | rql0.add(i.rlid) |
---|
343 | |
---|
344 | rqlInv = set() |
---|
345 | for u in rql0: |
---|
346 | if inx.uid[u]._h.label == 'remarks': |
---|
347 | rqlInv.add( u ) |
---|
348 | if len(rqlInv) != 0: |
---|
349 | mlg.prnt ( 'WARNING.001.00002: %s invalid request links from request items ...' % len(rqlInv) ) |
---|
350 | rql = set() |
---|
351 | for u in rql0: |
---|
352 | if inx.uid[u]._h.label != 'remarks': |
---|
353 | rql.add( u ) |
---|
354 | |
---|
355 | return rql, l1p, exset |
---|
356 | |
---|
357 | def varsByRql( self, rql, pmax=2, intersection=False, asDict=False): |
---|
358 | """The complete set of variables associated with a set of request links.""" |
---|
359 | inx = self.dq.inx |
---|
360 | cc1 = collections.defaultdict( set ) |
---|
361 | for i in rql: |
---|
362 | o = inx.uid[i] |
---|
363 | if o.opt == 'priority': |
---|
364 | p = int( float( o.opar ) ) |
---|
365 | assert p in [1,2,3], 'Priority incorrectly set .. %s, %s, %s' % (o.label,o.title, o.uid) |
---|
366 | cc1[inx.uid[i].mip].add( (inx.uid[i].refid,p) ) |
---|
367 | else: |
---|
368 | cc1[inx.uid[i].mip].add( inx.uid[i].refid ) |
---|
369 | |
---|
370 | if intersection: |
---|
371 | ccv = {} |
---|
372 | # |
---|
373 | # set of request variables for each MIP |
---|
374 | # |
---|
375 | for k in cc1: |
---|
376 | thisc = reduce( operator.or_, [set( inx.iref_by_sect[vg].a['requestVar'] ) for vg in cc1[k] ] ) |
---|
377 | rqvgs = collections.defaultdict( set ) |
---|
378 | for x in cc1[k]: |
---|
379 | if type(x) == type( () ): |
---|
380 | rqvgs[x[0]].add( x[1] ) |
---|
381 | else: |
---|
382 | rqvgs[x].add( 3 ) |
---|
383 | |
---|
384 | s = set() |
---|
385 | for vg in rqvgs: |
---|
386 | for l in inx.iref_by_sect[vg].a['requestVar']: |
---|
387 | if inx.uid[l].priority <= min(pmax,max(rqvgs[vg])): |
---|
388 | s.add( inx.uid[l].vid ) |
---|
389 | ccv[k] = s |
---|
390 | |
---|
391 | if len( ccv.keys() ) < len( list(imips) ): |
---|
392 | vars = set() |
---|
393 | else: |
---|
394 | vars = reduce( operator.and_, [ccv[k] for k in ccv] ) |
---|
395 | else: |
---|
396 | rqvgs = collections.defaultdict( set ) |
---|
397 | for k in cc1: |
---|
398 | for x in cc1[k]: |
---|
399 | if type(x) == type( () ): |
---|
400 | rqvgs[x[0]].add( x[1] ) |
---|
401 | else: |
---|
402 | rqvgs[x].add( 3 ) |
---|
403 | |
---|
404 | ###To obtain a set of variables associated with this collection of variable groups: |
---|
405 | |
---|
406 | if asDict: |
---|
407 | vars = collections.defaultdict( list ) |
---|
408 | else: |
---|
409 | vars = set() |
---|
410 | for vg in rqvgs: |
---|
411 | for l in inx.iref_by_sect[vg].a['requestVar']: |
---|
412 | if inx.uid[l].priority <= min(pmax,max(rqvgs[vg])): |
---|
413 | if asDict: |
---|
414 | vars[inx.uid[l].vid].append( vg ) |
---|
415 | else: |
---|
416 | vars.add(inx.uid[l].vid) |
---|
417 | |
---|
418 | ##col1 = reduce( operator.or_, [set( inx.iref_by_sect[vg].a['requestVar'] ) for vg in rqvg ] ) |
---|
419 | ### filter out cases where the request does not point to a CMOR variable. |
---|
420 | ##vars = {vid for vid in vars if inx.uid[vid][0] == u'CMORvar'} |
---|
421 | |
---|
422 | if asDict: |
---|
423 | thisvars = {} |
---|
424 | for vid in vars: |
---|
425 | if inx.uid[vid]._h.label == u'CMORvar': |
---|
426 | thisvars[vid] = vars[vid] |
---|
427 | else: |
---|
428 | thisvars = set() |
---|
429 | for vid in vars: |
---|
430 | if inx.uid[vid]._h.label == u'CMORvar': |
---|
431 | thisvars.add(vid) |
---|
432 | |
---|
433 | return thisvars |
---|
434 | |
---|
435 | def exptYears( self, rqll, ex=None): |
---|
436 | """Parse a set of request links, and get years requested for each (varGroup, expt, grid) tuple """ |
---|
437 | |
---|
438 | cc = collections.defaultdict( set ) |
---|
439 | for rl in rqll: |
---|
440 | if 'requestItem' not in self.dq.inx.iref_by_sect[rl.uid].a: |
---|
441 | print ( 'WARN.001.00001: no request items for: %s, %s' % (rl.uid, rl.title) ) |
---|
442 | else: |
---|
443 | |
---|
444 | if rl.grid == '100km': |
---|
445 | grd = '1deg' |
---|
446 | if rl.grid in ['1deg','2deg']: |
---|
447 | grd = rl.grid |
---|
448 | else: |
---|
449 | ## note that naming of "gridreq" is unfortunate ... "No" means that native grid is required |
---|
450 | if rl.gridreq in ['No', 'no'] or self.gridPolicyDefaultNative: |
---|
451 | grd = 'native' |
---|
452 | else: |
---|
453 | ## print ( 'INFO.grd.00001: defaulting to grid ..%s, %s, %s' % (rl.label,rl.title, rl.uid) ) |
---|
454 | grd = 'DEF' |
---|
455 | |
---|
456 | for iu in self.dq.inx.iref_by_sect[rl.uid].a['requestItem']: |
---|
457 | i = self.dq.inx.uid[iu] |
---|
458 | if iu in self.rqiExp: |
---|
459 | for e in self.rqiExp[iu][1]: |
---|
460 | if ex == None or e in ex: |
---|
461 | this = self.rqiExp[iu][1][e] |
---|
462 | if this != None: |
---|
463 | thisns = this[-3] |
---|
464 | thisny = this[-2] |
---|
465 | thisne = this[-1] |
---|
466 | cc[ (rl.refid,e,grd) ].add( filter1( thisns*thisny*thisne, i.nymax) ) |
---|
467 | ee = collections.defaultdict( dict ) |
---|
468 | |
---|
469 | revertToLast = True |
---|
470 | if revertToLast: |
---|
471 | for g,e,grd in cc: |
---|
472 | ee[g][(e,grd)] = max( cc[( g,e,grd) ] ) |
---|
473 | return ee |
---|
474 | ff = collections.defaultdict( dict ) |
---|
475 | ## |
---|
476 | ## this needs to be done separately for ocean and atmosphere, because of the default logic workflow ... |
---|
477 | for g,e,grd in cc: |
---|
478 | ee[(g,e)][grd] = max( cc[( g,e,grd) ] ) |
---|
479 | |
---|
480 | xx = collections.defaultdict( dict ) |
---|
481 | for g,e in ee: |
---|
482 | ddef = ee[(g,e)].get( 'DEF', 0 ) |
---|
483 | for grd in ee[(g,e)]: |
---|
484 | if grd != 'DEF': |
---|
485 | xx[(g,'a')][(e, grd)] = ee[(g,e)][grd] |
---|
486 | xx[(g,'o')][(e, grd)] = ee[(g,e)][grd] |
---|
487 | xx[(g,'')][(e, grd)] = ee[(g,e)][grd] |
---|
488 | if grd == 'native' and ddef != 0: |
---|
489 | xx[(g,'a')][(e, 'native')] = max( [xx[(g,'a')][(e, 'native')],ddef] ) |
---|
490 | xx[(g,'')][(e, 'native')] = max( [xx[(g,'')][(e, 'native')],ddef] ) |
---|
491 | if grd == '1deg' and ddef != 0: |
---|
492 | xx[(g,'o')][(e, '1deg')] = max( [xx[(g,'o')][(e, '1deg')],ddef] ) |
---|
493 | |
---|
494 | for grp,flg in xx: |
---|
495 | ff[grp][flg] = xx[(grp,flg)] |
---|
496 | |
---|
497 | ## return dict[<variable group>]{dict[<experiment><grid>]{<years>}} |
---|
498 | ## return dict[<variable group>][grid flag]{dict[<experiment>,<grid>]{<years>}} |
---|
499 | return ff |
---|
500 | |
---|
501 | def volByExpt( self, l1, ex, pmax=1, cc=None, retainRedundantRank=False, intersection=False,expFullEx=False, adsCount=False ): |
---|
502 | """volByExpt: calculates the total data volume associated with an experiment/experiment group and a list of request items. |
---|
503 | The calculation has some approximations concerning the number of years in each experiment group. |
---|
504 | cc: an optional collector, to accumulate indexed volumes. """ |
---|
505 | ## |
---|
506 | inx = self.dq.inx |
---|
507 | imips = set() |
---|
508 | for i in l1: |
---|
509 | imips.add(i.mip) |
---|
510 | |
---|
511 | rql, l1p, exset = self.rqlByExpt( l1, ex, pmax=pmax, expFullEx=expFullEx ) |
---|
512 | verbose = False |
---|
513 | if verbose: |
---|
514 | for i in rql: |
---|
515 | r = inx.uid[i] |
---|
516 | print ( '%s, %s, %s' % (r.label, r.title, r.uid) ) |
---|
517 | |
---|
518 | if ex != None: |
---|
519 | |
---|
520 | exi = self.dq.inx.uid[ex] |
---|
521 | if exi._h.label == 'experiment': |
---|
522 | exset = set( [ex,exi.egid,exi.mip] ) |
---|
523 | ##### |
---|
524 | if len( rql ) == 0: |
---|
525 | self.vars = set() |
---|
526 | return (0,{},{} ) |
---|
527 | |
---|
528 | ## The complete set of variables associated with these requests: |
---|
529 | vars = self.varsByRql( rql, pmax=pmax, intersection=intersection, asDict=True) |
---|
530 | ## |
---|
531 | ## filter by configuration option and rank |
---|
532 | ## |
---|
533 | if not retainRedundantRank: |
---|
534 | len1 = len(vars.keys()) |
---|
535 | cmv = self.cmvFilter.filterByChoiceRank(cmv=vars.keys()) |
---|
536 | vars = cmv |
---|
537 | |
---|
538 | self.vars = vars |
---|
539 | |
---|
540 | e = {} |
---|
541 | for u in rql: |
---|
542 | ### for request variables which reference the variable group attached to the link, add the associate CMOR variables, subject to priority |
---|
543 | i = inx.uid[u] |
---|
544 | e[i.uid] = set() |
---|
545 | si = collections.defaultdict( list ) |
---|
546 | for x in inx.iref_by_sect[i.refid].a['requestVar']: |
---|
547 | if inx.uid[x].priority <= pmax: |
---|
548 | e[i.uid].add( inx.uid[x].vid ) |
---|
549 | |
---|
550 | if verbose: |
---|
551 | cmv = inx.uid[inx.uid[x].vid] |
---|
552 | if cmv._h.label == 'CMORvar': |
---|
553 | si[ cmv.mipTable ].append( inx.uid[x].label ) |
---|
554 | # |
---|
555 | # for each variable, calculate the maximum number of years across all the request links which reference that variable. |
---|
556 | ## |
---|
557 | ## for each request item we have nymax, nenmax, nexmax. |
---|
558 | ## |
---|
559 | nymg = collections.defaultdict( dict ) |
---|
560 | ## |
---|
561 | ## if dataset count rather than volume is wanted, use item 3 from rqiExp tuple. |
---|
562 | if adsCount: |
---|
563 | irqi = 3 |
---|
564 | else: |
---|
565 | irqi = 2 |
---|
566 | |
---|
567 | sgg = set() |
---|
568 | for v in vars: |
---|
569 | s = set() |
---|
570 | sg = collections.defaultdict( set ) |
---|
571 | cc2 = collections.defaultdict( set ) |
---|
572 | cc2s = collections.defaultdict( c1s ) |
---|
573 | for i in l1p: |
---|
574 | ################## |
---|
575 | if (exset == None or i.esid in exset) and v in e[i.rlid]: |
---|
576 | ix = inx.uid[i.esid] |
---|
577 | rl = inx.uid[i.rlid] |
---|
578 | sgg.add( rl.grid ) |
---|
579 | if rl.grid in ['100km','1deg','2deg']: |
---|
580 | grd = rl.grid |
---|
581 | else: |
---|
582 | grd = 'native' |
---|
583 | |
---|
584 | this = None |
---|
585 | if exset == None: |
---|
586 | thisz = 100 |
---|
587 | ## |
---|
588 | ## for a single experiment, look up n years, and n ensemble. |
---|
589 | ## should have nstart???? |
---|
590 | ## |
---|
591 | elif exi._h.label == 'experiment' or ix._h.label == 'experiment': |
---|
592 | this = None |
---|
593 | if ex in self.rqiExp[i.uid][1]: |
---|
594 | this = self.rqiExp[i.uid][1][ex] |
---|
595 | elif ix.uid in self.rqiExp[i.uid][1]: |
---|
596 | this = self.rqiExp[i.uid][1][ix.uid] |
---|
597 | if this != None: |
---|
598 | thisns = this[-3] |
---|
599 | thisny = this[-2] |
---|
600 | thisne = this[-1] |
---|
601 | cc2s[grd].a[u].add( filter1( thisns*thisny*thisne, i.nymax) ) |
---|
602 | else: |
---|
603 | thisz = None |
---|
604 | if 'experiment' in inx.iref_by_sect[i.esid].a: |
---|
605 | for u in inx.iref_by_sect[i.esid].a['experiment']: |
---|
606 | if u in self.rqiExp[i.uid][1]: |
---|
607 | this = self.rqiExp[i.uid][1][u] |
---|
608 | thisns = this[-3] |
---|
609 | thisny = this[-2] |
---|
610 | thisne = this[-1] |
---|
611 | ## |
---|
612 | ### aggregate year count for each experiment and output grid |
---|
613 | ## clarify definition and usage of nymax -- should be redundant ... could be replaced by inward references from "timeSlice" |
---|
614 | cc2s[grd].a[u].add( filter1( thisns*thisny*thisne, i.nymax) ) |
---|
615 | |
---|
616 | if exset != None: |
---|
617 | sg[grd].add( self.rqiExp[i.uid][irqi] ) |
---|
618 | |
---|
619 | ### |
---|
620 | ### sum over experiments of maximum within each experiment |
---|
621 | ### |
---|
622 | for g in sg: |
---|
623 | nymg[v][g] = sum( [max( cc2s[g].a[k] ) for k in cc2s[g].a] ) |
---|
624 | |
---|
625 | szv = {} |
---|
626 | ov = [] |
---|
627 | for v in vars: |
---|
628 | if 'requestVar' not in inx.iref_by_sect[v].a: |
---|
629 | print ( 'Variable with no request ....: %s, %s' % (inx.uid[v].label, inx.uid[v].mipTable) ) |
---|
630 | try: |
---|
631 | szv[v] = self.sz[inx.uid[v].stid]*npy[inx.uid[v].frequency] |
---|
632 | except: |
---|
633 | if inx.uid[v].stid not in self.sz: |
---|
634 | print ('ERROR: size not found for stid %s (v=%s, %s)' % (inx.uid[v].stid,v,inx.uid[v].label) ) |
---|
635 | if inx.uid[v].frequency not in npy: |
---|
636 | print ('ERROR: npy not found for frequency %s (v=%s, %s)' % (inx.uid[v].frequency,v,inx.uid[v].label) ) |
---|
637 | szv[v] = 0 |
---|
638 | ov.append( self.dq.inx.uid[v] ) |
---|
639 | |
---|
640 | ff = {} |
---|
641 | for v in vars: |
---|
642 | if adsCount: |
---|
643 | ff[v] = 1 |
---|
644 | else: |
---|
645 | if 'native' in nymg[v]: |
---|
646 | ff[v] = szv[v] |
---|
647 | ny = nymg[v]['native'] |
---|
648 | else: |
---|
649 | if len( nymg[v] ) > 1: |
---|
650 | print ( '########### Selecting first in list .............' ) |
---|
651 | ks0 = nymg[v].keys() |
---|
652 | if len(ks0) == 0: |
---|
653 | ff[v] = 0. |
---|
654 | ny = 0. |
---|
655 | else: |
---|
656 | ks = list( nymg[v].keys() )[0] |
---|
657 | ny = nymg[v][ks] |
---|
658 | if inx.uid[v].stid in self.szg[ks]: |
---|
659 | ff[v] = self.szg[ks][ inx.uid[v].stid ] * npy[inx.uid[v].frequency] |
---|
660 | else: |
---|
661 | ff[v] = 0. |
---|
662 | |
---|
663 | if inx.uid[v].frequency != 'monClim': |
---|
664 | ff[v] = ff[v]*ny |
---|
665 | |
---|
666 | ee = self.listIndexDual( ov, 'mipTable', 'label', acount=None, alist=None, cdict=ff, cc=cc ) |
---|
667 | self.ngptot = sum( [ ff[v] for v in vars] ) |
---|
668 | return (self.ngptot, ee, ff ) |
---|
669 | |
---|
670 | def esid_to_exptList(self,esid,deref=False,full=False): |
---|
671 | if not esid in self.dq.inx.uid: |
---|
672 | mlg.prnt ( 'Attempt to dereferece invalid uid: %s' % esid ) |
---|
673 | raise |
---|
674 | |
---|
675 | if self.dq.inx.uid[esid]._h.label == 'experiment': |
---|
676 | expts = [esid,] |
---|
677 | elif self.dq.inx.uid[esid]._h.label != 'remarks': |
---|
678 | if esid in self.dq.inx.iref_by_sect and 'experiment' in self.dq.inx.iref_by_sect[esid].a: |
---|
679 | expts = list( self.dq.inx.iref_by_sect[esid].a['experiment'][:] ) |
---|
680 | else: |
---|
681 | expts = [] |
---|
682 | |
---|
683 | ## add in groups and mips for completeness |
---|
684 | ## |
---|
685 | if full: |
---|
686 | if self.dq.inx.uid[esid]._h.label == 'mip': |
---|
687 | s = set() |
---|
688 | for e in expts: |
---|
689 | if self.dq.inx.uid[e]._h.label != 'experiment': |
---|
690 | mlg.prnt ( 'ERROR: %s, %s, %s ' % (esid,e, self.dq.inx.uid[e].title ) ) |
---|
691 | s.add( self.dq.inx.uid[e].egid ) |
---|
692 | for i in s: |
---|
693 | expts.append( i ) |
---|
694 | expts.append( esid ) |
---|
695 | else: |
---|
696 | ##print ( 'WARNING: request link not associated with valid experiment group' ) |
---|
697 | ##raise |
---|
698 | return None |
---|
699 | |
---|
700 | if self.tierMax > 0: |
---|
701 | expts1 = [] |
---|
702 | for i in expts: |
---|
703 | if self.dq.inx.uid[i]._h.label == 'experiment': |
---|
704 | if self.dq.inx.uid[i].tier[0] <= self.tierMax: |
---|
705 | expts1.append( i ) |
---|
706 | elif self.dq.inx.uid[i]._h.label == 'exptgroup': |
---|
707 | if self.dq.inx.uid[i].tierMin <= self.tierMax: |
---|
708 | expts1.append( i ) |
---|
709 | else: |
---|
710 | expts1.append( i ) |
---|
711 | else: |
---|
712 | expts1 = expts |
---|
713 | |
---|
714 | if deref: |
---|
715 | return [self.dq.inx.uid[e] for e in expts1] |
---|
716 | else: |
---|
717 | return expts1 |
---|
718 | ## |
---|
719 | ## need to call this on load |
---|
720 | ## then use instead of i.ny etc below |
---|
721 | ## |
---|
722 | def requestItemExpAll( self ): |
---|
723 | self.rqiExp = {} |
---|
724 | for rqi in self.dq.coll['requestItem'].items: |
---|
725 | a,b,c,d = self.requestItemExp( rqi ) |
---|
726 | if a != None: |
---|
727 | self.rqiExp[rqi.uid] = (a,b,c,d) |
---|
728 | |
---|
729 | def requestItemExp( self, rqi ): |
---|
730 | assert rqi._h.label == "requestItem", 'Argument to requestItemExp must be a requestItem' |
---|
731 | u = rqi.esid |
---|
732 | if self.dq.inx.uid[u]._h.label == 'experiment': |
---|
733 | expts = [u,] |
---|
734 | elif self.dq.inx.uid[u]._h.label != 'remarks': |
---|
735 | if u in self.dq.inx.iref_by_sect and 'experiment' in self.dq.inx.iref_by_sect[u].a: |
---|
736 | expts = self.dq.inx.iref_by_sect[u].a['experiment'] |
---|
737 | else: |
---|
738 | expts = [] |
---|
739 | else: |
---|
740 | return (None, None, None, None) |
---|
741 | |
---|
742 | if self.tierMax > 0: |
---|
743 | expts = [i for i in expts if self.dq.inx.uid[i].tier[0] <= self.tierMax] |
---|
744 | |
---|
745 | self.multiTierOnly = False |
---|
746 | if self.multiTierOnly: |
---|
747 | expts = [i for i in expts if len(self.dq.inx.uid[i].tier) > 1] |
---|
748 | print ('Len expts: %s' % len(expts) ) |
---|
749 | |
---|
750 | if len(expts) > 0: |
---|
751 | e = [self.dq.inx.uid[i] for i in expts] |
---|
752 | for i in e: |
---|
753 | if i._h.label != 'experiment': |
---|
754 | mlg.prnt ( 'ERROR: %s, %s, %s ' % ( u,i._h.label, i.label, i.title ) ) |
---|
755 | ##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] |
---|
756 | dat2 = {} |
---|
757 | for i in e: |
---|
758 | 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) ) |
---|
759 | |
---|
760 | nytot = sum( [dat2[x][-2]*dat2[x][-3] for x in dat2 ] ) |
---|
761 | netot = sum( [dat2[x][-1] for x in dat2 ] ) |
---|
762 | else: |
---|
763 | dat2 = {} |
---|
764 | nytot = 0 |
---|
765 | netot = 0 |
---|
766 | |
---|
767 | ## |
---|
768 | ## to get list of years per expt for each requestLink ... expts is union of all dat2 keys, |
---|
769 | ## and want max of dat2[x][0] for each experiment x. |
---|
770 | ## |
---|
771 | return (expts, dat2, nytot, netot ) |
---|
772 | |
---|
773 | def setTierMax( self, tierMax ): |
---|
774 | """Set the maxium tier and recompute request sizes""" |
---|
775 | if tierMax != self.tierMax: |
---|
776 | self.tierMax = tierMax |
---|
777 | self.requestItemExpAll( ) |
---|
778 | |
---|
779 | def summaryByMip( self, pmax=1 ): |
---|
780 | bytesPerFloat = 2. |
---|
781 | for m in self.mipls: |
---|
782 | v = self.volByMip( m, pmax=pmax ) |
---|
783 | mlg.prnt ( '%12.12s: %6.2fTb' % (m,v*bytesPerFloat*1.e-12) ) |
---|
784 | |
---|
785 | def rqlByMip( self, mip): |
---|
786 | if mip == 'TOTAL': |
---|
787 | mip = self.mips |
---|
788 | |
---|
789 | if type(mip) in [type( '' ),type( u'') ]: |
---|
790 | if mip not in self.mips: |
---|
791 | mlg.prnt ( self.mips ) |
---|
792 | raise baseException( 'rqiByMip: Name of mip not recognised: %s' % mip ) |
---|
793 | l1 = [i for i in self.dq.coll['requestLink'].items if i.mip == mip] |
---|
794 | elif type(mip) in [ type( set()), type( [] ) ]: |
---|
795 | nf = [ m for m in mip if m not in self.mips] |
---|
796 | if len(nf) > 0: |
---|
797 | raise baseException( 'rqlByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
798 | l1 = [i for i in self.dq.coll['requestLink'].items if i.mip in mip] |
---|
799 | elif type(mip) == type( dict()): |
---|
800 | nf = [ m for m in mip if m not in self.mips] |
---|
801 | if len(nf) > 0: |
---|
802 | raise baseException( 'rqlByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
803 | l1 = [] |
---|
804 | for i in self.dq.coll['requestLink'].items: |
---|
805 | if i.mip in mip: |
---|
806 | ok = False |
---|
807 | if len( mip[i.mip] ) == 0: |
---|
808 | ok = True |
---|
809 | else: |
---|
810 | for ol in self.dq.inx.iref_by_sect[i.uid].a['objectiveLink']: |
---|
811 | o = self.dq.inx.uid[ol] |
---|
812 | if self.dq.inx.uid[o.oid].label in mip[i.mip]: |
---|
813 | ok = True |
---|
814 | if ok: |
---|
815 | l1.append( i ) |
---|
816 | else: |
---|
817 | raise baseException( 'rqiByMip: "mip" (1st explicit argument) should be type string or set: %s -- %s' % (mip, type(mip)) ) |
---|
818 | |
---|
819 | return l1 |
---|
820 | |
---|
821 | def rqiByMip( self, mip): |
---|
822 | l1 = self.rqlByMip( mip ) |
---|
823 | if len(l1) == 0: |
---|
824 | return [] |
---|
825 | l2 = [] |
---|
826 | for i in l1: |
---|
827 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
828 | for u in self.dq.inx.iref_by_sect[i.uid].a['requestItem']: |
---|
829 | l2.append( self.dq.inx.uid[u] ) |
---|
830 | |
---|
831 | l20 = self.rqiByMip0( mip ) |
---|
832 | for i in l20: |
---|
833 | assert i in l2 |
---|
834 | return l2 |
---|
835 | |
---|
836 | |
---|
837 | def rqiByMip0( self, mip): |
---|
838 | |
---|
839 | if mip == 'TOTAL': |
---|
840 | mip = self.mips |
---|
841 | if type(mip) in [type( '' ),type( u'') ]: |
---|
842 | if mip not in self.mips: |
---|
843 | mlg.prnt ( self.mips ) |
---|
844 | raise baseException( 'rqiByMip: Name of mip not recognised: %s' % mip ) |
---|
845 | l1 = [i for i in self.dq.coll['requestItem'].items if i.mip == mip] |
---|
846 | elif type(mip) in [ type( set()), type( [] ) ]: |
---|
847 | nf = [ m for m in mip if m not in self.mips] |
---|
848 | if len(nf) > 0: |
---|
849 | raise baseException( 'rqiByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
850 | l1 = [i for i in self.dq.coll['requestItem'].items if i.mip in mip] |
---|
851 | elif type(mip) == type( dict()): |
---|
852 | nf = [ m for m in mip if m not in self.mips] |
---|
853 | if len(nf) > 0: |
---|
854 | raise baseException( 'rqiByMip: Name of mip(s) not recognised: %s' % str(nf) ) |
---|
855 | l1 = [] |
---|
856 | for i in self.dq.coll['requestLink'].items: |
---|
857 | if i.mip in mip: |
---|
858 | ok = False |
---|
859 | if len( mip[i.mip] ) == 0: |
---|
860 | ok = True |
---|
861 | else: |
---|
862 | for ol in self.dq.inx.iref_by_sect[i.uid].a['objectiveLink']: |
---|
863 | o = self.dq.inx.uid[ol] |
---|
864 | if self.dq.inx.uid[o.oid].label in mip[i.mip]: |
---|
865 | ok = True |
---|
866 | if ok: |
---|
867 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
868 | for u in self.dq.inx.iref_by_sect[i.uid].a['requestItem']: |
---|
869 | l1.append( self.dq.inx.uid[u] ) |
---|
870 | else: |
---|
871 | raise baseException( 'rqiByMip: "mip" (1st explicit argument) should be type string or set: %s -- %s' % (mip, type(mip)) ) |
---|
872 | |
---|
873 | return l1 |
---|
874 | |
---|
875 | def checkDir(self,odir,msg): |
---|
876 | if not os.path.isdir( odir ): |
---|
877 | try: |
---|
878 | os.mkdir( odir ) |
---|
879 | except: |
---|
880 | print ('\n\nFailed to make directory "%s" for: %s: make necessary subdirectories or run where you have write access' % (odir,msg) ) |
---|
881 | print ( '\n\n' ) |
---|
882 | raise |
---|
883 | print ('Created directory %s for: %s' % (odir,msg) ) |
---|
884 | |
---|
885 | |
---|
886 | def xlsByMipExpt(self,m,ex,pmax,odir='xls',xls=True,txt=False,txtOpts=None): |
---|
887 | import scope_utils |
---|
888 | mxls = scope_utils.xlsTabs(self,tiermax=self.tierMax,pmax=pmax,xls=xls, txt=txt, txtOpts=txtOpts,odir=odir) |
---|
889 | mlab = makeTables.setMlab( m ) |
---|
890 | mxls.run( m, mlab=mlab ) |
---|
891 | |
---|
892 | def cmvByInvMip( self, mip,pmax=1,includeYears=False, exptFilter=None ): |
---|
893 | mips = set( self.mips[:] ) |
---|
894 | if type(mip) == type( '' ): |
---|
895 | mips.discard( mip ) |
---|
896 | else: |
---|
897 | for m in mip: |
---|
898 | mips.discard( m ) |
---|
899 | |
---|
900 | return self.cmvByMip( mips,pmax=pmax,includeYears=includeYears, exptFilter=exptFilter ) |
---|
901 | |
---|
902 | def cmvByMip( self, mip,pmax=1,includeYears=False, exptFilter=None ): |
---|
903 | if exptFilter != None: |
---|
904 | assert type(exptFilter) == type( set() ), 'Argument exptFilter must be None or a set: %s' % str(exptFilter) |
---|
905 | l1,ee = self.rvgByMip( mip, includePreset=True, returnLinks=True ) |
---|
906 | if includeYears: |
---|
907 | expys = self.exptYears( l1 ) |
---|
908 | cc = collections.defaultdict( set ) |
---|
909 | ss = set() |
---|
910 | for pr in ee: |
---|
911 | ### loop over request var groups. |
---|
912 | for i in ee[pr]: |
---|
913 | if 'requestVar' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
914 | # |
---|
915 | # loop over request vars in group |
---|
916 | # |
---|
917 | for x in self.dq.inx.iref_by_sect[i.uid].a['requestVar']: |
---|
918 | i1 = self.dq.inx.uid[x] |
---|
919 | if (pr == -1 and i1.priority <= pmax) or (pr > 0 and pr <= pmax): |
---|
920 | if includeYears and i1.vid in self.cmvGridId: |
---|
921 | ##assert i.uid in expys, 'No experiment info found for requestVarGroup: %s' % i.uid |
---|
922 | ## may have no entry as a consequence of tierMin being set in the requestLink(s). |
---|
923 | assert i1.vid in self.cmvGridId, 'No grid identification lookup found for %s: %s' % (i1.label,i1.vid) |
---|
924 | assert self.cmvGridId[i1.vid] in ['a','o','si','li'], 'Unexpected grid id: %s: %s:: %s' % (i1.label,i1.vid, self.cmvGridId[i1.vid]) |
---|
925 | gflg = {'si':'','li':''}.get( self.cmvGridId[i1.vid], self.cmvGridId[i1.vid] ) |
---|
926 | rtl = True |
---|
927 | if i.uid in expys: |
---|
928 | if rtl: |
---|
929 | for e,grd in expys[i.uid]: |
---|
930 | if exptFilter == None or e in exptFilter: |
---|
931 | if grd == 'DEF': |
---|
932 | if gflg == 'o': |
---|
933 | grd1 = '1deg' |
---|
934 | else: |
---|
935 | grd1 = 'native' |
---|
936 | else: |
---|
937 | grd1 = grd |
---|
938 | cc[(i1.vid,e,grd1)].add( expys[i.uid][e,grd] ) |
---|
939 | else: |
---|
940 | for gf in expys[i.uid]: |
---|
941 | for e,grd in expys[i.uid][gf]: |
---|
942 | if grd in ["1deg",'2deg'] or gf == gflg: |
---|
943 | if exptFilter == None or e in exptFilter: |
---|
944 | cc[(i1.vid,e,grd)].add( expys[i.uid][gf][e,grd] ) |
---|
945 | else: |
---|
946 | print ( 'SKIPPING %s: %s' % (i1.label,i1.vid) ) |
---|
947 | ss.add( i1.vid ) |
---|
948 | if includeYears: |
---|
949 | l2 = collections.defaultdict( dict ) |
---|
950 | l2x = collections.defaultdict( dict ) |
---|
951 | ## |
---|
952 | ## this removes lower ranked grids .... but for some groups want different grids for different variable categories |
---|
953 | ## |
---|
954 | if self.gridPolicyTopOnly: |
---|
955 | for v,e,g in cc: |
---|
956 | l2x[(v,e)][g] = max( list( cc[(v,e,g)] ) ) |
---|
957 | for v,e in l2x: |
---|
958 | if len( l2x[(v,e)].keys() ) == 1: |
---|
959 | g,val = list( l2x[(v,e)].items() )[0] |
---|
960 | else: |
---|
961 | if 'native' in l2x[(v,e)].keys(): |
---|
962 | g = 'native' |
---|
963 | val = l2x[(v,e)][g] |
---|
964 | else: |
---|
965 | g = sorted( list( l2x[(v,e)].keys() ) )[0] |
---|
966 | val = l2x[(v,e)][g] |
---|
967 | l2[v][(e,g)] = val |
---|
968 | else: |
---|
969 | for v,e,g in cc: |
---|
970 | l2[v][(e,g)] = max( list( cc[(v,e,g)] ) ) |
---|
971 | else: |
---|
972 | l2 = sorted( [i for i in [self.dq.inx.uid[i] for i in ss] if i._h.label != 'remarks'], key=lambda x: x.label ) |
---|
973 | return l2 |
---|
974 | |
---|
975 | def exptFilterList(self,val,option,ret='uid'): |
---|
976 | if type( val ) not in [[],()]: |
---|
977 | val = [val,] |
---|
978 | |
---|
979 | if option == 'lab': |
---|
980 | v0 = val[:] |
---|
981 | val = [] |
---|
982 | mm = [] |
---|
983 | for v in v0: |
---|
984 | if v not in self.exptByLabel: |
---|
985 | mm.append( v ) |
---|
986 | else: |
---|
987 | val.append( self.exptByLabel[v] ) |
---|
988 | |
---|
989 | assert len(mm) == 0, 'Experiment names not all recognised: %s' % str(mm) |
---|
990 | |
---|
991 | oo = set() |
---|
992 | for v in val: |
---|
993 | i = self.dq.inx.uid[v] |
---|
994 | if i._h.label in ['exptgroup','mip']: |
---|
995 | if 'experiment' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
996 | for u in self.dq.inx.iref_by_sect[i.uid].a['experiment']: |
---|
997 | oo.add( u ) |
---|
998 | elif i._h.label == 'experiment': |
---|
999 | oo.add( i.uid ) |
---|
1000 | else: |
---|
1001 | print ('WARNING .. skipping request for experiment which links to record of type %s' % i._h.label ) |
---|
1002 | return oo |
---|
1003 | |
---|
1004 | def getFreqStrSummary(self,mip,pmax=1): |
---|
1005 | ## |
---|
1006 | ## get a dictionary keyed on CMORvar uid, containing dictionary keyed on (experiment, grid) with value as number of years. |
---|
1007 | ## |
---|
1008 | if not self.uniqueRequest: |
---|
1009 | cmv = self.cmvByMip(mip,pmax=pmax,includeYears=True,exptFilter=self.exptFilter) |
---|
1010 | else: |
---|
1011 | cmv1 = self.cmvByInvMip(mip,pmax=pmax,includeYears=True,exptFilter=self.exptFilter) |
---|
1012 | cmv2 = self.cmvByMip('TOTAL',pmax=pmax,includeYears=True,exptFilter=self.exptFilter) |
---|
1013 | cmv = self.differenceSelectedCmvDict( cmv1, cmv2 ) |
---|
1014 | |
---|
1015 | self.selectedCmv = cmv |
---|
1016 | return self.cmvByFreqStr( cmv ) |
---|
1017 | |
---|
1018 | def differenceSelectedCmvDict( self, cmv1, cmv2 ): |
---|
1019 | """Return the diffence between two dictionaries of cmor variables returned by self.cmvByMip. |
---|
1020 | The dictionaries contain dictionaries of values. Differences may be subdictionaries not present, |
---|
1021 | elements of sub-dictionaries not present, or elements of sub-dictionaries present with different values. |
---|
1022 | A one sided difference is returned.""" |
---|
1023 | |
---|
1024 | cmv = {} |
---|
1025 | for i in cmv2: |
---|
1026 | if i not in cmv1: |
---|
1027 | cmv[i] = cmv2[i] |
---|
1028 | else: |
---|
1029 | eei = {} |
---|
1030 | for t in cmv2[i]: |
---|
1031 | if t not in cmv1[i]: |
---|
1032 | eei[t] = cmv2[i][t] |
---|
1033 | else: |
---|
1034 | if cmv2[i][t] > cmv1[i][t]: |
---|
1035 | eei[t] = cmv2[i][t] - cmv1[i][t] |
---|
1036 | if len( eei.keys() ) != 0: |
---|
1037 | cmv[i] = eei |
---|
1038 | return cmv |
---|
1039 | |
---|
1040 | def cmvByFreqStr(self,cmv,asDict=True,exptFilter=None): |
---|
1041 | if exptFilter != None: |
---|
1042 | assert type(exptFilter) == type( set() ), 'Argument exptFilter must be None or a set: %s' % str(exptFilter) |
---|
1043 | cc = collections.defaultdict( list ) |
---|
1044 | for i in cmv: |
---|
1045 | if asDict: |
---|
1046 | ii = self.dq.inx.uid[i] |
---|
1047 | if ii._h.label != 'remarks': |
---|
1048 | st = self.dq.inx.uid[ ii.stid ] |
---|
1049 | cc0 = collections.defaultdict( float ) |
---|
1050 | cc1 = collections.defaultdict( int ) |
---|
1051 | se = collections.defaultdict( set ) |
---|
1052 | for e,g in cmv[i]: |
---|
1053 | cc0[g] += cmv[i][(e,g)] |
---|
1054 | cc1[g] += 1 |
---|
1055 | se[g].add(e) |
---|
1056 | for g in cc0: |
---|
1057 | g1 = 'native' |
---|
1058 | if self.isLatLon[st.spid] != False: |
---|
1059 | g1 = g |
---|
1060 | if g1 == 'DEF' and self.isLatLon[st.spid] == 'o': |
---|
1061 | g1 = '1deg' |
---|
1062 | else: |
---|
1063 | g1 = 'native' |
---|
1064 | g1 = g |
---|
1065 | |
---|
1066 | cc[ (st.spid,st.odims,ii.frequency,g1) ].append( (i,cc0[g],cc1[g],se[g]) ) |
---|
1067 | |
---|
1068 | else: |
---|
1069 | st = self.dq.inx.uid[ i.stid ] |
---|
1070 | cc[ (st.spid,st.odims,i.frequency) ].append( i.label ) |
---|
1071 | |
---|
1072 | self.thiscmvset = set() |
---|
1073 | c2 = collections.defaultdict( dict ) |
---|
1074 | sf = set() |
---|
1075 | if asDict: |
---|
1076 | for s,o,f,g in cc.keys(): |
---|
1077 | c2[(s,o,g)][f] = cc[ (s,o,f,g) ] |
---|
1078 | sf.add( f ) |
---|
1079 | else: |
---|
1080 | for s,o,f in cc.keys(): |
---|
1081 | c2[(s,o)][f] = cc[ (s,o,f) ] |
---|
1082 | sf.add( f ) |
---|
1083 | lf = sorted( list(sf) ) |
---|
1084 | c3 = collections.defaultdict( dict ) |
---|
1085 | |
---|
1086 | for tt in sorted( c2.keys() ): |
---|
1087 | if asDict: |
---|
1088 | s,o,g = tt |
---|
1089 | else: |
---|
1090 | s,o = tt |
---|
1091 | g = 'native' |
---|
1092 | i = self.dq.inx.uid[ s ] |
---|
1093 | |
---|
1094 | if asDict: |
---|
1095 | for f in c2[tt]: |
---|
1096 | isClim = f.lower().find( 'clim' ) != -1 |
---|
1097 | ny = 0 |
---|
1098 | expts = set() |
---|
1099 | labs = [] |
---|
1100 | labs = collections.defaultdict( int ) |
---|
1101 | ccx = collections.defaultdict( list ) |
---|
1102 | for cmvi, ny1, ne, eset in c2[tt][f]: |
---|
1103 | ccx[cmvi].append( (ny1, ne, eset) ) |
---|
1104 | net = 0 |
---|
1105 | for cmvi in ccx: |
---|
1106 | if len( ccx[cmvi] ) == 1: |
---|
1107 | ny1, ne, eset = ccx[cmvi][0] |
---|
1108 | else: |
---|
1109 | ny1, ne, eset = ( 0,0,set() ) |
---|
1110 | for a,b,s in ccx[cmvi]: |
---|
1111 | ny1 += a |
---|
1112 | ne += b |
---|
1113 | eset = eset.union( s ) |
---|
1114 | |
---|
1115 | net += ne |
---|
1116 | if len(eset) != ne: |
---|
1117 | print ( 'WARNING: inconsistency in volume estimate ... possible duplication for %s,%s' % (cmvi,f) ) |
---|
1118 | for e in eset: |
---|
1119 | elab = self.dq.inx.uid[e].label |
---|
1120 | expts.add(elab) |
---|
1121 | |
---|
1122 | if exptFilter != None: |
---|
1123 | expts = exptFilter.intersection( expts ) |
---|
1124 | |
---|
1125 | if len(expts) > 0: |
---|
1126 | lab = self.dq.inx.uid[cmvi].label |
---|
1127 | self.thiscmvset.add( cmvi ) |
---|
1128 | ny += ny1 |
---|
1129 | labs[cmvi] += ny1 |
---|
1130 | ne = len( expts ) |
---|
1131 | nn = len( labs.keys() ) |
---|
1132 | |
---|
1133 | if isClim: |
---|
1134 | ny = net/float(nn) |
---|
1135 | else: |
---|
1136 | ny = ny/float(nn) |
---|
1137 | assert tt[2] in ['native','1deg','2deg'], 'BAD grid identifier: %s' % str(tt) |
---|
1138 | c3[tt][f] = (nn,ny,ne, labs,expts) |
---|
1139 | return (sf,c3) |
---|
1140 | |
---|
1141 | def getStrSz( self, g, stid=None, s=None, o=None, tt=False ): |
---|
1142 | assert stid == None or (s==None and o==None), 'Specify either stid or s and o' |
---|
1143 | assert stid != None or (s!=None and o!=None), 'Specify either stid or s and o' |
---|
1144 | |
---|
1145 | if stid != None: |
---|
1146 | st = self.dq.inx.uid[stid] |
---|
1147 | if st._h.label != 'remarks': |
---|
1148 | s = st.spid |
---|
1149 | o = st.odims |
---|
1150 | else: |
---|
1151 | self.strSz[ (stid,g) ] = (False,0) |
---|
1152 | if tt: |
---|
1153 | return (self.strSz[ (stid,g) ], None) |
---|
1154 | else: |
---|
1155 | return self.strSz[ (stid,g) ] |
---|
1156 | |
---|
1157 | g1 = g |
---|
1158 | if g1 == 'DEF': |
---|
1159 | if self.isLatLon[s] == 'o': |
---|
1160 | g1 = '1deg' |
---|
1161 | else: |
---|
1162 | g1 = 'native' |
---|
1163 | if (s,o,g) not in self.strSz: |
---|
1164 | |
---|
1165 | if o == '': |
---|
1166 | sf = 1 |
---|
1167 | elif o in self.odsz: |
---|
1168 | sf = self.odsz[o] |
---|
1169 | else: |
---|
1170 | # print ( 'SEVERE.odims.00006: no information on dimension size: %s' % o ) |
---|
1171 | sf = 5 |
---|
1172 | |
---|
1173 | if type( sf ) == type( () ): |
---|
1174 | sf = sf[0] |
---|
1175 | |
---|
1176 | |
---|
1177 | try: |
---|
1178 | if g1 != 'native' and self.isLatLon[s] != False: |
---|
1179 | szg = self.szgss[g1][s] |
---|
1180 | else: |
---|
1181 | szg = self.szss[s] |
---|
1182 | except: |
---|
1183 | print ( 'Failed to get size for: %s, %s, %s' % (g,g1,s ) ) |
---|
1184 | raise |
---|
1185 | |
---|
1186 | szg = szg * sf |
---|
1187 | self.strSz[ (s,o,g) ] = (True,szg) |
---|
1188 | |
---|
1189 | if tt: |
---|
1190 | return (self.strSz[ (s,o,g) ], (s,o,g1) ) |
---|
1191 | else: |
---|
1192 | return self.strSz[ (s,o,g) ] |
---|
1193 | |
---|
1194 | def xxx__csvFreqStrSummary(self,mip,records=False,pmax=1): |
---|
1195 | sf, c3 = self.getFreqStrSummary(mip,pmax=pmax) |
---|
1196 | lf = sorted( list(sf) ) |
---|
1197 | hdr = ['','',''] |
---|
1198 | for f in lf: |
---|
1199 | hdr += [f,'','',str( npy.get( f, '****') )] |
---|
1200 | orecs = [hdr,] |
---|
1201 | crecs = [None,] |
---|
1202 | for tt in sorted( c3.keys() ): |
---|
1203 | s,o,g = tt |
---|
1204 | i = self.dq.inx.uid[ s ] |
---|
1205 | if o != '': |
---|
1206 | msg = '%48.48s [%s]' % (i.title,o) |
---|
1207 | else: |
---|
1208 | msg = '%48.48s' % i.title |
---|
1209 | if g != 'native': |
---|
1210 | msg += '{%s}' % g |
---|
1211 | szg = self.getStrSz( g, s=s, o=o )[1] |
---|
1212 | |
---|
1213 | rec = [msg,szg,2] |
---|
1214 | crec = ['','',''] |
---|
1215 | for f in lf: |
---|
1216 | if f in c3[tt]: |
---|
1217 | nn,ny,ne,labs = c3[tt][f] |
---|
1218 | rec += [nn,ny,ne,''] |
---|
1219 | crec += [labs,'','',''] |
---|
1220 | else: |
---|
1221 | rec += ['','','',''] |
---|
1222 | crec += ['','','',''] |
---|
1223 | orecs.append( rec ) |
---|
1224 | crecs.append( crec ) |
---|
1225 | |
---|
1226 | if records: |
---|
1227 | return (orecs, crecs) |
---|
1228 | oo = open( 'text.csv', 'w' ) |
---|
1229 | for rec in orecs: |
---|
1230 | oo.write( '\t'.join( [str(x) for x in rec] ) + '\n' ) |
---|
1231 | oo.close() |
---|
1232 | |
---|
1233 | def rvgByMip( self, mip, years=False, includePreset=False, returnLinks=False ): |
---|
1234 | l1 = self.rqlByMip( mip ) |
---|
1235 | if includePreset: |
---|
1236 | cc = collections.defaultdict( set ) |
---|
1237 | ss = set() |
---|
1238 | for i in l1: |
---|
1239 | if 'requestItem' in self.dq.inx.iref_by_sect[i.uid].a: |
---|
1240 | prs = set() |
---|
1241 | for x in self.dq.inx.iref_by_sect[i.uid].a['requestItem']: |
---|
1242 | prs.add(self.dq.inx.uid[x].preset) |
---|
1243 | |
---|
1244 | for p in prs: |
---|
1245 | assert p in [-1,1,2,3], 'Bad preset value' |
---|
1246 | cc[p].add( i.refid ) |
---|
1247 | ee = {} |
---|
1248 | for p in cc: |
---|
1249 | l2 = sorted( [self.dq.inx.uid[i] for i in cc[p]], key=lambda x: x.label ) |
---|
1250 | ee[p] = l2 |
---|
1251 | if returnLinks: |
---|
1252 | return (l1,ee) |
---|
1253 | else: |
---|
1254 | return ee |
---|
1255 | else: |
---|
1256 | ss = set( [i.refid for i in l1] ) |
---|
1257 | l2 = sorted( [self.dq.inx.uid[i] for i in ss], key=lambda x: x.label ) |
---|
1258 | if returnLinks: |
---|
1259 | return (l1,l2) |
---|
1260 | else: |
---|
1261 | return l2 |
---|
1262 | |
---|
1263 | def volByMip( self, mip, pmax=2, retainRedundantRank=False, intersection=False, adsCount=False, exptid=None): |
---|
1264 | |
---|
1265 | l1 = self.rqiByMip( mip ) |
---|
1266 | |
---|
1267 | #### The set of experiments/experiment groups: |
---|
1268 | if exptid == None: |
---|
1269 | exps = self.experiments |
---|
1270 | else: |
---|
1271 | exps = set( [exptid,] ) |
---|
1272 | |
---|
1273 | self.volByE = {} |
---|
1274 | vtot = 0 |
---|
1275 | cc = collections.defaultdict( col_count ) |
---|
1276 | self.allVars = set() |
---|
1277 | for e in exps: |
---|
1278 | expts = self.esid_to_exptList(e,deref=True,full=False) |
---|
1279 | if expts not in [None,[]]: |
---|
1280 | ###print 'EXPTS: ',e,len(expts), list( expts )[0].label |
---|
1281 | for ei in expts: |
---|
1282 | self.volByE[ei.label] = self.volByExpt( l1, ei.uid, pmax=pmax, cc=cc, retainRedundantRank=retainRedundantRank, intersection=intersection, adsCount=adsCount ) |
---|
1283 | vtot += self.volByE[ei.label][0] |
---|
1284 | self.allVars = self.allVars.union( self.vars ) |
---|
1285 | ##else: |
---|
1286 | ##print 'No expts found: ',e |
---|
1287 | self.indexedVol = cc |
---|
1288 | |
---|
1289 | return vtot |
---|
1290 | |
---|
1291 | def listIndexDual(self, ll, a1, a2, acount=None, alist=None, cdict=None, cc=None ): |
---|
1292 | do_count = acount != None |
---|
1293 | do_list = alist != None |
---|
1294 | assert not (do_count and do_list), 'It is an error to request both list and count' |
---|
1295 | if not (do_count or do_list): |
---|
1296 | acount = '__number__' |
---|
1297 | do_count = True |
---|
1298 | |
---|
1299 | if cc == None: |
---|
1300 | if do_count: |
---|
1301 | cc = collections.defaultdict( col_count ) |
---|
1302 | elif do_list: |
---|
1303 | cc = collections.defaultdict( col_list ) |
---|
1304 | |
---|
1305 | if do_count: |
---|
1306 | for l in ll: |
---|
1307 | if cdict != None: |
---|
1308 | v = cdict[l.uid] |
---|
1309 | elif acount == '__number__': |
---|
1310 | v = 1 |
---|
1311 | else: |
---|
1312 | v = l.__dict__[acount] |
---|
1313 | |
---|
1314 | cc[ l.__dict__[a1] ].a[ l.__dict__[a2] ] += v |
---|
1315 | elif do_list: |
---|
1316 | for l in ll: |
---|
1317 | if cdict != None: |
---|
1318 | v = cdict[l.uid] |
---|
1319 | elif alist == '__item__': |
---|
1320 | v = l |
---|
1321 | else: |
---|
1322 | v = l.__dict__[alist] |
---|
1323 | cc[ l.__dict__[a1] ].a[ l.__dict__[a2] ].append( v ) |
---|
1324 | |
---|
1325 | od = {} |
---|
1326 | for k in cc.keys(): |
---|
1327 | d2 = {} |
---|
1328 | for k2 in cc[k].a.keys(): |
---|
1329 | d2[k2] = cc[k].a[k2] |
---|
1330 | od[k] = d2 |
---|
1331 | return od |
---|
1332 | |
---|
1333 | class dreqUI(object): |
---|
1334 | """Data Request Command line. |
---|
1335 | ------------------------- |
---|
1336 | -v : print version and exit; |
---|
1337 | --unitTest : run some simple tests; |
---|
1338 | -m <mip>: MIP of list of MIPs (comma separated; for objective selection see note [1] below); |
---|
1339 | -l <options>: List for options: |
---|
1340 | o: objectives |
---|
1341 | e: experiments |
---|
1342 | -q <options>: List information about the schema: |
---|
1343 | s: sections |
---|
1344 | <section>: attributes for a section |
---|
1345 | <section:attribute>: definition of an attribute. |
---|
1346 | -h : help: print help text; |
---|
1347 | -e <expt>: experiment; |
---|
1348 | -t <tier> maxmum tier; |
---|
1349 | -p <priority> maximum priority; |
---|
1350 | --xls : Create Excel file with requested variables; |
---|
1351 | --sf : Print summary of variable count by structure and frequency; |
---|
1352 | --SF : Print summary of variable count by structure and frequency for all MIPs; |
---|
1353 | --grdpol <native|1deg> : policy for default grid, if MIPs have not expressed a preference; |
---|
1354 | --allgrd : When a variable is requested on multiple grids, archive all grids requested (default: only the finest resolution); |
---|
1355 | --unique : List only variables which are not requested uniquely by this MIP, for at least one experiment; |
---|
1356 | --txt : Create text file with requested variables; |
---|
1357 | --mcfg : Model configuration: 7 integers, comma separated, 'nho','nlo','nha','nla','nlas','nls','nh1' |
---|
1358 | default: 259200,60,64800,40,20,5,100 |
---|
1359 | --txtOpts : options for content of text file: (v|c)[(+|-)att1[,att2[...]]] |
---|
1360 | --xlsDir <directory> : Directory in which to place variable listing [xls]; |
---|
1361 | --printLinesMax <n> : Maximum number of lines to be printed (default 20) |
---|
1362 | --printVars : If present, a summary of the variables (see --printLinesMax) fitting the selection options will be printed |
---|
1363 | --intersection : Analyse the intersection of requests rather than union. |
---|
1364 | |
---|
1365 | NOTES |
---|
1366 | ----- |
---|
1367 | [1] A set of objectives within a MIP can be specified in the command line. The extended syntax of the "-m" argument is: |
---|
1368 | -m <mip>[:objective[.obj2[.obj3 ...]]][,<mip2]...] |
---|
1369 | |
---|
1370 | e.g. |
---|
1371 | drq -m HighResMIP:Ocean.DiurnalCycle |
---|
1372 | """ |
---|
1373 | def __init__(self,args): |
---|
1374 | self.adict = {} |
---|
1375 | self.knownargs = {'-m':('m',True), '-p':('p',True), '-e':('e',True), '-t':('t',True), \ |
---|
1376 | '-h':('h',False), '--printLinesMax':('plm',True), \ |
---|
1377 | '-l':('l',True), |
---|
1378 | '-q':('q',True), |
---|
1379 | '--printVars':('vars',False), '--intersection':('intersection',False), \ |
---|
1380 | '--count':('count',False), \ |
---|
1381 | '--txt':('txt',False), \ |
---|
1382 | '--sf':('sf',False), \ |
---|
1383 | '--SF':('SF',False), \ |
---|
1384 | '--grdpol':('grdpol',True), \ |
---|
1385 | '--allgrd':('allgrd',False), \ |
---|
1386 | '--unique':('unique',False), \ |
---|
1387 | '--mcfg':('mcfg',True), \ |
---|
1388 | '--txtOpts':('txtOpts',True), \ |
---|
1389 | '--xlsDir':('xlsdir',True), '--xls':('xls',False) \ |
---|
1390 | } |
---|
1391 | aa = args[:] |
---|
1392 | notKnownArgs = [] |
---|
1393 | while len(aa) > 0: |
---|
1394 | a = aa.pop(0) |
---|
1395 | if a in self.knownargs: |
---|
1396 | b = self.knownargs[a][0] |
---|
1397 | if self.knownargs[a][1]: |
---|
1398 | v = aa.pop(0) |
---|
1399 | self.adict[b] = v |
---|
1400 | else: |
---|
1401 | self.adict[b] = True |
---|
1402 | else: |
---|
1403 | notKnownArgs.append(a) |
---|
1404 | |
---|
1405 | assert self.checkArgs( notKnownArgs ), 'FATAL ERROR 001: Arguments not recognised: %s' % (str(notKnownArgs) ) |
---|
1406 | |
---|
1407 | if 'm' in self.adict: |
---|
1408 | if self.adict['m'] == '_all_': |
---|
1409 | pass |
---|
1410 | elif self.adict['m'].find( ':' ) != -1: |
---|
1411 | ee = {} |
---|
1412 | for i in self.adict['m'].split(','): |
---|
1413 | bits = i.split( ':' ) |
---|
1414 | if len( bits ) == 1: |
---|
1415 | ee[bits[0]] = [] |
---|
1416 | else: |
---|
1417 | assert len(bits) == 2, 'Cannot parse %s' % self.adict['m'] |
---|
1418 | ee[bits[0]] = bits[1].split( '.' ) |
---|
1419 | self.adict['m'] = ee |
---|
1420 | else: |
---|
1421 | self.adict['m'] = set(self.adict['m'].split(',') ) |
---|
1422 | |
---|
1423 | if 'grdpol' in self.adict: |
---|
1424 | assert self.adict['grdpol'] in ['native','1deg'], 'Grid policy argument --grdpol must be native or 1deg : %s' % self.adict['grdpol'] |
---|
1425 | |
---|
1426 | integerArgs = set( ['p','t','plm'] ) |
---|
1427 | for i in integerArgs.intersection( self.adict ): |
---|
1428 | self.adict[i] = int( self.adict[i] ) |
---|
1429 | |
---|
1430 | self.intersection = self.adict.get( 'intersection', False ) |
---|
1431 | |
---|
1432 | |
---|
1433 | def checkArgs( self, notKnownArgs ): |
---|
1434 | if len( notKnownArgs ) == 0: |
---|
1435 | return True |
---|
1436 | print ('--------------------------------------') |
---|
1437 | print ('------------ %s Arguments Not Recognised ------------' % len(notKnownArgs) ) |
---|
1438 | k = 0 |
---|
1439 | for x in notKnownArgs: |
---|
1440 | k += 1 |
---|
1441 | if x[1:] in self.knownargs: |
---|
1442 | print ( '%s PERHAPS %s instead of %s' % (k, x[1:],x) ) |
---|
1443 | elif '-%s' % x in self.knownargs: |
---|
1444 | print ( '%s PERHAPS -%s instead of %s' % (k, x,x) ) |
---|
1445 | elif x[0] == '\xe2': |
---|
1446 | print ( '%s POSSIBLY -- (double hyphen) instead of long dash in %s' % (k, x) ) |
---|
1447 | print ('--------------------------------------') |
---|
1448 | |
---|
1449 | return len( notKnownArgs ) == 0 |
---|
1450 | |
---|
1451 | def run(self, dq=None): |
---|
1452 | if 'h' in self.adict: |
---|
1453 | mlg.prnt ( self.__doc__ ) |
---|
1454 | return |
---|
1455 | |
---|
1456 | if 'q' in self.adict: |
---|
1457 | if dq == None: |
---|
1458 | dq = dreq.loadDreq(configOnly=True) |
---|
1459 | s = self.adict['q'] |
---|
1460 | if self.adict['q'] == 's': |
---|
1461 | ss = sorted( [(i.title,i.label) for i in dq.coll['__sect__'].items] ) |
---|
1462 | for s in ss: |
---|
1463 | mlg.prnt( '%16s:: %s' % (s[1],s[0]) ) |
---|
1464 | else: |
---|
1465 | ss = [i.label for i in dq.coll['__sect__'].items] |
---|
1466 | if s.find( ':' ) != -1: |
---|
1467 | s,a = s.split( ':' ) |
---|
1468 | else: |
---|
1469 | a = None |
---|
1470 | if s not in ss: |
---|
1471 | mlg.prnt( 'ERROR: option must be a section; use "-q s" to list sections' ) |
---|
1472 | elif a == None: |
---|
1473 | x = [i for i in dq.coll['__sect__'].items if i.label == s] |
---|
1474 | s1 = [i for i in dq.coll['__main__'].items if 'ATTRIBUTE::%s' % s in i.uid] |
---|
1475 | mlg.prnt( x[0].title ) |
---|
1476 | mlg.prnt( ' '.join( sorted ([i.label for i in s1] ) )) |
---|
1477 | else: |
---|
1478 | x = [i for i in dq.coll['__main__'].items if i.uid == 'ATTRIBUTE::%s.%s' % (s,a) ] |
---|
1479 | if len(x) == 0: |
---|
1480 | mlg.prnt( 'ERROR: attribute not found' ) |
---|
1481 | s1 = [i for i in dq.coll['__main__'].items if 'ATTRIBUTE::%s' % s in i.uid] |
---|
1482 | mlg.prnt( 'ATTRIBUTES: ' + ' '.join( sorted ([i.label for i in s1] ) )) |
---|
1483 | else: |
---|
1484 | mlg.prnt( 'Section %s, attribute %s' % (s,a) ) |
---|
1485 | mlg.prnt( x[0].title ) |
---|
1486 | mlg.prnt( x[0].description ) |
---|
1487 | return |
---|
1488 | |
---|
1489 | if not ('m' in self.adict or 'SF' in self.adict): |
---|
1490 | mlg.prnt ( 'Current version requires -m or --SF argument' ) |
---|
1491 | mlg.prnt ( self.__doc__ ) |
---|
1492 | sys.exit(0) |
---|
1493 | |
---|
1494 | if dq == None: |
---|
1495 | self.dq = dreq.loadDreq() |
---|
1496 | else: |
---|
1497 | self.dq = dq |
---|
1498 | |
---|
1499 | if 'l' in self.adict: |
---|
1500 | self.printList() |
---|
1501 | return |
---|
1502 | |
---|
1503 | if 'mcfg' in self.adict: |
---|
1504 | ll = string.split( self.adict['mcfg'], ',' ) |
---|
1505 | assert len(ll) == 7, 'Length of model configuration argument must be 7 comma separated integers: %s' % self.adict['mcfg'] |
---|
1506 | lli = [ int(x) for x in ll] |
---|
1507 | |
---|
1508 | self.sc = dreqQuery( dq=self.dq ) |
---|
1509 | |
---|
1510 | if 'grdpol' in self.adict: |
---|
1511 | self.sc.gridPolicyDefaultNative = self.adict['grdpol'] == 'native' |
---|
1512 | print ( 'SETTING grid policy: %s' % self.sc.gridPolicyDefaultNative ) |
---|
1513 | if 'allgrd' in self.adict: |
---|
1514 | self.sc.gridPolicyTopOnly = False |
---|
1515 | print ( 'SETTING grid policy for multiple preferred grids: %s' % self.sc.gridPolicyTopOnly ) |
---|
1516 | if 'unique' in self.adict: |
---|
1517 | self.sc.uniqueRequest = True |
---|
1518 | |
---|
1519 | if 'mcfg' in self.adict: |
---|
1520 | self.sc.setMcfg( lli ) |
---|
1521 | |
---|
1522 | tierMax = self.adict.get( 't', 1 ) |
---|
1523 | self.sc.setTierMax( tierMax ) |
---|
1524 | pmax = self.adict.get( 'p', 1 ) |
---|
1525 | |
---|
1526 | makeXls = self.adict.get( 'xls', False ) |
---|
1527 | makeTxt = self.adict.get( 'txt', False ) |
---|
1528 | doSf = 'SF' in self.adict or 'sf' in self.adict |
---|
1529 | if makeXls or makeTxt or doSf: |
---|
1530 | xlsOdir = self.adict.get( 'xlsdir', 'xls' ) |
---|
1531 | self.sc.checkDir( xlsOdir, 'xls files' ) |
---|
1532 | |
---|
1533 | if 'SF' in self.adict: |
---|
1534 | import volsum |
---|
1535 | self.sc.gridPolicyDefaultNative = True |
---|
1536 | vs = volsum.vsum( self.sc, odsz, npy, odir=xlsOdir ) |
---|
1537 | vs.analAll(pmax) |
---|
1538 | |
---|
1539 | self.sc.gridPolicyDefaultNative = False |
---|
1540 | vs = volsum.vsum( self.sc, odsz, npy, odir=xlsOdir ) |
---|
1541 | vs.analAll(pmax) |
---|
1542 | |
---|
1543 | self.sc.setTierMax( 3 ) |
---|
1544 | vs = volsum.vsum( self.sc, odsz, npy, odir=xlsOdir ) |
---|
1545 | vs.analAll(3) |
---|
1546 | return |
---|
1547 | |
---|
1548 | ok = True |
---|
1549 | if self.adict['m'] == '_all_': |
---|
1550 | self.adict['m'] = set(self.sc.mips ) |
---|
1551 | mlab = 'TOTAL' |
---|
1552 | else: |
---|
1553 | for i in self.adict['m']: |
---|
1554 | if i not in self.sc.mips: |
---|
1555 | ok = False |
---|
1556 | mlg.prnt ( 'NOT FOUND: %s' % i ) |
---|
1557 | mlab = makeTables.setMlab( self.adict['m'] ) |
---|
1558 | assert ok,'Available MIPs: %s' % str(self.sc.mips) |
---|
1559 | |
---|
1560 | if 'sf' in self.adict: |
---|
1561 | import volsum |
---|
1562 | vs = volsum.vsum( self.sc, odsz, npy, odir=xlsOdir ) |
---|
1563 | vs.run( self.adict['m'], 'requestVol_%s_%s_%s' % (mlab,tierMax,pmax), pmax=pmax ) |
---|
1564 | vs.anal(olab=mlab,doUnique=False) |
---|
1565 | ttl = sum( [x for k,x in vs.res['vu'].items()] )*2.*1.e-12 |
---|
1566 | ttl2 = sum( [x for k,x in vs.res['vu'].items()] )*2.*1.e-12 |
---|
1567 | mlg.prnt( 'TOTAL volume: %8.2fTb' % ttl ) |
---|
1568 | return |
---|
1569 | |
---|
1570 | |
---|
1571 | eid = None |
---|
1572 | ex = None |
---|
1573 | if 'e' in self.adict: |
---|
1574 | ex = self.adict['e'] |
---|
1575 | for i in self.dq.coll['experiment'].items: |
---|
1576 | if i.label == self.adict['e']: |
---|
1577 | eid = i.uid |
---|
1578 | assert eid != None, 'Experiment %s not found' % self.adict['e'] |
---|
1579 | |
---|
1580 | adsCount = self.adict.get( 'count', False ) |
---|
1581 | |
---|
1582 | self.getVolByMip(pmax,eid,adsCount) |
---|
1583 | makeXls = self.adict.get( 'xls', False ) |
---|
1584 | makeTxt = self.adict.get( 'txt', False ) |
---|
1585 | if makeXls or makeTxt: |
---|
1586 | mips = self.adict['m'] |
---|
1587 | |
---|
1588 | if 'txtOpts' in self.adict: |
---|
1589 | if self.adict['txtOpts'][0] == 'v': |
---|
1590 | txtOpts = NT_txtopts( 'var' ) |
---|
1591 | else: |
---|
1592 | txtOpts = NT_txtopts( 'cmv' ) |
---|
1593 | else: |
---|
1594 | txtOpts=None |
---|
1595 | |
---|
1596 | self.sc.xlsByMipExpt(mips,eid,pmax,odir=xlsOdir,xls=makeXls,txt=makeTxt,txtOpts=txtOpts) |
---|
1597 | |
---|
1598 | def printList(self): |
---|
1599 | mips = self.adict['m'] |
---|
1600 | ee = {} |
---|
1601 | for i in self.dq.coll['mip'].items: |
---|
1602 | if i.label in mips: |
---|
1603 | ee[i.label] = i |
---|
1604 | if self.adict['l'] in ['o','e']: |
---|
1605 | targ = {'o':'objective', 'e':'experiment' }[self.adict['l']] |
---|
1606 | for k in sorted( ee.keys() ): |
---|
1607 | if targ in self.dq.inx.iref_by_sect[ee[k].uid].a: |
---|
1608 | for u in self.dq.inx.iref_by_sect[ee[k].uid].a[targ]: |
---|
1609 | print ( '%s: %s' % (ee[k].label, self.dq.inx.uid[u].label) ) |
---|
1610 | else: |
---|
1611 | print ('list objective *%s* not recognised (should be e or o)' % self.adict['l'] ) |
---|
1612 | |
---|
1613 | def getVolByMip(self,pmax,eid,adsCount): |
---|
1614 | |
---|
1615 | v0 = self.sc.volByMip( self.adict['m'], pmax=pmax, intersection=self.intersection, adsCount=adsCount, exptid=eid ) |
---|
1616 | mlg.prnt ( 'getVolByMip: %s [%s]' % (v0,makeTables.vfmt(v0*2.)) ) |
---|
1617 | cc = collections.defaultdict( int ) |
---|
1618 | for e in self.sc.volByE: |
---|
1619 | for v in self.sc.volByE[e][2]: |
---|
1620 | cc[v] += self.sc.volByE[e][2][v] |
---|
1621 | x = 0 |
---|
1622 | for v in cc: |
---|
1623 | x += cc[v] |
---|
1624 | |
---|
1625 | if python2: |
---|
1626 | vl = sorted( cc.keys(), cmp=cmpd(cc).cmp, reverse=True ) |
---|
1627 | else: |
---|
1628 | vl = sorted( cc.keys(), key=lambda x: cc[x], reverse=True ) |
---|
1629 | if self.adict.get( 'vars', False ): |
---|
1630 | printLinesMax = self.adict.get( 'plm', 20 ) |
---|
1631 | if printLinesMax > 0: |
---|
1632 | mx = min( [printLinesMax,len(vl)] ) |
---|
1633 | else: |
---|
1634 | mx = len(vl) |
---|
1635 | |
---|
1636 | for v in vl[:mx]: |
---|
1637 | mlg.prnt ( '%s.%s: %s' % (self.dq.inx.uid[v].mipTable,self.dq.inx.uid[v].label, makeTables.vfmt( cc[v]*2. ) ) ) |
---|
1638 | if mx < len(vl): |
---|
1639 | mlg.prnt ( '%s variables not listed (use --printLinesMax to print more)' % (len(vl)-mx) ) |
---|
1640 | |
---|