R/PrintPlotChrono.R

Defines functions PrintPlotChrono

PrintPlotChrono <- function(crn,
                            rwl = NULL,
                            file.name = "crono",
                            crono.type = "RAW",
                            saveCronoJpg = TRUE) {
  FILE_NAME <- paste(file.name, crono.type, sep = "")
  CRONO_NAME <- paste(file.name, crono.type, sep = "-")
  FILE_NAME_CSV <- paste(file.name, crono.type, ".CSV", sep = "")
  CURRENT_DIRECTORY <- getwd()

  if (!is.null(rwl)) {
    rwl <- rwl[crn[, ncol(crn)] > 0, ]
  }
  crn <- crn[crn[, ncol(crn)] > 0, ]

  CRN <- crn[, c(1, ncol(crn) - 1)]

  crn[, 1:(ncol(crn) - 2)] <- round(crn[, 1:(ncol(crn) - 2)], 3)
  WriteMatrix(
    crn,
    File = FILE_NAME_CSV,
    sep = ";",
    row.names = TRUE,
    row.name = "Year",
    ID = FALSE,
    col.width = 4
  ) # Save CSV

  write.crn(CRN, paste(FILE_NAME, ".CRN", sep = "")) # Save CRN
  # Save Chrono JPG
  jpeg(
    paste(CRONO_NAME, ".jpg", sep = ""),
    width = 600,
    height = 600,
    quality = 100
  )
  PlotChron(crn)
  dev.off()

  if (!is.null(rwl)) {
    jpeg(
      paste(CRONO_NAME, "-series.jpg", sep = ""),
      width = 600,
      height = 600,
      quality = 100
    )
    yr.vec <- as.numeric(rownames(rwl))
    matplot(
      yr.vec,
      rwl,
      ylim = c(0, max(rwl, na.rm = TRUE)),
      type = "l",
      xlab = "Years",
      ylab = "",
      main = file.name,
      las = 1,
      col = "grey20",
      lty = 1,
      lwd = 1
    )
    lines(yr.vec, crn[, 1], col = "red", lwd = 2)
    dev.off()
  }

  setwd(dirname(getwd()))
  dir.create(paste("Cronos", crono.type, sep = "-"), showWarnings = FALSE)
  setwd(paste("Cronos", crono.type, sep = "-"))
  write.crn(CRN, paste(file.name, crono.type, ".CRN", sep = ""))
  setwd(CURRENT_DIRECTORY)
}

Try the detrendeR package in your browser

Any scripts or data that you put into this service are public.

detrendeR documentation built on April 16, 2022, 1:06 a.m.