#library(s2dverification)

library(ncdf4)
library(plyr)
source("/esarchive/scratch/nperez/git/s2dverification/R/Load.R")
source("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R")
source("/esarchive/scratch/nperez/git/s2dverification/R/LeapYear.R")


model <- 'ecmwfS5'; ref1 <- 'eobsv15'; ref2 <- 'pthres'
latmax <- 42; latmin <- 40; lonmax <- 354; lonmin <- 352 # smaller domain (compared to ecmwfs5 one)
yearini <- 1993; yearend <- 2016; startMonth <- '04'
sdates <- paste0(yearini:yearend, startMonth, "01")
leadtimemin <- 21; leadtimemax <- 30 + 31 + 21
nsdates <- yearend - yearini + 1
psl_s5_path <- list(path = "/esarchive/exp/ecmwf/system5c3s/daily_mean/$VAR_NAME$_f6h/$VAR_NAME$_$YEAR$$MONTH$01.nc")
psl_eobs_path <- list(path = "/esarchive/obs/ecad/eobs_v15/daily_mean/$VAR_NAME$/$VAR_NAME$_$YEAR$$MONTH$.nc")


var = 'psl'
exp = list(psl_s5_path)
obs = list(psl_eobs_path)
nmember = 1

                 nmemberobs = NULL; nleadtime = NULL; leadtimemin = 1 
                 leadtimemax = 7; storefreq = 'daily'; sampleperiod = 1 
                 output = 'lonlat'; 
method = 'conservative'; grid = NULL
                 maskmod = vector("list", 15); maskobs = vector("list", 15) 
                 configfile = NULL; varmin = NULL; varmax = NULL
                 silent = FALSE; nprocs = 1; dimnames = NULL 
                 remapcells = 2; path_glob_permissive = 'partial'

parameter_names <- names(list(var = 'psl', exp = list(psl_s5_path), obs = list(psl_eobs_path), sdates = sdates, nmember = 1,
             lonmin = lonmin, lonmax = lonmax, latmin = latmin, latmax = latmax,
             storefreq = "daily", sampleperiod = 1, grid = NULL,
             leadtimemin = 1, leadtimemax = 7,
             output = "lonlat", nprocs = 1))

# source_lines("/esarchive/scratch/nperez/git/startR/inst/doc/usecase/ex2_1_timedim.R",
#              start = 4, end = 14)
source_lines <- function(file, start, end, ...) {
    file.lines <- scan(file, what=character(), skip=start-1, nlines=end-start+1, sep='\n')
    file.lines.collapsed <- paste(file.lines, collapse='\n')
    source(textConnection(file.lines.collapsed), ...)
}
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 858, end = 896)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 1391, end = 1461)# .warnings()
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 1, end = 4) #.IsFullMatch
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 1334, end = 1389) #.message
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 899, end = 1385)
print(jmod)
print(nmod)
# loop length 1:
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 6, end = 31) #.ConfigReplaceVariablesInString
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1387, end = 1421)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1423, end = 1430)
print(dims2define)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1436, end = 1443)
#work_piece, 
explore_dims = TRUE
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 86, end = 156)
length(files)
library(ncdf4)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 159, end = 574) 
length(expected_dims) # greater than 0
dim_matches <- match(expected_dims, var_dimnames)
      (any(is.na(dim_matches))) 
#> expected_dims
#[1] "lon"      "lat"      "ensemble"
#> var_dimnames
#[1] "lon"      "lat"      "ensemble" "time"     

#The problem doesn't appear the first time you use .LoadDataFiles
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 79, end = 951) #.LoadDataFile
library(plyr)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 1036, end = 1190) #.ReplaceGlobExpressions
output = 'lonlat'
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 899, end = 1586)
print(jobs)
print(nobs)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1588, end = 1616)
(is_file_per_dataset_obs[jobs]) # jump if -... go to else
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1700, end = 1700)
print(jsdate)
print(nsdates) # the last loop is the one that fails
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1721)
length(leadtimes) #7
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1723, end = 1741)
(dims2define) # TRUE


work_piece <- list(dataset_type = dataset_type,
                               filename = .ConfigReplaceVariablesInString(quasi_final_path, replace_values),
                               namevar = namevar, grid = grid, remap = remap, remapcells = remapcells,
                               is_file_per_member = is_file_per_member_obs[jobs],
                               is_file_per_dataset = is_file_per_dataset_obs[jobs],
                               lon_limits = c(lonmin, lonmax),
                               lat_limits = c(latmin, latmax), 
                               dimnames = obs[[jobs]][['dimnames']], single_dataset = single_dataset)
            found_data <- .LoadDataFile(work_piece, explore_dims = TRUE, silent = silent)
# falla... debuggar .LoadDataFile
 explore_dims = TRUE
source_lines("/esarchive/scratch/nperez/git/Flor/s2dverification_Utils.R",  start = 86, end = 156)
source_lines("/esarchive/scratch/nperez/git/Flor/s2dverification_Utils.R",  start = 158, end = 213)
is_2d_var
source_lines("/esarchive/scratch/nperez/git/Flor/s2dverification_Utils.R",  start = 215, end = 408)
source_lines("/esarchive/scratch/nperez/git/Flor/s2dverification_Utils.R",  start = 409, end = 557)
source_lines("/esarchive/scratch/nperez/git/Flor/s2dverification_Utils.R",  start = 562, end = 576)

source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 86, end = 154)
length(files)
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Utils.R", start = 159, end = 574)
length(expected_dims) # 2!!
var_dimnames #NULL!!
dim_matches <- match(expected_dims, var_dimnames)

    var_dimnames <- unlist(lapply(fnc$var[[namevar]][['dim']], '[[', 'name'))




source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1843)
jsdate
source_lines("/esarchive/scratch/nperez/git/s2dverification/R/Load.R", start = 1702, end = 1721)
length(leadtimes)







get('var', envir = environment())



    source("create_nc.R")  # Load function create_nc
    source("Support_functions_bias_correction.R")

    year.ini <- 1993
    monthName <- month.ini
    fcsts <- aggregation

    if (tolower(variable) == "tas") {
      standard_names <- "mean temperature"
      freq.exp <- 'f6h'
      freq.obs <-  'f6h'
    }

  exp <- list(name = 'syst5c3s',
     path = file.path(paste0("/esarchive/exp/ecmwf/system5c3s/monthly_mean",
               "/$VAR_NAME$_6h/$VAR_NAME$_$YEAR$$MONTH$01.nc")))

data <- Load(var = 'tas',
      list(exp),
      list(obs),
      sdates = sdates,
      nmember = nmember,
      lonmin = lonmin,
      lonmax = lonmax,
      latmin = latmin,
      latmax = latmax,
      storefreq = "monthly",
      sampleperiod = 1,
      grid = "r360x180",
      leadtimemin = ltmin,
      leadtimemax = ltmax,
      output = "lonlat",
      nprocs = 1)  #or system5_m1

