158 lines
5.6 KiB
R
158 lines
5.6 KiB
R
# filepath: c:\Users\timon\Resilience BV\4020 SCane ESA DEMO - Documenten\General\4020 SCDEMO Team\4020 TechnicalData\WP3\smartcane\r_app\mosaic_creation.R
|
|
#
|
|
# MOSAIC_CREATION.R
|
|
# ===============
|
|
# This script creates weekly mosaics from daily satellite imagery.
|
|
# It handles command-line arguments and initiates the mosaic creation process.
|
|
#
|
|
# Usage: Rscript mosaic_creation.R [end_date] [offset] [project_dir] [file_name] [use_tiles] [tile_size]
|
|
# - end_date: End date for processing (YYYY-MM-DD format)
|
|
# - offset: Number of days to look back from end_date
|
|
# - project_dir: Project directory name (e.g., "chemba")
|
|
# - file_name: Optional custom output file name
|
|
# - use_tiles: Use tile-based processing for memory efficiency (TRUE/FALSE, default: FALSE)
|
|
# - tile_size: Tile size in km (default: 5, only used if use_tiles=TRUE)
|
|
#
|
|
# Examples:
|
|
# Rscript 04_mosaic_creation.R 2025-12-21 7 angata
|
|
# Rscript 04_mosaic_creation.R 2025-12-21 7 angata week_51.tif TRUE 5 [tile-based]
|
|
#
|
|
|
|
# 1. Load required packages
|
|
# -----------------------
|
|
suppressPackageStartupMessages({
|
|
library(sf)
|
|
library(terra)
|
|
library(tidyverse)
|
|
library(lubridate)
|
|
library(here)
|
|
})
|
|
|
|
# 2. Process command line arguments and run mosaic creation
|
|
# ------------------------------------------------------
|
|
main <- function() {
|
|
# Capture command line arguments
|
|
args <- commandArgs(trailingOnly = TRUE)
|
|
|
|
# Process project_dir argument with default
|
|
if (length(args) >= 3 && !is.na(args[3])) {
|
|
project_dir <- as.character(args[3])
|
|
} else if (exists("project_dir", envir = .GlobalEnv)) {
|
|
project_dir <- get("project_dir", envir = .GlobalEnv)
|
|
} else {
|
|
# Default project directory
|
|
project_dir <- "angata"
|
|
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)
|
|
|
|
# Process end_date argument with default
|
|
if (length(args) >= 1 && !is.na(args[1])) {
|
|
end_date <- as.Date(args[1])
|
|
if (is.na(end_date)) {
|
|
message("Invalid end_date provided. Using current date.")
|
|
end_date <- Sys.Date()
|
|
end_date <- "2026-01-01" # Default date for testing
|
|
}
|
|
} else if (exists("end_date_str", envir = .GlobalEnv)) {
|
|
end_date <- as.Date(get("end_date_str", envir = .GlobalEnv))
|
|
} else {
|
|
# Default to current date if no argument is provided
|
|
end_date <- Sys.Date()
|
|
end_date <- "2026-01-01" # Default date for testing
|
|
message("No end_date provided. Using current date: ", format(end_date))
|
|
}
|
|
|
|
# Process offset argument with default
|
|
if (length(args) >= 2 && !is.na(args[2])) {
|
|
offset <- as.numeric(args[2])
|
|
if (is.na(offset) || offset <= 0) {
|
|
message("Invalid offset provided. Using default (7 days).")
|
|
offset <- 7
|
|
}
|
|
} else {
|
|
# Default to 7 days if no argument is provided
|
|
offset <- 7
|
|
message("No offset provided. Using default:", offset, "days")
|
|
}
|
|
|
|
# 3. Initialize project configuration
|
|
# --------------------------------
|
|
|
|
# Detect which data source directory exists (merged_tif or merged_tif_8b)
|
|
laravel_storage <- here::here("laravel_app/storage/app", project_dir)
|
|
data_source <- if (dir.exists(file.path(laravel_storage, "merged_tif_8b"))) {
|
|
message("Detected data source: merged_tif_8b (8-band optimized)")
|
|
"merged_tif_8b"
|
|
} else if (dir.exists(file.path(laravel_storage, "merged_tif"))) {
|
|
message("Detected data source: merged_tif (legacy 4-band)")
|
|
"merged_tif"
|
|
} else {
|
|
message("Warning: No data source found. Using default: merged_tif_8b")
|
|
"merged_tif_8b"
|
|
}
|
|
|
|
# Set global data_source for parameters_project.R
|
|
assign("data_source", data_source, envir = .GlobalEnv)
|
|
|
|
tryCatch({
|
|
source("parameters_project.R")
|
|
source("mosaic_creation_utils.R")
|
|
safe_log(paste("Successfully sourced files from default directory."))
|
|
}, error = function(e) {
|
|
message("Note: Could not open files from default directory (expected on some systems)")
|
|
message("Attempting to source from 'r_app' directory instead...")
|
|
tryCatch({
|
|
source(here::here("r_app", "parameters_project.R"))
|
|
source(here::here("r_app", "mosaic_creation_utils.R"))
|
|
message("✓ Successfully sourced files from 'r_app' directory")
|
|
}, error = function(e) {
|
|
stop("Failed to source required files from both default and 'r_app' directories.")
|
|
})
|
|
})
|
|
|
|
# 4. Generate date range for processing
|
|
# ---------------------------------
|
|
dates <- date_list(end_date, offset)
|
|
safe_log(paste("Processing data for week", dates$week, "of", dates$year))
|
|
|
|
# Create output filename
|
|
file_name_tif <- if (length(args) >= 4 && !is.na(args[4])) {
|
|
as.character(args[4])
|
|
} else {
|
|
paste0("week_", sprintf("%02d", dates$week), "_", dates$year, ".tif")
|
|
}
|
|
|
|
safe_log(paste("Output will be saved as:", file_name_tif))
|
|
|
|
# 5. Create weekly per-tile MAX mosaics
|
|
# ----------------------------------
|
|
|
|
tryCatch({
|
|
safe_log("Starting per-tile mosaic creation...")
|
|
|
|
# Set output directory for per-tile mosaics
|
|
tile_output_base <- file.path(laravel_storage, "weekly_tile_max")
|
|
|
|
created_tile_files <- create_weekly_mosaic_from_tiles(
|
|
dates = dates,
|
|
merged_final_dir = merged_final,
|
|
tile_output_dir = tile_output_base,
|
|
field_boundaries = field_boundaries
|
|
)
|
|
|
|
safe_log(paste("✓ Per-tile mosaic creation completed - created",
|
|
length(created_tile_files), "tile files"))
|
|
}, error = function(e) {
|
|
safe_log(paste("ERROR in mosaic creation:", e$message), "WARNING")
|
|
traceback()
|
|
stop("Mosaic creation failed")
|
|
})
|
|
}
|
|
|
|
if (sys.nframe() == 0) {
|
|
main()
|
|
}
|
|
|