Skip to content

Commit

Permalink
create lookup table for pulling dates from NDVI sources without expan…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
emmamendelsohn committed Oct 23, 2023
1 parent bf59d89 commit 134bb88
Show file tree
Hide file tree
Showing 3 changed files with 1,690 additions and 1,628 deletions.
47 changes: 47 additions & 0 deletions R/create_ndvi_date_lookup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' .. content for \description{} (no empty lines) ..
#'
#' .. content for \details{} ..
#'
#' @title
#' @param sentinel_ndvi_transformed
#' @param sentinel_ndvi_transformed_directory
#' @param modis_ndvi_transformed
#' @param modis_ndvi_transformed_directory
#' @return
#' @author Emma Mendelsohn
#' @export
create_ndvi_date_lookup <- function(sentinel_ndvi_transformed,
sentinel_ndvi_transformed_directory,
modis_ndvi_transformed,
modis_ndvi_transformed_directory) {


sent_dataset <- open_dataset(sentinel_ndvi_transformed_directory)
modi_dataset <- open_dataset(modis_ndvi_transformed_directory)

# create lookup table so we know which rows to query, without doing an expansion on the actual data
sent_dates <- sent_dataset |>
distinct(start_date, end_date) |>
arrange(start_date) |>
collect() |>
mutate(lookup_dates = map2(start_date, end_date, ~seq(.x, .y-1, by = "1 day"))) |>
mutate(satellite = "sentinel")

min_sent_start <- min(sent_dates$start_date) # this is when sentinel starts

modi_dates <- modi_dataset |>
distinct(start_date, end_date) |>
arrange(start_date) |>
collect() |>
mutate(lookup_dates = map2(start_date, end_date, ~seq(.x, .y-1, by = "1 day"))) |>
mutate(satellite = "modis") |>
mutate(lookup_dates = map(lookup_dates, ~na.omit(if_else(.>=min_sent_start, NA, .)))) |>
filter(map_lgl(lookup_dates, ~length(.) > 0))

ndvi_dates <- bind_rows(modi_dates, sent_dates) |>
mutate(lookup_day_of_year = map(lookup_dates, yday)) |>
relocate(satellite)

return(ndvi_dates)

}
15 changes: 14 additions & 1 deletion _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ data_targets <- tar_plan(
tar_target(model_dates, set_model_dates(start_year = 2005, end_year = 2022, n_per_month = 2, lag_intervals, seed = 212)),
tar_target(model_dates_selected, model_dates |> filter(select_date) |> pull(date)),

# weather data
# weather anomalies data
tar_target(weather_historical_means_directory,
create_data_directory(directory_path = "data/weather_historical_means")),

Expand Down Expand Up @@ -334,6 +334,19 @@ data_targets <- tar_plan(
pattern = weather_anomalies,
cue = tar_cue("never")), # only run this if you need to upload new data

# ndvi anomalies data
tar_target(ndvi_date_lookup,
create_ndvi_date_lookup(sentinel_ndvi_transformed,
sentinel_ndvi_transformed_directory,
modis_ndvi_transformed,
modis_ndvi_transformed_directory)),


tar_target(ndvi_historical_means_directory,
create_data_directory(directory_path = "data/ndvi_historical_means")),



)

# Model -----------------------------------------------------------
Expand Down
Loading

0 comments on commit 134bb88

Please sign in to comment.