generated from ecohealthalliance/container-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,454 additions
and
1,568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
lag_data <- function(data_files, | ||
lag_intervals, | ||
model_dates_selected, | ||
overwrite = TRUE, | ||
...) { | ||
|
||
# The goal of this is to figure out the average of the data column over the interval | ||
# Find dates at start and end interval back from date | ||
# Group by x, y, start_interval, end_interval, and take the mean don't forget na.rm = T | ||
|
||
data <- arrow::open_dataset(data_files) | ||
|
||
lagged_data <- map_dfr(1:(length(lag_intervals) - 1), function(i) { | ||
|
||
start_date = model_dates_selected - days(lag_intervals[i]) | ||
end_date = model_dates_selected - days(lag_intervals[i+1] - 1) | ||
|
||
data |> filter(date >= end_date, date <= start_date) |> | ||
collect() |> | ||
select(-source) |> | ||
group_by(x, y, date, doy, month, year) |> | ||
summarize(across(everything(), ~mean(.x, na.rm = T))) |> | ||
mutate(lag_interval = lag_intervals[i]) | ||
|
||
select(-doy, -month, -year) |> mutate(date = dplyr::lag(date, lag_interval)) |> rename_with(~ paste(.x, "lag", lag_interval, sep = "_"), contains("ndvi")) |> drop_na(date) | ||
}) |> reduce(left_join, by = c("x", "y", "date")) |> | ||
drop_na() |> | ||
rename_with(~gsub("_0", "", .x), contains("_0")) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
transform_ndvi <- function(modis_ndvi_transformed, | ||
sentinel_ndvi_transformed, | ||
ndvi_transformed_directory, | ||
overwrite = FALSE, | ||
...) { | ||
|
||
# ndvi_transformed_dataset <- arrow::open_dataset(c(sentinel_ndvi_transformed, modis_ndvi_transformed)) | ||
# | ||
# years <- ndvi_transformed_dataset |> select(year) |> distinct() |> arrange(year) |> pull(year, as_vector = T) | ||
# | ||
# ndvi_transformed <- map(years, function(yr) { | ||
# | ||
# ndvi_transformed_dataset <- arrow::open_dataset(c(sentinel_ndvi_transformed, modis_ndvi_transformed)) |> filter(year == yr) | ||
# | ||
# # Set filename | ||
# save_filename <- file.path(ndvi_transformed_directory, glue::glue("ndvi_transformed_{yr}.gz.parquet")) | ||
# message(paste0("Combining ndvi sources for ", yr)) | ||
# | ||
# # Check if file already exists and can be read | ||
# error_safe_read_parquet <- possibly(arrow::open_dataset, NULL) | ||
# | ||
# if(!is.null(error_safe_read_parquet(save_filename)) & !overwrite) { | ||
# message("file already exists and can be loaded, skipping") | ||
# return(save_filename) | ||
# } | ||
# | ||
# arrow::write_parquet(ndvi_transformed_dataset |> filter(year == yr), save_filename) | ||
# | ||
# rm(ndvi_transformed_dataset) | ||
# | ||
# return(save_filename) | ||
# }) | ||
# | ||
# ndvi_transformed | ||
|
||
} |
Oops, something went wrong.