SmartCane/r_app/ci_extraction.R

138 lines
4 KiB
R

# nolint start: commented_code_linter, line_length_linter,object_usage_linter.
library(sf)
library(terra)
library(tidyverse)
library(lubridate)
library(exactextractr)
library(readxl)
# Vang alle command line argumenten op
args <- commandArgs(trailingOnly = TRUE)
# Controleer of er ten minste één argument is doorgegeven
if (length(args) == 0) {
stop("Geen argumenten doorgegeven aan het script")
}
# Converteer het eerste argument naar een numerieke waarde
end_date <- as.Date(args[1])
if (is.na(end_date)) {
end_date <- lubridate::dmy("28-08-2024")
}
offset <- as.numeric(args[2])
# Controleer of weeks_ago een geldig getal is
if (is.na(offset)) {
# stop("Het argument is geen geldig getal")
offset <- 7
}
week <- week(end_date)
# Converteer het tweede argument naar een string waarde
project_dir <- as.character(args[3])
# Controleer of data_dir een geldige waarde is
if (!is.character(project_dir)) {
project_dir <- "chemba"
}
new_project_question = FALSE
source("parameters_project.R")
source("ci_extraction_utils.R")
dates <- date_list(end_date, offset)
print(dates)
raster_files <- list.files(planet_tif_folder,full.names = T, pattern = ".tif")
filtered_files <- map(dates$days_filter, ~ raster_files[grepl(pattern = .x, x = raster_files)]) %>%
compact() %>%
flatten_chr()
# Remove files that do not exist
existing_files <- filtered_files[file.exists(filtered_files)]
# Check if the list of existing files is empty
if (length(existing_files) == 0) {
message("No files exist for the given date(s).")
stop("Terminating script.")
}
# Continue with the rest of the script
print(existing_files)
vrt_list <- list()
for (file in existing_files) {
v_crop <- create_mask_and_crop(file, field_boundaries, merged_final)
emtpy_or_full <- global(v_crop, "notNA")
vrt_file <- here(daily_vrt, paste0(tools::file_path_sans_ext(basename(file)), ".vrt"))
if(emtpy_or_full[1,] > 100){
vrt_list[vrt_file] <- vrt_file
}else{
file.remove(vrt_file)
}
message(file, " processed")
gc()
}
raster_files_NEW <- list.files(merged_final,full.names = T, pattern = ".tif")
# Define the path to the file
file_path <- here(cumulative_CI_vals_dir, "combined_CI_data.rds")
# Check if the file exists
if (!file.exists(file_path)) {
# File does not exist, create it with all available data
print("combined_CI_data.rds does not exist. Preparing combined_CI_data.rds file for all available images.")
# Extract data from all raster files
walk(raster_files_NEW, extract_rasters_daily, field_geojson = field_boundaries, quadrants = FALSE, daily_CI_vals_dir)
# Combine all extracted data
extracted_values <- list.files(here(daily_CI_vals_dir), full.names = TRUE)
pivot_stats <- extracted_values %>%
map(readRDS) %>% list_rbind() %>%
group_by(sub_field)
# Save the combined data to the file
saveRDS(pivot_stats, file_path)
print("All CI values extracted from all historic images and saved to combined_CI_data.rds.")
} else {
# File exists, add new data
print("combined_CI_data.rds exists, adding the latest image data to the table.")
# Filter and process the latest data
filtered_files <- map(dates$days_filter, ~ raster_files_NEW[grepl(pattern = .x, x = raster_files_NEW)]) %>%
compact() %>%
flatten_chr()
walk(filtered_files, extract_rasters_daily, field_geojson = field_boundaries, quadrants = TRUE, daily_CI_vals_dir)
# Extract new values
extracted_values <- list.files(daily_CI_vals_dir, full.names = TRUE)
extracted_values <- map(dates$days_filter, ~ extracted_values[grepl(pattern = .x, x = extracted_values)]) %>%
compact() %>%
flatten_chr()
pivot_stats <- extracted_values %>%
map(readRDS) %>% list_rbind() %>%
group_by(sub_field)
# Load existing data and append new data
combined_CI_data <- readRDS(file_path)
pivot_stats2 <- bind_rows(pivot_stats, combined_CI_data)
# Save the updated combined data
saveRDS(pivot_stats2, file_path)
print("All CI values extracted from the latest images and added to combined_CI_data.rds.")
}