package MpmasStataImport;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use MpmasInputfile;
use MpmasInputBlock;
use Cwd;
our @ISA = qw(MpmasInputfile);

sub filename {
	"stata_import";
}

sub write {
  my ($self, $inihash, $scenlist, $num_periods, $ConstraintGroupingsForAllScenarios, $ActivityGroupingsForAllScenarios) = @_;

  #Hard coded defaults

  $inihash->{STATA_ROOT} = cwd() unless (exists $inihash->{STATA_ROOT});
  $inihash->{STATA_MEM} = "1000M" unless (exists $inihash->{STATA_MEM});
  $inihash->{STATA_MAXVAR} = "5000" unless (exists $inihash->{STATA_MAXVAR});
  unless (exists $inihash->{STATA_MPMASOUT}) {
	$inihash->{STATA_MPMASOUT} = "out/" ;
	$inihash->{STATA_MPMASOUT} = $inihash->{RUNOUTDIR}."/out/"   if (exists $inihash->{RUNOUTDIR});
  }
  unless (exists $inihash->{STATA_MPMASLAB}) {
	$inihash->{STATA_MPMASLAB} = "input/dat/" ;
	$inihash->{STATA_MPMASLAB} = $inihash->{RUNINDIR}."/input/dat/"   if (exists $inihash->{RUNINDIR});
  }
  #Open file
  open(my $stata_import_out, ">",  $inihash->{RUNSCRIPTDIR}.$inihash->{PREFIX}."import_to_stata.do") or print STDERR "$!" and exit(1);
  
  #Print script
  print $stata_import_out 
<<STATAIMPORTSCRIPT;
**********************************************************************************
* Script to import Mpmas output into stata
* automatically generated by mpmasql
*
* (c)  Pepijn Schreinemachers, Christian Troost, Universität Hohenheim, 2006-2012
**********************************************************************************
set more off
clear
set mem $inihash->{STATA_MEM}
set maxvar $inihash->{STATA_MAXVAR}
set more off

**********************************************************************************
* Part I: Settings
* These settings might be adapted by the user

** 1. SET THE MAIN DIRECTORY (THE VERSION FOLDER, ABSOLUTE PATH!!!)
	global Dir "$inihash->{STATA_ROOT}"

** 2. DEFINE THE PREFIX OF THE OUTPUT FILES (SIMULATION RUN):
      global SUPER "$inihash->{PREFIX}"
      global RUN "@$scenlist"

** 3. DEFINE THE NUMBER OF PERIODS IN THE SIMULATION:
	global PERIODS "$num_periods"
	local START = 1

** 5. DEFINE ADDITIONAL PREFIX FOR COMBINED FILE
	local prescen = ""
*
********************************************************************************
* Part II: Internal settings
* The following parameters usually do not have to be changed

** 0. DEFINE THE OUTPUT FILE TYPES TO IMPORT:
	global TYPE "u p k xu xp xk"

** A. DIRECTORY WHERE THE OUTPUT FILES ARE WRITTEN:
	global T1 "/$inihash->{STATA_MPMASOUT}"
	global OUT \$Dir\$T1
	cd \$OUT 	/* To check whether the directory exists */

** B. DIRECTORY WHERE THE FILES WITH LABELS ARE LOCATED:
	global T2 "/$inihash->{STATA_MPMASLAB}"
	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 T3 "/Stata/temp/"
	global TEMP \$Dir\$T3
	
	global T4 "/Stata/"
	capture mkdir \$Dir\$T4
	capture mkdir "\$TEMP"
	cd \$TEMP 	/* To check whether the directory exists */
	

** SET THE DIRECTORY WHERE THE COMBINED (STATA) FILES ARE TO BE WRITTEN:
	global T5 "/Stata/combined/"
	global FULL \$Dir\$T5
	capture mkdir "\$FULL"
	cd \$FULL 	/* To check whether the directory exists */
	
*
*********************************************************************************
* Part III: The actual script
cd \$TEMP
** IMPORT THE ASCII FILES AND SAVE THEM IN STATA-FORMAT:
foreach nn of global SUPER {
	foreach n0 of global RUN {		
		local n = "`nn'`n0'"
		foreach t of global TYPE {
			global PER = "0"
			local p = `START'
			while `p' <= \$PERIODS {
				if `p' <  10 & `p' >=0  global PER = "0`p'"
				if `p' >= 10 global PER = "`p'"
				if `p' < 0 global PER = "`p'"
				capture quietly insheet using \$OUT`n'_`t'\$PER.out, tab clear nonames
				
				quietly g 		period	= \$PER
				quietly g 		run		= "`n'"
				quietly save "\$TEMP/temp_`n'_`t'\$PER", replace
				noisily display in green "`n'_`t'\$PER.out" " was imported"
				local p = `p' + 1
			}
		}

		global XU "0"
		global XP "0"
		global XK "0"

** MERGE FILES FOR ALL PERIODS TOGETHER:

		foreach t of global TYPE {
			global PER = "0"
			local i = 1
			local p = `START'

			while `p' <= \$PERIODS {
				if `p' <  10 & `p' >=0  global PER = "0`p'"
				if `p' >= 10 global PER = "`p'"
				if `p' < 0 global PER = "`p'"
				** OPEN THE FIRST FILE:
				if `i' == 1 	   use "\$TEMP/temp_`n'_`t'\$PER", clear
				** APPEND ALL OTHER FILES:
				if `i' >  1 append using "\$TEMP/temp_`n'_`t'\$PER"
				local p = `p' + 1
				local i = `i' + 1
				**
				erase "\$TEMP/temp_`n'_`t'\$PER.dta"
			}
			order run period
			capture run \$LAB`n'_`t'_labels.do
			quietly compress

			if "`t'" == "u" {
				save temp_`t', replace
				capture keep if v1 == 2 
				quietly save "\$TEMP`n'_solution", replace
				use temp_`t', clear
				capture keep if v1 == 3 
				quietly save "\$TEMP`n'_prices", replace
				erase "temp_`t'.dta"
			}
			else if "`t'" == "xu" {
                                quietly count
                                if r(N) > 0 {
                                        global XU "1"
                        }

				save temp_`t', replace
				capture keep if v1 == 2 
				quietly save "\$TEMP`n'_Xsolution", replace
				use temp_`t', clear
				capture keep if v1 == 3 
				quietly save "\$TEMP`n'_Xprices", replace
				capture erase "temp_`t'.dta"
			}
			else if "`t'" == "k" {
				save temp_`t', replace
				capture keep if v1 == 4 
				quietly save "\$TEMP`n'_rhs", replace
				use temp_`t', clear
				capture	keep if v1 == 5 
				quietly save "\$TEMP`n'_lhs", replace
				erase "temp_`t'.dta"
								
			}

			else if "`t'" == "xk" {
				quietly count
                                if r(N) > 0 {
                                        global XK "1"
                                }

				save temp_`t', replace
				capture keep if v1 == 4 
				quietly save "\$TEMP`n'_Xrhs", replace
				use temp_`t', clear
				capture keep if v1 == 5 
				quietly save "\$TEMP`n'_Xlhs", replace
				capture erase "temp_`t'.dta"
								
			}
			else if "`t'" == "xp" {
				quietly count			
				if r(N) > 0 {
					global XP "1"
				}
                              	quietly save "\$TEMP`n'_Xp.dta", replace
                        }
			else {
				save temp_`t', replace
				quietly save "\$TEMP`n'_`t'", replace
			}		
		}	


** MERGE SOLUTION, RHS, LHS, PERFORMANCE INTO A COMBINED FILE

		if \$XP == 1 {
			use `n'_Xp.dta, clear
			gen exit = 1
			save tempXp.dta, replace
		}
		use `n'_p.dta, clear
		gen exit = 0
		if \$XP == 1 {
			append using tempXp.dta
		}
		renpfix v p
		gen entry = string(p5, "%09.0f") + string(period,"%04.0f")
		save x`n'_p.dta, replace
		capture erase "tempXp.dta"
		if \$XK == 1 {
			use `n'_Xrhs.dta, clear
			gen exit = 1
			save tempXrhs.dta, replace
		}
		use `n'_rhs.dta, clear
		gen exit = 0
		if \$XK == 1 {
			append using tempXrhs.dta
		}
		renpfix v r
		gen entry = string(r5, "%09.0f") + string(period,"%04.0f")
		save x`n'_rhs.dta, replace
		capture erase "tempXrhs.dta"
		if \$XK == 1 {		
			use `n'_Xlhs.dta, clear
			gen exit = 1
			save tempXlhs.dta, replace
		}
		use `n'_lhs.dta, clear
		gen exit = 0
		if \$XK == 1 {
			append using tempXlhs.dta
		}
		renpfix v l
		gen entry = string(l5, "%09.0f") + string(period,"%04.0f")
		save x`n'_lhs.dta, replace
		capture erase "tempXlhs.dta"
		
		if \$XU == 1 {	
			use `n'_Xsolution.dta, clear
			gen exit = 1
			save tempXsolution.dta, replace
		}
		use `n'_solution.dta, clear
		gen exit = 0
		if \$XU == 1 {
			append using tempXsolution.dta
		}
		renpfix v u
		gen entry = string(u5, "%09.0f") + string(period,"%04.0f")
		save x`n'_solution.dta, replace
		
		joinby entry using x`n'_rhs.dta
		joinby entry using x`n'_lhs.dta
		joinby entry using x`n'_p.dta
		order entry
		save "\$FULL`prescen'`n'_combined.dta", replace
		capture erase "tempXsolution.dta"

STATAIMPORTSCRIPT
  my %unique_ag = map {$_ => 1 } @$ActivityGroupingsForAllScenarios;
  my %unique_cg = map {$_ => 1 } @$ConstraintGroupingsForAllScenarios;
  for my $i ( sort { $a cmp $b } keys  %unique_ag ) {
    print $stata_import_out "\t\tcapture run \$LAB/`n'_create_".$i."_uvariables.do\n"
  }
  for my $i ( sort { $a cmp $b } keys %unique_cg ) {
    print $stata_import_out "\t\tcapture run \$LAB/`n'_create_".$i."_kvariables.do\n"
  }

  print $stata_import_out 
<<STATAIMPORTSCRIPT2;

		save "\$FULL`prescen'`n'_combined.dta", replace
	}
}


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

STATAIMPORTSCRIPT2

  #Close file
  close($stata_import_out);
}
