#library(urbanR)
#library(RNetCDF)
library(sp)
library(sf)
#library(mapview)
library(ncdf4)
# library(rgeos) 
library(ggplot2)
library(plyr) #OJO! la llibreria plyr ha de ser cridada abans que dplyr 
library(dplyr) 
#library(broom)
#library(magrittr)
library(ggpubr)
library(raster)
#library(rgdal)
#library(tmap)
library(gstat)
library(sp)
library(abind)
library(openair)
library(tidyr)
#library(RColorBrewer)
#library(Metrics)
#library(gbm)
#library(lubridate)
#library(matrixStats)
library(leaflet)
library(viridis)
library(viridisLite)



#enter variables from the terminal comand
args = commandArgs(TRUE)
if(length(args)<= 6) {
  pollutant <- args[1]
  start_day <- args[2]
  end_day <- args[3]
  path_nc <- args[4]
  path_save <- args[5]
  
  if(length(args)==6){
    shp_cat <- args[6]
  } else {
    shp_cat <- "contorn"  #if the border is not specified, get only cat contour
  }
  
}else{
  stop("***Arguments missing*** 
       Arguments: 
       pollutant=[no2,co,o3,so2,pm10,pm2p5] 
       start_date=[yyyymmdd] 
       end_date=[yyyymmdd] 
       input_path=[/esarchive/scratch/ccarnere/AQS_database/postprocessed/regional/hourly/]
       output_path=[/esarchive/scratch/abarrant/test_netcdf/]
       administrative_borders=[contorn,vegueries,comarques,municipis,provicies,seccensal,zqa]")
}

print(paste0("Pollutant: ",pollutant, ", Start date: ",start_day,", End date: ",end_day,
             ", Input path: ", path_save, ", Output_path: ", path_nc, ", Add borders: ", shp_cat ))



#open catalonia contour /home/abarrant
base_path_shp <- "/home/abarrant/esarchive/scratch/ccarnere/data/shp/"
shp_paths <- list(
  contorn    = paste0(base_path_shp, "divisions_admin/catalunya/divisions-administratives-v2r1-catalunya-5000-20230928.shp"),
  vegueries  = paste0(base_path_shp, "divisions_admin/vegueries/divisions-administratives-v2r1-vegueries-5000-20230928.shp"),
  comarques  = paste0(base_path_shp, "divisions_admin/comarques/divisions-administratives-v2r1-comarques-5000-20230928.shp"),
  municipis  = paste0(base_path_shp, "divisions_admin/municipis/divisions-administratives-v2r1-municipis-5000-20230928.shp"),
  provincies = paste0(base_path_shp, "divisions_admin/provincies/divisions-administratives-v2r1-provincies-5000-20230928.shp"),
  seccensal  = paste0(base_path_shp, "sec_censals_idescat/bseccenv10sh1f1_20220101_0.shp"),
  zqa        = paste0(base_path_shp, "ZQA_Catalunya/ATMOSFERA_ZONES_QUALITAT_AIRE.shp")
)


#pollutant    <- "no2"
#start_day    <- "20170101"
#end_day      <- "20171231"
#path_nc      <- "/home/abarrant/esarchive/scratch/ccarnere/AQS_database/postprocessed/regional/hourly/"
#path_save    <- "/home/abarrant/esarchive/scratch/abarrant/test_netcdf/"
#shp_cat      <- "contorn"


#choosing which contour to use
shp_admin <- read_sf(shp_paths[shp_cat]) 

start_date <- as.Date(start_day, format = "%Y%m%d")
end_date   <- as.Date(end_day, format = "%Y%m%d")
year <- as.numeric(format(start_date,'%Y'))

if (pollutant == "no2") {
  pollutant <- "sconcno2"
} else if (pollutant == "so2") {
  pollutant <- "sconcso2"
} else if (pollutant == "co") {
  pollutant <- "sconcco"
} else if (pollutant == "o3") {
  pollutant <- "sconco3"
}


#paths for each case
pollutant_dir     <- paste0(path_nc, pollutant, "/")
save_dir          <- paste0(path_save, pollutant, "/", year, "/")
concat_file       <- paste0(save_dir, "concat_", pollutant, "_", start_date,"_",end_date, ".nc")
mean_file         <- paste0(save_dir,"amean_",pollutant,"_",start_date,"_",end_date,".nc")
mean_file_shp     <- paste0(save_dir,"amean_",pollutant,"_",start_date,"_",end_date,".shp")

#check if path_save_dir exists  
dir.create(save_dir, recursive = TRUE, showWarnings = FALSE)






# GENERATION OF MEAN FILE THROUGH COMAND ----------------------------------------------------

#cdo cat ifiles oifile                    #concatenar tots els fitxers de lany /home/abarrant/esarchive/scratch/abarrant/test_netcdf/no2_2017/sconcno2_2017*
#cdo timmean oifile ofile                 #mitjana temporal sencera



#time range
date_seq <- seq.Date(start_date, end_date, by = "day")



#list of file names (fer aqui el if de si existeix el file q faci lappend)
file_list <- lapply(date_seq, function(date) {
  file_name <- paste0(pollutant_dir, pollutant, "_", format(date, "%Y%m%d"), "00.nc")
})


#ha de regenerar el concat_file si ja existeix pq sino s'afegeix?
if (file.exists(concat_file)) {
  unlink(concat_file) #delete the file
}

if (file.exists(mean_file)) {
  unlink(mean_file)
}



#concatenation and time mean
if (length(file_list) > 0) {
  file_list_str <- paste(file_list, collapse = " ")
  system(paste0("cdo cat ", file_list_str, " ", concat_file))  #concatenate the files
  
  #check for missing values in the concatenated file
  #system(paste0("cdo infon ", concat_file))
  
  #temporal mean (timmean) and setctomiss makes sure that nans are considered as nans
  system(paste0("cdo -timmean -setctomiss,nan ", concat_file, " ", mean_file))  
} else {
  stop("No files found.")
}







# PLOTING MEAN FILE---------------------------------------------------------------


legend_title <- function(pollutant) {
  names_vector <- c(
    "sconcno2" = "NO2",
    "sconcco" = "CO",
    "sconco3" = "O3",
    "sconcso2" = "SO2",
    "sconcpm10" = "PM10",
    "sconcpm25" = "PM2.5")
  return(names_vector[pollutant])
}


plot_annualmean <- function(nc_annual, start_date, end_date, pollutant){
  "plotting the annual mean netcdf file with catalonia contour (conversion to raster)
  input: netcdf annual mean file, year and pollutant
  output: plot"
  
  
  #nc_annual <- nc_open(mean_file)
  lon <- ncvar_get(nc_annual, "lon")
  lat <- ncvar_get(nc_annual, "lat", verbose = F)
  t <- ncvar_get(nc_annual, "time")  #24 hours
  
  sconcno2.array <- ncvar_get(nc_annual, pollutant) 
  
  #generation of raster
  r_sconcno2 <- raster(t(sconcno2.array), xmn=min(lon), xmx=max(lon), ymn=min(lat), ymx=max(lat), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
  r_sconcno2 <- flip(r_sconcno2, direction='y') 
  #mapview::mapview(r_sconcno2)
  
  #save into shapefile 
  #r_sconcno2 <- aggregate(r_sconcno2, fact=2)              #reduce resolution to half
  polygons <- rasterToPolygons(r_sconcno2, dissolve=TRUE)   #convert to polygons (spatial points)
  polygons_sf <- st_as_sf(polygons)
  st_write(polygons_sf, mean_file_shp, delete_dsn = TRUE)   #delete_dsn: overwrites any existing file
  
  
  #check if they have the same projection, otherwise shp_admin will be transformed to sconcno2 proj
  crs_raster <- crs(r_sconcno2)
  crs_shapefile <- st_crs(shp_admin) 
  
  if (crs_raster@projargs != crs_shapefile$proj4string) {
    #if CRS is different, reproject shapefile to match raster
    shp_admin <- st_transform(shp_admin, crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0")) #CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0")
  }
  
  #converting to df
  r_sconcno2_df <- as.data.frame(r_sconcno2, xy = TRUE) 
  names(r_sconcno2_df)[3] <- "value"    
  
  ggplot() + 
    geom_tile(data=r_sconcno2_df, aes(x=x, y=y, fill=value)) +
    geom_sf(data=shp_admin, fill=NA, color="white") +
    ggtitle(paste0(legend_title(pollutant)," mean from ",start_date, " to ", end_date)) +
    scale_fill_viridis_c(option="inferno") +   # limits=c(0, 55)
    theme(plot.background = element_rect(fill = "white"),
          panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_rect(fill = "white"),
          axis.ticks = element_blank(),
          axis.text = element_blank(),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          plot.title = element_text(hjust = 0.5)) +
    labs(fill =  paste0(legend_title(pollutant),expression("\n(\u03bcg/m³)"))) 
  
  #saving of the plot in the save directory, it wont be shown in R plots 
  ggsave(paste0(save_dir,pollutant,"_mean_",start_date, "_", end_date,".png"), width=5, height=5) 
}


#mean_file <- "/home/abarrant/esarchive/scratch/abarrant/test_netcdf/sconcno2/2020/amean_sconcno2_2020_0101-1231.nc"
nc_annual_sconcno2 <- nc_open(mean_file)
plot_annualmean(nc_annual_sconcno2, start_date, end_date, pollutant)
















