Skip to content

Commit

Permalink
test format_observations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Marques committed Jun 7, 2024
1 parent 2e97d77 commit 071b8cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 8 additions & 2 deletions R/format_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ format_pa <- function(raw,
time_column_name = "time",
crs = raw_crs,
remove = FALSE)
cat("format_pa() done\n")
return(x)
}

#' Format observations sent by IBM.
#' @param raw a data.frame with the raw observations
#' @param raw a data.frame, data.table, sf or sftime with the raw observations
#' and columns "obsTimeUtc", "lat", "lon", "tempAvg"
#' @param raw_tz the initial timezone, see PurpleAir API documentation
#' @param raw_temp_unit the initial temperature unit
#' @param raw_crs the initial coordinate reference system
Expand All @@ -87,10 +89,14 @@ format_wu <- function(raw,
raw_tz = "UTC",
raw_temp_unit = "F",
raw_crs = 4326) {
wu_cols <- c("obsTimeUtc", "lat", "lon", "tempAvg")
stopifnot("raw is not a data.frame, data.table, sf or sftime" =
inherits(raw, c("sf", "sftime", "data.table", "data.frame")),
"Some columns missing or mispelled" =
all(wu_cols %in% colnames(raw)))
x <- raw
if (raw_temp_unit != "C") {
x$tempAvg_c <- convert_temp(x$tempAvg, from = raw_temp_unit, to = "C")
x$tempLow_c <- convert_temp(x$tempLow, from = raw_temp_unit, to = "C")
}
x$obsTimeUtc <- as.POSIXct(x$obsTimeUtc, tz = "UTC") |>
lubridate::floor_date(unit = "hours")
Expand Down
24 changes: 23 additions & 1 deletion tests/testthat/test-format_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,29 @@ testthat::test_that("format_pa works well", {
})

testthat::test_that("format_wu works well", {

raw <- "../testdata/wu_raw_simulated_testdata.rds" |>
testthat::test_path() |>
readRDS()
expect_no_error(format_wu(raw))
raw_df <- raw |>
as.data.frame()
expect_no_error(format_wu(raw_df))
raw_sf <- raw |>
sf::st_as_sf(coords = c("lon", "lat"), crs = 4326, remove = FALSE)
expect_no_error(format_wu(raw_sf))
raw_sftime <- raw |>
sftime::st_as_sftime(coords = c("lon", "lat"),
time_column_name = "obsTimeUtc",
crs = 4326,
remove = FALSE)
expect_no_error(format_wu(raw_sftime))
# missing columns
expect_error(format_wu(raw[, c("obsTimeUtc", "lat", "lon")]))
# test output format
expect_true(inherits(format_wu(raw), "sftime"))
cols <- c("site_id", "time", "lon", "lat", "geometry", "temp", "network")
expect_true(all(colnames(format_wu(raw)) %in% cols))
expect_true(all(cols %in% colnames(format_wu(raw))))
})

testthat::test_that("format_ghcnh works well", {
Expand Down

0 comments on commit 071b8cd

Please sign in to comment.