library(startR)
library(s2dv)

hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = '01',
               time = 'all',
               lat = indices(1),
               lon = indices(1), num_procs = 2,
               retrieve = TRUE)

plot(1979:2018, MeanDims(hrmin, 'time'), type = 'b')

# Test mean values:
hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
       lon = 'all',
       num_procs = 2,
               debug = TRUE,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/", list.files("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/")) == sort(as.vector(attributes(hrmin)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  s2dv::MeanDims(datos, c('time', 'year'))
  
}
step <- Step(fun = temp_mean, 
             target_dims = c('time', 'year'),
             output_dims = 'time')
  
wf <- AddStep(hrmin, step)

queue_host = 'nord4'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory
  
res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 2,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'slurm',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '08:00:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
              wait = FALSE)

 saveRDS(res, file = 'test_collect.Rds')   

# Collect results
library(startR)
res <- readRDS('test_collect.Rds')
result <- Collect(res, wait = TRUE)

saveRDS(result$output1, file = "/esarchive/scratch/nperez/git/Flor/Landmarc/era5_hursmin/monthlymeans.rds")

# Plot
library(s2dv)
hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979),
               month = sprintf("%02d", 1:2),
               time = 'all',
               lat = 'all',
               lon = 'all',
               return_vars = list(lat = NULL, lon = NULL),
               retrieve = FALSE)


PlotLayout(PlotEquiMap, c('lat', 'lon'), result$output1, filled.c = F,
           lon = as.vector(attributes(hrmin)$Variables$common$lon),
           lat = as.vector(attributes(hrmin)$Variables$common$lat), 
           titles = month.name)
# New tests Ranges:
library(startR)
hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
       lon = 'all',
       num_procs = 2,
               debug = TRUE,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/", list.files("/esarchive/recon/ecmwf/era5/daily/hursmin-r1440x721cds/")) == sort(as.vector(attributes(hrmin)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  range(datos)
}
step <- Step(fun = temp_mean,
             target_dims = c('time', 'year'),
             output_dims = 'range')

wf <- AddStep(hrmin, step)

queue_host = 'nord4'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 2,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'slurm',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             polling_period = 10,
                             job_wallclock = '08:00:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              debug = TRUE,
              ecflow_suite_dir = ecflow_suite_dir,
              wait = FALSE)

 saveRDS(res, file = 'test_collect.Rds')


# -----
dim(result$output1)
range(result$output1[1,1,1,-5,,]) # correct
range(result$output1[1,1,1,5,,]) # wrong
may <- result$output1[1,1,1,5,,]
sum(may > 100)
wrong <- may > 100
which(apply(wrong, 1, max) == 1)

which(apply(wrong, 2, max) == 1)

PlotEquiMap(may[653:717, 33:618], lat = as.vector(attributes(hrmin)$Variables$common$lat)[653:717],
            lon = as.vector(attributes(hrmin)$Variables$common$lon)[33:618], filled.c = FALSE)

library(startR)
hrmin <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "hursmin",
               year = as.character(1979:2018),
               month = '05', #sprintf("%02d", 1:12),
               time = 'all',
               lat = indices(653:717),
               lon = indices(33:618),
               num_procs = 2,
               return_vars = list(lat = NULL, lon = NULL),
               retrieve = TRUE)

dim(hrmin)
range(hrmin)
wrong <- hrmin > 100
which(apply(wrong, c(1:3), max) == 1)
may_year <- MeanDims(hrmin, 'time')
PlotLayout(PlotEquiMap, c('lat', 'lon'), may_year[1,1,1:20,1,,], filled.c = F,
           lon = as.vector(attributes(hrmin)$Variables$common$lon),
           lat = as.vector(attributes(hrmin)$Variables$common$lat),
           titles = as.character(1979:1998))
PlotLayout(PlotEquiMap, c('lat', 'lon'), may_year[1,1,21:40,1,,], filled.c = F,
           lon = as.vector(attributes(hrmin)$Variables$common$lon),
           lat = as.vector(attributes(hrmin)$Variables$common$lat),
           titles = as.character(1999:2018))


# ---------------------------------------------
# Test mean values tasmax:
library(startR)
library(s2dv)

tasmax <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "tasmax",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
       lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               num_procs = 1,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily/tasmax-r1440x721cds/", list.files("/esarchive/recon/ecmwf/era5/daily/tasmax-r1440x721cds/")[349:828]) == sort(as.vector(attributes(tasmax)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  s2dv::MeanDims(datos, c('time', 'year'))

}
step <- Step(fun = temp_mean,
             target_dims = c('time', 'year'),
             output_dims = 'time')

wf <- AddStep(tasmax, step)

queue_host = 'nord3'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 1,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'lsf',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '03:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
	      debug = F,
              wait = T)


saveRDS(res$output1, file = 'tasmax_monthly.RDS')

tasmax <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$-r1440x721cds/$var$_$year$$month$.nc",
               var = "tasmax",
               year = as.character(1980),
               month = sprintf("%02d", 12),
               time = indices(1),
               lat = 'all',
               lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               return_vars = list(lat = NULL, lon = NULL),
               num_procs = 1,
               retrieve = FALSE)

PlotLayout(PlotEquiMap, c('lat', 'lon'), res$output1, filled.c = F,
           lon = as.vector(attributes(tasmax)$Variables$common$lon),
           lat = as.vector(attributes(tasmax)$Variables$common$lat),
           titles = month.name)

# ---------------------------------------------
# Test mean values wind:
library(startR)
library(s2dv)

wind <- Start(dat = "/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$year$$month$.nc",
               var = "sfcWind",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
       lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               num_procs = 1,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily_mean/sfcWind_f1h-r1440x721cds/", list.files("/esarchive/recon/ecmwf/era5/daily_mean/sfcWind_f1h-r1440x721cds/")[1:480]) == sort(as.vector(attributes(wind)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  s2dv::MeanDims(datos, c('time', 'year'))

}
step <- Step(fun = temp_mean,
             target_dims = c('time', 'year'),
             output_dims = 'time')

wf <- AddStep(wind, step)

queue_host = 'nord3'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 1,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'lsf',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '03:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
              debug = T,
              wait = T)
saveRDS(res, file = 'wind_Nord3_collect.Rds')


saveRDS(res$output1, file = 'wind_monthly.RDS')


wind <- Start(dat = "/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$year$$month$.nc",
               var = "sfcWind",
               year = as.character(1979),
               month = '05', #sprintf("%02d", 12),
               time = 'all',
               lat = 'all',
               lon = 'all',
               num_procs = 2,
               return_vars = list(lat = NULL, lon = NULL),
               retrieve = TRUE)

PlotLayout(PlotEquiMap, c('lat', 'lon'), res$output1, filled.c = F,
           lon = as.vector(attributes(wind)$Variables$common$lon),
           lat = as.vector(attributes(wind)$Variables$common$lat),
           titles = month.name)

#------------------------------------------------------
# Test mean values precipitation:
library(startR)
library(s2dv)

ppt <- Start(dat = "/esarchive/recon/ecmwf/era5/daily_mean/$var$_f1h-r1440x721cds/$var$_$year$$month$.nc",
               var = "prlr",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
        lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               return_vars = list(lat = NULL, lon = NULL),
               num_procs = 1,
               retrieve = FALSE)
all(paste0("/esarchive/recon/ecmwf/era5/daily_mean/prlr_f1h-r1440x721cds/",
    list.files("/esarchive/recon/ecmwf/era5/daily_mean/prlr_f1h-r1440x721cds/")[349:828]) == 
    sort(as.vector(attributes(ppt)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  s2dv::MeanDims(datos, c('time', 'year'))

}
step <- Step(fun = temp_mean,
             target_dims = c('time', 'year'),
             output_dims = 'time')

wf <- AddStep(ppt, step)

queue_host = 'nord3'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 1,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'lsf',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '03:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
              debug = T,
              wait = T)

saveRDS(res$output1, file = 'ppt_monthly.RDS')


PlotLayout(PlotEquiMap, c('lat', 'lon'), res$output1, filled.c = F,
           lon = as.vector(attributes(ppt)$Variables$common$lon),
           lat = as.vector(attributes(ppt)$Variables$common$lat),
           titles = month.name)

PlotLayout(PlotEquiMap, c('lat', 'lon'), res$output * 24*60*60*1000, filled.c = F,
           lon = as.vector(attributes(ppt)$Variables$common$lon),
           lat = as.vector(attributes(ppt)$Variables$common$lat),
           titles = month.name)


# ---------------------------------
# downloaded FWI

raw_fwi <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$/$var$_$year$$month$.nc",
               var = "fwi",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
               lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               return_vars = list(lat = NULL, lon = NULL),
               num_procs = 1,
               retrieve = FALSE)

all(paste0("/esarchive/recon/ecmwf/era5/daily/fwi/",
    list.files("/esarchive/recon/ecmwf/era5/daily/fwi/")) ==
    sort(as.vector(attributes(raw_fwi)$Expected)))



temp_mean <- function(datos) {
  # dims: time, year
  s2dv::MeanDims(datos, c('time', 'year'), na.rm = T))

}
step <- Step(fun = temp_mean,
             target_dims = c('time', 'year'),
             output_dims = 'time')

wf <- AddStep(raw_fwi, step)

queue_host = 'nord2'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(month = 12),
              threads_load = 1,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'lsf',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '03:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
              debug = T,
              wait = T)

saveRDS(res$output1, file = 'fwi_monthly.RDS')

apply(res$output1, 4, function(x) {sum(is.na(x))})

PlotLayout(PlotEquiMap, c('lat', 'lon'), res$output1[1,1,1,2:12,,], filled.c = F,
           lon = as.vector(attributes(raw_fwi)$Variables$common$lon),
           lat = as.vector(attributes(raw_fwi)$Variables$common$lat),
           titles = month.name[2:12], colNA = 'white')
range(res$output1)
which(res$output1 <= 5.2)
which(res$output1 > 5.2 && res$output1 <= 11.2)
which(res$output1 > 11.2 && res$output1 <= 21.3)
which(res$output1 > 21.2 && res$output1 <= 38.0)
which(res$output1 > 38.0 && res$output1 <= 50.0)
which(res$output1 > 50.0 && res$output1 <= 100.0)


# Check January:
raw_fwi <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$/$var$_$year$$month$.nc",
               var = "fwi",
               year = as.character(1979:2018),
               month = "01",
               time = 'all',
               lat = 'all',
               lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               return_vars = list(lat = NULL, lon = NULL),
               num_procs = 1,
               retrieve = FALSE)

temp_mean <- function(datos) {
  # dims: time
  s2dv::MeanDims(datos, 'time', na.rm = T)
}
step <- Step(fun = temp_mean,
             target_dims = c('time'),
             output_dims = 'info')

wf <- AddStep(raw_fwi, step)

queue_host = 'nord2'   #your own host name for power9
temp_dir =  '/gpfs/scratch/bsc32/bsc32339/startR_hpc/'
ecflow_suite_dir = '/home/Earth/nperez/startR_local/'  #your own local directory

res <- Compute(wf,
              chunks = list(year = 10),
              threads_load = 1,
              threads_compute = 16,
              cluster = list(queue_host = queue_host,
                             queue_type = 'lsf',
                             cores_per_job = 16,
                             temp_dir = temp_dir,
                             ## try new lib:
                             lib_dir = '~/R/x86_64-pc-linux-gnu-library/3.6',
                             polling_period = 10,
                             job_wallclock = '03:00',
                             max_jobs = 12,
                             bidirectional = FALSE),
              ecflow_suite_dir = ecflow_suite_dir,
              wait = T)
dim(res$output1)
ran <- res$output1[1,1,1,,1,,]


#----------------------------
# FWI Nooverwintered
NoOver_fwi <- Start(dat = "/esarchive/recon/ecmwf/era5/daily/$var$/$var$_$year$$month$.nc",
               var = "fwiNoOverwintering",
               year = as.character(1979:2018),
               month = sprintf("%02d", 1:12),
               time = 'all',
               lat = 'all',
               lon = 'all',
               synonims = list(lat = c('lat', 'latitude'),
                               lon = c('lon', 'longitude')),
               return_vars = list(lat = NULL, lon = NULL),
               num_procs = 1,
               retrieve = FALSE)

