rm(list=ls())
library(abind)
library(multiApply)
library(s2dv)
library(s2dverification)
library(rgdal)
library(plyr)
library(ggplot2)
library(maptools)

path <- '/esarchive/scratch/nperez/git/Flor/ArticXchange/region_exploration'
setwd(path)

# NUTS2
mask.file <- paste0(path, '/NUTS2/ref-nuts-2021-20m.shp/NUTS_RG_20M_2021_4326_LEVL_2.shp/NUTS_RG_20M_2021_4326_LEVL_2.shp')
shp <- rgdal::readOGR(mask.file)

# A data value for each NUTS 2 region in Finland
country <- shp$NUTS_ID
pos.FI <- which(substr(country, 1, 2) == "FI")
finland_nuts2 <- rep(NA, length(country))
finland_nuts2[pos.FI] <- 1:length(pos.FI)
finland_nuts2 <- data.frame(NUTS_ID = country, value = finland_nuts2)

# Merge data with shapefile
fn2 <- merge(shp, finland_nuts2, by.x = "NUTS_ID", by.y = "NUTS_ID")
fn2@data$id <- rownames(fn2@data)
fn2.points <- fortify(fn2, region="id")
fn2.df <- join(fn2.points, fn2@data, by="id")

# Subset region for Scandinavia
lat_min <- 50
lat_max <- 75
lon_min <- 0
lon_max <- 30

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

brk <- 0:5
gusa <- map_data("world")

# NUTS0 to plot country borders
mask.file_0 <- paste0(path, '/NUTS2/ref-nuts-2021-20m.shp/NUTS_RG_20M_2021_4326_LEVL_0.shp/NUTS_RG_20M_2021_4326_LEVL_0.shp')
shp_0 <- rgdal::readOGR(mask.file_0)


# LEGEND WITH NUTS2 IDs
ggplot(data = fn2.df) +
  geom_polygon(aes(long, lat, group = group),fill = "lightgrey", data = gusa) +
  # cut region:
  coord_map(projection = "stereographic",
            xlim = c((lon_min + 7),(lon_max - 2)),
            ylim = c((lat_min + 4),(lat_max - 4))) +
  geom_polygon(aes(x = long, y = lat, fill = cut(value, breaks = brk), group = group), 
               color = "grey",size = 0.08) +
  # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size=0.1) +
  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_fill_manual(values = pals, 
                    label = c(as.character(country[pos.FI]), "No Finland"), 
                    name = 'NUTS2') +
  theme(panel.background = element_rect(fill = 'azure'), 
        text = element_text(family = "Times")) +  # Colour of ocean
  xlab('Longitude') + ylab('Latitude') +
  scale_x_continuous(breaks = seq(-15, 45, 10),labels = waiver()) +
  scale_y_continuous(breaks = seq(30, 70, 10),labels = waiver()) +
  ggtitle('NUTS2 regions in Finland') +
  ggsave("FinlandNUTS2_ID.png")



# LEGEND WITH NUTS2 NAMEs
  ggplot(data = fn2.df) +
  geom_polygon(aes(long, lat, group = group),fill = "lightgrey", data = gusa) +
  # cut region:
  coord_map(projection = "stereographic",
            xlim = c((lon_min + 7),(lon_max - 2)),
            ylim = c((lat_min + 4),(lat_max - 4))) +
  geom_polygon(aes(x = long, y = lat, fill = cut(value, breaks = brk), group = group),
               color = "grey",size = 0.08) +
  # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size=0.1) +  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_fill_manual(values = pals,
                    label = c(as.character(shp$NUTS_NAME[pos.FI]), "No Finland"),
                    name = 'NUTS2') +
  theme(panel.background = element_rect(fill = 'azure'),
        text = element_text(family = "Times")) +  # Colour of ocean
  xlab('Longitude') + ylab('Latitude') +
  scale_x_continuous(breaks = seq(-15, 45, 10),labels = waiver()) +
  scale_y_continuous(breaks = seq(30, 70, 10),labels = waiver()) +
  ggtitle('NUTS2 regions in Finland') +
  ggsave("FinlandNUTS2_NAMES.png")

# -----------------------------------------------
# NUTS 3
# -----------------------------------------------

mask.file_3 <- paste0(path, '/NUTS3/NUTS_RG_60M_2021_4326.shp/NUTS_RG_60M_2021_4326.shp')

shp_3 <- rgdal::readOGR(mask.file_3)
shp_3 <- subset(shp_3, CNTR_CODE == "FI")
shp_3 <- subset(shp_3, NUTS_ID %in% paste0("FI1D", c(1:3,5,7:9)))
subregions <- shp_3$NUTS_ID
finland_nuts3 <- data.frame(NUTS_ID = subregions, value = as.numeric(1:length(subregions)))

# Merge data with shapefile
fn3 <- merge(shp_3, finland_nuts3, by.x = "NUTS_ID", by.y = "NUTS_ID")
fn3@data$id <- rownames(fn3@data)
fn3.points <- fortify(fn3, region = "id")
fn3.df <- join(fn3.points, fn3@data, by = "id")


brks <- 0:7
# LEGEND WITH NUTS2 NAMEs
shp_FI <- subset(shp, CNTR_CODE == "FI")
# LEGEND WITH NUTS2 NAMEs
ggplot(data = fn3.df) +
  geom_polygon(aes(long, lat, group = group),fill = "lightgrey", data = gusa) +
  # cut region:
  coord_map(projection = "stereographic",
            xlim = c((lon_min + 7),(lon_max - 2)),
            ylim = c((lat_min + 4),(lat_max - 4))) +
  geom_polygon(aes(x = long, y = lat, group = group),
               color = "grey",size = 0.08, data = shp_FI) +
  # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size = 0.1) +
  geom_polygon(aes(long, lat, fill = cut(value, breaks = brks))) +
  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_fill_manual(values = heat.colors(7),
                    label = c(as.character(shp_3$NUTS_NAME), "No Finland"),
                    name = 'NUTS3') +
  theme(panel.background = element_rect(fill = 'azure'),
        text = element_text(family = "Times")) +  # Colour of ocean
  xlab('Longitude') + ylab('Latitude') +
  scale_x_continuous(breaks = seq(-15, 45, 10),labels = waiver()) +
  scale_y_continuous(breaks = seq(30, 70, 10),labels = waiver()) +
  ggtitle('NUTS3 regions in Finland') +
  ggsave("FinlandNUTS3_NAMES.png")
