# CI_EXTRACTION_PER_FIELD.R # ========================= # Script 20 (Refactored for Per-Field Architecture) # # This script reads per-field TIFFs from Script 10 output and: # 1. Calculates Canopy Index (CI) from 4-band imagery (RGB + NIR) # 2. Outputs 5-band TIFFs with CI as the 5th band to field_tiles_CI/{FIELD}/{DATE}.tif # 3. Outputs per-field per-date RDS files to daily_vals/{FIELD}/{DATE}.rds # # Key differences from legacy Script 20: # - Input: field_tiles/{FIELD}/{DATE}.tif (4-band, from Script 10) # - Output: field_tiles_CI/{FIELD}/{DATE}.tif (5-band with CI) # - Output: daily_vals/{FIELD}/{DATE}.rds (per-field CI statistics) # - Directly extracts CI statistics per sub_field within each field # # Usage: # Rscript 20_ci_extraction_per_field.R [project_dir] [end_date] [offset] # Example: Rscript 20_ci_extraction_per_field.R angata 2026-01-02 7 suppressPackageStartupMessages({ library(sf) library(terra) library(tidyverse) library(lubridate) library(here) }) # ============================================================================= # Main Processing # ============================================================================= main <- function() { # STEP 1: Set working directory to project root (smartcane/) # This ensures all relative paths resolve correctly if (basename(getwd()) == "r_app") { setwd("..") } # STEP 2: SOURCE ALL UTILITY SCRIPTS (before any operations) # Parse command-line arguments FIRST args <- commandArgs(trailingOnly = TRUE) project_dir <- if (length(args) >= 1 && args[1] != "") args[1] else "angata" end_date <- if (length(args) >= 2 && args[2] != "") as.Date(args[2]) else Sys.Date() offset <- if (length(args) >= 3 && !is.na(as.numeric(args[3]))) as.numeric(args[3]) else 7 # Make project_dir available globally for parameters_project.R assign("project_dir", project_dir, envir = .GlobalEnv) # Load parameters_project.R (provides safe_log, date_list, setup_project_directories, etc.) tryCatch({ source("r_app/parameters_project.R") }, error = function(e) { cat(sprintf("Error loading parameters_project.R: %s\n", e$message)) stop(e) }) # Load CI extraction utilities tryCatch({ source("r_app/20_ci_extraction_utils.R") }, error = function(e) { cat(sprintf("Error loading 20_ci_extraction_utils.R: %s\n", e$message)) stop(e) }) # STEP 3: Now all utilities are loaded, proceed with script logic safe_log(sprintf("=== Script 20: CI Extraction Per-Field ===")) safe_log(sprintf("Project: %s | End Date: %s | Offset: %d days", project_dir, format(end_date, "%Y-%m-%d"), offset)) # Set up directory paths from parameters setup <- setup_project_directories(project_dir) # Load field boundaries directly from field_boundaries_path in setup tryCatch({ field_boundaries_sf <- st_read(setup$field_boundaries_path, quiet = TRUE) safe_log(sprintf("Loaded %d field/sub_field polygons from %s", nrow(field_boundaries_sf), setup$field_boundaries_path)) }, error = function(e) { safe_log(sprintf("Error loading field boundaries from %s: %s", setup$field_boundaries_path, e$message), "ERROR") stop(e) }) # Get list of dates to process dates <- date_list(end_date, offset) safe_log(sprintf("Processing dates: %s to %s (%d dates)", dates$start_date, dates$end_date, length(dates$days_filter))) safe_log(sprintf("Input directory: %s", setup$field_tiles_dir)) safe_log(sprintf("Output TIF directory: %s", setup$field_tiles_ci_dir)) safe_log(sprintf("Output RDS directory: %s", setup$daily_ci_vals_dir)) # Process each field if (!dir.exists(setup$field_tiles_dir)) { safe_log(sprintf("Field tiles directory not found: %s", setup$field_tiles_dir), "ERROR") stop("Script 10 output not found. Run Script 10 first.") } fields <- list.dirs(setup$field_tiles_dir, full.names = FALSE, recursive = FALSE) fields <- fields[fields != ""] # Remove empty strings if (length(fields) == 0) { safe_log("No fields found in field_tiles directory", "WARNING") return() } safe_log(sprintf("Found %d fields to process", length(fields))) # DEBUG: Check what paths are available in setup safe_log(sprintf("[DEBUG] Available setup paths: %s", paste(names(setup), collapse=", "))) safe_log(sprintf("[DEBUG] field_tiles_ci_dir: %s", setup$field_tiles_ci_dir)) safe_log(sprintf("[DEBUG] daily_ci_vals_dir: %s", setup$daily_ci_vals_dir)) # Use daily_ci_vals_dir for per-field daily CI output # Pre-create output subdirectories for all fields for (field in fields) { dir.create(file.path(setup$field_tiles_ci_dir, field), showWarnings = FALSE, recursive = TRUE) if (!is.null(setup$daily_ci_vals_dir)) { dir.create(file.path(setup$daily_ci_vals_dir, field), showWarnings = FALSE, recursive = TRUE) } } # Process each DATE (OPTIMIZED: load TIFF once, process all fields) total_success <- 0 total_error <- 0 ci_results_by_date <- list() for (date_str in dates$days_filter) { # Load the merged TIFF ONCE for this date merged_tif_path <- file.path(setup$field_tiles_dir, fields[1], sprintf("%s.tif", date_str)) # Find the actual TIFF path (it's in the first field that has it) input_tif_full <- NULL for (field in fields) { candidate_path <- file.path(setup$field_tiles_dir, field, sprintf("%s.tif", date_str)) if (file.exists(candidate_path)) { input_tif_full <- candidate_path break } } if (is.null(input_tif_full)) { safe_log(sprintf(" %s: Input TIFF not found (skipping)", date_str)) next } tryCatch({ # Load TIFF ONCE raster_4band <- terra::rast(input_tif_full) # Now process all fields from this single TIFF for (field in fields) { field_ci_path <- file.path(setup$field_tiles_ci_dir, field) field_daily_vals_path <- file.path(setup$daily_ci_vals_dir, field) output_tif <- file.path(field_ci_path, sprintf("%s.tif", date_str)) output_rds <- file.path(field_daily_vals_path, sprintf("%s.rds", date_str)) # MODE 3: Skip if both outputs already exist if (file.exists(output_tif) && file.exists(output_rds)) { next # Skip to next field } # MODE 2: Regeneration mode - RDS missing but CI TIFF exists if (file.exists(output_tif) && !file.exists(output_rds)) { tryCatch({ extract_rds_from_ci_tiff(output_tif, output_rds, field_boundaries_sf, field) total_success <<- total_success + 1 }, error = function(e) { total_error <<- total_error + 1 }) next } # MODE 1: Normal mode - calculate CI from 4-band input tryCatch({ # Calculate CI ci_raster <- calc_ci_from_raster(raster_4band) # Create 5-band TIFF (R, G, B, NIR, CI) five_band <- c(raster_4band, ci_raster) # Save 5-band TIFF terra::writeRaster(five_band, output_tif, overwrite = TRUE) # Extract CI statistics by sub_field ci_stats <- extract_ci_by_subfield(ci_raster, field_boundaries_sf, field) # Save RDS if (!is.null(ci_stats) && nrow(ci_stats) > 0) { saveRDS(ci_stats, output_rds) # Store for daily aggregation ci_stats_with_date <- ci_stats %>% mutate(date = date_str) key <- sprintf("%s_%s", field, date_str) ci_results_by_date[[key]] <<- ci_stats_with_date } total_success <<- total_success + 1 }, error = function(e) { total_error <<- total_error + 1 }) } }, error = function(e) { safe_log(sprintf(" %s: ✗ Error loading TIFF - %s", date_str, e$message), "ERROR") total_error <<- total_error + 1 }) } # Summary safe_log(sprintf("\n=== Processing Complete ===")) safe_log(sprintf("Successfully processed: %d", total_success)) safe_log(sprintf("Errors encountered: %d", total_error)) if (total_success > 0) { safe_log("Output files created in:") safe_log(sprintf(" TIFFs: %s", setup$field_tiles_ci_dir)) safe_log(sprintf(" RDS: %s", setup$daily_ci_vals_dir)) } } # Execute main if called from command line if (sys.nframe() == 0) { main() }