rm(list=ls())
source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Anomalies/Anomalies.R")

recipe_file <- "recipe_product.yml"
recipe <- prepare_outputs(recipe_file)

biases <- list()
pvals <- list()

point <- c(lon = -2, lat = 42)
time_step <- 1
years <- 1993:2016
for (i in 1:12) {
  recipe$Analysis$Time$sdate <- paste0(sprintf("%02d", i), '01')
  data <- Loading(recipe)
  data <- Units(recipe = recipe, data = data)
  PlotEquiMap(MeanDims(data$hcst$data, c("syear", "ensemble"))[1,1,1,1,1,,],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-20, 20, 5),
              toptitle = "SEAS5.1 tas 1993-2016 January Mean ft1", units = "C")

  bias <- Bias(data$hcst$data, data$obs$data, time_dim = 'syear', memb_dim = 'ensemble')
  PlotEquiMap(bias[1,1,1,1,1,,],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-5, 5, 1), sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January Bias ft1",
              units = "C")
  PlotEquiMap(MeanDims(data$hcst$data, c("syear", "ensemble"))[1,1,1,1,1,,],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(-20, 20, 5),
              boxlim = c(point[1] - .5, point[2] - .5,
                         point[1] + .5, point[2] + .5),
              toptitle = "SEAS5.1 tas 1993-2016 January Mean ft1", units = "C")

  hcst_point <- Subset(data$hcst$data,
                       along = c('longitude', 'latitude', 'time'),
                       indices = list(which(data$hcst$coords$longitude == 
                                            point[1]),
                                      which(data$hcst$coords$latitude == 
                                            point[2]), time_step), drop = T)
  obs_point <- Subset(data$obs$data,
                       along = c('longitude', 'latitude', 'time'),
                       indices = list(which(data$hcst$coords$longitude == 
                                            point[1]),
                                      which(data$hcst$coords$latitude == 
                                            point[2]), time_step), drop = T)
  source("https://earth.bsc.es/gitlab/es/sunset/-/raw/master/modules/Indices/R/plot_deterministic_forecast.R")
  plot_deterministic_forecast(obs_point, fcst = hcst_point,
                              time_dim = 'syear', member_dim = 'ensemble',
                              style = 'boxplot', fileout = 'test.png')

for (year in data$hcst$coords$syear) {
  hist(hcst_point[year,], main = paste("sdate:", years[year]),
       xlab = "tas (C)", ylim = c(0, 16), xlim = c(-1,8),
       breaks = seq(-1,8, 1))
  points(obs_point[year], 0.7, col = 'red', cex = 4)
  points(obs_point[year], 0.7, col = 'red', cex = 4, pch = 3)
  abline(v = mean(hcst_point[year,]), col = 'darkblue', lwd = 2, lty = 2)
  abline(v = median(hcst_point[year,]), col = 'green', lwd = 2, lty = 2)
  legend('topleft', c(paste('Mean', round(mean(hcst_point[year,]),2)),
                      paste('Median', round(median(hcst_point[year,]),2)),
                      'Individual members',
                      paste('Obs', round(obs_point[year], 2))),
         bty = 'n', lty = c(2,2,NA,1), 
         col = c('darkblue', 'green', 'black', 'red'), pch = c(NA, NA, 1, 1))
  points(hcst_point[year,], y = rep(1,25))
  browser()
}

# Bias Adjustment:
  hcst_adj <- CST_BiasCorrection(exp = data$hcst,
                                 obs = data$obs,
                                 sdate_dim = 'syear',
                                 memb_dim = 'ensemble')
  hcst_point <- Subset(hcst_adj$data,
                       along = c('longitude', 'latitude', 'time'),
                       indices = list(which(data$hcst$coords$longitude ==
                                            point[1]),
                                      which(data$hcst$coords$latitude ==
                                            point[2]), time_step), drop = T)
  
  plot_deterministic_forecast(obs_point, fcst = hcst_point,
                              time_dim = 'syear', member_dim = 'ensemble',
                              style = 'boxplot', fileout = 'test.png')

if (err == TRUE) {
# define the error range
clim <- mean(obs_point)
sup_clim <- clim + 1
inf_clim <- clim - 1 

} else {
clim <- mean(obs_point)

# define the probability range
hist(obs_point)
abline(v = clim)
prob <- 60
nevents <- ceiling(length(obs_point)/100*prob)
pos <- sapply(sort(abs(obs_point - clim))[1:nevents],
              function(x) { which(x ==abs(obs_point - clim))})
obs_point[pos]
points(obs_point, rep(0.5, length(obs_point)), pch = 16)
points(obs_point[pos], rep(0.5, nevents), pch = 16, col = 'red')
sup_clim <- max(obs_point[pos])
inf_clim <- min(obs_point[pos])
}
hits <- 0
res <- NULL
for (year in data$hcst$coords$syear) {
png(paste0("../hist/cat_prob_hist_SEAS51_tas_s", years[year], "ftime1.png"),
    width = 700, height = 700)
 hist(hcst_point[year,], main = paste("sdate:", years[year]),
       xlab = "tas (C)", ylim = c(0, 16), xlim = c(1,9),
       breaks = seq(1,9, 1))
  points(obs_point[year], 0.7, col = 'red', cex = 4)
  points(obs_point[year], 0.7, col = 'red', cex = 4, pch = 3)
  abline(v = clim, col = 'purple', lwd = 2, lty = 2)
  abline(v = mean(hcst_point[year,]), col = 'darkblue', lwd = 2, lty = 2)
  abline(v = median(hcst_point[year,]), col = 'green', lwd = 2, lty = 2)
  legend('topleft', c(paste('Mean', round(mean(hcst_point[year,]),2)),
                      paste('Median', round(median(hcst_point[year,]),2)),
                      paste('Clim', round(clim, 2)),
                      'Individual members',
                      paste('Obs', round(obs_point[year], 2))),
         bty = 'n', lty = c(2,2,2,NA,1),
         col = c('darkblue', 'green', 'purple', 'black', 'red'),
         pch = c(NA, NA, NA, 1, 1))
  points(hcst_point[year,], y = rep(1,25))

  nevents <- ceiling(length(hcst_point[year,])/100*prob)
  pos <- sapply(sort(abs(hcst_point[year,] - mean(hcst_point[year,])))[1:nevents],
              function(x) {
                which(x == abs(hcst_point[year,] - mean(hcst_point[year,])))})
  points(hcst_point[year,pos], rep(1,length(pos)), col = 'red')
  if (err){ 
    sup_mean <- mean(hcst_point[year,]) + 1
    inf_mean <- mean(hcst_point[year,]) - 1
  } else {
    sup_mean <- max(hcst_point[year,pos])
    inf_mean <- min(hcst_point[year,pos])
   # hist(hcst_point[year,], breaks = c(2,inf_mean-0.01, sup_mean +0.01, 9), freq = T) 
  }
  n_memb <- length(which(hcst_point[year,] >= inf_mean & 
                         hcst_point[year,] <= sup_mean))
  perc_mean <- n_memb*100/25
  text(8.5, 15, paste("+/- 1 Mean", round(perc_mean, 1), "%")) 
  hit_mean <- ifelse(obs_point[year] >= inf_mean & 
                     obs_point[year] <= sup_mean, 1, 0)
  hits <- hits + hit_mean
  print(hits)
  pos <- sapply(sort(abs(hcst_point[year,] - median(hcst_point[year,])))[1:nevents],
              function(x) {
                which(x == abs(hcst_point[year,] - median(hcst_point[year,])))})
  points(hcst_point[year,pos], rep(1,15), col = 'red')

  if (err) {
    sup_median <- median(hcst_point[year,]) + 1
    inf_median <- median(hcst_point[year,]) - 1
  } else { 
    sup_median <- max(hcst_point[year,pos])
    inf_median <- min(hcst_point[year,pos])
  #  hist(hcst_point[year,], breaks = c(2,inf_mean-0.01, sup_mean +0.01, 9), freq = T)
  }

  n_memb <- length(which(hcst_point[year,] >= inf_median & 
                         hcst_point[year,] <= sup_median))
  perc_median <- n_memb*100/25
  text(8.5, 14, paste("+/- 1 Median", round(perc_median, 1), "%"))

  hit_median <- ifelse(obs_point[year] >= inf_median & 
                     obs_point[year] <= sup_median, 1, 0)

  n_memb <- length(which(hcst_point[year,] >= inf_clim &
                         hcst_point[year,] <= sup_clim))
  perc_clim <- n_memb*100/25
  text(8.5, 13, paste("+/- 1 Clim", round(perc_clim, 1), "%"))
dev.off()
  hit_clim <- ifelse(obs_point[year] >= inf_clim &
                     obs_point[year] <= sup_clim, 1, 0)

  res <- rbind(res, data.frame(obs = obs_point[year], perc_mean = perc_mean,
             hit_mean = hit_mean, mean = mean(hcst_point[year,]), 
             inf_mean = inf_mean, sup_mean = sup_mean,
             perce_median = perc_median,
             hit_median = hit_median, 
             median = median(hcst_point[year,]), inf_median = inf_median, 
             sup_median = sup_median, perc_clim = perc_clim, 
             hit_clim = hit_clim, clim = clim, inf_clim = inf_clim, 
             sup_clim = sup_clim))
  #browser()
}



 
source("contingency.R")

contingency <- Apply(list(hcst_adj$data, data$obs$data), 
                     target_dims = list(c('syear', 'ensemble'),
                                       c('syear')),
                     fun = contingency, perc_lim = T, err = 1, perc = 0.6)

point <- c(lon = 2, lat = 42)
  PlotEquiMap(contingency$hits_mean[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 24, 2), 
#              boxlim = c(point[1] - .5, point[2] - .5,
#                         point[1] + .5, point[2] + .5),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Hits")

  PlotEquiMap(contingency$falseA_mean[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 24, 2),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "False Alarms")

  PlotEquiMap(contingency$miss_mean[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 24, 2),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Missed")
  PlotEquiMap(contingency$correctR_mea[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 24, 2),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Correct Rejected")

  PlotEquiMap(contingency$bias[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 2, 0.2),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Bias Score")
  PlotEquiMap(contingency$hit_rate[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 1, 0.1),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Hit rate")
  PlotEquiMap(contingency$fa_rate[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 1, 0.1),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "False Alarm rate")
  PlotEquiMap(contingency$accuracy[1,1,1,1,1,,,1],
              lon = data$hcst$coords$longitude,
              lat = data$hcst$coords$latitude, filled.c = F,
              brks = seq(0, 1, 0.1),
              sizetit = 0.8,
              toptitle = "SEAS5.1 ERA5 tas 1993-2016 January ft1",
              units = "Accuracy")




#saveRDS(list(biases = biases, pvals = pvals), file = 'bias_results.RDS')


