Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for gefs tests #319

Merged
merged 5 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion R/gefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ gefs_GET <- function(var, lat, lon,
raw = FALSE,
...) {

#Sanity Checks
###Sanity Checks
if (missing(var)) stop("Need to specify the variable to get. A list of variables is available from gefs_variables().")

# lats and lons must be sequential and within ranges
lats <- sort(round(lat, 0))
if (!all(lats == seq(lats[1], length.out = length(lats)))) stop("Latitudes must be sequential.")
if (any(lats < -90 | lats > 90)) stop("Latitudes must be in c(-90,90).")

lons <- sort(round(lon, 0))
if (!all(lons == seq(lons[1], length.out = length(lons)))) stop("Longitudes must be sequential.")
if (any(lons < -180 | lons > 360)) stop("Longitudes must be in c(-180,180) or c(0,360).")


#get a connection
con <- gefs_CONNECT(date, forecast_time)

Expand Down Expand Up @@ -170,6 +180,8 @@ gefs_GET <- function(var, lat, lon,
names(dim_vals) <- names(dim_idxs)
d = cbind(as.data.frame(as.vector(d_raw)), expand.grid(dim_vals))
names(d)[[1]] <- var
} else {
d <- d_raw
}

fname <- strsplit(con$filename, "_")[[1]]
Expand Down
8 changes: 4 additions & 4 deletions man/gefs.Rd

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

79 changes: 49 additions & 30 deletions tests/testthat/test-gefs.R
Original file line number Diff line number Diff line change
@@ -1,61 +1,80 @@
context("gefs")

#set a location
lat = 46.28125
lon = -116.2188
lons <- c(-1.1, 0, 0.8, 2)
lats <- c(50.1, 51, 51.9, 53, 54)

#variable
var = "Temperature_height_above_ground_ens"
temp = "Temperature_height_above_ground_ens"

# Get raw and processed data
d_raw <- gefs(var = temp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like any code in tests that make HTTP requests to be skipped on cran because any http request failure could cause cran maintainers to throw a tantrum

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do variable assignment up here but not HTTP requests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I knew there was a reason I had it differently before. Ultimately I should move those to mock api calls but for now I've moved them inside of the test and put all tests related to the returned data object inside of one test_that() call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

ens_idx = 1:2,
time_idx = 1:2,
forecast_time = "0000",
lon = lons, lat = lats, raw = TRUE)

d <- gefs(var = temp,
ens_idx = 1:2,
time_idx = 1:2,
forecast_time = "0000",
lon = lons, lat = lats)


test_that("gefs errors", {
skip_on_cran()
skip_on_appveyor()
skip_on_travis()

expect_error(gefs(lat=lat, lon=lon), "Need to specify the variable to get. A list of variables is available from gefs_variables().")
expect_error(gefs(var = temp, lat = c(-43, -41), lon = lons, ens_idx = 1, time_idx = 1), "Latitudes must be sequential.", fixed = TRUE)
expect_error(gefs(var = temp, lat = lats, lon = c(213, 211), ens_idx = 1, time_idx = 1), "Longitudes must be sequential.", fixed = TRUE)
expect_error(gefs(var = temp, lat = -91, lon = lons, ens_idx = 1, time_idx = 1), "Latitudes must be in c(-90,90).", fixed = TRUE)
expect_error(gefs(var = temp, lat = 91, lon = lons, ens_idx = 1, time_idx = 1), "Latitudes must be in c(-90,90).", fixed = TRUE)
expect_error(gefs(var = temp, lat = lats, lon = 361, ens_idx = 1, time_idx = 1), "Longitudes must be in c(-180,180) or c(0,360).", fixed = TRUE)
expect_error(gefs(var = temp, lat = lats, lon = -181, ens_idx = 1, time_idx = 1), "Longitudes must be in c(-180,180) or c(0,360).", fixed = TRUE)
})

test_that("gefs latitude and longitude selection returns correct values", {
skip_on_cran()
skip_on_travis()
skip_on_appveyor()

a <- gefs("u-component_of_wind_height_above_ground_ens",
ens_idx = 1,
time_idx = 1,
forecast_time = "0000",
lon = c(-1:2), lat = c(50:54))

expect_true(all(unique(a$data$lon) == c(0,1,2,359)))
expect_true(all(unique(a$data$lat) == c(54,54,54,54)))
test_that("gefs returns a correct object", {
expect_type(d, "list")
expect_equal(names(d),
c("forecast_date", "forecast_time", "dimensions", "data"))
expect_equal(d$forecast_date, format(Sys.time(), "%Y%m%d"))
expect_equal(d$forecast_time, "0000")
expect_s3_class(d$data, "data.frame")
})

test_that("gefs time and ensemble selection returns correct indices.", {
skip_on_cran()
skip_on_travis()
skip_on_appveyor()

ens_idx = 2:4
time_idx = 5:10
d = gefs(var, lat, lon, ens_idx = ens_idx, time_idx = time_idx)

time_var <- names(d$data)[6]
expect_equal(dim(d$data), c(length(ens_idx) * length(time_idx), 6))
expect_equal(unique(d$data$ens), ens_idx - 1)
expect_true(all(sort(unique(d$data$lon)) == sort(round(lons %% 360, 0))))
expect_true(all(sort(unique(d$data$lat)) == sort(round(lats, 0))))
})

test_that("gefs metadata", {
test_that("gefs data arrays are correctly transformed", {
skip_on_cran()
skip_on_travis()
skip_on_appveyor()

today = format(as.Date(Sys.time()) - 2, "%Y%m%d")
forecast_time = "0600"
d = gefs(var, lat, lon, ens=1, date=today, forecast_time=forecast_time)
grid <- expand.grid(lon = round(lons %% 360, 0), lat = round(lats, 0),
ens = 1:2, time = 1:2)

expect_equal(nrow(d$data), nrow(grid))

# NOTE: Remember that GEFS returns latitude in reverse order, i.e. -90:90

# 2nd lon, all lats, 1st ens, 2nd time
expect_true(all(d_raw$data[2,,1,2] == d$data[d$data$lon == lons[[2]] &
d$data$ens == 0 &
d$data$time2 == 1,][[temp]]))

expect_equal(d$forecast_date, today)
expect_equal(d$forecast_time, forecast_time)
expect_equal(d$dimensions[1:4], c("lon", "lat", "height_above_ground", "ens"))
# All lons, last lat, 1st ens, 1st time
expect_true(all(d_raw$data[,length(lats),1,2] == d$data[
d$data$lat == round(sort(lats, decreasing = TRUE)[[length(lats)]]) &
d$data$ens == 0 &
d$data$time2 == 1,][[temp]]))
})

test_that("gefs_variables returns characters.", {
Expand Down