1 | |
---|
2 | |
---|
3 | import collections, string |
---|
4 | from dreqPy import dreq |
---|
5 | from dreqPy import scope |
---|
6 | import xlsxwriter |
---|
7 | from dreqPy import makeTables |
---|
8 | |
---|
9 | jsh=''' |
---|
10 | <link type="text/css" href="/css/dreq.css" rel="Stylesheet" /> |
---|
11 | ''' |
---|
12 | |
---|
13 | def vfmt( x ): |
---|
14 | if x < 1.e9: |
---|
15 | s = '%sM' % int( x*1.e-6 ) |
---|
16 | elif x < 1.e12: |
---|
17 | s = '%sG' % int( x*1.e-9 ) |
---|
18 | elif x < 1.e13: |
---|
19 | s = '%3.1fT' % ( x*1.e-12 ) |
---|
20 | elif x < 1.e15: |
---|
21 | s = '%3iT' % int( x*1.e-12 ) |
---|
22 | elif x < 1.e18: |
---|
23 | s = '%3iP' % int( x*1.e-15 ) |
---|
24 | else: |
---|
25 | s = '{:,.2f}'.format( x*1.e-9 ) |
---|
26 | return s |
---|
27 | |
---|
28 | class c1(object): |
---|
29 | def __init__(self): |
---|
30 | self.a = collections.defaultdict( int ) |
---|
31 | class c2(object): |
---|
32 | def __init__(self): |
---|
33 | self.a = collections.defaultdict( list ) |
---|
34 | |
---|
35 | class r1(object): |
---|
36 | def __init__(self,sc,tiermax=1,pmax=1): |
---|
37 | self.pmax=pmax |
---|
38 | self.tiermax=tiermax |
---|
39 | self.sc = sc |
---|
40 | sc.setTierMax( tiermax ) |
---|
41 | self.cc = collections.defaultdict( c1 ) |
---|
42 | self.dq = sc.dq |
---|
43 | self.mips = ['AerChemMIP', 'C4MIP', 'CFMIP', 'DAMIP', 'DCPP', 'FAFMIP', 'GeoMIP', 'GMMIP', 'HighResMIP', 'ISMIP6', 'LS3MIP', 'LUMIP', 'OMIP', 'PMIP', 'RFMIP', 'ScenarioMIP', 'VolMIP', 'CORDEX', 'DynVar', 'SIMIP', 'VIACSAB'] |
---|
44 | self.mipsp = ['DECK','CMIP6',] + self.mips[:-4] |
---|
45 | tabs = makeTables.tables( sc, self.mips ) |
---|
46 | |
---|
47 | for m in self.mips + ['TOTAL',]: |
---|
48 | if m == 'TOTAL': |
---|
49 | l1 = sc.rqiByMip( set( self.mips ) ) |
---|
50 | else: |
---|
51 | l1 = sc.rqiByMip( m ) |
---|
52 | |
---|
53 | self.cc[m].dd = {} |
---|
54 | self.cc[m].ee = {} |
---|
55 | for m2 in self.mipsp: |
---|
56 | xx = [i for i in self.dq.coll['experiment'].items if i.mip == m2] |
---|
57 | self.cc[m].ee[m2] = xx |
---|
58 | ## |
---|
59 | ## need to check this option, and add a template for a view summarising the experiments for each mip-mip combinations |
---|
60 | ## |
---|
61 | ## sss=True not yet tested |
---|
62 | ## |
---|
63 | sss = True |
---|
64 | if sss: |
---|
65 | for i in xx: |
---|
66 | tabs.doTable(m,l1,i.uid,pmax,self.cc,acc=False) |
---|
67 | tabs.doTable(m,l1,m2,pmax,self.cc) |
---|
68 | self.makeMMhtml(m,m2) |
---|
69 | else: |
---|
70 | tabs.doTable(m,l1,m2,pmax,self.cc) |
---|
71 | |
---|
72 | self.writeMips(sss) |
---|
73 | |
---|
74 | def makeMMhtml(self,m,m2): |
---|
75 | """Make a html page for data requested by MIP 'm' from MIP 'm2' experiments""" |
---|
76 | fss = '%s-%s_%s_%s.html' % (m,m2,self.tiermax, self.pmax) |
---|
77 | kc = '_%s_%s' % (m,m2) |
---|
78 | if len( self.cc[kc].a.keys() ) == 0: |
---|
79 | return |
---|
80 | oo = open( 'tabs03/%s' % fss, 'w' ) |
---|
81 | ttl = 'Date requested by %s from %s experiments (tier %s, priority %s)' % (m,m2,self.tiermax,self.pmax) |
---|
82 | jsh = '' |
---|
83 | pream = '<h1>%s</h1>\n' % ttl |
---|
84 | pream += '<p>All variables in one <a href="../data/tabs02/%s-%s_%s_%s.xlsx">Excel file</a></p>\n' % (m,m2,self.tiermax, self.pmax) |
---|
85 | pream += '<ul>' |
---|
86 | kc = '_%s_%s' % (m,m2) |
---|
87 | for k in sorted( self.cc[kc].a.keys() ): |
---|
88 | pream += '<li>%s: %s</li>\n' % (k,vfmt(self.cc[kc].a[k]*2.) ) |
---|
89 | pream += '</ul>' |
---|
90 | |
---|
91 | bdy = pream + '<table>\n' |
---|
92 | bdy += '<tr><th>Experiment</th><th>Volume (and link to variable lists)</th></tr>\n' |
---|
93 | for i in self.cc[m].ee[m2]: |
---|
94 | if i.uid in self.cc[m].a: |
---|
95 | x = self.cc[m].a[i.uid]*2. |
---|
96 | if x > 0: |
---|
97 | s = vfmt( x ) |
---|
98 | bdy += '<tr><td>%s</td><td><a href="../data/tabs02/%s-%s_%s_%s.xlsx">%s</a></td></tr>\n' % (i.label,m,i.uid,self.tiermax, self.pmax,s) |
---|
99 | bdy += '</table>\n' |
---|
100 | |
---|
101 | oo.write( self.dq.pageTmpl % (ttl, jsh, '../', '../index.html', bdy ) ) |
---|
102 | oo.close() |
---|
103 | |
---|
104 | def writeMips(self,sss=False): |
---|
105 | |
---|
106 | oo = open( 'tab01_%s_%s.texfrag' % (self.tiermax,self.pmax), 'w' ) |
---|
107 | mmh = [] |
---|
108 | mhdr = [ '\\rot{80}{%s}' % m for m in self.mipsp + ['TOTAL',]] |
---|
109 | mhdrh = [ '<th><div><span>%s</span></div></th>' % m for m in self.mipsp] |
---|
110 | oo.write( string.join(['',] + mhdr, ' & ' ) + '\\\\ \n\\hline\n' ) |
---|
111 | mmh.append( '<table>\n<tr class="rotate">' + string.join(['<th></th>',] + mhdrh ) + '</tr>\n' ) |
---|
112 | htmltmpl_head = '<html><body>\n' |
---|
113 | |
---|
114 | for m in self.mips + ['TOTAL',]: |
---|
115 | ll = [m,] |
---|
116 | llh = [m,] |
---|
117 | ttl = 0. |
---|
118 | for m2 in self.mipsp + ['TOTAL',]: |
---|
119 | if m2 in self.cc[m].dd: |
---|
120 | oo1 = open( 'html/tt/rq-%s-expt-%s.html' % (m,m2), 'w' ) |
---|
121 | oo1.write( htmltmpl_head ) |
---|
122 | oo1.write( '''<div class="demo">\n<div id="tabs">\n<ul>''' ) |
---|
123 | ks = sorted( self.cc[m].dd[m2].keys() ) |
---|
124 | for t in ks: |
---|
125 | this1 = '<li><a href="#tabs-%s">%s</a></li>' % (t,t ) |
---|
126 | oo1.write( this1 ) |
---|
127 | oo1.write( '</ul>' ) |
---|
128 | for k in ks: |
---|
129 | oo1.write( '<div id="tabs-%s">\n' % k ) |
---|
130 | oo1.write( '<table><tr>' ) |
---|
131 | for h in ['Frequency','Table','Label','Title','Description','UID' ]: |
---|
132 | oo1.write( '<td>%s</td>' % h ) |
---|
133 | for t in self.cc[m].dd[m2][k]: |
---|
134 | oo1.write( '\n</tr><tr>\n' ) |
---|
135 | oo1.write( string.join( ['<td>%s</td>' % x for x in t ] ) + '\n' ) |
---|
136 | oo1.write( '</tr></table></div>\n' ) |
---|
137 | |
---|
138 | oo1.write( '</body></html>' ) |
---|
139 | |
---|
140 | if m2 in self.cc[m].dd or m2 == 'TOTAL': |
---|
141 | if self.cc[m].a[m2] == 0: |
---|
142 | ll.append( '' ) |
---|
143 | llh.append( '' ) |
---|
144 | else: |
---|
145 | try: |
---|
146 | x = self.cc[m].a[m2]*2. |
---|
147 | s = vfmt( x ) |
---|
148 | if m2 == 'TOTAL': |
---|
149 | s1 = '<b>%s</b>' % s |
---|
150 | s = '{\\bf %s}' % s |
---|
151 | ll.append( s ) |
---|
152 | fn = '%s-%s_%s_%s.xlsx' % (m,m2,self.tiermax, self.pmax) |
---|
153 | kc = '_%s_%s' % (m,m2) |
---|
154 | sm = string.join( ['%s: %s' % (k,vfmt(self.cc[kc].a[k]*2.)) for k in sorted( self.cc[kc].a.keys() ) ], '; ' ) |
---|
155 | if m2 == 'TOTAL': |
---|
156 | llh.append( s1 ) |
---|
157 | elif sss: |
---|
158 | fss = '%s-%s_%s_%s.html' % (m,m2,self.tiermax, self.pmax) |
---|
159 | llh.append( '<a title="Excel variable list: %s" href="tabs03/%s">%s</a>' % (sm,fss,s) ) |
---|
160 | else: |
---|
161 | llh.append( '<a title="Excel variable list: %s" href="data/tabs02/%s">%s</a>' % (sm,fn,s) ) |
---|
162 | except: |
---|
163 | print ( 'Failed to compute element: %s,%s %s' % (m,m2, str(self.cc[m].a[m2]) ) ) |
---|
164 | raise |
---|
165 | else: |
---|
166 | ll.append( '' ) |
---|
167 | llh.append( '' ) |
---|
168 | oo.write( string.join(ll, ' & ' ) + '\\\\ \n\\hline\n' ) |
---|
169 | mmh.append( '<tr>' + string.join(['<td>%s</td>' % x for x in llh] ) + '</tr>\n' ) |
---|
170 | mmh.append( '</table>' ) |
---|
171 | ttl = 'Data volume overview, upto tier %s and priority %s -- provisional' % (self.tiermax, self.pmax) |
---|
172 | bdy = '''<h1>%s</h1> |
---|
173 | <p>Data volumes are estimated for nominal model with 1 degree resolution and 40 levels in the atmosphere and 0.5 degrees with 60 levels in the ocean. The "Requesting MIP" (rows) is the MIP specifying the data required to meet their scientific objectives. The "designing MIP" (columns) is the MIP specifying the experimental design. <b>The figures below represent work in progress: there are still omissions and flaws, more details are on the |
---|
174 | <a href="https://earthsystemcog.org/projects/wip/CMIP6DataRequest" title="Data Request CoG page">Data Request home page</a>.</b></p> |
---|
175 | ''' % ttl |
---|
176 | bdy += string.join( mmh, '\n' ) |
---|
177 | ooh = open( 'tab01_%s_%s.html' % (self.tiermax,self.pmax), 'w' ) |
---|
178 | ooh.write( self.dq.pageTmpl % (ttl, jsh, './', './index.html', bdy ) ) |
---|
179 | ooh.close() |
---|
180 | oo.close() |
---|
181 | |
---|
182 | |
---|
183 | class tablesxxxx(object): |
---|
184 | def __init__(self,dq): |
---|
185 | self.dq = dq |
---|
186 | def doTable(self,m,l1,m2,collector,acc=True): |
---|
187 | """acc allows accumulation of values to be switched off when called in single expt mode""" |
---|
188 | |
---|
189 | x = sc.volByExpt( l1, m2, expFullEx=(m2 in self.mips), pmax=self.pmax ) |
---|
190 | if x[0] > 0: |
---|
191 | collector[m].a[m2] += x[0] |
---|
192 | im2 = self.dq.inx.uid[m2] |
---|
193 | # |
---|
194 | # create sum for each table |
---|
195 | # |
---|
196 | xs = 0 |
---|
197 | for k in x[2].keys(): |
---|
198 | i = self.dq.inx.uid[k] |
---|
199 | xxx = x[2][k] |
---|
200 | xs += xxx |
---|
201 | if xxx > 0: |
---|
202 | collector['_%s_%s' % (m,m2)].a[i.mipTable] += xxx |
---|
203 | assert x[0] == xs, 'ERROR.0088: consistency problem %s %s %s %s' % (m,m2,x[0],xs) |
---|
204 | if x[0] == 0: |
---|
205 | print 'Zero size:',m,m2 |
---|
206 | if len( x[2].keys() ) > 0: |
---|
207 | print 'ERROR:zero: ',m,m2,x[2].keys() |
---|
208 | |
---|
209 | if acc: |
---|
210 | collector[m].a['TOTAL'] += x[0] |
---|
211 | dd = collections.defaultdict( list ) |
---|
212 | lll = set() |
---|
213 | for v in x[2].keys(): |
---|
214 | vi = sc.dq.inx.uid[v] |
---|
215 | if vi._h.label != 'remarks': |
---|
216 | f,t,l,tt,d,u = (vi.frequency,vi.mipTable,vi.label,vi.title,vi.description,vi.uid) |
---|
217 | lll.add(u) |
---|
218 | dd[t].append( (f,t,l,tt,d,u) ) |
---|
219 | if len( dd.keys() ) > 0: |
---|
220 | collector[m].dd[m2] = dd |
---|
221 | if im2._h.label == 'experiment': |
---|
222 | dothis = self.tiermax >= im2.tier |
---|
223 | ### |
---|
224 | ### BUT ... there is a treset in the request item .... it may be that some variables are excluded ... |
---|
225 | ### need the variable list itself ..... |
---|
226 | ### |
---|
227 | makeTables.makeTab( subset=lll, dest='tab2/%s-%s_%s_%s.xlsx' % (m,m2,self.tiermax,self.pmax) ) |
---|
228 | |
---|
229 | if __name__ == "__main__": |
---|
230 | sc = scope.dreqQuery() |
---|
231 | r = r1( sc, tiermax=1, pmax=1 ) |
---|
232 | r = r1( sc, tiermax=3, pmax=3 ) |
---|