R/addToDataChangelog.R

Defines functions addToDataChangelog

Documented in addToDataChangelog

#' addToDataChangelog
#'
#' Prepend data from the given report to the changelog.
#'
#' @param report data.frame as obtained by readRDS("report.rds")
#' @param changelog Path to the changelog file
#' @param versionId The model version identifier, e.g. a release number like 4.9.1 or a date like 2025-02-01
#' @param years For which years the variables should be read and put into the changelog
#' @param variables Which variables to read from the report (e.g. "Emissions|CO2|Land|+|Land-use Change")
#' @param ... Reserved for future expansion.
#' @param maxEntries The maximum number of versionIds to keep in the changelog, the oldest one is removed first.
#' @param roundDigits Numbers are rounded to this many decimal places before being written to the changelog.
#' @return Invisibly, the written changelog as data.frame
#'
#' @author Pascal Sauer
#' @export
addToDataChangelog <- function(report, changelog, versionId, years, variables, ...,
                               maxEntries = 15, roundDigits = 2) {
  x <- report[report$region == "World"
              & report$variable %in% variables
              & report$period %in% years,
              c("variable", "period", "value")]
  notFound <- setdiff(variables, x$variable)
  if (length(notFound) > 0) {
    warning("No data-changelog data found for ", paste(notFound, collapse = ", "))
  }

  # shorten variable names
  variableNames <- names(variables)
  names(variableNames) <- variables
  x$variable <- variableNames[as.character(x$variable)]

  colnames(x)[ncol(x)] <- versionId

  out <- data.frame(version = setdiff(colnames(x), c("variable", "period")))
  for (variableName in variableNames) {
    for (year in years) {
      newColumn <- x[x$variable == variableName & x$period == year, 3]
      newColumn <- round(newColumn, roundDigits)
      out <- cbind(out, newColumn)
      colnames(out)[ncol(out)] <- paste0(variableName, year)
    }
  }

  if (file.exists(changelog)) {
    changelog <- normalizePath(changelog)
    tryCatch({
      out <- rbind(out, read.csv(changelog))
      out <- out[seq_len(min(maxEntries, nrow(out))), ]
    }, error = function(e) {
      renamedTo <- paste0(dirname(changelog), "/old_", basename(changelog))
      file.rename(changelog, renamedTo)
      warning("New data is incompatible with existing data changelog at ", changelog, ". ",
              "Renamed existing changelog to ", renamedTo)
    })
  }
  write.csv(out, changelog, quote = FALSE, row.names = FALSE)
  return(invisible(out))
}
pik-piam/magpie4 documentation built on Feb. 28, 2025, 3:29 p.m.