clear
set mem 500m

set more off

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

** MANUALLY SET THESE PARAMETERS:


** 1. SET THE MAIN DIRECTORY (THE VERSION FOLDER)
	global Dir "D:\MPMAS\TONO110\"

** 2. DEFINE THE PREFIX OF THE OUTPUT FILES (SIMULATION RUN):
	global RUN "BSL CRE"
	
** 3. DEFINE THE NUMBER OF PERIODS IN THE SIMULATION:
	global PERIODS "17"

** 4. DEFINE INITIAL YEAR IN THE SIMULATION
		global YEAR "2005"

** 5. DEFINE THE OUTPUT FILE TYPES TO IMPORT:
	global TYPE "k u a"
	/* AVAILABLE TYPES:
	a = agent exit data
	d = innovation diffusion data
	k = available and used resources
		v1=4 for available capacity (resources)
		v1=5 for actually used capacity
	p = agent performance data
	u = solution vector 
		v1=2 for primal solution 
		v1=3 for objective values
	y = crop yield data
	w = sector water data
	l = land data
	*/

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

** THE FOLLOWING PARAMETERS ARE SET AUTOMATICALLY

** A. DIRECTORY WHERE THE OUTPUT FILES ARE WRITTEN:
	global T1 "\out\"
	global OUT $Dir$T1
	cd $OUT 	/* To check whether the directory exists */

** B. DIRECTORY WHERE THE FILES WITH LABELS ARE LOCATED:
	global T2 "\Stata\labels\"
	global LAB $Dir$T2
	cd $LAB 	/* To check whether the directory exists */

** SET THE DIRECTORY WHERE THE TEMPORARY (STATA) FILES ARE TO BE WRITTEN:
	global TEMP "$dir\MPMAS\TONO110\Stata\temp"
	global T3 "\Stata\temp\"
	global TEMP $Dir$T3
	cd $TEMP 	/* To check whether the directory exists */

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


** IMPORT THE ASCII FILES AND SAVE THEM IN STATA-FORMAT:

	foreach n of global RUN {
		foreach t of global TYPE {
			global PER = "0"
			local p = 1
			while `p' <= $PERIODS {
				if `p' <  10 global PER = "0`p'"
				if `p' >= 10 global PER = "`p'"
				quietly insheet using $OUT\`n'_`t'$PER.out, tab clear nonames
				quietly g 		period	= $PER
				quietly g str10	run		= "`n'"
				quietly save "$TEMP\temp_`n'_`t'$PER", replace
				noisily display in green "`n'_`t'$PER.out" " was imported"
				local p = `p' + 1
				}
			}
		}


** MERGE ALL FILES TOGETHER:

	foreach n of global RUN {
		foreach t of global TYPE {
			global PER = "0"
			local p = 1
			while `p' <= $PERIODS {
				if `p' <  10 global PER = "0`p'"
				if `p' >= 10 global PER = "`p'"
				** OPEN THE FIRST FILE:
				if `p' == 1 	   use "$TEMP\temp_`n'_`t'$PER", clear
				** APPEND ALL OTHER FILES:
				if `p' >  1 append using "$TEMP\temp_`n'_`t'$PER"
				local p = `p' + 1
				**
				erase "$TEMP\temp_`n'_`t'$PER.dta"
				}


			order run period

			capture run "$LAB\label_`t'.do"

			quietly compress
			if "`t'" == "u" {
				quietly save "temp_`t'.dta", replace
				quietly keep if typ_str == 2
				sort run period popID aID 
				quietly save "$TEMP\`n'_Solution.dta", replace
				quietly use "temp_`t'.dta", clear
				quietly keep if typ_str == 3
				sort run period popID aID 
				quietly save "$TEMP\`n'_Prices.dta", replace
				erase "temp_`t'.dta"
				}
			else if "`t'" == "k" {
				quietly save "temp_`t'.dta", replace
				quietly keep if typ_str  == 4
				sort run period popID aID 
				quietly save "$TEMP\`n'_Rhs.dta", replace
				quietly use "temp_`t'.dta", clear
				quietly keep if typ_str  == 5
				sort run period popID aID 
				quietly save "$TEMP\`n'_Lhs.dta", replace
				erase "temp_`t'.dta"
				}
			

				else if "`t'" == "p" {
				capture run "$LAB\label_p.do"
				quietly save "temp_`t'.dta", replace
				quietly use "temp_`t'.dta", clear
				sort run period popID aID  
				quietly save "$TEMP\`n'_Performance.dta", replace
				erase "temp_`t'.dta"
				}

		else {
				quietly save "$TEMP\`n'_`t'.dta", replace
				}			
		}
			}	

** DROP THE CREATED GLOBAL MACROS:
	
	macro drop RUN PERIODS PER TYPE LAB


