# Plot Variables of table_eco's by eco_regions

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

# Plots by eco region
area = FALSE
for (eco in 1:10) {
  file <- paste0("table_eco_", eco, ".txt")
  data <- read.table(file, dec = ',')
print(summary(data$VPD))
  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")))
  ggplot(data, aes(x = month)) +
    ylim(c(0, 550)) + 
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = 1:12) + 
    coord_cartesian(xlim = c(1, 12)) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoMonths", eco, ".png"), width = 3, height = 3)

  ggplot(data, aes(x = year)) + 
    ylim(c(0, 300)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(2005, 2020, 5)) +
    coord_cartesian(xlim = c(2005, 2020)) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoYears", eco, ".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("Eco region", eco)) +
      ggsave(paste0("EcoArea", eco, ".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, 45)) +
    geom_histogram(binwidth = 500, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(0, 70000, 20000)) +
    coord_cartesian(xlim = c(0, 70000)) +
    ggtitle(paste("Eco region", eco, "- Area>500Ha")) +
    theme(plot.title = element_text(size = 12, face = "bold")) +
    annotate(geom = "text", x = 60000, y = 40, label = paste(total_A, "ha")) +
    annotate(geom = "text", x = 60000, y = 30, label = paste(total_B, "ha")) +
    ggsave(paste0("EcoArea500", eco, ".png"), width = 3, height = 3)
  
  ggplot(data, aes(x = COUNTRY)) +
    ylim(c(0, 2000)) +
    geom_bar(color = "black", fill = "lightgrey") +
    xlim(breaks = c('PT', 'ES', 'FR', 'IT', 'GR')) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoCountry", eco, ".png"), width = 3, height = 3)

 ggplot(data, aes(x = clu_disc)) +
    ylim(c(0, 300)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    scale_x_continuous(breaks = seq(1, 18, 3)) +
    coord_cartesian(xlim = c(1, 18)) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoCluster", eco, ".png"), width = 3, height = 3)

meant <- round(mean(data$TEMP), 2)
sdt <- round(sd(data$TEMP), 2)
 ggplot(data, aes(x = TEMP)) +
    ylim(c(0, 150)) +
    geom_histogram(binwidth = 1, color = "black", fill = "lightgrey") +
    coord_cartesian(xlim = c(-2, 42)) +
    ggtitle(paste("Eco region", eco)) +
    annotate(geom = "text", x = 5, y = 140, label = paste(meant, "degreesC"),
             size = 3) +
    annotate(geom = "text", x = 5, y = 130, label = paste(sdt, "degreesC"),
             size = 3) +
    ggsave(paste0("EcoTEMP", eco, ".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, 80)) +
    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 = 80, label = paste(meanhr, "%")) +
    annotate(geom = "text", x = 70, y = 75, label = paste(sdhr, "%")) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoRH", eco, ".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, 400)) +
    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 = 350, label = paste(meanws, "ms-1")) +
    annotate(geom = "text", x = 12, y = 300, label = paste(sdws, "ms-1")) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoWS", eco, ".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, 45)) +
    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("Eco region", eco)) +
    ggsave(paste0("EcoFWI", eco, ".png"), width = 3, height = 3)

meanvpd <- round(mean(data$VPD), 2)
sdfvpd <- round(sd(data$VPD), 2)
 ggplot(data, aes(x = VPD)) +
    ylim(c(0, 300)) +
    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 = 275, label = paste(meanvpd)) +
    annotate(geom = "text", x = 0, y = 250, label = paste(sdvpd)) +
    ggtitle(paste("Eco region", eco)) +
    ggsave(paste0("EcoVPD", eco, ".png"), width = 3, height = 3)

}

