Skip to content

Commit

Permalink
fix: ogimet_hourly logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bczernecki committed Nov 1, 2024
1 parent d063eff commit 5415429
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* removed option to download data for "semiannual and annual" time resolutions due to inconsistencies in the data
* Fix unit tests for ogimet- and IMGW-related datasets
* Resolving date formatting for hydrological data - the Date column represents calendar date
* Corrected logic in downloading hourly OGIMET dataset

# climate 1.2.1

Expand Down
26 changes: 14 additions & 12 deletions R/ogimet_hourly.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#' @param coords add geographical coordinates of the station (logical value TRUE or FALSE)
#' @param station WMO ID of meteorological station(s). Character or numeric vector
#' @param precip_split whether to split precipitation fields into 6/12/24h; default: TRUE
#' @param 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
#' @param 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
#' @importFrom XML readHTMLTable
#'
#' @export
Expand Down Expand Up @@ -51,9 +52,10 @@ ogimet_hourly_bp = function(date = date,

dates = seq.Date(min(as.Date(date)), max(as.Date(date)), by = "1 month") - 1
dates = unique(c(dates, as.Date(max(date))))
diff_dates = diff(dates)

# initalizing empty data frame for storing results:
data_station <-
data_station =
data.frame(
"Date" = character(),
"hour" = character(),
Expand Down Expand Up @@ -92,7 +94,10 @@ ogimet_hourly_bp = function(date = date,
year = format(dates[i], "%Y")
month = format(dates[i], "%m")
day = format(dates[i], "%d")
ndays = day
ndays = as.numeric(diff_dates[i - 1])
ndays = ifelse(ndays == 0, 1, ndays)
ndays = sprintf("%02d", ndays)

linkpl2 = paste("https://www.ogimet.com/cgi-bin/gsynres?ind=",
station_nr,
"&lang=en&decoded=yes&ndays=",
Expand All @@ -105,12 +110,14 @@ ogimet_hourly_bp = function(date = date,
day,
"&hora=23",
sep = "")
if (month == "01") linkpl2 = paste("http://ogimet.com/cgi-bin/gsynres?ind=",
if (month == "01") {
linkpl2 = paste("http://ogimet.com/cgi-bin/gsynres?ind=",
station_nr,
"&lang=en&decoded=yes&ndays=31&ano=",
year,
"&mes=02&day=1&hora=00",
sep = "")
}

temp = tempfile()
test_url(linkpl2, temp)
Expand Down Expand Up @@ -163,23 +170,17 @@ ogimet_hourly_bp = function(date = date,
}# end of looping for stations

if (nrow(data_station) > 0) {

data_station = data_station[!duplicated(data_station), ]

data_station = data_station[!duplicated(data_station), ]
# converting character to proper field representation:

# get rid off "---" standing for missing/blank fields:
data_station[which(data_station == "--" | data_station == "---" | data_station == "----" | data_station == "-----", arr.ind = TRUE)] = NA

# changing time..
data_station$Date = strptime(paste(data_station$Date, data_station$hour), "%m/%d/%Y %H:%M", tz = 'UTC')
data_station$hour = NULL

# other columns to numeric:
columns = c("TC", "TdC", "ffkmh", "Gustkmh", "P0hPa", "PseahPa", "PTnd", "Nt", "Nh",
"HKm", "InsoD1", "Viskm", "Snowcm", "station_ID")
columns = colnames(data_station)[(colnames(data_station) %in% columns)]

suppressWarnings(data_station[, columns] <-
as.data.frame(sapply(data_station[,columns], as.numeric)))

Expand Down Expand Up @@ -216,6 +217,7 @@ ogimet_hourly_bp = function(date = date,

} # end of checking whether object is empty

data_station = unique(data_station)
rownames(data_station) = NULL
return(data_station)

}
3 changes: 2 additions & 1 deletion man/ogimet_hourly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5415429

Please sign in to comment.