# ============================================================================== # FULL PIPELINE RUNNER # ============================================================================== # Mixed Python/R pipeline: # 1. Python: Download Planet images # 2. R 10: Create master grid and split TIFFs # 3. R 20: CI Extraction # 4. R 30: Interpolate growth model # 5. R 21: Convert CI RDS to CSV (uses Script 30 output) # 6. Python 31: Harvest imminent weekly # 7. R 40: Mosaic creation # 8. R 80: Calculate KPIs # 9. R 90 (Agronomic) OR R 91 (Cane Supply): Generate Word Report # # ============================================================================== # HOW TO RUN THIS SCRIPT # ============================================================================== # # Run from the smartcane/ directory: # # Option 1 (Recommended - shows real-time output): # Rscript r_app/run_full_pipeline.R # # Option 2 (Full path to Rscript - use & in PowerShell for paths with spaces): # & "C:\Program Files\R\R-4.4.3\bin\x64\Rscript.exe" r_app/run_full_pipeline.R # # Option 3 (Batch mode - output saved to .Rout file): # R CMD BATCH --vanilla r_app/run_full_pipeline.R # # ============================================================================== # ============================================================================== # *** EDIT THESE VARIABLES *** end_date <- as.Date("2026-02-04") # or specify: as.Date("2026-01-27") , Sys.Date() offset <- 7 # days to look back project_dir <- "angata" # project name: "esa", "aura", "angata", "chemba" force_rerun <- FALSE # Set to TRUE to force all scripts to run even if outputs exist migrate_legacy_format <- TRUE # Set to TRUE to migrate from old format (merged_tif/merged_tif_8b) to new format (field_tiles) # *** NOTE: data_source is now unified - all projects use field_tiles after migration *** # *************************** # Format dates end_date_str <- format(as.Date(end_date), "%Y-%m-%d") # Track success of pipeline pipeline_success <- TRUE # ============================================================================== # INTELLIGENT CHECKING: What has already been completed? # ============================================================================== cat("\n========== CHECKING EXISTING OUTPUTS ==========\n") # Check Script 10 outputs (field_tiles with per-field TIFFs) # Script 10 outputs to field_tiles/{field_id}/{date}.tif field_tiles_dir <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles") tiles_dates <- c() if (dir.exists(field_tiles_dir)) { # Get all field subdirectories field_dirs <- list.dirs(field_tiles_dir, full.names = TRUE, recursive = FALSE) if (length(field_dirs) > 0) { # Get unique dates from all field directories all_files <- list.files(field_dirs, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") tiles_dates <- unique(sub("\\.tif$", "", all_files)) } } cat(sprintf("Script 10: %d dates already tiled (field_tiles/)\n", length(tiles_dates))) # Check Script 20 outputs (CI extraction) - per-field CI TIFFs at field_tiles_CI/{FIELD}/{DATE}.tif # NOTE: This is the NEW per-field format, not the old extracted_ci/ flat format field_tiles_ci_dir <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles_CI") ci_tiff_dates <- c() if (dir.exists(field_tiles_ci_dir)) { # Get all field subdirectories field_dirs <- list.dirs(field_tiles_ci_dir, full.names = TRUE, recursive = FALSE) if (length(field_dirs) > 0) { # Get unique dates from all field directories (dates that have been processed through Script 20) all_files <- list.files(field_dirs, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") ci_tiff_dates <- unique(sub("\\.tif$", "", all_files)) } } cat(sprintf("Script 20: %d dates already processed (field_tiles_CI/)\n", length(ci_tiff_dates))) # Check Script 21 outputs (CSV conversion) - note: this gets overwritten each time, so we don't skip based on this # Instead, check if CI RDS files exist - if they do, 21 should also run # For now, just note that CSV is time-dependent, not a good skip indicator cat("Script 21: CSV file exists but gets overwritten - will run if Script 20 runs\n") # Check Script 40 outputs (mosaics in weekly_tile_max/5x5) mosaic_dir <- file.path("laravel_app", "storage", "app", project_dir, "weekly_tile_max") mosaic_files <- if (dir.exists(mosaic_dir)) { list.files(mosaic_dir, pattern = "\\.tif$") } else { c() } cat(sprintf("Script 40: %d mosaic files exist\n", length(mosaic_files))) # Check Script 80 outputs (KPIs in reports/kpis/field_stats) kpi_dir <- file.path("laravel_app", "storage", "app", project_dir, "reports", "kpis", "field_stats") kpi_files <- if (dir.exists(kpi_dir)) { list.files(kpi_dir, pattern = "\\.csv$|\\.json$") } else { c() } cat(sprintf("Script 80: %d KPI files exist\n", length(kpi_files))) # Determine if scripts should run based on outputs skip_10 <- FALSE # Script 10 should always run to pick up any new merged_tif files skip_20 <- FALSE # Script 20 always runs to process dates in the current window (per-field format) skip_21 <- FALSE # Skip 21 only if 20 is skipped skip_40 <- length(mosaic_files) > 0 && !force_rerun skip_80 <- FALSE # Always run Script 80 - it calculates KPIs for the current week (end_date), not historical weeks cat("\nSkipping decisions:\n") cat(sprintf(" Script 10: %s\n", if(skip_10) "SKIP (tiles exist)" else "RUN")) cat(sprintf(" Script 20: %s\n", if(skip_20) "SKIP (CI exists)" else "RUN")) cat(sprintf(" Script 21: %s\n", if(skip_21) "SKIP (CI exists)" else "RUN")) cat(sprintf(" Script 40: %s\n", if(skip_40) "SKIP (mosaics exist)" else "RUN")) cat(sprintf(" Script 80: %s\n", if(skip_80) "SKIP (KPIs exist)" else "RUN")) # ============================================================================== # PYTHON: DOWNLOAD PLANET IMAGES (MISSING DATES ONLY) # ============================================================================== cat("\n========== DOWNLOADING PLANET IMAGES (MISSING DATES ONLY) ==========\n") tryCatch({ # Setup paths base_path <- file.path("laravel_app", "storage", "app", project_dir) # Always check merged_tif/ for existing downloads (both modes) # merged_tif/ is where Python downloads go, before Script 10 splits to field_tiles/ merged_tifs_dir <- file.path(base_path, "merged_tif") existing_tiff_files <- list.files(merged_tifs_dir, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") existing_tiff_dates <- sub("\\.tif$", "", existing_tiff_files) if (migrate_legacy_format) { cat(sprintf(" Migration mode: Checking merged_tif/ for existing dates\n")) } else { cat(sprintf(" Production mode: Checking merged_tif/ and field_tiles/ for existing dates\n")) } # Find missing dates in the window # Window: from (end_date - offset) to end_date # Example: if end_date=2026-02-04 and offset=7, window is 2026-01-28 to 2026-02-04 (8 dates) start_date <- end_date - offset date_seq <- seq(start_date, end_date, by = "day") target_dates <- format(date_seq, "%Y-%m-%d") # Also check field_tiles/ for dates that have already been processed through Script 10 # field_tiles/ contains {field_id}/{date}.tif files - check which dates are present field_tiles_dir <- file.path(base_path, "field_tiles") processed_dates <- c() if (dir.exists(field_tiles_dir)) { # Get all field subdirectories field_dirs <- list.dirs(field_tiles_dir, full.names = TRUE, recursive = FALSE) if (length(field_dirs) > 0) { # Get unique dates from all field directories all_files <- list.files(field_dirs, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") processed_dates <- unique(sub("\\.tif$", "", all_files)) } } # Combine existing dates from both merged_tif and field_tiles all_existing_dates <- unique(c(existing_tiff_dates, processed_dates)) # Compare: which target dates don't exist in merged_tif/ or field_tiles/? missing_dates <- target_dates[!(target_dates %in% all_existing_dates)] cat(sprintf(" Existing dates in merged_tif/: %d\n", length(existing_tiff_dates))) cat(sprintf(" Processed dates in field_tiles/: %d\n", length(processed_dates))) cat(sprintf(" Target window: %s to %s (%d dates)\n", start_date, end_date, length(target_dates))) cat(sprintf(" Missing dates to download: %d\n", length(missing_dates))) # Download each missing date download_count <- 0 download_failed <- 0 if (length(missing_dates) > 0) { # Save current directory original_dir <- getwd() # Change to python_app directory so relative paths work correctly setwd("python_app") for (date_str in missing_dates) { cmd <- sprintf('python 00_download_8band_pu_optimized.py "%s" --date "%s" --resolution 3 --cleanup', project_dir, date_str) result <- system(cmd, ignore.stdout = FALSE, ignore.stderr = FALSE) if (result == 0) { download_count <- download_count + 1 } else { download_failed <- download_failed + 1 } } # Change back to original directory setwd(original_dir) } cat(sprintf("✓ Downloaded %d dates, %d failed\n", download_count, download_failed)) if (download_failed > 0) { cat("⚠ Some downloads failed, but continuing pipeline\n") } # Force Script 10 to run ONLY if downloads actually succeeded (not just attempted) if (download_count > 0) { skip_10 <- FALSE } }, error = function(e) { cat("✗ Error in planet download:", e$message, "\n") pipeline_success <<- FALSE }) # ============================================================================== # MIGRATION: Move legacy format files to new format (if enabled) # ============================================================================== if (pipeline_success && migrate_legacy_format) { cat("\n========== MIGRATION: MOVING LEGACY FORMAT FILES ==========\n") tryCatch({ base_path <- file.path("laravel_app", "storage", "app", project_dir) # PART 1: Move merged_tif files to field_tiles merged_tif_old <- file.path(base_path, "merged_tif") field_tiles_new <- file.path(base_path, "field_tiles") if (dir.exists(merged_tif_old)) { tif_files <- list.files(merged_tif_old, pattern = "\\.tif$", full.names = TRUE) if (length(tif_files) > 0) { dir.create(field_tiles_new, showWarnings = FALSE, recursive = TRUE) for (file in tif_files) { file.rename(file, file.path(field_tiles_new, basename(file))) } cat(sprintf("✓ Moved %d TIFF files from merged_tif/ to field_tiles/\n", length(tif_files))) } } # PART 2: Move merged_tif_final files (CI) to field_tiles_CI merged_tif_final_old <- file.path(base_path, "merged_tif_final") field_tiles_ci_new <- file.path(base_path, "field_tiles_CI") if (dir.exists(merged_tif_final_old)) { ci_files <- list.files(merged_tif_final_old, pattern = "\\.tif$", full.names = TRUE) if (length(ci_files) > 0) { dir.create(field_tiles_ci_new, showWarnings = FALSE, recursive = TRUE) for (file in ci_files) { file.rename(file, file.path(field_tiles_ci_new, basename(file))) } cat(sprintf("✓ Moved %d CI TIFF files from merged_tif_final/ to field_tiles_CI/\n", length(ci_files))) } } cat("✓ Migration completed successfully\n") }, error = function(e) { cat("✗ Error in migration:", e$message, "\n") pipeline_success <<- FALSE }) } # ============================================================================== # SCRIPT 10: CREATE MASTER GRID AND SPLIT TIFFs # ============================================================================== if (pipeline_success && !skip_10) { cat("\n========== RUNNING SCRIPT 10: CREATE MASTER GRID AND SPLIT TIFFs ==========\n") tryCatch({ # Set environment variables for the script (Script 10 uses these for filtering) assign("PROJECT", project_dir, envir = .GlobalEnv) assign("end_date", end_date, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) # Count field_tiles/ dates BEFORE Script 10 runs field_tiles_dir <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles") field_dirs_before <- c() if (dir.exists(field_tiles_dir)) { field_dirs_tmp <- list.dirs(field_tiles_dir, full.names = TRUE, recursive = FALSE) if (length(field_dirs_tmp) > 0) { all_files_before <- list.files(field_dirs_tmp, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") field_dirs_before <- unique(sub("\\.tif$", "", all_files_before)) } } # Suppress verbose per-date output, show only summary sink(nullfile()) source("r_app/10_create_per_field_tiffs.R") sink() # Count field_tiles/ dates AFTER Script 10 runs field_dirs_after <- c() if (dir.exists(field_tiles_dir)) { field_dirs_tmp <- list.dirs(field_tiles_dir, full.names = TRUE, recursive = FALSE) if (length(field_dirs_tmp) > 0) { all_files_after <- list.files(field_dirs_tmp, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") field_dirs_after <- unique(sub("\\.tif$", "", all_files_after)) } } # Calculate newly added dates newly_added <- length(field_dirs_after) - length(field_dirs_before) cat(sprintf("✓ Script 10 completed - processed %d new dates (total: %d dates in field_tiles/)\n", max(0, newly_added), length(field_dirs_after))) }, error = function(e) { sink() cat("✗ Error in Script 10:", e$message, "\n") pipeline_success <<- FALSE }) } else if (skip_10) { cat("\n========== SKIPPING SCRIPT 10 (tiles already exist) ==========\n") } # ============================================================================== # SCRIPT 20: CI EXTRACTION # ============================================================================== if (pipeline_success && !skip_20) { cat("\n========== RUNNING SCRIPT 20: CI EXTRACTION ==========\n") tryCatch({ # Set environment variables for the script assign("end_date", end_date, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) assign("project_dir", project_dir, envir = .GlobalEnv) # If in migration mode, find all dates that need processing if (migrate_legacy_format) { cat("Migration mode: Finding all dates in field_tiles/ that need CI processing...\n") # Get all dates from field_tiles/ field_tiles_dir_check <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles") all_dates_in_tiles <- c() if (dir.exists(field_tiles_dir_check)) { field_dirs_tmp <- list.dirs(field_tiles_dir_check, full.names = TRUE, recursive = FALSE) if (length(field_dirs_tmp) > 0) { all_files_tmp <- list.files(field_dirs_tmp, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") all_dates_in_tiles <- unique(sub("\\.tif$", "", all_files_tmp)) } } # Get dates already processed in field_tiles_CI/ field_tiles_ci_check <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles_CI") processed_ci_dates <- c() if (dir.exists(field_tiles_ci_check)) { field_dirs_ci <- list.dirs(field_tiles_ci_check, full.names = TRUE, recursive = FALSE) if (length(field_dirs_ci) > 0) { all_files_ci <- list.files(field_dirs_ci, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") processed_ci_dates <- unique(sub("\\.tif$", "", all_files_ci)) } } # Get dates already in old RDS format old_rds_dir <- file.path("laravel_app", "storage", "app", project_dir, "Data", "extracted_ci", "daily_vals") processed_rds_dates <- c() if (dir.exists(old_rds_dir)) { rds_files <- list.files(old_rds_dir, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.rds$") processed_rds_dates <- unique(sub("\\.rds$", "", rds_files)) } # Find dates missing from either location dates_missing_ci <- all_dates_in_tiles[!(all_dates_in_tiles %in% processed_ci_dates)] dates_missing_rds <- all_dates_in_tiles[!(all_dates_in_tiles %in% processed_rds_dates)] dates_to_process_migration <- sort(unique(c(dates_missing_ci, dates_missing_rds))) cat(sprintf(" All dates in field_tiles/: %d\n", length(all_dates_in_tiles))) cat(sprintf(" Already in field_tiles_CI/: %d\n", length(processed_ci_dates))) cat(sprintf(" Already in extracted_ci/daily_vals/: %d\n", length(processed_rds_dates))) cat(sprintf(" Dates needing processing: %d\n", length(dates_to_process_migration))) if (length(dates_to_process_migration) > 0) { assign("dates_to_process", dates_to_process_migration, envir = .GlobalEnv) cat(sprintf(" Will process: %s to %s\n", dates_to_process_migration[1], dates_to_process_migration[length(dates_to_process_migration)])) } } source("r_app/20_ci_extraction_per_field.R") main() # Verify output field_tiles_ci_verify <- file.path("laravel_app", "storage", "app", project_dir, "field_tiles_CI") tiff_count <- 0 if (dir.exists(field_tiles_ci_verify)) { field_dirs_verify <- list.dirs(field_tiles_ci_verify, full.names = TRUE, recursive = FALSE) if (length(field_dirs_verify) > 0) { all_files_verify <- list.files(field_dirs_verify, pattern = "^\\d{4}-\\d{2}-\\d{2}\\.tif$") tiff_count <- length(all_files_verify) } } cat(sprintf("✓ Script 20 completed - %d CI TIFFs in field_tiles_CI/\n", tiff_count)) }, error = function(e) { cat("✗ Error in Script 20:", e$message, "\n") pipeline_success <<- FALSE }) } else if (skip_20) { cat("\n========== SKIPPING SCRIPT 20 (CI already extracted) ==========\n") } # ============================================================================== # SCRIPT 30: INTERPOLATE GROWTH MODEL # ============================================================================== if (pipeline_success) { cat("\n========== RUNNING SCRIPT 30: INTERPOLATE GROWTH MODEL ==========\n") tryCatch({ # Set environment variables for the script assign("end_date", end_date, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) assign("project_dir", project_dir, envir = .GlobalEnv) source("r_app/30_interpolate_growth_model.R") main() # Call main() to execute the script with the environment variables # Verify interpolated output growth_dir <- file.path("laravel_app", "storage", "app", project_dir, "growth_model_interpolated") if (dir.exists(growth_dir)) { files <- list.files(growth_dir, pattern = "\\.rds$|\\.csv$") cat(sprintf("✓ Script 30 completed - generated %d growth model files\n", length(files))) } else { cat("✓ Script 30 completed\n") } }, error = function(e) { cat("✗ Error in Script 30:", e$message, "\n") pipeline_success <<- FALSE }) } # ============================================================================== # SCRIPT 21: CONVERT CI RDS TO CSV (uses Script 30 output) # ============================================================================== if (pipeline_success && !skip_21) { cat("\n========== RUNNING SCRIPT 21: CONVERT CI RDS TO CSV ==========\n") tryCatch({ # Set environment variables for the script assign("end_date", end_date, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) assign("project_dir", project_dir, envir = .GlobalEnv) source("r_app/21_convert_ci_rds_to_csv.R") main() # Call main() to execute the script with the environment variables # Verify CSV output was created ci_csv_path <- file.path("laravel_app", "storage", "app", project_dir, "ci_extracted") if (dir.exists(ci_csv_path)) { csv_files <- list.files(ci_csv_path, pattern = "\\.csv$") cat(sprintf("✓ Script 21 completed - converted to %d CSV files\n", length(csv_files))) } else { cat("✓ Script 21 completed\n") } }, error = function(e) { cat("✗ Error in Script 21:", e$message, "\n") pipeline_success <<- FALSE }) } else if (skip_21) { cat("\n========== SKIPPING SCRIPT 21 (CSV already created) ==========\n") } # ============================================================================== # PYTHON 31: HARVEST IMMINENT WEEKLY # ============================================================================== if (pipeline_success) { cat("\n========== RUNNING PYTHON 31: HARVEST IMMINENT WEEKLY ==========\n") tryCatch({ # Run Python script in pytorch_gpu conda environment # Script expects positional project name (not --project flag) # Run from smartcane root so conda can find the environment cmd <- sprintf('conda run -n pytorch_gpu python python_app/31_harvest_imminent_weekly.py %s', project_dir) cat("DEBUG: Running command:", cmd, "\n") result <- system(cmd) if (result == 0) { # Verify harvest output - check for THIS WEEK's specific file current_week <- as.numeric(format(end_date, "%V")) current_year <- as.numeric(format(end_date, "%Y")) expected_file <- file.path("laravel_app", "storage", "app", project_dir, "reports", "kpis", "field_stats", sprintf("%s_harvest_imminent_week_%02d_%d.csv", project_dir, current_week, current_year)) if (file.exists(expected_file)) { cat(sprintf("✓ Script 31 completed - generated harvest imminent file for week %02d\n", current_week)) } else { cat("✓ Script 31 completed (check if harvest.xlsx is available)\n") } } else { cat("⚠ Script 31 completed with errors (check harvest.xlsx availability)\n") } }, error = function(e) { setwd(original_dir) cat("⚠ Script 31 error:", e$message, "\n") }) } # ============================================================================== # SCRIPT 40: MOSAIC CREATION # ============================================================================== if (pipeline_success && !skip_40) { cat("\n========== RUNNING SCRIPT 40: MOSAIC CREATION ==========\n") tryCatch({ # Set environment variables for the script assign("end_date", end_date, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) assign("project_dir", project_dir, envir = .GlobalEnv) source("r_app/40_mosaic_creation_per_field.R") main() # Call main() to execute the script with the environment variables # Verify mosaic output mosaic_dir <- file.path("laravel_app", "storage", "app", project_dir, "weekly_tile_max", "5x5") if (dir.exists(mosaic_dir)) { files <- list.files(mosaic_dir, pattern = "\\.tif$") cat(sprintf("✓ Script 40 completed - generated %d mosaic files\n", length(files))) } else { cat("✓ Script 40 completed\n") } }, error = function(e) { cat("✗ Error in Script 40:", e$message, "\n") pipeline_success <<- FALSE }) } else if (skip_40) { cat("\n========== SKIPPING SCRIPT 40 (mosaics already created) ==========\n") } # ============================================================================== # SCRIPT 80: CALCULATE KPIs # ============================================================================== if (pipeline_success) { # Always run Script 80 - it calculates KPIs for the current week cat("\n========== RUNNING SCRIPT 80: CALCULATE KPIs ==========\n") tryCatch({ # Set environment variables for the script (Script 80's main() uses these as fallbacks) # NOTE: end_date is already a Date, just assign directly without as.Date() assign("end_date", end_date, envir = .GlobalEnv) assign("end_date_str", end_date_str, envir = .GlobalEnv) assign("offset", offset, envir = .GlobalEnv) assign("project_dir", project_dir, envir = .GlobalEnv) source("r_app/80_calculate_kpis.R") main() # Call main() to execute the script with the environment variables # Verify KPI output kpi_dir <- file.path("laravel_app", "storage", "app", project_dir, "reports", "kpis", "field_stats") if (dir.exists(kpi_dir)) { files <- list.files(kpi_dir, pattern = "\\.csv$|\\.json$") cat(sprintf("✓ Script 80 completed - generated %d KPI files\n", length(files))) } else { cat("✓ Script 80 completed\n") } }, error = function(e) { cat("✗ Error in Script 80:", e$message, "\n") cat("Full error:\n") print(e) pipeline_success <<- FALSE }) } # ============================================================================== # SCRIPT 90/91: GENERATE WORD REPORTS (CLIENT-TYPE SPECIFIC) # ============================================================================== if (pipeline_success) { # Determine client type from project mapping source("r_app/parameters_project.R") source("r_app/00_common_utils.R") client_type <- get_client_type(project_dir) if (client_type == "agronomic_support") { # SCRIPT 90: Agronomic Support Report (for Aura) cat("\n========== RUNNING SCRIPT 90: AGRONOMIC SUPPORT REPORT (WORD) ==========\n") tryCatch({ # Render the R Markdown file with parameters # The Rmd file will load parameters_project and utilities internally rmarkdown::render( "r_app/90_CI_report_with_kpis_simple.Rmd", params = list( data_dir = project_dir, report_date = end_date, mail_day = "Monday", borders = TRUE, ci_plot_type = "both", colorblind_friendly = FALSE, facet_by_season = FALSE, x_axis_unit = "days" ), output_file = sprintf("SmartCane_Report_agronomic_%s_%s.docx", project_dir, end_date_str), output_dir = file.path("laravel_app", "storage", "app", project_dir, "reports"), quiet = FALSE, knit_root_dir = getwd() ) # Verify report was created report_file <- file.path("laravel_app", "storage", "app", project_dir, "reports", sprintf("SmartCane_Report_agronomic_%s_%s.docx", project_dir, end_date_str)) if (file.exists(report_file)) { cat(sprintf("✓ Script 90 completed - generated Word report: %s\n", basename(report_file))) } else { cat("⚠ Script 90 report file not found - check rendering\n") } }, error = function(e) { cat("✗ Error in Script 90:", e$message, "\n") print(e) pipeline_success <<- FALSE }) } else if (client_type == "cane_supply") { # SCRIPT 91: Cane Supply Report (for Angata, Chemba, Xinavane, ESA) cat("\n========== RUNNING SCRIPT 91: CANE SUPPLY REPORT (WORD) ==========\n") tryCatch({ # Render the R Markdown file with parameters # The Rmd file will load parameters_project and utilities internally rmarkdown::render( "r_app/91_CI_report_with_kpis_Angata.Rmd", params = list( data_dir = project_dir, report_date = end_date, mail_day = "Monday", borders = TRUE, ci_plot_type = "both", colorblind_friendly = FALSE, facet_by_season = FALSE, x_axis_unit = "days" ), output_file = sprintf("SmartCane_Report_cane_supply_%s_%s.docx", project_dir, end_date_str), output_dir = file.path("laravel_app", "storage", "app", project_dir, "reports"), quiet = FALSE, knit_root_dir = getwd() ) # Verify report was created report_file <- file.path("laravel_app", "storage", "app", project_dir, "reports", sprintf("SmartCane_Report_cane_supply_%s_%s.docx", project_dir, end_date_str)) if (file.exists(report_file)) { cat(sprintf("✓ Script 91 completed - generated Word report: %s\n", basename(report_file))) } else { cat("⚠ Script 91 report file not found - check rendering\n") } }, error = function(e) { cat("✗ Error in Script 91:", e$message, "\n") print(e) pipeline_success <<- FALSE }) } } # ============================================================================== # SUMMARY # ============================================================================== cat("\n========== PIPELINE COMPLETE ==========\n") cat(sprintf("Project: %s\n", project_dir)) cat(sprintf("End Date: %s\n", end_date_str)) cat(sprintf("Offset: %d days\n", offset)) if (pipeline_success) { cat("Status: ✓ All scripts completed successfully\n") } else { cat("Status: ✗ Pipeline failed - check errors above\n") } cat("Pipeline sequence: Python Download → R 10 → R 20 → R 30 → R 21 → Python 31 → R 40 → R 80 → R 90/91\n")