Skip to content

Commit

Permalink
Add graceful fail to phenocam
Browse files Browse the repository at this point in the history
  • Loading branch information
gmyenni committed Jul 14, 2022
1 parent 3228c92 commit d46d359
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ Suggests:
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.1.2
RoxygenNote: 7.2.0
23 changes: 19 additions & 4 deletions R/phenocam.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@
phenocam <- function(level = "daily", path = get_default_data_path())
{
level <- tolower(level)
pheno <- read.csv(url("http://phenocam.sr.unh.edu/data/archive/portal/ROI/portal_SH_1000_1day.csv"),
skip = 22, header = TRUE, na.strings = c(""), stringsAsFactors = FALSE,

url <- "http://phenocam.sr.unh.edu/data/archive/portal/ROI/portal_SH_1000_1day.csv"

got <- tryCatch(GET(url),
error = function(e) NULL)
if (is.null(got)) {
return(NULL)
}

got <- tryCatch(stop_for_status(got),
error = function(e) NULL)
if (is.null(got)) {
return(NULL)
}

pheno <- read.csv(url,
skip = 22, header = TRUE, na.strings = c(""), stringsAsFactors = FALSE,
colClasses = c("Date", rep("integer", 3), "character",
rep("numeric", 22), rep("character", 5))) %>%
dplyr::arrange(.data$date)

if (level == "monthly") {

##########Summarise by Month -----------------
##########Summarize by Month -----------------

pheno <- pheno %>%
dplyr::mutate(month = lubridate::month(date)) %>%
Expand All @@ -33,7 +48,7 @@ phenocam <- function(level = "daily", path = get_default_data_path())

} else if (level == "newmoon") {

##########Summarise by lunar month -----------------
##########Summarize by lunar month -----------------
moon_dates <- load_datafile("Rodents/moon_dates.csv", na.strings = c(""), path = path)
newmoon_number <- moon_dates$newmoonnumber[-1]
newmoon_start <- as.Date(moon_dates$newmoondate[-nrow(moon_dates)])
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-11-phenocam.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
context("checks phenocam summary output")

portal_data_path <- tempdir()
daily_pheno <- phenocam("daily", path = portal_data_path)
monthly_pheno <- phenocam("monthly", path = portal_data_path)
newmoon_pheno <- phenocam("newmoon", path = portal_data_path)
daily_pheno <- tryCatch(phenocam("daily", path = portal_data_path), error = function(e) NULL)
monthly_pheno <- tryCatch(phenocam("monthly", path = portal_data_path), error = function(e) NULL)
newmoon_pheno <- tryCatch(phenocam("newmoon", path = portal_data_path), error = function(e) NULL)

test_that("Daily option returns 32 columns", {
skip_if(is.null(daily_pheno))
expect_equal(NCOL(daily_pheno), 32)
expect_equal(colnames(daily_pheno),
c("date","year","doy","image_count","midday_filename","midday_r","midday_g","midday_b",
Expand All @@ -16,12 +17,14 @@ test_that("Daily option returns 32 columns", {
})

test_that("Monthly option returns 7 columns", {
skip_if(is.null(monthly_pheno))
expect_equal(NCOL(monthly_pheno), 7)
expect_equal(colnames(monthly_pheno), c("year","month","mean_image_count","midday_gcc","midday_rcc",
"gcc_mean","rcc_mean"))
})

test_that("Newmoon option returns 6 columns", {
skip_if(is.null(newmoon_pheno))
expect_that(dim(newmoon_pheno)[2], equals(6))
expect_equal(colnames(newmoon_pheno), c("newmoonnumber","mean_image_count","midday_gcc","midday_rcc",
"gcc_mean","rcc_mean"))
Expand Down

0 comments on commit d46d359

Please sign in to comment.