diff --git a/drought/skill-assessment/drought_catalog/.gitignore b/drought/skill-assessment/drought_catalog/.gitignore new file mode 100644 index 0000000..ebb1893 --- /dev/null +++ b/drought/skill-assessment/drought_catalog/.gitignore @@ -0,0 +1,8 @@ +.RData +.Rhistory +*.csv +*.png +*.shp +*.dbf +*.prj +*.shx \ No newline at end of file diff --git a/drought/skill-assessment/drought_catalog/impactcatalogue_drought.r b/drought/skill-assessment/drought_catalog/impactcatalogue_drought.r new file mode 100644 index 0000000..7308aa2 --- /dev/null +++ b/drought/skill-assessment/drought_catalog/impactcatalogue_drought.r @@ -0,0 +1,130 @@ +rm(list=ls()) +library(tidyverse) +library(lubridate) +library(sf) +library(leaflet) +library(readr) +library(httr) +library(sp) +library(lubridate) +library(ggplot2) +library(ggthemes) +library(cowplot) + + +onedrive_folder <- "c:/Users/pphung/Rode Kruis" + + +## READ ADMIN AND LIVELIHOODZONE BOUNDARIES ---- +zwe_lhz <- st_read(sprintf('%s/510 - Data preparedness and IBF - [PRJ] FbF - Zimbabwe - Danish Red Cross/3. Data - Hazard exposure, vulnerability/zwe_livelihoodzones/ZW_LHZ_2011/ZW_LHZ_2011_fixed.shp', + onedrive_folder)) +zwe <- st_read(sprintf('%s/510 - Data preparedness and IBF - [PRJ] FbF - Zimbabwe - Danish Red Cross/3. Data - Hazard exposure, vulnerability/Admin/zwe_admbnda_adm2_zimstat_ocha_20180911/zwe_admbnda_adm2_zimstat_ocha_20180911.shp', + onedrive_folder)) +zwe <- zwe %>% + dplyr::select(ADM1_PCODE,ADM2_PCODE,ADM0_EN) +zwe_lhz <- zwe_lhz %>% + dplyr::mutate(ADM0_EN=COUNTRY) %>% + dplyr::select(LZCODE) + +admin_all <- st_intersection(zwe, zwe_lhz) %>% + dplyr::select(ADM1_PCODE,ADM2_PCODE,LZCODE) +st_geometry(admin_all) <- NULL +# plot(admin_all) + + +## LOAD AND CALCULATE CROP YIELD ANOMALY [PREDICTANT] ---- +months <- c(1:3,9:12) + +yield <- read.csv(sprintf('%s/510 - Data preparedness and IBF - [PRJ] FbF - Zimbabwe - Danish Red Cross/3. Data - Hazard exposure, vulnerability/zwe_cropyield/all_yield_maize_major.csv',onedrive_folder)) + +mean_sd <- yield %>% + group_by(pcode) %>% + summarise(mean = mean(yield,na.rm=TRUE), sd = sd(yield,na.rm=TRUE)) # calculate mean and standard deviation along the year axis +yield <- yield %>% + left_join(mean_sd,by='pcode') +yield$yield_anomaly <- (yield$yield-yield$mean)/yield$sd # calculate CYA +yield = subset(yield, select=-c(mean,sd)) + +df_all <- admin_all %>% # create joint table with adm2, lhz and yield anomaly + left_join(yield,by=c('LZCODE'='pcode')) + +df_all <- df_all[rep(seq_len(nrow(df_all)), each = 7), ] %>% + group_by(year,LZCODE,ADM2_PCODE) %>% + mutate(month = months) +df_all <- df_all[,c(1,2,3,4,7,5,6)] # rearrange column "month" + +shift <- function(x, n){ # function to shift row up in a column + c(x[-(seq(n))], rep(NA, n)) +} +df_all$yield <- shift(df_all$yield, 4) # shift yield and yield anomaly so that they start from Sept a year before +df_all$yield_anomaly <- shift(df_all$yield_anomaly, 4) + + + + +## LOAD INDICATORS [PREDICTORS] ---- + + +## ENSO current ---- + +enso_zwe <- read.csv(sprintf('%s/510 - Data preparedness and IBF - [RD] Impact-based forecasting/General_Data/elnino/ENSO.csv',onedrive_folder)) %>% + # mutate(year=year(Year)) %>% + dplyr::select(c(Year,OND)) # choose between "OND"/"NDJ" to fit the impact catalogue +colnames(enso_zwe)[colnames(enso_zwe) == 'OND'] <- 'enso_cur' + +df_all <- df_all %>% # join to a big table + left_join(enso_zwe, by=c('year'='Year')) +df_all$enso_cur[which(df_all$month %in% c(1:3))] <- NA # remove enso values in JFM + + + +## ENSO forecast ---- + + + + + +## SPI ---- + +spi_zwe <- read.csv(sprintf('%s/510 - Data preparedness and IBF - [PRJ] FbF - Zimbabwe - Danish Red Cross/3. Data - Hazard exposure, vulnerability/zwe_spi/zwe_spi3.csv',onedrive_folder))%>% + mutate(date=ymd(as.Date(date))) %>% + mutate(year=year(date)) + +df_all <- df_all %>% # join to a big table + left_join(spi_zwe, by=c('LZCODE'='livelihoodzone','year','month')) %>% + dplyr::select(-c(X,date)) + + + + +## SPEI ---- + +spei_zwe <- read.csv(sprintf('%s/510 - Data preparedness and IBF - [PRJ] FbF - Zimbabwe - Danish Red Cross/3. Data - Hazard exposure, vulnerability/zwe_spi/zwe_spei3.csv',onedrive_folder))%>% + mutate(date=ymd(as.Date(date))) %>% + mutate(year=year(date)) + +df_all <- df_all %>% # join to a big table + left_join(spei_zwe, by=c('LZCODE'='livelihoodzone','year','month')) %>% + dplyr::select(-c(X,date)) +# write.csv(df_all, './output/drought_catalog.csv') + + + + +## Soil moisture ---- + + + + + + +## VCI ---- + + + + + +## Cumulative precipitation ---- + + + diff --git a/drought/skill-assessment/drought_catalog/readme.md b/drought/skill-assessment/drought_catalog/readme.md new file mode 100644 index 0000000..e54ddf6 --- /dev/null +++ b/drought/skill-assessment/drought_catalog/readme.md @@ -0,0 +1,27 @@ +# Script for creating drought impact catalog + +## `impactcatalogue_drought.R` + +The script is for arranging predictants (yield, yield anomaly - for maize now) and predictors (drought indicators) per admin2 into a single catalog. + +The arrangement is based on [this excel sheet on Teams ][https://rodekruis.sharepoint.com/sites/510-CRAVK-510/_layouts/15/guestaccess.aspx?guestaccesstoken=5DtgHKRzRrHCPDqpWQl6D1XAJqVfimV3fujFdueQv%2F0%3D&docid=2_027fd1d81d676404d914d155ef55fd16d&rev=1&e=pBm3hv] + +The output impact catalog is saved as csv . + +### Data input: + +The script is now for Zimbabwe, data can be found in the FBF Zimbabwe channel, General_data of IBF channel and can be loaded in easily if synced through OneDrive from the IBF channel in Teams. + +At the moment, the inputs are `livelyhood zones`, `admin boundaries`, `crop yield` which are as base. Indicators are now included `ENSO`, `SPI3`, `SPEI3`. + +For new indicators, the expected format is similar to one of DMP: in csv, with columns: +- adm2 pcode and/ or livelihoodzone pcode as same as those in above files +- date (year, month) +- values for the indicators + + +### Usage: + +Sections `READ ADMIN AND LIVELIHOODZONE BOUNDARIES` and `LOAD AND CALCULATE CROP YIELD ANOMALY [PREDICTANT]` are fixed to define the data frame. Indicators are loaded and modified manually as following: +- Load the indicator from your synced OneDrive `[indicatorname]_[countryname]` +- Join to the combined table the indicator based on (1) `livelyhood zones` or `admin boundaries`; (2) `year` and `month` (if `month` available). \ No newline at end of file