capture program drop valid
program valid
syntax varlist, PARTition(numlist integer >0) [SCOrename(string) IMPute NORound CALCmethod(string) DESCitems GRAPHs cfa CFAMethod(string) CFAStand CONVdiv TCONVdiv(real 0.4) CONVDIVBoxplots Alpha(real 0.7) Delta(real 0.9) h(real 0.3) HJmin(real 0.3) REPet(varlist) KAPpa ICKAPpa(integer 0) kgv(varlist) KGVBoxplots KGVGroupboxplots conc(varlist) tconc(real 0.4)]
preserve

qui set autotabgraphs on

qui set more off

local C = 0
foreach z in `partition' {
	local C = `C' + `z'
}

local nbvars : word count `varlist' 
/*
if `C' > `nbvars' {
	di in red "The sum of the numbers in the partition option is greater than the number of variables in the dataset"
	exit 119
}
*/
local nbvars : word count `varlist'
if `C' != `nbvars' {
	di in red "The sum of the numbers in the partition option is different from the number of variables precised in varlist"
	exit 119
}

local P:word count `partition'

if "`scorename'" !="" {
	local S:word count `scorename'
	if `P'!=`S' {
		di in red "The number of score names given is different from the number of dimensions in the partition option" 
		exit 119
	}
	foreach sco in `scorename' {
		capture confirm variable `sco'
		if !_rc {
			di in red "`sco' is a variable of the dataset. Choose another name"
			exit 119
        }
		tokenize `scorename'
		local g = 0
		forvalues i = 1/`S' {
			if "`sco'" == "``i''" {
				local `++g'
			}
		}
		if `g' > 1 {
			di in red "2 or more dimensions have the same name"
			exit 119
		}
    }
}
else {
	local name
	local nname
	
	forvalues i = 1/`P' {
		local name "Dim`i'"
		local nname `nname' `name'
	}
local scorename = "`nname'"
}

/*
if "`ident'" == "" {
	tempvar id
	qui gen `id' = _n 
}
else {
	qui tab `ident', nofreq
	local u = r(r)
	if `u' != _N {
		di in red "`ident' has not unique values"
		exit 119
	}
}
*/
/*
if "`repitems'" !="" {
	local a : word count `repitems'
	if mod(`a',2) != 0 {
		di in red "error repitems: odd number"
		exit
	}
	foreach r in `repitems' {
		capture confirm variable `r'
		if _rc!=0 {
			di in red "`r' does not exist" 
			exit 119
        }
    }
}
*/
/*
local y = 0
if "`repscores'" !="" {
	local a : word count `repscores'
	if mod(`a',2) != 0 {
		di in red "error repscores : odd number"
		exit 119
	}
	foreach r in `repscores' {
		local x = 0
		local y = 0
		capture confirm variable `r'
		if _rc!=0 local x = 1
        foreach s in `scorename' {
			if "`r'" == "`s'" local y = `y'+1
		}
		if `x' == 1 & `y' < 1 {
			di "`r' " "non d�fini"
		exit 119
		}
	}
}
*/

if "`kgv'" !="" {
	foreach k in `kgv' {
		capture confirm variable `k'
		if _rc!=0 {
			di in red "`k' does not exist" 
			exit 119
		}
    }
}

if "`conc'" !="" {
	foreach c in `conc' {
		capture confirm variable `c'
		if _rc!=0 {
			di in red "`c' does not exist" 
			exit 119
		}
    }
}
/*
if `t2from' > _N {
	di in red "Reproductibility : invalid range for observations at time 2 (`t2from' > number of observations)"
	exit 119
}

if `t2to' > _N {
	di in red "Reproductibility : invalid range for observations at time 2 (`t2to' > number of observations)"
	exit 119
}

if "`t2from'" != "" & "`t2to'" != "" {
	if `t2from' > `t2to' {
		di in red "Reproductibility : invalid range for observations at time 2"
		exit 119
	}
}

if "`t2from'" == "" local t2from = 1
if "`t2to'" == "" local t2to = _N
*/

local i = 1 
foreach x in `varlist' {
	local var`i' = "`x'"
	local `++i'
}

di as result "Items used for calculation of the scores"
di

local i = 1
local j = 1
local y = 1
foreach p in `partition' {
	tokenize `scorename'
	di "{bf:``i''} : " _c
	
	if `j' == 1 local s = `p'
	else local s = `s' +`p'
	forvalues z = `y'/`s' {
		di "{text:`var`z'' }" _c
	}
	local `i++' 
	local `j++' 
	local y = `s'+1
	di
}

qui destring _all, replace
di
/*
local cpt = 0
if "`sum'" != "" {
	local cpt `cpt' + 1
}
if "`mean'" != "" {
	local cpt `cpt' + 1
}
if "`stand'" != "" {
	local cpt `cpt' + 1
}
if `cpt'>1 {
	di in red "You must choose between mean, sum or stand (the options are exclusive)"
	exit 119
}
*/
calcscore `varlist', scorename(`scorename') partition(`partition') calcmethod(`calcmethod')

if "`descitems'" != "" {
	di as result "{hline}"
	di "Description of items"
	di as result "{hline}"
	di
	descitems `varlist', partition(`partition')
	di
}
if "`graphs'" != "" {
	_graph `varlist', partition(`partition') scorename(`scorename') calcmethod(`calcmethod')
}

if "`repet'" != "" {
	if "`descitems'" != "" {
		di as result "{hline}"
		di "Description of items (time 2)"
		di as result "{hline}"
		di
		descitems `repet', partition(`partition')
		di
	}
}

if "`impute'" != "" {
	*if "`noround'" != "" { 
		*di "Missing data handling (noround)"
		*di
		
		imp `varlist', partition(`partition') `noround'
		if "`descitems'" != "" {
			di as result "{hline}"
			di "Description of items after missing data handling"
			di as result "{hline}"
			di
			descitems `varlist', partition(`partition')
			di
		}
		if "`graphs'" != "" {
			*_graph `varlist', partition(`partition') scorename(`scorename')
		}
		if "`repet'" != "" {
			imp `repet', partition(`partition') `noround'
			if "`descitems'" != "" {
				di as result "{hline}"
				di "Description of items after missing data handling (time 2)"
				di as result "{hline}"
				di
				descitems `repet', partition(`partition')
				di
			}
		}
		
		
	*}
	/*else {
		di "Missing data handling (round)"
		di
		imp `varlist', partition(`partition')
		di "Description of items after missing data handling"
		di
		desc_item `varlist', partition(`partition')
		if "`graphs'" != "" {
			*_graph `varlist', partition(`partition') scorename(`scorename')
		}
		di "Description of items after missing data handling (time 2)"
		di
		if "`repet'" != "" {
			imp `repet', partition(`partition')
			desc_item `repet', partition(`partition')
		}
		di "{hline}"
	}*/
}  

/*
if "`imp'" == "" & "`noround'" != "" {
	*di "noround"
	di
	imp `varlist', partition(`partition') `noround'
	if "`desc_items'" != "" {
		di "Description of items after missing data handling"
		di
		desc_item `varlist', partition(`partition')
	
	}
	if "`graphs'" != "" {
		*_graph `varlist', partition(`partition') scorename(`scorename')
	}
	di "Description of items after missing data handling (time 2)"
	di
	imp `repet', partition(`partition') `noround'
	desc_item `repet', partition(`partition')
	di "{hline}"
	
}
*/
		
*di "{hline}"

if "`convdiv'" != "" {
	convdiv `varlist', scorename(`scorename') partition(`partition') tconvdiv(`tconvdiv') `convdivboxplots'
	*di as result "{hline}"
	di
}

/*
if "`cfa'" != "" {
	local cpt = 0
	if "`cfa_ml'" != ""{
		local method = "cfa_ml"
		local cpt `cpt' + 1
	}
	if "`cfa_mlmv'" != "" {
		local method = "cfa_mlmv"
		local cpt `cpt' + 1
	}
	if "`cfa_adf'" != ""  {
		local method = "cfa_adf"
		local cpt `cpt' + 1
	}
	else local method = "cfa_ml"
	if `cpt' > 1 {
		di in red "You must choose between cfa_ml, cfa_mlmv or cfa_adf (the options are exclusive)"
		exit 119
	}

	cfa `varlist', scorename(`scorename') partition(`partition') `method' `cfa_stand'
	di as result "{hline}"	
}
*/

if "`cfa'" != "" {
	cfa `varlist', scorename(`scorename') partition(`partition') cfamethod(`cfamethod') `cfastand'
	*di as result "{hline}"
	di
}

rel `varlist', scorename(`scorename') partition(`partition') alpha(`alpha') delta(`delta') h(`h') hjmin(`hjmin')
di
*di "{hline}"

if "`repet'" != "" {
	repet `varlist', t2(`repet') partition(`partition') scorename(`scorename') `kappa' ickappa(`ickappa') calcmethod(`calcmethod')
	*di "{hline}"
	di
}

if "`kgv'" != "" {
	kgv `scorename', categ(`kgv') `kgvboxplots' `kgvgroupboxplots'
	*di "{hline}"
	di
}

if "`conc'" != "" {
	conc `scorename', comp(`conc') tconc(`tconc')
	*di "{hline}"
}
end