iefolders : more work task master dofile

	cap program drop 	parseReadLine
	program define		parseReadLine, rclass

		args line

		*Tokenize each line
		tokenize  `"`line'"' , parse("*")

		**Parse the beginning of the line to
		* test if this line is an iefolder line
		local divisorStar  		"`1'"
		local divisorIefoldeer  "`2'"

		if `"`divisorStar'`divisorIefoldeer'"' != "*iefolder" {

			*This is not a iefolder divisor
			return scalar ief_line 	= 0
			return scalar ief_end 	= 0
		}
		else {

			*This is a iefolder divisor,
			return scalar ief_line 	= 1

			*parse the rest of the line (from tokenize above)
			local partNum  		"`4'"
			local partName 		"`6'"
			local sectionNum 	"`8'"
			local sectionName 	"`10'"
			local itemName 		"`12'"
			local itemAbb	 	"`14'"

			*Return the part name and number. All iefolder divisor lines has this
			return local partNum  "`partNum'"
			return local partName "`partName'"

			*Get the prefix of the section name
			local sectPrefix = substr("`partName'",1,4)

			*Test if it is an end of section divisor
			if "`sectPrefix'" == "End_" {

				return scalar ief_end 	= 1
			}
			else {

				return scalar ief_end 	= 0

				*These are returned empty if they do not apply
				return local sectionNum 	"`sectionNum'"
				return local sectionName	"`sectionName'"
				return local sectionAbb 	"`sectionAbb'"
				return local itemName		"`itemName'"
				return local itemAbb 		"`itemAbb'"
			}
		}
		*di "parseReadLine end"
	end

	cap program drop 	newRndFolderAndGlobals
	program define		newRndFolderAndGlobals

		args rndName rnd dtfld_glb subHandle

		*KBKB: test if new fodler can be written - use "cap cd"

		*Write title to the folder globals to this round
		file write  `subHandle'	_col(4)"*`rndName' folder globals" _n

		*Round main folder
		createFolderWriteGlobal "`rndName'" 	 		"`dtfld_glb'"  	"`rnd'" 		`subHandle'

		*Sub folders
		createFolderWriteGlobal "DataSets" 				"`rnd'" 		"`rnd'_dt" 		`subHandle'
		createFolderWriteGlobal "Dofiles" 				"`rnd'"			"`rnd'_do" 		`subHandle'
		createFolderWriteGlobal "Output" 				"`rnd'"			"`rnd'_out"		`subHandle'
		createFolderWriteGlobal "Documentation" 		"`rnd'"	 		"`rnd'_doc"
		createFolderWriteGlobal "Questionnaire" 		"`rnd'"	 		"`rnd'_quest"

		*Create round master dofile
		createRoundMasterDofile "$projectfolder/`rndName'" "`rndName'" "`rnd'"

	end

	cap program drop 	createRoundMasterDofile
		program define	createRoundMasterDofile

		args roundfolder rndName rnd

		*Create a temporary textfile
		tempname 	roundHandle
		tempfile	roundTextFile
		file open  `roundHandle' using "`roundTextFile'", text write replace

		*Write intro part with description of round,
		mdofle_p0 		`roundHandle' round
		mdofle_p1_round `roundHandle' `rndName'

		*DataSets sub-folder
		file write  `roundHandle'		_col(4)"*DataSets sub-folder globals" _n
		createFolderWriteGlobal "Raw"  							"`rnd'_dt" 		"`rnd'_dtRaw" 	`roundHandle'
		createFolderWriteGlobal "Intermediate" 					"`rnd'_dt" 		"`rnd'_dtInt" 	`roundHandle'
		createFolderWriteGlobal "Final"  						"`rnd'_dt" 		"`rnd'_dtFin" 	`roundHandle'

		*Dofile sub-folder
		file write  `roundHandle' _n	_col(4)"*Dofile sub-folder globals" _n
		createFolderWriteGlobal "Dofiles Import"				"`rnd'_do" 		"`rnd'_doImp" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Cleaning"				"`rnd'_do" 		"`rnd'_doCln" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Construct"				"`rnd'_do" 		"`rnd'_doCon" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Analysis"				"`rnd'_do" 		"`rnd'_doAnl" 	`roundHandle'

		*Output subfolders
		file write  `roundHandle' _n	_col(4)"*Output sub-folder globals" _n
		createFolderWriteGlobal "Raw" 							"`rnd'_out"	 	"`rnd'_outRaw"	`roundHandle'
		createFolderWriteGlobal "Final" 						"`rnd'_out"	 	"`rnd'_outFin"	`roundHandle'

		*Questionnaire subfolders
		file write  `roundHandle' _n	_col(4)"*Questionnaire sub-folder globals" _n
		createFolderWriteGlobal "Questionnaire Develop" 		"`rnd'_quest"	 "`rnd'_qstDev"	`roundHandle'
		createFolderWriteGlobal "Questionnaire Final" 			"`rnd'_quest"	 "`rnd'_qstFin"	`roundHandle'
		createFolderWriteGlobal "PreloadData"	 				"`rnd'_quest"	 "`rnd'_prld"	`roundHandle'
		createFolderWriteGlobal "Questionnaire Documentation"	"`rnd'_quest"	 "`rnd'_doc"	`roundHandle'

		*Write sub devisor starting master and monitor data section section
		writeDevisor 	`roundHandle' 1 End_FolderGlobals

		mdofle_p3 `roundHandle' round `rndName' `rnd'

		*Closing the new main master dofile handle
		file close 		`roundHandle'

		*Copy the new master dofile from the tempfile to the original position
		copy "`roundTextFile'"  "`roundfolder'/`rndName'_MasterDofile.do" , replace

	end

	cap program drop 	createFolderWriteGlobal
		program define	createFolderWriteGlobal

		args  folderName parentGlobal globalName subHandle

			*di "createFolderWriteGlobal start `folderName'"

			if ("`subHandle'" != "") {

				file write  `subHandle' ///
					_col(4) `"global `globalName'"' _col(34) ///
					`"""' _char(36)`"`parentGlobal'/`folderName'" "' _n

			}

			global `globalName' "$`parentGlobal'/`folderName'"

			*noi di `"mkdir "${`parentGlobal'}\\`folderName'""'

			mkdir "${`parentGlobal'}\\`folderName'"

			*di "createFolderWriteGlobal end `folderName'"
	end

	cap program drop 	writeDevisor
	program define		writeDevisor , rclass

		args  subHandle partNum partName sectionNum sectionName itemName itemAbb

		*di "devisor start"

		local devisor "*iefolder*`partNum'*`partName'*`sectionNum'*`sectionName'*`itemName'*`itemAbb'*"

		local devisorLen = strlen("`devisor'")

		*Make all devisors at least 80 characters wide
		if (`devisorLen' < 80) {

			local numStars = 80 - `devisorLen'
			local addedStars _dup(`numStars') _char(42)
		}

		file write  `subHandle' _n "`devisor'" `addedStars'
		file write  `subHandle' _n "*iefolder wont work properly if the line above is edited"  _n _n

		*di "devisor end"

	end

	/*

		The functions below are all related to write the mostly
		static part of the master do file

	*/

	cap program drop 	mdofle_p0
	program define		mdofle_p0

		args subHandle itemType rndName task

		*di "start masterDofilePart0"

		file write  `subHandle' ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"*" _col(20) "your_`itemType'_name" _col(75) "*" _n ///
			_col(4)"*" _col(20) "MASTER DO_FILE" _col(75) "*" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_n _col(8)"/*" _n

		if "`itemType'" == "project" {

			file write  `subHandle'	///
				_col(8)"** PURPOSE:" _col(25) "Write intro to project here" _n ///
				_n ///
				_col(8)"** OUTLINE:" _col(25) "PART 0: Standardize settings and install paackages" _n ///
				_col(25) "PART 1: Set globals for dynamic file paths" _n ///
				_col(25) "PART 2: Set globals for constants and varlist" _n ///
				_col(32) "used across the project. Intall custom" _n ///
				_col(32) "commands needed." _n ///
				_col(25) "PART 3: Call the task specific master do-files " _n ///
				_col(32) "that call all do-files needed for that " _n ///
				_col(32) "tas. Do not include Part 0-2 in a task" _n ///
				_col(32) "specific master do-file" _n ///
				_n _n

			/*
			file write  `subHandle'	///
				_col(8)"** REQUIRES:" _col(25) "List all data sets using the globals that you " _n ///
				_col(25) "define below to indicate what data you will need" _n ///
				_n ///
				_col(25) "Example:" _n ///
				_col(25) _char(36)"BL_dtRaw/baseline_survey_v1.csv" _n ///
				_col(25) _char(36)"BL_dtRaw/baseline_survey_v2.csv" _n ///
				_col(25) _char(36)"moniData/monitor2016_data.xlsx" _n ///
				_n

			file write  `subHandle'	///
				_col(8)"** CREATES:" _col(25) "List all data sets using the globals that you " _n ///
				_col(25) "define below to indicate what data set are " _n ///
				_col(25) "created by the do-files this master do-file calls" _n ///
				_n ///
				_col(25) "Example:" _n ///
				_col(25) _char(36)"BL_dtFinal/baseline_clean.dta" _n ///
				_col(25) _char(36)"BL_dtFinal/baseline_constructs.dta" _n ///
				_col(25) "Multiple tables and grahps in \$BL_Out" _n ///
				_n
			*/
		}
		else if "`itemType'" == "round" {

			file write  `subHandle'	///
				_col(8)"** PURPOSE:" _col(25) "Write intro to round here" _n ///
				_n ///
				_col(8)"** OUTLINE:" _col(25) "PART 0: Standardize settings and install paackages" _n ///
				_col(25) "PART 1: Preparing folder path globals" _n ///
				_col(25) "PART 2: Run the master do files for each high level task" _n _n
		}

		file write  `subHandle'	///
			_col(8)"** IDS VAR:" _col(25) "list_ID_var_here		//Uniquely identifies households (update for your proejct)" _n ///
			_n	///
			_col(8)"** NOTES:" _n ///
			_n ///
			_col(8)"** WRITEN BY:" _col(25) "names_of_contributors" _n ///
			_n ///
			_col(8)"** Last date modified: `c(current_date)'" _n ///
			_col(8)"*/" _n

		*Write devisor starting setting standardize section
		writeDevisor  `subHandle' 0 StandardSettings

		file write  `subHandle'	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 0:  INSTALL PACKAGES AND STANDARDIZE SETTINGS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Install packages needed to run all dofiles called" _n ///
			_col(4)"*" _col(17) "by this master dofile." _n ///
			_col(4)"*" _col(16) "-Use ieboilstart to harmonize settings across users" _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		*Write devisor ending setting standardize section
		writeDevisor `subHandle' 0 End_StandardSettings

		file write  `subHandle' 	///
			_col(8)"*Install all packages that this project requires:" _n ///
			_col(8)"ssc install ietoolkit" _n ///
			_n	 ///
			_col(8)"*Standardize settings accross users" _n ///
			_col(8)"ieboilstart, version(12.1)" _col(40) "//Set the version number to the oldes version used by anyone in the project team" _n ///
			_col(8) _char(96)"r(version)'" 		_col(40) "//This line is needed to actually set the version from the command above" _n

		*di "masterDofilePart0a end"

	end

cap program drop 	mdofle_p1
	program define	mdofle_p1

		args   subHandle projectDir

		*Write devisor starting globals section
		writeDevisor  `subHandle' 1 FolderGlobals

		*di "masterDofilePart1 start"

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 1:  PREPARING FOLDER PATH GLOBALS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set the global box to point to the project folder" _n ///
			_col(4)"*" _col(17) "on each collaborators computer." _n ///
			_col(4)"*" _col(16) "-Set other locals that point to other folders of interest." _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		file write  `subHandle' ///
			_n ///
			_col(4)"* Users" _n ///
			_col(4)"* -----------" _n ///
			_n ///
			_col(4)"*User Number:" _n ///
			_col(4)"* You" _col(30) "1" _col(35) `"//Replace "You" with your name"' _n ///
			_col(4)"* Next User" _col(30) "2" _col(35) "//Assign a user number to each additional collaborator of this code" _n ///
			_n ///
			_col(4)"*Set this value to the user currently using this file" _n ///
			_col(4)"global user  1" _n ///
			_n ///
			_col(4)"* Root folder globals" _n ///
			_col(4)"* ---------------------" _n ///
			_n ///
			_col(4)"if "_char(36)"user == 1 {" _n ///
			_col(8)`"global projectfolder "`projectDir'""' _n ///
			_col(4)"}" _n ///
			_n ///
			_col(4)"if "_char(36)"user == 2 {" _n ///
			_col(8)`"global projectfolder ""  //Enter the file path to the projectfolder of next user here"' _n ///
			_col(4)"}" _n ///
			_n ///
			_n ///
			_col(4)"* Project folder globals" _n ///
			_col(4)"* ---------------------" _n

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 FolderGlobals 1 master

		*Create master data folder and add global to folder in master do file
		createFolderWriteGlobal "MasterData"  "projectfolder"  mastData  `subHandle'

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 FolderGlobals 3 monitor

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 End_FolderGlobals

		*di "masterDofilePart1a end"

	end

cap program drop 	mdofle_p1_round
	program define	mdofle_p1_round

		args   subHandle rndName

		local rndName_caps = upper("`rndName'")

		*Write devisor starting globals section
		writeDevisor  `subHandle' 1 FolderGlobals

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 1:  PREPARING `rndName_caps' FOLDER PATH GLOBALS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set global to point to the `rndName' folders" _n ///
			_col(4)"*" _col(16) "-Add to these globals as you need" _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n _n

	end

cap program drop 	mdofle_p2
	program define	mdofle_p2

		args   subHandle

		di "masterDofilePart2 start"

		*Write devisor starting standardization globals section
		writeDevisor  `subHandle' 2 StandardGlobals

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 2: - SET STANDARDIZATION GLOBALS AND OTHER CONSTANTS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set globals with numbers or lists of " _n ///
			_col(4)"*" _col(17) "variables that is supposed to stay the" _n ///
			_col(4)"*" _col(17) "same across the project." _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		file write  `subHandle' ///
			_n ///
			_col(4)"* Set all conversion rates used in unit standardization " _n ///
			_col(4)"* accross the whole project here. " _n ///
			_n ///
			_col(4)"**Example. Expand this section with globals for all constant" _n ///
			_col(4)"* scalars used in this project. Reference these globals instead" _n ///
			_col(4)"* of hardcode values each time constant converstion rates are used." _n ///
			_col(4)"*Standardizing to meters" _n ///
			_n ///
			_col(4)"global foot" _col(20) "= 0.3048" _n ///
			_col(4)"global mile" _col(20) "= 1609.34" _n ///
			_col(4)"global km" 	 _col(20) "= 1000" _n ///
			_n ///
			_col(4)"**Other examples to be included here could be regression controls" _n ///
			_col(4)"* used across the project. Everything that is constant may be" _n ///
			_col(4)"* included here. One example of something not constant that should" _n ///
			_col(4)"* be included here is exchange rates. It is best practice to have one" _n ///
			_col(4)"* global with the exchange rate here, and reference this each time a" _n ///
			_col(4)"* currency conversion is done. If the currency exchange reate needs to be" _n ///
			_col(4)"* updated, then it only has to be done at one place for the whole project." _n ///
			_n

		*Write devisor ending standardization globals section
		writeDevisor  `subHandle' 2 End_StandardGlobals

		di "masterDofilePart2 end"

	end

cap program drop 	mdofle_p3
	program define	mdofle_p3

		args   subHandle itemType rndName rnd

		di "masterDofilePart3 start"

		if "`itemType'" == "project" 	local partNum = 3
		if "`itemType'" == "round"		local partNum = 2

		*Write devisor starting the section running sub-master dofiles
		writeDevisor  `subHandle' `partNum' RunDofiles

			file write  `subHandle' 	///
				_col(4)"* ******************************************************************** *" _n ///
				_col(4)"*" _n ///
				_col(4)"*" _col(12) "PART `partNum': - RUN DOFILES CALLED BY THIS MASTER DO FILE" _n ///
				_col(4)"*" _n

			if "`itemType'" == "project" {
				file write  `subHandle' 	///
					_col(4)"*" _col(16) "-When survey rounds are added, this section will" _n ///
					_col(4)"*" _col(17) "link to the master dofile for that round." _n ///
					_col(4)"*" _col(16) "-The default is that these dofiles are set to not"  _n ///
					_col(4)"*" _col(17) "run. It is rare that all round specfic master dofiles"  _n ///
					_col(4)"*" _col(17) "are called at the same time, the round specifc master"  _n ///
					_col(4)"*" _col(17) "dofiles are almost always called individually. The"  _n ///
					_col(4)"*" _col(17) "excpetion is when reviewing or replicating a full project." _n
			}
			else if "`itemType'" == "round" {
				file write  `subHandle' 	///
					_col(4)"*" _col(16) "-A task master dofile has been created for each high"  _n ///
					_col(4)"*" _col(17) "level task (import, cleaning, construct, analyze). By "  _n ///
					_col(4)"*" _col(17) "running all of them all data work associated with the "  _n ///
					_col(4)"*" _col(17) "`rndName' should be replciaetd, including output of "  _n ///
					_col(4)"*" _col(17) "tablets, graphs, etc." _n ///
					_col(4)"*" _col(16) "-Feel free to add to this list if you have other high"  _n ///
					_col(4)"*" _col(17) "level tasks relevant to your project." _n
			}

			file write  `subHandle' 	///
				_col(4)"*" _n ///
				_col(4)"* ******************************************************************** *" _n

		if "`itemType'" == "round" {

			file write  `subHandle' 	_n ///
				_col(4)"**Set the locals corresponding to the taks you want" _n ///
				_col(4)"* run to 1. To not run a task, set the local to 0." _n ///
				_col(4)"local importDo" _col(25) "1" _n ///
				_col(4)"local cleaningDo" _col(25) "1" _n ///
				_col(4)"local constructDo" _col(25) "1" _n ///
				_col(4)"local analysisDo" _col(25) "1" _n ///

			*Create the references to the high level task
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "import"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "cleaning"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "construct"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "analysis"

		}

		*Write devisor ending the section running sub-master dofiles
		writeDevisor  `subHandle' `partNum' End_RunDofiles

		di "masterDofilePart3 end"

	end

	cap program drop 	highLevelTask
		program define	highLevelTask

			args roundHandle rndName rnd task

			di "highLevelTask start"

			*Write section where task master files are called
			file write  `roundHandle' 	_n ///
				_col(4)"if (" _char(96) "`task'' == 1) { //Change the local above to run or not to run this file" _n ///
				_col(8) `"do ""' _char(36) `"`rnd'_do/`task'_MasterDofile.do" "' _n ///
				_col(4)"}" _n

			*Create the task dofiles
			highLevelTaskMasterDofile `rndName' `task' `rnd'

	end

	cap program drop 	highLevelTaskMasterDofile
		program define	highLevelTaskMasterDofile

			di "highLevelTaskMasterDofile start"

			args rndName task rnd

			*Write the round dofile

			*Create a temporary textfile
			tempname 	taskHandle
			tempfile	taskTextFile
			file open  `taskHandle' using "`taskTextFile'", text write replace

			mdofle_task `taskHandle' task `rndName' `task'

			*Closing the new main master dofile handle
			file close 		`taskHandle'

			*Copy the new master dofile from the tempfile to the original position

			di ":`rnd'_do: `task'"
			di `"copy "`taskTextFile'"  "${`rnd'_do}/`rndName'_MasterDofile.do" , replace"'
			copy "`taskTextFile'"  "${`rnd'_do}/`rnd'_`task'_Master.do" , replace

	end

	cap program drop 	mdofle_task
	program define	mdofle_task

		args subHandle itemType rndName task

		local caps = upper("`rndName' `task'")

		local suffix
		if "`task'" == "import" {
			local suffix "_doImp"
		}
		if "`task'" == "cleaning" {
			local suffix "_doCln"
		}
		if "`task'" == "construct" {
			local suffix "_doCon"
		}
		if "`task'" == "analysis" {
			local suffix "_doAnl"
		}

		*di "start masterDofilePart0"

		file write  `subHandle' ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"*" _col(20) "`caps' MASTER DO_FILE" _col(75) "*" _n ///
			_col(4)"*" _col(20) "This master dofile calls all dofiles related" _col(75) "*" _n ///
			_col(4)"*" _col(20) "to `task' in the `rndName' round." _col(75) "*" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_n _col(8)"/*" _n 	///
			_col(4)"** IDS VAR:" _col(25) "list_ID_var_here		//Uniquely identifies households (update for your proejct)" _n ///
			_col(4)"** NOTES:" _n ///
			_col(4)"** WRITEN BY:" _col(25) "names_of_contributors" _n ///
			_col(4)"** Last date modified: `c(current_date)'" _n ///
			_col(4)"*/" _n _n ///
			_col(4)"*Standardize settings accross users" _n ///
			_col(4)"ieboilstart, version(12.1)" _col(40) "//Set the version number to the oldes version used by anyone in the project team" _n ///
			_col(4) _char(96)"r(version)'" 		_col(40) "//This line is needed to actually set the version from the command above" _n ///
			_n ///
			_col(4)"* ***************************************************** *" _n ///
			_col(4)"*" _col(60) "*" _n ///

		*di "masterDofilePart0a end"

	end

	cap program drop 	parseReadLine
	program define		parseReadLine, rclass

		args line

		*Tokenize each line
		tokenize  `"`line'"' , parse("*")

		**Parse the beginning of the line to
		* test if this line is an iefolder line
		local divisorStar  		"`1'"
		local divisorIefoldeer  "`2'"

		if `"`divisorStar'`divisorIefoldeer'"' != "*iefolder" {

			*This is not a iefolder divisor
			return scalar ief_line 	= 0
			return scalar ief_end 	= 0
		}
		else {

			*This is a iefolder divisor,
			return scalar ief_line 	= 1

			*parse the rest of the line (from tokenize above)
			local partNum  		"`4'"
			local partName 		"`6'"
			local sectionNum 	"`8'"
			local sectionName 	"`10'"
			local itemName 		"`12'"
			local itemAbb	 	"`14'"

			*Return the part name and number. All iefolder divisor lines has this
			return local partNum  "`partNum'"
			return local partName "`partName'"

			*Get the prefix of the section name
			local sectPrefix = substr("`partName'",1,4)

			*Test if it is an end of section divisor
			if "`sectPrefix'" == "End_" {

				return scalar ief_end 	= 1
			}
			else {

				return scalar ief_end 	= 0

				*These are returned empty if they do not apply
				return local sectionNum 	"`sectionNum'"
				return local sectionName	"`sectionName'"
				return local sectionAbb 	"`sectionAbb'"
				return local itemName		"`itemName'"
				return local itemAbb 		"`itemAbb'"
			}
		}
		*di "parseReadLine end"
	end

	cap program drop 	newRndFolderAndGlobals
	program define		newRndFolderAndGlobals

		args rndName rnd dtfld_glb subHandle

		*KBKB: test if new fodler can be written - use "cap cd"

		*Write title to the folder globals to this round
		file write  `subHandle'	_col(4)"*`rndName' folder globals" _n

		*Round main folder
		createFolderWriteGlobal "`rndName'" 	 		"`dtfld_glb'"  	"`rnd'" 		`subHandle'

		*Sub folders
		createFolderWriteGlobal "DataSets" 				"`rnd'" 		"`rnd'_dt" 		`subHandle'
		createFolderWriteGlobal "Dofiles" 				"`rnd'"			"`rnd'_do" 		`subHandle'
		createFolderWriteGlobal "Output" 				"`rnd'"			"`rnd'_out"		`subHandle'
		createFolderWriteGlobal "Documentation" 		"`rnd'"	 		"`rnd'_doc"
		createFolderWriteGlobal "Questionnaire" 		"`rnd'"	 		"`rnd'_quest"

		*Create round master dofile
		createRoundMasterDofile "$projectfolder/`rndName'" "`rndName'" "`rnd'"

	end

	cap program drop 	createRoundMasterDofile
		program define	createRoundMasterDofile

		args roundfolder rndName rnd

		*Create a temporary textfile
		tempname 	roundHandle
		tempfile	roundTextFile

		file open  	`roundHandle' using "`roundTextFile'", text write replace

		*Write intro part with description of round,
		mdofle_p0 		`roundHandle' round
		mdofle_p1_round `roundHandle' `rndName'

		*DataSets sub-folder
		file write  `roundHandle'		_col(4)"*DataSets sub-folder globals" _n
		createFolderWriteGlobal "Raw"  							"`rnd'_dt" 		"`rnd'_dtRaw" 	`roundHandle'
		createFolderWriteGlobal "Intermediate" 					"`rnd'_dt" 		"`rnd'_dtInt" 	`roundHandle'
		createFolderWriteGlobal "Final"  						"`rnd'_dt" 		"`rnd'_dtFin" 	`roundHandle'

		*Dofile sub-folder
		file write  `roundHandle' _n	_col(4)"*Dofile sub-folder globals" _n
		createFolderWriteGlobal "Dofiles Import "				"`rnd'_do" 		"`rnd'_doImp" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Cleaning "				"`rnd'_do" 		"`rnd'_doCln" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Construct "			"`rnd'_do" 		"`rnd'_doCon" 	`roundHandle'
		createFolderWriteGlobal "Dofiles Analyze "				"`rnd'_do" 		"`rnd'_doAnl" 	`roundHandle'

		*Output subfolders
		file write  `roundHandle' _n	_col(4)"*Output sub-folder globals" _n
		createFolderWriteGlobal "Raw" 							"`rnd'_out"	 	"`rnd'_outRaw"	`roundHandle'
		createFolderWriteGlobal "Final" 						"`rnd'_out"	 	"`rnd'_outFin"	`roundHandle'

		*Questionnaire subfolders
		file write  `roundHandle' _n	_col(4)"*Questionnaire sub-folder globals" _n
		createFolderWriteGlobal "Questionnaire Develop" 		"`rnd'_quest"	 "`rnd'_qstDev"	`roundHandle'
		createFolderWriteGlobal "Questionnaire Final" 			"`rnd'_quest"	 "`rnd'_qstFin"	`roundHandle'
		createFolderWriteGlobal "PreloadData"	 				"`rnd'_quest"	 "`rnd'_prld"	`roundHandle'
		createFolderWriteGlobal "Questionnaire Documentation"	"`rnd'_quest"	 "`rnd'_doc"	`roundHandle'

		*Write sub devisor starting master and monitor data section section
		writeDevisor 	`roundHandle' 1 End_FolderGlobals

		mdofle_p3 `roundHandle' round `rndName' `rnd'

		*Closing the new main master dofile handle
		file close 		`roundHandle'

		*Copy the new master dofile from the tempfile to the original position
		copy "`roundTextFile'"  "`roundfolder'/`rndName'_MasterDofile.do" , replace

	end

	cap program drop 	createFolderWriteGlobal
		program define	createFolderWriteGlobal

		args  folderName parentGlobal globalName subHandle

			*di "createFolderWriteGlobal start `folderName'"

			if ("`subHandle'" != "") {

				file write  `subHandle' ///
					_col(4) `"global `globalName'"' _col(34) ///
					`"""' _char(36)`"`parentGlobal'/`folderName'" "' _n

			}

			global `globalName' "$`parentGlobal'/`folderName'"

			*noi di `"mkdir "${`parentGlobal'}\\`folderName'""'

			mkdir "${`parentGlobal'}\\`folderName'"

			*di "createFolderWriteGlobal end `folderName'"
	end

	cap program drop 	writeDevisor
	program define		writeDevisor , rclass

		args  subHandle partNum partName sectionNum sectionName itemName itemAbb

		*di "devisor start"

		local devisor "*iefolder*`partNum'*`partName'*`sectionNum'*`sectionName'*`itemName'*`itemAbb'*"

		local devisorLen = strlen("`devisor'")

		*Make all devisors at least 80 characters wide
		if (`devisorLen' < 80) {

			local numStars = 80 - `devisorLen'
			local addedStars _dup(`numStars') _char(42)
		}

		file write  `subHandle' _n "`devisor'" `addedStars'
		file write  `subHandle' _n "*iefolder wont work properly if the line above is edited"  _n _n

		*di "devisor end"

	end

	/*

		The functions below are all related to write the mostly
		static part of the master do file

	*/

	cap program drop 	mdofle_p0
	program define		mdofle_p0

		args subHandle itemType

		*di "start masterDofilePart0"

		file write  `subHandle' ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"*" _col(20) "your_`itemType'_name" _col(75) "*" _n ///
			_col(4)"*" _col(20) "MASTER DO_FILE" _col(75) "*" _n ///
			_col(4)"*" _col(75) "*" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"* ******************************************************************** *" _n ///
			_n _col(8)"/*" _n

		if "`itemType'" == "project" {

			file write  `subHandle'	///
				_col(8)"** PURPOSE:" _col(25) "Write intro to project here" _n ///
				_n ///
				_col(8)"** OUTLINE:" _col(25) "PART 0: Standardize settings and install paackages" _n ///
				_col(25) "PART 1: Set globals for dynamic file paths" _n ///
				_col(25) "PART 2: Set globals for constants and varlist" _n ///
				_col(32) "used across the project. Intall custom" _n ///
				_col(32) "commands needed." _n ///
				_col(25) "PART 3: Call the task specific master do-files " _n ///
				_col(32) "that call all do-files needed for that " _n ///
				_col(32) "tas. Do not include Part 0-2 in a task" _n ///
				_col(32) "specific master do-file" _n ///
				_n _n

			/*
			file write  `subHandle'	///
				_col(8)"** REQUIRES:" _col(25) "List all data sets using the globals that you " _n ///
				_col(25) "define below to indicate what data you will need" _n ///
				_n ///
				_col(25) "Example:" _n ///
				_col(25) _char(36)"BL_dtRaw/baseline_survey_v1.csv" _n ///
				_col(25) _char(36)"BL_dtRaw/baseline_survey_v2.csv" _n ///
				_col(25) _char(36)"moniData/monitor2016_data.xlsx" _n ///
				_n

			file write  `subHandle'	///
				_col(8)"** CREATES:" _col(25) "List all data sets using the globals that you " _n ///
				_col(25) "define below to indicate what data set are " _n ///
				_col(25) "created by the do-files this master do-file calls" _n ///
				_n ///
				_col(25) "Example:" _n ///
				_col(25) _char(36)"BL_dtFinal/baseline_clean.dta" _n ///
				_col(25) _char(36)"BL_dtFinal/baseline_constructs.dta" _n ///
				_col(25) "Multiple tables and grahps in \$BL_Out" _n ///
				_n
			*/
		}
		else if "`itemType'" == "round" {

			file write  `subHandle'	///
				_col(8)"** PURPOSE:" _col(25) "Write intro to round here" _n ///
				_n ///
				_col(8)"** OUTLINE:" _col(25) "PART 0: Standardize settings and install paackages" _n ///
				_col(25) "PART 1:" _n _col(25) "PART 2:" _n ///
				_col(25) "PART 3:" _n _col(25) "PART 4:" _n ///
				_n _n

		}

		file write  `subHandle'	///
			_col(8)"** IDS VAR:" _col(25) "list_ID_var_here		//Uniquely identifies households (update for your proejct)" _n ///
			_n	///
			_col(8)"** NOTES:" _n ///
			_n ///
			_col(8)"** WRITEN BY:" _col(25) "names_of_contributors" _n ///
			_n ///
			_col(8)"** Last date modified:" _n ///
			_n ///
			_col(8)"*/" _n

		*Write devisor starting setting standardize section
		writeDevisor  `subHandle' 0 StandardSettings

		file write  `subHandle'	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 0:  INSTALL PACKAGES AND STANDARDIZE SETTINGS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Install packages needed to run all dofiles called" _n ///
			_col(4)"*" _col(17) "by this master dofile." _n ///
			_col(4)"*" _col(16) "-Use ieboilstart to harmonize settings across users" _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		*Write devisor ending setting standardize section
		writeDevisor `subHandle' 0 End_StandardSettings

		file write  `subHandle' 	///
			_col(8)"*Install all packages that this project requires:" _n ///
			_col(8)"ssc install ietoolkit" _n ///
			_n	 ///
			_n	 ///
			_col(8)"*Standardize settings accross users" _n ///
			_col(8)"ieboilstart, version(12.1)" _col(40) "//Set the version number to the oldes version used by anyone in the project team" _n ///
			_col(8) _char(96)"r(version)'" 		_col(40) "//This line is needed to actually set the version from the command above" _n

		*di "masterDofilePart0a end"

	end

cap program drop 	mdofle_p1
	program define	mdofle_p1

		args   subHandle projectDir

		*Write devisor starting globals section
		writeDevisor  `subHandle' 1 FolderGlobals

		*di "masterDofilePart1 start"

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 1:  PREPARING FOLDER PATH GLOBALS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set the global box to point to the project folder" _n ///
			_col(4)"*" _col(17) "on each collaborators computer." _n ///
			_col(4)"*" _col(16) "-Set other locals that point to other folders of interest." _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		file write  `subHandle' ///
			_n ///
			_col(4)"* Users" _n ///
			_col(4)"* -----------" _n ///
			_n ///
			_col(4)"*User Number:" _n ///
			_col(4)"* You" _col(30) "1" _col(35) `"//Replace "You" with your name"' _n ///
			_col(4)"* Next User" _col(30) "2" _col(35) "//Assign a user number to each additional collaborator of this code" _n ///
			_n ///
			_col(4)"*Set this value to the user currently using this file" _n ///
			_col(4)"global user  1" _n ///
			_n ///
			_col(4)"* Root folder globals" _n ///
			_col(4)"* ---------------------" _n ///
			_n ///
			_col(4)"if "_char(36)"user == 1 {" _n ///
			_col(8)`"global projectfolder "`projectDir'""' _n ///
			_col(4)"}" _n ///
			_n ///
			_col(4)"if "_char(36)"user == 2 {" _n ///
			_col(8)`"global projectfolder ""  //Enter the file path to the projectfolder of next user here"' _n ///
			_col(4)"}" _n ///
			_n ///
			_n ///
			_col(4)"* Project folder globals" _n ///
			_col(4)"* ---------------------" _n

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 FolderGlobals 1 master

		*Create master data folder and add global to folder in master do file
		createFolderWriteGlobal "MasterData"  "projectfolder"  mastData  `subHandle'

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 FolderGlobals 3 monitor

		*Write sub devisor starting master and monitor data section section
		writeDevisor `subHandle' 1 End_FolderGlobals

		*di "masterDofilePart1a end"

	end

cap program drop 	mdofle_p1_round
	program define	mdofle_p1_round

		args   subHandle rndName

		local rndName_caps = upper("`rndName'")

		*Write devisor starting globals section
		writeDevisor  `subHandle' 1 FolderGlobals

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 1:  PREPARING `rndName_caps' FOLDER PATH GLOBALS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set global to point to the `rndName' folders" _n ///
			_col(4)"*" _col(16) "-Add to these globals as you need" _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n _n

	end

cap program drop 	mdofle_p2
	program define	mdofle_p2

		args   subHandle

		di "masterDofilePart2 start"

		*Write devisor starting standardization globals section
		writeDevisor  `subHandle' 2 StandardGlobals

		file write  `subHandle' 	///
			_col(4)"* ******************************************************************** *" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(12) "PART 2: - SET STANDARDIZATION GLOBALS AND OTHER CONSTANTS" _n ///
			_col(4)"*" _n ///
			_col(4)"*" _col(16) "-Set globals with numbers or lists of " _n ///
			_col(4)"*" _col(17) "variables that is supposed to stay the" _n ///
			_col(4)"*" _col(17) "same across the project." _n ///
			_col(4)"*" _n ///
			_col(4)"* ******************************************************************** *" _n

		file write  `subHandle' ///
			_n ///
			_col(4)"* Set all conversion rates used in unit standardization " _n ///
			_col(4)"* accross the whole project here. " _n ///
			_n ///
			_col(4)"**Example. Expand this section with globals for all constant" _n ///
			_col(4)"* scalars used in this project. Reference these globals instead" _n ///
			_col(4)"* of hardcode values each time constant converstion rates are used." _n ///
			_col(4)"*Standardizing to meters" _n ///
			_n ///
			_col(4)"global foot" _col(20) "= 0.3048" _n ///
			_col(4)"global mile" _col(20) "= 1609.34" _n ///
			_col(4)"global km" 	 _col(20) "= 1000" _n ///
			_n ///
			_col(4)"**Other examples to be included here could be regression controls" _n ///
			_col(4)"* used across the project. Everything that is constant may be" _n ///
			_col(4)"* included here. One example of something not constant that should" _n ///
			_col(4)"* be included here is exchange rates. It is best practice to have one" _n ///
			_col(4)"* global with the exchange rate here, and reference this each time a" _n ///
			_col(4)"* currency conversion is done. If the currency exchange reate needs to be" _n ///
			_col(4)"* updated, then it only has to be done at one place for the whole project." _n ///
			_n

		*Write devisor ending standardization globals section
		writeDevisor  `subHandle' 2 End_StandardGlobals

		di "masterDofilePart2 end"

	end

cap program drop 	mdofle_p3
	program define	mdofle_p3

		args   subHandle itemType rndName rnd

		di "masterDofilePart3 start"

		*Write devisor starting the section running sub-master dofiles
		writeDevisor  `subHandle' 3 RunDofiles

			file write  `subHandle' 	///
				_col(4)"* ******************************************************************** *" _n ///
				_col(4)"*" _n ///
				_col(4)"*" _col(12) "PART 3: - RUN DOFILES CALLED BY THIS MASTER DO FILE" _n ///
				_col(4)"*" _n

			if "`itemType'" == "project" {
				file write  `subHandle' 	///
					_col(4)"*" _col(16) "-When survey rounds are added, this section will" _n ///
					_col(4)"*" _col(17) "link to the master dofile for that round." _n ///
					_col(4)"*" _col(16) "-The default is that these dofiles are set to not"  _n ///
					_col(4)"*" _col(17) "run. It is rare that all round specfic master dofiles"  _n ///
					_col(4)"*" _col(17) "are called at the same time, the round specifc master"  _n ///
					_col(4)"*" _col(17) "dofiles are almost always called individually. The"  _n ///
					_col(4)"*" _col(17) "excpetion is when reviewing or replicating a full project." _n
			}
			else if "`itemType'" == "round" {
				file write  `subHandle' 	///
					_col(4)"*" _col(16) "-A task master dofile has been created for each high"  _n ///
					_col(4)"*" _col(17) "level task (import, cleaning, construct, analyze). By "  _n ///
					_col(4)"*" _col(17) "running all of them all data work associated with the "  _n ///
					_col(4)"*" _col(17) "`rndName' should be replciaetd, including output of "  _n ///
					_col(4)"*" _col(17) "tablets, graphs, etc." _n ///
					_col(4)"*" _col(16) "-Feel free to add to this list if you have other high"  _n ///
					_col(4)"*" _col(17) "level tasks relevant to your project." _n
			}

			file write  `subHandle' 	///
				_col(4)"*" _n ///
				_col(4)"* ******************************************************************** *" _n

		if "`itemType'" == "round" {

			file write  `subHandle' 	_n ///
				_col(4)"**Set the locals corresponding to the taks you want" _n ///
				_col(4)"* run to 1. To not run a task, set the local to 0." _n ///
				_col(4)"local importDo" _col(25) "1" _n ///
				_col(4)"local cleaningDo" _col(25) "1" _n ///
				_col(4)"local constructDo" _col(25) "1" _n ///
				_col(4)"local analyzeDo" _col(25) "1" _n ///

			*Create the references to the high level task
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "import"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "cleaning"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "construct"
			highLevelTask `subHandle'  "`rndName'" "`rnd'" "analyze"

		}

		*Write devisor ending the section running sub-master dofiles
		writeDevisor  `subHandle' 3 End_RunDofiles

		di "masterDofilePart3 end"

	end

	cap program drop 	highLevelTask
		program define	highLevelTask

			args roundHandle rndName rnd task

			di "highLevelTask start"

			*Write section where task master files are called
			file write  `roundHandle' 	_n ///
				_col(4)"if (" _char(96) "`task'' == 1) { //Change the local above to run or not to run this file" _n ///
				_col(8) `"do ""' _char(36) `"`rnd'_do/`task'_MasterDofile.do" "' _n ///
				_col(4)"}" _n

			*Create the task dofiles
			highLevelTaskMasterDofile rndName task

	end

	cap program drop 	highLevelTaskMasterDofile
		program define	highLevelTaskMasterDofile

			di "highLevelTaskMasterDofile start"

			args rndName task

			local suffix
			if "`task'" == "import" {
				local suffix _doImp
			}
			if "`task'" == "cleaning" {
				local suffix _doCln
			}
			if "`task'" == "construct" {
				local suffix _doCon
			}
			if "`task'" == "analyze" {
				local suffix _doAnl
			}

			*Write the round dofile

	end


combine and redo table

combine and redo table

program dd_simple

use ${stash}panelfips, clear

local outcomes "`1'"
local mode `2'
local option `3'

di "Outcomes : `outcomes'"
di "Mode: `mode'"
di "Filename: `option'"

if "`mode'" == "run"{
    di "running"
    runreg "`outcomes'"
}

if "`mode'" == "write"{
    di "writing"
    writereg "`outcomes'" `option'
}

end

program runreg

local outcomes "`1'"

est clear

foreach x in `outcomes'{
    gen ln`x' = ln(`x'+1)

    local model reg
    eststo: qui `model' ln`x' 1.treat#1.post 1.treat i.time pop_year, cluster(fips)
    estadd local countyfe "No", replace
    estadd local monthfe "Yes", replace
    estimates save ${tables}ddsimple_`x'_`model', replace

    local model xtreg
    eststo: qui `model' ln`x' 1.treat#1.post i.time pop_year, fe cluster(fips)
    estadd local countyfe "Yes", replace
    estadd local monthfe "Yes", replace
    estimates save ${tables}ddsimple_`x'_`model', replace

    local model xtpoisson
    eststo: qui `model' `x' 1.treat#1.post i.time pop_year, fe vce(robust)
    estadd local countyfe "Yes", replace
    estadd local monthfe "Yes", replace
    estimates save ${tables}ddsimple_`x'_`model', replace
}

end

program writereg

est clear
local outcomes "`1'"
local filename `2'

foreach x in `outcomes'{
    foreach y in reg xtreg xtpoisson{
        di "`x' : `y'"
        estimates use ${tables}ddsimple_`x'_`y'
        eststo est`x'_`y'
    }
}

esttab using "${tables}`filename'.tex", keep(1.treat#1.post 1.treat) se ar2 nonotes star(+ 0.15 * 0.10 ** 0.05 *** 0.01) coeflabels(1.treat "Treat" 1.treat#1.post "Post X Treat"  _cons "Constant") replace booktabs s(countyfe monthfe monthfe N N_g, label("County FE" "Year FE" "Population" "N" "Clusters")) label mgroups("OLS" "OLS" "Poisson QMLE" "OLS" "OLS" "Poisson QMLE" , pattern(1 1 1 1 1 1)) mtitles("Ln(Contrib)" "Ln(Contrib)" "Contrib" "Ln(Contrib)" "Ln(Contrib)" "Contrib")

end

program dd_simple

use ${stash}panelfips, clear

local outcomes "`1'"
local mode `2'
local option `3'

* local filename "crossreg"
* local outcomes "numcontrib numuser numserious90"

di "Outcomes : `outcomes'"
di "Mode: `mode'"
di "Filename: `option'"

if "`mode'" == "run"{
    di "running"
    runreg "`outcomes'" `option'
}

if "`mode'" == "write"{
    di "writing"
    writereg "`outcomes'" `option'
}

end

program runreg

local outcomes "`1'"
local model `2'

est clear
foreach x in `outcomes'{
    di "`x' : `model'"
    eststo: `model' `x' 1.treat#1.post 1.post i.year, fe vce(robust)
    estadd local countyfe "Yes", replace
    estadd local monthfe "Yes", replace
    estimates save ${tables}ddsimple_`x'_`model', replace
}

end

program writereg

est clear
local outcomes "`1'"
local filename `2'

foreach x in `outcomes'{
    foreach y in xtreg xtpoisson{
        di "`x' : `y'"
        estimates use ${tables}ddsimple_`x'_`y'
        eststo est`x'_`y'
    }
}

esttab using "${tables}`filename'.tex", keep(1.treat#1.post 1.post) se ar2 nonotes star(+ 0.15 * 0.10 ** 0.05 *** 0.01) coeflabels(1.treat#1.post "Post X TIGER" 1.post "Post" _cons "Constant") replace booktabs s(countyfe monthfe N N_g, label("County FE" "Year FE" "N" "Clusters")) label mgroups("OLS" "Poisson" "OLS" "Poisson" "OLS" "Poisson" , pattern(1 1 1 1 1 1))

end


redo: switch to atmega1284p

redo: switch to atmega1284p

#!/bin/bash
#
# automaton
# (c) 2013 Justin Gottula
# The source code of this project is distributed under the terms of the
# simplified BSD license. See the LICENSE file for details.
#
# main redo script

exec >&2

# redo appears to be broken
set -e

TARGET=$1
TARGET_BASE=$2
OUTPUT=$3

PROJ_NAME="automaton"

# configuration files for microcontroller and programmer
CFG_MCU="atmega1284p"
CFG_PROG="dragon_jtag"

CFG_FILES=("cfg/mcu/$CFG_MCU" "cfg/prog/$CFG_PROG")
redo-ifchange "${CFG_FILES[@]}"
for CFG_FILE in "${CFG_FILES[@]}"; do
	source "$CFG_FILE"
done

CC="avr-gcc"
AS="avr-as"
LD="avr-ld"

NM="avr-nm"
OBJCOPY="avr-objcopy"
OBJDUMP="avr-objdump"

AVRDUDE="avrdude"

SOURCES=$(find src -type f -iname "*.c")
OBJECTS=${SOURCES//.c/.o}

OUT_ELF="out/$PROJ_NAME.elf"
OUT_BIN="out/$PROJ_NAME.bin"
OUT_MAP="out/$PROJ_NAME.map"
OUT_DUMP="out/$PROJ_NAME.dump"
OUT_LST="out/$PROJ_NAME.lst"

DEFINES=("F_CPU=${CPU_KHZ}000UL")
INC_DIRS=(src)
LIB_DIRS=()
LIBS=()

CFLAGS_STD=(-std=gnu11)
CFLAGS_CPU=("-mmcu=$MCU" -fpack-struct -funsigned-bitfields)
CFLAGS_DEBUG=(-ggdb)
CFLAGS_OPT=(-Os -flto -fuse-linker-plugin -fwhole-program -mcall-prologues \
	-mrelax -mstrict-X -fmerge-all-constants -ffast-math -fno-jump-tables \
	-fshort-enums -Wl,--gc-sections)
CFLAGS_WARN=(-Wall -Wextra -Wno-unused-function -fno-diagnostics-show-caret)
CFLAGS_ETC=(-pipe)

CFLAGS=("${CFLAGS_STD[@]}" "${CFLAGS_CPU[@]}" "${CFLAGS_DEBUG[@]}" \
	"${CFLAGS_OPT[@]}" "${CFLAGS_WARN[@]}" "${CFLAGS_ETC[@]}")
CFLAGS_COMPILE=("${CFLAGS[@]}" "${DEFINES[@]/#/-D}" "${INC_DIRS[@]/#/-I}")
CFLAGS_LINK=("${CFLAGS[@]}" "${LIB_DIRS[@]/#/-L}" "${LIBS[@]/#/-l}")

AVRDUDE_FLAGS=("${AVRDUDE_PART[@]/#/-p}" "${AVRDUDE_PROG[@]/#/-c}" \
	"${AVRDUDE_PORT[@]/#/-P}" "${AVRDUDE_BITCLOCK[@]/#/-B}" \
	"${AVRDUDE_BAUDRATE[@]/#/-b}" "${AVRDUDE_BANGDELAY[@]/#/-i}")

AVRDUDE_FLAGS_LOAD=("${AVRDUDE_FLAGS[@]}" -u \
	-Uflash:w:"$OUT_BIN":r)
AVRDUDE_FLAGS_VERIFY=("${AVRDUDE_FLAGS[@]}" -u \
	-Uflash:v:"$OUT_BIN":r)
AVRDUDE_FLAGS_FUSE=("${AVRDUDE_FLAGS[@]}" -q \
	-Ulfuse:w:"$AVRDUDE_LFUSE":m \
	-Uhfuse:w:"$AVRDUDE_HFUSE":m \
	-Uefuse:w:"$AVRDUDE_EFUSE":m)
AVRDUDE_FLAGS_TERM=("${AVRDUDE_FLAGS[@]}" -t)

case "$TARGET" in
all)
	redo-ifchange $OUT_ELF $OUT_BIN $OUT_MAP $OUT_DUMP $OUT_LST
	;;
$OUT_ELF)
	redo-ifchange $OBJECTS
	$CC "${CFLAGS_LINK[@]}" -o$OUTPUT $OBJECTS
	;;
$OUT_BIN)
	redo-ifchange $OUT_ELF
	$OBJCOPY -Obinary -j.text -j.data $OUT_ELF $OUTPUT

	stat --printf="%n: %s bytes\n" $OUTPUT
	md5sum $OUTPUT
	;;
$OUT_MAP)
	redo-ifchange $OUT_ELF
	$NM -Cn $OUT_ELF >$OUTPUT
	;;
$OUT_DUMP)
	redo-ifchange $OUT_ELF
	$OBJDUMP -aCdfhp $OUT_ELF >$OUTPUT
	;;
$OUT_LST)
	redo-ifchange $OUT_ELF
	$OBJDUMP -CdS -j.text $OUT_ELF >$OUTPUT
	;;
*.o)
	$CC "${CFLAGS_COMPILE[@]}" -o${TARGET//.o/.dep} -MM -MG ${TARGET//.o/.c}

	read DEPS <${TARGET//.o/.dep}
	redo-ifchange ${DEPS#*:}

	$CC "${CFLAGS_COMPILE[@]}" -o$OUTPUT -c ${TARGET//.o/.c}
	;;
clean)
	rm -rf $(find out -type f)
	rm -rf $(find src -type f -iname "*.o")
	rm -rf $(find src -type f -iname "*.dep")
	;;
load)
	redo all
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_LOAD[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_LOAD[@]}"
	;;
verify)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_VERIFY[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_VERIFY[@]}"
	;;
fuse)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_FUSE[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_FUSE[@]}"
	;;
term)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_TERM[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_TERM[@]}"
	;;
*)
	echo "unknown target '$TARGET'"
	exit 1
	;;
esac

#!/bin/bash
#
# automaton
# (c) 2013 Justin Gottula
# The source code of this project is distributed under the terms of the
# simplified BSD license. See the LICENSE file for details.
#
# main redo script

exec >&2

# redo appears to be broken
set -e

TARGET=$1
TARGET_BASE=$2
OUTPUT=$3

PROJ_NAME="automaton"

# configuration files for microcontroller and programmer
CFG_MCU="atmega324a"
CFG_PROG="dragon_jtag"

CFG_FILES=("cfg/mcu/$CFG_MCU" "cfg/prog/$CFG_PROG")
redo-ifchange "${CFG_FILES[@]}"
for CFG_FILE in "${CFG_FILES[@]}"; do
	source "$CFG_FILE"
done

CC="avr-gcc"
AS="avr-as"
LD="avr-ld"

NM="avr-nm"
OBJCOPY="avr-objcopy"
OBJDUMP="avr-objdump"

AVRDUDE="avrdude"

SOURCES=$(find src -type f -iname "*.c")
OBJECTS=${SOURCES//.c/.o}

OUT_ELF="out/$PROJ_NAME.elf"
OUT_BIN="out/$PROJ_NAME.bin"
OUT_MAP="out/$PROJ_NAME.map"
OUT_DUMP="out/$PROJ_NAME.dump"
OUT_LST="out/$PROJ_NAME.lst"

DEFINES=("F_CPU=${CPU_KHZ}000UL")
INC_DIRS=(src)
LIB_DIRS=()
LIBS=()

CFLAGS_STD=(-std=gnu11)
CFLAGS_CPU=("-mmcu=$MCU" -fpack-struct -funsigned-bitfields)
CFLAGS_DEBUG=(-ggdb)
CFLAGS_OPT=(-Os -flto -fuse-linker-plugin -fwhole-program -mcall-prologues \
	-mrelax -mstrict-X -fmerge-all-constants -ffast-math -fno-jump-tables \
	-fshort-enums -Wl,--gc-sections)
CFLAGS_WARN=(-Wall -Wextra -Wno-unused-function -fno-diagnostics-show-caret)
CFLAGS_ETC=(-pipe)

CFLAGS=("${CFLAGS_STD[@]}" "${CFLAGS_CPU[@]}" "${CFLAGS_DEBUG[@]}" \
	"${CFLAGS_OPT[@]}" "${CFLAGS_WARN[@]}" "${CFLAGS_ETC[@]}")
CFLAGS_COMPILE=("${CFLAGS[@]}" "${DEFINES[@]/#/-D}" "${INC_DIRS[@]/#/-I}")
CFLAGS_LINK=("${CFLAGS[@]}" "${LIB_DIRS[@]/#/-L}" "${LIBS[@]/#/-l}")

AVRDUDE_FLAGS=("${AVRDUDE_PART[@]/#/-p}" "${AVRDUDE_PROG[@]/#/-c}" \
	"${AVRDUDE_PORT[@]/#/-P}" "${AVRDUDE_BITCLOCK[@]/#/-B}" \
	"${AVRDUDE_BAUDRATE[@]/#/-b}" "${AVRDUDE_BANGDELAY[@]/#/-i}")

AVRDUDE_FLAGS_LOAD=("${AVRDUDE_FLAGS[@]}" -u \
	-Uflash:w:"$OUT_BIN":r)
AVRDUDE_FLAGS_VERIFY=("${AVRDUDE_FLAGS[@]}" -u \
	-Uflash:v:"$OUT_BIN":r)
AVRDUDE_FLAGS_FUSE=("${AVRDUDE_FLAGS[@]}" -q \
	-Ulfuse:w:"$AVRDUDE_LFUSE":m \
	-Uhfuse:w:"$AVRDUDE_HFUSE":m \
	-Uefuse:w:"$AVRDUDE_EFUSE":m)
AVRDUDE_FLAGS_TERM=("${AVRDUDE_FLAGS[@]}" -t)

case "$TARGET" in
all)
	redo-ifchange $OUT_ELF $OUT_BIN $OUT_MAP $OUT_DUMP $OUT_LST
	;;
$OUT_ELF)
	redo-ifchange $OBJECTS
	$CC "${CFLAGS_LINK[@]}" -o$OUTPUT $OBJECTS
	;;
$OUT_BIN)
	redo-ifchange $OUT_ELF
	$OBJCOPY -Obinary -j.text -j.data $OUT_ELF $OUTPUT

	stat --printf="%n: %s bytes\n" $OUTPUT
	md5sum $OUTPUT
	;;
$OUT_MAP)
	redo-ifchange $OUT_ELF
	$NM -Cn $OUT_ELF >$OUTPUT
	;;
$OUT_DUMP)
	redo-ifchange $OUT_ELF
	$OBJDUMP -aCdfhp $OUT_ELF >$OUTPUT
	;;
$OUT_LST)
	redo-ifchange $OUT_ELF
	$OBJDUMP -CdS -j.text $OUT_ELF >$OUTPUT
	;;
*.o)
	$CC "${CFLAGS_COMPILE[@]}" -o${TARGET//.o/.dep} -MM -MG ${TARGET//.o/.c}

	read DEPS <${TARGET//.o/.dep}
	redo-ifchange ${DEPS#*:}

	$CC "${CFLAGS_COMPILE[@]}" -o$OUTPUT -c ${TARGET//.o/.c}
	;;
clean)
	rm -rf $(find out -type f)
	rm -rf $(find src -type f -iname "*.o")
	rm -rf $(find src -type f -iname "*.dep")
	;;
load)
	redo all
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_LOAD[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_LOAD[@]}"
	;;
verify)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_VERIFY[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_VERIFY[@]}"
	;;
fuse)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_FUSE[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_FUSE[@]}"
	;;
term)
	echo "sudo $AVRDUDE ${AVRDUDE_FLAGS_TERM[@]}"
	sudo $AVRDUDE "${AVRDUDE_FLAGS_TERM[@]}"
	;;
*)
	echo "unknown target '$TARGET'"
	exit 1
	;;
esac


script updated to work with riviera pro

script updated to work with riviera pro

#========================================================================================================================
# Copyright (c) 2017 by Bitvis AS.  All rights reserved.
# You should have received a copy of the license file containing the MIT License (see LICENSE.TXT), if not,
# contact Bitvis AS <support@bitvis.no>.
#
# UVVM AND ANY PART THEREOF ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH UVVM OR THE USE OR
# OTHER DEALINGS IN UVVM.
#========================================================================================================================

# Overload quietly (Modelsim specific command) to let it work in Riviera-Pro
proc quietly { args } {
  if {[llength $args] == 0} {
    puts "quietly"
  } else {
    # this works since tcl prompt only prints the last command given. list prints "".
    uplevel $args; list;
  }
}

if {[batch_mode]} {
  onerror {abort all; exit -f -code 1}
  onbreak {abort all; exit -f}
} else {
  onerror {abort all}
}

quit -sim

# Argument number 1 : VHDL Version. Default 2002
quietly set vhdl_version "2008"
if { [info exists 1] } {
  quietly set vhdl_version "$1"
  unset 1
}

quietly set tb_part_path ../../bitvis_uart
do $tb_part_path/script/1_compile_src.do  $tb_part_path $vhdl_version
do $tb_part_path/script/2_compile_util.do $tb_part_path $vhdl_version
do $tb_part_path/script/3_compile_tb_dep_ex_util.do $tb_part_path $vhdl_version
do $tb_part_path/script/4_compile_uart_vvc_tb.do  $tb_part_path $vhdl_version
do $tb_part_path/script/5_simulate_uart_vvc_tb.do $tb_part_path $vhdl_version

#========================================================================================================================
# Copyright (c) 2017 by Bitvis AS.  All rights reserved.
# You should have received a copy of the license file containing the MIT License (see LICENSE.TXT), if not,
# contact Bitvis AS <support@bitvis.no>.
#
# UVVM AND ANY PART THEREOF ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH UVVM OR THE USE OR
# OTHER DEALINGS IN UVVM.
#========================================================================================================================

if {[batch_mode]} {
  onerror {abort all; exit -f -code 1}
  onbreak {abort all; exit -f}
} else {
  onerror {abort all}
}

quit -sim

# Argument number 1 : VHDL Version. Default 2002
quietly set vhdl_version "2008"
if { [info exists 1] } {
  quietly set vhdl_version "$1"
  unset 1
}

quietly set tb_part_path ../../bitvis_uart
do $tb_part_path/script/1_compile_src.do  $tb_part_path $vhdl_version
do $tb_part_path/script/2_compile_util.do $tb_part_path $vhdl_version
do $tb_part_path/script/3_compile_tb_dep_ex_util.do $tb_part_path $vhdl_version
do $tb_part_path/script/4_compile_uart_vvc_tb.do  $tb_part_path $vhdl_version
do $tb_part_path/script/5_simulate_uart_vvc_tb.do $tb_part_path $vhdl_version


t/350-deps: 'redo clean' was crashing.

t/350-deps: 'redo clean' was crashing.

redo basic/clean
rm -f *~ .*~ *.count t1a overwrite overwrite[123] \
	genfile2 genfile.log static.log

redo basic/clean dirtest/clean
rm -f *~ .*~ *.count t1a overwrite overwrite[123] \
	genfile2 genfile.log static.log


[BT] SP-488 #close #time 1h #comment aggregate result [RV] +review SR @admin


Add a category to the qaids, and estimate elasticity at mean

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2000.csv", delimiter(",") replace

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2005.csv", delimiter(",") replace

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2011.csv", delimiter(",") replace

*If we introduce some selection, i.e. if we focus on those with w1>0:

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)
*estat expenditure mu*
*estat uncompensated ce*
*outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2000.csv", delimiter(",") replace

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)
*estat expenditure mu*
*estat uncompensated ce*
*outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2005.csv", delimiter(",") replace

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) noquadratic
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)
*estat expenditure mu*
*estat uncompensated ce*
*outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2011.csv", delimiter(",") replace

*Still with selection, if we use demographics and the quadratic specification:

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr) vce(bootstrap)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w4, anot(5) prices(p1-p4) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*To compute bootstrap standard errors, add vce(bootstrap) at the end of the quaids line

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2000.csv", delimiter(",") replace

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2005.csv", delimiter(",") replace

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2011.csv", delimiter(",") replace

*If we introduce some selection, i.e. if we focus on those with w1>0:

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2000.csv", delimiter(",") replace

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2005.csv", delimiter(",") replace

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) noquadratic
estat expenditure mu*
estat uncompensated ce*
outsheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_quaids_2011.csv", delimiter(",") replace

*Still with selection, if we use demographics and the quadratic specification:

clear
*2000:
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2000.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*2005:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2005.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr) vce(bootstrap)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*2011:
clear
insheet using "C:\Users\thomas.douenne\Documents\GitHub\openfisca-france-indirect-taxation\openfisca_france_indirect_taxation\almost_ideal_demand_system\data_frame_energy_2011.csv", delimiter(",")
replace w1 = . if w1 == 0
quaids w1-w3, anot(5) prices(p1-p3) expenditure(depenses_par_uc) demographics(strate stalog nactifs nenfants dip14pr)
estat uncompensated, atmeans
matrix list r(uncompelas)
estat expenditure, atmeans
matrix list r(expelas)

*To compute bootstrap standard errors, add vce(bootstrap) at the end of the quaids line