# Print a summary of the loaded data for the user, for each object.
# object: hindcast, forecast or reference data in s2dv_cube format.
## 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 == "daily_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)), format = date_format)
  sdate_max <- format(max(as.Date(data_cube$attrs$Dates)), format = date_format)
  # Create log instance and sink output to logfile and terminal
  info(recipe$Run$logger, "DATA SUMMARY:")
  info(recipe$Run$logger, paste(object_name, "months:", months))
  info(recipe$Run$logger, paste(object_name, "range:", sdate_min, "to", 
				 sdate_max))
  info(recipe$Run$logger, 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, i)
  }
  info(recipe$Run$logger, paste0("Statistical summary of the data in ",
				 object_name, ":"))
  output_string <- capture.output(summary(data_cube$data))
  for (i in output_string) {
    info(recipe$Run$logger, i)
  }
  info(recipe$Run$logger, "---------------------------------------------")
  invisible(gc())
}
