library(startR)
#-----------modify according to your personal info---------
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
#---------


path1 <- '/esarchive/exp/ncar/cesm-dple/daily/$var$/$var$_$sdate$.nc'
path2 <- '/esarchive/exp/ecmwf/system5_m1/monthly_mean/$var$_f6h/$var$_$sdate$.nc'
var1 <- 'tas'
var2 <- 'tasmax'
target1 <- list(x = c('dat', 'var', 'time','sdate'))
target2 <- list(x = c('dat', 'var', 'member', 'time', 'sdate'))
output1 <- c('sdate',  'var')
output2 <- c('sdate', 'var', 'member')
threads_to_load1 <- 1
threads_to_load2 <- 2
latitude1 <- Sort()
latitude2 <- Sort(decreasing = FALSE)
latitude3 <- Sort(decreasing = TRUE)
latitude4 <- 'skipline'

#output1-target1:
tests <- list(test1 = list(path_test = path1, var_test = var1, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 1),
              test2 = list(path_test = path1, var_test = var1, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 2),
              test3 = list(path_test = path2, var_test = var1, latitude_test = latitude1, 
                           target_test = target1, output_test = output1, threads_test = 1),
              test4 = list(path_test = path2, var_test = var1, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 2),
              test5 = list(path_test = path1, var_test = var2, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 1),
              test6 = list(path_test = path1, var_test = var2, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 2),
              test7 = list(path_test = path2, var_test = var2, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 1),
              test8 = list(path_test = path2, var_test = var2, latitude_test = latitude1,
                           target_test = target1, output_test = output1, threads_test = 2),
              test9 = list(path_test = path1, var_test = var1, latitude_test = latitude2,
                           target_test = target1, output_test = output1, threads_test = 1),
              test10 = list(path_test = path1, var_test = var1, latitude_test = latitude2,
                           target_test = target1, output_test = output1, threads_test = 2),
              test11 = list(path_test = path2, var_test = var1, latitude_test = latitude2,
                            target_test = target1, output_test = output1, threads_test = 1),
              test12 = list(path_test = path2, var_test = var1, latitude_test = latitude2,
                            target_test = target1, output_test = output1, threads_test = 2),
              test13 = list(path_test = path1, var_test = var2, latitude_test = latitude2,                                     target_test = target1, output_test = output1, threads_test = 1),
              test14 = list(path_test = path1, var_test = var2, latitude_test = latitude2,
                            target_test = target1, output_test = output1, threads_test = 2),
              test15 = list(path_test = path2, var_test = var2, latitude_test = latitude2,
                            target_test = target1, output_test = output1, threads_test = 1),
              test16 = list(path_test = path2, var_test = var2, latitude_test = latitude2,
                            target_test = target1, output_test = output1, threads_test = 2),
              test17 = list(path_test = path1, var_test = var1, latitude_test = latitude3,
                            target_test = target1, output_test = output1, threads_test = 1),
              test18 = list(path_test = path1, var_test = var1, latitude_test = latitude3,
                            target_test = target1, output_test = output1, threads_test = 2),
              test19 = list(path_test = path2, var_test = var1, latitude_test = latitude3,
                            target_test = target1, output_test = output1, threads_test = 1),
              test20 = list(path_test = path2, var_test = var1, latitude_test = latitude3,                                     target_test = target1, output_test = output1, threads_test = 2),
              test21 = list(path_test = path1, var_test = var2, latitude_test = latitude3,                                     target_test = target1, output_test = output1, threads_test = 1),
              test22 = list(path_test = path1, var_test = var2, latitude_test = latitude3,                                     target_test = target1, output_test = output1, threads_test = 2),
              test23 = list(path_test = path2, var_test = var2, latitude_test = latitude3,  
                            target_test = target1, output_test = output1, threads_test = 1),
              test24 = list(path_test = path2, var_test = var2, latitude_test = latitude3,                                     target_test = target1, output_test = output1, threads_test = 2))

test_emptylat <- list(
              test25 = list(path_test = path1, var_test = var1,
                            target_test = target1, output_test = output1, threads_test = 1),
              test26 = list(path_test = path1, var_test = var1,
                            target_test = target1, output_test = output1, threads_test = 2),
              test27 = list(path_test = path2, var_test = var1,
                            target_test = target1, output_test = output1, threads_test = 1),
              test28 = list(path_test = path2, var_test = var1,
                            target_test = target1, output_test = output1, threads_test = 2),
              test29 = list(path_test = path1, var_test = var2,
                            target_test = target1, output_test = output1, threads_test = 1),
              test30 = list(path_test = path1, var_test = var2,
                            target_test = target1, output_test = output1, threads_test = 2),
              test31 = list(path_test = path2, var_test = var2, 
                            target_test = target1, output_test = output1, threads_test = 1),
              test32 = list(path_test = path2, var_test = var2,
                            target_test = target1, output_test = output1, threads_test = 2))
lapply(test_emptylat, function(x) {
         list2env(x, env = .GlobalEnv)
         source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")
         print(names(x))
         })
# Manually
## output1 <- output2
## target1 <- target2

# test 12: WORKS!
i = 12
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
# test 20: WORKS!
i = 20
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
# test 4: WORKS!
i = 4
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
# test 28: WORKS!
i = 4
print(names(test_emptylat)[i])
print(test_emptylat[[i]])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")
# test 3: WORKS!
i = 3
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
# test 11: WORKS!
i = 11
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
# test 19: WORKS!
i = 19
print(names(tests)[i])
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 27: WORKS!
i = 3
print(names(test_emptylat)[i])
print(test_emptylat[[i]])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")

#test 7: WORKS!
i = 7
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
#test 15: WORKS!
i = 15
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
#test 23: WORKS!
i = 23
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")
#test 31: WORKS!
i = 7
print(names(test_emptylat)[i])
print(test_emptylat[[i]])
list2env(test_emptylat[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")

#test 8: WORKS!
i = 8
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 16: WORKS!
i = 16
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 24: WORKS!
i = 24
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 32: WORKS!
i = 8
print(names(test_emptylat)[i])
list2env(test_emptylat[[i]], env = .GlobalEnv)
path_test <- '/esarchive/exp/ecmwf/system5c3s/daily/$var$/$var$_$sdate$.nc'
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")
print(test_emptylat[[i]])

#test 1: WORKS!
i = 1
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 9: WORKS!
i = 9
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 17: WORKS!
i = 17
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 25: WORKS!
i = 1
print(names(test_emptylat)[i])
print(test_emptylat[[i]])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")

#test 5: WORKS!
i = 5
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 13: WORKS!
i = 13
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 21: WORKS!
i = 21
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 29: WORKS!
i = 5
print(names(test_emptylat)[i])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")
print(test_emptylat[[i]])


#test 2: FAILS!
i = 2
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 10: FAILS!
i = 10
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 18: FAILS!
i = 18
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 26: WORKS!
i = 2
print(names(test_emptylat)[i])
print(test_emptylat[[i]])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")

#test 6: WORKS!
i = 6
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 14: WORKS!
i = 14
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 22: WORKS!
i = 22
print(names(tests[i]))
print(tests[[i]])
list2env(tests[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_lat.R")

#test 30: WORKS!
i = 6
print(names(test_emptylat)[i])
list2env(test_emptylat[[i]], env = .GlobalEnv)
source("/esarchive/scratch/nperez/git/Flor/Nord3/CodeTest_nolat.R")
print(test_emptylat[[i]])

