FFSM++  1.1.0
French Forest Sector Model ++
output_parser_lib.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os, sys
4 import csv, math
5 #from numba import jit
6 import numpy as np
7 import matplotlib
8 import matplotlib.pyplot as plt
9 import output_parser_globals as g
10 
11 ''' Scope of this script
12 - parse the pythia output to produce nice summarized tables
13 '''
14 
15 
16 # =============================================================================
17 # jit decorator tells Numba to compile this function.
18 # The argument types will be inferred by Numba when function is called.
20  #print ("Loading and preparing the data..")
21 
22  # A - creating empty dictionaries with just the keys..
23  for country, data in g.countries.items():
24  g.regions[country] = data[1] # add 11000: 'France' to regions
25  g.sortedregions = sorted(g.regions)
26  #k = d.keys(); k.sort(). Use k = sorted(d)
27 
28  specieswithAggregates = g.spGroups
29  specieswithAggregates.extend(g.spAggregates.keys())
30  tempSpecieswithAggregates = specieswithAggregates
31  #tempSpecieswithAggregates.append("") # attenction that python doesn not create a new variable, just alias the two
32  tempSpGroups = g.spGroups
33  tempSpGroups.append("")
34 
35 
36  variablesWithAggregates = list(g.forVars.keys())
37  for variable in g.forVars.keys():
38  #'expReturns': ['Expected returns','€/ha','forArea','totalExpReturns','globalft'],
39  if len(g.forVars[variable]) >= 3:
40  variablesWithAggregates.append(g.forVars[variable][3])
41 
42  for region in g.regions.keys():
43  for variable in variablesWithAggregates:
44  for scenario in g.scenarios.keys():
45  for spGroup in tempSpecieswithAggregates:
46  for year in g.years:
47  key = region, variable, scenario, spGroup, year
48  g.idata[key] = 0.0
49  for region in g.regions.keys():
50  for variable in variablesWithAggregates:
51  for scenario in g.scenarios.keys():
52  for spGroup in tempSpecieswithAggregates:
53  key = region, variable, scenario, spGroup
54  g.odata[key] = []
55  for year in g.years:
56  g.x.append(int(year))
57 
58 
59  # B - loading data..
60  for ifile in g.forIFiles:
61  idata_raw = csv.DictReader(open(ifile, 'r'), delimiter=g.sep)
62  for rec in idata_raw:
63  # scen;parName;country;region;forType;diamClass;year;value;
64  iForType = rec['forType']
65  if iForType == 'broadL':
66  debug = True
67  for spAggregateKey, spAggregate in g.spAggregates.items():
68  if (len(spAggregate) >= 3 and iForType == spAggregate[3]):
69  iForType = spAggregateKey
70  break
71  key = rec['region'],rec['parName'],rec['scen'],iForType,rec['year']
72  if key in g.idata:
73  g.idata[key] += float (rec['value'])
74  debug = g.idata
75  for ifile in g.prodIFiles:
76  idata_raw = csv.DictReader(open(ifile, 'r'), delimiter=g.sep)
77  for rec in idata_raw:
78  # scen;parName;country;region;prod;freeDim;year;value;
79  key = rec['region'],rec['parName'],rec['scen'],rec['prod'],rec['year']
80  if key in g.idata:
81  g.idata[key] += float (rec['value'])
82 
83  for ifile in g.carbonIFiles:
84  #print (g.carbonIFiles)
85  idata_raw = csv.DictReader(open(ifile, 'r'), delimiter=g.sep)
86  for rec in idata_raw:
87  # scen;parName;country;region;forType;diamClass;year;value;
88  key = rec['region'],rec['balItem'],rec['scen'],"",rec['year']
89  #print key
90  if key in g.idata:
91  g.idata[key] += float (rec['value'])
92  #print (key)
93  #print (g.idata[key])
94 
95  #exit(1)
96 
97  # C - creating aggregated data for variables that need to be pondered
98 # for variable in g.forVars.keys():
99 # #'expReturns': ['Expected returns','€/ha','forArea', 'totalExpReturns'],
100 # if len(g.forVars[variable]) >= 3:
101 # pondVariable = g.forVars[variable][2]
102 # totalVariable = g.forVars[variable][3]
103 # for region in g.regions.keys():
104 # for scenario in g.scenarios.keys():
105 # for spGroup in specieswithAggregates:
106 # for year in g.years:
107 # key = region, variable, scenario, spGroup, year
108 # key_tvar = region, totalVariable, scenario, spGroup, year
109 # if(g.forVars[variable][4] == 'sameft'):
110 # key_pvar = region, pondVariable, scenario, spGroup, year
111 # g.idata[key_tvar] = g.idata[key] * g.idata[key_pvar]
112 # elif(g.forVars[variable][4] == 'globalft'):
113 # totalPvar = 0.0;
114 # for spGroup2 in g.spGroups:
115 # key_pvar = region, pondVariable, scenario, spGroup2, year
116 # totalPvar +=g.idata[key_pvar]
117 # g.idata[key_tvar] = g.idata[key] * totalPvar
118 # else:
119 # print("Error, I don't know how to handle this ponderation method: "+g.forVars[variable][4])
120 # exit(1)
121 
122  # D - performing various summing up..
123 
124  # summing up the specie aggregation
125  for spAggregate, species in g.spAggregates.items():
126  for region in g.regions.keys():
127  for variable in variablesWithAggregates:
128  if(variable != 'expReturns' and variable != 'sumExpReturns'): # let's skip these as the sumExpReturns at group/forest levels are already exogenously read as these are not the sums
129  for scenario in g.scenarios.keys():
130  for year in g.years:
131  destKey = region, variable, scenario, spAggregate, year
132  g.idata[destKey] = 0.0
133  for specie in species[0]:
134  varToBeSumKey = region, variable, scenario, specie, year
135  g.idata[destKey] += g.idata[varToBeSumKey]
136 
137  # summing up to the country level..
138  for country, regionsInTheCountry in g.countries.items():
139  for variable in variablesWithAggregates:
140  for scenario in g.scenarios.keys():
141  for spGroup in tempSpGroups:
142  for year in g.years:
143  destKey = country, variable, scenario, spGroup, year
144  g.idata[destKey] = 0.0
145  for regionInTheCountry in regionsInTheCountry[0]:
146  varToBeSumKey = regionInTheCountry, variable, scenario, spGroup, year
147  g.idata[destKey] += g.idata[varToBeSumKey]
148 
149  # Correcting the country aggregation of expected returns
150  for scenario in g.scenarios.keys():
151  for spGroup in tempSpGroups:
152  for year in g.years:
153  countryForArea_key = country,'forArea',scenario,'00_Total',year
154  countrySumExpReturns_key = country, 'sumExpReturns', scenario, spGroup, year
155  target_key = country,'expReturns', scenario, spGroup, year
156  g.idata[target_key] = g.idata[countrySumExpReturns_key]/ g.idata[countryForArea_key]
157 
158  # checking country aggregation, ok
159  #for country, regionsInTheCountry in countries.iteritems():
160  #print "country: " + country + " " + str(idata[country,'vol', 'vRegFixed', 'broadL_highF', '2006'])
161  #for regionInTheCountry in regionsInTheCountry[0]:
162  #print "region: " + regionInTheCountry + " " + str(idata[regionInTheCountry,'vol', 'vRegFixed', 'broadL_highF', '2006'])
163 
164 
165 
166  # testing specie aggregating
167  #for spAggregate, species in spAggregates.iteritems():
168  #print "aggregate: "+ spAggregate + " " + str(idata['11042','vol', 'vRegFixed', spAggregate, '2006'])
169  #for specie in species[0]:
170  #print "specieGroup: " + specie + " " + str(idata['11042','vol', 'vRegFixed', specie, '2006'])
171 
172 # # E - after all the summing up let's compute the poundered value
173 # for variable in g.forVars.keys():
174 # #'expReturns': ['Expected returns','€/ha','forArea', 'totalExpReturns'],
175 # if len(g.forVars[variable]) >= 3:
176 # pondVariable = g.forVars[variable][2]
177 # totalVariable = g.forVars[variable][3]
178 # for region in g.regions.keys():
179 # for scenario in g.scenarios.keys():
180 # for spGroup in specieswithAggregates:
181 # for year in g.years:
182 # key = region, variable, scenario, spGroup, year
183 # key_pvar = region, pondVariable, scenario, spGroup, year
184 # key_tvar = region, totalVariable, scenario, spGroup, year
185 # g.idata[key] = (g.idata[key_tvar] / g.idata[key_pvar]) if g.idata[key_pvar] != 0 else 0
186 
187  # testing ponderation variables
188  #for variable in variables.keys():
189  #'expReturns': ['Expected returns','€/ha','forArea', 'totalExpReturns'],
190  #if len(variables[variable]) >= 3:
191  #pondVariable = variables[variable][2]
192  #totalVariable = variables[variable][3]
193  #print "Orig variable: " + variable + " " + str(idata['11000', variable, 'vRegFixed','Total', '2006'])
194  #print "Pond variable: " + pondVariable + " " + str(idata['11000', pondVariable, 'vRegFixed', 'Total', '2006'])
195  #print "Total variable: " + totalVariable + " " + str(idata['11000', totalVariable, 'vRegFixed', 'Total', '2006'])
196 
197  # F - converting everything in years array
198  for region in g.regions.keys():
199  for variable in variablesWithAggregates:
200  for scenario in g.scenarios.keys():
201  for spGroup in tempSpecieswithAggregates:
202  key = region, variable, scenario, spGroup
203  for year in g.years:
204  key_year = region, variable, scenario, spGroup, year
205  g.odata[key].append(g.idata[key_year])
206 
207  # testing odata
208  #print "idata[2005]: " + str(idata['11000', 'vol', 'vRegFixed','Total', '2005'])
209  #print "idata[2006]: " + str(idata['11000', 'vol', 'vRegFixed','Total', '2006'])
210  #print "odata: " + str(odata['11000', 'vol', 'vRegFixed','Total'])
211 
212 # =============================================================================
214  # G - Reset latex files
215  filename_t = g.tableoutdir+'/'+g.tablesmaster+'.tex'
216  filename_c = g.chartoutdir+'/'+g.chartsmaster+'.tex'
217  file_t = open(filename_t,'w')
218  file_c = open(filename_c,'w')
219  file_t.close()
220  file_c.close()
221 
222 # =============================================================================
223 def plotMultivariable(scenarios_h, variables_h, region, title, filename, printLegend=True, fwidth=10, fheight=15):
224 
225  nvar = len(variables_h)
226  nscen = len(scenarios_h)
227  #plt.figure(1)
228  fig = plt.gcf()
229  # suggested: fheight = (15/5)*nvar+0.2
230  #if nvar == 1:
231  # fheight = 4
232  #if nvar == 2:
233  # fheight = 8
234  fig.set_size_inches(10,fheight) # 15 inches height is fine with 4 variables
235  maintitle = myunicode(title)
236  handles =[]
237  labels = []
238  #plt.suptitle(maintitle, fontsize=16, ha='center')
239  for i in range(nvar):
240  #plt.subplot(nvar,1,i+1)
241  ax =fig.add_subplot(nvar,1,i+1)
242  subplotTitle = myunicode(g.forVars[variables_h[i]][0])
243  ylabel = myunicode(g.forVars[variables_h[i]][1])
244  plt.title(subplotTitle)
245  plt.ylabel(ylabel)
246  for spGroup in sorted(g.spAggregates.keys()):
247  for scenario in scenarios_h:
248  serieName = myunicode(spGroup + " - " + scenario)
249  serieColor = g.scenarios[scenario]
250  serieLineType = g.spAggregates[spGroup][1]
251  serieWidth = g.spAggregates[spGroup][2]
252  #print serieName+ " - " + serieLineType + " - " + str(serieWidth)
253  key = region, variables_h[i], scenario, spGroup
254  y = g.odata[key]
255  plt.plot(g.x, y, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor)
256  handles, labels = ax.get_legend_handles_labels()
257  #plt.subplots_adjust(hspace=0.6)
258  #handles, labels = ax.get_legend_handles_labels()
259  #ax.legend(handles, labels, ncol=3, shadow=False, title="Legend")
260  if printLegend:
261  plt.figlegend(handles, labels, loc = 'lower center', ncol=3, shadow=False, labelspacing=0., prop={'size':12})
262  #plt.savefig(chartoutdir+"/"+filename+"_"+region+"."+charttype, bbox_inches='tight', dpi=300)
263  plt.savefig(g.chartoutdir+"/"+filename+"_"+region+"."+g.charttype, dpi=300)
264  #plt.show()
265  plt.close()
266 
267  omasterfilename = g.chartoutdir+'/'+g.chartsmaster+'.tex'
268  omfile = open(omasterfilename,'a')
269  omfile.write("\\begin{figure}[htbp]\n")
270  omfile.write(" \\centering\n")
271  omfile.write(" \\caption{"+title+"}\n")
272  omfile.write(" \\includegraphics[width=0.8\\textwidth]{\""+g.chartoutdir+"/"+filename+"_"+region+"\"}\n")
273  omfile.write(" \\label{fig:"+filename+"}\n")
274  omfile.write("\\end{figure}\n")
275  omfile.close()
276 
277 # =============================================================================
278 def plotCarbonChart(scenarios_h,region,title, filename):
279 #def plotMultivariable(scenarios_h, variables_h, region, title, filename, printLegend=True):
280 
281 
282  cVariables = [
283  ['Forest pool', ['STOCK_INV','STOCK_EXTRA'],':',3,'#314004'],
284  ['Wood products pool', ['STOCK_PRODUCTS'],'--',3,'#7f0021'],
285  ['Net cumulative substitution effect', ['EM_ENSUB','EM_MATSUB','EM_FOROP'],'-',4,'#83caff'],
286  ]
287 
288  nscen = len(scenarios_h)
289 
290 
291  matplotlib.rcParams.update({'font.size': 22})
292 
293 
294  fig = plt.gcf()
295  fig.set_size_inches(12,10)
296  ylabel = myunicode("Gt CO2 eq")
297  plt.title(myunicode(title))
298  plt.ylabel(ylabel)
299 
300  totals = [[0]*len(g.x)]* nscen
301 
302  if nscen > 1: #normal line plots
303  for idg, cGroup in enumerate(cVariables):
304  for ids, scenario in enumerate(scenarios_h):
305  grTotals = [0]*len(g.x)
306  #serieName = myunicode(cGroup[0] + " - " + scenario)
307  serieName = "_"+myunicode(scenario) # not shown in legend
308  if idg==2:
309  serieName = myunicode(scenario)
310  serieColor = g.scenarios[scenario]
311  serieLineType = cGroup[2]
312  serieWidth = cGroup[3]
313  for var in cGroup[1]: # for idx, var in enumerate(cGroup[1]):
314  key = region, var, scenario, ""
315  varData = g.odata[key]
316  grTotals = [x2+y for x2, y in zip(grTotals, varData)]
317 
318  totals[ids] = [x3+y2 for x3, y2 in zip(totals[ids],grTotals)]
319  y = [x4 / 1000 for x4 in totals[ids]]
320  plt.plot(g.x, y, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor)
321  else: #area stacked plot
322  fillColours = []
323  y = []
324  for cGroup in cVariables:
325  y_local = np.zeros(len(g.x))
326  fillColour = cGroup[4]
327  for var in cGroup[1]: # for idx, var in enumerate(cGroup[1]):
328  key = region, var, scenarios_h[0], ""
329  varData = np.array(g.odata[key])
330  #y_local += varData # For some reasons this doesn't work
331  y_local = [t+(a/1000) for t, a in zip(y_local, varData)]
332  y.append(y_local)
333  fillColours.append(fillColour)
334  for cGroup in reversed(cVariables):
335  serieName = myunicode(cGroup[0])
336  fillColour = cGroup[4]
337  plt.plot([], [], color=fillColour, linewidth=4, label=serieName) # plotting emply data hack as stackplot doesn't support the legend
338 
339  ax = fig.add_subplot(111)
340  ax.stackplot(g.x, y, colors=fillColours, edgecolor = "none")
341  ax.autoscale_view('tight')
342 
343  #plt.legend(loc='lower right', ncol=3, shadow=False, labelspacing=0., prop={'size':12})
344  plt.legend(loc='lower right', ncol=1, shadow=False, labelspacing=0., prop={'size':14})
345  #plt.ylim([0,18]) # This would scale the plot y axis to the desired ranges
346  plt.savefig(g.chartoutdir+"/"+filename+"_"+region+"."+g.charttype, dpi=300)
347  #plt.show()
348  plt.close()
349 
350  omasterfilename = g.chartoutdir+'/'+g.chartsmaster+'.tex'
351  omfile = open(omasterfilename,'a')
352  omfile.write("\\begin{figure}[htbp]\n")
353  omfile.write(" \\centering\n")
354  omfile.write(" \\caption{"+title+"}\n")
355  omfile.write(" \\includegraphics[width=0.8\\textwidth]{\""+g.chartoutdir+"/"+filename+"_"+region+"\"}\n")
356  omfile.write(" \\label{fig:"+filename+"}\n")
357  omfile.write("\\end{figure}\n")
358  omfile.close()
359 
360 """
361  scenTotals
362  y = odata[key]
363  plt.plot(x, y, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor)
364  handles, labels = ax.get_legend_handles_labels()
365  #plt.subplots_adjust(hspace=0.6)
366  #handles, labels = ax.get_legend_handles_labels()
367  #ax.legend(handles, labels, ncol=3, shadow=False, title="Legend")
368  if printLegend:
369  plt.figlegend(handles, labels, loc = 'lower center', ncol=3, shadow=False, labelspacing=0., prop={'size':12})
370  #plt.savefig(chartoutdir+"/"+filename+"_"+region+"."+charttype, bbox_inches='tight', dpi=300)
371  plt.savefig(chartoutdir+"/"+filename+"_"+region+"."+charttype, dpi=300)
372  #plt.show()
373  plt.close()
374 
375  omasterfilename = chartoutdir+'/'+chartsmaster+'.tex'
376  omfile = open(omasterfilename,'a')
377  omfile.write("\\begin{figure}[htbp]\n")
378  omfile.write(" \\centering\n")
379  omfile.write(" \\caption{"+title+"}\n")
380  omfile.write(" \\includegraphics[width=0.8\\textwidth]{"+chartoutdir+"/"+filename+"_"+region+"}\n")
381  omfile.write(" \\label{fig:"+filename+"}\n")
382  omfile.write("\\end{figure}\n")
383  omfile.close()
384  """
385 
386 # =============================================================================
387 def plotLegend(scenarios_h, filename, title_h=""):
388  nscen = len(scenarios_h)
389  fig = plt.gcf()
390  fheight = (15/15)*nscen+0.2
391  fig.set_size_inches(10,fheight)
392  #ax = plt.axes()
393  #ax.set_axis_off()
394 
395  #fig = plt.figure()
396  ax =fig.add_subplot(111)
397  ax.set_axis_off()
398 
399  for spGroup in sorted(g.spAggregates.keys()):
400  for scenario in scenarios_h:
401  serieName = myunicode(spGroup + " - " + scenario)
402  serieColor = g.scenarios[scenario]
403  serieLineType = g.spAggregates[spGroup][1]
404  serieWidth = g.spAggregates[spGroup][2]
405  #print serieName+ " - " + serieLineType + " - " + str(serieWidth)
406  dummyx = [1]
407  dummyy = [1]
408  plt.plot(dummyx, dummyy, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor)
409  handles, labels = ax.get_legend_handles_labels()
410  ax.legend(handles, labels, ncol=3, shadow=False) # removed title=title_h
411  plt.savefig(g.chartoutdir+"/"+filename+"."+g.charttype, bbox_inches='tight', pad_inches=0.1, dpi=300)
412  #plt.show()
413  plt.close()
414 
415  omasterfilename = g.chartoutdir+'/'+g.chartsmaster+'.tex'
416  omfile = open(omasterfilename,'a')
417  omfile.write("\\begin{figure}[htbp]\n")
418  omfile.write(" \\centering\n")
419  omfile.write(" \\caption{"+title_h+"}\n")
420  omfile.write(" \\includegraphics[width=0.8\\textwidth]{\""+g.chartoutdir+"/"+filename+"\"}\n")
421  omfile.write(" \\label{fig:"+filename+"}\n")
422  omfile.write("\\end{figure}\n")
423  omfile.close()
424 
425 #import matplotlib.pyplot as plt
426 #ax = plt.subplot() #create the axes
427 #ax.set_axis_off() #turn off the axis
428 #.... #do patches and labels
429 #ax.legend(patches, labels, ...) #legend alone in the figure
430 #plt.show()
431 
432 # =============================================================================
433 def plotVectorChart_inner(origin,end1,endt,xlabel,ylabel,filename, comp1_color='red', totcomp_color='blue', diffcomp_color='green'):
434  '''
435  Plot a 2-d vector difference
436  # @params:
437  # origin: x and y of the origin of the vectors
438  # end1: (x,y) coordinates of the ending of the first component vector
439  # end2: (x,y) coordinates of the ending of the total component of the vector
440  # xlabel: xlabel
441  # ylabel: ylabel
442  # filename: filename
443  # totcomp_color: color (English or #HTML_code) of the vector representing the total component
444  # comp1_color: color (English or #HTML_code) of the vector representing the first component
445  # diffcomp_color: color (English or #HTML_code) of the vector representing the difference component
446  '''
447 
448  matplotlib.rc('xtick', labelsize=30)
449  matplotlib.rc('ytick', labelsize=30)
450  matplotlib.rcParams.update({'font.size': 26})
451 
452  a = plt.figure()
453  ax = plt.gca()
454  fig = plt.gcf()
455  flag_2d = True
456  if(origin[0] == end1[0] == endt[0]):
457  flag_2d = False;
458  fig.set_size_inches(6,10)
459  else:
460  fig.set_size_inches(10,10)
461  end2 = (endt[0]-end1[0]+origin[0],endt[1]-end1[1]+origin[1])
462  minx = min(origin[0],end1[0],end2[0],endt[0])
463  maxx = max(origin[0],end1[0],end2[0],endt[0])
464  miny = min(origin[1],end1[1],end2[1],endt[1])
465  maxy = max(origin[1],end1[1],end2[1],endt[1])
466  centre = (((maxx-minx)/2)+minx,((maxy-miny)/2)+miny)
467 
468  # This allows to write a serie of arrows in one go, but didn't got how in this case colours work
469  #X = (origin[0], origin[0], origin[0])
470  #Y = (origin[1], origin[1], origin[1])
471  #X2 = (end1[0]-origin[0], endt[0]-origin[0], end2[0]-origin[0])
472  #Y2 = (end1[1]-origin[1], endt[1]-origin[1], end2[1]-origin[1])
473  #C = (255,10,150) # ? colour codes, but didn't got it
474  # ax.quiver(X,Y,X2,Y2,Cangles='xy',scale_units='xy',scale=1, width=0.008)
475 
476  # Printing first component..
477  ax.quiver(origin[0],origin[1],end1[0]-origin[0],end1[1]-origin[1],angles='xy',scale_units='xy',scale=1, width=0.008, color=comp1_color)
478  # Printing total component..
479  ax.quiver(origin[0],origin[1],endt[0]-origin[0],endt[1]-origin[1],angles='xy',scale_units='xy',scale=1, width=0.02, color=totcomp_color)
480  # Printing diff component..
481  ax.quiver(origin[0],origin[1],end2[0]-origin[0],end2[1]-origin[1],angles='xy',scale_units='xy',scale=1, width=0.008, color=diffcomp_color)
482 
483  x = (end1[0],end2[0])
484  y = (end1[1],end2[1])
485  x2 = (endt[0]-end1[0], endt[0]-end2[0])
486  y2 = (endt[1]-end1[1], endt[1]-end2[1])
487 
488  if(flag_2d):
489  ax.quiver(x,y,x2,y2,angles='xy',scale_units='xy',scale=1, linestyle='dotted', facecolor='none', linewidth=2, color='gray', edgecolors='k', width=0.0001, headwidth=300, headlength=500)
490  ax.set_xlim([minx- (centre[0]-minx)*0.4, maxx + (maxx-centre[0])*0.4])
491 
492  ax.set_ylim([miny- (centre[1]-miny)*0.4, maxy + (maxy-centre[1])*0.4])
493 
494  plt.xlabel(myunicode(xlabel))
495  plt.ylabel(myunicode(ylabel))
496  # Uncomment the following lines if you want to display instead of save the figure..
497  #plt.draw()
498  #plt.show()
499  plt.savefig(filename, dpi=300, transparent=False, bbox_inches='tight', pad_inches=0.1)
500 
501 
502 # =============================================================================
503 def printTable(ref_scenario, comparing_scenarios, variables_h, regions_h, years_h, title, filename, singleComparation=False, refYear=0):
504  """Print a LaTeX Table for variables variable_h comparing ref_scenario scenario vs coparing_scenarios.
505  @param singleComparation: if True multiple comparing scenarios are treated as multiple replications of the same scenario and
506  some basic stats are computed; if False they are all represented as diff from the ref_scenario.
507  @param refYear: if 0 reference vs comparing scenarios are compared on the same year (or average of years if years_h has length > 1.).
508  Otherwise the comparing scneario at year(s) years_h is compared with reference scenario at year refYear (useful to see the dynamic
509  effects within a single scenario)
510  """
511  d = " & "
512  el = " \\\\"
513  label_comparing_scenario = "comparing scenarios"
514  labels_comparing_scenarios = []
515  nvar = len(variables_h)
516  nscen = len(comparing_scenarios)
517  nyears = len(years_h)
518  nregions = len(regions_h)
519  ncol = 4
520  label_ref_scenario = ref_scenario.replace("_", "\\_")
521 
522  for comp_scenario in comparing_scenarios:
523  labels_comparing_scenarios.append(comp_scenario.replace("_", "\\_"))
524 
525  if (singleComparation and nscen == 1):
526  label_comparing_scenario = labels_comparing_scenarios[0]
527 
528  if (singleComparation):
529  if nscen > 2:
530  ncol = 5
531  else:
532  ncol = nscen+2 #+1 for the val label and +1 for the ref scenario
533 
534  oString = ""
535  oString += "\\begin{table}[htbp]\n"
536  oString += "\\begin{center}\n"
537  oString += "\\begin{threeparttable}\n"
538  oString += "\\centering\n"
539  oString += "\\caption{"+title+"}\n"
540  oString += "\\begin{footnotesize}\n"
541  oString += "\\begin{tabularx}{\\textwidth}{l "
542  for nc in range(1,ncol):
543  oString += "r "
544  oString += "}\n"
545  oString += "\\hline\n"
546  if (singleComparation):
547  if nscen > 2:
548  oString += d+label_ref_scenario+d+label_comparing_scenario+d+"difference"+d+"cv"+el+"\n"
549  else:
550  oString += d+label_ref_scenario+d+label_comparing_scenario+d+"difference"+el+"\n"
551  else:
552  oString += d+label_ref_scenario
553  for label_comparing_scenarios in labels_comparing_scenarios:
554  oString += d+label_comparing_scenarios
555  oString += el+'\n'
556 
557  for region in regions_h:
558  oString += "\\hline\n"
559  if nregions > 1:
560  oString += "\\multicolumn{"+str(ncol)+"}{l}{"+regions[region]+"}"+el+'\n'
561 
562  for variable in variables_h:
563  oString += "\\multicolumn{"+str(ncol)+"}{l}{"+g.forVars[variable][0]+" (\\textit{"+g.forVars[variable][1]+"})}"+el+'\n'
564  for spGroup in sorted(g.spAggregates.keys()):
565  outSpGroup = spGroup.replace("_", "\\_")
566  sumRScenario = 0
567  sumCScenarios = [0] * nscen
568  valRScenario = 0
569  valCScenarios = [0] * nscen
570  for year in years_h:
571  rYear = str(refYear) if refYear else year # If we overrided the reference year we gonna pick it up here
572  keyr = region, variable, ref_scenario, spGroup, rYear
573  sumRScenario += g.idata[keyr]
574  for s in range(nscen):
575  keyc = region, variable, comparing_scenarios[s], spGroup, year
576  sumCScenarios[s] += g.idata[keyc]
577  valRScenario = sumRScenario/nyears
578  for s in range(nscen):
579  valCScenarios[s] = sumCScenarios[s]/nyears
580  oString += printTableRecord("- "+outSpGroup, d, el, nscen, valRScenario, valCScenarios, singleComparation)
581 
582  oString += "\\hline\n"
583  oString += "\\end{tabularx}\n"
584  oString += "\\end{footnotesize}\n"
585  oString += "\\label{tab:"+filename+"}\n"
586  if (singleComparation and nscen > 2):
587  oString += "\\begin{tablenotes}\n"
588  oString += "\\begin{footnotesize}\n"
589  oString += "\\item [a] Significantly different from 0 at $\\alpha=0.01$\n"
590  oString += "\\item [b] Significantly different from 0 at $\\alpha=0.001$\n"
591  oString += "\\end{footnotesize}\n"
592  oString += "\\end{tablenotes}\n"
593  oString += "\\end{threeparttable}\n"
594  oString += "\\end{center}\n"
595  oString += "\\end{table}\n"
596 
597  ofilename = g.tableoutdir+'/'+filename+'.tex'
598  ofile = open(ofilename,'w')
599  ofile.write(oString)
600  ofile.close()
601 
602  omasterfilename = g.tableoutdir+'/'+g.tablesmaster+'.tex'
603  omfile = open(omasterfilename,'a')
604  omfile.write("\\input{\""+g.tableoutdir+'/'+filename+".tex\"}\n")
605  omfile.close()
606 
607 # =============================================================================
608 def printAATable(ref_scenarios, comparing_scenarios, regions_h, years_h, title, filename, refYear=0) :
609 #def printTable(ref_scenario, comparing_scenarios, variables_h, regions_h, years_h, title, filename):
610 #printAATable(['cc1','cc1_nospvar','cc2','cc2_nospvar','cc3','cc3_nospvar','cc3','cc3_nospvar'],['bau','bau_nospvar','bau','bau_nospvar','bau','bau_nospvar','bau','bau_nospvar'], selectedregions, ['2100'],'Area allocation [% variation over bau]', 'area_allocation')
611  d = " & "
612  el = " \\\\"
613 
614  scenario_labels = []
615  nscen = len(ref_scenarios)
616  nscen_comp = len(comparing_scenarios)
617  if nscen != nscen_comp:
618  print ("Error in printAATable: number of comparing vs reference scenarios must be the same !")
619  exit(1)
620  nyears = len(years_h)
621  nregions = len(regions_h)
622  ntotcol = nscen+1
623  for scenario in comparing_scenarios:
624  scenario_labels.append(scenario.replace("_", "\\_"))
625 
626 
627  oString = ""
628  oString += "\\begin{table}[htbp]\n"
629  oString += "\\begin{center}\n"
630  oString += "\\begin{threeparttable}\n"
631  oString += "\\centering\n"
632  oString += "\\caption{"+title.replace("_", "\\_").replace("%", "\\%")+"}\n"
633  oString += "\\begin{footnotesize}\n"
634  oString += "\\begin{tabularx}{\\textwidth}{l "
635  for i in range(nscen):
636  oString += " r"
637  oString += "}\n"
638 
639  oString += "\\hline\n"
640  oString += "Region"
641  for scenario in scenario_labels:
642  oString += d+scenario
643  oString += el+'\n'
644  for spGroup in sorted(g.spAggregates.keys()):
645  oString += "\\multicolumn{"+str(ntotcol)+"}{l}{"+spGroup.replace("_", "\\_")+"}"+el+'\n'
646  for region in regions_h:
647  oString += g.regions[region]
648  for s in range(len(comparing_scenarios)):
649  sum_value_b = 0.0
650  sum_value_c = 0.0
651  for year in years_h:
652  rYear = str(refYear) if refYear else year # If we overrided the reference year we gonna pick it up here
653  key_b = region, 'forArea', ref_scenarios[s], spGroup, rYear
654  key_c = region, 'forArea', comparing_scenarios[s], spGroup, year
655  sum_value_b += g.idata[key_b]
656  sum_value_c += g.idata[key_c]
657  reldiff = (100*(sum_value_c-sum_value_b)/sum_value_b) if sum_value_b != 0 else 0
658  oString += d+"%+0.3f"%(reldiff)
659  oString += el+'\n'
660 
661 
662  oString += "\\hline\n"
663  oString += "\\end{tabularx}\n"
664  oString += "\\end{footnotesize}\n"
665  oString += "\\label{tab:"+filename+"}\n"
666  oString += "\\end{threeparttable}\n"
667  oString += "\\end{center}\n"
668  oString += "\\end{table}\n"
669 
670  ofilename = g.tableoutdir+'/'+filename+'.tex'
671  ofile = open(ofilename,'w')
672  ofile.write(oString)
673  ofile.close()
674 
675  omasterfilename = g.tableoutdir+'/'+g.tablesmaster+'.tex'
676  omfile = open(omasterfilename,'a')
677  omfile.write("\\input{\""+g.tableoutdir+'/'+filename+".tex\"}\n")
678  omfile.close()
679 
680 # =============================================================================
681 def printCarbonTable(ref_scenario, comparing_scenarios, region, year_start, year_end, title, filename, avg=False, singleComparation=True ) :
682  #Print carbon balance
683  # @params:
684  # avg: true => output is the yearly average in the period,
685  # false => output is the difference between year_start and year_end
686  # singleComparation: true => comparing scenarios are seens as repetition of a unique scenario, hence stats on their variance is performed,
687  # false => each comparing scenarios is presented indipendently
688  d = " & "
689  el = " \\\\"
690 
691  cvariables = [
692  ['Pools', "- Total pools", [
693  ['STOCK_INV',"- Inventoried forest pool"],
694  ['STOCK_EXTRA',"- Extra forest pool (branches and roots)"],
695  ['STOCK_PRODUCTS',"- Wood products pool"]
696  ]],
697  ['Emissions', "- Net substitution",
698  [['EM_ENSUB',"- Energy substitution"],
699  ['EM_MATSUB',"- Material substitution"],
700  ['EM_FOROP',"- Emissions from forest operations"]
701  ]],
702  ]
703 
704  label_comparing_scenario = "comparing scenarios"
705  labels_comparing_scenarios = []
706  nscen = len(comparing_scenarios)
707  nyears = (int(year_end) - int(year_start) + 1) if avg else 1
708  ncol = 4
709  label_ref_scenario = ref_scenario.replace("_", "\\_")
710 
711  for comp_scenario in comparing_scenarios:
712  labels_comparing_scenarios.append(comp_scenario.replace("_", "\\_"))
713 
714  if (singleComparation and nscen == 1):
715  label_comparing_scenario = labels_comparing_scenarios[0]
716 
717  if (singleComparation):
718  if nscen > 2:
719  ncol = 5
720  else:
721  ncol = nscen+2
722 
723  oString = ""
724  oString += "\\begin{table*}[!htbp]\n"
725  oString += "\\begin{center}\n"
726  oString += "\\begin{threeparttable}\n"
727  oString += "\\centering\n"
728  oString += "\\caption{"+title+"}\n"
729  oString += "\\begin{footnotesize}\n"
730  oString += "\\begin{tabularx}{\\textwidth}{l "
731  for nc in range(1,ncol):
732  oString += "r "
733  oString += "}\n"
734  oString += "\\hline\n"
735 
736  if (singleComparation):
737  if nscen > 2:
738  oString += d+"\\texttt{"+label_ref_scenario+"}"+d+"\\texttt{"+label_comparing_scenario+"}"+d+"difference"+d+"cv"+el+"\n"
739  else:
740  oString += d+"\\texttt{"+label_ref_scenario+"}"+d+"\\texttt{"+label_comparing_scenario+"}"+d+"difference"+el+"\n"
741  else:
742  oString += d+label_ref_scenario
743  for label_comparing_scenarios in labels_comparing_scenarios:
744  oString += d+label_comparing_scenarios
745  oString += el+'\n'
746 
747  if(nyears > 1):
748  oString += "\\multicolumn{"+str(ncol)+"}{l}{Carbon balance ($Mt~ \ce{CO2}eq.~y^{-1}$)}"+el+'\n'
749  else:
750  oString += "\\multicolumn{"+str(ncol)+"}{l}{Carbon balance ($Mt~ \ce{CO2}eq.)$}"+el+'\n'
751 
752  # Total totals..
753  totSumValRScenario = 0
754  totSumValCScenarios = [0] * nscen
755  for vargroup in cvariables:
756  # Group totals..
757  grSumValRScenario = 0
758  grSumValCScenarios = [0] * nscen
759  oString += "\\multicolumn{"+str(ncol)+"}{l}{"+vargroup[0]+"}"+el+'\n'
760  # Working on the single variables..
761  for cvar in vargroup[2]:
762  cvar_name = cvar[0]
763  cvar_label = cvar[1]
764  valRScenario = (g.idata[region, cvar_name, ref_scenario, "", year_end]-g.idata[region, cvar_name, ref_scenario, "", year_start])/nyears
765  grSumValRScenario += valRScenario
766  totSumValRScenario += valRScenario
767  valCScenarios = [0] * nscen
768 
769  for s in range(nscen):
770  valCScenarios[s] = (g.idata[region, cvar_name, comparing_scenarios[s], "", year_end]-g.idata[region, cvar_name, comparing_scenarios[s], "", year_start])/nyears
771  grSumValCScenarios[s] += valCScenarios[s]
772  totSumValCScenarios[s] += valCScenarios[s]
773  oString += printTableRecord(cvar_label, d, el, nscen, valRScenario, valCScenarios,singleComparation)
774  oString += printTableRecord(vargroup[1], d, el, nscen, grSumValRScenario, grSumValCScenarios,singleComparation)
775  oString += printTableRecord("Total \ce{CO2} balance", d, el, nscen, totSumValRScenario, totSumValCScenarios,singleComparation)
776 
777  oString += "\\hline\n"
778  oString += "\\end{tabularx}\n"
779  oString += "\\end{footnotesize}\n"
780  oString += "\\label{tab:"+filename+"}\n"
781  if (singleComparation and nscen > 2):
782  oString += "\\begin{tablenotes}\n"
783  oString += "\\begin{footnotesize}\n"
784  oString += "\\item [a] Significantly different from 0 at $\\alpha=0.01$\n"
785  oString += "\\item [b] Significantly different from 0 at $\\alpha=0.001$\n"
786  oString += "\\end{footnotesize}\n"
787  oString += "\\end{tablenotes}\n"
788  oString += "\\end{threeparttable}\n"
789  oString += "\\end{center}\n"
790  oString += "\\end{table*}\n"
791 
792  ofilename = g.tableoutdir+'/'+filename+'.tex'
793  ofile = open(ofilename,'w')
794  ofile.write(oString)
795  ofile.close()
796 
797  omasterfilename = g.tableoutdir+'/'+g.tablesmaster+'.tex'
798  omfile = open(omasterfilename,'a')
799  omfile.write("\\input{\""+g.tableoutdir+'/'+filename+".tex\"}\n")
800  omfile.close()
801 # =============================================================================
802 def printTableRecord(cvar_label, d, el, nscen, valRScenario, valCScenarios, singleComparation):
803 
804  oString = ""
805  if singleComparation:
806  avgCScenarios = sum(valCScenarios) / float(nscen)
807  scenarioDiff = avgCScenarios-valRScenario
808  scenarioRelativeDiff = 100 * scenarioDiff/valRScenario if valRScenario else 0.0
809  if nscen > 2:
810  significance = ""
811  qdiffCScenarios = [0] * nscen
812  sumqdiffCScenarios = 0
813  for s in range(nscen):
814  qdiffCScenarios[s] = (valCScenarios[s] - avgCScenarios)**2.0
815  sumqdiffCScenarios += qdiffCScenarios[s]
816  sd = (sumqdiffCScenarios/(nscen-1))**0.5
817  t = abs(scenarioDiff)*nscen**0.5/sd if sd>0.0 else 0.0
818  cv = 100.0 * sd/abs(avgCScenarios) if abs(avgCScenarios)> 0.0 else 0.0
819  if t >= g.tvalue001[nscen-1-1]:
820  significance = '$^a$'
821  if t >= g.tvalue0001[nscen-1-1]:
822  significance = '$^b$'
823  oString += cvar_label+d+"%0.3f"%(valRScenario)+d+"%0.3f"%(avgCScenarios)+d+"%0.3f"%(scenarioDiff)+significance+' ('+"%0.3f"%(scenarioRelativeDiff)+'\\%)'+d+"%0.2f"%(cv)+' \\%'+el+'\n'
824  else:
825  oString += cvar_label+d+"%0.3f"%(valRScenario)+d+"%0.3f"%(avgCScenarios)+d+"%0.3f"%(scenarioDiff)+' ('+"%0.2f"%(scenarioRelativeDiff)+'\\%)'+el+'\n'
826  else:
827  oString += cvar_label+d+"%0.3f"%(valRScenario)
828  for valCScenario in valCScenarios:
829  scenarioDiff = valCScenario-valRScenario
830  scenarioRelativeDiff = 100 * scenarioDiff/valRScenario if valRScenario else 0.0
831  oString += d+"%0.2f"%(scenarioRelativeDiff)+'\\%'
832  oString += el + '\n'
833  return oString
834 
835 
836 
837 # =============================================================================
838 def title (cat, level, title):
839  filename = ""
840  if cat == 't':
841  filename = g.tableoutdir+'/'+g.tablesmaster+'.tex'
842  elif cat == 'c':
843  filename = g.chartoutdir+'/'+g.chartsmaster+'.tex'
844  else:
845  print ("Error in printTable: not know where to print the title !")
846  exit(1)
847  file = open(filename,'a')
848 
849  file.write("\n\\clearpage\n")
850  file.write("\\"+level+"{"+title+"}\n")
851  file.close()
852 
853 # =============================================================================
854 def text(cat, text_h):
855  filename = ""
856  if cat == 't':
857  filename = g.tableoutdir+'/'+g.tablesmaster+'.tex'
858  elif cat == 'c':
859  filename = g.chartoutdir+'/'+g.chartsmaster+'.tex'
860  else:
861  print ("Error in text: not know where to print the title !")
862  exit(1)
863  file = open(filename,'a')
864  file.write(text_h+"\n")
865  file.close()
866 
867 # =============================================================================
868 def myunicode(astring):
869  if sys.version_info < (3, 0):
870  return unicode(astring, 'utf_8')
871  else:
872  return astring
def printTable(ref_scenario, comparing_scenarios, variables_h, regions_h, years_h, title, filename, singleComparation=False, refYear=0)
def text(cat, text_h)
def plotVectorChart_inner(origin, end1, endt, xlabel, ylabel, filename, comp1_color='red', totcomp_color='blue', diffcomp_color='green')
def myunicode(astring)
def plotLegend(scenarios_h, filename, title_h="")
def printAATable(ref_scenarios, comparing_scenarios, regions_h, years_h, title, filename, refYear=0)
def printCarbonTable(ref_scenario, comparing_scenarios, region, year_start, year_end, title, filename, avg=False, singleComparation=True)
def printTableRecord(cvar_label, d, el, nscen, valRScenario, valCScenarios, singleComparation)
def title(cat, level, title)
def plotMultivariable(scenarios_h, variables_h, region, title, filename, printLegend=True, fwidth=10, fheight=15)
def plotCarbonChart(scenarios_h, region, title, filename)