# Code to compute a climdex.pcic indicator in Landmarc project
# Author: Núria Pérez-Zanón
# Date: 02-11-2021

library(startR)

# Piece of data for testing:
seq.dates <- seq(as.POSIXct('1850-01-01 12:00:00 UTC', tz = 'UTC'),
                 as.POSIXct('2010-12-31 12:00:00 UTC', tz = 'UTC'), 'day')

hist <- Start(dat = "/esarchive/exp/CMIP6/historical/cnrm-cm6-1/cmip6-historical_i0p1/CMIP/CNRM-CERFACS/CNRM-CM6-1/historical/$member$/day/$var$/gr/v20200101/$var$_day_CNRM-CM6-1_historical_$member$_gr_$period$.nc",
              var = 'tas',
              member = 'r1i1p1f2',
              period = '18500101-20141231',
              lat = 'all',
              lon = 'all',
              time = indices(11324:12053),
                     #values(seq.dates),
                     #values(list(as.POSIXct('1981-01-01 12:00:00 UTC', tz = 'UTC'),
                      #           as.POSIXct('1982-12-31 12:00:00 UTC', tz = 'UTC'))),
              transform = CDORemapper,
              transform_extra_cells = 2,
              transform_params = list(grid = 'r180x90',
                                     method = 'conservative',
                                     crop = FALSE),
              transform_vars = c('lat', 'lon'),
              synonims = list(lon = c('lon', 'longitude'),
                              lat = c('lat', 'latitude')),
              return_vars = list(time = NULL,
                                 lat = 'dat',
                                 lon = 'dat'),
              retrieve =  FALSE)

lon <- attributes(hist)$Varia$dat1$lon
lat <- attributes(hist)$Varia$dat1$lat
time <- attributes(hist)$Variable$common$time

climdex_ind <- function(dat, times) {
  datefactor <- as.numeric(substr(times, 1, 4))
  datefactor <- as.factor(datefactor)
  fd <- climdex.pcic::number.days.op.threshold(dat, datefactor, 273.15, "<")
  return(fd)
}

step <- Step(fun = climdex_ind,
               target_dims = 'time',
               output_dims = 'time')
  
wf <- AddStep(hist, step, times = time)
  
## locally
res <- Compute(wf,
               chunks = list(lat = 2,
                             lon = 2))

s2dv::PlotEquiMap(res$output1[1,1,1,1,1,,], lon = lon, lat = lat, filled.con = F)


# Larger analysis:

hist <- Start(dat = "/esarchive/exp/CMIP6/historical/cnrm-cm6-1/cmip6-historical_i0p1/CMIP/CNRM-CERFACS/CNRM-CM6-1/historical/$member$/day/$var$/gr/v20200101/$var$_day_CNRM-CM6-1_historical_$member$_gr_$period$.nc",
              var = 'tas',
              member = 'r1i1p1f2',
              period = '18500101-20141231',
              lat = 'all',
              lon = 'all',
              time = indices(47848:58804),
                      #values(list(as.POSIXct('1981-01-01 12:00:00 UTC', tz = 'UTC'),
                       #          as.POSIXct('2010-12-31 12:00:00 UTC', tz = 'UTC'))),
              transform = CDORemapper,
              transform_extra_cells = 2,
              transform_params = list(grid = 'r180x90',
                                     method = 'conservative',
                                     crop = FALSE),
              transform_vars = c('lat', 'lon'),
              synonims = list(lon = c('lon', 'longitude'),
                              lat = c('lat', 'latitude')),
              return_vars = list(time = NULL,
                                 lat = 'dat',
                                 lon = 'dat'),
              retrieve = FALSE)
lon <- attributes(hist)$Varia$dat1$lon
lat <- attributes(hist)$Varia$dat1$lat
time <- attributes(hist)$Variable$common$time


wf <- AddStep(hist, step, times = time)

# Run Nord3

  queue_host = 'nord3'   #your own host name for nord3
  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(lat = 2,
                               lon = 2),
                 threads_load = 1,
                 threads_compute = 8,
                 cluster = list(queue_host = queue_host,
                                queue_type = 'lsf',
                                extra_queue_params = list('#BSUB -q bsc_es'),
                                cores_per_job = 8,
                                temp_dir = temp_dir,
                                polling_period = 10,
                                job_wallclock = '02:00',
                                max_jobs = 8,
                                bidirectional = FALSE),
                 ecflow_suite_dir = ecflow_suite_dir,
                 wait = TRUE)


s2dv::PlotEquiMap(res$output1[1,1,1,1,1,,], lon = lon, lat = lat, filled.con = F, brks = seq(0:366, 6))



