Enhance logging functionality and update report generation scripts

- Improved safe_log function to include timestamps and conditional logging
- Added diagnostic messages for field visualization processing
- Updated CI map rendering parameters for consistency
- Refined raster mapping functions in report_utils for clarity
- Added .png files to .gitignore
This commit is contained in:
DimitraVeropoulou 2026-02-16 10:21:55 +01:00
parent 684050d459
commit a9840171cb
4 changed files with 62 additions and 13 deletions

View file

@ -41,3 +41,6 @@ dist/
*.swo
*.swp
*.png

View file

@ -48,10 +48,30 @@
# #' safe_log("Check input file", "WARNING")
# #' safe_log("Failed to load data", "ERROR")
# #'
# safe_log <- function(message, level = "INFO") {
# prefix <- sprintf("[%s]", level)
# cat(sprintf("%s %s\n", prefix, message))
# }
safe_log <- function(message, level = "INFO") {
# Build the full log message with timestamp
timestamp <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
log_msg <- sprintf("[%s] [%s] %s", timestamp, level, message)
# Only output to console if NOT rendering with knitr
if (!isTRUE(getOption('knitr.in.progress'))) {
cat(log_msg, "\n")
}
# Always write to log file if available
if (exists("LOG_FILE", envir = .GlobalEnv)) {
log_file <- get("LOG_FILE", envir = .GlobalEnv)
if (!is.null(log_file) && nzchar(log_file)) {
tryCatch({
cat(log_msg, "\n", file = log_file, append = TRUE)
}, error = function(e) {
# Silently fail if log file can't be written
})
}
}
invisible(log_msg)
}
# #' SmartCane Debug Logging (Conditional)
# #'

View file

@ -841,7 +841,7 @@ tryCatch({
### Chlorophyll Index (CI) Overview Map - Current Week
```{r render_farm_ci_map, echo=FALSE, fig.height=5.5, fig.width=6.5, dpi=150, dev='png', message=FALSE, warning=FALSE}
```{r render_farm_ci_map, echo=FALSE, message=FALSE, warning=FALSE, fig.height=5.5, fig.width=6.5, dpi=150, dev='png', message=FALSE, warning=FALSE}
# Create farm-level chlorophyll index map with OpenStreetMap basemap
tryCatch({
if (!is.null(farm_ci_current_ll)) {
@ -1075,7 +1075,31 @@ This section provides detailed, field-specific analyses including chlorophyll in
```{r generate_field_visualizations, eval=TRUE, fig.height=3.8, fig.width=6.5, dpi=150, dev='png', message=TRUE, echo=FALSE, warning=TRUE, include=TRUE, results='asis'}
# Generate detailed visualizations for each field using purrr::walk
# DIAGNOSTIC MODE - Remove this after debugging
cat("\n## DIAGNOSTIC: Starting field visualization processing\n\n")
tryCatch({
# Check prerequisites
cat("- Fields to process:", nrow(AllPivots_merged), "\n")
cat("- Field names:", paste(AllPivots_merged$field, collapse = ", "), "\n")
cat("- Weekly mosaic directory:", weekly_CI_mosaic, "\n")
cat("- CI quadrant data available:", !is.null(CI_quadrant), "\n")
cat("- Harvesting data available:", !is.null(harvesting_data), "\n\n")
# Check if ci_plot function exists
if (!exists("ci_plot")) {
cat("**ERROR: ci_plot() function not found!**\n\n")
stop("ci_plot function missing")
}
if (!exists("cum_ci_plot")) {
cat("**ERROR: cum_ci_plot() function not found!**\n\n")
stop("cum_ci_plot function missing")
}
cat("- ci_plot() function:", "FOUND", "\n")
cat("- cum_ci_plot() function:", "FOUND", "\n\n")
# Prepare merged field list and week/year info
AllPivots_merged <- AllPivots0 %>%
dplyr::filter(!is.na(field), !is.na(sub_field)) %>%

View file

@ -64,9 +64,10 @@ create_CI_map <- function(pivot_raster, pivot_shape, pivot_spans, show_legend =
map <- tm_shape(pivot_raster, unit = "m")
# Add raster with continuous spectrum (fixed scale 8-1 for consistent comparison, reversed)
map <- map + tm_raster(col.scale = tm_scale_continuous(values = palette,
map <- map + tm_raster("CI",
col_scale = tm_scale_continuous(values = palette,
limits = c(1,8)),
col.legend = tm_legend(title = "CI",
col_legend = tm_legend(title = "CI",
orientation = if(legend_is_portrait) "portrait" else "landscape",
show = show_legend,
position = if(show_legend) tm_pos_out("left", "center") else c("left", "bottom"),
@ -135,10 +136,11 @@ create_CI_diff_map <- function(pivot_raster, pivot_shape, pivot_spans, show_lege
map <- tm_shape(pivot_raster, unit = "m")
# Add raster with continuous spectrum (centered at 0 for difference maps, fixed scale, reversed)
map <- map + tm_raster(col.scale = tm_scale_continuous(values = palette,
map <- map + tm_raster("CI",
col_scale = tm_scale_continuous(values = palette,
midpoint = 0,
limits = c(-3, 3)),
col.legend = tm_legend(title = "CI diff.",
col_legend = tm_legend(title = "CI diff.",
orientation = if(legend_is_portrait) "portrait" else "landscape",
show = show_legend,
position = if(show_legend) tm_pos_out("right", "center") else c("left", "bottom"),