diff --git a/ImpactInformationProfiles.xlsx b/ImpactInformationProfiles.xlsx index db10276..2609284 100644 Binary files a/ImpactInformationProfiles.xlsx and b/ImpactInformationProfiles.xlsx differ diff --git a/RCode/Analysis/SEN.R b/RCode/Analysis/SEN.R new file mode 100644 index 0000000..ab8e5ea --- /dev/null +++ b/RCode/Analysis/SEN.R @@ -0,0 +1,268 @@ +options(scipen = 999) + +iso3<-"SEN" + +impies<-readRDS("./CleanedData/MostlyImpactData/AllHaz_impies_20231019_nozeros.RData") +# Load taxonomies +taxies<-openxlsx::read.xlsx("./ImpactInformationProfiles.xlsx") +# Load the SEN data specifically +SEN<-impies%>%filter(ISO3=="SEN" & Year>2000 & !is.na(haz_Ab)) +# left join imp_det variable to the dataframe +SEN%<>%left_join(data.frame( + imp_det=taxies%>%filter(list_name=="imp_det")%>%pull(name), + imp_det_lab=taxies%>%filter(list_name=="imp_det")%>%pull(label) +)) +# left join imp_type variable to the dataframe +SEN%<>%left_join(data.frame( + imp_type=taxies%>%filter(list_name=="imp_type")%>%pull(name), + imp_type_lab=taxies%>%filter(list_name=="imp_type")%>%pull(label) +)) +# Merge the two so that we can see the full impact descriptions +SEN%<>%mutate(Impact=paste0(imp_det_lab," ",imp_type_lab)) + +SEN$Impact[SEN$Impact=="General Aid Contributions NA"]<-"IFRC Funding Allocated (DREF & EA)" +# Plot the yearly counts by data base +SEN%>%group_by(Year,imp_src_db)%>%reframe(Count=n())%>% + arrange(Year)%>%group_by(imp_src_db)%>%mutate(Cum=cumsum(Count))%>% + ggplot()+geom_point(aes(Year,Cum,colour=imp_src_db))#+scale_y_log10() +table(SEN$imp_cats) +SEN%>%filter(imp_cats=="impcatphyinf")%>% + ggplot()+geom_bar(aes(factor(Impact),fill=imp_src_db))+facet_wrap(~imp_src_db)+ + theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=1)) + +unique(impies$imp_spat_ID[impies$imp_src_db=="Desinventar"]) + +table(SEN$imp_src_db) + +q<-SEN%>%group_by(Year,imp_src_db,haz_Ab)%>%reframe(Count=n())%>% + arrange(Year)%>%group_by(imp_src_db,haz_Ab)%>%mutate(Cum=cumsum(Count))%>% + ggplot()+geom_point(aes(Year,Cum,colour=haz_Ab))+ + xlab("Year")+ylab("Cumulative Number")+ + theme(plot.title = element_text(hjust = 0.5,face="bold",size=18))+ + ggtitle("Cumulative Number of Hazard Events")+labs(colour="Hazard")+ + facet_wrap(~imp_src_db);q +ggsave("./Plots/SEN/SEN_NoFL_Des_perYear.png",q,width=12,height=8) + +q<-SEN%>%filter(Impact%in%c("Buildings Damaged","People (All Demographics) Deaths", + "People (All Demographics) Total Affected", + "IFRC Funding Allocated (DREF & EA)"))%>% + mutate(Date=as.Date(ev_sdate))%>% + ggplot()+geom_point(aes(Date,imp_value,colour=haz_Ab))+ + facet_wrap(~Impact,scales = "free_y")+labs(colour="Hazard")+ + theme(plot.title = element_text(hjust = 0.5,face="bold",size=18))+ + ggtitle("Estimated Impact")+labs(colour="Hazard")+ + ylab("Impact");q +ggsave("./Plots/SEN/SEN_HazTimeline_perImpact.png",q,width=12,height=4) + +#@@@@@@@@@@ ADM 1 @@@@@@@@@@# +# Desinventar +i<-1 +maps<-readRDS("./tmp_maps_SEN.RData") +loccy<-str_split(maps$filename[i+1],"/",simplify = T); loccy<-loccy[length(loccy)] +dADM<-sf::st_read(paste0("./RawData/MostlyImpactData/Desinventar/",str_to_lower(iso3),"/",loccy),quiet=T) +plot(dADM) +dADM%<>%dplyr::select(ID) + +dADM$Allrecords<-sapply(dADM$ID,function(codie){ + sum(grepl(codie,SEN$imp_spat_ID,ignore.case = T)) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=Allrecords), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("No. Impact Records (Desinventar)")+ + scale_fill_gradient(name = "Impact Records",guide="legend", trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + + +dADM$deaths<-sapply(dADM$ID,function(codie){ + sum(SEN$imp_value[grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$imp_det=="impdetallpeop" & SEN$imp_type=="imptypdeat"]) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=deaths), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Total Deaths (Desinventar)")+ + scale_fill_gradient(name = "Deaths",guide="legend", trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + +dADM$builds<-sapply(dADM$ID,function(codie){ + sum(SEN$imp_value[grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$imp_det=="impdetbuild"]) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=builds), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Total Buildings Destroyed/Damaged (Desinventar)")+ + scale_fill_gradient(name = "Buildings",guide="legend", #trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + + +dADM$FLdeaths<-sapply(dADM$ID,function(codie){ + sum(SEN$imp_value[grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$imp_det=="impdetallpeop" & SEN$imp_type=="imptypdeat" & + SEN$haz_Ab=="FL"]) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=FLdeaths), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Total Flood Deaths (Desinventar)")+ + scale_fill_gradient(name = "Deaths",guide="legend", trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + +dADM$FL<-sapply(dADM$ID,function(codie){ + sum(grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$haz_Ab=="FL") +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=FL), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Number of Floods (Desinventar)")+ + theme(legend.position = c(.75, .75))+ + scale_fill_gradient(name = "Flood Records",guide="legend",# trans = "log10", + low="darkblue",high="lightblue",n.breaks=6);q +ggsave("./Plots/SEN/SEN_NoFL_Des.png",q,width=12,height=10) + + +dADM$WF<-sapply(dADM$ID,function(codie){ + sum(grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$haz_Ab=="WF") +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=WF), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Number of Wildfires (Desinventar)")+ + theme(legend.position = c(.75, .75))+ + scale_fill_gradient(name = "Wildfire Records",guide="legend",trans = "log10", + low="darkred",high="red1",n.breaks=6);q +ggsave("./Plots/SEN/SEN_NoWF_Des.png",q,width=12,height=10) + +dADM$FLbuilds<-sapply(dADM$ID,function(codie){ + sum(SEN$imp_value[grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$imp_det=="impdetbuild" & + SEN$haz_Ab=="FL"]) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=FLbuilds), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Floods - Buildings Destroyed/Damaged (Desinventar)")+ + scale_fill_gradient(name = "Buildings",guide="legend", trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + + +dADM$WFbuilds<-sapply(dADM$ID,function(codie){ + sum(SEN$imp_value[grepl(codie,SEN$imp_spat_ID,ignore.case = T) & + SEN$imp_det=="impdetbuild" & + SEN$haz_Ab=="WF"]) +}) + +# Plot it out! +q<-dADM%>%ggplot()+ + geom_sf(aes(fill=WFbuilds), linewidth=0.1) + #, inherit.aes = FALSE) + + coord_sf(datum = NULL) + #ylim(c(-80,80)) + + # coord_sf(datum = crs_mappy, xlim = c(-100,-40), ylim=c(-30,30), expand = FALSE) + #ylim(c(-80,80)) + + ggthemes::theme_map() + + theme(legend.background = element_rect(fill="white", + linetype="solid", + colour ="black"), + plot.title = element_text(hjust = 0.5,face="bold",size=18), + legend.position = c(0.1, 0.1))+ + ggtitle("Wildfires - Buildings Destroyed/Damaged (Desinventar)")+ + scale_fill_gradient(name = "Buildings",guide="legend", trans = "log10", + low="magenta4",high="magenta",n.breaks=6);q + + + + + + +# EM-DAT +# eADM<-sf::st_read(paste0("./RawData/SocioPoliticalData/GAUL/gadm40_SEN_shp/gadm40_SEN_",i,".shp")) +eADM<-sf::st_read(paste0("./RawData/SocioPoliticalData/GAUL/SEN_adm_shp_old/SEN_adm",i,".shp")) +# eADM<-sf::st_read(paste0("./RawData/SocioPoliticalData/GAUL/gadm36_SEN_shp/gadm36_SEN_",i,".shp")) + +SEN_adm_shp_old + +codies<-SEN%>%filter(imp_src_db=="EM-DAT")%>%pull(imp_spat_ID)%>%str_split(";",simplify = T)%>%unlist()%>%c()%>%str_split(",",simplify = T)%>%c()%>%unique() + + + + + + +#@@@@@@@@@@ ADM 1 @@@@@@@@@@# +i<-1 +loccy<-str_split(maps$filename[i+1],"/",simplify = T); loccy<-loccy[length(loccy)] +dADM<-ADM<-sf::st_read(paste0("./RawData/MostlyImpactData/Desinventar/",str_to_lower(iso3),"/",loccy),quiet=T) +# EM-DAT +eADM<-GetGAUL(iso3,lADM=i) + + + diff --git a/RCode/Analysis/WDR_Analysis.R b/RCode/Analysis/WDR_Analysis.R index 9274ead..6b571bb 100644 --- a/RCode/Analysis/WDR_Analysis.R +++ b/RCode/Analysis/WDR_Analysis.R @@ -811,12 +811,154 @@ ggsave("./Plots/Counts_db_w-LOESS.png",p,width=10,height=6) - - - - - - +impies%<>%mutate(Region=left_join(impies,readxl::read_xlsx(filer)%>% + transmute(ISO3=`ISO Code`,continent=`UN Region`), + by="ISO3")$continent, + Subregion=left_join(impies,readxl::read_xlsx(filer)%>% + transmute(ISO3=`ISO Code`,continent=`World Bank Regions`), + by="ISO3")$continent) +# So that the months are plotted in english +Sys.setlocale(category = "LC_TIME", locale="en_GB.UTF-8") + +month_labs<-c("January","February","March","April", + "May","June","July","August", + "September","October","November","December") +# +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + (haz_type=="haztypehydromet" | haz_Ab=="WF") & imp_src_db=="EM-DAT")%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365))%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,fill=Subregion),alpha=0.5)+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + facet_wrap(~Subregion,nrow=3)+ + ggtitle("Proportion of Climate- & Weather-Related Events");p +ggsave("./Plots/PercEvents_Month_Subregion.png",p,height = 8,width=12) +# +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + imp_src_db=="EM-DAT" & haz_Ab%in%c("FL"))%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365))%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,fill=Subregion),alpha=0.5)+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + facet_wrap(~Subregion)+ + ggtitle("Proportion of Flood Events");p +ggsave("./Plots/PercFloods_Month_Subregion.png",p,height = 8,width=12) +# +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + (haz_type=="haztypehydromet" | haz_Ab=="WF") & + imp_det=="impdetallpeop" & imp_type=="imptypdeat")%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365), + Subregion=as.factor(Subregion))%>%filter(imp_src_db=="EM-DAT")%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,weights=imp_value/sum(imp_value),fill=Subregion),alpha=0.5)+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + facet_wrap(~Subregion,nrow=3)+ + ggtitle("Proportion of Climate- & Weather-Related Deaths");p +ggsave("./Plots/PercDeaths_Month_Subregion.png",p,height = 8,width=12) +# +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + (haz_type=="haztypehydromet" | haz_Ab=="WF") & + imp_det=="impdetallpeop" & imp_type=="imptypdeat")%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365))%>% + filter(imp_src_db=="EM-DAT")%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,weights=imp_value/sum(imp_value),fill=Subregion),alpha=0.5)+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + facet_wrap(~Subregion)+ + ggtitle("Proportion of Flood Deaths");p +ggsave("./Plots/PercFloodDeaths_Month_Subregion.png",p,height = 8,width=12) + + +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + (haz_type=="haztypehydromet" | haz_Ab=="WF") & + imp_det%in%c("impdetinfloccur","impdetloccur") & imp_type=="imptypcost")%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365))%>% + # filter(imp_src_db=="EM-DAT")%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,weights=imp_value/sum(imp_value), + fill=Subregion,linetype=imp_src_db,alpha=imp_src_db))+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + scale_alpha_discrete(range = c(0, 0.5))+ + facet_wrap(~Subregion)+ + ggtitle("Proportion of Cost");p +ggsave("./Plots/PercCost_Month_Subregion_perDB.png",p,height = 8,width=12) + +p<-impies%>%filter(!is.na(Subregion) & Subregion!="Not Classified" & + (haz_type=="haztypehydromet" | haz_Ab=="WF") & + imp_det=="impdetinfloccur" & imp_type=="imptypcost")%>% + distinct(GCDB_ID,.keep_all = T)%>%group_by(Subregion)%>% + mutate(difftime=as.numeric((as.Date(ev_sdate)-as.Date(paste0(AsYear(ev_sdate),"-01-01")))/365))%>% + # filter(imp_src_db=="EM-DAT")%>% + ggplot()+geom_density(aes(difftime,y=..scaled..,weights=imp_value/sum(imp_value), + fill=Subregion),alpha=0.5)+ + scale_x_continuous(breaks=0:11/12,labels=month_labs)+xlab("Month")+ylab("Proportion")+ + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1), + panel.background = element_rect(fill = "white", + colour = "white", + size = 0.5, linetype = "solid"), + panel.grid.major = element_line(size = 0.1, linetype = 'solid', + colour = "grey"), + plot.title = element_text(hjust = 0.5))+ + scale_alpha_discrete(range = c(0, 0.5))+ + facet_wrap(~Subregion)+ + ggtitle("Proportion of Cost (EM-DAT)");p +ggsave("./Plots/PercCost_Month_Subregion_EMDAT.png",p,height = 8,width=12) + + +tmp<-impies%>%mutate(Year=AsYear(ev_sdate), Date=as.Date(ev_sdate))%>% + filter(haz_Ab=="EQ" & imp_src_db=="EM-DAT" & + imp_det=="impdetallpeop" & imp_type=="imptypdeat" & + Year<2005)%>%distinct(GCDB_ID,.keep_all = T)%>% + arrange(Date) +tmp%>%group_by(Date)%>% + mutate(Count=sum(tmp$Date>unique(Date)-365/2 & tmp$Date% + ggplot()+geom_point(aes(Date,Count)) + +tmp + + + + +tmp$dateDiff2<-c(0,as.numeric(tmp$Date[2:nrow(tmp)]-tmp$Date[1:(nrow(tmp)-1)])) + +tmp%>%mutate(Gradient=zoo::rollmean(1/dateDiff2,k=30, fill=NA, align='right'))%>% + filter(Gradient<1 & Year>1970)%>% + ggplot()+geom_point(aes(Date,Gradient)) diff --git a/RCode/GCDB_Objects/GCDB_table.R b/RCode/GCDB_Objects/GCDB_table.R index 83c9b76..4bd05ac 100644 --- a/RCode/GCDB_Objects/GCDB_table.R +++ b/RCode/GCDB_Objects/GCDB_table.R @@ -66,7 +66,7 @@ col_tabGCDB<-c("event_ID"="character", # GCDB event ID "haz_link"="character", # Associated impactful-hazards to the specific hazard "haz_potlink"="character", # Potential other impactful-hazards that may be associated to the specific hazard "haz_maxvalue"="numeric", # Maximum intensity or magnitude of the hazard, e.g. - "haz_units"="character", # Units of the max intensity/magnitude value estimate + "haz_maxunits"="character", # Units of the max intensity/magnitude value estimate "haz_est_type"="character", # Estimate type: primary, secondary, modelled "haz_src_db"="character", # Source database name of impact estimate or the curated estimate "haz_src_org"="character", # Source organisation of impact estimate or the curated estimate @@ -84,21 +84,28 @@ col_tabGCDB<-c("event_ID"="character", # GCDB event ID "imp_spat_covcode"="character", # Spatial object type "imp_spat_res"="character", # Spatial resolution of impact estimate "imp_spat_resunits"="character", # Spatial resolution units of impact estimate (e.g. ADM level, raster grid) + "imp_spat_crs"="character", "imp_spat_srcorg"="character", # organisation of the spatial data + "imp_spat_srcdb"="character", "imp_spat_srcurl"="character", # URL of the impact estimate "imp_spat_colIDs"="character", "imp_spat_rowIDs"="character", "imp_spat_fileloc"="character", - + "imp_spat_fileread"="character", + "imp_spat_URL"="character", # Spatial info - hazard "haz_spat_ID"="character", # ID of the spatial object "haz_spat_covcode"="character", # Spatial object type + "haz_spat_fileread"="character", "haz_spat_res"="character", # Spatial resolution of impact estimate "haz_spat_resunits"="character", # Spatial resolution units of impact estimate (e.g. ADM level, raster grid) + "haz_spat_crs"="character", "haz_spat_srcorg"="character", # Source organisation from where the spatial object comes from "haz_spat_srcurl"="character", # URL of the impact estimate - "haz_spat_colIDs"="character", - "haz_spat_rowIDs"="character", + "haz_spat_srcdb"="character", + "haz_spat_URL"="character", + "haz_spat_colname"="character", + "haz_spat_rowname"="character", "haz_spat_fileloc"="character") # Required fields oblig_tabGCDB<-c("event_ID","imp_sub_ID","ev_ISO3s","imp_cat","imp_subcat","imp_det","imp_units", @@ -142,11 +149,16 @@ GetGCDB_hazID<-function(impies){ return(impies) } -GetGCDB_spatID<-function(impies){ +GetGCDB_imp_spatID<-function(impies){ impies%>%dplyr::select(imp_spat_srcorg,imp_spat_srcdb,imp_spat_covcode,imp_spat_res,imp_spat_resunits)%>% apply(1,function(x) paste0(x,collapse = "-")) } +GetGCDB_haz_spatID<-function(hazzies){ + hazzies%>%dplyr::select(haz_spat_srcorg,haz_spat_srcdb,haz_spat_covcode,haz_spat_res,haz_spat_resunits)%>% + apply(1,function(x) paste0(x,collapse = "-")) +} + AddEmptyColImp<-function(DF){ for(i in which(!names(col_tabGCDB)%in%colnames(DF))){ tmp<-NA diff --git a/RCode/GCDB_Objects/MontandonSchema_JSON.R b/RCode/GCDB_Objects/MontandonSchema_JSON.R index 0dd3aa5..6825dce 100644 --- a/RCode/GCDB_Objects/MontandonSchema_JSON.R +++ b/RCode/GCDB_Objects/MontandonSchema_JSON.R @@ -3,6 +3,19 @@ source("./RCode/Setup/GetPackages.R") # https://json-schema.org/learn/glossary # Get the RDLS JSON schema RDLS<-jsonlite::fromJSON("https://docs.riskdatalibrary.org/en/0__2__0/rdls_schema.json") + + + + + + +stop("rename ext_IDs to something less overlappyyyyy") +stop("change _fileread to _format, check _spat_fileloc and _spat_URL") + + + + + # Get the taxonomy data taxies<-openxlsx::read.xlsx("./ImpactInformationProfiles.xlsx") # Country codes and associated regions diff --git a/RCode/GCDB_Objects/MontyMethods.R b/RCode/GCDB_Objects/MontyMethods.R index c523c99..cc36acf 100644 --- a/RCode/GCDB_Objects/MontyMethods.R +++ b/RCode/GCDB_Objects/MontyMethods.R @@ -1,3 +1,44 @@ +taxies<-openxlsx::read.xlsx("./ImpactInformationProfiles.xlsx") +imp_class<-data.frame( + imp_det_code=taxies%>%filter(list_name=="imp_det")%>%pull(name), + imp_det_lab=taxies%>%filter(list_name=="imp_det")%>%pull(label), + imp_subcat_code=taxies%>%filter(list_name=="imp_det")%>%pull(link_group), + imp_subcat_lab=left_join(taxies[taxies$list_name=="imp_det",2:4], + taxies[taxies$list_name=="imp_subcats",2:4], + by=c("link_group"="name"))%>%pull(label.y), + imp_cat_code=taxies%>%filter(list_name=="imp_det")%>%pull(link_maingroup), + imp_cat_lab=left_join(left_join(taxies[taxies$list_name=="imp_det",2:4], + taxies[taxies$list_name=="imp_subcats",2:4], + by=c("link_group"="name")), + taxies[taxies$list_name=="imp_cats",2:3], + by=c("link_group.y"="name"))%>%pull(label) +) +haz_class<-data.frame( + haz_spec_code=taxies%>%filter(list_name=="hazardsubsubtypes")%>%pull(name), + haz_spec_lab=taxies%>%filter(list_name=="hazardsubsubtypes")%>%pull(label), + haz_cluster_code=taxies%>%filter(list_name=="hazardsubsubtypes")%>%pull(link_group), + haz_cluster_lab=left_join(taxies[taxies$list_name=="hazardsubsubtypes",2:4], + taxies[taxies$list_name=="hazardsubtypes",2:4], + by=c("link_group"="name"))%>%pull(label.y), + haz_type_code=taxies%>%filter(list_name=="hazardsubsubtypes")%>%pull(link_maingroup), + haz_type_lab=left_join(left_join(taxies[taxies$list_name=="hazardsubsubtypes",2:4], + taxies[taxies$list_name=="hazardsubtypes",2:4], + by=c("link_group"="name")), + taxies[taxies$list_name=="hazardtypes",2:3], + by=c("link_group.y"="name"))%>%pull(label) +) + +# ExtrLabel_Monty<-function(impies,HazNOTImp=F){ +# if(HazNOTImp){ +# +# } else { +# +# } +# +# return(impies) +# } + + # A function to go from a series of data into the JSON-accepted DF-list style object Add_EvIDlink_Monty<-function(dframe){ # Setup the other entries @@ -7,6 +48,8 @@ Add_EvIDlink_Monty<-function(dframe){ output$ext_IDs<-sapply(unique(dframe$event_ID),function(ID){ # Find the corresponding indices for this entry indy<-dframe$event_ID==ID + # check for no external IDs + if(any(is.na(dframe$ext_ID[indy]))) return(list()) # Highlight the external IDs that share the same Monty IDs list(data.frame(ext_ID=dframe$ext_ID[indy], ext_ID_db=dframe$ext_ID_db[indy], @@ -28,7 +71,7 @@ Add_EvSpat_Monty<-function(dframe){ dframe$ev_ISO3s[dframe$event_ID==ID] }) # Gimme gimme gimme - return(output) + return(output%>%distinct()) } Add_EvTemp_Monty<-function(dframe){ @@ -36,7 +79,7 @@ Add_EvTemp_Monty<-function(dframe){ dframe%>%group_by(event_ID)%>% reframe(ev_sdate=as.character(min(as.Date(ev_sdate))), ev_fdate=as.character(max(as.Date(ev_fdate))))%>% - dplyr::select(-event_ID)%>%distinct() + distinct()%>%dplyr::select(-event_ID) } Add_HazTax_Monty<-function(dframe){ @@ -56,13 +99,15 @@ Add_HazTax_Monty<-function(dframe){ } # A function to form the impact IDlinkage field -Add_ImIDlink_Monty<-function(dframe){ +Add_ImpIDlink_Monty<-function(dframe){ # Setup the other entries output<-dframe%>%dplyr::select(event_ID,imp_sub_ID)%>%distinct() # Generate all the elements of the dataset output$haz_sub_ID<-sapply(unique(dframe$imp_sub_ID),function(ID){ # Find the corresponding indices for this entry indy<-dframe$imp_sub_ID==ID + # check for no haz_sub_imp values + if(any(is.na(dframe$haz_sub_ID[indy]))) return(list()) # Highlight the external IDs that share the same Monty IDs list(dframe$haz_sub_ID[indy]) },simplify = T) @@ -72,7 +117,7 @@ Add_ImIDlink_Monty<-function(dframe){ return(output) } -Add_ImSpatID_Monty<-function(dframe){ +Add_ImpSpatID_Monty<-function(dframe){ output<-sapply(dframe$imp_spat_rowname,function(rnm){ list(data.frame( @@ -88,7 +133,7 @@ Add_ImSpatID_Monty<-function(dframe){ } -Add_ImSpatAll_Monty<-function(ID_linkage,spatial_info,source){ +Add_ImpSpatAll_Monty<-function(ID_linkage,spatial_info,source){ # multiple-entry rows: imp_spat_rowname,imp_spat_colname,imp_ISO3s,imp_spat_res,imp_spat_fileread lapply(ID_linkage$imp_sub_ID,function(ID){ # Set out only the entries that we need @@ -111,12 +156,103 @@ Add_ImSpatAll_Monty<-function(ID_linkage,spatial_info,source){ } +Add_hazIDlink_Monty<-function(dframe){ + # Setup the other entries + output<-dframe%>%dplyr::select(event_ID,haz_sub_ID)%>%distinct() + # Extract the external ID codes + output$haz_ext_IDs<-sapply(dframe$haz_sub_ID,function(ID){ + # Find the corresponding indices for this entry + indy<-dframe$haz_sub_ID==ID + # Check for no IDs + if(all(is.na(dframe$ext_ID[indy]))) return(list()) + # Highlight the external IDs that share the same Monty IDs + list(data.frame(ext_ID=dframe$ext_ID[indy], + ext_ID_db=dframe$ext_ID_db[indy], + ext_ID_org=dframe$ext_ID_org[indy])%>%distinct()) + },simplify = T) + # Let's keep this neat + names(output$haz_ext_IDs)<-NULL + # Output that bea-u-t + return(output) +} + + +Add_hazSpatAll_Monty<-function(ID_linkage,spatial_info,source){ + # multiple-entry rows: haz_spat_rowname,haz_spat_colname,haz_ISO3s,haz_spat_res,haz_spat_fileread + lapply(ID_linkage$haz_sub_ID,function(ID){ + # Set out only the entries that we need + indy<-ID_linkage$haz_sub_ID==ID + # Extract the easier elements + minout<-ID_linkage%>%filter(indy)%>% + dplyr::select(haz_spat_ID,haz_spat_fileloc)%>%distinct() + # Add the column and row specifier elements + minout$haz_spat_colname<-list(ID_linkage$haz_spat_colname[indy]) + minout$haz_spat_rowname<-list(ID_linkage$haz_spat_rowname[indy]) + # Output + output<-list() + output$ID_linkage<-minout + output$spatial_info<-spatial_info%>%filter(indy)%>%dplyr::select( + haz_ISO3s,haz_spat_covcode,haz_spat_res,haz_spat_resunits, + haz_spat_fileread,haz_spat_crs) + output$source<-source%>%filter(indy) + return(output) + }) +} + +# Function to left_join the s@#t out of Monty +OverlapMonty<-function(MontyA,MontyB){ + # Check which MontyA events lie generally within the window of MontyB + iiis<- + as.Date(MontyA$event_Level$temporal$ev_sdate)> + min(as.Date(MontyB$event_Level$temporal$ev_sdate))-10 & + as.Date(MontyA$event_Level$temporal$ev_fdate)> + max(as.Date(MontyB$event_Level$temporal$ev_fdate))+10 + # First reduce the crossover by hazard type (through the abbreviated hazard) + allMatch<-mclapply(which(iiis),function(i){ + # First check for any overlapping hazards + haz_Ab<-unique(MontyA$event_Level$allhaz_class[[i]]$all_hazs_Ab) + # Find all indices worth pursuing + indy<-vapply(1:length(MontyB$event_Level$allhaz_class),function(j) { + any(haz_Ab%in% + unique(MontyB$event_Level$allhaz_class[[j]]$all_hazs_Ab)) + },FUN.VALUE = logical(1)) + # Now let's check by overlapping dates + indy<-indy & + as.Date(MontyA$event_Level$temporal$ev_sdate[i]) > + as.Date(MontyB$event_Level$temporal$ev_sdate)-10 & + as.Date(MontyA$event_Level$temporal$ev_fdate[i]) < + as.Date(MontyB$event_Level$temporal$ev_fdate)+10 + # Now lets check for overlapping continents or countries + indy & + vapply(1:length(MontyB$event_Level$spatial$ev_ISO3s),function(j) { + any(convIso3Continent_alt(MontyA$event_Level$spatial$ev_ISO3s[[i]]) %in% + convIso3Continent_alt(MontyB$event_Level$spatial$ev_ISO3s[[j]])) | + any(MontyA$event_Level$spatial$ev_ISO3s[[i]] %in% + MontyB$event_Level$spatial$ev_ISO3s[[j]]) + },FUN.VALUE = logical(1)) %>% + which() + },mc.cores = 12) + + overlaps<-rep(F,length(iiis)) + overlaps[iiis]<-vapply(1:length(allMatch),function(k) any(allMatch[[k]]),logical(1)) + +} + + +MergeMonty<-function(MontyA,MontyB){ + Monty<-MontyA + Monty$event_Level$ID_linkage<-rbind(MontyA$event_Level$ID_linkage, + MontyB$event_Level$ID_linkage) + +} + + diff --git a/RCode/MainlyHazardData/GetGDACS.R b/RCode/MainlyHazardData/GetGDACS.R index 07d8f44..367b8b5 100755 --- a/RCode/MainlyHazardData/GetGDACS.R +++ b/RCode/MainlyHazardData/GetGDACS.R @@ -167,15 +167,15 @@ severitysplitter<-function(haz,txt){ # } if(haz=="EQ") { return(list(haz_maxvalue=txt$severity, - haz_units="unitsrichter")) + haz_maxunits="unitsrichter")) } else if(haz=="TC") { return(list(haz_maxvalue=txt$severity, - haz_units="unitskph")) + haz_maxunits="unitskph")) } else if(haz=="DR") { return(list(haz_maxvalue=txt$severity, - haz_units="unitskm2")) + haz_maxunits="unitskm2")) } else return(list(haz_maxvalue=NA_real_, - haz_units=NA_character_)) + haz_maxunits=NA_character_)) } GetIntMap<-function(hazard="EQ"){ @@ -227,6 +227,7 @@ FilterGDACS<-function(haz=NULL,syear=2016L,fyear=NULL,list_GDACS=NULL,red=F){ episodeid=rep(tmp$properties$episodeid,len), link=rep(tmp$properties$url$details,len), imp_ISO3s=dfct$ISO3, + ev_ISO3s=dfct$ISO3, country=dfct$country, ev_sdate=rep(as.Date(as.POSIXct(tmp$properties$fromdate),format = "%Y%m%d"),len), ev_fdate=rep(as.Date(as.POSIXct(tmp$properties$todate),format = "%Y%m%d"),len), @@ -237,7 +238,7 @@ FilterGDACS<-function(haz=NULL,syear=2016L,fyear=NULL,list_GDACS=NULL,red=F){ haz_src_org=tmp$properties$source, ext_IDs=rep(tmp$properties$glide,len), ext_ID_dbs="GLIDE", - ext_ID_orgs="Asian Disaster Reduction Center (ADRC)", + ext_ID_orgs="ADRC", geom_type=rep(tmp$geometry$type,len), cent_lon=rep(tmp$geometry$coordinates[1],len), cent_lat=rep(tmp$geometry$coordinates[2],len), @@ -266,34 +267,59 @@ GDACSHazards<-function(GDACS){ convGDACS_GCDB<-function(GDACS){ # Form the ID for the event GDACS$event_ID<-GetMonty_ID(GDACS) + # Make the dates the correct type + GDACS%<>%mutate_at(vars(ev_sdate,ev_fdate),as.character) # Date shifts - GDACS$imp_sdate<-GDACS$imp_unitdate<-GDACS$haz_sdate<-as.character(GDACS$ev_sdate) + GDACS$imp_sdate<-GDACS$imp_unitdate<-GDACS$haz_sdate<-GDACS$ev_sdate GDACS$imp_fdate<-GDACS$haz_fdate<-GDACS$ev_fdate # Add the continent, then remove the unnecesary layers - GDACS%<>%mutate(region=convIso3Continent(imp_ISO3s))%>% + GDACS%<>%mutate(region=convIso3Continent_alt(imp_ISO3s))%>% filter(!is.na(region)) # Add alertscore as the impact value - GDACS$imp_value<-GDACS$alertscore - GDACS$imp_cat<-"impother" - GDACS$imp_subcat<-"imptyperisk" - GDACS$imp_det<-"impdetalert" - GDACS$imp_type<-"imptypalert" - GDACS$imp_units<-"unitsgdacsalert" - # This estimate is modelled - GDACS$imp_est_type<-"esttype_model" - GDACS$haz_est_type<-"esttype_second" - # Organisation - GDACS$imp_src_db<-"GDACS" - GDACS$imp_src_org<-"European Commission" - GDACS$imp_src_orgtype<-"orgtyperio" + GDACS%<>%mutate( + imp_value=GDACS$alertscore, + imp_cat="impother", + imp_subcat="imptyperisk", + imp_det="impdetalert", + imp_type="imptypalert", + imp_units="unitsgdacsalert", + # This estimate is modelled + imp_est_type="esttype_model", + haz_est_type="esttype_second", + # Organisation + imp_src_db="GDACS", + imp_src_org="EC-JRC", + imp_src_orgtype="orgtyperio", + imp_spat_covcode="spat_polygon", + imp_spat_res=0, + imp_spat_resunits="adminlevel", + imp_spat_fileread="spatfstanshp", + imp_spat_crs="EPSG:4326", + imp_spat_srcorg="IFRC", + imp_spat_srcdb="GO-Maps", + imp_spat_URL="https://go-user-library.ifrc.org/maps", + imp_spat_ID=NA_character_, + haz_ISO3s=imp_ISO3s, + haz_spat_covcode="spat_polygon", + haz_spat_res=NA_real_, + haz_spat_resunits="spatresother", + haz_spat_fileread="spatfstanshp", + haz_spat_fileloc=src_URL, + haz_spat_crs="EPSG:4326", + haz_spat_srcorg="EC-JRC", + haz_spat_srcdb="GDACS", + haz_spat_URL=src_URL) + colnames(GDACS)[colnames(GDACS)=="src_URL"]<-"haz_src_URL" colnames(GDACS)[colnames(GDACS)=="link"]<-"imp_src_URL" # Convert to the UNDRR-ISC hazard taxonomy GDACS%<>%GDACSHazards() # Create the impact and hazard sub-ID for the speciic level, not event level GDACS%<>%GetGCDB_impID() + GDACS$imp_spat_ID<-GetGCDB_imp_spatID(GDACS) # Now for hazards GDACS%<>%GetGCDB_hazID() + GDACS$haz_spat_ID<-GetGCDB_haz_spatID(GDACS) # Add the extra columns to make it officially a GCDB object GDACS%>%AddEmptyColImp()%>%distinct() } @@ -305,6 +331,179 @@ GetGDACS_GCDB<-function(){ GDACS%>%convGDACS_GCDB() } +convGDACS_Monty<-function(GDACS){ + # Get rid of repeated entries + GDACS%<>%distinct(imp_sub_ID,.keep_all = TRUE) + # Extract the Monty JSON schema template + gdacsMonty<-jsonlite::fromJSON("./Taxonomies/Montandon_JSON-Example.json") + #@@@@@ Impact-level data @@@@@# + # IDs + ID_linkage<-Add_ImpIDlink_Monty( + GDACS%>% + dplyr::select(event_ID,imp_sub_ID,haz_sub_ID) + ) + # Sources for impact data + source<-GDACS%>%dplyr::select(imp_src_db,imp_src_URL,imp_src_org) + # impact estimates + impact_estimate<-GDACS%>% + dplyr::select(imp_det,imp_value,imp_type,imp_units,imp_est_type,imp_unitdate) + # Add temporal information + temporal<-GDACS%>%dplyr::select(imp_sdate,imp_fdate) + # Spatial data relevant to the impact estimates + # multiple-entry rows: imp_spat_rowname,imp_spat_colname,imp_ISO3s,imp_spat_res,imp_spat_fileread + spatial<-Add_ImpSpatAll_Monty( + ID_linkage=data.frame( + imp_sub_ID=GDACS$imp_sub_ID, + imp_spat_ID="GO-ADM0-World-shp", + imp_spat_fileloc="https://go-user-library.ifrc.org/maps", + imp_spat_colname="iso3", + imp_spat_rowname=GDACS$imp_ISO3s + ), + spatial_info=GDACS%>%dplyr::select( + imp_ISO3s, + imp_spat_covcode, + imp_spat_res, + imp_spat_resunits, + imp_spat_fileread, + imp_spat_crs + ), + source=GDACS%>%dplyr::select( + imp_spat_srcdb, + imp_spat_URL, + imp_spat_srcorg + ) + ) + + # Gather it all and store it in the template! + # (I know this is hideous, but I don't understand how JSON files can have lists that are also S3 data.frames) + gdacsMonty$impact_Data<-data.frame(imp_sub_ID=GDACS$imp_sub_ID) + gdacsMonty$impact_Data$ID_linkage=ID_linkage + gdacsMonty$impact_Data$source=source + gdacsMonty$impact_Data$impact_estimate=impact_estimate + gdacsMonty$impact_Data$temporal=temporal + gdacsMonty$impact_Data$spatial=spatial + gdacsMonty$impact_Data$imp_sub_ID<-NULL + + #@@@@@ Event-level data @@@@@# + # IDs + ID_linkage<-Add_EvIDlink_Monty( + GDACS%>%dplyr::select(event_ID, ev_name, ext_IDs,ext_ID_dbs,ext_ID_orgs)%>% + rename(ext_ID=ext_IDs,ext_ID_db=ext_ID_dbs,ext_ID_org=ext_ID_orgs) + ) + # Spatial + spatial<-Add_EvSpat_Monty( + GDACS%>%dplyr::select(event_ID,imp_ISO3s,location)%>% + rename(ev_ISO3s=imp_ISO3s,gen_location=location) + ) + # temporal + temporal<-Add_EvTemp_Monty( + GDACS%>%dplyr::select(event_ID,imp_sdate,imp_fdate,ev_sdate,ev_fdate) + ) + # Hazards + allhaz_class<-Add_HazTax_Monty( + GDACS%>%dplyr::select(event_ID, haz_Ab, haz_spec) + ) + # Gather it all and store it in the template! + gdacsMonty$event_Level<-data.frame(ev=ID_linkage$event_ID) + gdacsMonty$event_Level$ID_linkage<-ID_linkage + gdacsMonty$event_Level$temporal<-temporal + gdacsMonty$event_Level$spatial<-spatial + gdacsMonty$event_Level$allhaz_class<-allhaz_class + gdacsMonty$event_Level$ev<-NULL + + + #@@@@@ Hazard-level data @@@@@# + GDACS%<>%distinct(haz_sub_ID,.keep_all = T) + + # Nothing to put here as we haven't linked any hazard data yet + ID_linkage<-Add_hazIDlink_Monty( + GDACS%>% + dplyr::select(event_ID,haz_sub_ID,ext_IDs,ext_ID_dbs,ext_ID_orgs)%>% + rename(ext_ID=ext_IDs,ext_ID_db=ext_ID_dbs,ext_ID_org=ext_ID_orgs) + ) + # Sources for impact data + source<-GDACS%>%dplyr::select(haz_src_db,haz_src_URL,haz_src_org)%>%mutate(haz_src_db="GDACS") + # hazard intensity estimates + hazard_detail<-GDACS%>% + dplyr::select(haz_maxvalue,haz_maxunits,haz_est_type) + hazard_detail$concur_haz<-lapply(1:nrow(hazard_detail),function(i) list()) + # hazard taxonomy + hazard_taxonomy<-Add_HazTax_Monty( + GDACS%>%dplyr::select(haz_sub_ID, haz_Ab, haz_spec)%>%rename(event_ID=haz_sub_ID) + ) + # Add temporal information + temporal<-GDACS%>%dplyr::select(haz_sdate,haz_fdate) + # Spatial data relevant to the impact estimates + # multiple-entry rows: imp_spat_rowname,imp_spat_colname,imp_ISO3s,imp_spat_res,imp_spat_fileread + GDACS$haz_spat_fileloc<-GDACS$haz_spat_URL + # Spatial instance + spatial<-Add_hazSpatAll_Monty( + ID_linkage=GDACS%>%dplyr::select( + haz_sub_ID, + haz_spat_ID, + haz_spat_fileloc, + haz_spat_colname, + haz_spat_rowname + ), + spatial_info=GDACS%>%dplyr::select( + haz_ISO3s, + haz_spat_covcode, + haz_spat_res, + haz_spat_resunits, + haz_spat_fileread, + haz_spat_crs + ), + source=GDACS%>%dplyr::select( + haz_spat_srcdb, + haz_spat_URL, + haz_spat_srcorg + ) + ) + + # Gather it all and store it in the template! + # (I know this is hideous, but I don't understand how JSON files can have lists that are also S3 data.frames) + gdacsMonty$hazard_Data<-data.frame(imp_sub_ID=GDACS$imp_sub_ID) + gdacsMonty$hazard_Data$ID_linkage=ID_linkage + gdacsMonty$hazard_Data$source=source + gdacsMonty$hazard_Data$hazard_detail=hazard_detail + gdacsMonty$hazard_Data$hazard_taxonomy=hazard_taxonomy + gdacsMonty$hazard_Data$temporal=temporal + gdacsMonty$hazard_Data$spatial=spatial + gdacsMonty$hazard_Data$imp_sub_ID<-NULL + + + #@@@@@ Source Data In Taxonomy Field @@@@@# + gdacsMonty$taxonomies$src_info<-data.frame( + src_org_code="EC-JRC", + src_org_lab="European Commission - Joint Research Center", + src_org_typecode="orgtyperio", + src_org_typelab="Regional Intergovernmental Organisation", + src_org_email="coordination@gdacs.org", + src_db_code="GDACS", + src_db_lab="Global Disaster Alert and Coordination System (GDACS)", + src_db_attr="mediator", + src_db_lic="unknown", + src_db_URL="www.gdacs.org", + src_addinfo="" + ) + + # Write it out just for keep-sake + write(jsonlite::toJSON(gdacsMonty,pretty = T,auto_unbox=T), + "./CleanedData/MostlyHazardData/GDACS/GDACS_20231119.json") + + return(gdacsMonty) +} + + + + + + + + + + + GetGDACSalertscore<-function(dfGDACS=NULL,haz,bbox,sdater,fdater=NULL,isos=NULL){ if(any(is.null(c(haz,sdater,bbox)))) stop("Please provide hazard type, start date and bounding box to extract GDACS alertscore") diff --git a/RCode/MainlyImpactData/GetIFRCappeal.R b/RCode/MainlyImpactData/GetIFRCappeal.R index 8444a3e..4339869 100644 --- a/RCode/MainlyImpactData/GetIFRCappeal.R +++ b/RCode/MainlyImpactData/GetIFRCappeal.R @@ -130,7 +130,7 @@ CleanGO_app<-function(appeal){ # Create an impact-specific ID appeal%<>%GetGCDB_impID() - appeal$imp_spat_ID<-GetGCDB_spatID(appeal) + appeal$imp_spat_ID<-GetGCDB_imp_spatID(appeal) # Add missing columns & reorder the dataframe to fit imp_GCDB object # appeal%<>%AddEmptyColImp() @@ -220,7 +220,7 @@ CleanGO_field<-function(fieldr){ # Create an impact-specific ID fieldr%<>%GetGCDB_impID() - fieldr$imp_spat_ID<-GetGCDB_spatID(fieldr) + fieldr$imp_spat_ID<-GetGCDB_imp_spatID(fieldr) # Add missing columns & reorder the dataframe to fit imp_GCDB object fieldr%<>%AddEmptyColImp() @@ -284,7 +284,7 @@ convGOApp_Monty<-function(appeal){ appMonty<-jsonlite::fromJSON("./Taxonomies/Montandon_JSON-Example.json") #@@@@@ Impact-level data @@@@@# # IDs - ID_linkage<-Add_ImIDlink_Monty( + ID_linkage<-Add_ImpIDlink_Monty( appeal%>%mutate(haz_sub_ID=NA_character_)%>% dplyr::select(event_ID,imp_sub_ID,haz_sub_ID) ) @@ -299,7 +299,7 @@ convGOApp_Monty<-function(appeal){ temporal<-appeal%>%dplyr::select(imp_sdate,imp_fdate) # Spatial data relevant to the impact estimates # multiple-entry rows: imp_spat_rowname,imp_spat_colname,imp_ISO3s,imp_spat_res,imp_spat_fileread - spatial<-Add_ImSpatAll_Monty( + spatial<-Add_ImpSpatAll_Monty( ID_linkage=data.frame( imp_sub_ID=appeal$imp_sub_ID, imp_spat_ID="GO-ADM0-World-shp", diff --git a/RCode/Other/GetGoogleEarthEngineData.R b/RCode/Other/GetGoogleEarthEngineData.R index cbffe56..8833fa0 100644 --- a/RCode/Other/GetGoogleEarthEngineData.R +++ b/RCode/Other/GetGoogleEarthEngineData.R @@ -8,7 +8,7 @@ SetupGEE<-function(){ install.packages("rgee") library(rgee) - ee_install() + ee_install(py_env = "/home/hamishwp/anaconda3/bin/python3") Sys.setenv("EARTHENGINE_GCLOUD" = gee_bin) py_install( "earthengine-api==0.1.277", "rgee") ee_check() @@ -24,3 +24,19 @@ SetupGEE<-function(){ return(T) } + + +# library(reticulate) +# # Check which version of python3 you're using with +# python3 --version +# # Let's say we use python3.11, then we run: +# sudo apt-get install python3.11-venv +# use_python("/usr/bin/python3") +# # Then run +# py_install( "earthengine-api==0.1.277", "rgee") +# use_python("/home/hamishwp/anaconda3/bin/python3") +# rgee::ee_install_set_pyenv(py_path = "/home/hamishwp/anaconda3/bin/python3", py_env="rgee") + +# Sys.which("python3") +# library(reticulate) +# reticulate::use_python("/home/hamishwp/.local/share/r-miniconda/envs/r-reticulate/bin/python") \ No newline at end of file