library(startR)
library(multiApply)
library(CSTools)
library(s2dv)
library(lubridate)
library(parallel)
setwd("/esarchive/scratch/nperez/git/Flor/WildFires/")

#path <- '/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$months$.nc'
path <- '/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h/$var$_$months$.nc'
dates <- seq(ymd('20060101'), ymd('20181201'), by = 'month')
dates <- format(dates, "%Y%m") 
psl <- Start(dataset = path,
             var = 'psl',
             months = dates,
             time = 'all',
             latitude = values(list(30, 70)),
             latitude_reorder = Sort(),
             longitude = values(list(-40, 40)),
             longitude_reorder = CircularSort(-180, 180),
             return_vars = list(latitude = 'dataset',
                                longitude = 'dataset',
                                time = NULL),
             retrieve = TRUE)

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

psl <- SplitDim(psl, 'months', indices = sort(rep(1:13, 12)))
names(dim(psl)) <- c('dataset', 'var', 'month', 'day', 'lat', 'lon', 'year')
# Checks:
#all(psl[1,1,2,15, , ] == psl2[1,1,2,15, , ,1])
#PlotEquiMap(psl[1,1,2,15, , ], lon = lon, lat = lat,
#            filled.continents = FALSE) 
#dev.new()
#PlotEquiMap(psl2[1,1,2,15, , ,1], lon = lon, lat = lat,
#            filled.continents = FALSE)

psl_clim <- MeanDims(psl, dims = 'year')
psl_anom <- psl -  InsertDim(psl_clim, posdim = 7, lendim = 13) 

# Check:
#PlotEquiMap(psl_anom[1,1,2,15, , , 1], lon = lon, lat = lat,
#            filled.continents = FALSE) 

psl_anom <- MergeDims(psl_anom, merge_dims = c('day', 'month'))
psl_anom <- MergeDims(psl_anom, merge_dims = c('day', 'year'))
psl_anom <- MergeDims(psl_anom, merge_dims = c('lat', 'lon'),
                      rename = 'gridpoint')
# -----------------------------------------------------------------
######## 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
# -----------------------------------------------------------------
# Wildfires until 500 ha:
wf_500 <- subset(fires, area_ha < 500)
wf_500_dates <- sort(unique(as.Date(wf_500$date, "%Y-%m-%d")))

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

psl_500 <- psl_anom[1, 1, wf_500_pos, ]

pslSC <- scale(psl_500)
c <- prcomp(pslSC)
vars <- c$sdev^2
vars <- vars/sum(vars)
sum <- cbind(c$sdev, vars, cumsum(vars))
names(sum) <- lapply(c("sdev500.", "var500", "cvar500"), paste, sep="")
sumario <- cbind(sumario, sum[1:100,])
png("/esarchive/scratch/nperez/git/Flor/WildFires/PSL500_Scree.png",
    type = 'cairo')
plot(c, type = "lines", npcs = 20, main = "PSL 500 PCA Variance", col = "blue",
     pch = 16)
dev.off()           

# -----------------------------------------------------------------
# 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

psl_1000 <- psl_anom[1, 1, wf_1000_pos, ]

pslSC <- scale(psl_1000)
c <- prcomp(pslSC)
vars <- c$sdev^2
vars <- vars/sum(vars)
sum <- cbind(c$sdev, vars, cumsum(vars))
names(sum) <- lapply(c("sdev1000.", "var1000", "cvar1000"), paste, sep="")
sumario <- cbind(sumario, sum[1:100,])
png("/esarchive/scratch/nperez/git/Flor/WildFires/PSL1000_Scree.png",
    type = 'cairo')
plot(c, type = "lines", npcs = 20, main = "PSL 1000 PCA Variance", col = "blue",
     pch = 16)
dev.off()


# -----------------------------------------------------------------
# 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

psl_2500 <- psl_anom[1, 1, wf_2500_pos, ]

pslSC <- scale(psl_2500)
c <- prcomp(pslSC)
vars <- c$sdev^2
vars <- vars/sum(vars)
sum <- cbind(c$sdev, vars, cumsum(vars))
names(sum) <- lapply(c("sdev2500.", "var2500", "cvar2500"), paste, sep="")
sumario <- cbind(sumario, sum[1:100,])
png("/esarchive/scratch/nperez/git/Flor/WildFires/PSL2500_Scree.png",
    type = 'cairo')
plot(c, type = "lines", npcs = 20, main = "PSL 2500 PCA Variance", col = "blue",
     pch = 16)
dev.off()




# -----------------------------------------------------------------
# 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

psl_68000 <- psl_anom[1, 1, wf_68000_pos, ]

pslSC <- scale(psl_68000)
c <- prcomp(pslSC)
vars <- c$sdev^2
vars <- vars/sum(vars)
sum <- cbind(c$sdev, vars, cumsum(vars))
names(sum) <- lapply(c("sdev68000.", "var68000", "cvar68000"), paste, sep="")
sumario <- cbind(sumario, sum[1:100,])
png("/esarchive/scratch/nperez/git/Flor/WildFires/PSL68000_Scree.png",
    type = 'cairo')
plot(c, type = "lines", npcs = 20, main = "PSL 68000 PCA Variance", col = "blue",
     pch = 16)
dev.off()


# -----------------------------------------------------------------
# write the summary to choose the number of components:
write.table(sumario, "/esarchive/scratch/nperez/git/Flor/WildFires/PSL_variance.csv",
            row.names = FALSE, sep = ',')

 
