diff --git a/Model input/Input_file_new_KLA_div.csv b/Model input/Input_file_new_KLA_div.csv
new file mode 100644
index 0000000000000000000000000000000000000000..166c3c4f4bffbab1253e61608819f842bfc132b9
--- /dev/null
+++ b/Model input/Input_file_new_KLA_div.csv	
@@ -0,0 +1,6 @@
+iso,subarea,hdi,population,fraction_urban_pop,fraction_pop_under5,sheddingRate,shedding_duration,incidence_urban_under5,incidence_urban_5plus,incidence_rural_under5,incidence_rural_5plus,flushSewer_urb,flushSeptic_urb,flushPit_urb,flushOpen_urb,flushUnknown_urb,pitSlab_urb,pitNoSlab_urb,compostingTwinSlab_urb,compostingTwinNoSlab_urb,compostingToilet_urb,bucketLatrine_urb,containerBased_urb,hangingToilet_urb,openDefecation_urb,other_urb,isShared_urb,sewerLeak_urb,emptied_urb,isWatertight_urb,hasLeach_urb,coverBury_urb,emptiedTreatment_urb,emptyFrequency_urb,pitAdditive_urb,flushElsewhere_urb,onsiteFlushed_urb,onsiteDumpedwater_urb,onsiteDumpedland_urb,pitVIP_urb,pitTraditional_urb,otherLatrine_urb,otherImproved_urb,otherUnimproved_urb,dontKnow_urb,pitLined_urb,pitUnlined_urb,flushSewer_rur,flushSeptic_rur,flushPit_rur,flushOpen_rur,flushUnknown_rur,pitSlab_rur,pitNoSlab_rur,compostingTwinSlab_rur,compostingTwinNoSlab_rur,compostingToilet_rur,bucketLatrine_rur,containerBased_rur,hangingToilet_rur,openDefecation_rur,other_rur,isShared_rur,sewerLeak_rur,emptied_rur,isWatertight_rur,hasLeach_rur,coverBury_rur,emptiedTreatment_rur,emptyFrequency_rur,pitAdditive_rur,flushElsewhere_rur,onsiteFlushed_rur,onsiteDumpedwater_rur,onsiteDumpedland_rur,pitVIP_rur,pitTraditional_rur,otherLatrine_rur,otherImproved_rur,otherUnimproved_rur,dontKnow_rur,pitLined_rur,pitUnlined_rur,FractionPrimarytreatment,FractionSecondarytreatment,FractionTertiarytreatment,FractionQuaternarytreatment,FractionPonds,FractionNontreatment
+1,Central,0.493,63206,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.263426061,0.202395075,0.057237316,0.012221734,0.007753772,0.41097414,0.02447696,0,0,0.006835904,0,0,0,0.011578119,0.003100918,0.363873646,0,0.469944575,0,0.733457483,0,0.436780403,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.23077419,0.211512815,0.263426061,0.202395075,0.057237316,0.012221734,0.007753772,0.41097414,0.02447696,0,0,0.006835904,0,0,0,0.011578119,0.003100918,0.363873646,0,0.469944575,0,0.733457483,0,0.436780403,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.23077419,0.211512815,0.035,0.035,0,0,0.45,0.48
+2,Kawempe,0.493,333024,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.036860622,0.142274476,0.057601288,0.001679583,0.00524847,0.736130931,0.014416174,0,0,0.00033069,0,0,0.0001061,0.00459866,0.000745624,0.439653607,0,0.218648771,0,0.705524168,0,0.211244894,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.321520783,0.429357014,0.036860622,0.142274476,0.057601288,0.001679583,0.00524847,0.736130931,0.014416174,0,0,0.00033069,0,0,0.0001061,0.00459866,0.000745624,0.439653607,0,0.218648771,0,0.705524168,0,0.211244894,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.321520783,0.429357014,0.035,0.035,0,0,0.45,0.48
+3,Makindye,0.493,385309,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.028570368,0.259280693,0.082540034,0.003821692,0.017477095,0.574689697,0.024289707,0,0,0.001400786,0,0,3.45E-05,0.006082428,0.001834099,0.428658959,0,0.248507909,0,0.740031033,0,0.236410709,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.2588795,0.341499759,0.028570368,0.259280693,0.082540034,0.003821692,0.017477095,0.574689697,0.024289707,0,0,0.001400786,0,0,3.45E-05,0.006082428,0.001834099,0.428658959,0,0.248507909,0,0.740031033,0,0.236410709,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.2588795,0.341499759,0.035,0.035,0,0,0.45,0.48
+4,Nakawa,0.493,317023,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.098606697,0.282278627,0.065945258,0.002724668,0.016024352,0.49795729,0.027461236,0,0,0.000559863,0,0,0.000678506,0.006932757,0.000831023,0.389882378,0,0.196882531,0,0.788037462,0,0.190629622,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.215936282,0.310042107,0.098606697,0.282278627,0.065945258,0.002724668,0.016024352,0.49795729,0.027461236,0,0,0.000559863,0,0,0.000678506,0.006932757,0.000831023,0.389882378,0,0.196882531,0,0.788037462,0,0.190629622,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.215936282,0.310042107,0.035,0.035,0,0,0.45,0.48
+5,Rubaga,0.493,383216,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.004654201,0.164638786,0.089541423,0.001799026,0.010791608,0.688829209,0.031009589,0,0,0.000566465,0,0,2.01E-05,0.003735057,0.004392258,0.441673466,0,0.256076888,0,0.98927109,0,0.243857358,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.312687133,0.407713566,0.004654201,0.164638786,0.089541423,0.001799026,0.010791608,0.688829209,0.031009589,0,0,0.000566465,0,0,2.01E-05,0.003735057,0.004392258,0.441673466,0,0.256076888,0,0.98927109,0,0.243857358,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.312687133,0.407713566,0.035,0.035,0,0,0.45,0.48
diff --git a/Model input/KLA/Input_file_new_KLA_div.csv b/Model input/KLA/Input_file_new_KLA_div.csv
index be3ec5b2e7eb5f8ce2989cc7acabd126721a4cc0..166c3c4f4bffbab1253e61608819f842bfc132b9 100644
--- a/Model input/KLA/Input_file_new_KLA_div.csv	
+++ b/Model input/KLA/Input_file_new_KLA_div.csv	
@@ -1,6 +1,6 @@
-iso;subarea;hdi;population;fraction_urban_pop;fraction_pop_under5;sheddingRate;shedding_duration;incidence_urban_under5;incidence_urban_5plus;incidence_rural_under5;incidence_rural_5plus;flushSewer_urb;flushSeptic_urb;flushPit_urb;flushOpen_urb;flushUnknown_urb;pitSlab_urb;pitNoSlab_urb;compostingTwinSlab_urb;compostingTwinNoSlab_urb;compostingToilet_urb;bucketLatrine_urb;containerBased_urb;hangingToilet_urb;openDefecation_urb;other_urb;isShared_urb;sewerLeak_urb;emptied_urb;isWatertight_urb;hasLeach_urb;coverBury_urb;emptiedTreatment_urb;emptyFrequency_urb;pitAdditive_urb;flushElsewhere_urb;pitVIP_urb;pitTraditional_urb;otherLatrine_urb;otherImproved_urb;otherUnimproved_urb;dontKnow_urb;pitLined_urb;pitUnlined_urb;flushSewer_rur;flushSeptic_rur;flushPit_rur;flushOpen_rur;flushUnknown_rur;pitSlab_rur;pitNoSlab_rur;compostingTwinSlab_rur;compostingTwinNoSlab_rur;compostingToilet_rur;bucketLatrine_rur;containerBased_rur;hangingToilet_rur;openDefecation_rur;other_rur;isShared_rur;sewerLeak_rur;emptied_rur;isWatertight_rur;hasLeach_rur;coverBury_rur;emptiedTreatment_rur;emptyFrequency_rur;pitAdditive_rur;flushElsewhere_rur;pitVIP_rur;pitTraditional_rur;otherLatrine_rur;otherImproved_rur;otherUnimproved_rur;dontKnow_rur;pitLined_rur;pitUnlined_rur;FractionPrimarytreatment;FractionSecondarytreatment;FractionTertiarytreatment;FractionQuaternarytreatment;FractionPonds;FractionNontreatment
-1;Central;0.493;63206;1;0.177278887;1.00E+10;7;0.24;0.08;0.24;0.08;0.263426061;0.202395075;0.057237316;0.012221734;0.007753772;0.41097414;0.02447696;0;0;0.006835904;0;0;0;0.011578119;0.003100918;0.363873646;0;0.469944575;0;0.733457483;0;0.436780403;3;None;0;0;0;0;0;0;0;0.23077419;0.211512815;0.263426061;0.202395075;0.057237316;0.012221734;0.007753772;0.41097414;0.02447696;0;0;0.006835904;0;0;0;0.011578119;0.003100918;0.363873646;0;0.469944575;0;0.733457483;0;0.436780403;3;None;0;0;0;0;0;0;0;0.23077419;0.211512815;0.035;0.035;0;0;0.45;0.48
-2;Kawempe;0.493;333024;1;0.177278887;1.00E+10;7;0.24;0.08;0.24;0.08;0.036860622;0.142274476;0.057601288;0.001679583;0.00524847;0.736130931;0.014416174;0;0;0.00033069;0;0;0.0001061;0.00459866;0.000745624;0.439653607;0;0.218648771;0;0.705524168;0;0.211244894;3;None;0;0;0;0;0;0;0;0.321520783;0.429357014;0.036860622;0.142274476;0.057601288;0.001679583;0.00524847;0.736130931;0.014416174;0;0;0.00033069;0;0;0.0001061;0.00459866;0.000745624;0.439653607;0;0.218648771;0;0.705524168;0;0.211244894;3;None;0;0;0;0;0;0;0;0.321520783;0.429357014;0.035;0.035;0;0;0.45;0.48
-3;Makindye;0.493;385309;1;0.177278887;1.00E+10;7;0.24;0.08;0.24;0.08;0.028570368;0.259280693;0.082540034;0.003821692;0.017477095;0.574689697;0.024289707;0;0;0.001400786;0;0;3.45E-05;0.006082428;0.001834099;0.428658959;0;0.248507909;0;0.740031033;0;0.236410709;3;None;0;0;0;0;0;0;0;0.2588795;0.341499759;0.028570368;0.259280693;0.082540034;0.003821692;0.017477095;0.574689697;0.024289707;0;0;0.001400786;0;0;3.45E-05;0.006082428;0.001834099;0.428658959;0;0.248507909;0;0.740031033;0;0.236410709;3;None;0;0;0;0;0;0;0;0.2588795;0.341499759;0.035;0.035;0;0;0.45;0.48
-4;Nakawa;0.493;317023;1;0.177278887;1.00E+10;7;0.24;0.08;0.24;0.08;0.098606697;0.282278627;0.065945258;0.002724668;0.016024352;0.49795729;0.027461236;0;0;0.000559863;0;0;0.000678506;0.006932757;0.000831023;0.389882378;0;0.196882531;0;0.788037462;0;0.190629622;3;None;0;0;0;0;0;0;0;0.215936282;0.310042107;0.098606697;0.282278627;0.065945258;0.002724668;0.016024352;0.49795729;0.027461236;0;0;0.000559863;0;0;0.000678506;0.006932757;0.000831023;0.389882378;0;0.196882531;0;0.788037462;0;0.190629622;3;None;0;0;0;0;0;0;0;0.215936282;0.310042107;0.035;0.035;0;0;0.45;0.48
-5;Rubaga;0.493;383216;1;0.177278887;1.00E+10;7;0.24;0.08;0.24;0.08;0.004654201;0.164638786;0.089541423;0.001799026;0.010791608;0.688829209;0.031009589;0;0;0.000566465;0;0;2.01E-05;0.003735057;0.004392258;0.441673466;0;0.256076888;0;0.98927109;0;0.243857358;3;None;0;0;0;0;0;0;0;0.312687133;0.407713566;0.004654201;0.164638786;0.089541423;0.001799026;0.010791608;0.688829209;0.031009589;0;0;0.000566465;0;0;2.01E-05;0.003735057;0.004392258;0.441673466;0;0.256076888;0;0.98927109;0;0.243857358;3;None;0;0;0;0;0;0;0;0.312687133;0.407713566;0.035;0.035;0;0;0.45;0.48
+iso,subarea,hdi,population,fraction_urban_pop,fraction_pop_under5,sheddingRate,shedding_duration,incidence_urban_under5,incidence_urban_5plus,incidence_rural_under5,incidence_rural_5plus,flushSewer_urb,flushSeptic_urb,flushPit_urb,flushOpen_urb,flushUnknown_urb,pitSlab_urb,pitNoSlab_urb,compostingTwinSlab_urb,compostingTwinNoSlab_urb,compostingToilet_urb,bucketLatrine_urb,containerBased_urb,hangingToilet_urb,openDefecation_urb,other_urb,isShared_urb,sewerLeak_urb,emptied_urb,isWatertight_urb,hasLeach_urb,coverBury_urb,emptiedTreatment_urb,emptyFrequency_urb,pitAdditive_urb,flushElsewhere_urb,onsiteFlushed_urb,onsiteDumpedwater_urb,onsiteDumpedland_urb,pitVIP_urb,pitTraditional_urb,otherLatrine_urb,otherImproved_urb,otherUnimproved_urb,dontKnow_urb,pitLined_urb,pitUnlined_urb,flushSewer_rur,flushSeptic_rur,flushPit_rur,flushOpen_rur,flushUnknown_rur,pitSlab_rur,pitNoSlab_rur,compostingTwinSlab_rur,compostingTwinNoSlab_rur,compostingToilet_rur,bucketLatrine_rur,containerBased_rur,hangingToilet_rur,openDefecation_rur,other_rur,isShared_rur,sewerLeak_rur,emptied_rur,isWatertight_rur,hasLeach_rur,coverBury_rur,emptiedTreatment_rur,emptyFrequency_rur,pitAdditive_rur,flushElsewhere_rur,onsiteFlushed_rur,onsiteDumpedwater_rur,onsiteDumpedland_rur,pitVIP_rur,pitTraditional_rur,otherLatrine_rur,otherImproved_rur,otherUnimproved_rur,dontKnow_rur,pitLined_rur,pitUnlined_rur,FractionPrimarytreatment,FractionSecondarytreatment,FractionTertiarytreatment,FractionQuaternarytreatment,FractionPonds,FractionNontreatment
+1,Central,0.493,63206,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.263426061,0.202395075,0.057237316,0.012221734,0.007753772,0.41097414,0.02447696,0,0,0.006835904,0,0,0,0.011578119,0.003100918,0.363873646,0,0.469944575,0,0.733457483,0,0.436780403,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.23077419,0.211512815,0.263426061,0.202395075,0.057237316,0.012221734,0.007753772,0.41097414,0.02447696,0,0,0.006835904,0,0,0,0.011578119,0.003100918,0.363873646,0,0.469944575,0,0.733457483,0,0.436780403,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.23077419,0.211512815,0.035,0.035,0,0,0.45,0.48
+2,Kawempe,0.493,333024,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.036860622,0.142274476,0.057601288,0.001679583,0.00524847,0.736130931,0.014416174,0,0,0.00033069,0,0,0.0001061,0.00459866,0.000745624,0.439653607,0,0.218648771,0,0.705524168,0,0.211244894,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.321520783,0.429357014,0.036860622,0.142274476,0.057601288,0.001679583,0.00524847,0.736130931,0.014416174,0,0,0.00033069,0,0,0.0001061,0.00459866,0.000745624,0.439653607,0,0.218648771,0,0.705524168,0,0.211244894,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.321520783,0.429357014,0.035,0.035,0,0,0.45,0.48
+3,Makindye,0.493,385309,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.028570368,0.259280693,0.082540034,0.003821692,0.017477095,0.574689697,0.024289707,0,0,0.001400786,0,0,3.45E-05,0.006082428,0.001834099,0.428658959,0,0.248507909,0,0.740031033,0,0.236410709,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.2588795,0.341499759,0.028570368,0.259280693,0.082540034,0.003821692,0.017477095,0.574689697,0.024289707,0,0,0.001400786,0,0,3.45E-05,0.006082428,0.001834099,0.428658959,0,0.248507909,0,0.740031033,0,0.236410709,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.2588795,0.341499759,0.035,0.035,0,0,0.45,0.48
+4,Nakawa,0.493,317023,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.098606697,0.282278627,0.065945258,0.002724668,0.016024352,0.49795729,0.027461236,0,0,0.000559863,0,0,0.000678506,0.006932757,0.000831023,0.389882378,0,0.196882531,0,0.788037462,0,0.190629622,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.215936282,0.310042107,0.098606697,0.282278627,0.065945258,0.002724668,0.016024352,0.49795729,0.027461236,0,0,0.000559863,0,0,0.000678506,0.006932757,0.000831023,0.389882378,0,0.196882531,0,0.788037462,0,0.190629622,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.215936282,0.310042107,0.035,0.035,0,0,0.45,0.48
+5,Rubaga,0.493,383216,1,0.177278887,1.00E+10,7,0.24,0.08,0.24,0.08,0.004654201,0.164638786,0.089541423,0.001799026,0.010791608,0.688829209,0.031009589,0,0,0.000566465,0,0,2.01E-05,0.003735057,0.004392258,0.441673466,0,0.256076888,0,0.98927109,0,0.243857358,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.312687133,0.407713566,0.004654201,0.164638786,0.089541423,0.001799026,0.010791608,0.688829209,0.031009589,0,0,0.000566465,0,0,2.01E-05,0.003735057,0.004392258,0.441673466,0,0.256076888,0,0.98927109,0,0.243857358,3,None,0,0.3333333,0.333333,0.333334,0,0,0,0,0,0,0.312687133,0.407713566,0.035,0.035,0,0,0.45,0.48
diff --git a/Model input/isoraster_KLA_div.tif b/Model input/isoraster_KLA_div.tif
new file mode 100644
index 0000000000000000000000000000000000000000..9bfddf8a4f4e4bbcf63ba1aea19eb264830b54e6
Binary files /dev/null and b/Model input/isoraster_KLA_div.tif differ
diff --git a/Model input/kla_onsite.csv b/Model input/kla_onsite.csv
new file mode 100644
index 0000000000000000000000000000000000000000..06e9793c10991015bdec9decaafc795d04906c08
--- /dev/null
+++ b/Model input/kla_onsite.csv	
@@ -0,0 +1,6 @@
+"","iso","scenario","region","population","sheddingRate","prevalence","flushSewer","flushSeptic","flushPit","flushOpen","flushUnknown","pitSlab","pitNoSlab","compostingTwinSlab","compostingTwinNoSlab","compostingToilet","bucketLatrine","containerBased","hangingToilet","openDefecation","other","isShared","sewerLeak","emptied","isWatertight","hasLeach","coverBury","emptiedTreatment","emptyFrequency","pitAdditive","flushElsewhere","pitVIP","pitTraditional","otherLatrine","otherImproved","otherUnimproved","dontKnow","pitLined","pitUnlined"
+"1",1,NA,"Central",11205.089331722,1e+10,1.68e+10,0.263426061,0.202395075,0.057237316,0.012221734,0.007753772,0.41097414,0.02447696,0,0,0.006835904,0,0,0,0.011578119,0.003100918,0.363873646,0,0.469944575,0,0.733457483,0,0.436780403,3,"None",0,0,0,0,0,0,0,0.23077419,0.211512815
+"2",2,NA,"Kawempe",59038.124064288,1e+10,1.68e+10,0.036860622,0.142274476,0.057601288,0.001679583,0.00524847,0.736130931,0.014416174,0,0,0.00033069,0,0,0.0001061,0.00459866,0.000745624,0.439653607,0,0.218648771,0,0.705524168,0,0.211244894,3,"None",0,0,0,0,0,0,0,0.321520783,0.429357014
+"3",3,NA,"Makindye",68307.150671083,1e+10,1.68e+10,0.028570368,0.259280693,0.082540034,0.003821692,0.017477095,0.574689697,0.024289707,0,0,0.001400786,0,0,3.45e-05,0.006082428,0.001834099,0.428658959,0,0.248507909,0,0.740031033,0,0.236410709,3,"None",0,0,0,0,0,0,0,0.2588795,0.341499759
+"4",4,NA,"Nakawa",56201.484593401,1e+10,1.68e+10,0.098606697,0.282278627,0.065945258,0.002724668,0.016024352,0.49795729,0.027461236,0,0,0.000559863,0,0,0.000678506,0.006932757,0.000831023,0.389882378,0,0.196882531,0,0.788037462,0,0.190629622,3,"None",0,0,0,0,0,0,0,0.215936282,0.310042107
+"5",5,NA,"Rubaga",67936.105960592,1e+10,1.68e+10,0.004654201,0.164638786,0.089541423,0.001799026,0.010791608,0.688829209,0.031009589,0,0,0.000566465,0,0,2.01e-05,0.003735057,0.004392258,0.441673466,0,0.256076888,0,0.98927109,0,0.243857358,3,"None",0,0,0,0,0,0,0,0.312687133,0.407713566
diff --git a/Model input/pathogen_inputs.csv b/Model input/pathogen_inputs.csv
index 897ed62a550c6ae23f574c32db37d73eccfb578d..67f8b70887a8d71e1f741ff22d3c858bd0e49d63 100644
--- a/Model input/pathogen_inputs.csv	
+++ b/Model input/pathogen_inputs.csv	
@@ -1,3 +1,3 @@
-name,incidence_lowhdi_under5,incidence_highhdi_under5,incidence_lowhdi_over5,incidence_highhdi_over5,episodelength,shedding_pp_pd,RemovalPrimary,RemovalSecondary,RemovalTertiary,RemovalQuaternary,RemovalPonds,Kpit,Kt,TatKt,lambda,kd,kl,v_settling,retention_lower,retention_upper
-cryptosporidium,1.00E-01,5.00E-02,1.00E-01,5.00E-02,1,1.00E+09,0.1,0.55,0.9775,0.99,0.95,0.047,5.10E-03,4,1.58E-01,9.831,0.0004798,0.1,3.2,8.8
-rotavirus,0.24,0.08,0.01,0.01,1,1.00E+10,0.2,0.975,0.9921,0.999,0.95,0.13,NA,NA,NA,NA,NA,NA,NA,NA
+name,pathogenType,incidence_lowhdi_under5,incidence_highhdi_under5,incidence_lowhdi_over5,incidence_highhdi_over5,episodelength,shedding_pp_pd,RemovalPrimary,RemovalSecondary,RemovalTertiary,RemovalQuaternary,RemovalPonds,Runoff_Fraction,Kpit,Kt,Kt_tot,TatKt,Tcoeff_1,Tcoeff_2,Kr_tot,lambda,kd,ke,kl,zeu,Rcoeff_1,v_settling,retention_lower,retention_upper
+cryptosporidium,Protozoa,0.1,0.05,0.1,0.05,1,1.00E+09,0.1,0.55,0.9775,0.99,0.95,0.025,0.047,0.0051,NA,4,NA,NA,NA,0.158,9.831,NA,0.0004798,NA,NA,0.1,3.2,8.8
+rotavirus,Virus,0.24,0.08,0.01,0.01,7,1.00E+10,0.2,0.975,0.9921,0.999,0.95,0.025,0.13,NA,1,NA,0.008902808,0.039728754,1,NA,NA,1,0.0000471,1,4.605170186,NA,0.456925765,3.096910013
diff --git a/Model scripts/GloWPa configuration.R b/Model scripts/GloWPa configuration.R
index e48ed3efc472b5125465b6cc2f5279ee30458b06..12032b52291ca6cc8000b53a00e1ed0512c246ec 100644
--- a/Model scripts/GloWPa configuration.R	
+++ b/Model scripts/GloWPa configuration.R	
@@ -18,54 +18,97 @@ MONS <<- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","de
 # FUNCTIONS ---------------------------------------------------------------
 
 
-model.start <- function(input,loadings_module,scenario_mode){
+model.start <- function(config,loadings_module,tool_mode){
   # load functions from scripts
-
-  source(file.path(working.path.script,"Human_emissions.R"))
-
-  if(loadings_module==2){
-    source(file.path(working.path.in,"Pathogenflows.R"))
-  }
-  if(scenario_mode){
+  if(!tool_mode){
     # run scenarios with original human emissions
     # run.scenarios will call run() for each scenario
-    run.scenarios(input,loadings_module)
+    source(file.path(working.path.script,"Human_emissions.R"))
+    
+    run.original.mode(config)
   }
   else{
     # online tool with pathogenflows module
-    # TODO: @Nynke: How to handle isoraster in this case?
-    isoraster = "?" # @Nynke where do the isorasters come from in case of the online tool?
-    pathogen = "?" # @Nynke where is the pathogen information in case of the online tool?
-    run(input,pathogen,isoraster,loadings_module)
+    # run one row, because there is only one
+    run.mapping.tool(config[1,])
+   
   }
 }
 
+# this will the plumber api method (POST). scenario data can be retrieved from mapping_tool_init
+run.mapping.tool <- function(scenario,human_data_csv=NULL){
+  # TODO: @Nynke: How to handle isoraster in this case? 
+  isoraster<<-raster(file.path(working.path.in,scenario$isoraster_filename))
+  pathogen_inputs <- read.csv(file.path(working.path.in.pathogens,"pathogen_inputs.csv"),stringsAsFactors=FALSE)
+  # search pathogen information
+  pathogen_row <- which(pathogen_inputs$name==scenario$pathogen)
+  if(length(pathogen_row)<1){
+    stop("Error: pathogen misspelled or not in list")
+  }
+  pathogen <- pathogen_inputs[pathogen_row,]
+  if(is.null(human_data_csv)){
+    human_data <- read.csv(file.path(working.path.in,scenario$HumanData_filename))
+  }
+  else{
+    human_data <- read.csv(text = human_data_csv)
+  }
+  model.run(scenario, human_data,pathogen,isoraster,loadings_module = 2)
+}
 
-model.run <- function(input,pathogen,isoraster,loadings_module,run_id=NULL){
+model.run <- function(scenario,human_data,pathogen,isoraster,loadings_module){
+  # 0a) set globals
+  SCENARIO <<- scenario
+  HUMAN_DATA <<- human_data
+  ISORASTER <<- isoraster
+  AREA_EXTENT <<- extent(isoraster)
+  # 0b) correct population data
+  source("Model scripts/population.R")
+  populations <- population.preprocess()
   # 1) calculate loadings
   if(loadings_module==1){
-    loadings <- human.emissions.model.run(input,pathogen,isoraster)
+    # input is the scenario. human data is read in the human.emissions.model.run
+    emissions <- human.emissions.model.run(human_data,pathogen,isoraster)
   }
   else if(loadings_module==2){
     
     source(file.path(working.path.script,"Pathogenflows.R"))
     # online tool input != onsitedata. Extract urban/rural data in pathogenflows scripts
-    loadings <- pathogenflow.model.run(input,pathogen)
-
-    ## @Nynke: wwtp and rasterization also in human_emissions.R. It would be nice if we can use same functions for it, but the input format (column names) is different
-    ## in mapping tool input and Human_Data.csv.  
-    # 2) apply wwtp if available
-    
-    # 3) emissions -> raster
+    emissions <- pathogenflow.model.run(pathogen)
   }
   
-
-  
-  # 4) maps, figs
-  
-  # 5) hydrology
-  
-  # 6) concentrations
+  ## @Nynke: wwtp and rasterization also in human_emissions.R. It would be nice if we can use same functions for it, but the input format (column names) is different
+  ## in mapping tool input and Human_Data.csv.  
+  # 2) apply wwtp if available
+  source(file.path(working.path.script,"wwtp_module.R"))
+  wttp_output <- wwtp.module.run(emissions,pathogen,popurban_grid = popurban_grid,poprural_grid = poprural_grid)
+  emissions <- wttp_output$emissions
+  # 3) emissions -> raster
+  if(SCENARIO$hydrology_available){
+    
+  }
+  else{
+    #In case no hydrology is available, we assume a 0.025 fraction reaching the water. This is high for many regions!
+    
+    pathogen_land_grid<-0.025*wttp_output$grids$land
+    pathogen_water_grid<-wttp_output$grids$water+pathogen_land_grid
+    emissions$pathogen_urb_dif<-0.025*emissions$pathogen_urb_dif
+    emissions$pathogen_rur_dif<-0.025*emissions$pathogen_rur_dif
+    emissions$pathogen_urb_onsite_land<-0.025*emissions$pathogen_urb_onsite_land
+    emissions$pathogen_rur_onsite_land<-0.025*emissions$pathogen_rur_onsite_land
+    # save is asci grid format
+    writeRaster(pathogen_water_grid,file.path(working.path.out,sprintf("humanemissions_%s%s",pathogen$name,SCENARIO$run)), format="ascii", overwrite=TRUE)
+    
+  }
+  # 4) save results
+  # save geotiff
+  pathogen_water_grid[pathogen_water_grid==0]<-NA
+  writeRaster(pathogen_water_grid,file.path(working.path.out,sprintf("humanemissions_%s%s",pathogen$name,SCENARIO$run)),format="GTiff",overwrite=TRUE)
+  library(tidyverse)
+  library(dplyr)
+  selection <- point %>% select(1:16)
+  write.csv(selection, file=file.path(working.path.out,sprintf("HumanEmissionsCalculated_%s%s.csv",pathogen$name,SCENARIO$run)))
+  detach("package:tidyverse", unload=TRUE)
+  detach("package:dplyr", unload=TRUE)
 }
 
 
@@ -78,7 +121,7 @@ model.run <- function(input,pathogen,isoraster,loadings_module,run_id=NULL){
 #' @export
 #'
 #' @examples
-run.scenarios <- function(config, iso_read_format){
+run.original.mode <- function(config, iso_read_format=1){
   nruns <-length(config$run)
   #read in the pathogen specific input data
   pathogen_inputs <- read.csv(file.path(working.path.in.pathogens,"pathogen_inputs.csv"),stringsAsFactors=FALSE)
diff --git a/Model scripts/Human_emissions.r b/Model scripts/Human_emissions.r
index e0e4712c420203c2baa4e9fb8ddc266a75c2f643..dace0eaf453376f5ed3d0ae911d5f2d918029de1 100644
--- a/Model scripts/Human_emissions.r	
+++ b/Model scripts/Human_emissions.r	
@@ -24,9 +24,9 @@ human.emissions.model.run <- function(scenario,pathogen,isoraster){
   #calculate or provide correct variables to use in model for pathogen
   #calculate extretion/shedding rates per person
   ExcretionLowHDI_child<<-pathogen$incidence_lowhdi_under5*pathogen$shedding_pp_pd*pathogen$episodelength  #episodes per person per year * excretion per day * days per episode
-  ExcretionHighHDI_child<<-pathogen$incidence_highhdi_under5*pathogen$shedding_pp_pd*pathogen$episodelength
+  excretion_child<<-pathogen$incidence_highhdi_under5*pathogen$shedding_pp_pd*pathogen$episodelength
   ExcretionLowHDI_others<<-pathogen$incidence_lowhdi_over5*pathogen$shedding_pp_pd*pathogen$episodelength
-  ExcretionHighHDI_others<<-pathogen$incidence_highhdi_over5*pathogen$shedding_pp_pd*pathogen$episodelength
+  excretion_others<<-pathogen$incidence_highhdi_over5*pathogen$shedding_pp_pd*pathogen$episodelength
   
   area_extent <-extent(isoraster) #this extent covers the selected area. this extent can later be used to crop data (e.g. population)
   # READ HUMAN & WWTP
@@ -82,7 +82,7 @@ human.emissions.model.run <- function(scenario,pathogen,isoraster){
   # CALCULATION OF EMISSIONS PER SANITION TYPE AND SUBAREA
   # Be aware: in case we have WWTP location data, the reduction in those has not yet been added
   # to the emissions. That is why after this section the point dataframe cannot yet be saved as file.
-  emissions <- calc.emissions(scenario,pathogen,HumanData)
+  emissions <- get.emissions(scenario,pathogen,HumanData)
   emissions <- calc.pop.emissions(emissions,HumanData)
   # clean na values
   emissions <- remove.na.emissions(emissions)
@@ -181,7 +181,7 @@ correct.population <- function(pop_urban_grid,pop_rulal_grid,isoraster,HumanData
 #' @export
 #'
 #' @examples
-calc.emissions <- function(scenario,pathogen,HumanData){
+get.emissions <- function(scenario,pathogen,HumanData){
   
   #fill all the added columns
   HumanData$removalfraction <- 
@@ -234,118 +234,76 @@ calc.emissions <- function(scenario,pathogen,HumanData){
   HumanData$survival_pit_flush_rur<-NA
   
   #for HDI high
-  if(length(b)>0){
-    for (j in 1:length(HumanData$iso[b])){ #calculate per subarea
+  output <- calc.emissions(HumanData,point,b,ExcretionHighHDI_child, ExcretionHighHDI_others)
+  HumanData <- output$human
+  point<- output$emissions
+
+  #for HDI low
+  output <- calc.emissions(HumanData,point,a,ExcretionLowHDI_child, ExcretionHighHDI_others)
+  HumanData <- output$human
+  point<- output$emissions
+  
+  return(point)
+}
+
+calc.emissions <- function(HumanData, point, subset ,excretion_child, excretion_others){
+  if(length(subset>0)){
+    for (j in 1:length(HumanData$iso[subset])){ #calculate per subarea
       #calculate decay in onsite systems
-      int<-integrate(f2,0,HumanData$storage_empty_urb[b[j]])
-      HumanData$survival_pit_empty_urb[b[j]]<-int$value/HumanData$storage_empty_urb[b[j]]
-      int<-integrate(f2,0,HumanData$storage_empty_rur[b[j]])
-      HumanData$survival_pit_empty_rur[b[j]]<-int$value/HumanData$storage_empty_rur[b[j]]
+      int<-integrate(f2,0,HumanData$storage_empty_urb[subset[j]])
+      HumanData$survival_pit_empty_urb[subset[j]]<-int$value/HumanData$storage_empty_urb[subset[j]]
+      int<-integrate(f2,0,HumanData$storage_empty_rur[subset[j]])
+      HumanData$survival_pit_empty_rur[subset[j]]<-int$value/HumanData$storage_empty_rur[subset[j]]
       
-      int1<-integrate(f2,0,HumanData$storage_flush_urb[b[j]])
-      HumanData$survival_pit_flush_urb[b[j]]<-int1$value/HumanData$storage_flush_urb[b[j]]
+      int1<-integrate(f2,0,HumanData$storage_flush_urb[subset[j]])
+      HumanData$survival_pit_flush_urb[subset[j]]<-int1$value/HumanData$storage_flush_urb[subset[j]]
       
-      int1<-integrate(f2,0,HumanData$storage_flush_rur[b[j]])
-      HumanData$survival_pit_flush_rur[b[j]]<-int1$value/HumanData$storage_flush_rur[b[j]]
+      int1<-integrate(f2,0,HumanData$storage_flush_rur[subset[j]])
+      HumanData$survival_pit_flush_rur[subset[j]]<-int1$value/HumanData$storage_flush_rur[subset[j]]
       
       #calculate the emissions by the specific population groups
       #in case people are interested in children/adults, these number can be split into <5 and >5, but this complicates the model
-      point$pathogen_urb_con[b[j]]<-HumanData$con_urb[b[j]]*(ExcretionHighHDI_child*NappyCor*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])
-      point$pathogen_rur_con[b[j]]<-HumanData$con_rur[b[j]]*(ExcretionHighHDI_child*NappyCor*HumanData$pop_rur_under5[b[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[b[j]])
-      point$pathogen_urb_dir[b[j]]<-HumanData$dir_urb[b[j]]*UrbanDirect2Water*(ExcretionHighHDI_child*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])
-      point$pathogen_rur_dir[b[j]]<-HumanData$dir_rur[b[j]]*RuralDirect2Water*(ExcretionHighHDI_child*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])
-      point$pathogen_urb_dif[b[j]]<-HumanData$dif_urb[b[j]]*UrbanDiffuse2Water*(ExcretionHighHDI_child*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])
-      point$pathogen_rur_dif[b[j]]<-HumanData$dif_rur[b[j]]*RuralDiffuse2Water*(ExcretionHighHDI_child*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])
-      point$pathogen_urb_onsite_treated[b[j]]<-HumanData$onsite_urb_treated[b[j]]*HumanData$survival_pit_empty_urb[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])/HumanData$storage_empty_urb[b[j]]
-      point$pathogen_rur_onsite_treated[b[j]]<-HumanData$onsite_rur_treated[b[j]]*HumanData$survival_pit_empty_rur[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])/HumanData$storage_empty_rur[b[j]]
-      point$pathogen_urb_onsite_water[b[j]]<-HumanData$onsite_urb_water[b[j]]*HumanData$survival_pit_empty_urb[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])/HumanData$storage_empty_urb[b[j]]
-      point$pathogen_rur_onsite_water[b[j]]<-HumanData$onsite_rur_water[b[j]]*HumanData$survival_pit_empty_rur[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])/HumanData$storage_empty_rur[b[j]]
-      point$pathogen_urb_onsite_land[b[j]]<-HumanData$onsite_urb_land[b[j]]*HumanData$survival_pit_empty_urb[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])/HumanData$storage_empty_urb[b[j]]
-      point$pathogen_rur_onsite_land[b[j]]<-HumanData$onsite_rur_land[b[j]]*HumanData$survival_pit_empty_rur[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])/HumanData$storage_empty_rur[b[j]]
-      point$pathogen_urb_onsite_flush[b[j]]<-HumanData$onsite_urb_flush[b[j]]*HumanData$survival_pit_flush_urb[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_urb_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_urb_over5[b[j]])/HumanData$storage_flush_urb[b[j]]
-      point$pathogen_rur_onsite_flush[b[j]]<-HumanData$onsite_rur_flush[b[j]]*HumanData$survival_pit_flush_rur[b[j]]*UrbanOnsite2Water*(ExcretionHighHDI_child*NappyCor*HumanData$pop_rur_under5[b[j]]+ExcretionHighHDI_others*HumanData$pop_rur_over5[b[j]])/HumanData$storage_flush_rur[b[j]]
+      point$pathogen_urb_con[subset[j]]<-HumanData$con_urb[subset[j]]*(excretion_child*NappyCor*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])
+      point$pathogen_rur_con[subset[j]]<-HumanData$con_rur[subset[j]]*(excretion_child*NappyCor*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])
+      point$pathogen_urb_dir[subset[j]]<-HumanData$dir_urb[subset[j]]*UrbanDirect2Water*(excretion_child*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])
+      point$pathogen_rur_dir[subset[j]]<-HumanData$dir_rur[subset[j]]*RuralDirect2Water*(excretion_child*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])
+      point$pathogen_urb_dif[subset[j]]<-HumanData$dif_urb[subset[j]]*UrbanDiffuse2Water*(excretion_child*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])
+      point$pathogen_rur_dif[subset[j]]<-HumanData$dif_rur[subset[j]]*RuralDiffuse2Water*(excretion_child*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])
+      point$pathogen_urb_onsite_treated[subset[j]]<-HumanData$onsite_urb_treated[subset[j]]*HumanData$survival_pit_empty_urb[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])/HumanData$storage_empty_urb[subset[j]]
+      point$pathogen_rur_onsite_treated[subset[j]]<-HumanData$onsite_rur_treated[subset[j]]*HumanData$survival_pit_empty_rur[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])/HumanData$storage_empty_rur[subset[j]]
+      point$pathogen_urb_onsite_water[subset[j]]<-HumanData$onsite_urb_water[subset[j]]*HumanData$survival_pit_empty_urb[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])/HumanData$storage_empty_urb[subset[j]]
+      point$pathogen_rur_onsite_water[subset[j]]<-HumanData$onsite_rur_water[subset[j]]*HumanData$survival_pit_empty_rur[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])/HumanData$storage_empty_rur[subset[j]]
+      point$pathogen_urb_onsite_land[subset[j]]<-HumanData$onsite_urb_land[subset[j]]*HumanData$survival_pit_empty_urb[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])/HumanData$storage_empty_urb[subset[j]]
+      point$pathogen_rur_onsite_land[subset[j]]<-HumanData$onsite_rur_land[subset[j]]*HumanData$survival_pit_empty_rur[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])/HumanData$storage_empty_rur[subset[j]]
+      point$pathogen_urb_onsite_flush[subset[j]]<-HumanData$onsite_urb_flush[subset[j]]*HumanData$survival_pit_flush_urb[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_urb_under5[subset[j]]+excretion_others*HumanData$pop_urb_over5[subset[j]])/HumanData$storage_flush_urb[subset[j]]
+      point$pathogen_rur_onsite_flush[subset[j]]<-HumanData$onsite_rur_flush[subset[j]]*HumanData$survival_pit_flush_rur[subset[j]]*UrbanOnsite2Water*(excretion_child*NappyCor*HumanData$pop_rur_under5[subset[j]]+excretion_others*HumanData$pop_rur_over5[subset[j]])/HumanData$storage_flush_rur[subset[j]]
       
       if(scenario$wwtp_available==3){
         
         #In this case all emissions from the connected population are not yet included in the emissions to water. These are added to the grids in the next step. Treatment has not been applied yet.
-        point$pathogen_urb_conforgrid[b[j]]<-point$pathogen_urb_con[b[j]]+point$pathogen_urb_onsite_treated[b[j]]
-        point$pathogen_rur_conforgrid[b[j]]<-point$pathogen_rur_con[b[j]]+point$pathogen_rur_onsite_treated[b[j]]
-        point$pathogen_urb_waterforgrid[b[j]]<-point$pathogen_urb_dir[b[j]]+point$pathogen_urb_onsite_water[b[j]]+point$pathogen_urb_onsite_flush[b[j]]
-        point$pathogen_rur_waterforgrid[b[j]]<-point$pathogen_rur_dir[b[j]]+point$pathogen_rur_onsite_water[b[j]]+point$pathogen_rur_onsite_flush[b[j]]
-        point$pathogen_urb_landforgrid[b[j]]<-point$pathogen_urb_dif[b[j]]+point$pathogen_urb_onsite_land[b[j]]
-        point$pathogen_rur_landforgrid[b[j]]<-point$pathogen_rur_dif[b[j]]+point$pathogen_rur_onsite_land[b[j]]
+        point$pathogen_urb_conforgrid[subset[j]]<-point$pathogen_urb_con[subset[j]]+point$pathogen_urb_onsite_treated[subset[j]]
+        point$pathogen_rur_conforgrid[subset[j]]<-point$pathogen_rur_con[subset[j]]+point$pathogen_rur_onsite_treated[subset[j]]
+        point$pathogen_urb_waterforgrid[subset[j]]<-point$pathogen_urb_dir[subset[j]]+point$pathogen_urb_onsite_water[subset[j]]+point$pathogen_urb_onsite_flush[subset[j]]
+        point$pathogen_rur_waterforgrid[subset[j]]<-point$pathogen_rur_dir[subset[j]]+point$pathogen_rur_onsite_water[subset[j]]+point$pathogen_rur_onsite_flush[subset[j]]
+        point$pathogen_urb_landforgrid[subset[j]]<-point$pathogen_urb_dif[subset[j]]+point$pathogen_urb_onsite_land[subset[j]]
+        point$pathogen_rur_landforgrid[subset[j]]<-point$pathogen_rur_dif[subset[j]]+point$pathogen_rur_onsite_land[subset[j]]
         
       } else{
         
         #In this case all emissions from the connected population go to the water after treatment.
-        point$pathogen_urb_conforgrid[b[j]]<-NA
-        point$pathogen_rur_conforgrid[b[j]]<-NA
-        point$pathogen_urb_waterforgrid[b[j]]<-point$pathogen_urb_con[b[j]]*HumanData$removalfraction[b[j]]+point$pathogen_urb_dir[b[j]]+point$pathogen_urb_onsite_treated[b[j]]*HumanData$removalfraction[b[j]]+point$pathogen_urb_onsite_water[b[j]]+point$pathogen_urb_onsite_flush[b[j]]
-        point$pathogen_rur_waterforgrid[b[j]]<-point$pathogen_rur_con[b[j]]*HumanData$removalfraction[b[j]]+point$pathogen_rur_dir[b[j]]+point$pathogen_rur_onsite_treated[b[j]]*HumanData$removalfraction[b[j]]+point$pathogen_rur_onsite_water[b[j]]+point$pathogen_rur_onsite_flush[b[j]]
-        point$pathogen_urb_landforgrid[b[j]]<-point$pathogen_urb_dif[b[j]]+point$pathogen_urb_onsite_land[b[j]]
-        point$pathogen_rur_landforgrid[b[j]]<-point$pathogen_rur_dif[b[j]]+point$pathogen_rur_onsite_land[b[j]]
+        point$pathogen_urb_conforgrid[subset[j]]<-NA
+        point$pathogen_rur_conforgrid[subset[j]]<-NA
+        point$pathogen_urb_waterforgrid[subset[j]]<-point$pathogen_urb_con[subset[j]]*HumanData$removalfraction[subset[j]]+point$pathogen_urb_dir[subset[j]]+point$pathogen_urb_onsite_treated[subset[j]]*HumanData$removalfraction[subset[j]]+point$pathogen_urb_onsite_water[subset[j]]+point$pathogen_urb_onsite_flush[subset[j]]
+        point$pathogen_rur_waterforgrid[subset[j]]<-point$pathogen_rur_con[subset[j]]*HumanData$removalfraction[subset[j]]+point$pathogen_rur_dir[subset[j]]+point$pathogen_rur_onsite_treated[subset[j]]*HumanData$removalfraction[subset[j]]+point$pathogen_rur_onsite_water[subset[j]]+point$pathogen_rur_onsite_flush[subset[j]]
+        point$pathogen_urb_landforgrid[subset[j]]<-point$pathogen_urb_dif[subset[j]]+point$pathogen_urb_onsite_land[subset[j]]
+        point$pathogen_rur_landforgrid[subset[j]]<-point$pathogen_rur_dif[subset[j]]+point$pathogen_rur_onsite_land[subset[j]]
         
       }
     }
-    rm(j)
   }
+
   
-  #for HDI low
-  if(length(a)>0){
-    for (j in 1:length(HumanData$iso[a])){
-      int<-integrate(f2,0,HumanData$storage_empty_urb[a[j]])
-      HumanData$survival_pit_empty_urb[a[j]]<-int$value/HumanData$storage_empty_urb[a[j]]
-      int<-integrate(f2,0,HumanData$storage_empty_rur[a[j]])
-      HumanData$survival_pit_empty_rur[a[j]]<-int$value/HumanData$storage_empty_rur[a[j]]
-      
-      int1<-integrate(f2,0,HumanData$storage_flush_urb[a[j]])
-      HumanData$survival_pit_flush_urb[a[j]]<-int1$value/HumanData$storage_flush_urb[a[j]]
-      int1<-integrate(f2,0,HumanData$storage_flush_rur[a[j]])
-      HumanData$survival_pit_flush_rur[a[j]]<-int1$value/HumanData$storage_flush_rur[a[j]]
-      
-      point$pathogen_urb_con[a[j]]<-HumanData$con_urb[a[j]]*(ExcretionLowHDI_child*NappyCor*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])
-      point$pathogen_rur_con[a[j]]<-HumanData$con_rur[a[j]]*(ExcretionLowHDI_child*NappyCor*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])
-      point$pathogen_urb_dir[a[j]]<-HumanData$dir_urb[a[j]]*UrbanDirect2Water*(ExcretionLowHDI_child*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])
-      point$pathogen_rur_dir[a[j]]<-HumanData$dir_rur[a[j]]*RuralDirect2Water*(ExcretionLowHDI_child*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])
-      point$pathogen_urb_dif[a[j]]<-HumanData$dif_urb[a[j]]*UrbanDiffuse2Water*(ExcretionLowHDI_child*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])
-      point$pathogen_rur_dif[a[j]]<-HumanData$dif_rur[a[j]]*RuralDiffuse2Water*(ExcretionLowHDI_child*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])
-      point$pathogen_urb_onsite_treated[a[j]]<-HumanData$onsite_urb_treated[a[j]]*HumanData$survival_pit_empty_urb[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])/HumanData$storage_empty_urb[a[j]]
-      point$pathogen_rur_onsite_treated[a[j]]<-HumanData$onsite_rur_treated[a[j]]*HumanData$survival_pit_empty_rur[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])/HumanData$storage_empty_rur[a[j]]
-      point$pathogen_urb_onsite_water[a[j]]<-HumanData$onsite_urb_water[a[j]]*HumanData$survival_pit_empty_urb[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])/HumanData$storage_empty_urb[a[j]]
-      point$pathogen_rur_onsite_water[a[j]]<-HumanData$onsite_rur_water[a[j]]*HumanData$survival_pit_empty_rur[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])/HumanData$storage_empty_rur[a[j]]
-      point$pathogen_urb_onsite_land[a[j]]<-HumanData$onsite_urb_land[a[j]]*HumanData$survival_pit_empty_urb[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])/HumanData$storage_empty_urb[a[j]]
-      point$pathogen_rur_onsite_land[a[j]]<-HumanData$onsite_rur_land[a[j]]*HumanData$survival_pit_empty_rur[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])/HumanData$storage_empty_rur[a[j]]
-      point$pathogen_urb_onsite_flush[a[j]]<-HumanData$onsite_urb_flush[a[j]]*HumanData$survival_pit_flush_urb[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_urb_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_urb_over5[a[j]])/HumanData$storage_flush_urb[a[j]]
-      point$pathogen_rur_onsite_flush[a[j]]<-HumanData$onsite_rur_flush[a[j]]*HumanData$survival_pit_flush_rur[a[j]]*UrbanOnsite2Water*(ExcretionLowHDI_child*NappyCor*HumanData$pop_rur_under5[a[j]]+ExcretionLowHDI_others*HumanData$pop_rur_over5[a[j]])/HumanData$storage_flush_rur[a[j]]
-      
-      if(scenario$wwtp_available==3){
-        
-        #In this case all emissions from the connected population are not yet included in the emissions to water. These are added to the grids in the next step. Treatment has not been applied yet.
-        point$pathogen_urb_conforgrid[a[j]]<-point$pathogen_urb_con[a[j]]+point$pathogen_urb_onsite_treated[a[j]]
-        point$pathogen_rur_conforgrid[a[j]]<-point$pathogen_rur_con[a[j]]+point$pathogen_rur_onsite_treated[a[j]]
-        point$pathogen_urb_waterforgrid[a[j]]<-point$pathogen_urb_dir[a[j]]+point$pathogen_urb_onsite_water[a[j]]+point$pathogen_urb_onsite_flush[a[j]]
-        point$pathogen_rur_waterforgrid[a[j]]<-point$pathogen_rur_dir[a[j]]+point$pathogen_rur_onsite_water[a[j]]+point$pathogen_rur_onsite_flush[a[j]]
-        point$pathogen_urb_landforgrid[a[j]]<-point$pathogen_urb_dif[a[j]]+point$pathogen_urb_onsite_land[a[j]]
-        point$pathogen_rur_landforgrid[a[j]]<-point$pathogen_rur_dif[a[j]]+point$pathogen_rur_onsite_land[a[j]]
-        
-      }else{
-        
-        #In this case all emissions from the connected population go to the water after treatment.
-        point$pathogen_urb_conforgrid[a[j]]<-NA
-        point$pathogen_rur_conforgrid[a[j]]<-NA
-        point$pathogen_urb_waterforgrid[a[j]]<-point$pathogen_urb_con[a[j]]*HumanData$removalfraction[a[j]]+point$pathogen_urb_dir[a[j]]+point$pathogen_urb_onsite_treated[a[j]]*HumanData$removalfraction[a[j]]+point$pathogen_urb_onsite_water[a[j]]+point$pathogen_urb_onsite_flush[a[j]]
-        point$pathogen_rur_waterforgrid[a[j]]<-point$pathogen_rur_con[a[j]]*HumanData$removalfraction[a[j]]+point$pathogen_rur_dir[a[j]]+point$pathogen_rur_onsite_treated[a[j]]*HumanData$removalfraction[a[j]]+point$pathogen_rur_onsite_water[a[j]]+point$pathogen_rur_onsite_flush[a[j]]
-        point$pathogen_urb_landforgrid[a[j]]<-point$pathogen_urb_dif[a[j]]+point$pathogen_urb_onsite_land[a[j]]
-        point$pathogen_rur_landforgrid[a[j]]<-point$pathogen_rur_dif[a[j]]+point$pathogen_rur_onsite_land[a[j]]
-        
-        #to make sure these fractions are added correctly to the output file
-        point$pathogen_urb_con[a[j]]<-point$pathogen_urb_con[a[j]]*HumanData$removalfraction[a[j]]
-        point$pathogen_rur_con[a[j]]<-point$pathogen_rur_con[a[j]]*HumanData$removalfraction[a[j]]
-        
-      }
-    }
-  }
-  return(point)
+  return(list(human=HumanData, emissions=emissions))
 }
 
 calc.pop.emissions <- function(emissions,HumanData){
@@ -517,3 +475,6 @@ calc.emissions.grid <- function(isoraster,iso,emissions_forgrid_pp,pop_grid){
   return(pathogen_grid)
 }
 
+f <- function(a){
+  a$b <- 10
+}
\ No newline at end of file
diff --git a/Model scripts/Main.R b/Model scripts/Main.R
index 3a6f8078d76db839a222ae54042dd16db2db2a10..47f5f773fe7792dffd558b2662a9e299f0f778b0 100644
--- a/Model scripts/Main.R	
+++ b/Model scripts/Main.R	
@@ -25,12 +25,11 @@ working.path.out<<-paste0(model_path,"Model output/Uganda/")
 # Options 
 #ISO_READ_FORMATS <- list(POLYGON=1,RASTER=2)
 EMISSION_MODULES <- list(NYNKE=1,MATT=2)
-SCENARIO_MODES <- list(ON=T,OFF=F) 
+TOOL_MODES <- list(ON=T,OFF=F) 
 # Set option here
 #iso_read_format <- ISO_READ_FORMATS$POLYGON
 emissions_module <- EMISSION_MODULES$MATT
-scenario_mode <- SCENARIO_MODES$ON
-
+tool_mode <- TOOL_MODES$ON
 #############AFTER THIS LINE THERE IS NO NEED TO CHANGE ANYTHING IN THIS SCRIPT OR OTHER SCRIPTS#####################
 setwd(wd)
 source(file.path(working.path.script,"GloWPa configuration.R"))
@@ -41,15 +40,18 @@ source(file.path(working.path.script,"GloWPa configuration.R"))
 # will have to be run.
 
 # run original GloPWa
-if(scenarion_mode){
+if(tool_mode){
   #read in overall input file
-  input_file<-read.csv(paste0(working.path.in,"overall_inputs.csv"),stringsAsFactors=FALSE)
+  config <- read.csv(paste0(working.path.in,"overall_inputs.csv"),stringsAsFactors=FALSE)
 } else{
   # run GloWPa for mapping tool
-  input_file <- file.path(working.path.in,"input_data.csv")
+  source(file.path(working.path.script,"mapping_tool_init.R"))
+  config <- mapping.tool.get.config()
+  #input_file <- read.csv(file.path(working.path.in,"input_data.csv"))
 }
 
-model.start(overall_inputs,emissions_module,scenario_mode)
+
+model.start(config,emissions_module,scenario_mode)
 
 
 # End to open the overall input files and run selection -------------------
\ No newline at end of file
diff --git a/Model scripts/Pathogenflows.R b/Model scripts/Pathogenflows.R
index 88d067616fc88a74a10f3d7d8c212a6688adcd2d..3beafb2d7e38e6d9a389742257000422f859a3f1 100644
--- a/Model scripts/Pathogenflows.R	
+++ b/Model scripts/Pathogenflows.R	
@@ -1,18 +1,19 @@
 #devtools::install_github('mverbyla/pathogenflows')
 library(pathogenflows)
 
-pathogenflow.model.run <- function(input_data,pathogen){
-  emissions <- data.frame(iso=input_data$iso,subarea=input_data$subarea)
+pathogenflow.model.run <- function(pathogen){
+  emissions <- data.frame(iso=HUMAN_DATA$iso,subarea=HUMAN_DATA$subarea)
   # 1) extract pathogen type from pathogen
-  pathogenType <- "Virus"
+  pathogenType <- pathogen$pathogenType
   area_types <- c("urban","rural")
   human_age_types <- c("child","adult")
+  
   all_loadings <- list()
   # 2) call loadings function 4 times for urban-adult/child prevalence and rural-adult/child prevalence
   for(area_type in area_types){
     for(human_age_type in human_age_types ){
       #2a) prepare data for urban and rural
-      onsite_data <- pathogenflow.model.get.input(input_data,area_type,human_age_type)
+      onsite_data <- pathogenflow.model.get.input(area_type,human_age_type)
       file_out <- file.path(working.path.in,sprintf("onsite_%s_%s.csv",area_type,human_age_type))
       write.csv(onsite_data,file = file_out)
       #call loadings
@@ -35,80 +36,110 @@ pathogenflow.model.run <- function(input_data,pathogen){
       ncols <- ncol(adult[[sub_area]])
       sanitation_types <- adult[[sub_area]]$id
       emissions_sanitation <- adult[[sub_area]][3:ncols] + child[[sub_area]][3:ncols]
-      
+
       # apply to all sanitation types
       for(j in 1:length(sanitation_types)){
         sanitation_type <- sanitation_types[j]
+        # construct column names by sanitation type and rural/urban
         emissions_col_name_postfix <- sprintf("%s_%s",sanitation_type,area_type)
         emissions_col_to_surface <- sprintf("%s_%s","to_surface",emissions_col_name_postfix)
         emissions_col_sewerage <- sprintf("%s_%s","sewerage",emissions_col_name_postfix)
+        emissions_col_in_facal_sludge <-  sprintf("%s_%s","in_fecal_sludge",emissions_col_name_postfix)
         to_surface <- emissions_sanitation$toSurface[j]
         to_sewerage <- emissions_sanitation$sewerage[j]
-        # store only to_surface and sewerage
+        in_fecal_sludge <- emissions_sanitation$In_Fecal_Sludge[j]
+        # store only to_surface, to_sewerage and in_fecal_sludge
         emissions[[emissions_col_to_surface]][i] <- to_surface
         emissions[[emissions_col_sewerage]][i] <- to_sewerage
+        emissions[[emissions_col_in_facal_sludge]] <- in_fecal_sludge
       }
     }
   }
   
   # 3) create similair emissions data.frame as original human emissions
   
+  #In_Fecal_Sludge is the waste from onsite systems going to the WWTP
+  #In_Sewage is the waste from sewerage going to the WWTP
+  #to_Surface is the waste that goes either to the land or to the surface water directly.
+  #
+  #from to_Surface the following goes to water directly: flushSewer, flushSeptic*(1-onsiteDumpedland), 
+  #flushPit*(1-onsiteDumpedland), flushOpen, flushUnknown, pitSlab*(1-onsiteDumpedland), pitNoslab*(1-onsiteDumpedland)
+  #compostingTwinSlab*onsiteDumpedwater, compostingTwinNoSlab*onsiteDumpedwater, compostingToilet*onsiteDumpedwater
+  #bucketLatrine urban, containerBased urban, hangingToilet, openDefecation urban
+  #
+  #from to_Surface the following goes to land: flushSeptic*onsiteDumpedland, flushPit*onsiteDumpedland, 
+  #pitSlab*onsiteDumpedland, compostingTwinSlab*onsiteDumpedland, compostingTwinNoSlab*onsiteDumpedland,
+  #compostingToilet*onsiteDumpedland, bucketLatrine rural, containerBased rural, openDefecation rural
+  #
+  #???Ik weet niet zeker, maar klopt het dat emissions de file is die alle output heeft?
+  #???Ik weet niet hoe je de urbane en rurale waarden apart hebt bedacht...
+  #???Ik weet dus niet zo goed hoe ik onderstaande punten mbv bovenstaande aannames (die nog even
+  #bij Matt liggen ook voor definitieve OK kan opzetten...)
+  
+  # emissions$pathogen_urb_conforgrid_sewer<- 
+  # emissions$pathogen_rur_conforgrid_sewer<-
+  # emissions$pathogen_urb_conforgrid_onsite<-
+  # emissions$pathogen_rur_conforgrid_onsite<-
+  # emissions$pathogen_urb_waterforgrid<-
+  # emissions$pathogen_rur_waterforgrid<-
+  # emissions$pathogen_urb_landforgrid<-
+  # emissions$pathogen_rur_landforgrid<-
+  
   # 4) return emissions
   return(emissions)
 }
 
-pathogenflow.model.get.input <- function(input,area_type,human_type){
-  #@Nynke: population is population per human type (adult/child) or total population?
+pathogenflow.model.get.input <- function(area_type,human_type){
   col_names <- c("iso","scenario","region","population","sheddingRate","prevalence","flushSewer","flushSeptic","flushPit","flushOpen",
                           "flushUnknown","pitSlab","pitNoSlab","compostingTwinSlab","compostingTwinNoSlab","compostingToilet","bucketLatrine",
                           "containerBased","hangingToilet","openDefecation","other","isShared","sewerLeak","emptied","isWatertight","hasLeach","coverBury",	
                           "emptiedTreatment","emptyFrequency","pitAdditive","flushElsewhere","pitVIP","pitTraditional","otherLatrine","otherImproved",
                           "otherUnimproved","dontKnow","pitLined", "pitUnlined") 
-  onsite_data <- data.frame(matrix(ncol=length(col_names),nrow = dim(input)[1]))
+  onsite_data <- data.frame(matrix(ncol=length(col_names),nrow = dim(HUMAN_DATA[1]))
   colnames(onsite_data) <- col_names
-  onsite_data$iso <- input$iso
-  onsite_data$region <- input$subarea
-  onsite_data$sheddingRate <- input$sheddingRate
+  onsite_data$iso <- HUMAN_DATA$iso
+  onsite_data$region <- HUMAN_DATA$subarea
+  onsite_data$sheddingRate <- HUMAN_DATA$sheddingRate
 
-  input_col_names <- colnames(input)
+  input_col_names <- colnames(HUMAN_DATA
   if(area_type=="urban"){
     postfix <- "urb"
-    onsite_data$population <- input$population*input$fraction_urban_pop
+    onsite_data$population <- HUMAN_DATA$population*HUMAN_DATA$fraction_urban_pop
   }
   else if(area_type=="rural"){
     postfix <- "rur"
-    onsite_data$population <- input$population* (1- input$fraction_urban_pop)
+    onsite_data$population <- HUMAN_DATA$population* (1- HUMAN_DATA$fraction_urban_pop)
   }
   # filter for urban and rural
-  filtered_cols <- input_col_names[str_ends(input_col_names,postfix)]
+  filtered_cols <- input_col_names[endsWith(input_col_names,postfix)]
   for(input_colname in filtered_cols){
     # get corresponding colname for onsite_data
-    onsite_colname <- str_replace(input_colname,sprintf("_%s",postfix),"")
+    onsite_colname <- gsub(sprintf("_%s",postfix),"",input_colname)
     # check if it part of onsite_data
     if(onsite_colname %in% col_names){
-      onsite_data[[onsite_colname]] <- input[[input_colname]]
+      onsite_data[[onsite_colname]] <- HUMAN_DATA[[input_colname]]
     }
   }
   incidence = 0
   if(human_type == "child"){
-    onsite_data$population <- input$population * input$fraction_pop_under5
+    onsite_data$population <- HUMAN_DATA$population * HUMAN_DATA$fraction_pop_under5
     if(area_type == "urban"){
-      incidence <- input$incidence_urban_under5
+      incidence <- HUMAN_DATA$incidence_urban_under5
     }
     else if(area_type == "rural"){
-      incidence <- input$incidence_rural_under5 
+      incidence <- HUMAN_DATA$incidence_rural_under5 
     }
   }
   else if(human_type == "adult"){
-    onsite_data$population <- input$population * (1-input$fraction_pop_under5)
+    onsite_data$population <- HUMAN_DATA$population * (1-HUMAN_DATA$fraction_pop_under5)
     if(area_type == "urban"){
-      incidence <- input$incidence_urban_5plus
+      incidence <- HUMAN_DATA$incidence_urban_5plus
     }
     else if(area_type == "rural"){
-      incidence <- input$incidence_rural_5plus 
+      incidence <- HUMAN_DATA$incidence_rural_5plus 
     }
   }
   # @Nynke: Is this ok?
-  onsite_data$prevalence <- incidence*input$sheddingRate*input$shedding_duration
+  onsite_data$prevalence <- incidence*HUMAN_DATA$sheddingRate*HUMAN_DATA$shedding_duration
   return(onsite_data)
 }
\ No newline at end of file
diff --git a/Model scripts/mapping_tool_init.R b/Model scripts/mapping_tool_init.R
new file mode 100644
index 0000000000000000000000000000000000000000..80bf71ce74d45ac457d5c670dc6022af8d639be7
--- /dev/null
+++ b/Model scripts/mapping_tool_init.R	
@@ -0,0 +1,17 @@
+mapping.tool.get.config <- function(){
+  config <- data.frame(run=1)
+  config$subareas <- "districts"
+  config$pathogen <- "cryptosporidium"
+  config$livestock_switch <- FALSE
+  config$incidence_available <- FALSE
+  config$wwtp_available <- 3
+  config$hydrology_available <- FALSE
+  config$interest_concentrations <- FALSE
+  config$resolution <- 0.08333
+  config$isoraster_filename <- "isoraster.asc"
+  config$HumanData_filename <- "input_data.csv"
+  config$WWTPData_filename <- "wwtp_input_data.csv"
+  config$population_urban_filename <- "urban.pop.grd"
+  config$population_rural_filename <- "rural.pop.grd"
+  return(config)
+}
\ No newline at end of file
diff --git a/Model scripts/population.R b/Model scripts/population.R
new file mode 100644
index 0000000000000000000000000000000000000000..c2fe76c8d2d7319de6235c8e40bbca2ffe18c0a1
--- /dev/null
+++ b/Model scripts/population.R	
@@ -0,0 +1,78 @@
+
+population.preprocess <- function(){
+  #read
+  popurban_grid<-raster(file.path(working.path.in,SCENARIO$population_urban_filename))
+  poprural_grid<-raster(file.path(working.path.in,SCENARIO$population_rural_filename))
+  populations <- list(urban=poprural_grid,rural=poprural_grid)
+  # validate
+  population.validate(populations)
+  # crop to extent
+  populations <- lapply(populations,FUN = function(grid){
+    return(crop(grid,AREA_EXTENT))
+    })
+  # correct with HumanData values
+  populations <- population.correct(populations)
+  return(populations)
+}
+
+population.validate <-function(populations){
+  # validate all population grids
+  reso_valid <- lapply(population_grids,FUN = validate.resolution)
+  # must be all valid
+  if(!all(reso_valid)){
+    print("ERROR: population file(s) are incorrect resolution!")
+    quit
+  }
+  
+}
+
+population.correct <- function(populations){
+  library(dplyr)
+  all_data <- data.frame(Urban = values(populations$urban),Rural = values(populations$rural),ISO = values(ISORASTER))
+  summed <- all_data %>%
+    filter(ISO %in% HUMAN_DATA$iso) %>% # restrict to only ISOs in HumanData_iso
+    group_by(ISO) %>% # do the following per ISO code
+    summarise(UrbanTotal = sum(Urban, na.rm = TRUE),
+              RuralTotal = sum(Rural, na.rm = TRUE)) %>%
+    ungroup()
+  HUMAN_DATA <- HUMAN_DATA[(HUMAN_DATA$iso %in% summed$ISO), ]
+  
+  HUMAN_DATA$popurb<-NA
+  HUMAN_DATA$poprur<-NA
+  
+  for (j in 1:length(HUMAN_DATA$iso)){
+    q<-which(summed$ISO==HUMAN_DATA$iso[j])
+    if(length(q)>0){
+      HUMAN_DATA$popurb[q]<-summed$UrbanTotal[q]
+      HUMAN_DATA$poprur[q]<-summed$RuralTotal[q]
+    }
+  }
+  detach("package:dplyr", unload=TRUE)
+  
+  urban_pop<-HUMAN_DATA$f_urb*HUMAN_DATA$pop_total
+  rural_pop<-(1-HUMAN_DATA$f_urb)*HUMAN_DATA$pop_total
+  
+  dif_urban<-urban_pop/HUMAN_DATA$popurb
+  dif_rural<-rural_pop/HUMAN_DATA$poprur
+  
+  dif_urban<-data.frame(iso=HUMAN_DATA$iso,value=dif_urban)
+  dif_urban_raster<-subs(isoraster, dif_urban, subsWithNA=T)
+  
+  dif_rural<-data.frame(iso=HUMAN_DATA$iso,value=dif_rural)
+  dif_rural_raster<-subs(isoraster, dif_rural, subsWithNA=T)
+  
+  populations$urban<-populations$urban*dif_urban_raster
+  populations$rural<-populations$rural*dif_rural_raster
+  
+  return(populations)
+}
+
+
+validate.resolution <- function(grid){
+  if( reso-xres(grid) > 1e-5 || reso-yres(grid)>1e-5){
+    return(FALSE)
+  }
+  else{
+    return(TRUE)
+  }
+}
\ No newline at end of file
diff --git a/Model scripts/wwtp_module.R b/Model scripts/wwtp_module.R
new file mode 100644
index 0000000000000000000000000000000000000000..68f385187f055e8eec5e690585dd466e8a4bf9bd
--- /dev/null
+++ b/Model scripts/wwtp_module.R	
@@ -0,0 +1,159 @@
+wwtp.module.run <- function(emissions,pathogen,popurban_grid,poprural_grid){
+  #open .csv file with WWTP data
+  # if 3 then location of WWTP known
+  # if 2 then treatment, but location unknown
+  # if 1 then no treatment
+  
+  # if(wwtp_available==3){
+  #   WWTP_inputs<-read.csv(paste0(working.path.in,overall_inputs$WWTPData_filename[i],".csv"))  #file has district, lat, lon, capacity, system categories. Would be good to add treatment type (if available) and reduction (if available) -> these data should come from Matt's model later on. For now just average removal has been used.
+  #   WWTP_inputs$treatment_type<-as.character(WWTP_inputs$treatment_type)
+  #   WWTP_inputs$subregion<-as.character(WWTP_inputs$subregion)
+  # }
+  
+  # spatial distribution
+  # from population to pathogens
+  # for connected emissions in the case the location of WWTPs is known
+  
+  pathogen_treatment<-isoraster
+  pathogen_treatment[]<-0
+  
+  # calculate the pathogen treathment of WWTP per subarea
+  if(SCENARIO$wwtp_available ==3){
+    
+    WWTP_inputs <- read.csv(SCENARIO$WWTPData_filename)
+    
+    emissions$pathogen_urb_fconsewer<-0
+    emissions$pathogen_rur_fconsewer<-0
+    emissions$pathogen_urb_flandonsite<-0
+    emissions$pathogen_rur_flandonsite<-0
+    emissions$pathogen_urb_flanddiffuse<-0
+    emissions$pathogen_rur_flanddiffuse<-0
+    
+    a<-which(emissions$pathogen_urb_conforgrid>0)
+    b<-which(emissions$pathogen_rur_conforgrid>0)
+    d<-which(emissions$pathogen_urb_landforgrid+emissions$pathogen_rur_landforgrid>0)
+    
+    emissions$pathogen_urb_fconsewer[a]<-emissions$pathogen_urb_con[a]/emissions$pathogen_urb_conforgrid[a]
+    emissions$pathogen_rur_fconsewer[b]<-emissions$pathogen_rur_con[b]/emissions$pathogen_rur_conforgrid[b]
+    emissions$pathogen_urb_flandonsite[d]<-emissions$pathogen_urb_onsite_land[d]/(emissions$pathogen_urb_landforgrid[d]+emissions$pathogen_rur_landforgrid[d])
+    emissions$pathogen_rur_flandonsite[d]<-emissions$pathogen_rur_onsite_land[d]/(emissions$pathogen_urb_landforgrid[d]+emissions$pathogen_rur_landforgrid[d])
+    emissions$pathogen_urb_flanddiffuse[d]<-emissions$pathogen_urb_dif[d]/(emissions$pathogen_urb_landforgrid[d]+emissions$pathogen_rur_landforgrid[d])
+    emissions$pathogen_rur_flanddiffuse[d]<-emissions$pathogen_rur_dif[d]/(emissions$pathogen_urb_landforgrid[d]+emissions$pathogen_rur_landforgrid[d])
+
+    WWTP_inputs$subarea<-raster::extract(isoraster,matrix(data=c(WWTP_inputs$Lon,WWTP_inputs$Lat),nrow=length(WWTP_inputs$Lon),ncol=2))
+    
+    for(j in 1:length(WWTP_inputs$subarea)){
+
+      if(identical(WWTP_inputs$treatment_type[j],"primary")){
+        WWTP_inputs$removalfraction_sewer[j]<-pathogen$RemovalPrimary[pathogen_row]
+        WWTP_inputs$removalfraction_onsite[j]<-pathogen$RemovalPrimary[pathogen_row]
+      }else if(identical(WWTP_inputs$treatment_type[j],"secondary")){
+        WWTP_inputs$removalfraction_sewer[j]<-pathogen$RemovalSecondary[pathogen_row]
+        WWTP_inputs$removalfraction_onsite[j]<-pathogen$RemovalSecondary[pathogen_row]
+      }else if(identical(WWTP_inputs$treatment_type[j],"tertiary")){
+        WWTP_inputs$removalfraction_sewer[j]<-pathogen$RemovalTertiary[pathogen_row]
+        WWTP_inputs$removalfraction_onsite[j]<-pathogen$RemovalTertiary[pathogen_row]
+      }else if(identical(WWTP_inputs$treatment_type[j],"quaternary")){
+        WWTP_inputs$removalfraction_sewer[j]<-pathogen$RemovalQuaternary[pathogen_row]
+        WWTP_inputs$removalfraction_onsite[j]<-pathogen$RemovalQuaternary[pathogen_row]
+      }else if(identical(WWTP_inputs$treatment_type[j],"ponds")){
+        WWTP_inputs$removalfraction_sewer[j]<-pathogen$RemovalPonds[pathogen_row]
+        WWTP_inputs$removalfraction_onsite[j]<-pathogen$RemovalPonds[pathogen_row]
+      }else if(identical(WWTP_inputs$treatment_type[j],"not treated")){
+        WWTP_inputs$removalfraction_sewer[j]<-0
+        WWTP_inputs$removalfraction_onsite[j]<-0
+      }
+    }
+    #estimate the fraction of people connected per capacity unit
+    capacity<-sum(WWTP_inputs$Capacity,na.rm=TRUE)  #how to deal with capacity is NA -> WWTP non existing?
+    WWTP_inputs$fcapacity<-WWTP_inputs$Capacity/capacity
+    
+    #calculate the emissions per WWTP
+    #the removal file for the WWTP is used if specified from the Sketcher tool, or the removal rate from the
+    #literature is used (specified in for loop above)
+    WWTP_inputs$emissions_per_WWTP_sewer_urb<-WWTP_inputs$fcapacity*(sum((emissions$pathogen_urb_conforgrid*emissions$pathogen_urb_fconsewer),na.rm=TRUE))*(1-WWTP_inputs$removalfraction_sewer)
+    WWTP_inputs$emissions_per_WWTP_sewer_rur<-WWTP_inputs$fcapacity*(sum((emissions$pathogen_rur_conforgrid*emissions$pathogen_rur_fconsewer),na.rm=TRUE))*(1-WWTP_inputs$removalfraction_sewer)
+    WWTP_inputs$emissions_per_WWTP_onsite_urb<-WWTP_inputs$fcapacity*(sum((emissions$pathogen_urb_conforgrid*(1-emissions$pathogen_urb_fconsewer)),na.rm=TRUE))*(1-WWTP_inputs$removalfraction_onsite)
+    WWTP_inputs$emissions_per_WWTP_onsite_rur<-WWTP_inputs$fcapacity*(sum((emissions$pathogen_rur_conforgrid*(1-emissions$pathogen_rur_fconsewer)),na.rm=TRUE))*(1-WWTP_inputs$removalfraction_onsite)
+    WWTP_inputs$emissions_per_WWTP<-WWTP_inputs$emissions_per_WWTP_sewer_urb+WWTP_inputs$emissions_per_WWTP_sewer_rur+WWTP_inputs$emissions_per_WWTP_onsite_urb+WWTP_inputs$emissions_per_WWTP_onsite_rur
+    
+    print("Warning: currently average treatment coverage and efficiency are used. No link is yet established with the pathogen flow tool")
+    
+    WWTP_emissions_per_subarea<-array(dim=c(length(unique(WWTP_inputs$subarea)),5))
+    colnames(WWTP_emissions_per_subarea)<-c("subarea","sewer_urb","sewer_rur","onsite_urb","onsite_rur")
+    WWTP_emissions_per_subarea<-as.data.frame(WWTP_emissions_per_subarea)
+    
+    for(j in 1:length(unique(WWTP_inputs$subarea))){
+      temp<-which(WWTP_inputs$subarea==unique(WWTP_inputs$subarea)[j])
+      WWTP_emissions_per_subarea$subarea[j]<-WWTP_inputs$subarea[temp[1]]
+      WWTP_emissions_per_subarea$sewer_urb[j]<-sum(WWTP_inputs$emissions_per_WWTP_sewer_urb[temp],na.rm=TRUE)
+      WWTP_emissions_per_subarea$sewer_rur[j]<-sum(WWTP_inputs$emissions_per_WWTP_sewer_rur[temp],na.rm=TRUE)
+      WWTP_emissions_per_subarea$onsite_urb[j]<-sum(WWTP_inputs$emissions_per_WWTP_onsite_urb[temp],na.rm=TRUE)
+      WWTP_emissions_per_subarea$onsite_rur[j]<-sum(WWTP_inputs$emissions_per_WWTP_onsite_rur[temp],na.rm=TRUE)
+    }
+    
+    emissions$pathogen_urb_WWTP_sewer<-0
+    emissions$pathogen_rur_WWTP_sewer<-0
+    emissions$pathogen_urb_WWTP_onsite<-0
+    emissions$pathogen_rur_WWTP_onsite<-0
+    
+    for(j in 1:length(WWTP_emissions_per_subarea$subarea)){
+      emissions$pathogen_urb_WWTP_sewer[which(HUMAN_DATA$iso==WWTP_emissions_per_subarea$subarea[j])]<-WWTP_emissions_per_subarea$sewer_urb[j]
+      emissions$pathogen_rur_WWTP_sewer[which(HUMAN_DATA$iso==WWTP_emissions_per_subarea$subarea[j])]<-WWTP_emissions_per_subarea$sewer_rur[j]
+      emissions$pathogen_urb_WWTP_onsite[which(HUMAN_DATA$iso==WWTP_emissions_per_subarea$subarea[j])]<-WWTP_emissions_per_subarea$onsite_urb[j]
+      emissions$pathogen_rur_WWTP_onsite[which(HUMAN_DATA$iso==WWTP_emissions_per_subarea$subarea[j])]<-WWTP_emissions_per_subarea$onsite_rur[j]
+    }
+    
+    #now add the value to the correct grid in a new raster.
+
+    pathogen_treatment[cellFromXY(isoraster,matrix(data=c(WWTP_inputs$Lon,WWTP_inputs$Lat),nrow=length(WWTP_inputs$Lon),ncol=2))]<-WWTP_inputs$emissions_per_WWTP
+    
+    emissions$pathogen_urb_con<-emissions$pathogen_urb_WWTP_sewer
+    emissions$pathogen_rur_con<-emissions$pathogen_rur_WWTP_sewer
+    emissions$pathogen_urb_onsite_treated<-emissions$pathogen_urb_WWTP_onsite
+    emissions$pathogen_rur_onsite_treated<-emissions$pathogen_rur_WWTP_onsite
+    
+  }
+  
+  pathogen_urban_water_pp<-data.frame(iso=emissions$iso,value=emissions$pathogen_urb_waterforgrid_pp)
+  pathogen_urban_water_pp_raster<-subs(isoraster, pathogen_urban_water_pp , subsWithNA=T)
+  
+  pathogen_rural_water_pp<-data.frame(iso=emissions$iso,value=emissions$pathogen_rur_waterforgrid_pp)
+  pathogen_rural_water_pp_raster<-subs(isoraster, pathogen_rural_water_pp , subsWithNA=T)
+  
+  pathogen_urban_water_grid<-pathogen_urban_water_pp_raster*popurban_grid
+  pathogen_rural_water_grid<-pathogen_rural_water_pp_raster*poprural_grid
+  
+  temp<-data.frame(bla=NA,value=0)
+  pathogen_urban_water_grid<-subs(pathogen_urban_water_grid,temp,subsWithNA=F)
+  pathogen_rural_water_grid<-subs(pathogen_rural_water_grid,temp,subsWithNA=F)
+  
+  temp<-data.frame(bla=NaN,value=0)
+  pathogen_urban_water_grid<-subs(pathogen_urban_water_grid,temp,subsWithNA=F)
+  pathogen_rural_water_grid<-subs(pathogen_rural_water_grid,temp,subsWithNA=F)
+  
+  pathogen_water_grid<-pathogen_urban_water_grid+pathogen_rural_water_grid
+  
+  #for emissions to land
+  pathogen_urban_land_pp<-data.frame(iso=emissions$iso,value=emissions$pathogen_urb_landforgrid_pp)
+  pathogen_urban_land_pp_raster<-subs(isoraster, pathogen_urban_land_pp , subsWithNA=T)
+  
+  pathogen_rural_land_pp<-data.frame(iso=emissions$iso,value=emissions$pathogen_rur_landforgrid_pp)
+  pathogen_rural_land_pp_raster<-subs(isoraster, pathogen_rural_land_pp , subsWithNA=T)
+  
+  pathogen_urban_land_grid<-pathogen_urban_land_pp_raster*popurban_grid
+  pathogen_rural_land_grid<-pathogen_rural_land_pp_raster*poprural_grid
+  
+  temp<-data.frame(bla=NA,value=0)
+  pathogen_urban_land_grid<-subs(pathogen_urban_land_grid,temp,subsWithNA=F)
+  pathogen_rural_land_grid<-subs(pathogen_rural_land_grid,temp,subsWithNA=F)
+  
+  temp<-data.frame(bla=NaN,value=0)
+  pathogen_urban_land_grid<-subs(pathogen_urban_land_grid,temp,subsWithNA=F)
+  pathogen_rural_land_grid<-subs(pathogen_rural_land_grid,temp,subsWithNA=F)
+  
+  pathogen_land_grid<-pathogen_urban_land_grid+pathogen_rural_land_grid
+    
+  return(list(emissions=emissions,grids=list(land=pathogen_land_grid,water=pathogen_water_grid)))
+  
+}
\ No newline at end of file