# filepath: c:\Users\timon\Resilience BV\4020 SCane ESA DEMO - Documenten\General\4020 SCDEMO Team\4020 TechnicalData\WP3\smartcane\r_app\interpolate_growth_model.R # # INTERPOLATE_GROWTH_MODEL.R # ========================= # This script interpolates CI (Chlorophyll Index) values between measurement dates # to create a continuous growth model. It generates daily values and cumulative # CI statistics for each field. # # Usage: Rscript interpolate_growth_model.R [project_dir] # - project_dir: Project directory name (e.g., "chemba") # # 1. Load required packages # ----------------------- suppressPackageStartupMessages({ library(tidyverse) library(lubridate) library(here) }) # 2. Main function to handle interpolation # ------------------------------------- main <- function() { # Process command line arguments args <- commandArgs(trailingOnly = TRUE) # Get project directory from arguments or use default if (length(args) >= 1 && !is.na(args[1])) { project_dir <- as.character(args[1]) } else { project_dir <- "chemba" message("No project_dir provided. Using default:", project_dir) } # Make project_dir available globally so parameters_project.R can use it assign("project_dir", project_dir, envir = .GlobalEnv) # Initialize project configuration and load utility functions tryCatch({ source("parameters_project.R") source("growth_model_utils.R") }, error = function(e) { warning("Default source files not found. Attempting to source from 'r_app' directory.") tryCatch({ source(here::here("r_app", "parameters_project.R")) source(here::here("r_app", "growth_model_utils.R")) warning(paste("Successfully sourced files from 'r_app' directory.")) }, error = function(e) { stop("Failed to source required files from both default and 'r_app' directories.") }) }) log_message("Starting CI growth model interpolation") # Load and process the data tryCatch({ # Load the combined CI data CI_data <- load_combined_ci_data(cumulative_CI_vals_dir) # Validate harvesting data if (is.null(harvesting_data) || nrow(harvesting_data) == 0) { stop("No harvesting data available") } # Get the years from harvesting data years <- harvesting_data %>% filter(!is.na(season_start)) %>% distinct(year) %>% pull(year) log_message(paste("Processing data for years:", paste(years, collapse = ", "))) # Generate interpolated CI data for each year and field CI_all <- generate_interpolated_ci_data(years, harvesting_data, CI_data) # Calculate growth metrics and save the results if (nrow(CI_all) > 0) { # Add daily and cumulative metrics CI_all_with_metrics <- calculate_growth_metrics(CI_all) # Save the processed data save_growth_model( CI_all_with_metrics, cumulative_CI_vals_dir, "All_pivots_Cumulative_CI_quadrant_year_v2.rds" ) } else { log_message("No CI data was generated after interpolation", level = "WARNING") } log_message("Growth model interpolation completed successfully") }, error = function(e) { log_message(paste("Error in growth model interpolation:", e$message), level = "ERROR") stop(e$message) }) } if (sys.nframe() == 0) { main() }