/******************
* Chart spending to be anticipated using
* funding formula vs. amount actually
* spent
* 
* NOTE that this do-file has the following 
* system requirements:
* (1) It must be run in Stata for Linux in order to
*     use the Serif font and CLI operations ps2pdf and 
*     converter to grayscale JPEG
* (2) It must be run in xstata (GUI stata) or the TIFF
*     export will malfunction
*
* Stata for Windows and Mac can substitute other fonts
* (e.g. Times New Roman) and export directly to PDF,
* but have no JPEG or grayscale options.
* http://www.imagemagick.org/
******************/

insheet using "$data/deg45_plot_extend.txt", clear
save "$temp/extend", replace			

set more off
use "$data2/poverty_full_data", clear
append using "$data2/extend"
drop if stfips_f==11 | stfips_f==2 | stfips_f==15


set scheme s1mono						// Graph appearance
graph set window fontface "Serif"		// Set font for all graph text;
										// NOTE! This is a system setting
										// and requires a different font name 
										// for different operating systems.
										// more explanation here:
										// http://www.stata.com/bookstore/pdf/g_text.pdf

#delimit ;
foreach y in 65 66 67 68 6568 {; // 
	
	gen fed_m`y'=fed`y'/1000000;	//<= use total (non-indexed) agg. NARA data!
	
	reg fed_m`y' pred_fund_`y';					// Fit values
	predict fitval;
	sort fitval;
	
foreach scale in fixed flex zoom {; // log
	if `y'<100 {;
		local year "19`y'";
		local min_fix=0;
		local min_log=1;
		local max=120;
		local max_zoom=15;
		local logticks "1 5 25 125";
	};
	if `y'==6568 {;
		local year "1965-8";
		local min_fix=2;
		local min_log=2;
		local max=261;
		local max_zoom=40;
		local logticks "4 16 64 256";
	};
	display "`scale' `y' `year' `min_fix' `min_log' `max' `logticks'";
	
	if "`scale'"=="log" {;
		display "log set";
		local min=`min_log';
		local scaleoptions `"
			yscale(log range(`min_log' `max')) 
			xscale(log range(`min_log' `max'))
			ylabel(`logticks')
			xlabel(`logticks')
		"'
		;
		//";
	};
	if "`scale'"=="fixed" {;
		display "fixed set";
		local min=`min_fix';
		local scaleoptions `"
			yscale(range(`min' `max')) 
			xscale(range(`min' `max'))
			ylabel(#4)
			xlabel(#4)
		"';
		//";
	};
	if "`scale'"=="flex" {;
		display "flex set";
		local min=`min_fix';
		local scaleoptions `"
			ylabel(#4)
			xlabel(#4)
		"';
		//";
	};
	if "`scale'"=="zoom" {;
		display "zoom set";
		local min=`min_fix';
		local max=`max_zoom';
		local scaleoptions `"
			yscale(range(`min' `max_zoom')) 
			xscale(range(`min' `max_zoom'))
			ylabel(#4)
			xlabel(#4)
		"';
		//";
	};
	
	display "macros complete";
	display "min `min' max `max'";


	
	twoway (scatter fed_m`y' pred_fund_`y' 
			if pred_fund_`y'>=`min' & fed_m`y'>=`min'
			& pred_fund_`y'<=`max' & fed_m`y'<=`max',
			mlabel(state_postal)
			mlabsize(small)
			mlabpos(0)
			msymbol(none)
			mlabcolor(black)
			legend(off)
		)
		(lfit pred_fund_`y' pred_fund_`y' 
			if pred_fund_`y'>=`min' & fed_m`y'>=`min'
			& pred_fund_`y'<=`max' & fed_m`y'<=`max',			// 45-deg line
			lpattern(solid)
		)
		(connect fitval pred_fund_`y' 
			if pred_fund_`y'>=`min' & fed_m`y'>=`min'
			& pred_fund_`y'<=`max' & fed_m`y'<=`max',			// fit line
			lpattern(dash)
			msymbol(i)
		),

		ytitle("Actual CAP Allocations")
		xtitle("State Funding Per EOA Index")
		`scaleoptions'
		;
		
	graph display, xsize(11) ysize(8.5);		// Resize to letter paper
	
	graph export "$temp/alloc_spent_`scale'`y'.ps", 
		replace
		as(ps)
		fontface(Serif)
		orientation(landscape)					// Export to PDF
		tmargin(0)
		lmargin(0)
	;
	!ps2pdf "$temp/alloc_spent_`scale'`y'.ps" "$output/alloc_spent_`y'_`scale'.pdf";

};	// end SCALE loop	

	drop fitval;
};	// end YEAR loop

