Sys.setenv(LANG = "en")
Sys.setlocale("LC_TIME","en_GB")
library(startR)
library(s2dverification)
library(abind)
library(CSTools)
library(zeallot)
library(s2dv)
library(easyVerification)
library(ClimProjDiags)
library(ggplot2)

data0 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tasmax-intlr-4nn-tw32-lt0.RDS")

# Select the point of Barcelona
bcn0 <- data0$hcst$data[,49,63,1,1,1,1,]

# Select August 2003
aug03bcn0 <- bcn0[5,] -273.15

# Estimate terciles from hindcast as well as the extremes
terciles <-quantile(bcn0[-5,], probs = c(.1,.33,.66,.9), na.rm = TRUE)
terciles <- terciles -273.15


# Same for leadtimes 1, 2 and 3
data1 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tasmax-intlr-4nn-tw32-lt1.RDS")
bcn1 <- data1$hcst$data[,49,63,1,1,1,1,]
aug03bcn1 <- bcn1[5,] - 273.15

data2 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tasmax-intlr-4nn-tw32-lt2.RDS")
bcn2 <- data2$hcst$data[,49,63,1,1,1,1,]
aug03bcn2 <- bcn2[5,] -273.15

data3 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tasmax-intlr-4nn-tw32-lt3.RDS")
bcn3 <- data3$hcst$data[,49,63,1,1,1,1,]
aug03bcn3 <- bcn3[5,] -273.15

# Add RPSS
skill0 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/skill/cerra/tasmax-intlr-4nn-tw32-lt0.RDS")
skill0 <- skill0$bss90[1,49,63]
skill0 <- round(skill0,digits=2)

skill1 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/skill/cerra/tasmax-intlr-4nn-tw32-lt1.RDS")
skill1 <- skill1$bss90[1,49,63]
skill1 <- round(skill1,digits=2)

skill2 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/skill/cerra/tasmax-intlr-4nn-tw32-lt2.RDS")
skill2 <- skill2$bss90[1,49,63]
skill2 <- round(skill2,digits=2)


skill3 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/skill/cerra/tasmax-intlr-4nn-tw32-lt3.RDS")
skill3 <- skill3$bss90[1,49,63]
skill3 <- round(skill3,digits=2)



# Plot forecast pdf

# To plot the observations, I take the average of all members 
obs0 <- mean(aug03bcn0)
obs1 <- mean(aug03bcn1)
obs2 <- mean(aug03bcn2)
obs3 <- mean(aug03bcn3)


fcst <- data.frame(fcst1 = aug03bcn3, fcst2 = aug03bcn2, fcst3 = aug03bcn1, fcst4 = aug03bcn0)

plot <-PlotForecastPDF(fcst,tercile.limits = c(terciles[2],terciles[3]),var.name = "Temperatura maxima diaria (C)",fcst.names = c(paste0("ftime: days 26-32\nsdate: 14/07/03\nBSS90: ",skill3), paste0("ftime: days 19-25\nsdate: 21/07/03\nBSS90: ",skill2), paste0("ftime: days 12-18\nsdate: 28/07/03\nBSS90: ",skill1), paste0("ftime: days 5-11\nsdate: 04/08/03\nBSS90: ",skill0)),obs = c(obs0,obs0,obs0,obs0),title = "Prediccion de la ola de calor de 2003 - Barcelona")
ggsave("subseasonal_BCN_heatwave_tasmax_bss90.png", plot, width = 7, height = 5)
#if I add extreme.limits it says "Error in integrate(approxfun(x, ymax), lower = min(x), upper = max(x)) : 
#  maximum number of subdivisions reached"

PlotForecastPDF(fcst,tercile.limits = c(terciles[2],terciles[3]),var.name = "Temperatura media diaria (C)",fcst.names = c(paste0("ftime: days 26-32\nsdate: 14/07/03\nRPSS: ",skill3), paste0("ftime: days 19-25\nsdate: 21/07/03\nRPSS: ",skill2), paste0("ftime: 
days 12-18\nsdate: 28/07/03\nRPSS: ",skill1), paste0("ftime: days 5-11\nsdate: 04/08/03\nRPSS: ",skill0)),obs = c(obs0,obs0,obs0,obs0),title = "Prediccion de la ola de calor de 2003 - Barcelona")
