add_logo <- function(recipe, logo, logo_resize_percentage = 0.25) {
  # recipe: SUNSET recipe
  # logo: URL to the logo
  system <- list.files(paste0(recipe$Run$output_dir, "/plots/"))

  files <- lapply(system, function(x) {
                  f <- list.files(paste0(recipe$Run$output_dir, "/plots/",
                                         system, "/"),
                                  recursive = TRUE,
                                  full.names = TRUE)
                                      })[[1]]
  dim(files) <- c(file = length(files))

  Apply(list(files), target_dims = NULL,
	fun = function(x, logo, logo_resize_percentage) {
	 fig_width <- as.numeric(system(paste("identify -format '%w'", x), 
					intern = TRUE))
	logo_height <- as.numeric(system(paste("convert", logo, 
		"-resize", fig_width * 0.1, "-format '%h' info:", sep=" "),
					 intern = TRUE))
         system(paste0("convert ", x, 
		       " -gravity south -background white -splice 0x",
		       logo_height, " extended_fig.png"))
         system(paste0("convert extended_fig.png \\( ", 
		       logo, " -resize ", fig_width * logo_resize_percentage,
		       " \\) -gravity southeast -composite ",
		       x))
          }, 
	 logo = logo,
	 logo_resize_percentage = logo_resize_percentage,
       	ncores = 1)
  file.remove("extended_fig.png")
}


#fig <- "/home/bsc/bsc032339/recipe_tas_20241106160039/plots/Meto-France-System8/ERA5/evmos/tas/forecast_most_likely_tercile-20240801_ft01.png"

#fig <- "/home/bsc/bsc032339/recipe_tas_20241106160039/plots/Meto-France-System8/ERA5/evmos/tas/forecast_ensemble_median-20240801_ft01.png"

#fig <- "/home/bsc/bsc032339/recipe_tas_20241106160039/plots/Meto-France-System8/ERA5/evmos/tas/rpss-august_ft01.png"

#fig_width <- as.numeric(system(paste("identify -format '%w'", fg), intern = TRUE))

#logo <- "rsz_rsz_bsc_logo.png"
#logo_height <- as.numeric(system(paste("convert", logo, "-resiz", fig_width * 0.1, "-format '%h' info:", sep=" "), intern = TRUE))

#system(paste0("convert ", fig, " -gravity south -background whie -splice 0x", logo_height, " extended_fig.png"))

#system(paste0("convert extended_fig.png \\( ", logo, " -resize , map_width * 0.25, " \\) -gravity southeast -composite extended_fig.png"))
