Skip to content

Commit

Permalink
test format_ghcnh and rename ghcnh testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
Eva Marques committed Jun 7, 2024
1 parent 071b8cd commit b1fb889
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
28 changes: 22 additions & 6 deletions R/format_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,25 @@ format_wu <- function(raw,

#' Format observations from raw data downloaded on NOAA website
#' See function load_ghcnh_station.
#' @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 "Year", "Month", "Day", "Hour", "temperature", "Latitude",
#' "Longitude", "temperature_Source_Code"
#' @return sftime from hourly_temp class
#' @author Eva Marques
#' @export
format_ghcnh <- function(raw) {
ghcnh_cols <- c("Year",
"Month",
"Day",
"Hour",
"temperature",
"Latitude",
"Longitude",
"temperature_Source_Code")
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(ghcnh_cols %in% colnames(raw)))
x <- raw
# note: as.POSIXct return a double when called through apply.
# this is why it is called after the apply
Expand Down Expand Up @@ -152,11 +166,7 @@ format_ghcnh <- function(raw) {
lat = "lat",
lon = "lon",
time = "time",
network = "GHCNh") |>
sftime::st_as_sftime(coords = c("lon", "lat"),
time_column_name = "time",
crs = 4326,
remove = FALSE)
network = "GHCNh")
network <- raw[, c("Longitude", "Latitude", "temperature_Source_Code")] |>
dplyr::rename("lon" = "Longitude") |>
dplyr::rename("lat" = "Latitude") |>
Expand All @@ -169,5 +179,11 @@ format_ghcnh <- function(raw) {
x$network <- factor(x$network,
levels = c(343, 345),
labels = c("NCEI/ASOS/AWOS", "NCEI/US CRN"))
x <- x |>
sftime::st_as_sftime(coords = c("lon", "lat"),
time_column_name = "time",
crs = 4326,
remove = FALSE)
cat("format_ghcnh() done\n")
return(x)
}
File renamed without changes.
13 changes: 12 additions & 1 deletion tests/testthat/test-format_observations.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ testthat::test_that("format_wu works well", {
})

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

raw <- download_ghcnh_station(site_id = "USW00013722", year = 2021)
expect_no_error(format_ghcnh(raw))
# test output format
expect_true(inherits(format_ghcnh(raw), "sftime"))
cols <- c("site_id", "time", "lon", "lat", "geometry", "temp", "network")
expect_true(all(colnames(format_ghcnh(raw)) %in% cols))
expect_true(all(cols %in% colnames(format_ghcnh(raw))))
# check error if not the right columns
raw <- "../testdata/ghcnh_formatted_testdata.rds" |>
testthat::test_path() |>
readRDS()
expect_error(format_ghcnh(raw))
})

0 comments on commit b1fb889

Please sign in to comment.