clear
set more off
*set mem 1g
# delimit ;

/************************
	***MACROS***
************************/;

**Helen's Paths;
if c(username)=="HelenW" {;
//PATH;
	local path "/Users/HelenW/Dropbox/BF";
//DEEFINE DATASET NAME;
	local simul_dataset "/Users/HelenW/Dropbox/BF/dtafiles/hhXwelldata_uncontaminated_1999.dta";
	local simul_datasetC "/Users/HelenW/Dropbox/BF/dtafiles/hhXwelldata_contaminated_1999.dta";
	local simul_datasetC100 "/Users/HelenW/Dropbox/BF/dtafiles/hhXwelldata_contaminated_1999_100.dta";
	local finaldataset "/Users/HelenW/Dropbox/BF/dtafiles/collapsedGPS1999.dta";
};
if c(username)=="gshastry" | c(username)=="Kartini" {;
**Kartini's Paths;
local name=c(username);
//PATH;
local path "C:\Users\\`name'\Dropbox\BF";
//DEEFINE DATASET NAME;
*desktop;
local simul_dataset "E:\Economics/BF/dtafiles/hhXwelldata_uncontaminated_1999.dta";
local simul_datasetC "E:\Economics/BF/dtafiles/hhXwelldata_contaminated_1999.dta";
local simul_datasetC100 "E:\Economics/BF/dtafiles/hhXwelldata_contaminated_1999_100.dta";
local finaldataset "E:\Economics/BF/dtafiles/collapsedGPS1999.dta";
*laptop;
local simul_dataset "`path'/dtafiles/hhXwelldata_uncontaminated_1999.dta";
local simul_datasetC "`path'/dtafiles/hhXwelldata_contaminated_1999.dta";
local simul_datasetC100 "`path'/dtafiles/hhXwelldata_contaminated_1999_100.dta";
local finaldataset "`path'/dtafiles/collapsedGPS1999.dta";
};


cd "`path'";

use "`path'/dtafiles/expandedDHSGPS1999.dta";
bysort dhsid: gen wellcount=_n;
sort wellcount; 

**HW: merges so that each value of wellcount is assigned to every cluster**;
merge m:1 wellcount using "`path'/dtafiles/BGS_labelled.dta";

*label all variables that we might use;

*Arsenic is a string and it would be more useful as a number: ren as arsenic;
gen arsenic=real(as);
replace arsenic=0.5 if as=="< 0.5";
replace arsenic=6 if as=="< 6";

label variable arsenic "Arsenic contamination of well";
ren lat_deg latwell;
label variable latwell "latitude of well";
ren long_deg longwell;
label variable longwell "longitude of well";
ren latnum lathhid;
label variable lathhid "latitude of household";
ren longnum longhhid;
label variable longhhid "longitude of household";
label variable well_depth "depth of well";
label variable dhsid "id number of household";

destring well_depth, replace;  

*calculate distance from longnum latnum to long_deg lat_deg;



vincenty latwell longwell lathhid longhhid, h(distance); 
*I used the regular command, but if you want to change it to km: inkm requests distances expressed in kilometers- the default is miles, each of which is 5280*12*2.54/1e5=1.609344 km;
label variable distance "distance between well and household";

egen mindistance=min(distance),by(dhsid);

label variable mindistance "minimum distance from household to well"; 

*have to use the round command because the egen/min line above rounds mindistance;
gen as_closest=arsenic if round(distance,.0001)==round(mindistance,.0001);

gen uncontaminatedcont=arsenic if arsenic<50;
gen latwellu=latwell;
replace latwellu=. if uncontaminatedcont==.;
gen longwellu=longwell;
replace longwellu=. if uncontaminatedcont==.;
vincenty latwellu longwellu lathhid longhhid, h(distanceu);
egen mindistanceu=min(distanceu), by(dhsid);
label variable mindistanceu "minimum distance from cluster to uncontaminated well";

gen contaminatedcont=arsenic if arsenic>=50;
gen latwellc=latwell;
replace latwellc=. if contaminatedcont==.;
gen longwellc=longwell;
replace longwellc=. if contaminatedcont==.;
vincenty latwellc longwellc lathhid longhhid, h(distancec);
egen mindistancec=min(distancec), by(dhsid);
label variable mindistancec "minimum distance from cluster to contaminated well";

gen as_walkable10=arsenic if distance<10;
label variable as_walkable10 "Arsenic level of wells within walking distance to household";

gen as_walkable5=arsenic if distance<5;
label variable as_walkable5 "Arsenic level of wells within walking distance to household";

gen year_closest=year_construction if round(distance,.0001)==round(mindistance,.0001);
label variable year_closest "year that the closest well was constructed";

gen year_walkable10=year_construction if distance<10;
label variable year_walkable10 "Year of construction of wells within walking distance (10) to household";

gen year_walkable5=year_construction if distance<5;
label variable year_walkable5 "Year of construction of wells within walking distance (5) to household"; 

egen temp=max(year_construction) if distance<10, by(dhsid);
gen as_youngest=arsenic if temp==year_construction;

gen depth_closest=well_depth if round(distance,.0001)==round(mindistance,.0001);
label variable depth_closest "Depth of well that is closest to household";

gen depth_walkable10=well_depth if distance<10;
label variable depth_walkable10 "Depth of wells within walking distance (10) to household";

gen depth_walkable5=well_depth if distance<5;
label variable depth_walkable5 "Depth of wells within walking distance (5) to household";

gen depth_dangerous=depth_walkable10 if well_depth>20 & well_depth<100;
label variable depth_dangerous "Depth of wells that is the range of depth where arsenic levels have been historically high"; 
*egen tempd1=max;

gen contaminated=1 if arsenic>=50;
gen contaminated5=contaminated if distance<5;
gen contaminated10=contaminated if distance<10;

gen uncontaminated=1 if arsenic<50;
gen uncontaminated5=uncontaminated if distance<5;
gen uncontaminated10=uncontaminated if distance<10;

egen tempd=max(well_depth) if distance<10, by(dhsid);
gen as_deepest=arsenic if tempd==well_depth;

egen tempd2=min(well_depth) if distance<10, by(dhsid);
gen as_shallowest=arsenic if tempd2==well_depth;

compress;
preserve;

keep if uncontaminated==1;
sum mindistanceu;
keep if distance<r(max)+10.1;
keep dhsid wellcount lathhid longhhid latwell longwell distance arsenic;

save "`simul_dataset'", replace;
restore;

preserve;

keep if contaminated==1;
sum mindistancec;
keep if distance<r(max)+10.1;
keep dhsid wellcount lathhid longhhid latwell longwell distance arsenic;

save "`simul_datasetC'", replace;
restore;

preserve;

keep if arsenic>=100;
egen mindistance100=min(distancec), by(dhsid);
sum mindistance100;
keep if distance<r(max)+10.1;
keep dhsid wellcount lathhid longhhid latwell longwell distance arsenic;

save "`simul_datasetC100'", replace;
restore;

**HW: collapses to a single observation per DHS cluster by summary stats**;
collapse (mean) as_closest mindistanceu mindistancec as_walkable5 as_walkable10 year_closest year_walkable5 year_walkable10 as_youngest as_deepest as_shallowest depth_closest depth_walkable5 depth_walkable10  
		(count) numwalkable5=as_walkable5 numwalkable10=as_walkable10 depth_dangerous numcontaminated5=contaminated5 numcontaminated10=contaminated10 numuncontaminated5=uncontaminated5 numuncontaminated10=uncontaminated10
		(min) minaswalkable5=as_walkable5 minaswalkable10=as_walkable10 minyearwalkable5=year_walkable5 minyearwalkable10=year_walkable10 minasyoungest=as_youngest mindepthwalkable5=depth_walkable5 mindepthwalkable10=depth_walkable10 
		(max) maxaswalkable5=as_walkable5 maxaswalkable10=as_walkable10	maxyearwalkable5=year_walkable5 maxyearwalkable10=year_walkable10 maxdepthwalkable5=depth_walkable5 maxdepthwalkable10=depth_walkable10	
		(mean) ALT_DEM, by(dhsid); 
		
gen pctcontaminated5=(numcontaminated5/numwalkable5)*100;
gen pctcontaminated10=(numcontaminated10/numwalkable10)*100;
label variable pctcontaminated5 "proportion of wells within 5 mi contaminated";
label variable pctcontaminated10 "proportion of wells within 10 mi contaminated";

gen dhsid99=real(substr(dhsid,-3,3));



*collapse to 1-obs per DHS GPS point;

*save "`dataset'small.dta", replace;
 
*gen distance=uniform();
*br dhsid wellcount distance;
*br dhsid count distance;
*sort dhsid count;
*br dhsid count distance;

save "`finaldataset'", replace;




 
