hcst<-Start(dat = "/esarchive/exp/ncep/cfs-v2/weekly_mean/s2s/$var$_f24h/$file_date$.nc",
            var = 'tas',
            file_date = c("tas_20130611", "tas_20130625"),
            time = indices(1:4),
            latitude = values(list(20, 50)),
            latitude_reorder = Sort(decreasing = TRUE),
            longitude = values(list(-20, 10)),
            longitude_reorder = CircularSort(-180, 180),
            synonims = list(latitude=c('lat','latitude'),
                            longitude=c('lon','longitude')),
            ensemble = indices(1:12),
            return_vars = list(latitude = 'dat',
                               longitude = 'dat',
                               time = c('file_date')),
            #split_multiselected_dims = TRUE,
            num_procs = 1,
            retrieve = TRUE)

# Initial code:
  variable     <- "tas" 
  fcst.sdate   <- 20200611 
  system.name <- "cfs-v2"
  hcst.dir <- "/esarchive/exp/ncep/cfs-v2/"
  lons.min <- -20  
  lons.max <- 10  
  lats.min <- 20  
  lats.max <- 50 
  hcst.nmember <- 12
  fcst.freq <- "weekly_mean"
  ltmin <- 1
  ltmax <- 4 
  obs.grid <- "-384x190"
  hcst.ltmin <- 3 
  hcst.ltmax <- 7 

  library(startR)
  get.file_dates <- function(startdate, var){
      file_dates <- numeric(0)
      sdates <- numeric(0)
      startdate <- format(as.Date(as.character(startdate), "%Y%m%d"), "%Y%m")
      startdate <- as.Date(toString(paste0(startdate,01)), "%Y%m%d")
      for (dumday in c(0:30)) {
          if (format(startdate + dumday, "%a") == "Thu") {
                  sdates <- c(sdates,format(startdate + dumday, "%m%d")) 
          }
      }

      fcst.year <- substr(startdate,1,4)

      for (year in c((as.numeric(fcst.year)-hcst.ltmax):(as.numeric(fcst.year)-hcst.ltmin))){
         file_dates <- append(file_dates,paste0(year,sdates))
      }
      load_file_dates <- array(paste0(var,"_",file_dates), dim=c(length(sdates),1,hcst.ltmax-hcst.ltmin))

      names(dim(load_file_dates)) <- c('sweek','sday','syear')
      return(load_file_dates)
  }

  file_dates <- get.file_dates(fcst.sdate,variable)

  switch (tolower(variable),
      "tas"     = {freq.hcst <- 'f24h';    freq.obs <- 'f1h'}
  ) 

  hcst.path <- paste0(
               hcst.dir,fcst.freq,"/s2s","/$var$_",
               freq.hcst,"/$file_date$",
               ".nc")

  hcst<-Start(dat = hcst.path,
              var = variable,
              file_date = file_dates,
              time = indices(ltmin:ltmax),
	                latitude = values(list(lats.min, lats.max)),
		        latitude_reorder = Sort(decreasing = TRUE),
	                longitude = values(list(lons.min, lons.max)),
			longitude_reorder = CircularSort(-180, 180),          
	      synonims = list(latitude=c('lat','latitude'),
                              longitude=c('lon','longitude')),
	      ensemble = indices(1:hcst.nmember),
              return_vars = list(latitude = 'dat',
                                 longitude = 'dat',
                                  time = c('file_date')),
              split_multiselected_dims = TRUE,
	      num_procs = 1,
              retrieve = T)

