# ----------------------------------------------
# Today is 5th August 2019
# ISSUE #29 StartR VERO 
# ----------------------------------------------
install.packages("../../startR_0.1.3.tar.gz", source = TRUE, repos =NULL)
library(startR)

grid_file<-'/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20140701.nc'

lons.min <- 0
lons.max <- 40
lats.min <- 0
lats.max <- 40

exp <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = paste0(2000 : 2010, '0101'),
             ensemble = 'all',
             time = indices(1),
             latitude = values(list(lats.min, lats.max)),
             longitude = values(list(lons.min, lons.max)),
             transform = CDORemapper,
             transform_params = list(grid = grid_file,
                                     method = 'con', crop = c(lons.min, lons.max,
                                                              lats.min,lats.max)),
             synonims = list(latitude = c('lat', 'latitude'),
                             longitude = c('longitude', 'lon')),
             transform_vars = c('latitude', 'longitude'),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = c('sdate')),
             retrieve = FALSE)

obs <- Start(dat = '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = paste0(2000:2010, '02'),
             time = indices(1),
             latitude = values(list(lats.min, lats.max)),
             longitude = values(list(lons.min, lons.max)),
             transform = CDORemapper,
             transform_params = list(grid = grid_file,
                                     method = 'con', crop = c(lons.min, lons.max,
                                                              lats.min,lats.max)),
             transform_vars = c('latitude', 'longitude'),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time = c('sdate')),
             split_multiselected_dims = TRUE,
             merge_across_dims = TRUE,
             retrieve = FALSE)


# Define function (applied on the essential 'target' dimensions)
compute_scores <- function(exp, obs) {
  library(SpecsVerification)
  my.prob <- c(1/3, 2/3)
  my.FairRpss <- easyVerification::veriApply("FairRpss", fcst = exp, obs = obs,
                           prob = my.prob, tdim = 1, ensdim = 2, na.rm = TRUE)[[1]]
  dim(my.FairRpss) <- c(score = length(my.FairRpss))
  return(my.FairRpss)
}


#output<-Apply(data=list(exp,obs),target_dims = list(exp=c('sdate','ensemble'),obs=c('sdate')), fun=compute_scores)


#startR Step functionm specifies expected input and output

step <- Step(compute_scores,
             target_dims = list(exp = c('sdate', 'ensemble'),
                                obs = c('sdate')),
             output_dims = list(my.FairRpss = 'score'),
             use_attributes = list(exp = list('Variables'), obs = list('Variables')),
             use_libraries = c('easyVerification',
                               'SpecsVerification'))

# workflow of operations
wf <- AddStep(list(exp, obs), step)

res <- Compute(wf,
               chunks = list(latitude = 1,
                             longitude = 1))

res <- Compute(wf, chunks = list(latitude = 1, 
                                 longitude = 1,
                                 time = 1))

# fat nodes
res_fat <- Compute(wf,
                   chunks = list(latitude = 1,
                                 longitude = 1,
                                 time = 1),
                   threads_load = 1,
                   threads_compute = 4,
                   cluster = list(queue_host = "bsceslogin01.bsc.es",
                                  cores_per_job = 2,
                                  extra_queue_params = list('#SBATCH -w gustafson'),
                                  max_jobs = 4, job_wallclock = '00:10:00'),
                   ecflow_suite_dir = "/esarchive/scratch/vtorralba/ecflow")

##################### example without cdoremapper ##################################

library(startR)

grid_file<-'/esarchive/exp/ecmwf/system5c3s/monthly_mean/tas_f6h/tas_20140701.nc'

lons.min <- 0
lons.max <- 40
lats.min <- 0
lats.max <- 40

exp <- Start(dat = '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = paste0(2000:2010,'0101'),
             ensemble = 'all',
             time = indices(1),
             latitude=indices(1:10),
             longitude=indices(1:10),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time=c('sdate')),
             retrieve= F)

obs <- Start(dat = '/esarchive/recon/ecmwf/era5/monthly_mean/$var$_f1h/$var$_$sdate$.nc',
             var = 'tas',
             sdate = paste0(2000:2010,'02'),
             time = indices(1),
             latitude=indices(1:10),
             longitude=indices(1:10),
             return_vars = list(latitude = 'dat',
                                longitude = 'dat',
                                time=c('sdate')),
             split_multiselected_dims = TRUE,
             merge_across_dims = TRUE,
             retrieve= F)


# Define function (applied on the essential 'target' dimensions)
compute_scores <- function(exp, obs) {
  my.prob<-c(1/3,2/3)
  my.FairRpss <- veriApply("FairRpss", fcst=exp, obs=obs,
                           prob=my.prob, tdim=1, ensdim=2, na.rm=TRUE)[[1]]
  dim(my.FairRpss) <- c(score = length(my.FairRpss))
  return(my.FairRpss)
}


#startR Step functionm specifies expected input and output

step <- Step(compute_scores, 
             target_dims = list(exp=c('sdate','ensemble'),
                                obs=c('sdate')), 
             output_dims = list(my.FairRpss = 'score'),
             use_libraries = c('easyVerification',
                               'SpecsVerification'))

# workflow of operations
wf <- AddStep(list(exp,obs), step)

# fat nodes
res_fat <- Compute(wf,
                   chunks = list(latitude = 2,
                                 longitude = 2,
                                 time = 1),
                   threads_load = 2,
                   threads_compute = 4,
                   cluster = list(queue_host = "bsceslogin01.bsc.es",
                                  cores_per_job = 2,
                                  extra_queue_params = list('#SBATCH -w gustafson'),
                                  max_jobs = 4, job_wallclock = '00:10:00'),
                   ecflow_suite_dir = "/esarchive/scratch/vtorralba/ecflow")

frpss<-res_fat$my.FairRpss
