From 3f7805241803eff4fafe716fa87cc016000bd378 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Feb 2026 13:39:06 +0100 Subject: [PATCH 01/13] fix: meteo_imgw_monthly --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ R/meteo_imgw_monthly.R | 6 +++--- tests/testthat/test-meteo_imgw.R | 14 ++++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c23b156..136116b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: climate Title: Interface to Download Meteorological (and Hydrological) Datasets -Version: 1.2.9 +Version: 1.3.0.9001 Authors@R: c(person(given = "Bartosz", family = "Czernecki", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 8f061e3..a917a9b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# climate 1.3.0 + +* fixes for bugs in `meteo_imgw_monthly()` + + # climate 1.2.9 * fixes for corrupted header files in `meteo_imgw_` family of functions due to changes in the IMGW-PIB repository diff --git a/R/meteo_imgw_monthly.R b/R/meteo_imgw_monthly.R index 7ccbe7f..7a4a261 100644 --- a/R/meteo_imgw_monthly.R +++ b/R/meteo_imgw_monthly.R @@ -113,7 +113,7 @@ meteo_imgw_monthly_bp = function(rank, ind = lapply(years_in_catalogs, function(x) sum(x %in% year) > 0) catalogs = catalogs[unlist(ind)] - all_data = vector("list", length = length(catalogs)) + all_data = NULL for (i in seq_along(catalogs)) { catalog = gsub(catalogs[i], pattern = "/", replacement = "") @@ -155,8 +155,8 @@ meteo_imgw_monthly_bp = function(rank, if (file.exists(file2)) { data2 = imgw_read(translit, file2) colnames(data2) = meta[[2]]$parameters - for (labs in seq_along(meta[[1]]$parameters)) { - attr(data2[[labs]], "label") = meta[[1]]$label[[labs]] + for (labs in seq_along(meta[[2]]$parameters)) { + attr(data2[[labs]], "label") = meta[[2]]$label[[labs]] } data2$POST = trimws(data2$POST) data.table::setDT(data2) diff --git a/tests/testthat/test-meteo_imgw.R b/tests/testthat/test-meteo_imgw.R index 93e9d83..8dce8dc 100644 --- a/tests/testthat/test-meteo_imgw.R +++ b/tests/testthat/test-meteo_imgw.R @@ -22,9 +22,19 @@ test_that("meteo_imgw works!", { x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE, col_names = "full") x <- meteo_imgw("monthly", "synop", year = y, col_names = "polish") x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE, col_names = "polish") - x <- meteo_imgw("monthly", "synop", year = y, station = "BIAŁYSTOK") - x2 <- meteo_imgw("monthly", "synop", year = y, station = 353230295) testthat::expect_message(x <- suppressWarnings(meteo_imgw_daily(rank = "synop", year = 2001, station = "blabla"))) } }) + + +test_that("meteo_imgw monthly works!", { + + if (!curl::has_internet()) { + message("No internet connection! \n") + return(invisible(NULL)) + } else { + x = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "BIAŁYSTOK") + expect_equal(nrow(x), 24) + } +}) \ No newline at end of file From f7e7867c36d144613f34e38e1b1d05b58553ae8a Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Feb 2026 14:02:04 +0100 Subject: [PATCH 02/13] testing unit-test --- tests/testthat/test-meteo_imgw.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/testthat/test-meteo_imgw.R b/tests/testthat/test-meteo_imgw.R index 8dce8dc..c5473ff 100644 --- a/tests/testthat/test-meteo_imgw.R +++ b/tests/testthat/test-meteo_imgw.R @@ -19,8 +19,6 @@ test_that("meteo_imgw works!", { x <- meteo_imgw("monthly", "synop", year = y, status = TRUE) x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE) x <- meteo_imgw("monthly", "synop", year = y, col_names = "full") - x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE, col_names = "full") - x <- meteo_imgw("monthly", "synop", year = y, col_names = "polish") x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE, col_names = "polish") testthat::expect_message(x <- suppressWarnings(meteo_imgw_daily(rank = "synop", year = 2001, station = "blabla"))) @@ -35,6 +33,6 @@ test_that("meteo_imgw monthly works!", { return(invisible(NULL)) } else { x = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "BIAŁYSTOK") - expect_equal(nrow(x), 24) + testthat::expect_equal(nrow(x), 24) } }) \ No newline at end of file From 56c141160b377681a4c17879365acbaa9675a3c4 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Feb 2026 14:39:37 +0100 Subject: [PATCH 03/13] fix: unit-test --- tests/testthat/test-meteo_imgw.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-meteo_imgw.R b/tests/testthat/test-meteo_imgw.R index c5473ff..7343029 100644 --- a/tests/testthat/test-meteo_imgw.R +++ b/tests/testthat/test-meteo_imgw.R @@ -32,7 +32,7 @@ test_that("meteo_imgw monthly works!", { message("No internet connection! \n") return(invisible(NULL)) } else { - x = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "BIAŁYSTOK") + x = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "LESZNO") testthat::expect_equal(nrow(x), 24) } }) \ No newline at end of file From ebcc72641938a1ab3ad69d218c809ae07ba8481a Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Feb 2026 14:52:59 +0100 Subject: [PATCH 04/13] fix: unit test --- R/meteo_imgw_monthly.R | 14 +++++++------- tests/testthat/test-meteo_imgw.R | 15 ++------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/R/meteo_imgw_monthly.R b/R/meteo_imgw_monthly.R index 7a4a261..8d82fc4 100644 --- a/R/meteo_imgw_monthly.R +++ b/R/meteo_imgw_monthly.R @@ -39,13 +39,13 @@ #' } #' meteo_imgw_monthly = function(rank = "synop", - year, - status = FALSE, - coords = FALSE, - station = NULL, - col_names = "short", - allow_failure = TRUE, - ...) { + year, + status = FALSE, + coords = FALSE, + station = NULL, + col_names = "short", + allow_failure = TRUE, + ...) { if (allow_failure) { tryCatch( meteo_imgw_monthly_bp( diff --git a/tests/testthat/test-meteo_imgw.R b/tests/testthat/test-meteo_imgw.R index 7343029..ba1940a 100644 --- a/tests/testthat/test-meteo_imgw.R +++ b/tests/testthat/test-meteo_imgw.R @@ -20,19 +20,8 @@ test_that("meteo_imgw works!", { x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE) x <- meteo_imgw("monthly", "synop", year = y, col_names = "full") x <- meteo_imgw("monthly", "synop", year = y, coords = TRUE, col_names = "polish") - testthat::expect_message(x <- suppressWarnings(meteo_imgw_daily(rank = "synop", year = 2001, station = "blabla"))) + leszno = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "LESZNO") + testthat::expect_equal(nrow(leszno), 24) } }) - - -test_that("meteo_imgw monthly works!", { - - if (!curl::has_internet()) { - message("No internet connection! \n") - return(invisible(NULL)) - } else { - x = meteo_imgw(interval = "monthly", rank = "synop", year = 2020:2021, station = "LESZNO") - testthat::expect_equal(nrow(x), 24) - } -}) \ No newline at end of file From 6c29b3f32e096cbc23b4ea2db5990057ed3682da Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 11:28:42 +0100 Subject: [PATCH 05/13] fixes for meteo_imgw_monthly --- DESCRIPTION | 2 +- NEWS.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 136116b..e0574ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: climate Title: Interface to Download Meteorological (and Hydrological) Datasets -Version: 1.3.0.9001 +Version: 1.2.9.9001 Authors@R: c(person(given = "Bartosz", family = "Czernecki", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index a917a9b..ea5acb4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# climate 1.3.0 +# climate 1.2.9.9001 * fixes for bugs in `meteo_imgw_monthly()` From af52f39c1fcba82bc6f5219f9ff2a4c169c14c7b Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 13:10:37 +0100 Subject: [PATCH 06/13] fix: hydro imgw --- R/clean_metadata_hydro.R | 24 +++++++++--------------- R/hydro_imgw_daily.R | 13 ++++++++++--- R/hydro_metadata_imgw.R | 6 +++--- R/meteo_imgw.R | 7 ++++--- R/meteo_imgw_monthly.R | 4 ++-- man/meteo_imgw.Rd | 7 ++++--- man/meteo_imgw_monthly.Rd | 4 ++-- 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/R/clean_metadata_hydro.R b/R/clean_metadata_hydro.R index a5dc1d6..3dd9a22 100644 --- a/R/clean_metadata_hydro.R +++ b/R/clean_metadata_hydro.R @@ -8,21 +8,15 @@ #' @noRd clean_metadata_hydro = function(address, interval) { - temp = tempfile() + temp = tempfile() test_url(link = address, output = temp) - a = read.csv(temp, header = FALSE, stringsAsFactors = FALSE, - fileEncoding = "CP1250", skip = 1, sep = "\t")$V1 - a = gsub(a, pattern = "\\?", replacement = "") - a = gsub(x = a, pattern = "'", replacement = "") - a = trimws(gsub(x = a, pattern = "\\^", replacement = "")) - a = gsub(a, pattern = "\\s+", replacement = " ") - - if (interval == "monthly") { - b = list(data.frame(parameters = a[1:10])) - } - if (interval == "daily") { - b = data.frame(parameters = a[1:10]) - } - return(b) + a = read.csv(temp, header = FALSE, stringsAsFactors = FALSE)$V1 + + inds = grepl("^[A-Z]{2}.{5}", a) + + code = trimws(substr(a, 1, 7))[inds] + name = trimws(substr(a, 10, nchar(a)))[inds] + a = data.frame(parameters = code, label = name) + return(a) } diff --git a/R/hydro_imgw_daily.R b/R/hydro_imgw_daily.R index 336cdbe..13a3906 100644 --- a/R/hydro_imgw_daily.R +++ b/R/hydro_imgw_daily.R @@ -135,6 +135,12 @@ hydro_imgw_daily_bp = function(year, } #end of loop for (usually monthly) zip files in a given year + # rename colnames starting with ^ZJ to be changed to ^CO: + # to adjust colnames in zjaw_data to match those in codz_data before merging: + colnames(zjaw_data)[grepl(x = colnames(zjaw_data), "^ZJ")] = + gsub(x = colnames(zjaw_data)[grepl(x = colnames(zjaw_data), "^ZJ")], + pattern = "^ZJ", replacement = "CO") + all_data[[length(all_data) + 1]] = merge(data.table(codz_data), data.table(zjaw_data), by = intersect(colnames(codz_data), colnames(zjaw_data)), @@ -143,7 +149,7 @@ hydro_imgw_daily_bp = function(year, # station selection and names cleaning: if (!is.null(station)) { if (is.character(station)) { - inds = unique(as.numeric(unlist(sapply(station, function(x) grep(pattern = x, x = trimws(all_data[[length(all_data)]]$`Nazwa stacji`)))))) + inds = unique(as.numeric(unlist(sapply(station, function(x) grep(pattern = x, x = trimws(all_data[[length(all_data)]]$PSNZWP)))))) if (any(is.na(inds)) || length(inds) == 0) { env$logs = c( env$logs, @@ -163,6 +169,7 @@ hydro_imgw_daily_bp = function(year, all_data[all_data == 99.9] = NA all_data[all_data == 999] = NA + # TODO: if (coords) { all_data = merge(climate::imgw_hydro_stations, all_data, by.x = "id", @@ -173,12 +180,12 @@ hydro_imgw_daily_bp = function(year, #station selection if (!is.null(station)) { if (is.character(station)) { - all_data = all_data[substr(all_data$`Nazwa stacji`, 1, nchar(station)) == station, ] + all_data = all_data[substr(all_data$PSNZWP, 1, nchar(station)) == station, ] if (nrow(all_data) == 0) { stop("Selected station(s) is not available in the database.", call. = FALSE) } } else if (is.numeric(station)) { - all_data = all_data[all_data$`Kod stacji` %in% station, ] + all_data = all_data[all_data$PSKDSZS %in% station, ] if (nrow(all_data) == 0) { stop("Selected station(s) is not available in the database.", call. = FALSE) } diff --git a/R/hydro_metadata_imgw.R b/R/hydro_metadata_imgw.R index fcd5419..2cff223 100644 --- a/R/hydro_metadata_imgw.R +++ b/R/hydro_metadata_imgw.R @@ -29,13 +29,13 @@ hydro_metadata_imgw_bp = function(interval) { if (interval == "daily") { # dobowe - address_meta1 = paste0(base_url, "dobowe/codz_info.txt") - address_meta2 = paste0(base_url, "dobowe/zjaw_info.txt") + address_meta1 = paste0(base_url, "dobowe/CODZ_publiczne_format.txt") + address_meta2 = paste0(base_url, "dobowe/ZJAW_publiczne_format.txt") meta = list(clean_metadata_hydro(address_meta1, interval), clean_metadata_hydro(address_meta2, interval)) } else if (interval == "monthly") { #miesieczne - address_meta = paste0(base_url, "miesieczne/mies_info.txt") + address_meta = paste0(base_url, "miesieczne/MIES_publiczne_format.txt") meta = clean_metadata_hydro(address_meta, interval) } else { stop("Wrong `interval` value. It should be either 'daily' or 'monthly'.") diff --git a/R/meteo_imgw.R b/R/meteo_imgw.R index 4e66261..b8cc50d 100644 --- a/R/meteo_imgw.R +++ b/R/meteo_imgw.R @@ -12,9 +12,10 @@ #' @param col_names three types of column names possible: "short" - default, values with shorten names, #' "full" - full English description, "polish" - original names in the dataset #' @param ... other parameters that may be passed to the 'shortening' function that shortens column names -#' @param station vector of hydrological stations danepubliczne.imgw.pl -#' can be name of station CAPITAL LETTERS(character). -#' It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric) +#' @param station name of meteorological station(s). +#' It accepts names (characters in CAPITAL LETTERS). Stations' IDs (numeric) are no longer supported. +#' Please note that station names may change over time and thus sometimes 2 names +#' are required in some cases, e.g. `c("POZNAŃ", "POZNAŃ-ŁAWICA")`. #' @export #' @return A data.frame with columns describing the meteorological parameters #' (e.g. temperature, wind speed, precipitation) where each row represent a measurement, diff --git a/R/meteo_imgw_monthly.R b/R/meteo_imgw_monthly.R index 8d82fc4..8438620 100644 --- a/R/meteo_imgw_monthly.R +++ b/R/meteo_imgw_monthly.R @@ -8,8 +8,8 @@ #' @param status leave the columns with measurement and observation statuses #' (default status = FALSE - i.e. the status columns are deleted) #' @param coords add coordinates of the station (logical value TRUE or FALSE) -#' @param station name or ID of meteorological station(s). -#' It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric). +#' @param station name of meteorological station(s). +#' It accepts names (characters in CAPITAL LETTERS). Stations' IDs (numeric) are no longer supported. #' Please note that station names may change over time and thus sometimes 2 names #' are required in some cases, e.g. `c("POZNAŃ", "POZNAŃ-ŁAWICA")`. #' @param col_names three types of column names possible: "short" - default, diff --git a/man/meteo_imgw.Rd b/man/meteo_imgw.Rd index 4ed5913..5e6bc86 100644 --- a/man/meteo_imgw.Rd +++ b/man/meteo_imgw.Rd @@ -27,9 +27,10 @@ meteo_imgw( \item{coords}{add coordinates of the station (logical value TRUE or FALSE)} -\item{station}{vector of hydrological stations danepubliczne.imgw.pl -can be name of station CAPITAL LETTERS(character). -It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric)} +\item{station}{name of meteorological station(s). +It accepts names (characters in CAPITAL LETTERS). Stations' IDs (numeric) are no longer supported. +Please note that station names may change over time and thus sometimes 2 names +are required in some cases, e.g. \code{c("POZNAŃ", "POZNAŃ-ŁAWICA")}.} \item{col_names}{three types of column names possible: "short" - default, values with shorten names, "full" - full English description, "polish" - original names in the dataset} diff --git a/man/meteo_imgw_monthly.Rd b/man/meteo_imgw_monthly.Rd index 3ea4887..3d3f222 100644 --- a/man/meteo_imgw_monthly.Rd +++ b/man/meteo_imgw_monthly.Rd @@ -25,8 +25,8 @@ meteo_imgw_monthly( \item{coords}{add coordinates of the station (logical value TRUE or FALSE)} -\item{station}{name or ID of meteorological station(s). -It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric). +\item{station}{name of meteorological station(s). +It accepts names (characters in CAPITAL LETTERS). Stations' IDs (numeric) are no longer supported. Please note that station names may change over time and thus sometimes 2 names are required in some cases, e.g. \code{c("POZNAŃ", "POZNAŃ-ŁAWICA")}.} From f23b68a8689ad7ee95844ab2b4597f54118578bd Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 22:38:04 +0100 Subject: [PATCH 07/13] fix imgw hydro daily --- NEWS.md | 4 +++- R/hydro_imgw_daily.R | 45 +++++++++-------------------------------- man/hydro_imgw_daily.Rd | 6 ------ 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/NEWS.md b/NEWS.md index ea5acb4..375405f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # climate 1.2.9.9001 -* fixes for bugs in `meteo_imgw_monthly()` +* adapting code to most recent changes in the IMGW-PIB repository: + * fixes for `meteo_imgw_monthly()` + * changes in metadata and downloading functions for hydrological datasets # climate 1.2.9 diff --git a/R/hydro_imgw_daily.R b/R/hydro_imgw_daily.R index 13a3906..09f2a29 100644 --- a/R/hydro_imgw_daily.R +++ b/R/hydro_imgw_daily.R @@ -129,24 +129,17 @@ hydro_imgw_daily_bp = function(year, unzip(zipfile = temp, exdir = temp2) file2 = paste(temp2, dir(temp2), sep = "/")[1] data2 = imgw_read(translit, file2) - colnames(data2) = meta[[2]][, 1] + colnames(data2) = gsub(x = meta[[2]][, 1], "^ZJ", "CO") # rename colnames starting with ^ZJ to be changed to ^CO: zjaw_data = rbind(zjaw_data, data2) } } #end of loop for (usually monthly) zip files in a given year - # rename colnames starting with ^ZJ to be changed to ^CO: - # to adjust colnames in zjaw_data to match those in codz_data before merging: - colnames(zjaw_data)[grepl(x = colnames(zjaw_data), "^ZJ")] = - gsub(x = colnames(zjaw_data)[grepl(x = colnames(zjaw_data), "^ZJ")], - pattern = "^ZJ", replacement = "CO") - all_data[[length(all_data) + 1]] = merge(data.table(codz_data), data.table(zjaw_data), by = intersect(colnames(codz_data), colnames(zjaw_data)), all.x = TRUE) - # station selection and names cleaning: if (!is.null(station)) { if (is.character(station)) { inds = unique(as.numeric(unlist(sapply(station, function(x) grep(pattern = x, x = trimws(all_data[[length(all_data)]]$PSNZWP)))))) @@ -169,43 +162,25 @@ hydro_imgw_daily_bp = function(year, all_data[all_data == 99.9] = NA all_data[all_data == 999] = NA - # TODO: if (coords) { all_data = merge(climate::imgw_hydro_stations, all_data, by.x = "id", - by.y = "Kod stacji", + by.y = "PSKDSZS", all.y = TRUE) } - #station selection - if (!is.null(station)) { - if (is.character(station)) { - all_data = all_data[substr(all_data$PSNZWP, 1, nchar(station)) == station, ] - if (nrow(all_data) == 0) { - stop("Selected station(s) is not available in the database.", call. = FALSE) - } - } else if (is.numeric(station)) { - all_data = all_data[all_data$PSKDSZS %in% station, ] - if (nrow(all_data) == 0) { - stop("Selected station(s) is not available in the database.", call. = FALSE) - } - } else { - stop("Selected station(s) are not in the proper format.", call. = FALSE) - } - } - all_data = as.data.frame(all_data) - all_data = all_data[do.call(order, all_data[grep(x = colnames(all_data), "Nazwa stacji|Rok hydro|w roku hydro|Dzie")]), ] + all_data = all_data[do.call(order, all_data[grep(x = colnames(all_data), "PSNZWP|COROKH|COMSCH|CODZIEN")]), ] # fix dates and add as seperate column: - yy_ind = grep(x = colnames(all_data), "Rok hydrologiczny") - mm_ind = grep(x = colnames(all_data), "kalendarzowy") - dd_ind = grep(x = colnames(all_data), "Dzie") + yy_ind = grep(x = colnames(all_data), "COROKH") + mm_ind = grep(x = colnames(all_data), "COMSCK") + dd_ind = grep(x = colnames(all_data), "CODZIEN") data_df = all_data[, c(yy_ind, mm_ind, dd_ind)] data_df$yy = ifelse(data_df[, 2] >= 11, data_df[, 1] - 1, data_df[, 1]) all_data$Data = as.Date(ISOdate(year = data_df$yy, month = data_df[, 2], day = data_df[, 3])) - all_data = all_data[, c(1:3, ncol(all_data), 4:(ncol(all_data) - 1)), ] - - all_data = hydro_shortening_imgw(all_data, col_names = col_names, ...) - + #all_data = all_data[, c(1:3, ncol(all_data), 4:(ncol(all_data) - 1)), ] + #all_data = hydro_shortening_imgw(all_data, col_names = col_names, ...) + all_data = unique(all_data) + rownames(all_data) = 1:nrow(all_data) return(all_data) } \ No newline at end of file diff --git a/man/hydro_imgw_daily.Rd b/man/hydro_imgw_daily.Rd index 0de9f59..c3e9f4b 100644 --- a/man/hydro_imgw_daily.Rd +++ b/man/hydro_imgw_daily.Rd @@ -8,7 +8,6 @@ hydro_imgw_daily( year, coords = FALSE, station = NULL, - col_names = "short", allow_failure = TRUE, ... ) @@ -21,11 +20,6 @@ hydro_imgw_daily( \item{station}{name or ID of hydrological station(s). It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric)} -\item{col_names}{three types of column names possible: -"short" - default, values with shorten names, -"full" - full English description, -"polish" - original names in the dataset} - \item{allow_failure}{logical - whether to proceed or stop on failure. By default set to TRUE (i.e. don't stop on error). For debugging purposes change to FALSE} \item{...}{other parameters that may be passed to the 'shortening' function that shortens column names} From 7f27aa126484378482dfa6b8ab7c0a08294799d4 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 22:55:07 +0100 Subject: [PATCH 08/13] fix: hydro --- R/hydro_imgw_monthly.R | 20 ++++++++++---------- tests/testthat/test-hydro_imgw.R | 10 ++-------- tests/testthat/test-hydro_metadata_imgw.R | 10 ++++++---- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/R/hydro_imgw_monthly.R b/R/hydro_imgw_monthly.R index 92c1ba7..8f86377 100644 --- a/R/hydro_imgw_monthly.R +++ b/R/hydro_imgw_monthly.R @@ -25,7 +25,7 @@ hydro_imgw_monthly = function(year, coords = FALSE, station = NULL, - col_names= "short", + col_names = "short", allow_failure = TRUE, ...) { @@ -86,7 +86,7 @@ hydro_imgw_monthly_bp = function(year, unzip(zipfile = temp, exdir = temp2) file1 = paste(temp2, dir(temp2), sep = "/")[1] data1 = imgw_read(translit, file1) - colnames(data1) = meta[[1]][, 1] + colnames(data1) = meta[, 1] all_data[[i]] = data1 } all_data = do.call(rbind, all_data) @@ -94,20 +94,20 @@ hydro_imgw_monthly_bp = function(year, all_data[all_data == 9999] = NA all_data[all_data == 99999.999] = NA all_data[all_data == 99.9] = NA - colnames(all_data) = meta[[1]][, 1] + colnames(all_data) = meta[, 1] # coords if (coords) { - all_data = merge(climate::imgw_hydro_stations, all_data, by.x = "id", by.y = "Kod stacji", all.y = TRUE) + all_data = merge(climate::imgw_hydro_stations, all_data, by.x = "id", by.y = "PSKDSZS", all.y = TRUE) } #station selection if (!is.null(station)) { if (is.character(station)) { - all_data = all_data[substr(all_data$`Nazwa stacji`, 1, nchar(station)) == station, ] + all_data = all_data[substr(all_data$PSNZWP, 1, nchar(station)) == station, ] if (nrow(all_data) == 0) { stop("Selected station(s) is not available in the database.", call. = FALSE) } } else if (is.numeric(station)) { - all_data = all_data[all_data$`Kod stacji` %in% station, ] + all_data = all_data[all_data$PSKDSZS %in% station, ] if (nrow(all_data) == 0) { stop("Selected station(s) is not available in the database.", call. = FALSE) } @@ -115,17 +115,17 @@ hydro_imgw_monthly_bp = function(year, stop("Selected station(s) are not in the proper format.", call. = FALSE) } } - all_data = all_data[do.call(order, all_data[grep(x = colnames(all_data), "Nazwa stacji|Rok hydrologiczny|w roku hydro")]), ] + all_data = all_data[do.call(order, all_data[grep(x = colnames(all_data), "PSNZWP|MCROKH")]), ] # fix dates and add as seperate column: - yy_ind = grep(x = colnames(all_data), "Rok hydrologiczny") - mm_ind = grep(x = colnames(all_data), "kalendarzowy") + yy_ind = grep(x = colnames(all_data), "MCROKH") + mm_ind = grep(x = colnames(all_data), "MCMSCK") data_df = all_data[, c(yy_ind, mm_ind)] data_df$day = 1 data_df$yy = ifelse(data_df[, 2] >= 11, data_df[, 1] - 1, data_df[, 1]) all_data$Data = as.Date(ISOdate(year = data_df$yy, month = data_df[, 2], day = data_df$day)) all_data = all_data[, c(1:3, ncol(all_data), 4:(ncol(all_data) - 1)), ] - all_data = hydro_shortening_imgw(all_data, col_names = col_names, ...) + #all_data = hydro_shortening_imgw(all_data, col_names = col_names, ...) return(all_data) } diff --git a/tests/testthat/test-hydro_imgw.R b/tests/testthat/test-hydro_imgw.R index 9fa844b..2892d46 100644 --- a/tests/testthat/test-hydro_imgw.R +++ b/tests/testthat/test-hydro_imgw.R @@ -3,12 +3,6 @@ y <- 2017 test_that("hydro_imgw_not_available", { - expect_error(suppressWarnings(hydro_imgw(interval = "daily", year = 1960, coord = TRUE, - station = "not available", allow_failure = FALSE))) - - expect_error(suppressWarnings(hydro_imgw(interval = "daily", year = 1960, coord = TRUE, - station = 999, allow_failure = FALSE))) - expect_error(suppressWarnings(hydro_imgw(interval = "monthly", year = 1960, coord = TRUE, station = "not available", allow_failure = FALSE))) @@ -24,7 +18,7 @@ test_that("hydro_imgw_not_available", { if (is.data.frame(h2022_2023) & nrow(h2022_2023 > 50000)) { testthat::expect_true(is.data.frame(h2022_2023)) testthat::expect_true(nrow(h2022_2023) > 50000) - testthat::expect_true(class(h2022_2023$date) == "Date") + testthat::expect_true(class(h2022_2023$Data) == "Date") } } @@ -36,7 +30,7 @@ test_that("hydro_imgw_not_available", { if (is.data.frame(h2022_2023d) & nrow(h2022_2023d > 50000)) { testthat::expect_true(is.data.frame(h2022_2023d)) testthat::expect_true(nrow(h2022_2023d) > 50000) - testthat::expect_true(class(h2022_2023d$date) == "Date") + testthat::expect_true(class(h2022_2023d$Data) == "Date") } } diff --git a/tests/testthat/test-hydro_metadata_imgw.R b/tests/testthat/test-hydro_metadata_imgw.R index c344052..62982d4 100644 --- a/tests/testthat/test-hydro_metadata_imgw.R +++ b/tests/testthat/test-hydro_metadata_imgw.R @@ -4,9 +4,11 @@ h_d <- suppressWarnings(hydro_metadata_imgw("daily")) h_m <- suppressWarnings(hydro_metadata_imgw("monthly")) test_that("hydro-metadata works!", { - if (is.list(h_d) && is.list(h_m)) { - expect_equal(dim(h_d[[1]]), c(10, 1)) - expect_equal(dim(h_d[[2]]), c(10, 1)) - expect_equal(dim(h_m[[1]]), c(10, 1)) + if (is.list(h_d)) { + expect_equal(dim(h_d[[1]]), c(10, 2)) + expect_equal(dim(h_d[[2]]), c(10, 2)) + } + if (is.data.frame(h_m)) { + expect_equal(dim(h_m), c(10, 2)) } }) From d1dd3b27f1abce1f9c88e67aed96f458018f973d Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 23:08:11 +0100 Subject: [PATCH 09/13] fix .Rd --- man/hydro_imgw_daily.Rd | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/man/hydro_imgw_daily.Rd b/man/hydro_imgw_daily.Rd index c3e9f4b..0de9f59 100644 --- a/man/hydro_imgw_daily.Rd +++ b/man/hydro_imgw_daily.Rd @@ -8,6 +8,7 @@ hydro_imgw_daily( year, coords = FALSE, station = NULL, + col_names = "short", allow_failure = TRUE, ... ) @@ -20,6 +21,11 @@ hydro_imgw_daily( \item{station}{name or ID of hydrological station(s). It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric)} +\item{col_names}{three types of column names possible: +"short" - default, values with shorten names, +"full" - full English description, +"polish" - original names in the dataset} + \item{allow_failure}{logical - whether to proceed or stop on failure. By default set to TRUE (i.e. don't stop on error). For debugging purposes change to FALSE} \item{...}{other parameters that may be passed to the 'shortening' function that shortens column names} From ea68c89d1b9f6b8a58b25dd06333fefd2f0bac3e Mon Sep 17 00:00:00 2001 From: bczernecki Date: Wed, 4 Mar 2026 23:33:36 +0100 Subject: [PATCH 10/13] fix: hydro vignette --- DESCRIPTION | 2 +- NEWS.md | 3 ++- vignettes/articles/pl.Rmd | 25 +++++++++---------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e0574ba..d45741a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: climate Title: Interface to Download Meteorological (and Hydrological) Datasets -Version: 1.2.9.9001 +Version: 1.3.0 Authors@R: c(person(given = "Bartosz", family = "Czernecki", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 375405f..48d6fa2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,9 @@ -# climate 1.2.9.9001 +# climate 1.3.0 * adapting code to most recent changes in the IMGW-PIB repository: * fixes for `meteo_imgw_monthly()` * changes in metadata and downloading functions for hydrological datasets + * ppdated Polish vignette for hydro dataset # climate 1.2.9 diff --git a/vignettes/articles/pl.Rmd b/vignettes/articles/pl.Rmd index 40a1bd4..74763b3 100644 --- a/vignettes/articles/pl.Rmd +++ b/vignettes/articles/pl.Rmd @@ -48,18 +48,11 @@ framework powinna być dostępne po roku 2000 roku - **meteo_imgw()** - Pozwala pobrać dane meteorologiczne interwale godzinowym, dziennym lub miesięcznym o danej randze stacji(SYNOP / CLIMATE / PRECIP) dostępnych w kolekcji dane.imgw.pl. -Jest to wrapper funckji `meteo_monthly()`, `meteo_daily()` oraz `meteo_hourly()`z pakietu **imgw** (poprzedniej wersji paczki). - -- **meteo\_shortening()** - Skracanie nazw kolumn parametrów meteorologicznych - poprawiając czytelność pobranego zestawu danych i usuwanie zduplikowanych nazw kolumn. 2. Dane hydrologiczne: - **hydro_imgw()** - Pobieranie danych hydrologicznych o interwale dziennym, miesięcznym lub rocznym ze stacji dostępnych w repozytorium dane.imgw.pl. -Jest to wrapper funckji `meteo_monthly()`, `meteo_daily()` oraz `meteo_hourly()`z pakietu **imgw** (poprzedniej -wersji paczki). - 3. Dane radiosondażowe : @@ -79,22 +72,22 @@ h = hydro_imgw(interval = "monthly", year = 2001:2010, coords = TRUE) head(h) ``` -Zmienna `idex` reprezentuje id ekstremum, gdzie "1" oznacza minimum, "2" oznacza średnią, a "3" maksimum. ^[Więcej informacji na ten temat można znaleźć w zestawie danych`hydro_abbrev`.] +Zmienna `MCWSKEX` reprezentuje id ekstremum, gdzie "1" oznacza minimum, "2" oznacza średnią, a "3" maksimum. ^[Więcej informacji na ten temat można znaleźć w zestawie danych`hydro_abbrev`.] Analizy hydrologiczne często koncentrują się na jednej grupy zjawisk, np. związanych z przepływami maksymalnymi. W tym celu pozostaną w ramce danych tylko wartości przepływów maksymalnych oraz kolumny zawierające interesujące nas -informacje, tj. identyfikator stacji - `id`, rok hydrologiczny (` hyy`), szerokość geograficzną `X` i długość +informacje, tj. identyfikator stacji - `id`, rok hydrologiczny (`MCROKH`), szerokość geograficzną `X` i długość geograficzną` Y`. Następnie obliczymy średnią maksymalną wartość przepływu na stacjach w każdym roku za pomocą ** dplyr ** `summarise()`, -oraz rozdzielimy dane według roku używając ** tidyr **' `spread ()` aby uzyskać roczne średnie maksymalne przepływy w +oraz rozdzielimy dane według roku używając ** tidyr **' `spread ()` aby uzyskać roczne średnie maksymalne przepływy (`MCPRZP`) w kolejnych kolumnach. ```{r filtering, eval=TRUE, include=TRUE} h2 = h %>% - filter(idex == 3) %>% - select(id, station, X, Y, hyy, Q) %>% - group_by(hyy, id, station, X, Y) %>% - summarise(srednie_roczne_Q = round(mean(Q, na.rm = TRUE),1)) %>% - spread(hyy, srednie_roczne_Q) + filter(MCWSKEX == 3) %>% + select(id, PSNZWP, X, Y, MCROKH, MCPRZP) %>% + group_by(MCROKH, id, PSNZWP, X, Y) %>% + summarise(srednie_roczne_Q = round(mean(MCPRZP, na.rm = TRUE),1)) %>% + spread(MCROKH, srednie_roczne_Q) ``` ```{r filtering2, echo=FALSE} @@ -126,7 +119,7 @@ world = ne_countries(scale = "medium", returnclass = "sf") h3 = h2 %>% filter(!is.na(X)) %>% - st_as_sf(coords = c("X", "Y")) + st_as_sf(coords = c("X", "Y"), crs = 4326) tm_shape(h3) + tm_symbols(size = as.character(c(2001:2010)), From e738c99ffb60d7be25e4e5af3f3c2c97d07ec176 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Mar 2026 00:17:13 +0100 Subject: [PATCH 11/13] climate 1.3 rc --- NAMESPACE | 1 + NEWS.md | 7 ++++++- R/match_imgw_wmoid_inds.R | 2 +- R/meteo_imgw_daily.R | 1 + R/meteo_imgw_hourly.R | 4 ++-- R/meteo_imgw_monthly.R | 3 +-- R/utils.R | 33 +++++++++++++++++++++++++++++++++ man/find_all_station_names.Rd | 25 +++++++++++++++++++++++++ man/meteo_imgw_hourly.Rd | 3 +-- 9 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 man/find_all_station_names.Rd diff --git a/NAMESPACE b/NAMESPACE index c7dc955..399347f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(.onAttach) +export(find_all_station_names) export(hydro_imgw) export(hydro_imgw_daily) export(hydro_imgw_datastore) diff --git a/NEWS.md b/NEWS.md index 48d6fa2..ce48e34 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,12 @@ * adapting code to most recent changes in the IMGW-PIB repository: * fixes for `meteo_imgw_monthly()` * changes in metadata and downloading functions for hydrological datasets - * ppdated Polish vignette for hydro dataset + * updated Polish vignette for hydro dataset + * added `find_all_station_names()` function to find all station names in the IMGW-PIB + repository that match the provided name(s) and return all available datasets for + those stations (e.g.entry "WARSZAWA" will also search for: "WARSZAWA-BIELANY", + "WARSZAWA-OKECIE", "WARSZAWA-OBSERWATORIUM", etc.) + # climate 1.2.9 diff --git a/R/match_imgw_wmoid_inds.R b/R/match_imgw_wmoid_inds.R index ad050b3..ee39e7a 100644 --- a/R/match_imgw_wmoid_inds.R +++ b/R/match_imgw_wmoid_inds.R @@ -1,5 +1,5 @@ #' Match WMO station IDs for IMGW SYNOP -#' @param station vector or station names provided to imgw_meteo_ family of functions +#' @param station vector or station names provided to meteo_imgw_ family of functions #' @keywords internal #' @noRd match_imgw_wmoid_inds = function(station) { diff --git a/R/meteo_imgw_daily.R b/R/meteo_imgw_daily.R index d883fa2..268d249 100644 --- a/R/meteo_imgw_daily.R +++ b/R/meteo_imgw_daily.R @@ -90,6 +90,7 @@ meteo_imgw_daily_bp = function(rank, # match WMO ID of a given station(s) to download selectively for SYNOP stations if (!is.null(station) && rank == "synop") { + station = find_all_station_names(station) ids_to_download = match_imgw_wmoid_inds(station) } else { ids_to_download = NULL diff --git a/R/meteo_imgw_hourly.R b/R/meteo_imgw_hourly.R index f02c8ef..f3f8ec2 100644 --- a/R/meteo_imgw_hourly.R +++ b/R/meteo_imgw_hourly.R @@ -8,8 +8,7 @@ #' @param status leave the columns with measurement and observation statuses #' (default status = FALSE - i.e. the status columns are deleted) #' @param coords add coordinates of the station (logical value TRUE or FALSE) -#' @param station name or ID of meteorological station(s). -#' It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric) +#' @param station name of meteorological station(s) (character vector) #' @param col_names three types of column names possible: "short" - default, #' values with shorten names, "full" - full English description, #' "polish" - original names in the dataset @@ -250,6 +249,7 @@ meteo_imgw_hourly_bp = function(rank, # station selection and names cleaning: if (!is.null(station)) { if (is.character(station)) { + find_all_station_names(station) inds = unique(as.numeric(unlist(sapply(station, function(x) grep(pattern = x, x = trimws(all_data$POST)))))) if (any(is.na(inds)) || length(inds) == 0) { env$logs = c( diff --git a/R/meteo_imgw_monthly.R b/R/meteo_imgw_monthly.R index 8438620..aec6c9c 100644 --- a/R/meteo_imgw_monthly.R +++ b/R/meteo_imgw_monthly.R @@ -248,7 +248,6 @@ meteo_imgw_monthly_bp = function(rank, # adding option to shorten columns and removing duplicates: # TODO: turned off temporarily, consistent with daily implementation # all_data = meteo_shortening_imgw(all_data, col_names = col_names, ...) - rownames(all_data) = NULL - + rownames(all_data) = 1:nrow(all_data) return(all_data) # clipping to selected years only } diff --git a/R/utils.R b/R/utils.R index d227012..fe79e87 100644 --- a/R/utils.R +++ b/R/utils.R @@ -49,3 +49,36 @@ remove_status = function(df) { return(df) } + + + +#' Find all variants of station' names +#' +#' For IMGW-PIB stations different naming were used historically. For example, +#' `POZNAŃ` and ``POZNAŃ-ŁAWICA, thus both names should be used when searching +#'for the station. This function finds all variants of station' names +#' status information and expand the created object +#' +#' @param station_name character vector of station names +#' @export +#' @returns character vector of station names with all variants of station's names +#' @examples { +#' find_all_station_names(c("WARSZAWA", "POZNAŃ")) +#' } + +find_all_station_names = function(station_name) { + + pattern = paste0("(?=.*", toupper(station_name), ")(?=.*-)") + matches = unlist( + sapply(pattern, function(x) { + grep(x, climate::imgw_meteo_stations$station, + perl = TRUE, + ignore.case = TRUE, + value = TRUE) + } + ) + ) + names(matches) = NULL + + return(sort(unique(c(station_name, matches)))) +} diff --git a/man/find_all_station_names.Rd b/man/find_all_station_names.Rd new file mode 100644 index 0000000..d8d3de7 --- /dev/null +++ b/man/find_all_station_names.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{find_all_station_names} +\alias{find_all_station_names} +\title{Find all variants of station' names} +\usage{ +find_all_station_names(station_name) +} +\arguments{ +\item{station_name}{character vector of station names} +} +\value{ +character vector of station names with all variants of station's names +} +\description{ +For IMGW-PIB stations different naming were used historically. For example, +\code{POZNAŃ} and ``POZNAŃ-ŁAWICA, thus both names should be used when searching +for the station. This function finds all variants of station' names +status information and expand the created object +} +\examples{ +{ +find_all_station_names(c("WARSZAWA", "POZNAŃ")) +} +} diff --git a/man/meteo_imgw_hourly.Rd b/man/meteo_imgw_hourly.Rd index c0fcb0d..7f11607 100644 --- a/man/meteo_imgw_hourly.Rd +++ b/man/meteo_imgw_hourly.Rd @@ -25,8 +25,7 @@ meteo_imgw_hourly( \item{coords}{add coordinates of the station (logical value TRUE or FALSE)} -\item{station}{name or ID of meteorological station(s). -It accepts names (characters in CAPITAL LETTERS) or stations' IDs (numeric)} +\item{station}{name of meteorological station(s) (character vector)} \item{col_names}{three types of column names possible: "short" - default, values with shorten names, "full" - full English description, From 2d38f74382452fa2d8426c782f54cc14f6376442 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Mar 2026 00:47:20 +0100 Subject: [PATCH 12/13] ogimet vignette update --- .github/workflows/html5-check.yaml | 3 ++- vignettes/articles/usecase_ogimet.Rmd | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/html5-check.yaml b/.github/workflows/html5-check.yaml index d31bd6e..8c74109 100644 --- a/.github/workflows/html5-check.yaml +++ b/.github/workflows/html5-check.yaml @@ -35,7 +35,8 @@ jobs: run: sudo apt install tidy pandoc - name: Install dependencies - run: R -e 'install.packages(c("knitr", "rmarkdown", "XML", "httr", "data.table", "maps", "dplyr", "tidyr", "xml2", "testthat", "archive"))' + run: R -e 'install.packages(c("knitr", "rmarkdown", "XML", "httr", "maps", "dplyr", "tidyr", "xml2", "testthat", "archive"))' + run: R -e 'install.packages("data.table", type = "source")' - uses: r-lib/actions/check-r-package@v2 with: diff --git a/vignettes/articles/usecase_ogimet.Rmd b/vignettes/articles/usecase_ogimet.Rmd index 880fb3a..0eb99d4 100644 --- a/vignettes/articles/usecase_ogimet.Rmd +++ b/vignettes/articles/usecase_ogimet.Rmd @@ -33,7 +33,7 @@ df$date <- df$Date df <- left_join(df, wdir) df$ws <- df$ffkmh / 3.6 # conversion to m/s from km/h -df$gust <- df$Gustkmh / 3.6 # conversion to m/s from km/h +df$gust <- as.numeric(df$Gustmax) / 3.6 # conversion to m/s from km/h windRose(mydata = df, ws = "ws", wd = "dir", type = "season", paddle = FALSE, main = "Svalbard Lufthavn (2018)", ws.int = 3, dig.lab = 3, layout = c(4, 1)) From dfb043f472664c1d8faff82e585f33714e2156d7 Mon Sep 17 00:00:00 2001 From: bczernecki Date: Thu, 5 Mar 2026 00:49:08 +0100 Subject: [PATCH 13/13] fix CI/CD --- .github/workflows/html5-check.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/html5-check.yaml b/.github/workflows/html5-check.yaml index 8c74109..0ba7814 100644 --- a/.github/workflows/html5-check.yaml +++ b/.github/workflows/html5-check.yaml @@ -34,9 +34,14 @@ jobs: - name: Install tidy and pandoc run: sudo apt install tidy pandoc + - name: Remove cached R libraries + run: rm -rf /home/runner/work/_temp/Library/data.table + - name: Install dependencies run: R -e 'install.packages(c("knitr", "rmarkdown", "XML", "httr", "maps", "dplyr", "tidyr", "xml2", "testthat", "archive"))' - run: R -e 'install.packages("data.table", type = "source")' + + - name: Install data.table from source + run: Rscript -e 'install.packages("data.table", type = "source")' - uses: r-lib/actions/check-r-package@v2 with: