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 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tasmax-intlr-4nn-tw33-lt0.RDS")
obs0 <- obs0$obs$data[5,1,1,1,49,1,63]

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")


# Area aggregation:

data0 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tas-intlr-4nn-tw32-lt0.RDS")
cat0 <- data0$hcst$data[,,,1,1,1,1,]
cat0 <- MeanDims(cat0[,,,] -273.15, c("latitude", "longitude"), na.rm=TRUE)
aug03cat0 <- cat0[5,]

data1 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tas-intlr-4nn-tw32-lt1.RDS")
cat1 <- data1$hcst$data[,,,1,1,1,1,]
cat1 <- MeanDims(cat1[,,,] -273.15, c("latitude", "longitude"), na.rm=TRUE)
aug03cat1 <- cat1[5,]

data2 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tas-intlr-4nn-tw32-lt2.RDS")
cat2 <- data2$hcst$data[,,,1,1,1,1,]
cat2 <- MeanDims(cat2[,,,] -273.15, c("latitude", "longitude"), na.rm=TRUE)
aug03cat2 <- cat2[5,]

data3 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tas-intlr-4nn-tw32-lt3.RDS")
cat3 <- data3$hcst$data[,,,1,1,1,1,]
cat3 <- MeanDims(cat3[,,,] -273.15, c("latitude", "longitude"), na.rm=TRUE)
aug03cat3 <- cat3[5,]

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

obs0 <- readRDS("/esarchive/scratch/eduzenli/downscaling/impetus/subseasonal/catalonia/intlr/data/cerra/tas-intlr-4nn-tw33-lt0.RDS")
obs0 <- MeanDims(obs0$obs$data[5,1,1,1,,1,] -273.15, c("latitude","longitude"), na.rm = TRUE)

fcst <- data.frame(fcst1 = aug03cat3, fcst2 = aug03cat2, fcst3 = aug03cat1, fcst4 = aug03cat0)

plot <- PlotForecastPDF(fcst,tercile.limits = c(terciles[2],terciles[3]),var.name = "Temperatura diaria (C)",fcst.names = c(paste0("ftime: days 26-32\nsdate: 14/07/03"), paste0("ftime: days 19-25\nsdate: 21/07/03"), paste0("ftime: days 12-18\nsdate: 28/07/03"), paste0("ftime: days 5-11\nsdate: 04/08/03")),obs = c(obs0,obs0,obs0,obs0),title = "Prediccion de temperatura diaria, Agosto de 2003 - Cataluņa")
ggsave("subseasonal_CAT_heatwave_tas.png", plot, width = 7, height = 5)
