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 = seq(0.55,1,0.05),
                    LAU_ID = shp_herding@data$LAU_ID[1:10])
  df2 <- data.frame(nor = seq(0.5,1,0.05),
                    LAU_ID = shp_herding@data$LAU_ID[11:21])
  df3 <- data.frame(an = seq(0.55,1,0.05),
                    LAU_ID = shp_herding@data$LAU_ID[22:31])
shp_df1 <- subset(shp_herding, LAU_ID %in% shp_herding@data$LAU_ID[1:10])
shp_df2 <- subset(shp_herding, LAU_ID %in% shp_herding@data$LAU_ID[11:21])
shp_df3 <- subset(shp_herding, LAU_ID %in% shp_herding@data$LAU_ID[22:31])


shp_df1 <- merge(shp_df1, 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_df2, 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_df3, 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")


new_scale <- function(new_aes) {
   structure(ggplot2::standardise_aes_names(new_aes), class = "new_aes")
}

 ggplot() +
  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(data = df.df1, 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) +
       scale_fill_manual(values = c(rev(brewer.pal(9, 'Blues')),
                                     brewer.pal(8, 'Reds'), "black"),
                          drop = FALSE, name = 'tas(ºC)') +
new_scale("fill") +
  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) +
       scale_fill_manual(values = c(rev(brewer.pal(9, 'Greys')),
                                     brewer.pal(8, 'Reds'), "black"),
                          drop = FALSE, name = 'What?') +
new_scale("fill") +
  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) +
       scale_fill_manual(values = c(rev(brewer.pal(9, 'Reds')),
                                     brewer.pal(8, 'Reds'), "black"),
                          drop = FALSE, name = 'Pez') +
    geom_polygon(aes(x = long, y = lat, group = group), fill = NA,
                    color = "grey", size = 0.5, data = shp_herding) #+

