SmartCane/r_app/max_ci_field.r
2025-03-25 10:42:04 +01:00

55 lines
1.3 KiB
R

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")
raster_files <- list.files(merged_final,full.names = T, pattern = ".tif")
# Load all raster files
rasters <- lapply(raster_files, function(file) {
r <- rast(file)
r[[which(names(r) == "CI")]] # Select the CI band
})
# Calculate the maximum CI per pixel
max_ci_raster <- do.call(terra::app, c(rasters, fun = mean, na.rm = TRUE))
plot(max_ci_raster)