diff --git a/R/open_data_heatmodel.R b/R/open_prediction_grid.R similarity index 57% rename from R/open_data_heatmodel.R rename to R/open_prediction_grid.R index 2edec58..2ffd10a 100755 --- a/R/open_data_heatmodel.R +++ b/R/open_prediction_grid.R @@ -1,29 +1,23 @@ #' Open prediction grid for a given period #' #' @param period A vector of "Date" objects +#' @param data_path a character to data folder #' @returns a data.table of prediction points #' (each row corresponds to a lat, lon, date) -open_pred_period <- function(period) { +open_pred_period <- function(period, data_path) { if (class(period) != "Date") { stop("period is not a Date") } period <- as.character(period) for (d in period) { - file <- paste0( - "../input/prediction-grid/", - "prediction_grid_points_urb_rur_space_time_covariates_", - d, - ".csv" - ) + file <- paste0(data_path, "pred_grid_", d, ".tif") if (!file.exists(file)) { stop(paste0("date ", d, " is not in files")) } } list_pred <- list() for (d in period) { - pred_d <- fread(file) - list_pred <- append(list_pred, list(pred_d)) + list_pred[[d]] <- terra::rast(file) } - pred_p <- rbindlist(list_pred, fill = TRUE) - return(pred_p = pred_p) + return(list_pred = list_pred) } diff --git a/tests/testthat/test-open_data_heatmodel.R b/tests/testthat/test-open_data_heatmodel.R deleted file mode 100755 index c54f525..0000000 --- a/tests/testthat/test-open_data_heatmodel.R +++ /dev/null @@ -1,23 +0,0 @@ -test_that("Check open_pred_period works", { - skip() - # -- 1st example: period is not a Date object - p <- c("2022-06-01", "2022-06-03") - expect_error(open_pred_period(p), "period is not a Date") - - # -- 2nd example: date is not in files - p <- c(as.Date("2022-06-01"), as.Date("2100-06-03")) - expect_error(open_pred_period(p), "date 2100-06-03 is not in files") - - # -- 3rd example: return a data.table with all covariates - p <- c(as.Date("2022-08-01")) - output <- open_pred_period(p) - expect_equal(output, c("data.table", "data.frame")) - expect_contains( - colnames(output), - c( - "lon", "lat", "date", "geo", - "tcc", "build.fp", "dem", "build.h", - "TNwmo", "TN7am", "TN12am", "TXwmo", "TX7am", "TX12am" - ) - ) -}) diff --git a/tests/testthat/test-open_prediction_grid.R b/tests/testthat/test-open_prediction_grid.R new file mode 100755 index 0000000..f114fe4 --- /dev/null +++ b/tests/testthat/test-open_prediction_grid.R @@ -0,0 +1,20 @@ +test_that("Check open_pred_period works", { + data_path <- "../testdata/" + + # -- 1st example: period is not a Date object + p <- c("2022-06-01", "2022-06-03") + expect_error(open_pred_period(period = p, data_path = data_path), + "period is not a Date") + + # -- 2nd example: date is not in files + p <- c(as.Date("2022-08-01"), as.Date("2100-06-03")) + expect_error(open_pred_period(period = p, data_path = data_path), + "date 2100-06-03 is not in files") + + # -- 3rd example: should work + p <- c(as.Date("2022-08-01"), as.Date("2022-08-02")) + expect_no_error(open_pred_period(period = p, data_path = data_path)) + output <- open_pred_period(period = p, data_path = data_path) + expect_equal(class(output), "list") + expect_equal(names(output), as.character(p)) +}) diff --git a/vignettes/prediction_grid.Rmd b/vignettes/prediction_grid.Rmd new file mode 100755 index 0000000..6ae406b --- /dev/null +++ b/vignettes/prediction_grid.Rmd @@ -0,0 +1,24 @@ +--- +title: "Prediction grid creation" +output: html_document +date: "2023-12-18" +--- + +# Create and store prediction grid + +```{r} +getwd() +cfl <- list_covar_testdata(covar_folder = "../tests/testdata/") +create_pred_rds(borders_path = "../tests/testdata/rtp_counties.shp", + covar_files_list = cfl, + output_path = "../tests/testdata/") +``` + +# Open prediction grid + +```{r} +data_path <- "../tests/testdata/" +period <- seq(as.Date("2022-08-01"), as.Date("2022-08-02"), by = "1 day") +grid <- open_pred_period(period = period, data_path = data_path) +``` +