# folder <- "/esarchive/exp/"
# institution <- c("meteofrance", "cmcc")
# res <- esarchive_search(institution = c('meteofrance'),
#                         variables = c('tas_', 'prlr_'), freq = 'daily')
esarchive_search <- function(folder = "/esarchive/exp/", institution = NULL, 
                             variables, freq = NULL) {
    information <- NULL
    if (is.null(institution)) {
        institution <- list.dirs(folder, recursive = FALSE, full.names = F)
        print(paste("Total", length(institution), "institutions."))
    }
    if (is.null(freq)) {
        freq <- c('daily', 'monthly', 'annual', 'hourly')
    }
    paths <- lapply(institution, function(x) {file.path(folder, x)})
    for (i in 1:length(paths)) {
        print("Inventorying esarchive...")
        dirs <- list.dirs(paths[[i]], recursive = TRUE)
        print("Subsetting ... ")
        pos <- lapply(variables, function(x) {grep(x, dirs)})
        dirs <- lapply(pos, function(x) {dirs[x]}) 
        pos <- lapply(dirs, function(y) {
                  lapply(freq, function(x) {grep(x, y)})})
        dirs <- lapply(1:length(variables), function(x) {dirs[[x]][unlist(pos[[x]])]})
           for (k in 1:length(variables)) {
               for (j in dirs[[k]]) {
                files <- list.files(j, pattern = ".nc")
                if (length(files) > 0) {
                    period <- range(as.numeric(substr(files, nchar(variables[k]) + 1,
                                   nchar(variables[k]) + 4))) 
                    tryCatch({
                        f <- ncdf4::nc_open(file.path(j,files[1])) 
                        meta <- ncdf4::ncatt_get(f, 
                                 substr(variables[k], 1, nchar(variables[k]) - 1))
                        ncdf4::nc_close(f)
                            }, error = function(e) {
                                      meta <- list(long_name = NA, units = NA)})
                    info <- c(variables[k], j,  period ,meta$long_name, meta$units)
                    information <- rbind(information, info) 
                } 
             }
         }
    }
    return(information)
}

