1 | |
---|
2 | import collections, string, os, sys |
---|
3 | |
---|
4 | try: |
---|
5 | import dreq |
---|
6 | import vrev |
---|
7 | import misc_utils |
---|
8 | except: |
---|
9 | import dreqPy.dreq as dreq |
---|
10 | import dreqPy.vrev as vrev |
---|
11 | import dreqPy.misc_utils as misc_utils |
---|
12 | |
---|
13 | python2 = True |
---|
14 | if sys.version_info[0] == 3: |
---|
15 | python2 = False |
---|
16 | def cmp(x,y): |
---|
17 | if x == y: |
---|
18 | return 0 |
---|
19 | elif x > y: |
---|
20 | return 1 |
---|
21 | else: |
---|
22 | return -1 |
---|
23 | |
---|
24 | if sys.version_info >= (2,7): |
---|
25 | from functools import cmp_to_key |
---|
26 | oldpython = False |
---|
27 | else: |
---|
28 | oldpython = True |
---|
29 | |
---|
30 | try: |
---|
31 | import xlsxwriter |
---|
32 | except: |
---|
33 | print ('No xlsxwrite: will not make tables ...') |
---|
34 | |
---|
35 | ##NT_txtopts = collections.namedtuple( 'txtopts', ['mode'] ) |
---|
36 | |
---|
37 | def setMlab( m ): |
---|
38 | if type(m) == type(''): |
---|
39 | mlab = m |
---|
40 | else: |
---|
41 | ll = sorted( list(m) ) |
---|
42 | if len(ll) == 1: |
---|
43 | mlab = list(m)[0] |
---|
44 | else: |
---|
45 | mlab=string.join( [ x[:2].lower() for x in m ], '.' ) |
---|
46 | return mlab |
---|
47 | |
---|
48 | class xlsx(object): |
---|
49 | def __init__(self,fn,xls=True,txt=False,txtOpts=None): |
---|
50 | self.xls=xls |
---|
51 | self.txt=txt |
---|
52 | self.txtOpts = txtOpts |
---|
53 | self.mcfgNote = 'Reference Volume (1 deg. atmosphere, 0.5 deg. ocean)' |
---|
54 | if xls: |
---|
55 | self.wb = xlsxwriter.Workbook('%s.xlsx' % fn) |
---|
56 | self.hdr_cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#aaaacc'}) |
---|
57 | self.hdr_cell_format.set_text_wrap() |
---|
58 | self.sect_cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 14, 'font_color':'#0000ff', 'bold':1, 'fg_color':'#ccccbb'}) |
---|
59 | self.sect_cell_format.set_text_wrap() |
---|
60 | self.cell_format = self.wb.add_format({'text_wrap': True, 'font_size': 11}) |
---|
61 | self.cell_format.set_text_wrap() |
---|
62 | |
---|
63 | if txt: |
---|
64 | self.oo = open( '%s.csv' % fn, 'w' ) |
---|
65 | |
---|
66 | def header(self,tableNotes,collected): |
---|
67 | if self.xls: |
---|
68 | sht = self.newSheet( 'Notes' ) |
---|
69 | sht.write( 0,0, '', self.hdr_cell_format ) |
---|
70 | sht.write( 0,1, 'Notes on tables', self.hdr_cell_format ) |
---|
71 | ri = 0 |
---|
72 | sht.set_column(0,0,30) |
---|
73 | sht.set_column(1,1,60) |
---|
74 | self.sht = sht |
---|
75 | for t in tableNotes: |
---|
76 | ri += 1 |
---|
77 | for i in range(2): |
---|
78 | sht.write( ri,i, t[i], self.cell_format ) |
---|
79 | |
---|
80 | if collected != None: |
---|
81 | ri += 2 |
---|
82 | sht.write( ri, 0, 'Table', self.sect_cell_format ) |
---|
83 | sht.write( ri, 1, self.mcfgNote, self.sect_cell_format ) |
---|
84 | for k in sorted( collected.keys() ): |
---|
85 | ri += 1 |
---|
86 | sht.write( ri, 0, k ) |
---|
87 | sht.write( ri, 1, vfmt( collected[k]*2. ) ) |
---|
88 | |
---|
89 | if self.txt: |
---|
90 | self.oo.write( string.join( ['Notes','','Notes on tables'], '\t') + '\n' ) |
---|
91 | for t in tableNotes: |
---|
92 | self.oo.write( string.join( ['Notes',] + list(t), '\t') + '\n' ) |
---|
93 | |
---|
94 | if collected != None: |
---|
95 | self.oo.write( string.join( ['Notes','Table','Reference Volume (1 deg. atmosphere, 0.5 deg. ocean)'], '\t') + '\n') |
---|
96 | for k in sorted( collected.keys() ): |
---|
97 | self.oo.write( string.join( ['Notes',k,vfmt( collected[k]*2. )], '\t') + '\n' ) |
---|
98 | |
---|
99 | def cmvtabrec(self,j,t,orec): |
---|
100 | if self.xls: |
---|
101 | for i in range(len(orec)): |
---|
102 | self.sht.write( j,i, orec[i], self.cell_format ) |
---|
103 | |
---|
104 | if self.txt: |
---|
105 | self.oo.write( string.join( [t,] + orec, '\t') + '\n' ) |
---|
106 | |
---|
107 | def varrec(self,j,orec): |
---|
108 | if self.xls: |
---|
109 | for i in range(len(orec)): |
---|
110 | self.sht.write( j,i, orec[i], self.cell_format ) |
---|
111 | |
---|
112 | if self.txt: |
---|
113 | self.oo.write( string.join( orec, '\t') + '\n' ) |
---|
114 | |
---|
115 | def var(self): |
---|
116 | if self.xls: |
---|
117 | self.sht = self.newSheet( 'var' ) |
---|
118 | j = 0 |
---|
119 | hrec = ['Long name', 'units', 'description', 'Variable Name', 'CF Standard Name' ] |
---|
120 | if self.xls: |
---|
121 | self.sht.set_column(1,1,40) |
---|
122 | self.sht.set_column(1,2,30) |
---|
123 | self.sht.set_column(1,3,60) |
---|
124 | self.sht.set_column(1,4,40) |
---|
125 | self.sht.set_column(1,5,40) |
---|
126 | |
---|
127 | if self.xls: |
---|
128 | for i in range(len(hrec)): |
---|
129 | self.sht.write( j,i, hrec[i], self.hdr_cell_format ) |
---|
130 | |
---|
131 | if self.txt: |
---|
132 | for i in range(len(hrec)): |
---|
133 | self.oo.write( hrec[i] + '\t' ) |
---|
134 | self.oo.write( '\n' ) |
---|
135 | |
---|
136 | def cmvtab(self,t,addMips,mode='c'): |
---|
137 | if self.xls: |
---|
138 | self.sht = self.newSheet( t ) |
---|
139 | j = 0 |
---|
140 | ncga = 'NetCDF Global Attribute' |
---|
141 | if mode == 'c': |
---|
142 | hrec = ['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'] |
---|
143 | 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',''] |
---|
144 | if self.xls: |
---|
145 | self.sht.set_column(1,1,40) |
---|
146 | self.sht.set_column(1,3,50) |
---|
147 | self.sht.set_column(1,4,30) |
---|
148 | self.sht.set_column(1,5,50) |
---|
149 | self.sht.set_column(1,6,30) |
---|
150 | self.sht.set_column(1,9,40) |
---|
151 | self.sht.set_column(1,18,40) |
---|
152 | self.sht.set_column(1,19,40) |
---|
153 | else: |
---|
154 | 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'] |
---|
155 | |
---|
156 | if addMips: |
---|
157 | hrec.append( 'MIPs (requesting)' ) |
---|
158 | hrec.append( 'MIPs (by experiment)' ) |
---|
159 | |
---|
160 | if self.xls: |
---|
161 | for i in range(len(hrec)): |
---|
162 | self.sht.write( j,i, hrec[i], self.hdr_cell_format ) |
---|
163 | if hcmt[i] != '': |
---|
164 | self.sht.write_comment( j,i,hcmt[i]) |
---|
165 | |
---|
166 | if self.txt: |
---|
167 | self.oo.write( 'MIP table\t' ) |
---|
168 | for i in range(len(hrec)): |
---|
169 | self.oo.write( hrec[i] + '\t' ) |
---|
170 | self.oo.write( '\n' ) |
---|
171 | self.oo.write( t + '\t' ) |
---|
172 | for i in range(len(hrec)): |
---|
173 | if hcmt[i] != '': |
---|
174 | self.oo.write( hcmt[i] + '\t') |
---|
175 | else: |
---|
176 | self.oo.write( '\t') |
---|
177 | self.oo.write( '\n' ) |
---|
178 | |
---|
179 | def newSheet(self,name): |
---|
180 | self.worksheet = self.wb.add_worksheet(name=name) |
---|
181 | return self.worksheet |
---|
182 | |
---|
183 | def close(self): |
---|
184 | if self.xls: |
---|
185 | self.wb.close() |
---|
186 | if self.txt: |
---|
187 | self.oo.close() |
---|
188 | |
---|
189 | def vfmt( x ): |
---|
190 | if x < 1.e9: |
---|
191 | s = '%sM' % int( x*1.e-6 ) |
---|
192 | elif x < 1.e12: |
---|
193 | s = '%sG' % int( x*1.e-9 ) |
---|
194 | elif x < 1.e13: |
---|
195 | s = '%3.1fT' % ( x*1.e-12 ) |
---|
196 | elif x < 1.e15: |
---|
197 | s = '%3iT' % int( x*1.e-12 ) |
---|
198 | elif x < 1.e18: |
---|
199 | s = '%3iP' % int( x*1.e-15 ) |
---|
200 | else: |
---|
201 | s = '{:,.2f}'.format( x*1.e-9 ) |
---|
202 | return s |
---|
203 | |
---|
204 | #priority long name units comment questions & notes output variable name standard name unconfirmed or proposed standard name unformatted units cell_methods valid min valid max mean absolute min mean absolute max positive type CMOR dimensions CMOR variable name realm frequency cell_measures flag_values flag_meanings |
---|
205 | |
---|
206 | strkeys = [u'procNote', u'uid', u'odims', u'flag_meanings', u'prov', u'title', u'tmid', u'label', u'cell_methods', u'coords', u'cell_measures', u'spid', u'flag_values', u'description'] |
---|
207 | |
---|
208 | ntstr = collections.namedtuple( 'ntstr', strkeys ) |
---|
209 | |
---|
210 | class cmpd(object): |
---|
211 | def __init__(self,k): |
---|
212 | self.k = k |
---|
213 | def cmp(self,x,y): |
---|
214 | return cmp( x.__dict__[self.k], y.__dict__[self.k] ) |
---|
215 | |
---|
216 | class cmpd2(object): |
---|
217 | def __init__(self,k1,k2): |
---|
218 | self.k1 = k1 |
---|
219 | self.k2 = k2 |
---|
220 | def cmp(self,x,y): |
---|
221 | if x.__dict__[self.k1] == y.__dict__[self.k1]: |
---|
222 | return cmp( x.__dict__[self.k2], y.__dict__[self.k2] ) |
---|
223 | else: |
---|
224 | return cmp( x.__dict__[self.k1], y.__dict__[self.k1] ) |
---|
225 | |
---|
226 | class cmpdn(object): |
---|
227 | def __init__(self,kl): |
---|
228 | self.kl = kl |
---|
229 | def cmp(self,x,y): |
---|
230 | for k in self.kl: |
---|
231 | if x.__dict__[k] != y.__dict__[k]: |
---|
232 | return cmp( x.__dict__[k], y.__dict__[k] ) |
---|
233 | |
---|
234 | return cmp( 0,0 ) |
---|
235 | |
---|
236 | def cmpAnnex( x, y ): |
---|
237 | ax = len(x) > 2 and x[:2] == 'em' |
---|
238 | ay = len(y) > 2 and y[:2] == 'em' |
---|
239 | bx = len(x) > 5 and x[:5] in ['CMIP5','CORDE','SPECS'] |
---|
240 | by = len(y) > 5 and y[:5] in ['CMIP5','CORDE','SPECS'] |
---|
241 | if ax == ay and bx == by: |
---|
242 | return cmp(x,y) |
---|
243 | elif ax: |
---|
244 | if by: |
---|
245 | return cmp(0,1) |
---|
246 | else: |
---|
247 | return cmp(1,0) |
---|
248 | elif ay: |
---|
249 | if bx: |
---|
250 | return cmp(1,0) |
---|
251 | else: |
---|
252 | return cmp(0,1) |
---|
253 | elif bx: |
---|
254 | return cmp(1,0) |
---|
255 | else: |
---|
256 | return cmp(0,1) |
---|
257 | |
---|
258 | |
---|
259 | if not oldpython: |
---|
260 | kAnnex = cmp_to_key( cmpAnnex ) |
---|
261 | |
---|
262 | import re |
---|
263 | |
---|
264 | class makePurl(object): |
---|
265 | def __init__(self): |
---|
266 | c1 = re.compile( '^[a-zA-Z][a-zA-Z0-9]*$' ) |
---|
267 | mv = dq.coll['var'].items |
---|
268 | oo = open( 'htmlRewrite.txt', 'w' ) |
---|
269 | for v in mv: |
---|
270 | if c1.match( v.label ): |
---|
271 | oo.write( 'RewriteRule ^%s$ http://clipc-services.ceda.ac.uk/dreq/u/%s.html\n' % (v.label,v.uid) ) |
---|
272 | else: |
---|
273 | print ('Match failed: %s' % v.label ) |
---|
274 | oo.close() |
---|
275 | |
---|
276 | class makeTab(object): |
---|
277 | def __init__(self, dq, subset=None, mcfgNote=None, dest='tables/test', skipped=set(), collected=None,xls=True,txt=False,txtOpts=None): |
---|
278 | """txtOpts: gives option to list MIP variables instead of CMOR variables""" |
---|
279 | if subset != None: |
---|
280 | cmv = [x for x in dq.coll['CMORvar'].items if x.uid in subset] |
---|
281 | else: |
---|
282 | cmv = dq.coll['CMORvar'].items |
---|
283 | if oldpython: |
---|
284 | tables = sorted( list( set( [i.mipTable for i in cmv] ) ), cmp=cmpAnnex ) |
---|
285 | else: |
---|
286 | tables = sorted( list( set( [i.mipTable for i in cmv] ) ), key=kAnnex ) |
---|
287 | |
---|
288 | addMips = True |
---|
289 | if addMips: |
---|
290 | c = vrev.checkVar(dq) |
---|
291 | mode = 'c' |
---|
292 | tableNotes = [ |
---|
293 | ('Request Version',str(dq.version)), |
---|
294 | ('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')] |
---|
295 | |
---|
296 | wb = xlsx( dest, xls=xls, txt=txt ) |
---|
297 | if mcfgNote != None: |
---|
298 | wb.mcfgNote = mcfgNote |
---|
299 | wb.header( tableNotes, collected) |
---|
300 | |
---|
301 | if txtOpts != None and txtOpts.mode == 'var': |
---|
302 | vl = list( set( [v.vid for v in cmv] ) ) |
---|
303 | vli = [dq.inx.uid[i] for i in vl] |
---|
304 | thisvli = sorted( vli, cmp=cmpdn(['sn','label']).cmp ) |
---|
305 | wb.var() |
---|
306 | |
---|
307 | j = 0 |
---|
308 | for v in thisvli: |
---|
309 | ###hrec = ['Long name', 'units', 'description', 'Variable Name', 'CF Standard Name' ] |
---|
310 | orec = [v.title, v.units, v.description, v.label, v.sn] |
---|
311 | j += 1 |
---|
312 | wb.varrec( j,orec ) |
---|
313 | else: |
---|
314 | withoo = False |
---|
315 | for t in tables: |
---|
316 | if withoo: |
---|
317 | oo = open( 'tables/test_%s.csv' % t, 'w' ) |
---|
318 | wb.cmvtab(t,addMips,mode='c') |
---|
319 | |
---|
320 | j = 0 |
---|
321 | thiscmv = sorted( [v for v in cmv if v.mipTable == t], cmp=cmpdn(['prov','rowIndex','label']).cmp ) |
---|
322 | |
---|
323 | for v in thiscmv: |
---|
324 | cv = dq.inx.uid[ v.vid ] |
---|
325 | strc = dq.inx.uid[ v.stid ] |
---|
326 | if strc._h.label == 'remarks': |
---|
327 | print ( 'ERROR: structure not found for %s: %s .. %s (%s)' % (v.uid,v.label,v.title,v.mipTable) ) |
---|
328 | ok = False |
---|
329 | else: |
---|
330 | sshp = dq.inx.uid[ strc.spid ] |
---|
331 | tshp = dq.inx.uid[ strc.tmid ] |
---|
332 | ok = all( [i._h.label != 'remarks' for i in [cv,strc,sshp,tshp]] ) |
---|
333 | #[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'] |
---|
334 | |
---|
335 | if not ok: |
---|
336 | if (t,v.label) not in skipped: |
---|
337 | ml = [] |
---|
338 | for i in range(4): |
---|
339 | ii = [cv,strc,sshp,tshp][i] |
---|
340 | if ii._h.label == 'remarks': |
---|
341 | ml.append( ['var','struct','time','spatial'][i] ) |
---|
342 | print ( 'makeTables: skipping %s %s: %s' % (t,v.label,string.join( ml, ',')) ) |
---|
343 | skipped.add( (t,v.label) ) |
---|
344 | else: |
---|
345 | dims = [] |
---|
346 | dims += string.split( sshp.dimensions, '|' ) |
---|
347 | dims += string.split( tshp.dimensions, '|' ) |
---|
348 | dims += string.split( strc.odims, '|' ) |
---|
349 | dims += string.split( strc.coords, '|' ) |
---|
350 | dims = string.join( dims ) |
---|
351 | if mode == 'c': |
---|
352 | orec = [str(v.defaultPriority),cv.title, cv.units, cv.description, v.description, cv.label, cv.sn, strc.cell_methods, v.positive, v.type, dims, v.label, v.modeling_realm, v.frequency, strc.cell_measures, v.prov,v.provNote,str(v.rowIndex),v.uid,v.vid,v.stid,strc.title] |
---|
353 | else: |
---|
354 | orec = ['',cv.title, cv.units, v.description, '', cv.label, cv.sn, '','', strc.cell_methods, v.valid_min, v.valid_max, v.ok_min_mean_abs, v.ok_max_mean_abs, v.positive, v.type, dims, v.label, v.modeling_realm, v.frequency, strc.cell_measures, strc.flag_values, strc.flag_meanings,v.prov,v.provNote,str(v.rowIndex),cv.uid] |
---|
355 | if addMips: |
---|
356 | thismips = c.chkCmv( v.uid ) |
---|
357 | thismips2 = c.chkCmv( v.uid, byExpt=True ) |
---|
358 | orec.append( string.join( sorted( list( thismips) ),',') ) |
---|
359 | orec.append( string.join( sorted( list( thismips2) ),',') ) |
---|
360 | |
---|
361 | if withoo: |
---|
362 | oo.write( string.join(orec, '\t' ) + '\n' ) |
---|
363 | j+=1 |
---|
364 | wb.cmvtabrec( j,t,orec ) |
---|
365 | |
---|
366 | if withoo: |
---|
367 | oo.close() |
---|
368 | wb.close() |
---|
369 | |
---|
370 | hdr = """ |
---|
371 | function f000(value) { return (value + "").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">") }; |
---|
372 | |
---|
373 | function formatter00(row, cell, value, columnDef, dataContext) { |
---|
374 | var vv = value.split(" "); |
---|
375 | return '<b><a href="u/' + vv[1] + '.html">' + (vv[0] + " ").replace(/&/g,"&") + '</a></b>, '; |
---|
376 | }; |
---|
377 | function formatter01(row, cell, value, columnDef, dataContext) { return '<i>' + f000(value) + '</i> ' }; |
---|
378 | function formatter02(row, cell, value, columnDef, dataContext) { return '[' + f000(value) + '] ' }; |
---|
379 | function formatter03(row, cell, value, columnDef, dataContext) { if (value != "'unset'" ) { return '(' + f000(value) + ') ' } else {return ''} }; |
---|
380 | function formatter04(row, cell, value, columnDef, dataContext) { return '{' + f000(value) + '} ' }; |
---|
381 | function formatter05(row, cell, value, columnDef, dataContext) { return '<' + f000(value) + '> ' }; |
---|
382 | |
---|
383 | var getData = { |
---|
384 | cols: function() { |
---|
385 | var columns = [ {id:0, name:'Variable', field:0, width: 100, formatter:formatter00 }, |
---|
386 | {id:1, name:'Standard name', field:1, width: 210, formatter:formatter01 }, |
---|
387 | {id:2, name:'Long name', field:2, width: 180, formatter:formatter02}, |
---|
388 | {id:3, name:'Units', field:3, width: 180, formatter:formatter03}, |
---|
389 | {id:4, name:'Description', field:4, width: 180, formatter:formatter04}, |
---|
390 | {id:5, name:'uid', field:5, width: 180, formatter:formatter05}]; |
---|
391 | return columns; |
---|
392 | }, |
---|
393 | |
---|
394 | data: function() { |
---|
395 | var data = []; |
---|
396 | """ |
---|
397 | ftr = """return data; |
---|
398 | } |
---|
399 | }; |
---|
400 | """ |
---|
401 | ##rtmpl = 'data[%(n)s] = { "id":%(n)s, 0:"%(var)s", 1:"%(sn)s", 2:"%(ln)s", 3:"%(u)s", 4:"%(uid)s" };' |
---|
402 | rtmpl = 'data[%(n)s] = { "id":%(n)s, 0:"%(var)s", 1:"%(sn)s", 2:"%(ln)s", 3:"%(u)s", 4:"%(d)s", 5:"%(uid)s" };' |
---|
403 | |
---|
404 | class htmlTrees(object): |
---|
405 | def __init__(self,dq,odir='html/t/'): |
---|
406 | self.dq = dq |
---|
407 | self.odir = odir |
---|
408 | self.c = vrev.checkVar( dq ) |
---|
409 | self.anno = {} |
---|
410 | for v in dq.coll['var'].items: |
---|
411 | self.makeTree( v ) |
---|
412 | |
---|
413 | def makeTree( self, v ): |
---|
414 | ee = self.c.chk2( v.label ) |
---|
415 | if len(ee.keys()) > 0: |
---|
416 | title = 'Usage tree for %s' % v.label |
---|
417 | bdy = ['<h1>%s</h1>' % title, ] |
---|
418 | bdy.append( '<html><head></head><body>\n' ) |
---|
419 | bdy.append( '<ul>\n' ) |
---|
420 | for k in sorted( ee.keys() ): |
---|
421 | l1, xx = ee[k] |
---|
422 | lx = list( xx ) |
---|
423 | if len( lx ) == 0: |
---|
424 | bdy.append( '<li>%s: Empty</li>\n' % l1 ) |
---|
425 | else: |
---|
426 | bdy.append( '<li>%s:\n<ul>' % l1 ) |
---|
427 | for x in lx: |
---|
428 | bdy.append( '<li>%s</li>\n' % x ) |
---|
429 | bdy.append( '</ul></li>\n' ) |
---|
430 | bdy.append( '</ul></body></html>\n' ) |
---|
431 | oo = open( '%s/%s.html' % (self.odir,v.label), 'w' ) |
---|
432 | oo.write( dq.pageTmpl % ( title, '', '../', '../index.html', string.join( bdy, '\n' ) ) ) |
---|
433 | oo.close() |
---|
434 | self.anno[v.label] = '<a href="../t/%s.html">Usage</a>' % v.label |
---|
435 | else: |
---|
436 | self.anno[v.label] = 'Unused' |
---|
437 | |
---|
438 | |
---|
439 | class makeJs(object): |
---|
440 | def __init__(self,dq): |
---|
441 | n = 0 |
---|
442 | rl = [] |
---|
443 | for v in dq.coll['var'].items: |
---|
444 | var = '%s %s' % (v.label,v.uid) |
---|
445 | sn = v.sn |
---|
446 | ln = v.title |
---|
447 | u = v.units |
---|
448 | d = v.description |
---|
449 | uid = v.uid |
---|
450 | d = locals() |
---|
451 | for k in ['sn','ln','u','var','d']: |
---|
452 | |
---|
453 | if string.find( d[k], '"' ) != -1: |
---|
454 | print ( "WARNING ... quote in %s .. %s [%s]" % (k,var,d[k]) ) |
---|
455 | d[k] = string.replace( d[k], '"', "'" ) |
---|
456 | print ( d[k] ) |
---|
457 | |
---|
458 | rr = rtmpl % d |
---|
459 | rl.append( rr ) |
---|
460 | n += 1 |
---|
461 | oo = open( 'data3.js', 'w' ) |
---|
462 | oo.write( hdr ) |
---|
463 | for r in rl: |
---|
464 | oo.write( r + '\n' ) |
---|
465 | oo.write( ftr ) |
---|
466 | oo.close() |
---|
467 | |
---|
468 | |
---|
469 | |
---|
470 | class styles(object): |
---|
471 | def __init__(self): |
---|
472 | pass |
---|
473 | |
---|
474 | def rqvLink01(self,targ,frm='',ann=''): |
---|
475 | if targ._h.label == 'remarks': |
---|
476 | return '<li>%s: %s</li>' % ( targ.__href__(odir='../u/', label=targ.title), "Link to request variable broken" ) |
---|
477 | elif frm != "CMORvar": |
---|
478 | cmv = targ._inx.uid[ targ.vid ] |
---|
479 | if targ._h.label == 'remarks': |
---|
480 | return '<li>%s [%s]: %s</li>' % ( cmv.label, targ.__href__(odir='../u/',label=targ.priority) , 'Variable not defined or not found' ) |
---|
481 | else: |
---|
482 | return '<li>%s [%s]: %s</li>' % ( cmv.label, targ.__href__(odir='../u/',label=targ.priority) , cmv.__href__(odir='../u/',label=cmv.title) ) |
---|
483 | else: |
---|
484 | rg = targ._inx.uid[ targ.vgid ] |
---|
485 | if targ._h.label == 'remarks': |
---|
486 | return '<li>%s [%s]: %s</li>' % ( targ.label, targ.__href__(label=targ.priority) , 'Link not defined or not found' ) |
---|
487 | elif rg._h.label == 'remarks': |
---|
488 | return '<li>%s [%s]: %s</li>' % ( rg.label, targ.__href__(label=targ.priority) , 'Group not defined or not found' ) |
---|
489 | else: |
---|
490 | return '<li>%s [%s]: %s</li>' % ( rg.label, targ.__href__(label=targ.priority) , rg.__href__(label=rg.mip) ) |
---|
491 | |
---|
492 | def snLink01(self,a,targ,frm='',ann=''): |
---|
493 | if targ._h.label == 'remarks': |
---|
494 | return '<li>%s: Standard name under review [%s]</li>' % ( a, targ.__href__() ) |
---|
495 | else: |
---|
496 | return '<li>%s [%s]: %s</li>' % ( targ._h.title, a, targ.__href__(label=targ.label) ) |
---|
497 | |
---|
498 | def stidLink01(self,a,targ,frm='',ann=''): |
---|
499 | if targ._h.label == 'remarks': |
---|
500 | return '<li>%s: Broken link to structure [%s]</li>' % ( a, targ.__href__() ) |
---|
501 | else: |
---|
502 | return '<li>%s [%s]: %s [%s]</li>' % ( targ._h.title, a, targ.__href__(label=targ.title), targ.label ) |
---|
503 | |
---|
504 | def rqlLink02(self,targ,frm='',ann=''): |
---|
505 | t2 = targ._inx.uid[targ.refid] |
---|
506 | if t2._h.label == 'remarks': |
---|
507 | return '<li>%s: %s</li>' % ( targ.__href__(odir='../u/', label=targ.title), "Link to variable group broken" ) |
---|
508 | elif frm == "requestVarGroup": |
---|
509 | return '<li>%s: %s [%s]</li>' % ( targ.__href__(odir='../u/', label=targ.mip), targ.title, targ.objective ) |
---|
510 | else: |
---|
511 | gpsz = len(t2._inx.iref_by_sect[t2.uid].a['requestVar']) |
---|
512 | return '<li>%s: Link to group: %s [%s]</li>' % ( targ.__href__(odir='../u/', label='%s:%s' % (targ.mip,targ.title)), t2.__href__(odir='../u/', label=t2.title), gpsz ) |
---|
513 | |
---|
514 | def rqiLink02(self,targ,frm='',ann=''): |
---|
515 | t2 = targ._inx.uid[targ.rlid] |
---|
516 | if t2._h.label == 'remarks': |
---|
517 | return '<li>%s: %s</li>' % ( targ.__href__(odir='../u/', label=targ.title), "Link to request link broken" ) |
---|
518 | else: |
---|
519 | t3 = t2._inx.uid[t2.refid] |
---|
520 | if t3._h.label == 'remarks': |
---|
521 | return '<li>%s [%s]: %s</li>' % ( targ.__href__(odir='../u/', label=targ.title), t2.__href__(odir='../u/', label=t2.title),"Link to request group broken" ) |
---|
522 | else: |
---|
523 | nv = len( t3._inx.iref_by_sect[t3.uid].a['requestVar'] ) |
---|
524 | return '<li>%s [%s]: %s (%s variables)</li>' % ( targ.__href__(odir='../u/', label=targ.title), t2.__href__(odir='../u/', label=t2.title), t3.__href__(odir='../u/', label=t3.title), nv ) |
---|
525 | |
---|
526 | def snLink(self,targ,frm='',ann=''): |
---|
527 | return '<li>%s [%s]: %s</li>' % ( targ.title, targ.units, targ.__href__(odir='../u/') ) |
---|
528 | |
---|
529 | def varLink(self,targ,frm='',ann=''): |
---|
530 | return '<li>%s: %s [%s]%s</li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.title, targ.units, ann ) |
---|
531 | |
---|
532 | def mipLink(self,targ,frm='',ann=''): |
---|
533 | if targ.url != '': |
---|
534 | return '<li>%s: %s <a href="%s">[project site]</a></li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.title, targ.url ) |
---|
535 | else: |
---|
536 | return '<li>%s: %s</li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.title ) |
---|
537 | |
---|
538 | def cmvLink(self,targ,frm='',ann=''): |
---|
539 | t2 = targ._inx.uid[targ.stid] |
---|
540 | if 'requestVar' in targ._inx.iref_by_sect[targ.uid].a: |
---|
541 | nrq = len( targ._inx.iref_by_sect[targ.uid].a['requestVar'] ) |
---|
542 | else: |
---|
543 | nrq = 'unused' |
---|
544 | return '<li>%s {%s}: %s [%s: %s] (%s)</li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.mipTable, targ.title, targ.frequency, t2.title, nrq ) |
---|
545 | |
---|
546 | def objLink(self,targ,frm='',ann=''): |
---|
547 | return '<li>%s: %s</li>' % ( targ.label, targ.__href__(odir='../u/', label=targ.title,title=targ.description) ) |
---|
548 | |
---|
549 | def unitLink(self,targ,frm='',ann=''): |
---|
550 | return '<li>%s [%s]: %s</li>' % ( targ.text, targ.label, targ.__href__(odir='../u/', label=targ.title) ) |
---|
551 | |
---|
552 | def strLink(self,targ,frm='',ann=''): |
---|
553 | return '<li>%s: %s</li>' % ( targ.label, targ.__href__(odir='../u/', label=targ.title) ) |
---|
554 | |
---|
555 | def cmLink(self,targ,frm='',ann=''): |
---|
556 | return '<li>%s [%s]: %s</li>' % ( targ.cell_methods,targ.label, targ.__href__(odir='../u/', label=targ.title) ) |
---|
557 | |
---|
558 | def objLnkLink(self,targ,frm='',ann=''): |
---|
559 | if frm == 'objective': |
---|
560 | t2 = targ._inx.uid[targ.rid] |
---|
561 | t3 = targ._inx.uid[t2.refid] |
---|
562 | thislab = '%s (%s)' % (t2.mip,t3.label) |
---|
563 | return '<li>%s: %s</li>' % ( t2.title, t2.__href__(odir='../u/',label=thislab) ) |
---|
564 | else: |
---|
565 | t2 = targ._inx.uid[targ.oid] |
---|
566 | return '<li>%s: %s</li>' % ( t2.label, t2.__href__(odir='../u/',label=t2.title) ) |
---|
567 | |
---|
568 | def labTtl(self,targ,frm='',ann=''): |
---|
569 | return '<li>%s: %s</li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.title ) |
---|
570 | |
---|
571 | def vgrpLink(self,targ,frm='',ann=''): |
---|
572 | gpsz = len(targ._inx.iref_by_sect[targ.uid].a['requestVar']) |
---|
573 | nlnk = len(targ._inx.iref_by_sect[targ.uid].a['requestLink']) |
---|
574 | return '<li>%s {%s}: %s variables, %s request links</li>' % ( targ.__href__(odir='../u/', label=targ.label), targ.mip, gpsz, nlnk ) |
---|
575 | |
---|
576 | class tables(object): |
---|
577 | def __init__(self,sc, mips, odir='xls',xls=True,txt=False,txtOpts=None): |
---|
578 | self.sc = sc |
---|
579 | self.dq = sc.dq |
---|
580 | self.mips = mips |
---|
581 | self.odir = odir |
---|
582 | self.accReset() |
---|
583 | self.doXls = xls |
---|
584 | self.doTxt = txt |
---|
585 | self.txtOpts = txtOpts |
---|
586 | |
---|
587 | def accReset(self): |
---|
588 | self.acc = [0.,collections.defaultdict(int),collections.defaultdict( float ) ] |
---|
589 | |
---|
590 | def accAdd(self,x): |
---|
591 | self.acc[0] += x[0] |
---|
592 | for k in x[2]: |
---|
593 | self.acc[2][k] += x[2][k] |
---|
594 | |
---|
595 | def doTable(self,m,l1,m2,pmax,collector,acc=True, mlab=None,exptids=None,cc=None): |
---|
596 | """*acc* allows accumulation of values to be switched off when called in single expt mode""" |
---|
597 | |
---|
598 | self.verbose = False |
---|
599 | if mlab == None: |
---|
600 | mlab = setMlab( m ) |
---|
601 | |
---|
602 | cc0 = misc_utils.getExptSum( self.dq, mlab, l1 ) |
---|
603 | ks = sorted( list( cc0.keys() ) ) |
---|
604 | if self.verbose: |
---|
605 | print ('Experiment summary: %s %s' % (mlab,string.join( ['%s: %s' % (k,len(cc0[k])) for k in ks], ', ' ) ) ) |
---|
606 | |
---|
607 | if m2 in [None, 'TOTAL']: |
---|
608 | x = self.acc |
---|
609 | else: |
---|
610 | x = self.sc.volByExpt( l1, m2, pmax=pmax ) |
---|
611 | |
---|
612 | ##self.volByExpt( l1, e, pmax=pmax, cc=cc, retainRedundantRank=retainRedundantRank, intersection=intersection, adsCount=adsCount ) |
---|
613 | v0 = self.sc.volByMip( m, pmax=pmax, exptid=m2 ) |
---|
614 | #### |
---|
615 | if cc==None: |
---|
616 | cc = collections.defaultdict( int ) |
---|
617 | for e in self.sc.volByE: |
---|
618 | if self.verbose: |
---|
619 | print ('INFO.mlab.... %s: %s: %s' % ( mlab, e, len( self.sc.volByE[e][2] ) ) ) |
---|
620 | for v in self.sc.volByE[e][2]: |
---|
621 | cc[v] += self.sc.volByE[e][2][v] |
---|
622 | xxx = 0 |
---|
623 | for v in cc: |
---|
624 | xxx += cc[v] |
---|
625 | #### |
---|
626 | if acc: |
---|
627 | for e in self.sc.volByE: |
---|
628 | self.accAdd(self.sc.volByE[e]) |
---|
629 | |
---|
630 | if m2 not in [ None, 'TOTAL']: |
---|
631 | im2 = self.dq.inx.uid[m2] |
---|
632 | ismip = im2._h.label == 'mip' |
---|
633 | mlab2 = im2.label |
---|
634 | |
---|
635 | x0 = 0 |
---|
636 | for e in self.sc.volByE: |
---|
637 | if exptids == None or e in exptids: |
---|
638 | x = self.sc.volByE[e] |
---|
639 | if x[0] > 0: |
---|
640 | collector[mlab].a[mlab2] += x[0] |
---|
641 | x0 += x[0] |
---|
642 | else: |
---|
643 | ismip = False |
---|
644 | mlab2 = 'TOTAL' |
---|
645 | x0 = x[0] |
---|
646 | |
---|
647 | if mlab2 == 'TOTAL' and x0 == 0: |
---|
648 | print ( 'no data detected for %s' % mlab ) |
---|
649 | |
---|
650 | if x0 > 0: |
---|
651 | # |
---|
652 | # create sum for each table |
---|
653 | # |
---|
654 | xs = 0 |
---|
655 | kkc = '_%s_%s' % (mlab,mlab2) |
---|
656 | kkct = '_%s_%s' % (mlab,'TOTAL') |
---|
657 | if m2 in [None, 'TOTAL']: |
---|
658 | x = self.acc |
---|
659 | x2 = set(x[2].keys() ) |
---|
660 | for k in x[2].keys(): |
---|
661 | i = self.dq.inx.uid[k] |
---|
662 | xxx = x[2][k] |
---|
663 | xs += xxx |
---|
664 | else: |
---|
665 | x2 = set() |
---|
666 | for e in self.sc.volByE: |
---|
667 | if exptids == None or e in exptids: |
---|
668 | x = self.sc.volByE[e] |
---|
669 | x2 = x2.union( set( x[2].keys() ) ) |
---|
670 | for k in x[2].keys(): |
---|
671 | i = self.dq.inx.uid[k] |
---|
672 | xxx = x[2][k] |
---|
673 | xs += xxx |
---|
674 | if xxx > 0: |
---|
675 | collector[kkc].a[i.mipTable] += xxx |
---|
676 | if ismip: |
---|
677 | collector[kkct].a[i.mipTable] += xxx |
---|
678 | |
---|
679 | ## |
---|
680 | ## One user was getting false error message here, with ('%s' % x0) == ('%s' % xs) |
---|
681 | ## |
---|
682 | if abs(x0 -xs) > 1.e-8*( abs(x0) + abs(xs) ): |
---|
683 | print ( 'ERROR.0088: consistency problem %s %s %s %s' % (m,m2,x0,xs) ) |
---|
684 | if x0 == 0: |
---|
685 | print ( 'Zero size: %s, %s' % (m,m2) ) |
---|
686 | if len( x[2].keys() ) > 0: |
---|
687 | print ( 'ERROR:zero: %s, %s: %s' % (m,m2,str(x[2].keys()) ) ) |
---|
688 | |
---|
689 | if acc and m2 not in [ None, 'TOTAL']: |
---|
690 | collector[mlab].a['TOTAL'] += x0 |
---|
691 | |
---|
692 | ##print 'xxxxq', collector[mlab].a[mlab2], collector[mlab].a['TOTAL'] |
---|
693 | ##print 'xxxxz', kkc, collector[kkc].a['Amon'], collector[kkct].a['Amon'], ismip |
---|
694 | dd = collections.defaultdict( list ) |
---|
695 | lll = set() |
---|
696 | for v in x2: |
---|
697 | vi = self.sc.dq.inx.uid[v] |
---|
698 | if vi._h.label != 'remarks': |
---|
699 | f,t,l,tt,d,u = (vi.frequency,vi.mipTable,vi.label,vi.title,vi.description,vi.uid) |
---|
700 | lll.add(u) |
---|
701 | dd[t].append( (f,t,l,tt,d,u) ) |
---|
702 | |
---|
703 | ##print 'xxxxx',mlab,mlab2, x0, len( dd.keys() ) |
---|
704 | if len( dd.keys() ) > 0: |
---|
705 | collector[mlab].dd[mlab2] = dd |
---|
706 | if m2 not in [ None, 'TOTAL']: |
---|
707 | if im2._h.label == 'experiment': |
---|
708 | dothis = self.sc.tierMax >= min( im2.tier ) |
---|
709 | ### |
---|
710 | ### BUT ... there is a treset in the request item .... it may be that some variables are excluded ... |
---|
711 | ### need the variable list itself ..... |
---|
712 | ### |
---|
713 | ##print '> maketab: ','%s/%s-%s_%s_%s.xlsx' % (self.odir,mlab,mlab2,self.sc.tierMax,pmax) |
---|
714 | makeTab( self.sc.dq, subset=lll, dest='%s/%s-%s_%s_%s' % (self.odir,mlab,mlab2,self.sc.tierMax,pmax), collected=collector[kkc].a, |
---|
715 | mcfgNote=self.sc.mcfgNote, |
---|
716 | txt=self.doTxt, xls=self.doXls, txtOpts=self.txtOpts ) |
---|
717 | |
---|
718 | styls = styles() |
---|
719 | |
---|
720 | htmlStyle = {} |
---|
721 | htmlStyle['CMORvar'] = {'getIrefs':['__all__']} |
---|
722 | htmlStyle['requestVarGroup'] = {'getIrefs':['requestVar','requestLink']} |
---|
723 | htmlStyle['var'] = {'getIrefs':['CMORvar']} |
---|
724 | htmlStyle['objective'] = {'getIrefs':['objectiveLink']} |
---|
725 | htmlStyle['requestLink'] = {'getIrefs':['objectiveLink','requestItem']} |
---|
726 | htmlStyle['exptgroup'] = {'getIrefs':['__all__']} |
---|
727 | htmlStyle['requestItem'] = {'getIrefs':['__all__']} |
---|
728 | htmlStyle['experiment'] = {'getIrefs':['__all__']} |
---|
729 | htmlStyle['mip'] = {'getIrefs':['__all__']} |
---|
730 | htmlStyle['miptable'] = {'getIrefs':['__all__']} |
---|
731 | htmlStyle['remarks'] = {'getIrefs':['__all__']} |
---|
732 | htmlStyle['grids'] = {'getIrefs':['__all__']} |
---|
733 | htmlStyle['varChoice'] = {'getIrefs':['__all__']} |
---|
734 | htmlStyle['spatialShape'] = {'getIrefs':['__all__']} |
---|
735 | htmlStyle['temporalShape'] = {'getIrefs':['__all__']} |
---|
736 | htmlStyle['structure'] = {'getIrefs':['__all__']} |
---|
737 | htmlStyle['cellMethods'] = {'getIrefs':['__all__']} |
---|
738 | htmlStyle['standardname'] = {'getIrefs':['__all__']} |
---|
739 | htmlStyle['varRelations'] = {'getIrefs':['__all__']} |
---|
740 | htmlStyle['varRelLnk'] = {'getIrefs':['__all__']} |
---|
741 | htmlStyle['units'] = {'getIrefs':['__all__']} |
---|
742 | |
---|
743 | if __name__ == "__main__": |
---|
744 | assert os.path.isdir( 'html' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml' |
---|
745 | assert os.path.isdir( 'html/u' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' |
---|
746 | assert os.path.isdir( 'html/index' ), 'Before running this script you need to create "html", "html/index" and "html/u" sub-directories, or edit the call to dq.makeHtml, and refernces to "u" in style lines below' |
---|
747 | assert os.path.isdir( 'tables' ), 'Before running this script you need to create a "tables" sub-directory, or edit the makeTab class' |
---|
748 | |
---|
749 | dq = dreq.loadDreq( htmlStyles=htmlStyle, manifest='out/dreqManifest.txt' ) |
---|
750 | ## |
---|
751 | ## add special styles to dq object "itemStyle" dictionary. |
---|
752 | ## |
---|
753 | |
---|
754 | dq.itemStyles['standardname'] = styls.snLink |
---|
755 | dq.itemStyles['var'] = styls.varLink |
---|
756 | dq.itemStyles['mip'] = styls.mipLink |
---|
757 | dq.itemStyles['CMORvar'] = styls.cmvLink |
---|
758 | dq.itemStyles['objective'] = styls.objLink |
---|
759 | dq.itemStyles['units'] = styls.unitLink |
---|
760 | dq.itemStyles['structure'] = styls.strLink |
---|
761 | dq.itemStyles['cellMethods'] = styls.cmLink |
---|
762 | dq.itemStyles['objectiveLink'] = styls.objLnkLink |
---|
763 | dq.itemStyles['requestVarGroup'] = styls.vgrpLink |
---|
764 | dq.itemStyles['requestLink'] = styls.rqlLink02 |
---|
765 | dq.itemStyles['requestItem'] = styls.rqiLink02 |
---|
766 | dq.itemStyles['spatialShape'] = styls.labTtl |
---|
767 | dq.coll['var'].items[0].__class__._linkAttrStyle['sn'] = styls.snLink01 |
---|
768 | dq.coll['CMORvar'].items[0].__class__._linkAttrStyle['stid'] = styls.stidLink01 |
---|
769 | ##dq.coll['requestVarGroup'].items[0].__class__._linkAttrStyle['requestVar'] = styls.rqvLink01 |
---|
770 | dq.itemStyles['requestVar'] = styls.rqvLink01 |
---|
771 | |
---|
772 | ht = htmlTrees(dq) |
---|
773 | dq.makeHtml( annotations={'var':ht.anno}, ttl0='Data Request [%s]' % dreq.version ) |
---|
774 | try: |
---|
775 | import xlsxwriter |
---|
776 | mt = makeTab( dq) |
---|
777 | except: |
---|
778 | print ('Could not make tables ...') |
---|
779 | raise |
---|
780 | mp = makePurl() |
---|
781 | mj = makeJs( dq ) |
---|