# 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/obs/fmi/climgrid/monthly_mean/$var$/$var$_$year$$month$.nc"

tas <- Start(dat = path,
             var = 'tas',
             year = as.character(1993:2021),
             month = sprintf("%02d", 1:12),
             y = indices(1:60),
             x = 'all',
             synonims = list(y = c('y', 'lat', 'latitude'),
                             x = c('x', 'lon', 'longitude')),
             return_vars = list(y = NULL, x = NULL),
             retrieve = TRUE)

ys <- attributes(tas)$Variable$common$y
xs <- attributes(tas)$Variable$common$x

##       image(0:68, 0:59, t(tas[1,1,1,1,60:1,]))
##       image(xs, rev(ys), t(tas[1,1,1,1,60:1,]))
#tas_df <- data.frame(tas = as.vector(t(tas[1,1,1,1,,])),
#                     xs = rep(xs, length(ys)),
#                     ys = sort(rep(ys, length(xs)), decreasing = TRUE))
# data.frame is correctly built for a single map

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

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

gusa <- map_data("world")

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

brks <- 0:5
brks <- seq(258, 274, 2)

# 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),
                       XCoord = rep(xs, length(ys)),
                       YCoord = sort(rep(ys, length(xs)), decreasing = TRUE))
  tas_df <- tas_df[which(!is.na(tas_df[,1])),]
# Coordinate Reference System: ETRS89 / ETRS-TM35FIN (EPSG:3067)
# From https://etsin.fairdata.fi/dataset/815098c4-4d6f-4986-a9d1-4184efe54dd3
# Look for it https://www.spatialreference.org/ref/?search=Finland
# see https://stackoverflow.com/questions/59183384/how-to-convert-x-and-y-coordinates-into-latitude-and-longitude
  tas_sf_df <- st_as_sf(tas_df, coords = c("XCoord", "YCoord"), crs = 'EPSG:3067') 
##       plot(tas_sf_df)
  my_latlon_df <- st_transform(tas_sf_df, crs = 4326) 
  my_latlon_df <- my_latlon_df%>%
  dplyr::mutate(long = st_coordinates(my_latlon_df)[,1],
                lat = st_coordinates(my_latlon_df)[,2])

## Should it be weighted? reduced by a factor 0.65!!
##   my_latlon_df$tas <- my_latlon_df$tas * sqrt(cos(my_latlon_df$lat * pi/180))

# Subset FMI points on Lapland:
  coord <- data.frame(x = my_latlon_df$long, y = my_latlon_df$lat)
  tas.sp <- SpatialPointsDataFrame(coord, 
                                   data = data.frame(my_latlon_df$tas))
  proj4string(tas.sp) <- proj4string(shp_lapland)
# coordenadas intersección:
  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$my_latlon_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(-14, 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)
print("MON")
print(mon)
print(dim(df.df))
print(dim(polys_data))
}
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("AXpoints.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("AXpolys.jpg", plot, width = 18, height = 13, bg = 'white')

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

