#---------------------------------------------------------
# Scriptname: 1.From_Cases_To_Case_Crossover.R
#
# Purpose of script:
#
# 1) Create a case-crossover dataset from list of cases.
# 2) Merge the case-crossover dataset with meteorological factors
#    lagged up to 8 weeks.
#
#  ! "sim_data.csv" is a simulated dataset including cases   !
#  ! from 4 different geographical areas (NUTS_ID).          !
#
# Author(s): Giovenale Moirano
#
# Date Created: 5 May 2024
#
# Email: giovenale.moirano@bsc.es
#
#---------------------------------------------------------
# 1. Clean Environment & set Directory.
#---------------------------------------------------------

rm(list = ls())

# setwd("mydirectory/Code_wnv_cc")

#---------------------------------------------------------
# 2. Loadpackages.
#---------------------------------------------------------

packages <- c(
  "ggmap", "sf", "zoo", "lubridate",
  "dplyr", "terra", "readxl", "Hmisc"
  )
install.packages(setdiff(packages, rownames(installed.packages())))
lapply(packages, require, character.only = TRUE)

# Source function that create a case crossover dataset from
# linelist of cases

source("functions/tscc_sampling.R")

#---------------------------------------------------------
# 3. Load Simulated Cases.
#---------------------------------------------------------

data <- read.csv(file = "data/3.sim_cases.csv")
head(data)

#------------------------------------------------------------
# 4. Create a case-crossover dataset.
#------------------------------------------------------------

data$ID <- seq(1 : length(data$date))
data$ID <- paste0("ID", data$ID)
head(data)

# 4a. Create a space-time-stratified Case Crossover dataset with the
# 'tscc_sampling' function.

db_cc <- tscc_sampling(data = data, dates = "date")
head(db_cc)

# 4b. Merge the case-crossover dataset with NUTS_ID of orginal cases.
data <- db_cc %>%
  left_join(data, by = "ID")

# 4c. Now "data" is an individual case-crossover dataset.

head(data)

#------------------------------------------------------------
# 5. For each case and control sampling date, compute the number of days
#   since the 2013-12-31. This is done to combine cases and control
#   periods with time-series of meteorological data that starts
#   on 2014-01-01.
#------------------------------------------------------------

data$ref_date <- as.Date("2013-12-31")
data$sampling_date <- as.Date(data$sampling_date)
data$index <- data$sampling_date - data$ref_date
class(data$index)
data$index <- as.numeric(data$index)
head(data)

# -------------------------------------------------------------
# 6a. Load Whole history of Temperature Exposure for Each NUTS_ID.
#--------------------------------------------------------------

data_temp <- read.csv(file = "data/4.temperatures.csv")
class(data_temp)
dim(data_temp) # 4 NUTS3

#------------------------------------------------------------
# 7a. Merge "data" and "data_temp" into a temporary file
#   ! Remeber that "data_temp" contains all exposures
#   ! at daily level from 1.1.2014 to 31.12.2022
#------------------------------------------------------------

temp_data <- merge(data, data_temp, by = "NUTS_ID", all.x = T)
names(temp_data)
dim(temp_data)

#------------------------------------------------------------
# 8a. Subset Exposure history in the 8 weeks (56 days) preceding
#    the case or control sampling date to a list.
#    ! At the moment you need to check at which column of the
#    ! merged file the exposure matrix begins
#------------------------------------------------------------

names(temp_data)[1:20] # Exposure matrix starts at column 8

exp_fin <- NULL
for (i in 1:dim(temp_data)[1]) {
  exp_fin[[i]] <- temp_data[i, c(1:7, (7 + temp_data$index[i] - 1):(7 + temp_data$index[i] - 56))]
}

#------------------------------------------------------------
# 9a. With the list of exposures for cases and control fill a
#    new database called df
#------------------------------------------------------------

# to a matrix
mat <- matrix(
  ncol = length(exp_fin[[1]]),
  nrow = length(exp_fin),
  unlist(exp_fin),
  byrow = TRUE
)

# convert mat to a data.frame

df <- as.data.frame(mat)

# change names
names(df) <- names(exp_fin[[1]])
name_to_be_replaced <- c(names(df))
name_to_be_replaced[substr(name_to_be_replaced, 1, 1) == "X"] <- paste0("tg_lag", c(1:56))
names(df) <- name_to_be_replaced
names(df)

#------------------------------------------------------------
# 10a. Re-express measurment unit to C° (x-273)
#------------------------------------------------------------

df[, which(substr(names(df), 1, 2) == "tg")] <- apply(df[, which(substr(names(df), 1, 2) == "tg")], FUN = as.numeric, MARGIN = 2)
df[, which(substr(names(df), 1, 2) == "tg")] <- apply(df[, which(substr(names(df), 1, 2) == "tg")], function(x) {
  x - 273
  }, MARGIN = 2)

#------------------------------------------------------------
# 6b. Load Whole history of Precipitation Exposure for Each NUTS_ID
#------------------------------------------------------------
data_prep <- read.csv(file = "data/5.precipitation.csv")
dim(data_prep)

#------------------------------------------------------------
# 7b. Merge "data" and "data_prep" into a temporary file
#   ! Remeber that "data_prep" contains all exposures
#   ! at daily level from 1.1.2013 to 31.12.2018
#------------------------------------------------------------

temp_data2 <- merge(data, data_prep, by = "NUTS_ID", all.x = T)

#------------------------------------------------------------
# 8b. Subset Exposure history in the 8 weeks (56 days) preceding
#    case or control sampling dates to a list.
#    ! At the moment you need to check at which column of the
#    ! merged file the exposure matrix begin
#------------------------------------------------------------

names(temp_data2)[1:20] # Exposure matrix start at column 8.

exp_fin2 <- NULL

for (i in 1:dim(temp_data2)[1]) {
  exp_fin2[[i]] <- temp_data2[i, c(1:7, (7 + temp_data2$index[i] - 1):(7 + temp_data2$index[i] - 56))]
}

#------------------------------------------------------------
# 9b. With the list of prep exposures for cases and control fill a
#    new database called db2
#------------------------------------------------------------
# to a matrix 

mat2 <- matrix(
  ncol = length(exp_fin2[[1]]),
  nrow = length(exp_fin2),
  unlist(exp_fin2), byrow = TRUE
)
dim(mat2)

# convert to a data.frame
df2 <- as.data.frame(mat2)

# names
names(df2) <- names(exp_fin2[[1]])
name_to_be_replaced2 <- c(names(df2))
name_to_be_replaced2[substr(name_to_be_replaced2, 1, 1) == "X"] <- paste0("prp_lag", c(1:56))
names(df2) <- name_to_be_replaced2
names(df2)

#------------------------------------------------------------
# 10b. Re-express measurment unit to mm (x*60*60*24*1000)
#------------------------------------------------------------

df2[, which(substr(names(df2), 1, 2) == "pr")] <- apply(df2[, which(substr(names(df2), 1, 2) == "pr")], FUN = as.numeric, MARGIN = 2)
df2[, which(substr(names(df2), 1, 2) == "pr")] <- apply(df2[, which(substr(names(df2), 1, 2) == "pr")], function(x) {
  x * 60 * 60 * 24 * 1000
}, MARGIN = 2)

#------------------------------------------------------------
# 11. Merge df1(case_crossover_temp) e df2 (case_crossover_prep).
#------------------------------------------------------------

CC_data <- merge(df, df2, by = c(names(df)[1:7]))
dim(CC_data)
ftable(CC_data$caco)
head(CC_data)

#------------------------------------------------------------
# 12. Save the final case-crossover dataset for analyses.
#------------------------------------------------------------

write.csv(CC_data,
  file = "data/6.case_cross_data.csv",
  row.names = FALSE
)
