# Once you know the number of components, here the scores of a pca with varimax rotation is computed.
library(startR)
library(multiApply)
library(CSTools)
library(s2dv)
library(lubridate)
library(parallel)
library(psych)
setwd("/esarchive/scratch/nperez/git/Flor/WildFires/")

# Choose the variable:
varname = 'ta850' # psl ta850 o ta1000 o g500 hur700 o hur950 o
name = 'T850'
num_comp = c(wf500 = 26, wf1000 = 25, wf2500 = 23, wf68000 = 21, wfall = 26)#
if (varname == '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") 
data2 <- Start(dataset = path,
             var = varname,
             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

# Apply latitudinal correction:
data <- multiApply::Apply(list(data), target_dims = 'latitude',
                          fun = function(x) {x * sqrt(cos(lat * pi / 180))})$output1
data <- Reorder(data, c('dataset', 'var', 'months', 'time', 'latitude', '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')
    # Checks:
    #all(data[1,1,2,15, , ] == data2[1,1,2,15, , ,1])
    #PlotEquiMap(data[1,1,2,15, , ], lon = lon, lat = lat,
    #            filled.continents = FALSE) 
    #dev.new()
    #PlotEquiMap(data2[1,1,2,15, , ,1], lon = lon, lat = lat,
    #            filled.continents = FALSE)

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

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

# 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_anom <- MergeDims(data_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')
scores <- NULL
weights <- 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")))

# 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

# Select the dates:
data_500 <- data_anom[1, 1, wf_500_pos, ]

# Varimax too much gridpoints, doesn't work.
#fit <- principal(data_500, nfactors = num_comp[1], rotate = "none", 
#                 scores = TRUE)
c <- prcomp(data_500)
selected <- c$x[1 : num_comp[1]]	
scores <- cbind(scores, selected)
selected <- c$rotation
weig <- fit$weights
weights <- cbind(weights, weig)

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

data_1000 <- data_anom[1, 1, wf_1000_pos, ]

# Varimax:
fit <- principal(data_1000, nfactors = num_comp[2], rotate = "varimax", scores = TRUE)
selected <- fit$scores[, 1:num_comp[2]] # scores de las componentes seleccionadas
colnames(selected) <- as.character(paste0("Score", 1:num_comp[2]))
scores <- cbind(scores, selected)

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

data_2500 <- data_anom[1, 1, wf_2500_pos, ]

# Varimax:
fit <- principal(data_2500, nfactors = num_comp[3], rotate = "varimax",
                 scores = TRUE, eps = 1e-14)
selected <- fit$scores[, 1:num_comp[3]] # scores de las componentes seleccionadas
colnames(selected) <- as.character(paste0("Score", 1:num_comp[3]))
scores <- cbind(scores, selected)

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

data_68000 <- data_anom[1, 1, wf_68000_pos, ]

# Varimax:
fit <- principal(data_68000, nfactors = num_comp[4], rotate = "varimax", scores = TRUE)
selected <- fit$scores[, 1:num_comp[4]] # scores de las componentes seleccionadas
colnames(selected) <- as.character(paste0("Score", 1:num_comp[4]))
scores <- cbind(scores, selected)

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

data_all <- data_anom[1, 1, wf_pos, ]

# Varimax:
fit <- principal(data_1000, nfactors = num_comp[5], rotate = "varimax", scores = TRUE)
selected <- fit$scores[, 1:num_comp[5]] # scores de las componentes seleccionadas
colnames(selected) <- as.character(paste0("Score", 1:num_comp[5]))
scores <- cbind(scores, selected)


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

 
