# Print a summary of the loaded data for the user, for each object.
# data_cube: An s2dv_cube returned by one of the functions in Auto-S2S
# recipe: The Auto-S2S recipe as returned by read_yaml()

## TODO: Adapt to daily/subseasonal cases
## TODO: Add check for missing files/NAs by dimension

data_summary <- function(data_cube, recipe) {
  # Get name, leadtime months and date range
  object_name <- deparse(substitute(data_cube))
  if (recipe$Analysis$Variables$freq == "monthly_mean") {
    date_format <- "%b %Y"
  } else if (recipe$Analysis$Variables$freq %in%
             c("daily", "daily_mean", "weekly_mean")) {
    date_format <- "%b %d %Y"
  }
  months <- unique(format(as.Date(data_cube$attrs$Dates), format = '%B'))
  months <- paste(as.character(months), collapse=", ")
  sdate_min <- format(min(as.Date(data_cube$attrs$Dates), na.rm = TRUE),
                      format = date_format)
  sdate_max <- format(max(as.Date(data_cube$attrs$Dates), na.rm = TRUE),
                      format = date_format)
  # Log the summary
  info(recipe$Run$logger, retrieve = TRUE, "DATA SUMMARY:")
  if (recipe$Analysis$Variables$freq == "monthly_mean") {
    info(recipe$Run$logger, retrieve = TRUE,paste(object_name, "months:", months))
  } else if (recipe$Analysis$Variables$freq == "weekly_mean") {
    sdate <- recipe$Analysis$Time$sdate
    ftime_min <- recipe$Analysis$Time$ftime_min
    ftime_max <- recipe$Analysis$Time$ftime_max
    week_valid_ini <- ymd(sdate) + (1 - wday(ymd(sdate), week_start = 1)) + 
      (ftime_min:ftime_max)*7
    info(recipe$Run$logger, retrieve = TRUE,
         paste(object_name, "corresponding forecast times:",
         paste(week_valid_ini, collapse = ' ')))
  } # Should we include daily summaries?
  info(recipe$Run$logger, retrieve = TRUE,
       paste(object_name, "range:", sdate_min, "to", sdate_max))
  info(recipe$Run$logger, retrieve = TRUE,
       paste(object_name, "dimensions:"))
  # Use capture.output() and for loop to display results neatly
  output_string <- capture.output(dim(data_cube$data))
  for (i in output_string) {
    info(recipe$Run$logger, retrieve = TRUE, i)
  }
  # Print statistical summary of the data for every variable
  info(recipe$Run$logger, retrieve = TRUE, 
       paste0("Statistical summary of the data in ", object_name, ":"))
  for (var_index in 1:data_cube$dims[['var']]) {
    variable_name <- data_cube$attrs$Variable$varName[var_index]
    variable_units <- data_cube$attrs$Variable$metadata[[variable_name]]$units
    info(recipe$Run$logger, retrieve = TRUE,
         paste0("Variable: ", variable_name,
                " (units: ", variable_units, ")"))
    output_string <- capture.output(summary(Subset(data_cube$data,
                                                   along = "var",
                                                   indices = var_index)))
    for (i in output_string) {
      info(recipe$Run$logger, retrieve = TRUE, i)
    }
  }
  info(recipe$Run$logger, retrieve = TRUE,
       "---------------------------------------------")
  invisible(gc())
}

