Spaces:
Sleeping
Sleeping
Download funs/check_update.R from climateexploer/wmonormals: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/spaces/climateexploer/wmonormals/resolve/main/funs/check_update.R
- Command line
-
hf download hf://spaces/climateexploer/wmonormals/funs/check_update.R
-
curl -L -o check_update.R https://huggingface.co/spaces/climateexploer/wmonormals/resolve/main/funs/check_update.R
3.6 kB
| # Robust data checking function for WMO Normals online updates | |
| get_dataset_status <- function() { | |
| cache_file <- "data/dataset_status_cache.rds" | |
| local_data_file <- "data/wmo_normals_9120_TAVG_min24.csv" | |
| # Determine local file date | |
| local_date <- NA | |
| if (file.exists(local_data_file)) { | |
| # Extract just the Date from the mtime | |
| local_date <- as.Date(file.info(local_data_file)$mtime) | |
| } | |
| # Allow offline test suites to skip remote HTTP checks and cache writes | |
| if (identical(tolower(Sys.getenv("WMONORMALS_SKIP_REMOTE_SYNC")), "true")) { | |
| if (file.exists(cache_file)) { | |
| cache_data <- tryCatch(readRDS(cache_file), error = function(e) NULL) | |
| if (!is.null(cache_data)) { | |
| return(list( | |
| local_date = local_date, | |
| online_date = cache_data$online_date, | |
| status = if (is.na(cache_data$online_date) || is.na(local_date)) "Unknown" else if (cache_data$online_date > local_date) "Update Available" else "Up-to-date", | |
| error = cache_data$error, | |
| cached = TRUE | |
| )) | |
| } | |
| } | |
| return(list( | |
| local_date = local_date, | |
| online_date = NA, | |
| status = "Unknown", | |
| error = NULL, | |
| cached = TRUE | |
| )) | |
| } | |
| # Check cache first | |
| if (file.exists(cache_file)) { | |
| cache_data <- readRDS(cache_file) | |
| # If cache is less than 30 days old, return cached online date | |
| days_diff <- as.numeric(Sys.Date() - cache_data$last_checked) | |
| if (!is.na(days_diff) && days_diff < 30) { | |
| return(list( | |
| local_date = local_date, | |
| online_date = cache_data$online_date, | |
| status = if (is.na(cache_data$online_date) || is.na(local_date)) "Unknown" else if (cache_data$online_date > local_date) "Update Available" else "Up-to-date", | |
| error = cache_data$error, | |
| cached = TRUE | |
| )) | |
| } | |
| } | |
| # If we need to fetch from online | |
| online_date <- NA | |
| err_msg <- NULL | |
| tryCatch({ | |
| url <- "https://www.ncei.noaa.gov/access/metadata/landing-page/bin/iso?id=gov.noaa.nodc:0253808" | |
| # Using readLines safely, ignoring warnings if connection fails temporarily | |
| lines <- suppressWarnings(readLines(url, warn = FALSE)) | |
| # Grab the string denoting a revision: "YYYY-MM-DDTHH:MM:SSZ - NCEI Accession 0253808 was revised" | |
| matches <- grep("NCEI Accession 0253808 was revised", lines, value = TRUE) | |
| if (length(matches) > 0) { | |
| # Extract the ISO date component part e.g. "2024-06-12" | |
| dates_str <- regmatches(matches, regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", matches)) | |
| if (length(dates_str) > 0) { | |
| # The revision might have occurred multiple times; pick the most recent one | |
| online_date <- max(as.Date(dates_str)) | |
| } | |
| } | |
| }, error = function(e) { | |
| err_msg <<- e$message | |
| }) | |
| # Attempt to fallback to previously cached online date if a network error occurs | |
| if (is.na(online_date) && file.exists(cache_file)) { | |
| cache_data <- readRDS(cache_file) | |
| online_date <- cache_data$online_date | |
| } | |
| # Save the new check to cache | |
| cache_data <- list( | |
| last_checked = Sys.Date(), | |
| online_date = online_date, | |
| error = err_msg | |
| ) | |
| # Ensure data directory exists before writing cache | |
| if (!dir.exists("data")) dir.create("data") | |
| saveRDS(cache_data, cache_file) | |
| # Final return | |
| status <- "Unknown" | |
| if (!is.na(online_date) && !is.na(local_date)) { | |
| status <- if (online_date > local_date) "Update Available" else "Up-to-date" | |
| } | |
| list( | |
| local_date = local_date, | |
| online_date = online_date, | |
| status = status, | |
| error = err_msg, | |
| cached = FALSE | |
| ) | |
| } | |