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")
shp_FI <- subset(shp_FI, NUTS_ID != "FI1D")
# LEGEND WITH NUTS2 NAMEs
ggplot(data = fn3.df) +
  geom_polygon(aes(long, lat, group = group),fill = "lightgrey", data = gusa) +
  geom_polygon(aes(x = long, y = lat, group = group),
               color = "grey",size = 0.08, data = shp_FI) +
  geom_polygon(aes(long, lat, fill = cut(value, breaks = brks))) +
 # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size = 0.1) +
  coord_map(projection = "stereographic",
            xlim = c((lon_min + 7),(lon_max - 2)),
            ylim = c((lat_min + 4),(lat_max - 4))) +

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


# Zoom Finland:
#------------------

# Subset region for Scandinavia
lat_min <- 55
lat_max <- 75
lon_min <- 13
lon_max <- 33

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-Laplan') +
  ggsave("FinlandZoom_NUTS3_NAMES.png", width = 15, height = 18, units = 'cm')

# -------------------------------------------------
# PLOT DATA ON TOP:
# -------------------------------------------------

library(startR)

tasmax <- startR::Start(
     dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "tasmax",
               year = "2018", #as.character(1979:2018),
               month = "01", #sprintf("%02d", 1:12),
               time = indices(1), #'all',
               lat = startR::values(list(50, 80)),
               lon = startR::values(list(10, 35)),
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               num_procs = 1,
               return_vars = list(lat = NULL, lon = NULL),
               retrieve = TRUE)
coord <- data.frame(x = sort(rep(attributes(tasmax)$Variables$common$lon,
                                 length(attributes(tasmax)$Variables$common$lat))),
                    y = rep(attributes(tasmax)$Variables$common$lat,
                            length(attributes(tasmax)$Variables$common$lon)))

tmax.df <- data.frame(tmax = as.vector(tasmax),
                      long = sort(rep(attributes(tasmax)$Variables$common$lon,
                                 length(attributes(tasmax)$Variables$common$lat))),
                      lat = rep(attributes(tasmax)$Variables$common$lat,
                      length(attributes(tasmax)$Variables$common$lon)))

tmax.sp <- SpatialPointsDataFrame(coord, data = tmax.df)
proj4string(tmax.sp) <- proj4string(shp_3)
library(rgeos)
# coordenadas intersección:
Pohjois <- gIntersection(tmax.sp, shp_3)
plot(Pohjois)
plot(shp_3, border = 'darkblue', add = T)


# Look for the indices:
pos <- c()
for (i in 1:nrow(Pohjois@coords)) {
  for (j in 1:nrow(tmax.sp@coords)) {
    if (all(Pohjois@coords[i,] == tmax.sp@coords[j, ])) {
      pos <- c(pos, j) 
    }
  }
}

res <- tmax.sp[pos, ]
plot(res)


brks <- seq(261, 276, 2)
ggplot(data = res@data) +
  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_point(aes(long, lat, color = cut(tmax, breaks = brks))) +
  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_colour_manual(values = rev(heat.colors(8)),
                   # label = c("A", "B", "C", "(270-271]", "(271-272]", "(272-273]",
                   #           "(273-274]", "(274-275]", "(275-276]",
                   #           "(276-277]", "(277-278]","(278-279]", "(279-280]", "No Finland"),
                    name = 'Maximum Temperature') +
  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('Single day temperature ERA5') +
  ggsave("Finland_NUTS3_TMAX_test.png", width = 15, height = 18, units = 'cm')



### Subset for Lappi

Lappi <- subset(shp_3, NUTS_NAME == "Lappi")
Lappi <- gIntersection(tmax.sp, Lappi)

NoLappi <- subset(shp_3, NUTS_NAME != "Lappi")


pos <- c()
for (i in 1:nrow(Lappi@coords)) {
  for (j in 1:nrow(tmax.sp@coords)) {
    if (all(Lappi@coords[i,] == tmax.sp@coords[j, ])) {
      pos <- c(pos, j)
    }
  }
}

res <- tmax.sp[pos, ]
plot(res)


brks <- seq(261, 276, 2)
ggplot(data = res@data) +
  geom_polygon(aes(long, lat, group = group), fill = "darkgrey", 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 = "white", fill = 'grey',  size = 0.08, data = shp_FI) +
  geom_polygon(aes(x = long, y = lat, group = group),
               color = "white", fill = 'lightgrey',  size = 0.08, data = NoLappi) +
  # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size = 0.1) +
  geom_point(aes(long, lat, color = cut(tmax, breaks = brks))) +
  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_colour_manual(values = rev(heat.colors(8)),
                   # label = c("A", "B", "C", "(270-271]", "(271-272]", "(272-273]",
                   #           "(273-274]", "(274-275]", "(275-276]",
                   #           "(276-277]", "(277-278]","(278-279]", "(279-280]", "No Finland"),
                    name = 'Maximum Temperature') +
  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('Single day temperature ERA5 Lappi') +
  ggsave("Lappi_NUTS3_TMAX_test.png", width = 15, height = 18, units = 'cm')

# Add Rovaniemi
ggplot(data = res@data) +
  geom_polygon(aes(long, lat, group = group), fill = "darkgrey", 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 = "white", fill = 'grey',  size = 0.08, data = shp_FI) +
  geom_polygon(aes(x = long, y = lat, group = group),
               color = "white", fill = 'lightgrey',  size = 0.08, data = NoLappi) +
  # add borders: 
  geom_polygon(aes(long, lat, group = group), fill = NA, data = shp_0,
               color = "black", size = 0.1) +
  geom_point(aes(long, lat, color = cut(tmax, breaks = brks))) +
  geom_point(aes(y = 66.5039, x= 25.7294), pch = 1, colour = 'blue') +
  # edit legend
  theme_bw() +                                                                                          # Theme black and white
  scale_colour_manual(values = rev(heat.colors(8)),
                   # label = c("A", "B", "C", "(270-271]", "(271-272]", "(272-273]",
                   #           "(273-274]", "(274-275]", "(275-276]",
                   #           "(276-277]", "(277-278]","(278-279]", "(279-280]", "No Finland"),
                    name = 'Maximum Temperature') +
  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('Single day temperature ERA5 Lappi') +
  ggsave("Rovanievi_NUTS3_TMAX_test.png", width = 15, height = 18, units = 'cm')


# LAUS --------------------


mask.file_4 <- paste0(path, '/LAU/LAU_RG_01M_2020_4326.shp')

shp_4 <- rgdal::readOGR(mask.file_4)
shp_4 <- subset(shp_4, CNTR_CODE == "FI")

muni <- c('009', '046', '047', '069', '071', '072', '074', '090', '097', '105', '139',
          '140', '146', '148', '167', '171', '176', '178', '204', '205', '208', '213', 
          '217', '236', '239', '240', '320', '241', '244', '260', '261', '263', '272', 
          '273', '276', '290', '297', '305', '317', '402', '420', '421', '422', '425', 
          '426', '436', '483', '491', '494', '498', '507', '535', '541', '563', '564',
          '309', '578', '583', '854', '584', '588', '593', '595', '607', '614', '615',
          '620', '623', '625', '626', '630', '678', '681', '683', '686', '687', '691', 
          '697', '698', '707', '732', '740', '742', '746', '748', '791', '749', '751', 
          '758', '762', '765', '768', '777', '778', '832', '844', '845', '848', '849', 
          '851', '857', '859', '889', '890', '785', '915', '921', '924', '925', '976', 
          '977')
muni2 <- c('047', '148', '240', '320', '241', '261', '273', '498', '583', '854',
          '614', '683', '698', '732', '742', '751', '758', '845', '851', '890', '976')

shp_muni <- subset(shp_4, LAU_ID %in% muni)
plot(shp_muni)
plot(shp_FI, col = 'blue', add = T)
plot(shp_3, border = 'red', add = T)

dev.new()
shp_muni2 <- subset(shp_4, LAU_ID %in% muni2)
plot(shp_3, pch = 'n',  lwd = 0)
plot(shp_muni2, add = T)
shp_5 <- subset(shp_3, NUTS_ID != 'FI1D7')
plot(shp_5, col = 'grey', add = T)
legend('topleft', c('Lappi', 'Etelä-Savo', 'Pohjois-Savo', 'Pohjois-Karjala', 'Keski-Pohjanmaa', 'Kainuu', 'Pohjois-Pohjanmaa'), fill = c('white', rep('grey', 7)), bty = 'n')


n <- 21
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
plot(shp_muni2, col = col_vector)
legend('topleft', legend = shp_muni2@data$LAU_NAME, fill = col_vector, bty = 'n')


