## TODO: remove paths to personal scratchs 
source("modules/Loading/R/get_regrid_params.R")
source("modules/Loading/R/check_latlon.R")

orography_correction <- function(recipe, data) {

  ref.name <- recipe$Analysis$Datasets$Reference$name
  exp.name <- recipe$Analysis$Datasets$System$name
  lats.min <- recipe$Analysis$Region$latmin
  lats.max <- recipe$Analysis$Region$latmax
  lons.min <- recipe$Analysis$Region$lonmin
  lons.max <- recipe$Analysis$Region$lonmax
  
  archive <- read_yaml("conf/archive.yml")[[recipe$Run$filesystem]]

  # Define regrid parameters:
  regrid_params <- get_regrid_params(recipe, archive)
  
  circularsort <- check_latlon(lats.min, lats.max, lons.min, lons.max)
  
  ## Load exp orography
  orography_exp <- Start(dat = archive$System[[exp.name]]$orography, 
                         var = 'orography',  
                         lon = values(list(lons.min, lons.max)),
                         lon_reorder = circularsort,
                         lat = values(list(lats.min, lats.max)),
                         lat_reorder = Sort(),
                         transform = regrid_params$fcst.transform,
                         transform_params = list(grid = regrid_params$fcst.gridtype,
                                                 method = regrid_params$fcst.gridmethod),
                         transform_vars = c('lat','lon'),
                         return_vars = list(lat = NULL, lon = NULL), 
                         synonims = list(lon = c('lon','longitude'),
                                         lat = c('lat','latitude')),
                         num_procs = 1, retrieve = TRUE)
  
  ## Set negative values to zero
  orography_exp <- pmax(orography_exp, 0)
  
  ## load obs orography
  orography_obs <- Start(dat = archive$Reference[[ref.name]]$orography, 
                         var = 'orography',  
                         lon = values(list(lons.min, lons.max)),
                         lon_reorder = circularsort,
                         lat = values(list(lats.min, lats.max)),
                         lat_reorder = Sort(),
                         transform = regrid_params$obs.transform,
                         transform_params = list(grid = regrid_params$obs.gridtype,
                                                 method = regrid_params$obs.gridmethod),
                         transform_vars = c('lat','lon'),
                         return_vars = list(lat = NULL, lon = NULL), 
                         synonims = list(lon = c('lon','longitude'),
                                         lat = c('lat','latitude')),
                         num_procs = 1, retrieve = TRUE)
  
  ## Set negative values to zero
  orography_obs <- pmax(orography_obs, 0)
  
  ## Calculate difference between orographies
  oro_diff <- orography_exp - orography_obs
  
  ## Apply lapse rate factor to correct temperature = -0.0065 K/m (-6.5 K/km)
  oro_obs_corr <- oro_diff * -0.0065
  oro_obs_corr <- Reorder(data = drop(oro_obs_corr), order = c('lat', 'lon'))

  ## Apply correction to obs temperature data
  for(ens in 1:dim(data$obs$data)['ensemble']){
    for(time in 1:dim(data$obs$data)['time']){
      for(syear in 1:dim(data$obs$data)['syear']){
        data$obs$data[,,,,syear,time,,,ens] <- data$obs$data[,,,,syear,time,,,ens] + oro_obs_corr
        
      }
    }  
  }
  
  return(data)
  
}

