R/fdk_setup_sofun.R

Defines functions fdk_setup_sofun

Documented in fdk_setup_sofun

#' Complements the setup settings
#'
#' Complements the settings based on the site meta info CSV file or data frame.
#'
#' @param site_info A character string specifying the path to the site meta
#' info file, or a dataframe containing the site meta info.
#' @param params_siml A named list containing the simulation parameters
#'  for SOFUN.
#'
#' @return A dataframe (tibble) containing the site meta info,
#' complemented by column 'params_siml' which is a nested list
#' of complemented simulation parameters.
#' @export
#' @examples
#' \dontrun{
#'  setup <- prepare_setup_sofun(
#'    site_info = site_info,
#'    params_siml = params_siml
#'   )
#' }

fdk_setup_sofun <- function(
  site_info,
  params_siml
  ){

  # predefine variables for CRAN check compliance
  year_end <- year_start <- . <- NULL

  ##-----------------------------------------------------------
  ## Ensemble: multiple site-scale simulations that "go together"
  ## In this case, <settings$name> is the name of the ensemble
  ## (e.g., "fluxnet2015")
  ##-----------------------------------------------------------
  ## Read info that varies between sites from the meta information
  ## file <site_info>:
  ## - site name, must be: column number 1
  ## - longitude of site, column must be named 'lon'
  ## - latitude of site, column must be named 'lat'
  ## - elevation of site, column must be named 'elv'
  ## - years for which simulation is to be done
  ## (corresponding to data availability from site),
  ##   requires two columns named 'year_start' and 'year_end'.
  if (is.character(site_info)){
    if (!file.exists(site_info)){
      stop( "prepare_setup_sofun():
            Path specified by site_info does not exist." )
    }
    site_info <- utils::read.csv2( site_info )
  }

  ##--------------------------------------
  ## Complement settings with meta info for each site
  ##--------------------------------------
  site_info <- site_info |>

    ## clean up and complement
    mutate(year_start = as.numeric(year_start),
           year_end = as.numeric(year_end)) |>
    mutate(date_start = lubridate::ymd(
             paste0( as.character( site_info$year_start ), "-01-01" ) ),
           date_end   = lubridate::ymd(
             paste0( as.character( site_info$year_end ), "-12-31" ) ))

  ## add simulation parameters as a list nested in 'site_info'
  site_info <- params_siml |>
    dplyr::bind_cols(.) |>
    dplyr::mutate(tmp = 1) |>
    dplyr::right_join(mutate(site_info, tmp = 1), by = "tmp") |>
    dplyr::select(names(site_info), names(params_siml)) |>
    dplyr::mutate(nyeartrend = year_end - year_start + 1) |>
    dplyr::rename(firstyeartrend = year_start) |>
    tidyr::nest(params_siml = c(names(params_siml),
                                "firstyeartrend", "nyeartrend"))

  return(site_info)

}
computationales/flux_data_kit documentation built on Feb. 23, 2025, 8:19 p.m.