source("modules/Loading/R/get_regrid_params.R")
# Load required libraries/funs
source("tools/CST_ChangeDimName.R")
source("modules/Loading/R/compare_exp_obs_grids.R")
source("modules/Loading/R/get_regrid_params.R")

load_sample <- function(recipe) {
  # Hindcast:
  # Change hcst dimension names
  hcst <- CST_ChangeDimName(lonlat_temp_st$exp,
                            original_dimnames = c("dataset", "sdate", "ftime",
                                                  "lat", "lon", "member"),
                            final_dimnames = c("dat", "syear", "time",
                                               "latitude", "longitude",
                                               "ensemble"))
  # Add sday and sweek dimensions
  hcst <- CST_InsertDim(hcst, posdim = 3, lendim = 1, name = "sday", values = 1)
  hcst <- CST_InsertDim(hcst, posdim = 4, lendim = 1, name = "sweek", values = 1)
  dim(hcst$attrs$Dates) <- c(sday = 1, sweek = 1, 
                             syear = dim(hcst$attrs$Dates)[['syear']],
                             time = dim(hcst$attrs$Dates)[['time']])
  # Observations:
  # Change obs dimension names
  obs <- CST_ChangeDimName(lonlat_temp_st$obs,
                           original_dimnames = c("dataset", "sdate", "ftime",
                                                 "lat", "lon"),
                           final_dimnames = c("dat", "syear", "time",
                                              "latitude", "longitude"))
  # Add sday and sweek dimensions
  obs <- CST_InsertDim(obs, posdim = 3, lendim = 1, name = "sday", values = 1)
  obs <- CST_InsertDim(obs, posdim = 4, lendim = 1, name = "sweek", values = 1)
  dim(obs$attrs$Dates) <- c(sday = 1, sweek = 1,
                            syear = dim(obs$attrs$Dates)[['syear']],
                            time = dim(obs$attrs$Dates)[['time']])
  # Add ensemble dimension to obs
  obs <- CST_InsertDim(obs, posdim = 7, lendim = 1, name = "ensemble", values = 1)
  # Adjust name of 'load_parameters$date' attribute
  obs$attrs$load_parameters$dat1$file_date <- obs$attrs$load_parameters$dat1$date
  
  # Sample fcst NULL
  fcst <- NULL
  # Check for consistency between hcst and obs grid
  compare_exp_obs_grids(hcst, obs)
  
  info(recipe$Run$logger,
       "##### DATA LOADING COMPLETED SUCCESSFULLY #####")
  .log_memory_usage(recipe$Run$logger, when = "After loading (sample data)")  
  return(list(hcst = hcst, fcst = fcst, obs = obs))
}
