From c99aa53f3784f68e52f57fda5f9f1fcccff52f28 Mon Sep 17 00:00:00 2001
From: Hofstra <nynke.hofstra@wur.nl>
Date: Wed, 20 Oct 2021 00:30:44 +0200
Subject: [PATCH] Fixed two bugs: 1. A typo in the total calculation for
 total_pitNoSlab in pathogenflows.R. 2. A problem in the emissions.R script
 where HUMAN_DATA$popurb and HUMAN_DATA$poprur were used for the per capita
 emissions calculations. Instead HUMAN_DATA$population should have been used
 together with HUMAN_DATA$fraction_urban_pop.

One minor problem remains. For some of the island states (Comoros, Djibouti, Guinea Bissou, Kiribati, Maldives, Sri Lanka, Solomon Islands, Saint Vincent and the Grenadines, Vanuatu and Samoa) the urban emissions have not been plotted on the map. The reason for this is that the gridded urban and rural population files indicate only rural population on these islands. That is due to a preprocessing script. As these islands do not contribute much to the overall picture, I decided to ignore this problem right now.
---
 Model scripts/emissions.R     | 8 ++++----
 Model scripts/pathogenflows.R | 7 ++++---
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/Model scripts/emissions.R b/Model scripts/emissions.R
index b40335b..11a36ce 100644
--- a/Model scripts/emissions.R	
+++ b/Model scripts/emissions.R	
@@ -1,10 +1,10 @@
 
 emissions.calc.avg.pp <- function(emissions){
-  emissions$pathogen_urb_waterforgrid_pp <- emissions$pathogen_urb_waterforgrid/HUMAN_DATA$popurb
-  emissions$pathogen_rur_waterforgrid_pp <- emissions$pathogen_rur_waterforgrid/HUMAN_DATA$poprur
+  emissions$pathogen_urb_waterforgrid_pp <- emissions$pathogen_urb_waterforgrid/(HUMAN_DATA$population*HUMAN_DATA$fraction_urban_pop)
+  emissions$pathogen_rur_waterforgrid_pp <- emissions$pathogen_rur_waterforgrid/(HUMAN_DATA$population*(1-HUMAN_DATA$fraction_urban_pop))
   
-  emissions$pathogen_urb_landforgrid_pp <- emissions$pathogen_urb_landforgrid/HUMAN_DATA$popurb
-  emissions$pathogen_rur_landforgrid_pp <- emissions$pathogen_rur_landforgrid/HUMAN_DATA$poprur
+  emissions$pathogen_urb_landforgrid_pp <- emissions$pathogen_urb_landforgrid/(HUMAN_DATA$population*HUMAN_DATA$fraction_urban_pop)
+  emissions$pathogen_rur_landforgrid_pp <- emissions$pathogen_rur_landforgrid/(HUMAN_DATA$population*(1-HUMAN_DATA$fraction_urban_pop))
   return(emissions)
 }
 
diff --git a/Model scripts/pathogenflows.R b/Model scripts/pathogenflows.R
index 6fcf5c5..54fa078 100644
--- a/Model scripts/pathogenflows.R	
+++ b/Model scripts/pathogenflows.R	
@@ -6,6 +6,7 @@ log_info("Loaded package {session_info$otherPkgs$pathogenflows$Package}  {sessio
 pathogenflow.run <- function(pathogen){
   log_info("Using Pathogenflows to calculate emissions")
   emissions <- data.frame(iso=HUMAN_DATA$iso,subarea=HUMAN_DATA$subarea)
+
   # 1) extract pathogen type from pathogen
   pathogen_type <- pathogen$pathogenType
   area_types <- c("urban","rural")
@@ -22,7 +23,7 @@ pathogenflow.run <- function(pathogen){
 
   tic("calc pathogenflows loadings")
   # for debugging inside getLoadings
-  # loadings <- pathogenflows::getLoadings(onsite_data$urban$child,pathogen_type)
+  #loadings <- pathogenflows::getLoadings(onsite_data$urban$child,pathogen_type)
   cl <- makeCluster(detectCores(),outfile=LOG_FILE)
   clusterEvalQ(cl, library(pathogenflows))
   clusterExport(cl, varlist=c("log_info"))
@@ -100,6 +101,7 @@ pathogenflow.run <- function(pathogen){
   # TODO: check with @Nynke. Can we ussume this?
   HUMAN_DATA$removalfraction <- replace(HUMAN_DATA$removalfraction, is.na(HUMAN_DATA$removalfraction),1)
   
+  
   for(i in 1:length(emissions$subarea)){
     emissions$pathogen_urb_conforgrid_sewer[i]<-sum(c(emissions$to_sewerage_flushSewer_urb[i]),na.rm=TRUE) #,emissions$to_sewerage_flushSeptic_urb[i],emissions$to_sewerage_flushPit_urb[i],emissions$to_sewerage_flushOpen_urb[i],emissions$to_sewerage_flushUnknown_urb[i],emissions$to_sewerage_pitSlab_urb[i],emissions$to_sewerage_pitNoSlab_urb[i],emissions$to_sewerage_compostingTwinSlab_urb[i],emissions$to_sewerage_compostingTwinNoSlab_urb[i],emissions$to_sewerage_compostingToilet_urb[i],emissions$to_sewerage_bucketLatrine_urb[i],emissions$to_sewerage_containerBased_urb[i],emissions_to_sewerage_hangingToilet_urb[i],emissions_to_sewerage_openDefecation_urb[i],emissions$to_sewerage_other_urb[i]),na.rm=TRUE) #I added all toilet types, but I think only emissions$to_sewerage_flushSewer_urb should have a value >0
     emissions$pathogen_rur_conforgrid_sewer[i]<-sum(c(emissions$to_sewerage_flushSewer_rur[i]),na.rm=TRUE) #emissions$to_sewerage_flushSeptic_rur[i],emissions$to_sewerage_flushPit_rur[i],emissions$to_sewerage_flushOpen_rur[i],emissions$to_sewerage_flushUnknown_rur[i],emissions$to_sewerage_pitSlab_rur[i],emissions$to_sewerage_pitNoSlab_rur[i],emissions$to_sewerage_compostingTwinSlab_rur[i],emissions$to_sewerage_compostingTwinNoSlab_rur[i],emissions$to_sewerage_compostingToilet_rur[i],emissions$to_sewerage_bucketLatrine_rur[i],emissions$to_sewerage_containerBased_rur[i],emissions_to_sewerage_hangingToilet_rur[i],emissions_to_sewerage_openDefecation_rur[i],emissions$to_sewerage_other_rur[i]),na.rm=TRUE)
@@ -360,7 +362,6 @@ pathogenflow.calc.totals <- function(emissions){
   totals$total_openDefecation_out<- rowSums(cbind(emissions$to_surface_openDefecation_urb_out,emissions$to_surface_openDefecation_rur_out), na.rm = T)
   totals$total_other_out<- rowSums(cbind(emissions$to_fecalSludge_other_urb_out,emissions$to_surface_other_urb_out,emissions$to_fecalSludge_other_rur_out,emissions$to_surface_other_rur_out), na.rm = T)
 
-  #THERE IS A MISTAKE IN THE CODE BELOW, pitNoSlab seems to have been excluded from the totals calculateion.  
   for (i in 1:length(HUMAN_DATA$iso)){
     if(sum(as.numeric(totals[i,3:15]),na.rm=TRUE)==0 && sum(as.numeric(HUMAN_DATA[i,21:33]),na.rm=TRUE)+sum(as.numeric(HUMAN_DATA[i,44:56]),na.rm=TRUE)==0){
       totals[i,3:15]<-NA      
@@ -369,7 +370,7 @@ pathogenflow.calc.totals <- function(emissions){
   
   totals$total<-NA
   for(i in 1:length(totals$iso)){
-    totals$total[i]<-sum(c(totals$total_flushSewer_out[i],totals$total_flushSeptic_out[i],totals$total_flushPit_out[i],totals$total_flushOpen_out[i],totals$total_flushUnknown_out[i],totals$total_pitSlab_out[i],totals$total_flushpitNoSlab_out[i],totals$total_compostingToilet_out[i],totals$total_bucketLatrine_out[i],totals$total_containerBased_out[i],totals$total_hangingToilet_out[i],totals$total_openDefecation_out[i],totals$total_other_out[i]),na.rm=TRUE)
+    totals$total[i]<-sum(c(totals$total_flushSewer_out[i],totals$total_flushSeptic_out[i],totals$total_flushPit_out[i],totals$total_flushOpen_out[i],totals$total_flushUnknown_out[i],totals$total_pitSlab_out[i],totals$total_pitNoSlab_out[i],totals$total_compostingToilet_out[i],totals$total_bucketLatrine_out[i],totals$total_containerBased_out[i],totals$total_hangingToilet_out[i],totals$total_openDefecation_out[i],totals$total_other_out[i]),na.rm=TRUE)
   }
   
   for(i in 1:length(totals$iso)){
-- 
GitLab