/****************
* Map 1960 share nonwhite by 1960 counties 
* requires extra .ado files:
ssc install shp2dta --- turn shapefiles into Stata data
ssc install spmap --- make map using Stata graphics
****************/

ssc install shp2dta, replace //turn shapefiles into Stata data
ssc install spmap, replace // make map using Stata graphics


	// Grab 1960 share nonwhite and population from the raw Haines Census files
use "$data/02896-0074-Data",clear
rename var9 shnonwhite
rename var1 totpop
keep if level==1
keep *fip* shnonwhite totpop
drop if fips==11000 // duplicated, DC also = 11001
sort fips
save "$data2/1960cen", replace


	// Map super-county file back to counties
use "$data2/poverty_full_county2", clear
keep *fip* fedc6568_pc pct_?kput
gen fips1=stfips_f*1000 + cofips_f
drop if stfips_f==2 | stfips_f==15
drop stfips_f cofips_f

	// Create extra fips for subsumed counties
gen fips2=.
gen fips3=.
gen fips4=.
gen fips5=.

	// Use standard_cofips.do to put back missing counties
replace fips2=36005 if fips1==36061
replace fips3=36047 if fips1==36061
replace fips4=36081 if fips1==36061		//NYC
replace fips5=36085 if fips1==36061

replace fips2=51129 if fips1==51550
replace fips3=51785 if fips1==51550		// Chesapeake

replace fips2=51059 if fips1==51600

replace fips2=55078 if fips1==55115
replace fips3=55083 if fips1==55115		// wisconsin

replace fips2=56047 if fips1==56029		// yelowstone
replace fips2=30113 if fips1==30111

replace fips2=32025 if fips1==32510		// carson city

replace fips2=29186 if fips1==29193		// ste. genevieve par.

replace fips2=46071 if fips1==46131		//SD

replace fips2=51151 if fips1==51810

	// Turn into duplicate obs
gen x=_n
reshape long fips, i(x) j(fip_num)
drop fip_num
drop if fips==.
sort fips
save "$temp/fedc_map", replace


	// Translate county shapefile into data
	// Data about counties stored in county_db
	// Coordinates defining county shapes stored in county_coord
shp2dta using "$data/US_counties_19591231.shp", ///
	database("$temp/county_db") coordinates("$temp/county_coord") replace  genid(idvar)


	//-------------------------------
	// Prepare county mapping data
use "$data2/county_db", clear
destring FIPS, generate(fips)
drop if fips==.		// Some AK districts and two large lakes in Louisiana have shapes
					// but not fips codes
gen statefip=int(fips/1000)

	// Merge onto haines data
sort fips
merge 1:1 fips using "$data2/1960cen", generate(_m1)
drop if statefip==2 | statefip==15 | statefip>56		// Drop Alaska and Hawaii
list fips _m1 if _m1!=3


	// Add CAP grants
sort fips
merge 1:1 fips using "$temp/fedc_map", generate(_m2)
list fips _m1 _m2 if _m2!=3
drop if _m2==2
assert _m2!=1

	//---------------------
	// Graphing
local color_s1color "Blues2"
local color_s1mono "Greys2"

local borderwidth "vvthin"

local cut_fedc6568_pc "0 3 10 20 50 200 1000"
local cut_shnonwhite "0 100"

foreach schemename in s1mono s1color {	// Optimize baseline chart scheme for b/w printing

	foreach var in fedc6568_pc  { 
		spmap `var' using "$temp/county_coord" , id(idvar) fcolor(`color_`schemename'') 	/// 
			clnumber(14) 															///	
			osize(vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin vvthin )
		
		*graph display, xsize(6) ysize(4)
		graph save "$temp/map_`var'", replace
		*graph export "$output/map_`var'.eps", as(eps) logo(off) orientation(landscape) 
		graph export "$temp/temp.ps", as(ps) replace logo(off) 	///
			orientation(landscape) tmargin(0) lmargin(0)
		!ps2pdf "$temp/temp.ps" "$temp/temp.pdf"
		!pdfcrop --clip "$temp/temp.pdf" "$output/map_`var'_`schemename'.pdf"
		*!convert "$output/map_`var'_`schemename'.pdf" -size 1800x1200 "$output/map_`var'_`schemename'.jpg"
	}
}
