8 import matplotlib.pyplot
as plt
9 import output_parser_globals
as g
11 ''' Scope of this script 12 - parse the pythia output to produce nice summarized tables 23 for country, data
in g.countries.items():
24 g.regions[country] = data[1]
25 g.sortedregions = sorted(g.regions)
28 specieswithAggregates = g.spGroups
29 specieswithAggregates.extend(g.spAggregates.keys())
30 tempSpecieswithAggregates = specieswithAggregates
32 tempSpGroups = g.spGroups
33 tempSpGroups.append(
"")
36 variablesWithAggregates = list(g.forVars.keys())
37 for variable
in g.forVars.keys():
39 if len(g.forVars[variable]) >= 3:
40 variablesWithAggregates.append(g.forVars[variable][3])
42 for region
in g.regions.keys():
43 for variable
in variablesWithAggregates:
44 for scenario
in g.scenarios.keys():
45 for spGroup
in tempSpecieswithAggregates:
47 key = region, variable, scenario, spGroup, year
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
60 for ifile
in g.forIFiles:
61 idata_raw = csv.DictReader(open(ifile,
'r'), delimiter=g.sep) 64 iForType = rec[
'forType']
65 if iForType ==
'broadL':
67 for spAggregateKey, spAggregate
in g.spAggregates.items():
68 if (len(spAggregate) >= 3
and iForType == spAggregate[3]):
69 iForType = spAggregateKey
71 key = rec[
'region'],rec[
'parName'],rec[
'scen'],iForType,rec[
'year']
73 g.idata[key] += float (rec[
'value'])
75 for ifile
in g.prodIFiles:
76 idata_raw = csv.DictReader(open(ifile,
'r'), delimiter=g.sep) 79 key = rec[
'region'],rec[
'parName'],rec[
'scen'],rec[
'prod'],rec[
'year']
81 g.idata[key] += float (rec[
'value'])
83 for ifile
in g.carbonIFiles:
85 idata_raw = csv.DictReader(open(ifile,
'r'), delimiter=g.sep) 88 key = rec[
'region'],rec[
'balItem'],rec[
'scen'],
"",rec[
'year']
91 g.idata[key] += float (rec[
'value'])
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'):
129 for scenario
in g.scenarios.keys():
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]
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:
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]
150 for scenario
in g.scenarios.keys():
151 for spGroup
in tempSpGroups:
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]
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
204 key_year = region, variable, scenario, spGroup, year
205 g.odata[key].append(g.idata[key_year])
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')
223 def plotMultivariable(scenarios_h, variables_h, region, title, filename, printLegend=True, fwidth=10, fheight=15):
225 nvar = len(variables_h)
226 nscen = len(scenarios_h)
234 fig.set_size_inches(10,fheight)
239 for i
in range(nvar):
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)
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]
253 key = region, variables_h[i], scenario, spGroup
255 plt.plot(g.x, y, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor)
256 handles, labels = ax.get_legend_handles_labels()
261 plt.figlegend(handles, labels, loc =
'lower center', ncol=3, shadow=
False, labelspacing=0., prop={
'size':12})
263 plt.savefig(g.chartoutdir+
"/"+filename+
"_"+region+
"."+g.charttype, dpi=300)
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")
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'],
288 nscen = len(scenarios_h)
291 matplotlib.rcParams.update({
'font.size': 22})
295 fig.set_size_inches(12,10)
300 totals = [[0]*len(g.x)]* nscen
303 for idg, cGroup
in enumerate(cVariables):
304 for ids, scenario
in enumerate(scenarios_h):
305 grTotals = [0]*len(g.x)
310 serieColor = g.scenarios[scenario]
311 serieLineType = cGroup[2]
312 serieWidth = cGroup[3]
313 for var
in cGroup[1]:
314 key = region, var, scenario,
"" 315 varData = g.odata[key]
316 grTotals = [x2+y
for x2, y
in zip(grTotals, varData)]
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)
324 for cGroup
in cVariables:
325 y_local = np.zeros(len(g.x))
326 fillColour = cGroup[4]
327 for var
in cGroup[1]:
328 key = region, var, scenarios_h[0],
"" 329 varData = np.array(g.odata[key])
331 y_local = [t+(a/1000)
for t, a
in zip(y_local, varData)]
333 fillColours.append(fillColour)
334 for cGroup
in reversed(cVariables):
336 fillColour = cGroup[4]
337 plt.plot([], [], color=fillColour, linewidth=4, label=serieName)
339 ax = fig.add_subplot(111)
340 ax.stackplot(g.x, y, colors=fillColours, edgecolor =
"none")
341 ax.autoscale_view(
'tight')
344 plt.legend(loc=
'lower right', ncol=1, shadow=
False, labelspacing=0., prop={
'size':14})
346 plt.savefig(g.chartoutdir+
"/"+filename+
"_"+region+
"."+g.charttype, dpi=300)
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")
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") 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) 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") 388 nscen = len(scenarios_h)
390 fheight = (15/15)*nscen+0.2
391 fig.set_size_inches(10,fheight)
396 ax =fig.add_subplot(111)
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]
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)
411 plt.savefig(g.chartoutdir+
"/"+filename+
"."+g.charttype, bbox_inches=
'tight', pad_inches=0.1, dpi=300)
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")
433 def plotVectorChart_inner(origin,end1,endt,xlabel,ylabel,filename, comp1_color='red', totcomp_color='blue', diffcomp_color='green'):
435 Plot a 2-d vector difference 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 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 448 matplotlib.rc(
'xtick', labelsize=30)
449 matplotlib.rc(
'ytick', labelsize=30)
450 matplotlib.rcParams.update({
'font.size': 26})
456 if(origin[0] == end1[0] == endt[0]):
458 fig.set_size_inches(6,10)
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)
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)
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)
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)
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])
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])
492 ax.set_ylim([miny- (centre[1]-miny)*0.4, maxy + (maxy-centre[1])*0.4])
499 plt.savefig(filename, dpi=300, transparent=
False, bbox_inches=
'tight', pad_inches=0.1)
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) 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)
520 label_ref_scenario = ref_scenario.replace(
"_",
"\\_")
522 for comp_scenario
in comparing_scenarios:
523 labels_comparing_scenarios.append(comp_scenario.replace(
"_",
"\\_"))
525 if (singleComparation
and nscen == 1):
526 label_comparing_scenario = labels_comparing_scenarios[0]
528 if (singleComparation):
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):
545 oString +=
"\\hline\n" 546 if (singleComparation):
548 oString += d+label_ref_scenario+d+label_comparing_scenario+d+
"difference"+d+
"cv"+el+
"\n" 550 oString += d+label_ref_scenario+d+label_comparing_scenario+d+
"difference"+el+
"\n" 552 oString += d+label_ref_scenario
553 for label_comparing_scenarios
in labels_comparing_scenarios:
554 oString += d+label_comparing_scenarios
557 for region
in regions_h:
558 oString +=
"\\hline\n" 560 oString +=
"\\multicolumn{"+str(ncol)+
"}{l}{"+regions[region]+
"}"+el+
'\n' 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(
"_",
"\\_")
567 sumCScenarios = [0] * nscen
569 valCScenarios = [0] * nscen
571 rYear = str(refYear)
if refYear
else year
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)
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" 597 ofilename = g.tableoutdir+
'/'+filename+
'.tex' 598 ofile = open(ofilename,
'w')
602 omasterfilename = g.tableoutdir+
'/'+g.tablesmaster+
'.tex' 603 omfile = open(omasterfilename,
'a')
604 omfile.write(
"\\input{\""+g.tableoutdir+
'/'+filename+
".tex\"}\n")
608 def printAATable(ref_scenarios, comparing_scenarios, regions_h, years_h, title, filename, refYear=0) :
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 !")
620 nyears = len(years_h)
621 nregions = len(regions_h)
623 for scenario
in comparing_scenarios:
624 scenario_labels.append(scenario.replace(
"_",
"\\_"))
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):
639 oString +=
"\\hline\n" 641 for scenario
in scenario_labels:
642 oString += d+scenario
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)):
652 rYear = str(refYear)
if refYear
else year
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)
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" 670 ofilename = g.tableoutdir+
'/'+filename+
'.tex' 671 ofile = open(ofilename,
'w')
675 omasterfilename = g.tableoutdir+
'/'+g.tablesmaster+
'.tex' 676 omfile = open(omasterfilename,
'a')
677 omfile.write(
"\\input{\""+g.tableoutdir+
'/'+filename+
".tex\"}\n")
681 def printCarbonTable(ref_scenario, comparing_scenarios, region, year_start, year_end, title, filename, avg=False, singleComparation=True ) :
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"]
697 [
'Emissions',
"- Net substitution",
698 [[
'EM_ENSUB',
"- Energy substitution"],
699 [
'EM_MATSUB',
"- Material substitution"],
700 [
'EM_FOROP',
"- Emissions from forest operations"]
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
709 label_ref_scenario = ref_scenario.replace(
"_",
"\\_")
711 for comp_scenario
in comparing_scenarios:
712 labels_comparing_scenarios.append(comp_scenario.replace(
"_",
"\\_"))
714 if (singleComparation
and nscen == 1):
715 label_comparing_scenario = labels_comparing_scenarios[0]
717 if (singleComparation):
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):
734 oString +=
"\\hline\n" 736 if (singleComparation):
738 oString += d+
"\\texttt{"+label_ref_scenario+
"}"+d+
"\\texttt{"+label_comparing_scenario+
"}"+d+
"difference"+d+
"cv"+el+
"\n" 740 oString += d+
"\\texttt{"+label_ref_scenario+
"}"+d+
"\\texttt{"+label_comparing_scenario+
"}"+d+
"difference"+el+
"\n" 742 oString += d+label_ref_scenario
743 for label_comparing_scenarios
in labels_comparing_scenarios:
744 oString += d+label_comparing_scenarios
748 oString +=
"\\multicolumn{"+str(ncol)+
"}{l}{Carbon balance ($Mt~ \ce{CO2}eq.~y^{-1}$)}"+el+
'\n' 750 oString +=
"\\multicolumn{"+str(ncol)+
"}{l}{Carbon balance ($Mt~ \ce{CO2}eq.)$}"+el+
'\n' 753 totSumValRScenario = 0
754 totSumValCScenarios = [0] * nscen
755 for vargroup
in cvariables:
757 grSumValRScenario = 0
758 grSumValCScenarios = [0] * nscen
759 oString +=
"\\multicolumn{"+str(ncol)+
"}{l}{"+vargroup[0]+
"}"+el+
'\n' 761 for cvar
in vargroup[2]:
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
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)
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" 792 ofilename = g.tableoutdir+
'/'+filename+
'.tex' 793 ofile = open(ofilename,
'w')
797 omasterfilename = g.tableoutdir+
'/'+g.tablesmaster+
'.tex' 798 omfile = open(omasterfilename,
'a')
799 omfile.write(
"\\input{\""+g.tableoutdir+
'/'+filename+
".tex\"}\n")
802 def printTableRecord(cvar_label, d, el, nscen, valRScenario, valCScenarios, singleComparation):
805 if singleComparation:
806 avgCScenarios = sum(valCScenarios) / float(nscen)
807 scenarioDiff = avgCScenarios-valRScenario
808 scenarioRelativeDiff = 100 * scenarioDiff/valRScenario
if valRScenario
else 0.0
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' 825 oString += cvar_label+d+
"%0.3f"%(valRScenario)+d+
"%0.3f"%(avgCScenarios)+d+
"%0.3f"%(scenarioDiff)+
' ('+
"%0.2f"%(scenarioRelativeDiff)+
'\\%)'+el+
'\n' 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)+
'\\%' 841 filename = g.tableoutdir+
'/'+g.tablesmaster+
'.tex' 843 filename = g.chartoutdir+
'/'+g.chartsmaster+
'.tex' 845 print (
"Error in printTable: not know where to print the title !")
847 file = open(filename,
'a')
849 file.write(
"\n\\clearpage\n")
850 file.write(
"\\"+level+
"{"+title+
"}\n")
857 filename = g.tableoutdir+
'/'+g.tablesmaster+
'.tex' 859 filename = g.chartoutdir+
'/'+g.chartsmaster+
'.tex' 861 print (
"Error in text: not know where to print the title !")
863 file = open(filename,
'a')
864 file.write(text_h+
"\n")
869 if sys.version_info < (3, 0):
870 return unicode(astring,
'utf_8')
def printTable(ref_scenario, comparing_scenarios, variables_h, regions_h, years_h, title, filename, singleComparation=False, refYear=0)
def plotVectorChart_inner(origin, end1, endt, xlabel, ylabel, filename, comp1_color='red', totcomp_color='blue', diffcomp_color='green')
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)