154 lines
4.7 KiB
R
154 lines
4.7 KiB
R
# CREATE_ALL_WEEKLY_MOSAICS.R
|
|
# ===========================
|
|
# Generate weekly mosaics for all available weeks in the merged_final_tif dataset
|
|
# This script identifies all unique weeks from the TIF files and creates mosaics
|
|
# for weeks that don't already have mosaics.
|
|
|
|
suppressPackageStartupMessages({
|
|
library(terra)
|
|
library(sf)
|
|
library(dplyr)
|
|
library(lubridate)
|
|
library(here)
|
|
})
|
|
|
|
# Set project directory
|
|
project_dir <- "esa"
|
|
assign("project_dir", project_dir, envir = .GlobalEnv)
|
|
|
|
# Source required files
|
|
cat("Loading project configuration...\n")
|
|
source(here("r_app", "parameters_project.R"))
|
|
source(here("r_app", "mosaic_creation_utils.R"))
|
|
|
|
# Get all TIF files from merged_final_tif
|
|
merged_final_dir <- here("laravel_app/storage/app", project_dir, "merged_final_tif")
|
|
tif_files <- list.files(merged_final_dir, pattern = "\\.tif$", full.names = FALSE)
|
|
|
|
cat("Found", length(tif_files), "TIF files\n")
|
|
|
|
# Extract dates from filenames
|
|
dates <- as.Date(gsub("\\.tif$", "", tif_files))
|
|
cat("Date range:", as.character(min(dates)), "to", as.character(max(dates)), "\n")
|
|
|
|
# Create a data frame with week and year for each date
|
|
weeks_df <- data.frame(
|
|
date = dates,
|
|
week = isoweek(dates),
|
|
year = isoyear(dates)
|
|
) %>%
|
|
# Get unique weeks
|
|
distinct(week, year) %>%
|
|
arrange(year, week) %>%
|
|
# Create a representative date for each week (middle of the week)
|
|
mutate(
|
|
# Calculate the Monday of each ISO week
|
|
week_start = as.Date(paste0(year, "-01-01")) + weeks((week - 1)),
|
|
# Adjust to ensure it's actually the correct week
|
|
week_start = week_start - wday(week_start, week_start = 1) + 1,
|
|
# Use Wednesday as the representative date (middle of week)
|
|
representative_date = week_start + 2
|
|
)
|
|
|
|
cat("Total unique weeks:", nrow(weeks_df), "\n")
|
|
|
|
# Check which mosaics already exist
|
|
weekly_mosaic_dir <- here("laravel_app/storage/app", project_dir, "weekly_mosaic")
|
|
existing_mosaics <- list.files(weekly_mosaic_dir, pattern = "^week_.*\\.tif$", full.names = FALSE)
|
|
existing_weeks <- gsub("^week_|.tif$", "", existing_mosaics) %>%
|
|
strsplit("_") %>%
|
|
lapply(function(x) data.frame(week = as.integer(x[1]), year = as.integer(x[2]))) %>%
|
|
bind_rows()
|
|
|
|
cat("Existing mosaics:", nrow(existing_weeks), "\n")
|
|
|
|
# Find missing weeks
|
|
weeks_df <- weeks_df %>%
|
|
anti_join(existing_weeks, by = c("week", "year")) %>%
|
|
arrange(year, week)
|
|
|
|
cat("Missing mosaics:", nrow(weeks_df), "\n")
|
|
cat("\n")
|
|
|
|
if (nrow(weeks_df) == 0) {
|
|
cat("All mosaics already exist!\n")
|
|
quit(save = "no", status = 0)
|
|
}
|
|
|
|
# Ask for confirmation
|
|
cat("This will create", nrow(weeks_df), "weekly mosaics.\n")
|
|
cat("Estimated time: ~", round(nrow(weeks_df) * 30 / 60, 1), "minutes (assuming 30 seconds per mosaic)\n")
|
|
cat("\nProcessing will begin in 5 seconds... (Ctrl+C to cancel)\n")
|
|
Sys.sleep(5)
|
|
|
|
# Create mosaics for each missing week
|
|
cat("\n=== Starting mosaic creation ===\n\n")
|
|
success_count <- 0
|
|
error_count <- 0
|
|
error_weeks <- list()
|
|
|
|
for (i in 1:nrow(weeks_df)) {
|
|
week_info <- weeks_df[i, ]
|
|
|
|
cat(sprintf("[%d/%d] Creating mosaic for week %02d of %d...",
|
|
i, nrow(weeks_df), week_info$week, week_info$year))
|
|
|
|
tryCatch({
|
|
# Use the representative date (Wednesday of the week) with 7-day offset
|
|
end_date <- week_info$representative_date
|
|
offset <- 7 # Look back 7 days to cover the whole week
|
|
|
|
# Generate date range
|
|
dates <- date_list(end_date, offset)
|
|
|
|
# Create output filename
|
|
file_name_tif <- sprintf("week_%02d_%d.tif", week_info$week, week_info$year)
|
|
|
|
# Create the mosaic
|
|
output_file <- create_weekly_mosaic(
|
|
dates = dates,
|
|
field_boundaries = field_boundaries,
|
|
daily_vrt_dir = daily_vrt,
|
|
merged_final_dir = merged_final_dir,
|
|
output_dir = weekly_mosaic_dir,
|
|
file_name_tif = file_name_tif,
|
|
create_plots = FALSE # Disable plots for batch processing
|
|
)
|
|
|
|
if (file.exists(output_file)) {
|
|
cat(" ✓\n")
|
|
success_count <- success_count + 1
|
|
} else {
|
|
cat(" ✗ (file not created)\n")
|
|
error_count <- error_count + 1
|
|
error_weeks[[length(error_weeks) + 1]] <- week_info
|
|
}
|
|
|
|
}, error = function(e) {
|
|
cat(" ✗\n")
|
|
cat(" Error:", e$message, "\n")
|
|
error_count <- error_count + 1
|
|
error_weeks[[length(error_weeks) + 1]] <- week_info
|
|
})
|
|
|
|
# Progress update every 10 mosaics
|
|
if (i %% 10 == 0) {
|
|
cat(sprintf("\nProgress: %d/%d completed (%.1f%%) | Success: %d | Errors: %d\n\n",
|
|
i, nrow(weeks_df), (i/nrow(weeks_df))*100, success_count, error_count))
|
|
}
|
|
}
|
|
|
|
# Final summary
|
|
cat("\n=== SUMMARY ===\n")
|
|
cat("Total weeks processed:", nrow(weeks_df), "\n")
|
|
cat("Successful:", success_count, "\n")
|
|
cat("Errors:", error_count, "\n")
|
|
|
|
if (error_count > 0) {
|
|
cat("\nWeeks with errors:\n")
|
|
error_df <- bind_rows(error_weeks)
|
|
print(error_df)
|
|
}
|
|
|
|
cat("\nDone!\n")
|