R/createPLUFile.R

Defines functions createPLUFile

#' @export
#' @import data.table
createPLUFile <- function(dat, placename) {

  if(substr(Sys.getenv("OS"),1,7) == "Windows") {
    # set Windows newline
    newLine <- "\n"
  }
  else {
    # set non-Windows newline
    newLine <- "\r\n"
  }

  dat$precipitation.amount <- format(round(dat$precipitation.amount, 1), nsmall = 1)

  dat$precip_text <- NA_character_

  dat$precip_text <- stringi::stri_pad_left(dat$precipitation.amount, width = 10)

  text = paste0(
    placename, newLine,
    "     1  : Daily records (1=daily, 2=10-daily and 3=monthly data)", newLine,
    stringi::stri_pad_left(lubridate::day(min(dat$date)), width = 6), "  : First day of record (1, 11 or 21 for 10-day or 1 for months)", newLine,
    stringi::stri_pad_left(lubridate::month(min(dat$date)), width = 6), "  : First month of record", newLine,
    "  ", lubridate::year(min(dat$date)), "  : First year of record (1901 if not linked to a specific year)", newLine,
    "", newLine,
    "  Total Rain (mm)", newLine,
    "=======================", newLine,
    paste0(dat$precip_text, collapse = newLine)
  )

  return(text)
}
aWhereAPI/aWhere-AquaCrop documentation built on Feb. 16, 2021, 7:02 p.m.