Spaces:
Sleeping
Sleeping
Download global.R from climateexploer/wmonormals: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/spaces/climateexploer/wmonormals/resolve/main/global.R
- Command line
-
hf download hf://spaces/climateexploer/wmonormals/global.R
-
curl -L -o global.R https://huggingface.co/spaces/climateexploer/wmonormals/resolve/main/global.R
11.5 kB
| library(shiny) | |
| library(bslib) | |
| library(mapgl) | |
| library(sf) | |
| library(dplyr) | |
| library(leaflet) | |
| library(DT) | |
| library(plotly) | |
| # Map Configurations matching GHCNM | |
| ofm_positron_style <- "https://tiles.openfreemap.org/styles/positron" | |
| ofm_bright_style <- "https://tiles.openfreemap.org/styles/bright" | |
| # EOX Sentinel-2 Cloudless (Free for commercial use with attribution) | |
| sentinel_url <- "https://tiles.maps.eox.at/wmts/1.0.0/s2cloudless-2023_3857/default/GoogleMapsCompatible/{z}/{y}/{x}.jpg" | |
| sentinel_attribution <- '<a href="https://s2maps.eu" target="_blank">Sentinel-2 cloudless - by EOX IT Services GmbH</a> (Contains modified Copernicus Sentinel data 2023)' | |
| cat("Loading multi-annual parameter composites...\n") | |
| # Source custom functions | |
| source("funs/load_wmo_data.R", local = TRUE) | |
| source("funs/render_station_plots.R", local = TRUE) | |
| source("funs/check_update.R", local = TRUE) | |
| # Supported WMO Climatological Normal Periods | |
| NORMALS_PERIOD_CHOICES <- c( | |
| "1991–2020 (Current Standard)" = "1991-2020", | |
| "1981–2010 (Previous Standard)" = "1981-2010", | |
| "1961–1990 (Reference Baseline)" = "1961-1990" | |
| ) | |
| PERIOD_CODE_MAP <- c( | |
| "1991-2020" = "9120", | |
| "1981-2010" = "8110", | |
| "1961-1990" = "6190" | |
| ) | |
| # In-memory memoization cache for loaded sf datasets | |
| wmo_cache <- new.env(parent = emptyenv()) | |
| get_wmo_dataset <- function(period = "1991-2020", param = "TAVG") { | |
| if (is.null(period) || length(period) == 0 || is.na(period[1]) || !nzchar(trimws(period[1]))) { | |
| period <- "1991-2020" | |
| } | |
| if (is.null(param) || length(param) == 0 || is.na(param[1]) || !nzchar(trimws(param[1]))) { | |
| param <- "TAVG" | |
| } | |
| p_code <- if (!is.na(PERIOD_CODE_MAP[period])) unname(PERIOD_CODE_MAP[period]) else "9120" | |
| cache_key <- paste0(p_code, "_", param) | |
| mtime_key <- paste0("mtime_", cache_key) | |
| filename <- paste0("wmo_normals_", p_code, "_", param, "_min24.csv") | |
| filepath <- paste0("data/", filename) | |
| file_mtime <- if (file.exists(filepath)) file.info(filepath)$mtime else as.POSIXct(0, origin = "1970-01-01") | |
| if (exists(cache_key, envir = wmo_cache) && exists(mtime_key, envir = wmo_cache)) { | |
| if (identical(get(mtime_key, envir = wmo_cache), file_mtime)) { | |
| return(get(cache_key, envir = wmo_cache)) | |
| } | |
| } | |
| df_sf <- load_wmo_data(filename) | |
| assign(cache_key, df_sf, envir = wmo_cache) | |
| assign(mtime_key, file_mtime, envir = wmo_cache) | |
| return(df_sf) | |
| } | |
| # Pre-warm default 1991-2020 layers at startup | |
| tavg_sf <- get_wmo_dataset("1991-2020", "TAVG") | |
| tmax_sf <- get_wmo_dataset("1991-2020", "TMAX") | |
| tmin_sf <- get_wmo_dataset("1991-2020", "TMIN") | |
| prcp_sf <- get_wmo_dataset("1991-2020", "PRCP") | |
| mslp_sf <- get_wmo_dataset("1991-2020", "MSLP") | |
| mnvp_sf <- get_wmo_dataset("1991-2020", "MNVP") | |
| dp01_sf <- get_wmo_dataset("1991-2020", "DP01") | |
| tsun_sf <- get_wmo_dataset("1991-2020", "TSUN") | |
| get_countries_for_period <- function(period = "1991-2020") { | |
| if (is.null(period) || length(period) == 0 || is.na(period[1]) || !nzchar(trimws(period[1]))) { | |
| period <- "1991-2020" | |
| } | |
| params <- c("TAVG", "PRCP", "TMAX", "TMIN", "MSLP", "MNVP", "DP01", "TSUN") | |
| p_code <- if (!is.na(PERIOD_CODE_MAP[period])) unname(PERIOD_CODE_MAP[period]) else "9120" | |
| # Compute combined timestamp hash across parameter files | |
| mtimes <- vapply(params, function(p) { | |
| fp <- paste0("data/wmo_normals_", p_code, "_", p, "_min24.csv") | |
| if (file.exists(fp)) as.character(file.info(fp)$mtime) else "0" | |
| }, character(1)) | |
| mtimes_hash <- paste(mtimes, collapse = "|") | |
| cache_key <- paste0("countries_", period) | |
| hash_key <- paste0("hash_countries_", period) | |
| if (exists(cache_key, envir = wmo_cache) && exists(hash_key, envir = wmo_cache)) { | |
| if (identical(get(hash_key, envir = wmo_cache), mtimes_hash)) { | |
| return(get(cache_key, envir = wmo_cache)) | |
| } | |
| } | |
| all_countries <- character(0) | |
| for (p in params) { | |
| ds <- get_wmo_dataset(period, p) | |
| if (inherits(ds, "sf") && nrow(ds) > 0 && "Country" %in% names(ds)) { | |
| all_countries <- c(all_countries, ds$Country) | |
| } | |
| } | |
| uniq_c <- sort(unique(all_countries[!is.na(all_countries) & nzchar(trimws(all_countries))])) | |
| assign(cache_key, uniq_c, envir = wmo_cache) | |
| assign(hash_key, mtimes_hash, envir = wmo_cache) | |
| return(uniq_c) | |
| } | |
| countries_list <- get_countries_for_period("1991-2020") | |
| WMO_COUNTRY_EQUIVALENCE_GROUPS <- list( | |
| c("Cabo_Verde_Republic", "Cape Verde", "Cabo Verde"), | |
| c("Czech_Republic", "Czech Republic", "Czechia"), | |
| c("Denmark_Faroe_Islands", "The Faroe Islands", "Faroe Islands"), | |
| c("Denmark_Greenland", "Greenland"), | |
| c("Hong_Kong,_China", "Hong Kong", "Hong Kong, China"), | |
| c("Iran", "Iran, Islamic Republic Of", "Iran (Islamic Republic of)"), | |
| c("Kazakhstan", "Kazakstan (Asia)", "Kazakstan (Europe)", "Kazakstan"), | |
| c("Kyrgyzstan", "Kyrgyz Republic"), | |
| c("Laos", "Lao Pdr", "Lao People'S Democratic Republic", "Lao PDR"), | |
| c("Macau,_China", "Macao China", "Macao, China"), | |
| c("Moldova", "Moldova, Republic Of", "Republic of Moldova"), | |
| c("Netherlands", "The Netherlands"), | |
| c("Rep._Korea", "The Republic Of Korea", "Korea, Republic Of", "Republic of Korea", "South Korea"), | |
| c("Russian_Federation_(In_Asia)", "Russian Federation (Asia)"), | |
| c("Russian_Federation_(In_Europe)", "Russian Federation (Europe)"), | |
| c("Saudi_Arabia", "Kingdom Of Saudi Arabia"), | |
| c("Tanzania", "Tanzania, United Republic Of", "United Republic of Tanzania"), | |
| c("United_Kingdom", "United Kingdom Of Great Britain & Northern Ireland", "UK"), | |
| c("United_States", "United States Of America", "United States", "USA") | |
| ) | |
| resolve_wmo_country <- function(country_param, valid_countries) { | |
| if (is.null(country_param) || length(country_param) == 0 || is.na(country_param[1])) { | |
| return(NULL) | |
| } | |
| raw <- trimws(as.character(country_param[1])) | |
| if (!nzchar(raw)) { | |
| return(NULL) | |
| } | |
| if (tolower(raw) %in% c("all", "all countries")) { | |
| return("All") | |
| } | |
| if (length(valid_countries) == 0) { | |
| return(NULL) | |
| } | |
| idx <- which(valid_countries == raw) | |
| if (length(idx) > 0) return(valid_countries[idx[1]]) | |
| valid_norms <- normalize_wmo_key(valid_countries) | |
| raw_norm <- normalize_wmo_key(raw) | |
| if (nzchar(raw_norm)) { | |
| idx <- which(valid_norms == raw_norm) | |
| if (length(idx) > 0) return(valid_countries[idx[1]]) | |
| } | |
| valid_slugs <- wmo_slugify(valid_countries) | |
| raw_slug <- wmo_slugify(raw) | |
| if (nzchar(raw_slug)) { | |
| idx <- which(valid_slugs == raw_slug) | |
| if (length(idx) > 0) return(valid_countries[idx[1]]) | |
| } | |
| for (grp in WMO_COUNTRY_EQUIVALENCE_GROUPS) { | |
| grp_norms <- normalize_wmo_key(grp) | |
| grp_slugs <- wmo_slugify(grp) | |
| in_grp <- (raw %in% grp) || | |
| (nzchar(raw_norm) && raw_norm %in% grp_norms) || | |
| (nzchar(raw_slug) && raw_slug %in% grp_slugs) | |
| if (in_grp) { | |
| for (alias in grp) { | |
| a_idx <- which(valid_countries == alias) | |
| if (length(a_idx) > 0) return(valid_countries[a_idx[1]]) | |
| a_norm <- normalize_wmo_key(alias) | |
| if (nzchar(a_norm)) { | |
| a_idx <- which(valid_norms == a_norm) | |
| if (length(a_idx) > 0) return(valid_countries[a_idx[1]]) | |
| } | |
| a_slug <- wmo_slugify(alias) | |
| if (nzchar(a_slug)) { | |
| a_idx <- which(valid_slugs == a_slug) | |
| if (length(a_idx) > 0) return(valid_countries[a_idx[1]]) | |
| } | |
| } | |
| } | |
| } | |
| NULL | |
| } | |
| canonical_wmo_parent_country <- function(country_val) { | |
| if (is.null(country_val) || length(country_val) == 0 || is.na(country_val[1])) { | |
| return("All") | |
| } | |
| raw <- trimws(as.character(country_val[1])) | |
| if (!nzchar(raw) || tolower(raw) %in% c("all", "all countries")) { | |
| return("All") | |
| } | |
| resolved <- resolve_wmo_country(raw, countries_list) | |
| if (!is.null(resolved) && nzchar(resolved)) { | |
| return(resolved) | |
| } | |
| raw | |
| } | |
| resolve_wmo_station <- function(station_param, country_param = NULL, norm_period = "1991-2020", active_param = "TAVG") { | |
| if (is.null(station_param) || length(station_param) == 0 || is.na(station_param[1])) { | |
| return(NULL) | |
| } | |
| raw <- trimws(as.character(station_param[1])) | |
| if (!nzchar(raw) || tolower(raw) %in% c("all", "all stations")) { | |
| return(NULL) | |
| } | |
| param_order <- unique(c(active_param, "TAVG", "PRCP", "TMAX", "TMIN", "MSLP", "MNVP", "DP01", "TSUN")) | |
| candidate_dfs <- c( | |
| lapply(param_order, function(p) get_wmo_dataset(norm_period, p)), | |
| list( | |
| get_wmo_dataset("1991-2020", "TAVG"), | |
| get_wmo_dataset("1991-2020", "PRCP"), | |
| get_wmo_dataset("1981-2010", "TAVG"), | |
| get_wmo_dataset("1961-1990", "TAVG") | |
| ) | |
| ) | |
| raw_no_zero <- sub("^0+", "", raw) | |
| raw_is_digits <- grepl("^[0-9]+$", raw) | |
| raw_norm <- normalize_wmo_key(raw) | |
| raw_slug <- wmo_slugify(raw) | |
| match_df <- function(cdf) { | |
| if (is.null(cdf) || !inherits(cdf, "sf") || nrow(cdf) == 0) { | |
| return(NULL) | |
| } | |
| nms <- as.character(cdf$Station_Name) | |
| wmo_ids <- trimws(as.character(cdf$WMO_ID)) | |
| wigos_ids <- if ("WIGOS_ID" %in% names(cdf)) trimws(as.character(cdf$WIGOS_ID)) else rep("", nrow(cdf)) | |
| s_slugs <- if ("Station_Slug" %in% names(cdf)) as.character(cdf$Station_Slug) else wmo_slugify(nms) | |
| # 1. Exact Station_Name, WMO_ID, or case-insensitive WIGOS_ID | |
| idx <- which( | |
| nms == raw | | |
| (!is.na(wmo_ids) & wmo_ids == raw) | | |
| (!is.na(wigos_ids) & nzchar(wigos_ids) & tolower(wigos_ids) == tolower(raw)) | |
| ) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| # 2. Leading-zero-normalized numeric WMO / WIGOS suffix match | |
| if (raw_is_digits && nzchar(raw_no_zero)) { | |
| idx <- which( | |
| (!is.na(wmo_ids) & nzchar(wmo_ids) & sub("^0+", "", wmo_ids) == raw_no_zero) | | |
| (!is.na(wigos_ids) & nzchar(wigos_ids) & sub("^0+", "", sub("^.*-", "", wigos_ids)) == raw_no_zero) | |
| ) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| } | |
| # 3. Normalized Station_Name key match | |
| idx <- which(normalize_wmo_key(nms) == raw_norm) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| # 4. Slug matches (disambiguated override slug, standard slug, ID slug / suffix) | |
| if (nzchar(raw_slug)) { | |
| id_slugs <- wmo_slugify(wmo_ids) | |
| id_nozero_slugs <- wmo_slugify(sub("^0+", "", wmo_ids)) | |
| override_full <- ifelse(nzchar(id_slugs), paste0(s_slugs, "-", id_slugs), "") | |
| override_nz <- ifelse(nzchar(id_nozero_slugs), paste0(s_slugs, "-", id_nozero_slugs), "") | |
| idx <- which( | |
| (nzchar(override_full) & override_full == raw_slug) | | |
| (nzchar(override_nz) & override_nz == raw_slug) | |
| ) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| idx <- which(s_slugs == raw_slug) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| idx <- which( | |
| nzchar(id_slugs) & | |
| (id_slugs == raw_slug | endsWith(raw_slug, paste0("-", id_slugs))) | |
| ) | |
| if (length(idx) > 0) return(cdf[idx[1], ]) | |
| } | |
| NULL | |
| } | |
| all_known_countries <- unique(c( | |
| get_countries_for_period(norm_period), | |
| get_countries_for_period("1991-2020"), | |
| get_countries_for_period("1981-2010"), | |
| get_countries_for_period("1961-1990") | |
| )) | |
| c_target <- resolve_wmo_country(country_param, all_known_countries) | |
| if (!is.null(c_target) && !identical(c_target, "All")) { | |
| for (cdf in candidate_dfs) { | |
| if (!is.null(cdf) && inherits(cdf, "sf") && nrow(cdf) > 0 && "Country" %in% names(cdf)) { | |
| sub_cdf <- cdf[cdf$Country == c_target, ] | |
| hit <- match_df(sub_cdf) | |
| if (!is.null(hit)) return(hit) | |
| } | |
| } | |
| } | |
| for (cdf in candidate_dfs) { | |
| hit <- match_df(cdf) | |
| if (!is.null(hit)) return(hit) | |
| } | |
| NULL | |
| } | |