From a9840171cbc539d7d048e096d6ae7c34e5ace819 Mon Sep 17 00:00:00 2001 From: DimitraVeropoulou Date: Mon, 16 Feb 2026 10:21:55 +0100 Subject: [PATCH] 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 --- python_app/.gitignore | 3 ++ r_app/00_common_utils.R | 28 ++++++++++++--- ..._CI_report_with_kpis_agronomic_support.Rmd | 34 ++++++++++++++++--- r_app/report_utils.R | 10 +++--- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/python_app/.gitignore b/python_app/.gitignore index 7c3d9f1..5199317 100644 --- a/python_app/.gitignore +++ b/python_app/.gitignore @@ -41,3 +41,6 @@ dist/ *.swo *.swp +*.png + + diff --git a/r_app/00_common_utils.R b/r_app/00_common_utils.R index 49a7b58..e7e9a44 100644 --- a/r_app/00_common_utils.R +++ b/r_app/00_common_utils.R @@ -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) # #' diff --git a/r_app/90_CI_report_with_kpis_agronomic_support.Rmd b/r_app/90_CI_report_with_kpis_agronomic_support.Rmd index 33451e9..c2d7d3d 100644 --- a/r_app/90_CI_report_with_kpis_agronomic_support.Rmd +++ b/r_app/90_CI_report_with_kpis_agronomic_support.Rmd @@ -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,15 +1075,39 @@ 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({ - # Prepare merged field list and week/year info - AllPivots_merged <- AllPivots0 %>% + # 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)) %>% dplyr::group_by(field) %>% dplyr::summarise(.groups = 'drop') - # Helper to get week/year from a date - get_week_year <- function(date) { + # Helper to get week/year from a date + get_week_year <- function(date) { list( week = as.numeric(format(date, "%V")), year = as.numeric(format(date, "%G")) diff --git a/r_app/report_utils.R b/r_app/report_utils.R index e7e7888..855d78c 100644 --- a/r_app/report_utils.R +++ b/r_app/report_utils.R @@ -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"),