
# ------
------------------------------------------------------------
# Function working on time dimension e.g.: Season
# ------------------------------------------------------------------
rm(list = ls())

devtools::install_git('https://earth.bsc.es/gitlab/es/startR', branch = 'master')
# library(startR, lib.loc = .libPaths()[2])


library(startR)
packageVersion("startR")
packageVersion("base")

library(tictoc)
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1 : 20),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = indices(1 : 40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = FALSE)

## devtools::install_git('https://earth.bsc.es/gitlab/es/s2dverification', branch = 'devel-SeasonApply')
## library(s2dverication)
#source("/home/Earth/nperez/git/SeasonApply.R")
source("/home/Earth/aho/aho-testtest/startR/Season_v2.R")
library(multiApply)
 
fun_spring <- function(x) {
    source("/esarchive/scratch/nperez/Season_v2.R")
    y <- Season_v2(x,  monini = 1, moninf = 3, monsup = 5)
    return(y)
}
step1 <- Step(fun = fun_spring,
             target_dims = c('time'),
             output_dims = c('time'))#,

wf1 <- AddStep(data, step1)

# Locally
tic()
res1 <- Compute(wf1,
        
    chunks = list(ensemble = 2,
                          sdate = 2))
toc() # WS: 626.79 sec elapsed

dim(res1$output1)
str(res1$output1)
summary(res1$output1)

# -----------------------------------------------------------------
# ALL Output: to reproduce it, use R 3.2.0 and startR_0.1.2
#
#> dim(res1$output1)
#     time       dat       var     sdate  ensemble  latitude longitude 
#        1         1         1         2        20       640        40 
#> str(res1$output1)
# num [1, 1, 1, 1:2, 1:20, 1:640, 1:40] 260 257 257 260 257 ...
#> summary(res1$output1)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  220.5   273.3   282.7   279.9   295.6   306.8 
# ----------------------------------------------------------------
# NEW Output: to reproduce it, use R 3.4.2 and startR_0.1.3
> dim(res1$output1)
     time       dat       var     sdate  ensemble  latitude longitude 
        1         1         1         2        20       640        40 
> str(res1$output1)
 num [1, 1, 1, 1:2, 1:20, 1:640, 1:40] 249 249 249 249 249 ...
> summary(res1$output1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  215.1   273.7   283.5   280.1   295.0   310.6 




res1_F <- Compute(wf1,
                  chunks = list(ensemble = 20,
                                sdate = 2),
                  threads_load = 2,
                  threads_compute = 4,
                  cluster = list(queue_host = 'cte-power',
                                 queue_type = 'slurm',
                                 cores_per_job = 2,
                                 temp_dir = '/gpfs/scratch/bsc32/bsc32339/',
                                 r_module = 'R/3.5.0-foss-2018b',
                     extra_queue_params = list('#SBATCH --mem-per-cpu=3000'),
                                 polling_period = 10,
                                 job_wallclock = '01:00:00',
                                 max_jobs = 40,
                                 bidirectional = FALSE),
               ecflow_suite_dir = '/esarchive/scratch/nperez/',
               wait = TRUE)

res <- Compute(wf1,
               chunks = list(ensemble = 2,
                             sdate = 2),
               threads_load = 1,
               threads_compute = 2,
               cluster = list(queue_host = 'bsceslogin01.bsc.es',
                              queue_type = 'slurm',
                              temp_dir = '/home/Earth/nperez//',
                              cores_per_job = 1,
                              job_wallclock = '00:10:00',
                              max_jobs = 4,
                              bidirectional = TRUE
                             ),
               ecflow_suite_dir = '/esarchive/scratch/nperez/startR_local/')


# Review output:
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = indices(1 : 40),
              ensemble = indices(1:20),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = TRUE)
outp <- Season(data, monini = 1, moninf = 3, monsup = 5)


outp[dat = 1, var = 1, sdate = 1 : 2, ensemble = 1, time = 1, lat = 1, lon = 1] 
mean(data[dat = 1, var = 1, sdate = 1, ensemble = 1, time = 3 : 5, lat = 1, lon = 1])
mean(data[dat = 1, var = 1, sdate = 2, ensemble = 1, time = 3 : 5, lat = 1, lon = 1])
res1$output1[time = 1, dat = 1, var = 1, sdate = 1 : 2, ensemble = 1, lat = 1, lon = 1]

 res1$output1[1,1,1,,1,1,1]
[1] 259.9109 256.9104

# New check:
library(ncdf4)
file <- nc_open("/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20170101.nc")
names(file$dim)
dats <- ncvar_get(file, "tas",
                  start = c(longitude = 1, latitude = 1, ensemble = 1, time = 1),
                  count = c(longitude = 40, latitude = 640, ensemble = 20, time = 7))
dim(dats)
> dats[lon = 1, lat = 1, ensemble = 1:3, time = 1:7]
[1,] 248.5009 251.2370 251.8830 258.8511 268.9987 273.6894 273.6727
[2,] 249.2918 247.3308 248.3783 254.3748 266.8282 273.6169 273.6101
[3,] 248.5038 242.7727 248.3554 255.8992 267.8885 273.6942 273.8943

> data[dat = 1, var = 1, sdate = 1, time = 1:7, lat = 1, lon = 1, ensemble = 1:3]
        [,1]     [,2]     [,3]
[1,] 248.5009 249.2918 248.5038
[2,] 251.2370 247.3308 242.7727
[3,] 251.8830 248.3783 248.3554
[4,] 258.8511 254.3748 255.8992
[5,] 268.9987 266.8282 267.8885
[6,] 273.6894 273.6169 273.6942
[7,] 273.6727 273.6101 273.8943

> dats[lon = 1, lat = 1:5, ensemble = 1:3, time = 1]
         [,1]     [,2]     [,3]
[1,] 248.5009 249.2918 248.5038
[2,] 248.7802 249.3055 248.5312
[3,] 249.0634 249.3563 248.6620
[4,] 249.3368 249.4578 248.8749
[5,] 249.6630 249.6121 249.0702
> data[dat = 1, var = 1, sdate = 1, time = 1, lat = 1:5, lon = 1, ensemble = 1:3]
         [,1]     [,2]     [,3]
[1,] 248.5009 249.2918 248.5038
[2,] 248.7802 249.3055 248.5312
[3,] 249.0634 249.3563 248.6620
[4,] 249.3368 249.4578 248.8749
[5,] 249.6630 249.6121 249.0702

> dats[lon = 1:6, lat = 1, ensemble = 1, time = 1:3]
         [,1]     [,2]     [,3]
[1,] 248.5009 251.2370 251.8830
[2,] 248.5012 251.2371 251.8836
[3,] 248.5016 251.2372 251.8842
[4,] 248.5020 251.2373 251.8849
[5,] 248.5023 251.2374 251.8855
[6,] 248.5027 251.2374 251.8861
> data[dat = 1, var = 1, sdate = 1, time = 1:3, lat = 1, lon = 1:6, ensemble = 1]
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
[1,] 248.5009 248.5012 248.5016 248.5020 248.5023 248.5027
[2,] 251.2370 251.2371 251.2372 251.2373 251.2374 251.2374
[3,] 251.8830 251.8836 251.8842 251.8849 251.8855 251.8861

> data[dat = 1, var = 1, sdate = 1, time = 1:7, lat = 1:2, lon = 1, ensemble = 1]
         [,1]     [,2]
[1,] 248.5009 **248.5034**
[2,] 248.5012 248.5038
[3,] 248.5016 248.5041
[4,] 248.5020 248.5045
[5,] 248.5023 248.5049
[6,] 248.5027 248.5052
[7,] 248.5030 248.5056

> dats[lon = 1:14, lat = 1:3, ensemble = 1, time = 1]
          [,1]     [,2]     [,3]
 [1,] 248.5009 248.7802 249.0634
 [2,] 248.5012 248.7815 249.0662
 [3,] 248.5016 248.7829 249.0691
 [4,] 248.5020 248.7843 249.0720
 [5,] 248.5023 248.7857 249.0748
 [6,] 248.5027 248.7870 249.0777
 [7,] 248.5030 248.7884 249.0806
 [8,] **248.5034** 248.7898 249.0835
 [9,] 248.5038 248.7912 249.0863
[10,] 248.5041 248.7925 249.0892
[11,] 248.5045 248.7939 249.0921
[12,] 248.5049 248.7953 249.0949
[13,] 248.5052 248.7967 249.0978
[14,] 248.5056 248.7980 249.1007



file2 <- nc_open("/esarchive/exp/ecmwf/system5_m1/monthly_mean/tas_f6h/tas_20180101.nc")
names(file2$dim)
dats2 <- ncvar_get(file2, "tas", start = c(longitude = 1, latitude = 1, ensemble = 1, time = 1),
                  count = c(longitude = 40, latitude = 640, ensemble = 20, time = 7))
dim(dats2)




outp2 <- Season(dats, posdim = "time", monini = 1, moninf = 3, monsup = 5)
outp2[ensemble = 1 : 2, lat = 1, lon = 1, time = 1]

outp3 <- Season(dats2, posdim = "time", monini = 1, moninf = 3, monsup = 5)
outp3[ensemble = 1 : 2, lat = 1, lon = 1, time = 1]
outp[dat = 1, var = 1, sdate = 2, ensemble = 1 : 2, time = 1, lat = 1, lon = 1]
res1$output1[time = 1, dat = 1, var = 1, sdate = 2, ensemble = 1 : 2, lat = 1, lon = 1]




# New start() headers:
data_v2 <- Start(dat = repos,
                var = 'tas',
                sdate = c('20170101', '20180101'),
                #time = 'all',
                latitude = 'all', #indices(1:40),
                longitude = indices(1 : 40),
                ensemble = indices(1:20),
                time = 'all',
                return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
                retrieve = TRUE)
> dim(data_v2)
      dat       var     sdate  latitude longitude  ensemble      time
        1         1         2       640        40        20         7
> data_v2[dat = 1, var = 1, sdate = 1, lat = 1:2, lon = 1, ensemble = 1, time = 1:7]
         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
[1,] 248.5009 251.2370 251.8830 258.8511 268.9987 273.6894 273.6727
[2,] 248.5012 251.2371 251.8836 258.8518 268.9988 273.6893 273.6727 




data_v3 <- Start(dat = repos,
                var = 'tas',
                sdate = c('20170101', '20180101'),
                #time = 'all',
                #latitude = 'all', #indices(1:40),
                longitude = indices(1 : 40),
                latitude = 'all',
                ensemble = indices(1:20),
                time = 'all',
                return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
                retrieve = TRUE)
> dim(data_v3)
      dat       var     sdate longitude  latitude  ensemble      time 
        1         1         2        40       640        20         7 

# ------------------------------------------------------------------
# Function using attributes of the data e.g.: latitudinal correction
# ------------------------------------------------------------------
rm(list = ls())
library(multiApply)
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1 : 20),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = indices(1 : 40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = FALSE)
funp <- function(x) {
          lat = attributes(x)$Variables$dat1$latitude
          weight = sqrt(cos(lat * pi / 180))
          corrected = Apply(list(x), target_dims = "latitude",
                            fun = function(x) {x * weight})
        }


step2 <- Step(fun = funp,
             target_dims = 'latitude',
             output_dims = 'latitude',
             use_attributes = list(data = "Variables"))

wf2 <- AddStep(data, step2)
tic()
res2 <- Compute(workflow = wf2,
            chunks = list(ensemble = 2,
                          sdate = 2))
toc() # WS: 12.846 sec elapsed

dim(res2$output1)
head(res2$output1)
summary(res2$output1)

# ------------------------------------------------------------------
# Output:
> dim(res2$output1)
 latitude       dat       var     sdate  ensemble      time longitude 
      640         1         1         2        20         7        40 
> head(res2$output1)
[1] 15.22683 23.09543 28.94978 33.82982 38.11695 41.99680
> summary(res2$output1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  13.19  169.60  237.60  217.90  284.10  305.10 
# -----------------------------------------------------------------

res2_F <- Compute(wf2,
                  chunks = list(ensemble = 2,
                                sdate = 2),
                  threads_load = 1,
                  threads_compute = 2,
                  cluster = list(queue_host = 'cte-power',
                              queue_type = 'slurm',
                              temp_dir = '/home/Earth/nperez//',
                              cores_per_job = 1,
                              job_wallclock = '00:10:00',
                              max_jobs = 4,
                              bidirectional = TRUE
                             ),
               ecflow_suite_dir = '/esarchive/scratch/nperez/startR_local/')
----------------------------------------------------------
# Function doing regridding CDO (e.g.: s2dverification::CDORemap)
#---------------------------------------------------------------
rm(list = ls())
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1 : 2),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = 'all', #indices(1 : 40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = FALSE)


fun_deb2 <- function(x) {
             lons_data = as.vector(attr(x, 'Variables')$dat1$longitude)
             lats_data = as.vector(attr(x, 'Variables')$dat1$latitude)
             resgrid = "r360x180"   # prlr
             r <- s2dverification::CDORemap(x, lons_data, lats_data, resgrid,
                                      'bil', crop = FALSE, force_remap = TRUE)[[1]]
             return(r)
}


step3 <- Step(fun = fun_deb2,
             target_dims = c('latitude','longitude'),
             output_dims = c('latitude', 'longitude'),
             use_attributes = list(data = "Variables"))
wf3 <- AddStep(data, step3)
tic()
res3 <- Compute(workflow = wf3,
            chunks = list(ensemble = 2,
                          sdate = 2))
toc() # WS: 15.242 sec elapsed

dim(res3$output1)
head(res3$output1)
summary(res3$output1)

# --------------------------------------------------------------------
# Output:
> dim(res3$output1)
 latitude longitude       dat       var     sdate  ensemble      time 
      180       360         1         1         2         2         7 
> head(res3$output1)
[1] 245.2156 244.6700 245.1513 244.7413 244.4198 245.0041
> summary(res3$output1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  206.3   269.2   283.3   278.8   296.4   314.2 
# --------------------------------------------------------------------
res3_P <- Compute(wf3,
                  chunks = list(ensemble = 2,
                                sdate = 2),
                  threads_load = 2,
                  threads_compute = 4,
                  cluster = list(queue_host = 'cte-power',
                                 queue_type = 'slurm',
                                 cores_per_job = 1,
                                 temp_dir = '/gpfs/scratch/bsc32/bsc32339/',
                                 r_module = 'R/3.5.0-foss-2018b',
                                 CDO_module = 'CDO/1.9.5-foss-2018b',
                     extra_queue_params = list('#SBATCH --mem-per-cpu=3000'),
                                 polling_period = 100,
                                 job_wallclock = '01:00:00',
                                 max_jobs = 4,
                                 bidirectional = FALSE),
               ecflow_suite_dir = '/esarchive/scratch/nperez/',
               wait = TRUE)

res3_F <- Compute(wf3,
                  chunks = list(ensemble = 2,
                                sdate = 2),
                  threads_load = 1,
                  threads_compute = 2,
                  cluster = list(queue_host = 'bsceslogin01.bsc.es',
                                 queue_type = 'slurm',
                                 cores_per_job = 1,
                                 job_wallclock = '00:10:00',
                                 max_jobs = 4,
                                 bidirectional = TRUE),
               ecflow_suite_dir = '/home/Earth/nperez/')


# --------------------------------------------------------------
# Two functions (e.g.: s2dverification::CDORemap and Season)
#---------------------------------------------------------------

rm(list = ls())
repos <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1 : 2),
              time = 'all',
              latitude = 'all', #indices(1:40),
              longitude = 'all', #indices(1 : 40),
              return_vars = list(latitude = 'dat', longitude = 'dat', time = 'sdate'),
              retrieve = FALSE)


devtools ::install_git('https://earth.bsc.es/gitlab/es/s2dverification',
                       branch = 'devel-SeasonApply')
library(s2dverification)

fun_deb3 <- function(x) {
             source("/esarchive/scratch/nperez/Season_v2.R")
             lons_data = as.vector(attr(x, 'Variables')$dat1$longitude)
             lats_data = as.vector(attr(x, 'Variables')$dat1$latitude)
             resgrid = "r360x180"   # prlr
             y = Season_v2(x, posdim = 'time', monini = 1, moninf = 1, monsup = 3)
             r <- s2dverification::CDORemap(y, lons_data, lats_data, resgrid,
                                      'bil', crop = FALSE, force_remap = TRUE)[[1]]
             return(r)
}


step4 <- Step(fun = fun_deb3,
             target_dims = c('latitude','longitude', 'time'),
             output_dims = c('latitude', 'longitude', 'time'),
             use_attributes = list(data = "Variables"))
wf4 <- AddStep(data, step4)
tic()
res4 <- Compute(workflow = wf4,
            chunks = list(ensemble = 2,
                          sdate = 2))
toc() # WS: 576.331 sec elapsed


dim(res4$output1)
head(res4$output1)
summary(res4$output1)

# ------------------------------------------------------------------
# Output:
> dim(res4$output1)
 latitude longitude      time       dat       var     sdate  ensemble 
      180       360         1         1         1         2         2 
> head(res4$output1)
[1] 237.1389 237.2601 238.0882 238.0312 237.7883 238.4835
> summary(res4$output1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  227.3   259.6   280.8   277.1   296.2   306.7 
# ------------------------------------------------------------------

res4_P <- Compute(wf4,
                  chunks = list(ensemble = 2,
                                sdate = 2),
                  threads_load = 2,
                  threads_compute = 4,
                  cluster = list(queue_host = 'cte-power',
                                 queue_type = 'slurm',
                                 cores_per_job = 1,
                                 temp_dir = '/gpfs/scratch/bsc32/bsc32339/',
                                 r_module = 'R/3.5.0-foss-2018b',
                                 CDO_module = 'CDO/1.9.5-foss-2018b',
                     extra_queue_params = list('#SBATCH --mem-per-cpu=3000'),
                                 polling_period = 10,
                                 job_wallclock = '01:00:00',
                                 max_jobs = 6,
                                 bidirectional = FALSE),
               ecflow_suite_dir = '/esarchive/scratch/nperez/',
               wait = TRUE)

res4_F <- Compute(wf4,
                  chunks = list(ensemble = 2,
                                sdate = 2),
                  threads_load = 1,
                  threads_compute = 2,
                  cluster = list(queue_host = 'bsceslogin01.bsc.es',
                                 queue_type = 'slurm',
                                 cores_per_job = 1,
                                 job_wallclock = '00:10:00',
                                 max_jobs = 4,
                                 bidirectional = TRUE),
               ecflow_suite_dir = '/home/Earth/nperez/')




