* 	THIS FILE APPENDS THE SOLUTION FILES OF DIFF SCENARIOS AND ANALYSES THE CONSUMPTION


** 	CHANGE THE DIRECTORY
	cd $DIR

** 	OPEN SOLUTION.DTA
	use BSL_Solution, clear

**FAMILY SIZE (IN MALE ADULT (18-62) EQUIVALENT: 3.259 BJ/YEAR)
	g hhsize = e_demand
	g hhsize_ae = hhsize / 3.259
	label var hhsize "Household size [BJ]
	label var hhsize_ae "Household size [male adult equivalents]

** PERCAPITA ENERGY CONSUMPTION

	g pcap_en = e_supply/hhsize_ae
	keep run period aID pcap_en

	save Enddata_E0_ , replace

**********************************************************************************

	use B_IFL1_Solution, clear

**FAMILY SIZE (IN MALE ADULT (18-62) EQUIVALENT: 3.259 BJ/YEAR)
	g hhsize = e_demand
	g hhsize_ae = hhsize / 3.259
	label var hhsize "Household size [BJ]
	label var hhsize_ae "Household size [male adult equivalents]

** PERCAPITA ENERGY CONSUMPTION

	g pcap_en = e_supply/hhsize_ae
	keep run period aID pcap_en

	save Enddata_E1_c , replace
/*
*******************************************************************************

	use CRE_DRY1_Solution, clear

**FAMILY SIZE (IN MALE ADULT (18-62) EQUIVALENT: 3.259 BJ/YEAR)
	g hhsize = e_demand
	g hhsize_ae = hhsize / 3.259
	label var hhsize "Household size [BJ]
	label var hhsize_ae "Household size [male adult equivalents]

** PERCAPITA ENERGY CONSUMPTION

	g pcap_en = e_supply/hhsize_ae
	keep run period aID pcap_en

	save Enddata_E2_m , replace
****************************************************************************
*/

local E "E0_ E1_c"

	foreach x of local E {
		use EndData_`x', clear
		
		keep run period aID  pcap_en 
		
		reshape wide  pcap_en , i(period aID) j(run) string
		sort period aID
		save EndData_temp_`x', replace
	}


use EndData_temp_E0_, clear
	foreach x of local E {
		sort period aID
		merge period aID using EndData_temp_`x'
		tab _m 
		drop _m
	}
	
	save EndData_E, replace

	use EndData_E, clear


	
 	collapse (mean)  pcap_en* , by(aID) fast

/*
****************************************************************************************
*** CALCULATE THE GAPS, IN %
****************************************************************************************

	** CREDIT:
	egen with_c		= rsum(pcap_enCRE)
	egen without_c 	= rsum(pcap_enBSL)

	** DRY SEASON MIGRATION:
	egen with_m		= rsum(pcap_enDRY)
	egen without_m 	= rsum(pcap_enBSL)

*** Quantify the yield gap:
	g gap_c = with_c  - without_c
	g gap_m = with_m - without_m
	

	g gap_o = pcap_enBSL - gap_c - gap_m 
	egen gap = rsum(gap_c gap_m gap_o)

	g gap_pot = pcap_enCRE	
	g gap_act = pcap_enBSL
*/
***	DIVIDE INTO GROUPS:
	xtile en10  = pcap_enBSL , nq(10)

	table en10, c(mean  pcap_enBSL )
 
****************************************************************************************
*** CALCULATE THE GAPS, IN %
****************************************************************************************


	
	graph bar (mean) pcap_enBSL pcap_enB_IFL1 , over(en10, axis(off) gap(25)) ylabel(0(2)12, angle(horizontal) labsize(medlarge) glc(gs11)) ytitle("billion joules / capita", size(large)) bar(1, bc(forest_green*0.9) ) bar(2, bc(red*7)) bar(3, bc(sienna*0.7))   legend(label(1 "Baseline")  label(2 "Short-term" "credit") size(medium) col(1) position(3) symxsize(*0.5) region(lc(none) margin(medsmall)) order(2 1) rowgap(*5)) xsize(20) ysize(12) graphregion(margin(b+12) color(white)) plotregion(margin(none) lc(black) lw(*2)) text( -1.5 50 "% of agents",size(large)) text(-0.5 15 "20",size(large)) text(-0.5 95 "100", size(large)) text(-0.50 35 "40", size(large)) text(-0.50 55 "60", size(large)) text(-0.50 75 "80", size(large)) 
	
graph export "D:\MPMAS\Ghana_casestudy\TONO_10\Stata\results\Decom_percapita FoodConsumption_Drought_RF_CRE.tif",replace


