1 | import collections, os, sys |
---|
2 | |
---|
3 | try: |
---|
4 | import xlsxwriter |
---|
5 | except: |
---|
6 | print ('No xlsxwrite: will not make tables ...') |
---|
7 | |
---|
8 | try: |
---|
9 | import vrev |
---|
10 | import misc_utils |
---|
11 | except: |
---|
12 | import dreqPy.vrev as vrev |
---|
13 | import dreqPy.misc_utils as misc_utils |
---|
14 | |
---|
15 | def realmFlt( ss ): |
---|
16 | if ss == '': |
---|
17 | return ss |
---|
18 | if ss.find( ' ' ) == -1: |
---|
19 | return ss |
---|
20 | return ss.split( ' ' )[0] |
---|
21 | |
---|
22 | python2 = True |
---|
23 | if sys.version_info[0] == 3: |
---|
24 | python2 = False |
---|
25 | def cmp(x,y): |
---|
26 | if x == y: |
---|
27 | return 0 |
---|
28 | elif x > y: |
---|
29 | return 1 |
---|
30 | else: |
---|
31 | return -1 |
---|
32 | |
---|
33 | if sys.version_info >= (2,7): |
---|
34 | from functools import cmp_to_key |
---|
35 | oldpython = False |
---|
36 | else: |
---|
37 | oldpython = True |
---|
38 | |
---|
39 | class cmpdn(object): |
---|
40 | def __init__(self,kl): |
---|
41 | self.kl = kl |
---|
42 | def cmp(self,x,y): |
---|
43 | for k in self.kl: |
---|
44 | if x.__dict__[k] != y.__dict__[k]: |
---|
45 | return cmp( x.__dict__[k], y.__dict__[k] ) |
---|
46 | |
---|
47 | return cmp( 0,0 ) |
---|
48 | |
---|
49 | def cmpAnnex( x, y ): |
---|
50 | ax = len(x) > 2 and x[:2] == 'em' |
---|
51 | ay = len(y) > 2 and y[:2] == 'em' |
---|
52 | bx = len(x) > 5 and x[:5] in ['CMIP5','CORDE','SPECS'] |
---|
53 | by = len(y) > 5 and y[:5] in ['CMIP5','CORDE','SPECS'] |
---|
54 | if ax == ay and bx == by: |
---|
55 | return cmp(x,y) |
---|
56 | elif ax: |
---|
57 | if by: |
---|
58 | return cmp(0,1) |
---|
59 | else: |
---|
60 | return cmp(1,0) |
---|
61 | elif ay: |
---|
62 | if bx: |
---|
63 | return cmp(1,0) |
---|
64 | else: |
---|
65 | return cmp(0,1) |
---|
66 | elif bx: |
---|
67 | return cmp(1,0) |
---|
68 | else: |
---|
69 | return cmp(0,1) |
---|
70 | |
---|
71 | if not oldpython: |
---|
72 | kAnnex = cmp_to_key( cmpAnnex ) |
---|
73 | kCmpdn = cmp_to_key( cmpdn(['sn','label']).cmp ) |
---|
74 | kCmpdnPrl = cmp_to_key( cmpdn(['prov','rowIndex','label']).cmp ) |
---|
75 | |
---|
76 | |
---|
77 | class xlsx(object): |
---|
78 | def __init__(self,fn,xls=True,txt=False,txtOpts=None): |
---|
79 | self.xls=xls |
---|
80 | self.txt=txt |
---|
81 | self.txtOpts = txtOpts |
---|
82 | self.mcfgNote = 'Reference Volume (1 deg. atmosphere, 0.5 deg. ocean)' |
---|
83 | if xls: |
---|
84 | self.wb = xlsxwriter.Workbook('%s.xlsx' % fn) |
---|
85 | self.hdr_cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#aaaacc'}) |
---|
86 | self.hdr_cell_format.set_text_wrap() |
---|
87 | self.sect_cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#ccccbb'}) |
---|
88 | self.sect_cell_format.set_text_wrap() |
---|
89 | self.cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 11}) |
---|
90 | self.cell_format.set_text_wrap() |
---|
91 | |
---|
92 | if txt: |
---|
93 | self.oo = open( '%s.csv' % fn, 'w' ) |
---|
94 | |
---|
95 | def header(self,tableNotes,collected): |
---|
96 | if self.xls: |
---|
97 | sht = self.newSheet( 'Notes' ) |
---|
98 | sht.write( 0,0, '', self.hdr_cell_format ) |
---|
99 | sht.write( 0,1, 'Notes on tables', self.hdr_cell_format ) |
---|
100 | ri = 0 |
---|
101 | sht.set_column(0,0,30) |
---|
102 | sht.set_column(1,1,60) |
---|
103 | self.sht = sht |
---|
104 | for t in tableNotes: |
---|
105 | ri += 1 |
---|
106 | for i in range(2): |
---|
107 | sht.write( ri,i, t[i], self.cell_format ) |
---|
108 | |
---|
109 | if collected != None: |
---|
110 | ri += 2 |
---|
111 | sht.write( ri, 0, 'Table', self.sect_cell_format ) |
---|
112 | sht.write( ri, 1, self.mcfgNote, self.sect_cell_format ) |
---|
113 | ttl = 0. |
---|
114 | for k in sorted( collected.keys() ): |
---|
115 | ri += 1 |
---|
116 | sht.write( ri, 0, k ) |
---|
117 | sht.write( ri, 1, misc_utils.vfmt( collected[k]*2. ) ) |
---|
118 | ttl += collected[k] |
---|
119 | |
---|
120 | ri += 1 |
---|
121 | sht.write( ri, 0, 'TOTAL' ) |
---|
122 | sht.write( ri, 1, misc_utils.vfmt( ttl*2. ) ) |
---|
123 | |
---|
124 | if self.txt: |
---|
125 | self.oo.write( '\t'.join( ['Notes','','Notes on tables']) + '\n' ) |
---|
126 | for t in tableNotes: |
---|
127 | self.oo.write( '\t'.join( ['Notes',] + list(t)) + '\n' ) |
---|
128 | |
---|
129 | if collected != None: |
---|
130 | self.oo.write( '\t'.join( ['Notes','Table','Reference Volume (1 deg. atmosphere, 0.5 deg. ocean)']) + '\n') |
---|
131 | for k in sorted( collected.keys() ): |
---|
132 | self.oo.write( '\t'.join( ['Notes',k,misc_utils.vfmt( collected[k]*2. )]) + '\n' ) |
---|
133 | |
---|
134 | def cmvtabrec(self,j,t,orec): |
---|
135 | if self.xls: |
---|
136 | for i in range(len(orec)): |
---|
137 | if str( type(orec[i]) ) == "<class 'dreq.dreqItem_CoreAttributes'>": |
---|
138 | self.sht.write( j,i, '', self.cell_format ) |
---|
139 | else: |
---|
140 | ##print i, orec[i], type( orec[i] ) |
---|
141 | try: |
---|
142 | self.sht.write( j,i, orec[i], self.cell_format ) |
---|
143 | except: |
---|
144 | print ('FAILED TO WRITE RECORD: %s' % str(orec)) |
---|
145 | print ('FAILED TO WRITE RECORD: %s' % str(orec[i])) |
---|
146 | raise |
---|
147 | |
---|
148 | if self.txt: |
---|
149 | self.oo.write( '\t'.join( [t,] + [x.replace('"',"'") for x in orec]) + '\n' ) |
---|
150 | |
---|
151 | def varrec(self,j,orec): |
---|
152 | if self.xls: |
---|
153 | for i in range(len(orec)): |
---|
154 | self.sht.write( j,i, orec[i], self.cell_format ) |
---|
155 | |
---|
156 | if self.txt: |
---|
157 | self.oo.write( '\t'.join( orec, '\t') + '\n' ) |
---|
158 | |
---|
159 | def var(self): |
---|
160 | if self.xls: |
---|
161 | self.sht = self.newSheet( 'var' ) |
---|
162 | j = 0 |
---|
163 | hrec = ['Long name', 'units', 'description', 'Variable Name', 'CF Standard Name' ] |
---|
164 | if self.xls: |
---|
165 | self.sht.set_column(1,1,40) |
---|
166 | self.sht.set_column(1,2,30) |
---|
167 | self.sht.set_column(1,3,60) |
---|
168 | self.sht.set_column(1,4,40) |
---|
169 | self.sht.set_column(1,5,40) |
---|
170 | self.sht.set_column(1,20,40) |
---|
171 | self.sht.set_column(1,21,40) |
---|
172 | |
---|
173 | if self.xls: |
---|
174 | for i in range(len(hrec)): |
---|
175 | self.sht.write( j,i, hrec[i], self.hdr_cell_format ) |
---|
176 | |
---|
177 | if self.txt: |
---|
178 | for i in range(len(hrec)): |
---|
179 | self.oo.write( hrec[i] + '\t' ) |
---|
180 | self.oo.write( '\n' ) |
---|
181 | |
---|
182 | def cmvtab(self,t,addMips,mode='c',tslice=False,byFreqRealm=False,truePriority=False): |
---|
183 | if self.xls: |
---|
184 | self.sht = self.newSheet( t ) |
---|
185 | j = 0 |
---|
186 | ncga = 'NetCDF Global Attribute' |
---|
187 | if mode == 'c': |
---|
188 | hrec = ['Default Priority','Long name', 'units', 'description', 'comment', 'Variable Name', 'CF Standard Name', 'cell_methods', 'positive', 'type', 'dimensions', 'CMOR Name', 'modeling_realm', 'frequency', 'cell_measures', 'prov', 'provNote','rowIndex','UID','vid','stid','Structure Title','valid_min', 'valid_max', 'ok_min_mean_abs', 'ok_max_mean_abs'] |
---|
189 | hcmt = ['Default priority (generally overridden by settings in "requestVar" record)',ncga,'','','Name of variable in file','','','CMOR directive','','','CMOR name, unique within table','','','','','','','','','','CMOR variable identifier','MIP variable identifier','Structure identifier','','','','',''] |
---|
190 | if truePriority: |
---|
191 | hrec[0] = 'Priority' |
---|
192 | hcmt[0] = 'Lowest priority value set in request for this variable for this experiment' |
---|
193 | if self.xls: |
---|
194 | self.sht.set_column(1,1,40) |
---|
195 | self.sht.set_column(1,3,50) |
---|
196 | self.sht.set_column(1,4,30) |
---|
197 | self.sht.set_column(1,5,50) |
---|
198 | self.sht.set_column(1,6,30) |
---|
199 | self.sht.set_column(1,9,40) |
---|
200 | self.sht.set_column(1,18,40) |
---|
201 | self.sht.set_column(1,19,40) |
---|
202 | self.sht.set_column(1,20,40) |
---|
203 | self.sht.set_column(1,21,40) |
---|
204 | self.sht.set_column(1,26,40) |
---|
205 | self.sht.set_column(1,27,40) |
---|
206 | self.sht.set_column(1,30,40) |
---|
207 | else: |
---|
208 | hrec = ['','Long name', 'units', 'description', '', 'Variable Name', 'CF Standard Name', '','', 'cell_methods', 'valid_min', 'valid_max', 'ok_min_mean_abs', 'ok_max_mean_abs', 'positive', 'type', 'dimensions', 'CMOR name', 'modeling_realm', 'frequency', 'cell_measures', 'flag_values', 'flag_meanings', 'prov', 'provNote','rowIndex','UID'] |
---|
209 | if addMips: |
---|
210 | hrec.append( 'MIPs (requesting)' ) |
---|
211 | hrec.append( 'MIPs (by experiment)' ) |
---|
212 | |
---|
213 | if byFreqRealm: |
---|
214 | hrec = ['Table',] + hrec |
---|
215 | hcmt = ['CMOR table',] + hcmt |
---|
216 | if tslice: |
---|
217 | hrec += ['Number of Years','Slice Type','Years','Grid'] |
---|
218 | hcmt += ['','','',''] |
---|
219 | |
---|
220 | if self.xls: |
---|
221 | for i in range(len(hrec)): |
---|
222 | self.sht.write( j,i, hrec[i], self.hdr_cell_format ) |
---|
223 | if hcmt[i] != '': |
---|
224 | self.sht.write_comment( j,i,hcmt[i]) |
---|
225 | |
---|
226 | if self.txt: |
---|
227 | self.oo.write( 'MIP table\t' ) |
---|
228 | for i in range(len(hrec)): |
---|
229 | self.oo.write( hrec[i] + '\t' ) |
---|
230 | self.oo.write( '\n' ) |
---|
231 | self.oo.write( t + '\t' ) |
---|
232 | for i in range(len(hrec)): |
---|
233 | if hcmt[i] != '': |
---|
234 | self.oo.write( hcmt[i] + '\t') |
---|
235 | else: |
---|
236 | self.oo.write( '\t') |
---|
237 | self.oo.write( '\n' ) |
---|
238 | |
---|
239 | def newSheet(self,name): |
---|
240 | self.worksheet = self.wb.add_worksheet(name=name) |
---|
241 | return self.worksheet |
---|
242 | |
---|
243 | def close(self): |
---|
244 | if self.xls: |
---|
245 | self.wb.close() |
---|
246 | if self.txt: |
---|
247 | self.oo.close() |
---|
248 | |
---|
249 | ### |
---|
250 | ### need to have name of experiment here, for the aggregation over MIPs to work ... in the column of request by MIPs |
---|
251 | ### |
---|
252 | class makeTab(object): |
---|
253 | def __init__(self, sc, subset=None, mcfgNote=None, dest='tables/test', skipped=set(), collected=None,xls=True,txt=False,txtOpts=None,byFreqRealm=False, tslice=None, exptUid=None, tabMode=None, pdict=None): |
---|
254 | """txtOpts: gives option to list MIP variables instead of CMOR variables""" |
---|
255 | self.sc = sc |
---|
256 | dq = sc.dq |
---|
257 | if subset != None: |
---|
258 | cmv = [x for x in dq.coll['CMORvar'].items if x.uid in subset] |
---|
259 | else: |
---|
260 | cmv = dq.coll['CMORvar'].items |
---|
261 | self.byFreqRealm=byFreqRealm |
---|
262 | |
---|
263 | ixt = collections.defaultdict(list) |
---|
264 | if not byFreqRealm: |
---|
265 | for i in cmv: |
---|
266 | ixt[i.mipTable].append( i.uid ) |
---|
267 | else: |
---|
268 | for i in cmv: |
---|
269 | ixt['%s.%s' % (i.frequency,realmFlt( i.modeling_realm) )].append( i.uid ) |
---|
270 | |
---|
271 | if oldpython: |
---|
272 | tables = sorted( ixt.keys(), cmp=cmpAnnex ) |
---|
273 | else: |
---|
274 | tables = sorted( ixt.keys(), key=kAnnex ) |
---|
275 | |
---|
276 | addMips = True |
---|
277 | if addMips: |
---|
278 | chkv = vrev.checkVar(dq) |
---|
279 | chkv.sc = self.sc |
---|
280 | mode = 'c' |
---|
281 | tableNotes = [ |
---|
282 | ('Request Version',str(dq.version)), |
---|
283 | ('MIPs (...)','The last two columns in each row list MIPs associated with each variable. The first column in this pair lists the MIPs which are requesting the variable in one or more experiments. The second column lists the MIPs proposing experiments in which this variable is requested. E.g. If a variable is requested in a DECK experiment by HighResMIP, then HighResMIP appears in the first column and DECK in the second')] |
---|
284 | |
---|
285 | wb = xlsx( dest, xls=xls, txt=txt ) |
---|
286 | if mcfgNote != None: |
---|
287 | wb.mcfgNote = mcfgNote |
---|
288 | wb.header( tableNotes, collected) |
---|
289 | truePriority = tabMode == 'e' and pdict != None |
---|
290 | |
---|
291 | if txtOpts != None and txtOpts.mode == 'var': |
---|
292 | vl = list( set( [v.vid for v in cmv] ) ) |
---|
293 | vli = [dq.inx.uid[i] for i in vl] |
---|
294 | if oldpython: |
---|
295 | thisvli = sorted( vli, cmp=cmpdn(['sn','label']).cmp ) |
---|
296 | else: |
---|
297 | thisvli = sorted( vli, key=kCmpdn ) |
---|
298 | wb.var() |
---|
299 | |
---|
300 | j = 0 |
---|
301 | for v in thisvli: |
---|
302 | ###hrec = ['Long name', 'units', 'description', 'Variable Name', 'CF Standard Name' ] |
---|
303 | orec = [v.title, v.units, v.description, v.label, v.sn] |
---|
304 | j += 1 |
---|
305 | wb.varrec( j,orec ) |
---|
306 | else: |
---|
307 | withoo = False |
---|
308 | for t in tables: |
---|
309 | if withoo: |
---|
310 | oo = open( 'tables/test_%s.csv' % t, 'w' ) |
---|
311 | wb.cmvtab(t,addMips,mode='c',tslice=tslice != None,byFreqRealm=byFreqRealm,truePriority = truePriority) |
---|
312 | |
---|
313 | j = 0 |
---|
314 | if oldpython: |
---|
315 | thiscmvlist = sorted( [dq.inx.uid[u] for u in ixt[t]], cmp=cmpdn(['prov','rowIndex','label']).cmp ) |
---|
316 | else: |
---|
317 | thiscmvlist = sorted( [dq.inx.uid[u] for u in ixt[t]], key=kCmpdnPrl ) |
---|
318 | |
---|
319 | for cmv in thiscmvlist: |
---|
320 | var = dq.inx.uid[ cmv.vid ] |
---|
321 | strc = dq.inx.uid[ cmv.stid ] |
---|
322 | if strc._h.label == 'remarks': |
---|
323 | print ( 'ERROR: structure not found for %s: %s .. %s (%s)' % (cmv.uid,cmv.label,cmv.title,cmv.mipTable) ) |
---|
324 | ok = False |
---|
325 | else: |
---|
326 | sshp = dq.inx.uid[ strc.spid ] |
---|
327 | tshp = dq.inx.uid[ strc.tmid ] |
---|
328 | ok = all( [i._h.label != 'remarks' for i in [var,strc,sshp,tshp]] ) |
---|
329 | #[u'shuffle', u'ok_max_mean_abs', u'vid', '_contentInitialised', u'valid_min', u'frequency', u'uid', u'title', u'rowIndex', u'positive', u'stid', u'mipTable', u'label', u'type', u'description', u'deflate_level', u'deflate', u'provNote', u'ok_min_mean_abs', u'modeling_realm', u'prov', u'valid_max'] |
---|
330 | |
---|
331 | if not ok: |
---|
332 | if (t,cmv.label) not in skipped: |
---|
333 | ml = [] |
---|
334 | for i in range(4): |
---|
335 | ii = [var,strc,sshp,tshp][i] |
---|
336 | if ii._h.label == 'remarks': |
---|
337 | ml.append( ['var','struct','time','spatial'][i] ) |
---|
338 | print ( 'makeTables: skipping %s %s: %s' % (t,cmv.label,','.join( ml)) ) |
---|
339 | skipped.add( (t,cmv.label) ) |
---|
340 | else: |
---|
341 | dims = [] |
---|
342 | dims += sshp.dimensions.split( '|' ) |
---|
343 | if 'odims' in strc.__dict__: |
---|
344 | dims += strc.odims.split( '|' ) |
---|
345 | dims += tshp.dimensions.split( '|' ) |
---|
346 | if 'coords' in strc.__dict__: |
---|
347 | dims += strc.coords.split( '|' ) |
---|
348 | dims = ' '.join( dims ) |
---|
349 | if "qcranges" in dq.inx.iref_by_sect[cmv.uid].a: |
---|
350 | u = dq.inx.iref_by_sect[cmv.uid].a['qcranges'][0] |
---|
351 | qc = dq.inx.uid[u] |
---|
352 | ll = [] |
---|
353 | for k in ['valid_min', 'valid_max', 'ok_min_mean_abs', 'ok_max_mean_abs']: |
---|
354 | if qc.hasattr(k): |
---|
355 | ll.append( '%s %s' % (qc.__dict__[k],qc.__dict__['%s_status' % k][0]) ) |
---|
356 | else: |
---|
357 | ll.append( '' ) |
---|
358 | valid_min, valid_max, ok_min_mean_abs, ok_max_mean_abs = tuple( ll ) |
---|
359 | else: |
---|
360 | valid_min, valid_max, ok_min_mean_abs, ok_max_mean_abs = ('','','','') |
---|
361 | |
---|
362 | if mode == 'c': |
---|
363 | try: |
---|
364 | if tabMode == 'e' and pdict != None: |
---|
365 | if cmv.uid in pdict: |
---|
366 | thisp = str( min( pdict[cmv.uid] ) ) |
---|
367 | else: |
---|
368 | thisp = str(cmv.defaultPriority) |
---|
369 | print ('ERROR.priority.0101: %s, %s ' % (cmv.label,dest) ) |
---|
370 | else: |
---|
371 | thisp = str(cmv.defaultPriority) |
---|
372 | orec = [thisp,var.title, var.units, var.description, cmv.description, var.label, var.sn, strc.cell_methods, cmv.positive, cmv.type, dims, cmv.label, cmv.modeling_realm, cmv.frequency, strc.cell_measures, cmv.prov,cmv.provNote,str(cmv.rowIndex),cmv.uid,cmv.vid,cmv.stid,strc.title, valid_min, valid_max, ok_min_mean_abs, ok_max_mean_abs] |
---|
373 | except: |
---|
374 | print ('FAILED TO CONSTRUCT RECORD: %s [%s], %s [%s]' % (cmv.uid,cmv.label,var.uid,var.label) ) |
---|
375 | raise |
---|
376 | else: |
---|
377 | orec = ['',var.title, var.units, cmv.description, '', var.label, var.sn, '','', strc.cell_methods, valid_min, valid_max, ok_min_mean_abs, ok_max_mean_abs, cmv.positive, cmv.type, dims, cmv.label, cmv.modeling_realm, cmv.frequency, strc.cell_measures, strc.flag_values, strc.flag_meanings,cmv.prov,cmv.provNote,str(cmv.rowIndex),var.uid] |
---|
378 | |
---|
379 | if byFreqRealm: |
---|
380 | orec = [cmv.mipTable,] + orec |
---|
381 | |
---|
382 | ##! |
---|
383 | # CHECK -- ERROR HERE FOR "TOTAL" ROW --spurious mips in thismips --- |
---|
384 | ##! |
---|
385 | ## change "c" to something searchable |
---|
386 | if addMips: |
---|
387 | ## |
---|
388 | ## union of all mips interested in this variable |
---|
389 | ## |
---|
390 | thismips = chkv.chkCmv( cmv.uid ) |
---|
391 | ## |
---|
392 | ## all mips requesting this variable for this experiment |
---|
393 | ## |
---|
394 | thismips2 = chkv.chkCmv( cmv.uid, byExpt=True, expt=exptUid ) |
---|
395 | orec.append( ','.join( sorted( list( thismips) ) ) ) |
---|
396 | orec.append( ','.join( sorted( list( thismips2) ) ) ) |
---|
397 | |
---|
398 | if tslice != None: |
---|
399 | msgLevel = 0 |
---|
400 | if cmv.uid in tslice and msgLevel > 1: |
---|
401 | print ( 'INFO.table_utils.01001: slice 3: %s : %s' % ( str( tslice[cmv.uid] ), cmv.label ) ) |
---|
402 | if cmv.uid not in tslice: |
---|
403 | orec += ['All', '','',''] |
---|
404 | elif type( tslice[cmv.uid] ) == type( 0 ): |
---|
405 | print ( 'ERROR: unexpected tslice type: %s, %s' % (cmv.uid, tslice[cmv.uid] ) ) |
---|
406 | elif len( tslice[cmv.uid] ) == 3: |
---|
407 | x,priority,grid = tslice[cmv.uid] |
---|
408 | orec[0] = priority |
---|
409 | if x != None: |
---|
410 | tslab,tsmode,a,b = x |
---|
411 | orec += [tslab,tsmode,'',grid] |
---|
412 | else: |
---|
413 | print ( 'WARN.table_utils.01001: slice 3: %s : %s' % ( str( tslice[cmv.uid] ), cmv.label ) ) |
---|
414 | orec += ['*unknown*','','',grid] |
---|
415 | else: |
---|
416 | tslab,tsmode,a,b,priority,grid = tslice[cmv.uid] |
---|
417 | if type( priority ) != type(1): |
---|
418 | thisp = priority |
---|
419 | priority = thisp[1] |
---|
420 | orec[0] = '%s' % priority |
---|
421 | |
---|
422 | if tsmode[:4] in ['simp','bran']: |
---|
423 | nys = b + 1 - a |
---|
424 | ys = range(a,b+1) |
---|
425 | orec += [str(nys), '',str(ys)] |
---|
426 | elif tsmode in ['rangeplus']: |
---|
427 | nys = b + 1 - a + 0.01 |
---|
428 | ys = [1850,] + range(a,b+1) |
---|
429 | orec += [str(nys), 'Partial 1850 + %s to %s' % (a,b),'%6.2f' % ys] |
---|
430 | elif tsmode[:4] in ['YEAR']: |
---|
431 | nys = a |
---|
432 | ys = b |
---|
433 | orec += [str(nys), '',str(ys)] |
---|
434 | else: |
---|
435 | orec += ['slice', tslab,''] |
---|
436 | orec.append( grid ) |
---|
437 | |
---|
438 | if orec[0] in [0,'0',None]: |
---|
439 | print ('ERROR.priority.006: %s, %s ' % (orec,dest)) |
---|
440 | if withoo: |
---|
441 | oo.write( '\t'.join(orec ) + '\n' ) |
---|
442 | j+=1 |
---|
443 | wb.cmvtabrec( j,t,orec ) |
---|
444 | |
---|
445 | if withoo: |
---|
446 | oo.close() |
---|
447 | wb.close() |
---|
448 | |
---|
449 | class tables(object): |
---|
450 | def __init__(self,sc, odir='xls',xls=True,txt=False,txtOpts=None): |
---|
451 | self.sc = sc |
---|
452 | self.dq = sc.dq |
---|
453 | ##self.mips = mips |
---|
454 | self.odir = odir |
---|
455 | self.accReset() |
---|
456 | self.doXls = xls |
---|
457 | self.doTxt = txt |
---|
458 | self.txtOpts = txtOpts |
---|
459 | |
---|
460 | def accReset(self): |
---|
461 | self.acc = [0.,collections.defaultdict(int),collections.defaultdict( float ) ] |
---|
462 | |
---|
463 | def accAdd(self,x): |
---|
464 | self.acc[0] += x[0] |
---|
465 | for k in x[2]: |
---|
466 | self.acc[2][k] += x[2][k] |
---|
467 | |
---|
468 | |
---|
469 | def doTable(self,m,l1,m2,pmax,collector,acc=True, mlab=None,exptids=None,cc=None): |
---|
470 | """*acc* allows accumulation of values to be switched off when called in single expt mode""" |
---|
471 | |
---|
472 | self.verbose = False |
---|
473 | if mlab == None: |
---|
474 | mlab = misc_utils.setMlab( m ) |
---|
475 | |
---|
476 | cc0 = misc_utils.getExptSum( self.dq, mlab, l1 ) |
---|
477 | ks = sorted( list( cc0.keys() ) ) |
---|
478 | if self.verbose: |
---|
479 | print ('Experiment summary: %s %s' % (mlab,', '.join( ['%s: %s' % (k,len(cc0[k])) for k in ks] ) ) ) |
---|
480 | |
---|
481 | if m2 in [None, 'TOTAL']: |
---|
482 | x = self.acc |
---|
483 | else: |
---|
484 | x = self.sc.volByExpt( l1, m2, pmax=pmax ) |
---|
485 | |
---|
486 | ##self.volByExpt( l1, e, pmax=pmax, cc=cc, retainRedundantRank=retainRedundantRank, intersection=intersection, adsCount=adsCount ) |
---|
487 | v0 = self.sc.volByMip( m, pmax=pmax, exptid=m2 ) |
---|
488 | #### |
---|
489 | if cc==None: |
---|
490 | cc = collections.defaultdict( int ) |
---|
491 | for e in self.sc.volByE: |
---|
492 | if self.verbose: |
---|
493 | print ('INFO.mlab.... %s: %s: %s' % ( mlab, e, len( self.sc.volByE[e][2] ) ) ) |
---|
494 | for v in self.sc.volByE[e][2]: |
---|
495 | cc[v] += self.sc.volByE[e][2][v] |
---|
496 | xxx = 0 |
---|
497 | for v in cc: |
---|
498 | xxx += cc[v] |
---|
499 | #### |
---|
500 | if acc: |
---|
501 | for e in self.sc.volByE: |
---|
502 | self.accAdd(self.sc.volByE[e]) |
---|
503 | |
---|
504 | if m2 not in [ None, 'TOTAL']: |
---|
505 | im2 = self.dq.inx.uid[m2] |
---|
506 | ismip = im2._h.label == 'mip' |
---|
507 | mlab2 = im2.label |
---|
508 | |
---|
509 | x0 = 0 |
---|
510 | for e in self.sc.volByE: |
---|
511 | if exptids == None or e in exptids: |
---|
512 | x = self.sc.volByE[e] |
---|
513 | if x[0] > 0: |
---|
514 | collector[mlab].a[mlab2] += x[0] |
---|
515 | x0 += x[0] |
---|
516 | else: |
---|
517 | ismip = False |
---|
518 | mlab2 = 'TOTAL' |
---|
519 | x0 = x[0] |
---|
520 | |
---|
521 | if mlab2 == 'TOTAL' and x0 == 0: |
---|
522 | print ( 'no data detected for %s' % mlab ) |
---|
523 | |
---|
524 | if x0 > 0: |
---|
525 | # |
---|
526 | # create sum for each table |
---|
527 | # |
---|
528 | xs = 0 |
---|
529 | kkc = '_%s_%s' % (mlab,mlab2) |
---|
530 | kkct = '_%s_%s' % (mlab,'TOTAL') |
---|
531 | if m2 in [None, 'TOTAL']: |
---|
532 | x = self.acc |
---|
533 | x2 = set(x[2].keys() ) |
---|
534 | for k in x[2].keys(): |
---|
535 | i = self.dq.inx.uid[k] |
---|
536 | xxx = x[2][k] |
---|
537 | xs += xxx |
---|
538 | else: |
---|
539 | x2 = set() |
---|
540 | for e in self.sc.volByE: |
---|
541 | if exptids == None or e in exptids: |
---|
542 | x = self.sc.volByE[e] |
---|
543 | x2 = x2.union( set( x[2].keys() ) ) |
---|
544 | for k in x[2].keys(): |
---|
545 | i = self.dq.inx.uid[k] |
---|
546 | xxx = x[2][k] |
---|
547 | xs += xxx |
---|
548 | if xxx > 0: |
---|
549 | collector[kkc].a[i.mipTable] += xxx |
---|
550 | if ismip: |
---|
551 | collector[kkct].a[i.mipTable] += xxx |
---|
552 | |
---|
553 | ## |
---|
554 | ## One user was getting false error message here, with ('%s' % x0) == ('%s' % xs) |
---|
555 | ## |
---|
556 | if abs(x0 -xs) > 1.e-8*( abs(x0) + abs(xs) ): |
---|
557 | print ( 'ERROR.0088: consistency problem %s %s %s %s' % (m,m2,x0,xs) ) |
---|
558 | if x0 == 0: |
---|
559 | print ( 'Zero size: %s, %s' % (m,m2) ) |
---|
560 | if len( x[2].keys() ) > 0: |
---|
561 | print ( 'ERROR:zero: %s, %s: %s' % (m,m2,str(x[2].keys()) ) ) |
---|
562 | |
---|
563 | if acc and m2 not in [ None, 'TOTAL']: |
---|
564 | collector[mlab].a['TOTAL'] += x0 |
---|
565 | |
---|
566 | dd = collections.defaultdict( list ) |
---|
567 | lll = set() |
---|
568 | for v in x2: |
---|
569 | vi = self.sc.dq.inx.uid[v] |
---|
570 | if vi._h.label != 'remarks': |
---|
571 | f,t,l,tt,d,u = (vi.frequency,vi.mipTable,vi.label,vi.title,vi.description,vi.uid) |
---|
572 | lll.add(u) |
---|
573 | dd[t].append( (f,t,l,tt,d,u) ) |
---|
574 | |
---|
575 | if len( dd.keys() ) > 0: |
---|
576 | collector[mlab].dd[mlab2] = dd |
---|
577 | if m2 not in [ None, 'TOTAL']: |
---|
578 | if im2._h.label == 'experiment': |
---|
579 | dothis = self.sc.tierMax >= min( im2.tier ) |
---|
580 | ### |
---|
581 | ### BUT ... there is a treset in the request item .... it may be that some variables are excluded ... |
---|
582 | ### need the variable list itself ..... |
---|
583 | ### |
---|
584 | makeTab( self.sc, subset=lll, dest='%s/%s-%s_%s_%s' % (self.odir,mlab,mlab2,self.sc.tierMax,pmax), collected=collector[kkc].a, |
---|
585 | mcfgNote=self.sc.mcfgNote, |
---|
586 | txt=self.doTxt, xls=self.doXls, txtOpts=self.txtOpts, exptUid=self.sc.exptByLabel.get(mlab2,mlab2) ) |
---|