## ---------------------------
##
## Script name: 02_harmonize_data.R
##
## Purpose of script: Harmonize and merge data 
## Author: Giovenale Moirano
## Date Created: 2025-01-24
## Email: giovenale.moirano@bsc.es
##
## ---------------------------

## load up the packages
packages <- c("ggplot2", "dplyr", "tidyr", "lubridate", "sf","scales")
lapply(packages, require, character.only = TRUE)


#Load Climate Data 
climate <- read.csv("data/laos/laos_climate_2015-2023.csv")
head(climate)

#Process climate Data 
climate <- climate %>% rename(province = orgunit_name,
                              province_id = orgunit_id)

climate <- climate %>% 
  group_by(province,month,year) %>% 
  summarise(tasmax = mean (tasmax),
            tasmin = mean(tasmin),
            tas = mean(tas),
            prlr = sum (prlr))


climate$date <- paste0(climate$year, "-", sprintf("%02d", climate$month), "-01")

#Load Dengue Data 
dengue <- read.csv("data/laos/laos_dengue_2015-2023.csv")
View(dengue)

#Population Data 
pop <- read.csv("data/laos/laos_population_2022.csv")


#Merge dataset 
harmonized_data <- dengue %>% 
  left_join(pop, by = c("province")) %>% 
  left_join(climate, by = c("province", "date", "month","year")) 
     
gplimpse(harmonized_data)  

write.csv(harmonized_data, file = "data/laos_harmonized/harmonized_data.csv")
