# Saving data in plots
rm(list = ls())
setwd("/esarchive/scratch/nperez/git/Flor/WildFires/")

library(CSTools)
library(s2dv)
library(lubridate)

# ------ Choose k according to 'name' order ------------------
name = c('PSL', 'Z500', 'T850', 'T1000', 'H700', 'H950')
k = 1 # CHANGE IT TO THE DESIRED VARIABLE (from 1 to 6)  
load(file = paste0("./Figures/", name[k], "maps.RData"))

setwd("./Maps/")
# Save as .csv:
for (k in 1:6) {
load(file = paste0("../Figures/", name[k], "maps.RData"))
 for (j in 1:length(data_num_500)) {
   colnames(data_num_500[[j]]) <- lon
   rownames(data_num_500[[j]]) <- lat
   write.table(data_num_500[[j]], file = paste0(name[k], '_500ha_Cluster', j, '.txt'), 
               sep = ' ', col.names = TRUE, row.names = TRUE)
   colnames(data_anom_num_500[[j]]) <- lon
   rownames(data_anom_num_500[[j]]) <- lat
   write.table(data_anom_num_500[[j]], file = paste0(name[k], '_Anomaly_500ha_Cluster', 
                                                     j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
} 
 for (j in 1:length(data_num_1000)) {
   colnames(data_num_1000[[j]]) <- lon
   rownames(data_num_1000[[j]]) <- lat
   write.table(data_num_1000[[j]], file = paste0(name[k], '_1000ha_Cluster', j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
   colnames(data_anom_num_1000[[j]]) <- lon
   rownames(data_anom_num_1000[[j]]) <- lat
   write.table(data_anom_num_1000[[j]], file = paste0(name[k], '_Anomaly_1000ha_Cluster', 
                                                     j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
}
 for (j in 1:length(data_num_2500)) {
   colnames(data_num_2500[[j]]) <- lon
   rownames(data_num_2500[[j]]) <- lat
   write.table(data_num_2500[[j]], file = paste0(name[k], '_2500ha_Cluster', j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
   colnames(data_anom_num_2500[[j]]) <- lon
   rownames(data_anom_num_2500[[j]]) <- lat
   write.table(data_anom_num_2500[[j]], file = paste0(name[k], '_Anomaly_2500ha_Cluster', 
                                                     j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
}
 for (j in 1:length(data_num_68000)) {
   colnames(data_num_68000[[j]]) <- lon
   rownames(data_num_68000[[j]]) <- lat
   write.table(data_num_68000[[j]], file = paste0(name[k], '_68000ha_Cluster', j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
   colnames(data_anom_num_68000[[j]]) <- lon
   rownames(data_anom_num_68000[[j]]) <- lat
   write.table(data_anom_num_500[[j]], file = paste0(name[k], '_Anomaly_68000ha_Cluster', 
                                                     j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
}
 for (j in 1:length(data_num_all)) {
   colnames(data_num_all[[j]]) <- lon
   rownames(data_num_all[[j]]) <- lat
   write.table(data_num_all[[j]], file = paste0(name[k], '_all_Cluster', j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
   colnames(data_anom_num_all[[j]]) <- lon
   rownames(data_anom_num_all[[j]]) <- lat
   write.table(data_anom_num_all[[j]], file = paste0(name[k], '_Anomaly_all_Cluster', 
                                                     j, '.txt'),
               sep = ' ', col.names = TRUE, row.names = TRUE)
  }
}


# PLOTTING OPTION:
# Review k:
k
# ------ Choose kk according to 'extension' order ------------------
extension <- c('500', '1000', '2500', '68000', 'all')
kk = 1  # CHANGE IT TO THE DESIRED EXTENSION (from 1 to 5) 
assign("data_anom", get(paste0('data_anom_num_', extension[kk])))
assign("data", get(paste0('data_num_', extension[kk])))
data <- lapply(data, function(x) {list(contours = x)})
num_events <- c(1746, 375, 241, 142, 1797)
assign("freq", get(paste0('freq_', extension[kk])))




# Fires until 500 ha
if (k == 1) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom, 
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 200), 
           brks = seq(-3300, 3300, 300),  contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = data,
          toptitle = paste(name[k], 'less than 500 ha; N.days:', num_events[kk]), 
          units = 'Pa',  
          titles = paste('Cluster', 1:length(data_anom), 'Freq', freq),
          fileout = paste0(name[k], '_', extension[kk], '_clusters.png'))
} else if (k == 2) {  
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom,
           lat = lat, lon = lon, brks2 = seq(50000, 58000, 500),
           brks = seq(-3000, 3000, 300), contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_500[[1]]),
          list(contours = data_num_500[[2]]), list(contours = data_num_500[[3]]),
          list(contours = data_num_500[[4]]),
          list(contours = data_num_500[[5]]), list(contours = data_num_500[[6]])),
          toptitle = paste(name[k], 'less than 500 ha; N.days:', length(wf_500_pos)),
          units = 'm^2 · s^-2',
          titles = paste('Cluster', 1:length(data_anom_num_500), 'Freq', freq_500),
          fileout = paste0(name[k], '_', extension[kk], '_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom,
           lat = lat, lon = lon, brks2 = seq(255, 310, 5),
           brks = seq(-12, 12, 1),  contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_500[[1]]),
          list(contours = data_num_500[[2]]), list(contours = data_num_500[[3]]),
          list(contours = data_num_500[[4]]),
          list(contours = data_num_500[[5]]), list(contours = data_num_500[[6]])),
          toptitle = paste(name[k], 'less than 500 ha; N.days:', length(wf_500_pos)),
          units = 'K',
          titles = paste('Cluster', 1:length(data_anom_num_500), 'Freq', freq_500),
          fileout = paste0(name[k], '_', extension[kk], '_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom,
           lat = lat, lon = lon, brks2 = seq(20, 100, 20),
           brks = seq(-44, 44, 4), contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_500[[1]]),
          list(contours = data_num_500[[2]]), list(contours = data_num_500[[3]]),
          list(contours = data_num_500[[4]]),
          list(contours = data_num_500[[5]]), list(contours = data_num_500[[6]])),
          toptitle = paste(name[k], 'less than 500 ha; N.days:', length(wf_500_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_500), 'Freq', freq_500),
          fileout = paste0(name[k], '_', extension[kk], '_clusters.png'))
} else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom,
           lat = lat, lon = lon, brks2 = seq(20, 100, 20),
           brks = seq(-30, 30, 4), contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_500[[1]]),
          list(contours = data_num_500[[2]]), list(contours = data_num_500[[3]]),
          list(contours = data_num_500[[4]]),
          list(contours = data_num_500[[5]]), list(contours = data_num_500[[6]])),
          toptitle = paste(name[k], 'less than 500 ha; N.days:', length(wf_500_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_500), 'Freq', freq_500),
          fileout = paste0(name[k], '_', extension[kk], '_clusters.png'))
}
#for(j in 1:length(clu_num_500_pos)) {
#PlotEquiMap(data_anom_num_500[[j]], lon = lon, lat=lat, filled.continents = FALSE,
#            contours = data_num_500[[j]], brks2 = seq(10000, 102400, 500), 
#            fileout = paste0(name[k], '_cluster', j, '_500ha.png'))
#}
