## ERA5 exploration:
vars <- c('tas', 'tasmin', 'tasmax', 'prlr')
path <- "/esarchive/recon/ecmwf/era5/monthly_mean/"
raw_freq <- c('_f1h', '_f24h', '_f24h', '_f1h')

monthly_files <- as.vector(sapply(1991:2020, paste0, sprintf("%02d", 1:12)))

for (v in 1:length(vars)) {
  files <- list.files(paste0(path, vars[v], raw_freq[v],'-r1440x721cds/'))
  in_es <- sapply(files, substr, length(strsplit(vars[v], '')[[1]]) + 2,
                                 length(strsplit(vars[v], '')[[1]]) + 7)
  if (all(monthly_files %in% in_es)) {
    print(paste("period available for", vars[v]))
  } else {
    print(monthly_files[!monthly_files %in% in_es])
  }
}

# --------- Daily data files
vars <- c('tas', 'tasmin', 'tasmax', 'prlr')
path <- "/esarchive/recon/ecmwf/era5/daily_mean/"

monthly_files <- as.vector(sapply(1991:2020, paste0, sprintf("%02d", 1:12)))

for (v in 1:length(vars)) {
  files <- list.files(paste0(path, vars[v], '_f1h-r1440x721cds/'))
  in_es <- sapply(files, substr, length(strsplit(vars[v], '')[[1]]) + 2,
                                 length(strsplit(vars[v], '')[[1]]) + 7)
  if (all(monthly_files %in% in_es)) {
    print(paste("period available for", vars[v]))
  } else {
    print(monthly_files[!monthly_files %in% in_es])
  }
}


## ERA5-Land exploration:
vars <- c('tas', 'tasmin', 'tasmax', 'prlr')
path <- "/esarchive/recon/ecmwf/era5land/monthly_mean/"
raw_freq <- c('_f1h', '_f24h', '_f24h', '_f1h')

monthly_files <- as.vector(sapply(1991:2020, paste0, sprintf("%02d", 1:12)))

for (v in 1:length(vars)) {
  files <- list.files(paste0(path, vars[v], raw_freq[v]))
  in_es <- sapply(files, substr, length(strsplit(vars[v], '')[[1]]) + 2,
                                 length(strsplit(vars[v], '')[[1]]) + 7)
  if (all(monthly_files %in% in_es)) {
    print(paste("period available for", vars[v]))
  } else {
    print(monthly_files[!monthly_files %in% in_es])
  }
}

# --------- Daily data files
vars <- c('tas', 'tasmin', 'tasmax', 'prlr')
path <- "/esarchive/recon/ecmwf/era5land/"
freq <- c('daily_mean', 'daily', 'daily', 'daily_mean')

monthly_files <- as.vector(sapply(1991:2020, paste0, sprintf("%02d", 1:12)))

for (v in 1:length(vars)) {
  files <- list.files(paste0(path, vars[v], '_f1h/'))
  in_es <- sapply(files, substr, length(strsplit(vars[v], '')[[1]]) + 2,
                                 length(strsplit(vars[v], '')[[1]]) + 7)
  if (all(monthly_files %in% in_es)) {
    print(paste("period available for", vars[v]))
  } else {
    print(monthly_files[!monthly_files %in% in_es])
  }
}

