SmartCane/r_app/run_full_pipeline.R
2026-01-06 14:17:37 +01:00

158 lines
6.3 KiB
R

# ==============================================================================
# FULL PIPELINE RUNNER
# ==============================================================================
# Runs scripts 02, 03, 04, 09 (KPIs), 09 (Weekly), and 10 (CI Report Simple)
#
# ==============================================================================
# HOW TO RUN THIS SCRIPT
# ==============================================================================
#
# In PowerShell or Command Prompt:
#
# Option 1 (Recommended - shows real-time output):
# Rscript 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" run_full_pipeline.R
#
# Option 3 (Batch mode - output saved to .Rout file):
# R CMD BATCH --vanilla run_full_pipeline.R
#
# ==============================================================================
# ==============================================================================
# *** EDIT THESE VARIABLES ***
end_date <- "2025-12-24" # or specify: "2025-12-02", Sys.Date()
offset <- 7 # days to look back
project_dir <- "angata" # project name: "esa", "aura", "angata", "chemba"
data_source <- if (project_dir == "angata") "merged_tif_8b" else "merged_tif"
# ***************************
# Format dates
end_date_str <- format(as.Date(end_date), "%Y-%m-%d")
# Track success of pipeline
pipeline_success <- TRUE
# ==============================================================================
# SCRIPT 02: CI EXTRACTION
# ==============================================================================
cat("\n========== RUNNING SCRIPT 02: CI EXTRACTION ==========\n")
tryCatch({
source("r_app/02_ci_extraction.R")
main() # Call the main function
cat("✓ Script 02 completed\n")
}, error = function(e) {
cat("✗ Error in Script 02:", e$message, "\n")
pipeline_success <<- FALSE
})
# ==============================================================================
# SCRIPT 03: INTERPOLATE GROWTH MODEL
# ==============================================================================
cat("\n========== RUNNING SCRIPT 03: INTERPOLATE GROWTH MODEL ==========\n")
tryCatch({
source("r_app/03_interpolate_growth_model.R")
main() # Call the main function
cat("✓ Script 03 completed\n")
}, error = function(e) {
cat("✗ Error in Script 03:", e$message, "\n")
pipeline_success <<- FALSE
})
# ==============================================================================
# SCRIPT 04: MOSAIC CREATION
# ==============================================================================
cat("\n========== RUNNING SCRIPT 04: MOSAIC CREATION ==========\n")
tryCatch({
source("r_app/04_mosaic_creation.R")
main() # Call the main function
cat("✓ Script 04 completed\n")
}, error = function(e) {
cat("✗ Error in Script 04:", e$message, "\n")
pipeline_success <<- FALSE
})
# ==============================================================================
# SCRIPT 09: CALCULATE KPIs
# ==============================================================================
cat("\n========== RUNNING SCRIPT 09: CALCULATE KPIs ==========\n")
tryCatch({
source("r_app/09_calculate_kpis.R")
main() # Call the main function
cat("✓ Script 09 (KPIs) completed\n")
}, error = function(e) {
cat("✗ Error in Script 09 (KPIs):", e$message, "\n")
pipeline_success <<- FALSE
})
# ==============================================================================
# SCRIPT 09: FIELD ANALYSIS WEEKLY
# ==============================================================================
# Only run field analysis weekly for angata project
if (project_dir == "angata") {
cat("\n========== RUNNING SCRIPT 09: FIELD ANALYSIS WEEKLY ==========\n")
tryCatch({
source("r_app/09_field_analysis_weekly.R")
main() # Call the main function
cat("✓ Script 09 (Weekly) completed\n")
}, error = function(e) {
cat("✗ Error in Script 09 (Weekly):", e$message, "\n")
pipeline_success <<- FALSE
})
} else {
cat("\n========== SKIPPING SCRIPT 09: FIELD ANALYSIS WEEKLY (only runs for angata) ==========\n")
}
# ==============================================================================
# SCRIPT 91: CI REPORT ANGATA (only for angata)
# ==============================================================================
if (project_dir == "angata") {
cat("\n========== RUNNING SCRIPT 91: CI REPORT ANGATA ==========\n")
if (pipeline_success) {
tryCatch({
rmarkdown::render("r_app/91_CI_report_with_kpis_Angata.Rmd",
output_format = "word_document",
params = list(data_dir = project_dir, report_date = end_date_str))
cat("✓ Script 91 (Report) completed\n")
}, error = function(e) {
cat("✗ Error in Script 91 (Report):", e$message, "\n")
})
} else {
cat("✗ Skipping Script 91: Previous pipeline scripts failed\n")
}
}
# ==============================================================================
# SCRIPT 10: CI REPORT (SIMPLE)
# ==============================================================================
# Only run CI report for non-angata projects
if (project_dir != "angata") {
cat("\n========== RUNNING SCRIPT 10: CI REPORT SIMPLE ==========\n")
if (pipeline_success) {
tryCatch({
rmarkdown::render("r_app/10_CI_report_with_kpis_simple.Rmd",
output_format = "word_document",
params = list(data_dir = project_dir, report_date = end_date_str))
cat("✓ Script 10 (Report) completed\n")
}, error = function(e) {
cat("✗ Error in Script 10 (Report):", e$message, "\n")
})
} else {
cat("✗ Skipping Script 10: Previous pipeline scripts failed\n")
}
} else {
cat("\n========== SKIPPING SCRIPT 10: CI REPORT SIMPLE (not applicable for angata) ==========\n")
}
# ==============================================================================
# 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))
cat("Scripts executed: 02, 03, 04, 09 (KPIs), 09 (Weekly), 10 (CI Report)\n")