# Explore FMI dataset:
rm(list=ls())
# library(maptools) # to plot shp files

library(maptools)

mask.file <- paste0("/esarchive/shapefiles/",
  'NUTS/ref-nuts-2021-20m.shp/NUTS_RG_20M_2021_4326_LEVL_2.shp/NUTS_RG_20M_2021_4326_LEVL_2.shp')
shp_eu <- rgdal::readOGR(mask.file)
shp_fi <- subset(shp_eu, CNTR_CODE == "FI")
##     plot(shp_fi)
#LAUS
mask.file_laus <- paste0("/esarchive/shapefiles/", '/LAU/LAU_RG_01M_2020_4326.shp')
shp_laus <- rgdal::readOGR(mask.file_laus)
shp_laus <- subset(shp_laus, CNTR_CODE == "FI")
Laus_lapland <- c('047', '148', '240', '320', '241', '261', '273', '498', '583', '854',
                  '614', '683', '698', '732', '742', '751', '758', '845', '851', '890',
                  '976')
shp_lapland <- subset(shp_laus, LAU_ID %in% Laus_lapland)
##     plot(shp_lapland)

# Subset region
lat_min <- 65#59
lat_max <- 71
lon_min <- 20
lon_max <- 31

library(startR)
path <- "/esarchive/recon/ecmwf/era5land/monthly_mean/$var$_f1h/$var$_$year$$month$.nc"

tas <- Start(dat = path,
             var = 'tas',
             year = as.character(1993:2021),
             month = sprintf("%02d", 1:12),
             latitude = values(list(lat_min, lat_max)),
             latitude_reorder = Sort(),
             longitude = values(list(lon_min, lon_max)),
             longitude_reorder = CircularSort(-180, 180),
             synonims = list(latitude=c('lat','latitude'),
                             longitude=c('lon','longitude')),
             return_vars = list(lon = NULL, lat = NULL),
             retrieve = TRUE)

lat <- attributes(tas)$Variable$common$lat
lon <- attributes(tas)$Variable$common$lon

tas_clim <- s2dv::MeanDims(tas, 'year')

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

gusa <- map_data("world")

# LOOP on MONTHS:
plot_dots <- list()
plot_poly <- list()
polys_data <- NULL
for (mon in 1:12) {
  tas_df <- data.frame(tas = as.vector(t(tas_clim[1,1,mon,,]) - 273.15),
                       long = rep(lon, length(lat)),
                       lat = sort(rep(lat, length(lon)), decreasing = FALSE))
  tas_df <- tas_df[which(!is.na(tas_df[,1])),]


## The elipsoid is already the EPSG:4326, no need to be transform:
## https://gis.stackexchange.com/questions/379877/convert-era5-data-to-wgs84 
##  tas_sf_df <- st_as_sf(tas_df, coords = c("long", "lat"), crs = 'EPSG:4326')

  coord <- data.frame(x = tas_df$long, y = tas_df$lat)
  tas.sp <- SpatialPointsDataFrame(coord,
                                   data = data.frame(tas_df$tas))
  proj4string(tas.sp) <- proj4string(shp_lapland)

  data_pol  <- c()
  for (pol in shp_lapland@data$FID) { # Loop polygons
    shp_pol <- subset(shp_lapland, FID == pol)
    shp_pol <- gIntersection(tas.sp, shp_pol)

    pos <- c()
    for (i in 1:nrow(shp_pol@coords)) { # Loop coordinates in the polygon
      for (j in 1:nrow(tas.sp@coords)) { # Loop coordinates in data
        if (identical(all.equal(shp_pol@coords[i,], tas.sp@coords[j,]), TRUE)) {
          pos <- c(pos, j)
        }
      }
    }
    data <- tas.sp[pos,]
    data2 <- cbind(tas = data, FID = rep(pol, nrow(data)))
    colnames(data2@data) <- c('tas', 'FID')
    if (pol == 'FI_583') { # This condition initializes data_byFID
      data_byFID <- data2
    } else {
      data_byFID <- rbind(data_byFID, data2)
    }
    data_pol <- c(data_pol, mean(data$tas_df.tas))
  }
  data_byFID_df <- cbind(data_byFID@data, data_byFID@coords)


  plot_dots[[mon]] <- ggplot(data = data_byFID_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(long, lat, group = group), fill = NA, data = gusa,
                   color = "grey", size = 0.5) +
  #    geom_polygon(aes(x = long, y = lat, group = group), fill = NA,
  #                 color = "black", size = 0.5, data = shp_lapland) +
      geom_point(aes(x, y, col = cut(tas, breaks = seq(-15, 18, 1))), size = 1) +
      geom_polygon(aes(x = long, y = lat, group = group), fill = NA,
                   color = "grey", size = 0.5, data = shp_lapland) +
      scale_color_manual(values = c(brewer.pal(9, 'Blues'), brewer.pal(5, 'Greens'),
                                    brewer.pal(9, 'Purples'),
                                    brewer.pal(9, 'Reds'), "black"),

#c("#053061", "#2971B1", "#6AABD0", "#C1DDEB", "#F7F7F7", 
#                                    "#FACDB5", "#E58267", "#BB2A33", "#67001F", "black"),
                         drop = FALSE, name = 'tas (ºC)') +
      xlab('Longitude') + ylab('Latitude') +
      scale_x_continuous(breaks = seq(-12, 45, 2),labels = waiver()) +
      scale_y_continuous(breaks = seq(32, 70, 2),labels = waiver()) +
      ggtitle(month.name[mon]) + theme_bw() +
      theme(panel.background = element_rect(fill = 'azure'),
            text = element_text(family = "Times"))



## Now fill each polygon with the mean value
  data_pol <- data.frame(data = data_pol, LAU_ID = shp_lapland@data$LAU_ID)
  shp_data <- merge(shp_lapland, 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")

  plot_poly[[mon]] <- ggplot(data = 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)),
                     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_lapland) +
        theme_bw() +
        scale_fill_manual(values = c(rev(brewer.pal(9, 'Blues')),
                                     brewer.pal(8, '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(month.name[mon])# +

  df.df <- cbind(df.df, mon = rep(mon, nrow(df.df)))
  polys_data <- rbind(polys_data, df.df)
}
library(ggpubr)
plot <- ggarrange(plotlist = plot_dots, ncol = 6, nrow = 2, common.legend = TRUE, legend = "bottom")
plot <- annotate_figure(plot, top = text_grob("FMI Grided dataset Monthly clim 1993-2021",
                        color = "black", face = "bold", size = 14))
ggsave("ERA5Lpoints.jpg", plot, width = 18, height = 13, bg = 'white')

plot <- ggarrange(plotlist = plot_poly, ncol = 6, nrow = 2, common.legend = TRUE, legend = "bottom")
plot <- annotate_figure(plot, top = text_grob("FMI Grided dataset Monthly clim 1993-2021",
                        color = "black", face = "bold", size = 14))
ggsave("ERA5Lpolys.jpg", plot, width = 18, height = 13, bg = 'white')

saveRDS(polys_data, file = "ERA5L_monthly_clim_polygons.RDS")

