library(startR)
source('/home/Earth/aho/aho-testtest/startR/issue/issue7/Start.R')

#repos <- paste0('/esnas/exp/ecearth/i019/monthly_mean/',
#                '$var$_*/$var$_Omon_EC-EARTH_i019_*S$sdate$_$member$_$chunk$.nc')

repos <- paste0('/home/Earth/aho/aho-testtest/startR/issue/',
                '$var$_Omon_EC-EARTH_i019_*S$sdate$_$member$_$chunk$.nc')

region <- array('Barents_Sea', dim = c(chunk = 3, region = 1))  #yes
region <- array(c('Baffin', 'Barents_Sea', 'Barents_Sea'), dim = c(chunk = 3, region = 1))  #only the first used

#region <- 'Barents_Sea'  #yes
#region <- c('Barents_Sea', 'Grnland')  #no
#region <- c('Grnland', 'Barents_Sea')  #no
#region <- c('Baffin', 'Grnland') #yes #candidates = 1 20
#region <- c('Grnland', 'Baffin')  #no
#region <- c('Baffin', 'Barents_Sea') #yes #candidates = 1 2
#region <- c('Barents_Sea', 'Baffin') #no
#region <- c('Baffin', 'Barents_Sea', 'Grnland') #no
#region <- c('Barents_Sea', 'Baffin', 'Grnland') #no
region <- c('Grnland', 'Barents_Sea', 'Baffin') #no
#region <- c('Baffin', 'Beaufort_Sea') #no
#region <- c('Beaufort_Sea', 'Baffin') #no
#region <- c("Baffin", "Barents_Sea", "Beaufort_Sea")  #yes
#region <- c("Baffin_Bay", "Grnland")  #yes #candidates = 19 20
#region <- c("Grnland", "Baffin_Bay")  #no
region <- c("Baffin", "Laptev_Sea", "Barents_Sea")

data <- Start(dat = repos, 
              var = 'tosmean1', 
              sdate = '20051101', 
              member = 'all', 
              chunk = 'all', #indices(1:3),  #'all', 
              time = 'all',
              region = region,  #indices(1:2),   #region,
              chunk_depends = 'sdate', 
              time_across = 'chunk',  
              region_var= 'region',
              return_vars = list(region = NULL),
              merge_across_dims = FALSE, #TRUE,
              retrieve = TRUE)

#---------------------------
  path <- '/home/Earth/aho/aho-testtest/startR/issue/tosmean1_Omon_EC-EARTH_i019_S20051101_r1i1p1_230411-230502.nc'
  fnc <- NcOpen(path)
  fnc_dims <- NcReadDims(fnc)
  var_names <- NcReadVarNames(fnc)
  fnc_arr <- NcToArray(fnc, vars_to_read = 'region')

