# Author: Victòria Agudetse Roures, Sara Moreno, Núria Pérez Zanón
# Date: August 2025
# ------------------------------------------------------------------

# -----------------------------------------------------------------
# Running Compute() on a machine with a different filesystem.
# 
# In this example, we use Compute() to apply a function to some data
# using an HPC cluster that does not share a filesystem with our local
# machine. The data files are available on our local machine *and* on the
# cluster, under different folders ('local_path' and 'cluster_path'). 
# We show how to change the paths of the files so that startR can read the
# metadata from our local machine and then load the correct files on the
# remote cluster.
# ------------------------------------------------------------------

library(startR)

# Define the local path and the path on the cluster
local_path <- "/esarchive/"
cluster_path <- "/gpfs/projects/bsc32/esarchive_cache/"

# Step 1: Load the data in esarchive with retrieve = FALSE
repos <- '/esarchive/exp/ecmwf/system51c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
data <- Start(dat = repos,
              var = 'tas',
              sdate = c('20170101', '20180101'),
              ensemble = indices(1:20),
              time = 'all',
              lat = 'all',
              lon = indices(1:40),
              return_vars = list(lat = 'dat', lon = 'dat', time = 'sdate'),
              retrieve = FALSE)

# Step 2: Replace the esarchive path pattern with the gpfs path pattern
file_dims <- dim(attr(data, "ExpectedFiles"))
new_files <- sapply(attr(data, "ExpectedFiles"),
                    function(x) {
                      gsub(pattern = local_path, replacement = cluster_path, x)
                    })
dim(new_files) <- file_dims
attr(data, "ExpectedFiles") <- new_files
data[2] <- gsub(pattern = local_path, replacement = cluster_path, x = data[[2]])

# Step 3: Define function and step and run Compute()
fun_spring <- function(x) {
  stop()
  y <- s2dv::Season(x, time_dim = 'time', monini = 1, moninf = 3, monsup = 5)
  return(y)
}

step1 <- Step(fun = fun_spring,
              target_dims = c('var', 'time'),
              output_dims = c('var', 'time'))

wf1 <- AddStep(data, step1)

#-----------modify according to your personal info---------
  queue_host <- 'amd'
  run_dir <- '/gpfs/scratch/bsc32/bsc032762/startR_hpc/' # temporary directory to use on cluster
  autosubmit_suite_dir <- '/esarchive/scratch/vagudets/startR_local/' # your own local directory
  autosubmit_expid <- 'a9p5' # your autosubmit expid (see documentation)
#------------------------------------------------------------
  res <- Compute(workflow = wf1,
                 chunks = list(ensemble = 2, sdate = 2),
                 threads_load = 2,
                 threads_compute = 4,
                 cluster = list(
                   queue_host = queue_host,
                   expid = autosubmit_expid,
                   hpc_user = "bsc032762",
                   autosubmit_module = 'autosubmit/4.1.14-foss-2023b-Python-3.11.5',
                   cores_per_job = 4,
                   job_wallclock = '01:00',
                   max_jobs = 100,
                   run_dir = run_dir
                 ),
                 workflow_manager = 'autosubmit',
                 autosubmit_suite_dir = autosubmit_suite_dir,
                 autosubmit_server = 'bscesautosubmit03',
                 wait = TRUE
  )

