
source("modules/Loading/Loading.R")
source("modules/Saving/Saving.R")
source("modules/Units/Units.R")
source("modules/Visualization/Visualization.R")
args = commandArgs(trailingOnly = TRUE)
recipe_file <- args[1]
#recipe_file <- "recipe_tas_oper.yml"
recipe <- read_atomic_recipe(recipe_file)
#recipe <- prepare_outputs(recipe_file)
# Load datasets
data <- Loading(recipe)
data <- Units(recipe, data)
data_summary(data$hcst, recipe)
data_summary(data$obs, recipe)

# UKMO January 1993 is missing:
if (recipe$Analysis$Time$sdate == '0101') {
  if (recipe$Analysis$Datasets$System$name == "UKMO-System602") {
    if (1993 %in% recipe$Analysis$Time$hcst_start:recipe$Analysis$Time$hcst_end) {	  
      info(recipe$Run$logger,
         "UKMO January 1993 not available")
      ind <- recipe$Analysis$Time$hcst_start:recipe$Analysis$Time$hcst_end
      ind <- (1:length(ind))[-which(ind == 1993)]
      data$hcst <- CST_Subset(data$hcst, along = 'syear', indices = ind)
      data$obs <- CST_Subset(data$obs, along = 'syear', indices = ind)
      sdate_dim <- dim(data$hcst$data)['syear']
    }
  }
}

calibraion_comp <- TRUE
anomalies_comp <- FALSE
source("crossval.R")

## Define FAIR option:
fair <- TRUE

## START SKILL ASSESSMENT:
# RPS
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/GetProbs.R")
cal_hcst_probs_ev <- GetProbs(res$cal_hcst_ev, time_dim = 'syear', 
                 prob_thresholds = NULL,
                 bin_dim_abs = 'probs', 
		 indices_for_quantiles = NULL,
                 memb_dim = 'ensemble', abs_thresholds = res$lims_cal_hcst_tr,
                 ncores = recipe$Analysis$ncores)
cal_obs_probs_ev <- GetProbs(data$obs$data, time_dim = 'syear',
                 prob_thresholds = NULL,
                 bin_dim_abs = 'probs', 
		 indices_for_quantiles = NULL,
                 memb_dim = 'ensemble',
                 abs_thresholds = res$lims_cal_obs_tr,
                 ncores = recipe$Analysis$ncores)
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/modules/Skill/R/tmp/RPS.R")
rps <- RPS(exp = ano_hcst_probs_ev, obs = ano_obs_probs_ev, memb_dim = NULL,
           cat_dim = 'probs', cross.val = FALSE, time_dim = 'syear',
	   Fair = fair, nmemb = nmemb,
           ncores = recipe$Analysis$ncores)
source("modules/Skill/R/RPS_clim.R")
rps_clim <- Apply(list(ano_obs_probs_ev),
                  target_dims = c('probs', 'syear'),
                  RPS_clim, bin_dim_abs = 'probs', Fair = fair,
		  cross.val = FALSE, ncores = recipe$Analysis$ncores)$output1
# RPSS
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/RPSS.R")
source("https://earth.bsc.es/gitlab/es/sunset/-/raw/dev-test_CERISE/RandomWalkTest.R")
rpss <- RPSS(exp = cal_hcst_probs_ev, obs = cal_obs_probs_ev,
                   time_dim = 'syear', memb_dim = NULL,
                   cat_dim = 'probs', Fair = fair, nmemb = nmemb,
                   # We should use a line like this 
                   #abs_threshold = res$lims_ano_hcst_tr, 
                   #prob_threshold = c(1/3, 2/3),
                   cross.val = FALSE, 
                   ncores = recipe$Analysis$ncores)

cal_fcst <- CST_Calibration(data$hcst, data$obs, data$fcst,
                            sdate_dim = 'syear', memb_dim = 'ensemble')


shp_file = "/esarchive/shapefiles/NUTS3/NUTS_RG_60M_2021_4326.shp"
shp <- sf::st_read(shp_file)  # class sf
ids <- subset(shp, shp$LEVL_CODE == 3)


######
library(sf)
library(ggplot2)
#library(ggplot2, lib.loc = .libPaths()[2])
library(RColorBrewer)
library(rgeos)

gusa <- map_data("world")

pals <- brewer.pal(9, 'Reds')[3:9]
pals <- c("#ffffb3",pals,'#525252')

brks <- 0:5
brks <- seq(258, 274, 2)

plot_poly <- list()
polys_data <- NULL


## Now fill each polygon with the mean value
  data_pol <- data.frame(data = apply(cal_fcst$data[1,1,1,1,1,1,,], 2, mean),
                         LAU_ID = polys)
  shp_data <- merge(subset(shp, data_pol, by.x="LAU_ID", by.y="LAU_ID")

# Magic lines below that converts SpatialPolygonsDataFrame to normal data frame
  shp_data@data$id <- rownames(shp_data@data)
  df.points <- fortify(shp_data, region = "id")
  df.df <- plyr::join(df.points, shp_data@data, by = "id")
ano <- df.df
ano$data <- ano$data - fmi_clim_m$data

ggplot(data = ano) + #df.df) +
        geom_polygon(aes(long, lat, group = group),fill = "lightgrey", data = gusa) +
        coord_map(projection = "stereographic",
                  xlim = c(lon_min, lon_max),
                  ylim = c(lat_min, lat_max)) +
       # geom_polygon(aes(x = long, y = lat, fill = cut(data, breaks = seq(-14, 18, 2)),
        geom_polygon(aes(x = long, y = lat, fill = cut(data,
                         breaks = seq(0, 5, 0.5)),
             group = group), color = "grey",size = 0.08) +
        geom_polygon(aes(x = long, y = lat, group = group), fill = NA,
                    color = "grey", size = 0.5, data = shp_herding) +
        theme_bw() + 
        scale_fill_manual(values = c(#rev(brewer.pal(9, 'Blues')),
                                     'white', brewer.pal(9, 'Reds'), "black"),
                          drop = FALSE, name = 'tas(ºC)') +
        theme(panel.background = element_rect(fill = 'azure'),
              text = element_text(family = "Times")) +  # Colour of ocean
        xlab('Longitude') + ylab('Latitude') +
        scale_x_continuous(breaks = seq(-12, 45, 4),labels = waiver()) +
        scale_y_continuous(breaks = seq(32, 70, 4),labels = waiver()) +
        ggtitle(paste(month.name[mes], year))# +

######
#PlotEquiMap(rps[1,,], lon = data$hcst$coords$longitude,
#	    lat = data$hcst$coords$latitude, filled.c = F,
#	    fileout = "test.png")
# CRPS
crps <- CRPS(exp  = res$ano_hcst_ev, obs = res$ano_obs_ev,
             time_dim = 'syear', memb_dim = 'ensemble',
	     Fair = fair,
             ncores = recipe$Analysis$ncores)
# Este no sé como se calcula????:
# Aquí no se puede porque estaría incluyendo información de los otros años
#source("modules/Skill/R/CRPS_clim.R")
# Pero si lo hago con el ano_obs_tr si puedo hacerlo aquí
# el resultado es igual a dentro del bucle.
crps_clim <- CRPS(exp = res$ano_obs_tr, obs = res$ano_obs_ev,
                  time_dim = 'syear', memb_dim = 'sample.syear',
		  Fair = fair
                  ncores = recipe$Analysis$ncores) 


# CRPSS
ref <- res$ano_obs_tr
dim(ref) <- c(ensemble = as.numeric(sdate_dim) -1, 
              nftime, nlats, nlons, sdate_dim)
crpss <- CRPSS(exp = res$ano_hcst_ev, obs = res$ano_obs_ev, ref = ref,
               memb_dim = 'ensemble', Fair = fair,
               time_dim = 'syear', clim.cross.val = FALSE,
               ncores = recipe$Analysis$ncores)



# Corr
source("modules/Skill/R/tmp/Corr.R")
enscorr <- Corr(res$ano_hcst_ev, res$ano_obs_ev, 
                          dat_dim = NULL, 
                          time_dim = 'syear', 
                          method = 'pearson', 
                          memb_dim = 'ensemble',
                          memb = F,
                          conf = F,
                          pval = F,
                          sign = T,
                          alpha = 0.05,
                          ncores = recipe$Analysis$ncores)

# Mean Bias
#mean_bias <- Bias(res$ano_hcst_ev, res$ano_obs_ev,
mean_bias <- Bias(data$hcst$data, data$obs$data,
                 time_dim = 'syear',
                 memb_dim = 'ensemble',
                 ncores = recipe$Analysis$ncores)

mean_bias_sign <- Apply(list(data$hcst$data, data$obs$data),
			target_dims = list(c('syear', 'ensemble'), 'syear'),
                        fun = function(x,y) {
		          if (!(any(is.na(x)) || any(is.na(y)))) {
  			    res <- t.test(x = y,
			 	 	  y = apply(x, 1, mean, na.rm = T),
				   	  alternative = "two.sided")$p.value
			  } else {
		  	    res <- NA
		 	  }
			  return(res)},
			ncores = sdate_dim)$output1
mean_bias_sign <- mean_bias_sign <= 0.05
#PlotEquiMap(mean_bias[1,1,1,1,1,,], lat = data$hcst$coords$latitude,
#	    lon = data$hcst$coords$longitude,
#	    dots = mean_bias_sign[1,1,1,1,1,,,1])
		
# Spread error ratio
source("SprErr.R")
enssprerr <- SprErr(exp = res$ano_hcst_ev, obs = res$ano_obs_ev,
	            memb_dim = 'ensemble', dat_dim = NULL,
                    time_dim = 'syear', pval = TRUE,
		    ncores = recipe$Analysis$ncores)
enssprerr_sign <- enssprerr$p.val
enssprerr_sign <- enssprerr_sign <= 0.05
enssprerr <- enssprerr$ratio

# RMSE
rms <- RMS(exp = res$ano_hcst_ev, obs = res$ano_obs_ev,
           memb_dim = 'ensemble', dat_dim = NULL,
           time_dim = 'syear', alpha = 0.05,
           ncores = recipe$Analysis$ncores)

#obs_noensdim <- ClimProjDiags::Subset(res$ano_obs_ev, "ensemble", 1,
#                                            drop = "selected")

#enssprerr <- easyVerification::veriApply(verifun = 'EnsSprErr',
#                                         fcst = res$ano_hcst_ev,
#                                         obs = obs_noensdim,
#                                         tdim = which(names(dim(res$ano_hcst_ev))=='syear'),
#                                         ensdim = which(names(dim(res$ano_hcst_ev))=='ensemble'),
#                                         na.rm = FALSE,
#                                         ncpus = recipe$Analysis$ncores)
if (any(is.na(rpss$sing))) {
	   info(recipe$Run$logger,
       "RPSS NA")

	rpss$sing[is.na(rpss$sign)] <- FALSE
}
skill_metrics <- list(mean_bias = mean_bias,
		      mean_bias_significance = mean_bias_sign,
		      enscorr = enscorr$corr,
                      enscorr_significance = enscorr$sign,
		      enssprerr = enssprerr,
                      enssprerr_significance = enssprerr_sign,
                      rps = rps, rps_clim = rps_clim, crps = crps, crps_clim = crps_clim,
                      rpss = rpss$rpss, rpss_significance = rpss$sign, #crps = crps,
                      crpss = crpss$crpss, crpss_significance = crpss$sign,
		      rms = rms$rms)
skill_metrics <- lapply(skill_metrics, function(x) {
                      InsertDim(drop(x), len = 1, pos = 1, name = 'var')})
original <- recipe$Run$output_dir
recipe$Run$output_dir <- paste0(original, "/outputs/Skill/")

skill_metrics <- lapply(skill_metrics, function(x) {
		   if (is.logical(x)) {
		     dims <- dim(x)
		     res <- as.numeric(x)
		     dim(res) <- dims
		   } else {
		     res <- x
		   }
		   return(res)
		      })
# Compute save metrics 
source("modules/Saving/Saving.R")
#Saving <- Saving(recipe = recipe, data = data, skill = skill_metrics)
      save_metrics(recipe = recipe,
                   metrics = skill_metrics,
                   data_cube = data$hcst, agg = 'global',
                   outdir = recipe$Run$output_dir)

recipe$Run$output_dir <- original

source("modules/Visualization/Visualization.R")
if (data$hcst$coords$longitude[1] != 0) {
  skill_metrics <- lapply(skill_metrics, function(x) {
                Subset(x, along = 'longitude', indices = c(182:360, 1:181))
        })
}
     info(recipe$Run$logger,
       paste("lons:", data$hcst$coords$longitude))
     info(recipe$Run$logger,
       paste("lons:", data$obs$coords$longitude))


data$hcst$coords$longitude <- -179:180

Visualization(recipe, data, skill_metrics, significance = TRUE)

source("tools/add_logo.R")
add_logo(recipe, "rsz_rsz_bsc_logo.png")


