rm(list=ls()); gc();

### Creation date: 3rd October 2022
# Author: N. Pérez-Zanón
# Refer CSTools package and manuscript when using it.
# ----------------------------------------

library(CSTools)
library(s2dv)
library(ragg)
library(multiApply)
library(startR)
library(sf)
library(rgeos)
library(multiApply)
library(ggplot2)
library(RColorBrewer)


# ERA5Land Subset region
lat_min <- 64#59
lat_max <- 71
lon_min <- 20
lon_max <- 31
setwd("/esarchive/scratch/nperez/git/Flor/ArticXchange")

mask_fmi <- readRDS("masks/FMI_Herding_mask.RDS")
polys <- unique(as.vector(mask_fmi$mask))[-1]
dim(polys) <- c(poly = length(polys))



mask.file_4 <- paste0('region_exploration/LAU/LAU_RG_01M_2020_4326.shp')
shp_4 <- rgdal::readOGR(mask.file_4)
shp_4 <- subset(shp_4, CNTR_CODE == "FI")
shp_herding <- subset(shp_4, LAU_ID %in% sprintf("%03d", polys))
gusa <- map_data("world")


# FAKE:
  df1 <- data.frame(bn = c(seq(0.55,1,0.05), rep(NA, 21)),
                    LAU_ID = shp_herding@data$LAU_ID)
  df2 <- data.frame(nor = c(rep(NA, 10), seq(0.5,1,0.05), rep(NA, 10)),
                    LAU_ID = shp_herding@data$LAU_ID)
  df3 <- data.frame(an = c(rep(NA, 21), seq(0.55,1,0.05)),
                    LAU_ID = shp_herding@data$LAU_ID)
shp_df1 <- merge(shp_herding, df1, by.x="LAU_ID", by.y="LAU_ID")
# Magic lines below that converts SpatialPolygonsDataFrame to normal data frame
  shp_df1@data$id <- rownames(shp_df1@data)
  df.points1 <- fortify(shp_df1, region = "id")
  df.df1 <- plyr::join(df.points1, shp_df1@data, by = "id")

shp_df2 <- merge(shp_herding, df2, by.x="LAU_ID", by.y="LAU_ID")
# Magic lines below that converts SpatialPolygonsDataFrame to normal data frame
  shp_df2@data$id <- rownames(shp_df2@data)
  df.points2 <- fortify(shp_df2, region = "id")
  df.df2 <- plyr::join(df.points2, shp_df2@data, by = "id")

shp_df3 <- merge(shp_herding, df3, by.x="LAU_ID", by.y="LAU_ID")
# Magic lines below that converts SpatialPolygonsDataFrame to normal data frame
  shp_df3@data$id <- rownames(shp_df3@data)
  df.points3 <- fortify(shp_df3, region = "id")
  df.df3 <- plyr::join(df.points3, shp_df3@data, by = "id")


 ggplot(data = df.df1) +
  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(bn,
                         breaks = seq(0.4, 1, 0.1)),
             group = group), na.rm = T, color = "grey",size = 0.08) +
  geom_polygon(data = df.df2, aes(x = long, y =lat, fill = cut(nor,
                         breaks = seq(0.4,1,0.1)),
            group = group), na.rm = T, color = "grey",size = 0.08) +
  geom_polygon(data = df.df3, aes(x = long, y =lat, fill = cut(an,
                         breaks = seq(0.4,1,0.1)),
            group = group), na.rm = T, color = "grey",size = 0.08) +
    geom_polygon(aes(x = long, y = lat, group = group), fill = NA,
                    color = "grey", size = 0.5, data = shp_herding) #+
# .------------------------
# To be addapted:
        theme_bw() +
       scale_fill_manual(values = rev(c("deepskyblue3", "#33BFD1", "#b5b5b5",
                                     "#FF764D","indianred3")),
                          labels = rev(c('Lower extreme (p10)',
                                     'Below normal (p33)',
                                     'Normal (between p33-p66)',
                                     'Above normal (p66)',
                                     'Upper extreme (p90)')),
                          drop = FALSE, name = 'Probability') +
        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))# +

