# Plot Variables of table_eco's by cluster_regions

setwd("/esarchive/scratch/nperez/git/Flor/WildFires/table_eco/")
library(ggplot2)
library(dplyr)

# Plots by cluster region
area = FALSE
datos <- NULL
for (eco in 1:10) {
  file <- paste0("table_eco_", eco, ".txt")
  data <- read.table(file, dec = ',')
  data <- cbind(data, dates = as.Date(data$Date))
  data <- cbind(data, year = as.numeric(format(as.Date(data$Date),
                                               format = "%Y")))
  data <- cbind(data, month = as.numeric(format(as.Date(data$Date), 
                                                format = "%m")))
  datos <- rbind(datos, data)
}


for (cluster in 1:18) {
  
  data <- subset(datos, datos$clu_disc == cluster)

  ggplot(data, aes(x = month)) +
    ylim(c(0, 400)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = 1:12) +
    coord_cartesian(xlim = c(1, 12)) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTMonths", cluster, ".png"), width = 3, height = 3)

  ggplot(data, aes(x = year)) + 
    ylim(c(0, 200)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(2005, 2020, 5)) +
    coord_cartesian(xlim = c(2005, 2020)) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTYears", cluster, ".png"), width = 3, height = 3)

  if (area == TRUE) {
    ggplot(data, aes(x = AREA_HA)) +
      ylim(c(0, 2000)) +
      geom_histogram(binwidth = 1000, color = "black", fill = "lightgrey") +
      scale_x_continuous(breaks = seq(0, 70000, 5000)) +
      coord_cartesian(xlim = c(0, 12000)) +
      ggtitle(paste("WT", cluster)) +
      ggsave(paste0("WTArea", cluster, ".png"), width = 3, height = 3)
  }
  total_A <- sum(data$AREA_HA)
  total_B <- sum(subset(data, data$AREA_HA <= 500)$AREA_HA)
  ggplot(subset(data, data$AREA_HA > 1000), aes(x = AREA_HA)) +
    ylim(c(0, 30)) +
    geom_histogram(binwidth = 500, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 70000, 20000)) +
    coord_cartesian(xlim = c(0, 70000)) +
    ggtitle(paste("WT", cluster, "- Area>500Ha")) +
    theme(plot.title = element_text(size = 12, face = "bold")) +
    annotate(geom = "text", x = 60000, y = 25, label = paste(total_A, "ha")) +
    annotate(geom = "text", x = 60000, y = 20, label = paste(total_B, "ha")) +
    ggsave(paste0("WTArea500", cluster, ".png"), width = 3, height = 3)
  
  ggplot(data, aes(x = COUNTRY)) +
    ylim(c(0, 500)) +
    geom_bar(color = "black", fill = "lightgrey") +
    xlim(breaks = c('PT', 'ES', 'FR', 'IT', 'GR')) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTCountry", cluster, ".png"), width = 3, height = 3)

print(paste('WT', cluster, 'days:', dim(data)[1]))

meant <- round(mean(data$TEMP), 2)
sdt <- round(sd(data$TEMP), 2)
 ggplot(data, aes(x = TEMP)) +
    ylim(c(0, 75)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    coord_cartesian(xlim = c(-2, 42)) +
    ggtitle(paste("WT", cluster)) +
    annotate(geom = "text", x = 5, y = 70, label = paste(meant, "degreesC"),
             size = 3) +
    annotate(geom = "text", x = 5, y = 65, label = paste(sdt, "degreesC"),
             size = 3) +
    ggsave(paste0("WTTEMP", cluster, ".png"), width = 3, height = 3)

meanhr <- round(mean(data$RH), 2)
sdhr <- round(sd(data$RH), 2)
 ggplot(data, aes(x = RH)) +
    ylim(c(0, 30)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 100, 20)) +
    coord_cartesian(xlim = c(0, 100)) +
    annotate(geom = "text", x = 70, y = 25, label = paste(meanhr, "%")) +
    annotate(geom = "text", x = 70, y = 23, label = paste(sdhr, "%")) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTRH", cluster, ".png"), width = 3, height = 3)

meanws <- round(mean(data$WS), 2)
sdws <- round(sd(data$WS), 2)
 ggplot(data, aes(x = WS)) +
    ylim(c(0, 150)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 14, 2)) +
    coord_cartesian(xlim = c(0, 14)) +  
    annotate(geom = "text", x = 12, y = 140, label = paste(meanws, "ms-1")) +
    annotate(geom = "text", x = 12, y = 120, label = paste(sdws, "ms-1")) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTWS", cluster, ".png"), width = 3, height = 3)

meanfwi <- round(mean(data$FWI), 2)
sdfwi <- round(sd(data$FWI), 2)
 ggplot(data, aes(x = FWI)) +
    ylim(c(0, 40)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 120, 10)) +
    coord_cartesian(xlim = c(-1, 120)) +
    annotate(geom = "text", x = 110, y = 35, label = paste(meanfwi)) +
    annotate(geom = "text", x = 110, y = 32, label = paste(sdfwi)) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTFWI", cluster, ".png"), width = 3, height = 3)

meanvpd <- round(mean(data$VPD), 2)
sdvpd <- round(sd(data$VPD), 2)
 ggplot(data, aes(x = VPD)) +
    ylim(c(0, 150)) +
    geom_histogram(binwidth = 0.5, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 8, 1)) +
    coord_cartesian(xlim = c(-1, 8)) +
    annotate(geom = "text", x = 0, y = 140, label = paste(meanvpd)) +
    annotate(geom = "text", x = 0, y = 125, label = paste(sdvpd)) +
    ggtitle(paste("WT", cluster)) +
    ggsave(paste0("WTVPD", cluster, ".png"), width = 3, height = 3)

}

