Skip to content

Commit

Permalink
ndvi anomaly calculations with historical date ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
emmamendelsohn committed Nov 6, 2023
1 parent 939d64f commit 2688c34
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
34 changes: 21 additions & 13 deletions R/calculate_ndvi_anomalies.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @author Emma Mendelsohn
#' @export
calculate_ndvi_anomalies <- function(ndvi_date_lookup, ndvi_historical_means,
ndvi_anomalies_directory, model_dates,
ndvi_anomalies_directory,
model_dates_selected, lag_intervals,
overwrite = FALSE) {

Expand All @@ -30,25 +30,33 @@ calculate_ndvi_anomalies <- function(ndvi_date_lookup, ndvi_historical_means,
return(file.path(ndvi_anomalies_directory, save_filename))
}

# Get historical means for DOY
doy <- model_dates |> filter(date == date_selected) |> pull(day_of_year)
doy_frmt <- str_pad(doy,width = 3, side = "left", pad = "0")
historical_means <- read_parquet(ndvi_historical_means[str_detect(ndvi_historical_means, doy_frmt)]) |>
select(-day_of_year)

# Get the lagged anomalies for selected dates, mapping over the lag intervals
row_select <- which(model_dates$date == date_selected)

lag_intervals_start <- c(1 , 1+lag_intervals[-length(lag_intervals)])
lag_intervals_end <- lag_intervals
lag_intervals_start <- c(1 , 1+lag_intervals[-length(lag_intervals)]) # 1 to start with previous day
lag_intervals_end <- lag_intervals # 30 days total including end day

anomalies <- map2(lag_intervals_start, lag_intervals_end, function(start, end){

lag_dates <- model_dates |> slice((row_select - start):(row_select - end))
# get lag dates, removing doy 366
lag_dates <- seq(date_selected - end, date_selected - start, by = "day")
lag_doys <- yday(lag_dates)
if(366 %in% lag_doys){
lag_doys <- lag_doys[lag_doys!=366]
lag_doys <- c(head(lag_doys, 1) - 1, lag_doys)
}

# Get historical means for lag period
doy_start <- head(lag_doys, 1)
doy_end <- tail(lag_doys, 1)
doy_start_frmt <- str_pad(doy_start, width = 3, side = "left", pad = "0")
doy_end_frmt <- str_pad(doy_end, width = 3, side = "left", pad = "0")
doy_range <- glue::glue("{doy_start_frmt}_to_{doy_end_frmt}")

historical_means <- read_parquet(ndvi_historical_means[str_detect(ndvi_historical_means, doy_range)])
assertthat::assert_that(nrow(historical_means) > 0)

# get files and weights for the calculations
weights <- ndvi_date_lookup |>
mutate(lag_date = map(lookup_dates, ~. %in% lag_dates$date)) |>
mutate(lag_date = map(lookup_dates, ~. %in% lag_dates)) |>
mutate(weight = unlist(map(lag_date, sum))) |>
filter(weight > 0) |>
select(start_date, filename, weight)
Expand Down
1 change: 0 additions & 1 deletion _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ data_targets <- tar_plan(
tar_target(ndvi_anomalies, calculate_ndvi_anomalies(ndvi_date_lookup,
ndvi_historical_means,
ndvi_anomalies_directory,
model_dates,
model_dates_selected,
lag_intervals,
overwrite = FALSE),
Expand Down
24 changes: 24 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@
"Version": "1.3.2",
"Repository": "CRAN"
},
"crew": {
"Source": "Repository",
"Package": "crew",
"Version": "0.4.0",
"Repository": "CRAN"
},
"crul": {
"Source": "Repository",
"Package": "crul",
Expand Down Expand Up @@ -425,6 +431,12 @@
"Version": "0.2-2",
"Repository": "CRAN"
},
"getip": {
"Source": "Repository",
"Package": "getip",
"Version": "0.1-3",
"Repository": "CRAN"
},
"ggforce": {
"Source": "Repository",
"Package": "ggforce",
Expand Down Expand Up @@ -641,6 +653,12 @@
"Version": "0.12",
"Repository": "CRAN"
},
"mirai": {
"Source": "Repository",
"Package": "mirai",
"Version": "0.9.1",
"Repository": "CRAN"
},
"modelr": {
"Source": "Repository",
"Package": "modelr",
Expand All @@ -653,6 +671,12 @@
"Version": "0.5.0",
"Repository": "CRAN"
},
"nanonext": {
"Source": "Repository",
"Package": "nanonext",
"Version": "0.9.1",
"Repository": "CRAN"
},
"nasapower": {
"Source": "Repository",
"Package": "nasapower",
Expand Down

0 comments on commit 2688c34

Please sign in to comment.