SmartCane/r_app/experiments/interactive_ci_visualization/test_ci_functions.R
2025-09-05 15:23:41 +02:00

84 lines
2.8 KiB
R

# Test CI Interactive Visualization
# ================================
# Simple test script to verify CI functions work
cat("Testing CI Interactive Visualization...\n")
# Load required libraries
library(terra)
library(sf)
library(tmap)
# Set working directory to the experiment folder
# (This script should be run from the interactive_ci_visualization folder)
# Source the CI analysis functions
source("ci_analysis_functions.R")
# Test 1: Check if we can find CI weekly mosaics
cat("\n=== Test 1: Check CI Weekly Data ===\n")
data_dir <- "../../../laravel_app/storage/app/aura/weekly_mosaic"
cat("Data directory:", data_dir, "\n")
cat("Directory exists:", file.exists(data_dir), "\n")
if (file.exists(data_dir)) {
files <- list.files(data_dir, pattern = "\\.tif$")
cat("Available CI files:", length(files), "\n")
cat("Files:", paste(files, collapse = ", "), "\n")
# Test getting available weeks
available_weeks <- get_available_ci_weeks(data_dir)
cat("Available weeks:", paste(available_weeks, collapse = ", "), "\n")
# Test getting latest week
latest_week <- get_latest_ci_week(data_dir)
cat("Latest week:", latest_week, "\n")
}
# Test 2: Try loading one CI file
cat("\n=== Test 2: Load Single CI File ===\n")
if (exists("latest_week")) {
test_ci <- load_ci_weekly(latest_week, data_dir = data_dir)
if (!is.null(test_ci)) {
cat("✓ CI data loaded successfully\n")
cat(" Dimensions:", dim(test_ci), "\n")
cat(" CRS:", as.character(crs(test_ci)), "\n")
ci_range <- global(test_ci, range, na.rm = TRUE)
cat(" Value range:", round(ci_range[1,1], 3), "to", round(ci_range[2,1], 3), "\n")
} else {
cat("✗ Failed to load CI data\n")
}
}
# Test 3: Check field boundaries
cat("\n=== Test 3: Check Field Boundaries ===\n")
field_file <- "../../../pivot.geojson"
cat("Field boundaries file:", field_file, "\n")
cat("File exists:", file.exists(field_file), "\n")
if (file.exists(field_file)) {
fields <- st_read(field_file, quiet = TRUE)
cat("✓ Field boundaries loaded\n")
cat(" Number of fields:", nrow(fields), "\n")
cat(" Columns:", paste(names(fields), collapse = ", "), "\n")
}
# Test 4: Test comparison data loading
cat("\n=== Test 4: Test Comparison Data Loading ===\n")
if (file.exists(data_dir)) {
tryCatch({
comparison_data <- load_ci_comparison_data(data_dir)
cat("✓ Comparison data loaded successfully\n")
cat(" Current week:", comparison_data$current_week, "\n")
cat(" Previous week:", comparison_data$previous_week, "\n")
cat(" Current CI available:", !is.null(comparison_data$current_ci), "\n")
cat(" Previous CI available:", !is.null(comparison_data$previous_ci), "\n")
}, error = function(e) {
cat("✗ Error loading comparison data:", e$message, "\n")
})
}
cat("\n=== Test Complete ===\n")
cat("If all tests passed, you can run the Interactive_CI_Report.Rmd\n")