subseas_file_dates <- function(startdate, n.skill.weeks, n.days,
                               hcst.start, hcst.end,
                               ftime_min, ftime_max, out) {
  ftime_min <- as.numeric(substr(as.character(startdate), 1, 4)) - hcst.end
  ftime_max <- as.numeric(substr(as.character(startdate), 1, 4)) - hcst.start
  startdate <- as.Date(toString(startdate), "%Y%m%d") 
  sdates <- numeric(0)
  while (length(sdates) < n.skill.weeks){
    if (format(startdate, "%a") == "Thu" || format(startdate, "%a") == "Mon") {
      sdates <- c(sdates, format(startdate, "%Y%m%d"))
    }
    startdate <- startdate - 1
  }

  file_dates <- array(numeric(), c(0, n.days, (ftime_max -ftime_min) + 1))
  file_dates.fcst <- array(numeric(), c(0, n.days, (ftime_max - ftime_min) + 1))
  for (sdate in sdates) {
    ## TODO: take into account year of the fcst to select hcst years
    fcst.day <- substr(sdate, 7, 8)
    fcst.month <- substr(sdate, 5, 6)
    fcst.year <- substr(sdate, 1, 4)

    startdate <- as.Date(toString(sdate), "%Y%m%d")

    # makes sure of giving a: [M T M || T M T] combination
    if(format(startdate, "%a") == "Thu") {
      hcst.sdays <- c(startdate - 3, startdate, startdate + 4, recursive = T)
    } else {
      hcst.sdays <- c(startdate - 4, startdate, startdate + 3, recursive = T)
    }

    hcst.sdays <- format(as.Date(hcst.sdays,"%Y%m%d"), "%Y%m%d")

    sdays.file_dates <- array(numeric(),c(0, (ftime_max - ftime_min) + 1))
    sdays.file_dates.fcst <- array(hcst.sdays, c(n.days, 
                                   (ftime_max - ftime_min) + 1))
    for (sday.sdate in hcst.sdays) {
      sday.year <- substr(sday.sdate, 1, 4)
      sday.mday <- substr(sday.sdate, 5, 8)
      sday.years <- paste((strtoi(sday.year)- ftime_max):
                                  (strtoi(sday.year) - ftime_min), sep = "")
        sday.dates <- apply(expand.grid(sday.years, sday.mday), 
                            1, paste, collapse = "")
        sdays.file_dates <- abind(sdays.file_dates, sday.dates, along = 1)
    }

    names(dim(sdays.file_dates)) <- c('sday','syear')

    file_dates <- abind(file_dates, sdays.file_dates, along = 1)
    file_dates.fcst <- abind(file_dates.fcst, sdays.file_dates.fcst, along = 1)

  }
  names(dim(file_dates)) <- c('sweek','sday','syear')
  names(dim(file_dates.fcst)) <- c('sweek','sday','syear')

#  dir_dates <- paste0(file_dates.fcst,"/",var,"_",file_dates)
#  dim(dir_dates) <- dim(file_dates)
#  names(dim(dir_dates)) <- c('sweek','sday','syear')
#  dir_dates <- substr(dir_dates,10,24)
#  return(dir_dates)
  if (out == 'fcst') {
    central_day <- (n.days + 1)/2
    central_week <- (n.skill.weeks + 1)/2
    file_dates <- file_dates.fcst[central_week,central_day, 1]
  }
  return(file_dates)
}
