Skip to content

Commit

Permalink
fetchHenry(): fix for #333
Browse files Browse the repository at this point in the history
 - use the correct year when creating dates from julian days
  • Loading branch information
brownag committed Jan 2, 2024
1 parent 3ef739f commit 0bbae8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
3 changes: 2 additions & 1 deletion R/fetchHenry.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ summarizeSoilTemperature <- function(soiltemp.data) {
# daily.mean: mean of non-NA values
.doySummary <- function(i) {
res <- data.frame(
year = i$year[1],
n.total = length(i$sensor_value),
n = length(na.omit(i$sensor_value)),
daily.mean = mean(i$sensor_value, na.rm = TRUE)
Expand All @@ -73,7 +74,7 @@ summarizeSoilTemperature <- function(soiltemp.data) {
d <- dd[, .doySummary(.SD), by = c('sid', 'doy')]

# convert DOY -> month
d$month <- format(as.Date(as.character(d$doy), format = "%j"), "%b")
d$month <- format(as.Date(paste0(d$year, "-", d$doy), format = "%Y-%j"), "%b")
d$season <- month2season(d$month)

# compute unbiased MAST, number of obs, complete records per average no. days in year
Expand Down
39 changes: 14 additions & 25 deletions tests/testthat/test-fetchHenry.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
context("fetchHenry and related")

test_that("month2season() works as expected", {


skip_on_cran()

x <- c('Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May')
res <- month2season(x)

Expand All @@ -28,7 +30,7 @@ test_that("month2season() works as expected", {

test_that("summarizeSoilTemperature() works as expected", {

## this is kind of stupid, consider a new sample dataset
skip_on_cran()

x <- structure(
list(
Expand Down Expand Up @@ -119,7 +121,7 @@ test_that("summarizeSoilTemperature() works as expected", {
row.names = c(NA, 749L)
)

s <- .formatDates(x, gran = 'day', pad.missing.days = TRUE)
s <- .formatDates(x, gran = 'day', pad.missing.days = TRUE, tz = "GMT")
salt <- .formatDates(x, gran = 'day', pad.missing.days = FALSE)

expect_equal(nrow(s), 1096) # filled
Expand All @@ -137,19 +139,21 @@ test_that("summarizeSoilTemperature() works as expected", {

expect_equal(res$complete.yrs, 1L)

expect_equal(res$MAST, 11.65)
expect_equal(res$MAST, 11.65, tolerance = 0.1)

expect_equal(res$Winter, 6.16)
expect_equal(res$Winter, 6.15, tolerance = 0.1)

expect_equal(res$Summer, 16.92)
expect_equal(res$Summer, 16.94, tolerance = 0.1)

expect_equal(as.character(res$STR), 'mesic')

})


test_that(".fill_missing_days() works as expected", {


skip_on_cran()

x <- data.frame(
sid = 1,
date_time = as.POSIXct('2010-02-17 00:00:00'),
Expand All @@ -173,8 +177,9 @@ test_that(".fill_missing_days() works as expected", {


test_that(".formatDates() works as expected", {

## TODO: be careful with timezones

skip_on_cran()

x <- data.frame(
sid = c(1L, 2L),
date_time = c("2010-02-17 10:00:00", "2021-01-01 10:00:00"),
Expand Down Expand Up @@ -205,19 +210,3 @@ test_that(".formatDates() works as expected", {
expect_equal(res$water_day, c(140L, 93L))

})



test_that("fetchHenry() works as expected", {

expect_true(TRUE)

})








0 comments on commit 0bbae8d

Please sign in to comment.