# Create the composites and plots
setwd("/esarchive/scratch/nperez/git/Flor/WildFires/")
clusters <- read.table("Clusters_groups.csv", skip = 1, na.string = 'NA')
colnames(clusters) <- c('All', '500ha', '1000ha', '2500ha', '68000ha')

library(startR)
library(multiApply)
library(CSTools)
library(s2dv)
library(lubridate)

# Function to undo change: from position to dates:
wf_date <- function(position) {
  year = trunc(position/372)
  month = trunc((position/372 - year) * 12) + 1
  day = round(((position/372 - year) * 12 - month + 1) * 31, 0)
  year = year + 2006
  return(as.Date(paste(year, month, day, sep = "-"), format = "%Y-%m-%d"))
}

# -----------------------------------------------------------------
######## DATES WILDFIRES:
# -----------------------------------------------------------------
fires <- read.table('EFFIS02.csv', sep = ',', skip = 1)
colnames(fires) <- c('N', 'date', 'country', 'area_ha', 'place', 'province',
                     'yearseason', 'fireid', 'ID')
sumario <- NULL
i <- 1
# -----------------------------------------------------------------
# Wildfires until 500 ha:
wf_500 <- subset(fires, area_ha < 500)
wf_500_dates <- sort(unique(as.Date(wf_500$date, "%Y-%m-%d")))

# Create vector of positions:
day <- day(wf_500_dates)
month <- month(wf_500_dates)
year <- year(wf_500_dates)-2005
wf_500_pos <- (year - 1) * 12 * 31 + (month - 1) * 31 + day

#Subset and identify days of each cluster:
clu_500 <- clusters[!is.na(clusters[,2]),2]
clu_num_500 <- lapply(1:max(clu_500), function(x){which(clu_500 == x)})
clu_num_500_pos <- lapply(1:max(clu_500), function(x){wf_500_pos[clu_num_500[[x]]]})
clu_num_500_dates <- lapply(1:max(clu_500), function(x){wf_500_dates[clu_num_500[[x]]]})
tot_500 <- lapply(clu_num_500, length)
freq_500 <- round(unlist(lapply(tot_500, function(x){x/length(clu_500)*100})),1)


# -----------------------------------------------------------------
# Wildfires until 1000 ha:
wf_1000 <- subset(fires, area_ha >= 500 & area_ha < 1000)
wf_1000_dates <- sort(unique(as.Date(wf_1000$date, "%Y-%m-%d")))

day <- day(wf_1000_dates)
month <- month(wf_1000_dates)
year <- year(wf_1000_dates)-2005
wf_1000_pos <- (year - 1) * 12 * 31 + (month - 1) * 31 + day

#Subset and identify days of each cluster:
clu_1000 <- clusters[!is.na(clusters[, 3]), 3]
clu_num_1000 <- lapply(1:max(clu_1000), function(x){which(clu_1000 == x)})
clu_num_1000_pos <- lapply(1:max(clu_1000), function(x){wf_1000_pos[clu_num_1000[[x]]]})
clu_num_1000_dates <- lapply(1:max(clu_1000), function(x){wf_1000_dates[clu_num_1000[[x]]]})
tot_1000 <- lapply(clu_num_1000, length)
freq_1000 <- round(unlist(lapply(tot_1000, function(x){x / length(clu_1000) * 100})), 1)

# -----------------------------------------------------------------
# Wildfires until 2500 ha:
wf_2500 <- subset(fires, area_ha >= 1000 & area_ha < 2500)
wf_2500_dates <- sort(unique(as.Date(wf_2500$date, "%Y-%m-%d")))

day <- day(wf_2500_dates)
month <- month(wf_2500_dates)
year <- year(wf_2500_dates)-2005
wf_2500_pos <- (year - 1) * 12 * 31 + (month - 1) * 31 + day

#Subset and identify days of each cluster:
clu_2500 <- clusters[!is.na(clusters[, 4]), 4]
clu_num_2500 <- lapply(1:max(clu_2500), function(x){which(clu_2500 == x)})
clu_num_2500_pos <- lapply(1:max(clu_2500), function(x){wf_2500_pos[clu_num_2500[[x]]]})
clu_num_2500_dates <- lapply(1:max(clu_2500), function(x){wf_2500_dates[clu_num_2500[[x]]]})
tot_2500 <- lapply(clu_num_2500, length)
freq_2500 <- round(unlist(lapply(tot_2500, function(x){x / length(clu_2500) * 100})), 1)

# -----------------------------------------------------------------
# Wildfires greater than 2500 ha:
wf_68000 <- subset(fires, area_ha >= 2500)
wf_68000_dates <- sort(unique(as.Date(wf_68000$date, "%Y-%m-%d")))

day <- day(wf_68000_dates)
month <- month(wf_68000_dates)
year <- year(wf_68000_dates)-2005
wf_68000_pos <- (year - 1) * 12 * 31 + (month - 1) * 31 + day

#Subset and identify days of each cluster:
clu_68000 <- clusters[!is.na(clusters[, 5]), 5]
clu_num_68000 <- lapply(1:max(clu_68000), function(x){which(clu_68000 == x)})
clu_num_68000_pos <- lapply(1:max(clu_68000), function(x){wf_68000_pos[clu_num_68000[[x]]]})
clu_num_68000_dates <- lapply(1:max(clu_68000), function(x){wf_68000_dates[clu_num_68000[[x]]]})
tot_68000 <- lapply(clu_num_68000, length)
freq_68000 <- round(unlist(lapply(tot_68000, function(x){x/length(clu_68000) * 100})), 1)

# -----------------------------------------------------------------
# Wildfires all dates:
wf_dates <- sort(unique(as.Date(fires$date, "%Y-%m-%d")))

day <- day(wf_dates)
month <- month(wf_dates)
year <- year(wf_dates)-2005
wf_pos <- (year - 1) * 12 * 31 + (month - 1) * 31 + day

#Subset and identify days of each cluster:
clu_all <- clusters[!is.na(clusters[, 1]), 1]
clu_num_all <- lapply(1:max(clu_all), function(x){which(clu_all == x)})
clu_num_all_pos <- lapply(1:max(clu_all), function(x){wf_pos[clu_num_all[[x]]]})
clu_num_all_dates <- lapply(1:max(clu_all), function(x){wf_dates[clu_num_all[[x]]]})
tot_all <- lapply(clu_num_all, length)
freq_all <- round(unlist(lapply(tot_all, function(x){x/length(clu_all) * 100})), 1)

# -----------------------------------------------------------------
# Datasets!
# -----------------------------------------------------------------
# Choose the variable:
varname = c('psl', 'g500', 'ta850', 'ta1000', 'hur700', 'hur950')
name = c('PSL', 'Z500', 'T850', 'T1000', 'H700', 'H950')

setwd("./Figures")
for (k in 1:length(varname)) {

if (varname[k] == 'psl') {
  path <- '/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h/$var$_$months$.nc'
} else {
  path <- '/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$months$.nc'
}
# Load the gridded data:
dates <- seq(ymd('20060101'), ymd('20181201'), by = 'month')
dates <- format(dates, "%Y%m")
data <- Start(dataset = path,
             var = varname[k],
             months = dates,
             time = 'all',
             latitude = values(list(30, 70)),
             latitude_reorder = Sort(decreasing = FALSE),
             longitude = values(list(-40, 40)),
             longitude_reorder = CircularSort(-180, 180),
             #transform = CDORemapper,
             #transform_extra_cells = 2,
             #transform_params = list(grid = 'r360x181',
             #                        method = 'conservative',
             #                        crop = c(-40, 40,
             #                                 30, 70)),
             #transform_vars = c('latitude', 'longitude'),
             synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('lon', 'longitude')),
             return_vars = list(latitude = 'dataset',
                                longitude = 'dataset',
                                time = NULL),
             retrieve = TRUE)

time <- attributes(data)$Variables$common$time
lat <- attributes(data)$Variables$dat1$latitude
lon <- attributes(data)$Variables$dat1$longitude


# Split by day of the year to compute the climatologies:
data <- SplitDim(data, 'months', indices = sort(rep(1:13, 12)))
names(dim(data)) <- c('dataset', 'var', 'month', 'day', 'lat', 'lon', 'year')

# Compute the climatologies:
data_clim <- MeanDims(data, dims = 'year')
# Compute the anomalies
data_anom <- data -  InsertDim(data_clim, posdim = 7, lendim = 13)

# Get a matrix of time vs gridpoint
data_anom <- MergeDims(data_anom, merge_dims = c('day', 'month'))
data_anom <- MergeDims(data_anom, merge_dims = c('day', 'year'))
data <- MergeDims(data, merge_dims = c('day', 'month'))
data <- MergeDims(data, merge_dims = c('day', 'year'))
#data_anom <- MergeDims(data_anom, merge_dims = c('lat', 'lon'),
#                      rename = 'gridpoint')
# -----------------------------------------------------------------
# Fires until 500 ha
# Select the dates:
data_anom_num_500 <- lapply(1:length(clu_num_500_pos), function(x){
                     MeanDims(data_anom[1,1,clu_num_500_pos[[x]],,], 'day')})
data_num_500 <- lapply(1:length(clu_num_500_pos), function(x){
                     MeanDims(data[1,1,clu_num_500_pos[[x]],,], 'day')})
anoms_500 <- unlist(data_anom_num_500)
dim(anoms_500) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_500_pos))
anoms_500 <- Reorder(anoms_500, c('clu', 'lat', 'lon'))
dats_500 <- unlist(data_num_500)
dim(dats_500) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_500_pos))
dats_500 <- Reorder(dats_500, c('clu', 'lat', 'lon'))

if (k == 1) {
options(bitmapType="cairo")
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_500, 
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 200), 
           brks = seq(-3300, 3300, 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 = 'Pa', 
          titles = paste('Cluster', 1:length(data_anom_num_500), 'Freq', freq_500),
          fileout = paste0(name[k], '_500ha_clusters.png'))
} else if (k == 2) {  
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_500,
           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], '_500ha_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_500,
           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], '_500ha_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_500,
           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], '_500ha_clusters.png'))
} else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_500,
           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], '_500ha_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'))
#}
#save(anoms_500, data_num_500, lon, lat, file = 'PSLCluster500.RData', version = 3)
# -----------------------------------------------------------------
# Fires until 1000 ha
# Select the dates:
data_anom_num_1000 <- lapply(1:length(clu_num_1000_pos), function(x){
                     MeanDims(data_anom[1,1,clu_num_1000_pos[[x]],,], 'day')})
data_num_1000 <- lapply(1:length(clu_num_1000_pos), function(x){
                     MeanDims(data[1,1,clu_num_1000_pos[[x]],,], 'day')})
anoms_1000 <- unlist(data_anom_num_1000)
dim(anoms_1000) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_1000_pos))
dats_1000 <- unlist(data_num_1000)
dim(dats_1000) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_1000_pos))

if (k == 1) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_1000,
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 500), 
           brks = seq(-12, 12, 1),
           brks = seq(-3300, 3300, 300),  contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_1000[[1]]),
          list(contours = data_num_1000[[2]]), list(contours = data_num_1000[[3]]),
          list(contours = data_num_1000[[4]]),
          list(contours = data_num_1000[[5]]), list(contours = data_num_1000[[6]]),
          list(contours = data_num_1000[[7]]), list(contours = data_num_1000[[8]]),
          list(contours = data_num_1000[[9]])), title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '500 < A < 1000 ha; N.days:', length(wf_1000_pos)), 
          units = 'Pa',
          titles = paste('Cluster', 1:length(data_anom_num_1000), 'Freq', freq_1000),
          fileout = paste0(name[k], '_1000ha_clusters.png'))
} else if (k == 2) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_1000,
           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_1000[[1]]),
          list(contours = data_num_1000[[2]]), list(contours = data_num_1000[[3]]),
          list(contours = data_num_1000[[4]]),
          list(contours = data_num_1000[[5]]), list(contours = data_num_500[[6]]),
          list(contours = data_num_1000[[7]]), list(contours = data_num_1000[[8]]),
          list(contours = data_num_1000[[9]])),
          toptitle = paste(name[k], '500 < A < 1000 ha; N.days:', length(wf_1000_pos)),
          units =  'm^2 · s^-2',
          titles = paste('Cluster', 1:length(data_anom_num_1000), 'Freq', freq_1000),
          fileout = paste0(name[k], '_1000ha_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_1000,
           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_1000[[1]]),
          list(contours = data_num_1000[[2]]), list(contours = data_num_1000[[3]]),
          list(contours = data_num_1000[[4]]),
          list(contours = data_num_1000[[5]]), list(contours = data_num_1000[[6]]),
          list(contours = data_num_1000[[7]]), list(contours = data_num_1000[[8]]),
          list(contours = data_num_1000[[9]])), title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '500 < A < 1000 ha; N.days:', length(wf_1000_pos)),
          units = 'K',
          titles = paste('Cluster', 1:length(data_anom_num_1000), 'Freq', freq_1000),
          fileout = paste0(name[k], '_1000ha_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_1000,
           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_1000[[1]]),
          list(contours = data_num_1000[[2]]), list(contours = data_num_1000[[3]]),
          list(contours = data_num_1000[[4]]),
          list(contours = data_num_1000[[5]]), list(contours = data_num_1000[[6]]),
          list(contours = data_num_1000[[7]]), list(contours = data_num_1000[[8]]),
          list(contours = data_num_1000[[9]])), title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '500 < A < 1000 ha; N.days:', length(wf_1000_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_1000), 'Freq', freq_1000),
          fileout = paste0(name[k], '_1000ha_clusters.png'))
} else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_1000,
           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_1000[[1]]),
          list(contours = data_num_1000[[2]]), list(contours = data_num_1000[[3]]),
          list(contours = data_num_1000[[4]]),
          list(contours = data_num_1000[[5]]), list(contours = data_num_1000[[6]]),
          list(contours = data_num_1000[[7]]), list(contours = data_num_1000[[8]]),
          list(contours = data_num_1000[[9]])), title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '500 < A < 1000 ha; N.days:', length(wf_1000_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_1000), 'Freq', freq_1000),
          fileout = paste0(name[k], '_1000ha_clusters.png'))
}


#for(j in 1:length(clu_num_1000_pos)) {
#PlotEquiMap(data_anom_num_1000[[j]], lon = lon, lat=lat, filled.continents = FALSE,
#            contours = data_num_1000[[j]], brks2 = seq(10000, 102400, 500),
#            fileout = paste0(name[k], '_cluster', j, '_1000ha.png'))
#}


# -----------------------------------------------------------------
# Fires until 2500 ha
# Select the dates:
data_anom_num_2500 <- list(NULL, NULL, NULL, NULL)
data_anom_num_2500[[1]] <-  MeanDims(data_anom[1,1,clu_num_2500_pos[[1]],,], 'day')
data_anom_num_2500[[2]] <- data_anom[1,1,clu_num_2500_pos[[2]],,] 
data_anom_num_2500[[3]] <-  MeanDims(data_anom[1,1,clu_num_2500_pos[[3]],,], 'day')
data_anom_num_2500[[4]] <-  MeanDims(data_anom[1,1,clu_num_2500_pos[[4]],,], 'day')
data_num_2500 <- list(NULL, NULL, NULL, NULL)
data_num_2500[[1]] <-  MeanDims(data[1,1,clu_num_2500_pos[[1]],,], 'day')
data_num_2500[[2]] <- data[1,1,clu_num_2500_pos[[2]],,]
data_num_2500[[3]] <-  MeanDims(data[1,1,clu_num_2500_pos[[3]],,], 'day')
data_num_2500[[4]] <-  MeanDims(data[1,1,clu_num_2500_pos[[4]],,], 'day')

anoms_2500 <- unlist(data_anom_num_2500)
dim(anoms_2500) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_2500_pos))
dats_2500 <- unlist(data_num_2500)
dim(dats_2500) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_2500_pos))

if (k == 1) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_2500,
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 500),
           brks = seq(-3300, 3300, 300),  contour_color = 'darkgreen',
           filled.continents = FALSE, special_args = list(list(contours = data_num_2500[[1]]),
          list(contours = data_num_2500[[2]]), list(contours = data_num_2500[[3]]),
          list(contours = data_num_2500[[4]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '1000 < A < 2500 ha; N.days:', length(wf_2500_pos)), 
          units = 'Pa',
          titles = paste('Cluster', 1:length(data_anom_num_2500), 'Freq', freq_2500),
          fileout = paste0(name[k], '_2500ha_clusters.png'))
} else if (k == 2) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_2500,
           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_2500[[1]]),
          list(contours = data_num_2500[[2]]), list(contours = data_num_2500[[3]]),
          list(contours = data_num_2500[[4]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '1000 < A < 2500 ha; N.days:', length(wf_2500_pos)),
          units = 'm^2 · s^-2',
          titles = paste('Cluster', 1:length(data_anom_num_2500), 'Freq', freq_2500),
          fileout = paste0(name[k], '_2500ha_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_2500,
           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_2500[[1]]),
          list(contours = data_num_2500[[2]]), list(contours = data_num_2500[[3]]),
          list(contours = data_num_2500[[4]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '1000 < A < 2500 ha; N.days:', length(wf_2500_pos)),
          units = 'K',
          titles = paste('Cluster', 1:length(data_anom_num_2500), 'Freq', freq_2500),
          fileout = paste0(name[k], '_2500ha_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_2500,
           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_2500[[1]]),
          list(contours = data_num_2500[[2]]), list(contours = data_num_2500[[3]]),
          list(contours = data_num_2500[[4]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '1000 < A < 2500 ha; N.days:', length(wf_2500_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_2500), 'Freq', freq_2500),
          fileout = paste0(name[k], '_2500ha_clusters.png'))
} else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_2500,
           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_2500[[1]]),
          list(contours = data_num_2500[[2]]), list(contours = data_num_2500[[3]]),
          list(contours = data_num_2500[[4]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], '1000 < A < 2500 ha; N.days:', length(wf_2500_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_2500), 'Freq', freq_2500),
          fileout = paste0(name[k], '_2500ha_clusters.png'))
}


#for(j in 1:length(clu_num_2500_pos)) {
#PlotEquiMap(data_anom_num_2500[[j]], lon = lon, lat=lat, filled.continents = FALSE,
#            contours = data_num_2500[[j]], brks2 = seq(10000, 102400, 500),
#            fileout = paste0(name[k], '_cluster', j, '_2500ha.png'))
#}

# -----------------------------------------------------------------
# Fires greater than 2500 ha
# Select the dates:
data_anom_num_68000 <- lapply(1:length(clu_num_68000_pos), function(x){
                     MeanDims(data_anom[1,1,clu_num_68000_pos[[x]],,], 'day')})
data_num_68000 <- lapply(1:length(clu_num_68000_pos), function(x){
                     MeanDims(data[1,1,clu_num_68000_pos[[x]],,], 'day')})
anoms_68000 <- unlist(data_anom_num_68000)
dim(anoms_68000) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_68000_pos))
dats_68000 <- unlist(data_num_68000)
dim(dats_68000) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_68000_pos))

if (k == 1) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_68000,
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 500),
           brks = seq(-3300, 3300, 300), contour_color = 'darkgreen',
           filled.continents = FALSE,
          special_args = list(list(contours = data_num_68000[[1]]),
          list(contours = data_num_68000[[2]]), list(contours = data_num_68000[[3]]),
          list(contours = data_num_68000[[4]]), list(contours = data_num_68000[[5]]),
          list(contours = data_num_68000[[6]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], 'area greater than 2500 ha; N.days:', 
                           length(wf_68000_pos)), 
          units = 'Pa',
          titles = paste('Cluster', 1:length(data_anom_num_68000), 'Freq', freq_68000),
          fileout = paste0(name[k], '_68000ha_clusters.png'))
} else if (k == 2) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_68000,
           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_68000[[1]]),
          list(contours = data_num_68000[[2]]), list(contours = data_num_68000[[3]]),
          list(contours = data_num_68000[[4]]), list(contours = data_num_68000[[5]]),
          list(contours = data_num_68000[[6]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], 'area greater than 2500 ha; N.days:',
                           length(wf_68000_pos)),
          units = 'm^2 · s^-2',
          titles = paste('Cluster', 1:length(data_anom_num_68000), 'Freq', freq_68000),
          fileout = paste0(name[k], '_68000ha_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_68000,
           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_68000[[1]]),
          list(contours = data_num_68000[[2]]), list(contours = data_num_68000[[3]]),
          list(contours = data_num_68000[[4]]), list(contours = data_num_68000[[5]]),
          list(contours = data_num_68000[[6]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], 'area greater than 2500 ha; N.days:',
                           length(wf_68000_pos)),
          units = 'K',
          titles = paste('Cluster', 1:length(data_anom_num_68000), 'Freq', freq_68000),
          fileout = paste0(name[k], '_68000ha_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_68000,
           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_68000[[1]]),
          list(contours = data_num_68000[[2]]), list(contours = data_num_68000[[3]]),
          list(contours = data_num_68000[[4]]), list(contours = data_num_68000[[5]]),
          list(contours = data_num_68000[[6]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], 'area greater than 2500 ha; N.days:',
                           length(wf_68000_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_68000), 'Freq', freq_68000),
          fileout = paste0(name[k], '_68000ha_clusters.png'))
} else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_68000,
           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_68000[[1]]),
          list(contours = data_num_68000[[2]]), list(contours = data_num_68000[[3]]),
          list(contours = data_num_68000[[4]]), list(contours = data_num_68000[[5]]),
          list(contours = data_num_68000[[6]])), #title_scale = 0.7, bar_scale = 0.7,
          toptitle = paste(name[k], 'area greater than 2500 ha; N.days:',
                           length(wf_68000_pos)),
          units = '%',
          titles = paste('Cluster', 1:length(data_anom_num_68000), 'Freq', freq_68000),
          fileout = paste0(name[k], '_68000ha_clusters.png'))
}
#for(j in 1:length(clu_num_68000_pos)) {
#PlotEquiMap(data_anom_num_68000[[j]], lon = lon, lat=lat, filled.continents = FALSE,
#            contours = data_num_68000[[j]], brks2 = seq(10000, 102400, 500),
#            fileout = paste0(name[k], '_cluster', j, '_68000ha.png'))
#}

# -----------------------------------------------------------------
# Fires until 500 ha
# Select the dates:
data_anom_num_all <- lapply(1:length(clu_num_all_pos), function(x){
                     MeanDims(data_anom[1,1,clu_num_all_pos[[x]],,], 'day')})
data_num_all <- lapply(1:length(clu_num_all_pos), function(x){
                     MeanDims(data[1,1,clu_num_all_pos[[x]],,], 'day')})
anoms_all <- unlist(data_anom_num_all)
dim(anoms_all) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_all_pos))
dats_all <- unlist(data_num_all)
dim(dats_all) <- c(lat = length(lat), lon = length(lon), clu = length(clu_num_all_pos))

if (k == 1) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_all,
           lat = lat, lon = lon, brks2 = seq(10000, 102400, 500),
           brks = seq(-3300, 3300, 300), contour_color = 'darkgreen', 
          filled.continents = FALSE,
          special_args = list(list(contours = data_num_68000[[1]],title_scale = 0.7),
          list(contours = data_num_all[[2]], title_scale = 0.7),
          list(contours = data_num_all[[3]], title_scale = 0.7),
          list(contours = data_num_all[[4]], title_scale = 0.7),
          list(contours = data_num_all[[5]], title_scale = 0.7),
          list(contours = data_num_all[[6]], title_scale = 0.7),
           list(contours = data_num_all[[7]], title_scale = 0.7),
          list(contours = data_num_all[[8]], title_scale = 0.7),
           list(contours = data_num_all[[9]], title_scale = 0.7),
          list(contours = data_num_all[[10]], title_scale = 0.7), 
          list(contours = data_num_all[[11]], title_scale = 0.7)), 
          title_scale = 0.7, bar_scale = 0.7, units = 'Pa',
          toptitle = paste(name[k], 'all dates; N.days:', length(wf_pos)),
          titles = paste('Cluster', 1:length(data_anom_num_all), 'Freq', freq_all),
          fileout = paste0(name[k], '_allha_clusters.png'))
#for(j in 1:length(clu_num_all_pos)) {
#PlotEquiMap(data_anom_num_all[[j]], lon = lon, lat=lat, filled.continents = FALSE,
#            contours = data_num_all[[j]], brks2 = seq(10000, 102400, 500),
#            fileout = paste0(name[k], '_cluster', j, '_all.png'))
#}

} else if (k == 2) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_all,
           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_all[[1]],title_scale = 0.7),
                         list(contours = data_num_all[[2]], title_scale = 0.7),
                         list(contours = data_num_all[[3]], title_scale = 0.7),
                          list(contours = data_num_all[[4]], title_scale = 0.7),
                          list(contours = data_num_all[[5]], title_scale = 0.7),
                          list(contours = data_num_all[[6]], title_scale = 0.7),
                           list(contours = data_num_all[[7]], title_scale = 0.7),
                          list(contours = data_num_all[[8]], title_scale = 0.7),
                           list(contours = data_num_all[[9]], title_scale = 0.7),
                          list(contours = data_num_all[[10]], title_scale = 0.7),
                          list(contours = data_num_all[[11]], title_scale = 0.7)),
          title_scale = 0.7, bar_scale = 0.7, units = 'm^2 · s^-2',
          toptitle = paste(name[k], 'all dates; N.days:', length(wf_pos)),
          titles = paste('Cluster', 1:length(data_anom_num_all), 'Freq', freq_all),
          fileout = paste0(name[k], '_allha_clusters.png'))
} else if (k == 3 | k == 4) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_all,
           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_all[[1]],title_scale = 0.7),
                         list(contours = data_num_all[[2]], title_scale = 0.7),
                         list(contours = data_num_all[[3]], title_scale = 0.7),
                          list(contours = data_num_all[[4]], title_scale = 0.7),
                          list(contours = data_num_all[[5]], title_scale = 0.7),
                          list(contours = data_num_all[[6]], title_scale = 0.7),
                           list(contours = data_num_all[[7]], title_scale = 0.7),
                          list(contours = data_num_all[[8]], title_scale = 0.7),
                           list(contours = data_num_all[[9]], title_scale = 0.7),
                          list(contours = data_num_all[[10]], title_scale = 0.7),
                          list(contours = data_num_all[[11]], title_scale = 0.7)),
          title_scale = 0.7, bar_scale = 0.7, units = 'K',
          toptitle = paste(name[k], 'all dates; N.days:', length(wf_pos)),
          titles = paste('Cluster', 1:length(data_anom_num_all), 'Freq', freq_all),
          fileout = paste0(name[k], '_allha_clusters.png'))
} else if (k == 5) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_all,
           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_all[[1]],title_scale = 0.7),
                         list(contours = data_num_all[[2]], title_scale = 0.7),
                         list(contours = data_num_all[[3]], title_scale = 0.7),
                          list(contours = data_num_all[[4]], title_scale = 0.7),
                          list(contours = data_num_all[[5]], title_scale = 0.7),
                          list(contours = data_num_all[[6]], title_scale = 0.7),
                           list(contours = data_num_all[[7]], title_scale = 0.7),
                          list(contours = data_num_all[[8]], title_scale = 0.7),
                           list(contours = data_num_all[[9]], title_scale = 0.7),
                          list(contours = data_num_all[[10]], title_scale = 0.7),
                          list(contours = data_num_all[[11]], title_scale = 0.7)),
          title_scale = 0.7, bar_scale = 0.7, units = '%',
          toptitle = paste(name[k], 'all dates; N.days:', length(wf_pos)),
          titles = paste('Cluster', 1:length(data_anom_num_all), 'Freq', freq_all),
          fileout = paste0(name[k], '_allha_clusters.png'))
}  else if (k == 6) {
PlotLayout(PlotEquiMap, plot_dims = c('lat', 'lon'), var = data_anom_num_all,
           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_all[[1]],title_scale = 0.7),
                         list(contours = data_num_all[[2]], title_scale = 0.7),
                         list(contours = data_num_all[[3]], title_scale = 0.7),
                          list(contours = data_num_all[[4]], title_scale = 0.7),
                          list(contours = data_num_all[[5]], title_scale = 0.7),
                          list(contours = data_num_all[[6]], title_scale = 0.7),
                           list(contours = data_num_all[[7]], title_scale = 0.7),
                          list(contours = data_num_all[[8]], title_scale = 0.7),
                           list(contours = data_num_all[[9]], title_scale = 0.7),
                          list(contours = data_num_all[[10]], title_scale = 0.7),
                          list(contours = data_num_all[[11]], title_scale = 0.7)),
          title_scale = 0.7, bar_scale = 0.7, units = '%',
          toptitle = paste(name[k], 'all dates; N.days:', length(wf_pos)),
          titles = paste('Cluster', 1:length(data_anom_num_all), 'Freq', freq_all),
          fileout = paste0(name[k], '_allha_clusters.png'))
}
save(data_anom_num_500, data_num_500, data_anom_num_1000, data_num_1000,
     data_anom_num_2500, data_num_2500, data_anom_num_68000, data_num_68000, 
     data_anom_num_all, data_num_all, lat, lon, freq_500, freq_1000, freq_2500,
     freq_68000, freq_all, file = paste0(name[k], 'maps.RData'), version = 3)
}
