R/create.report.R

Defines functions create.report

Documented in create.report

#'@title Create a PDF report for a vertical profile performed using an optical package
#'
#'@description
#'A PDF report is generated using knitr package facilities and a *.Rnw
#'template. It provides an efficent mean to visualise the data.
#'It can be called from the higher level function \code{\link{IOPs.go}}, or
#'run in the command line as along as IOPs.RData and IOPs.fitted.down.RData have been
#'generated by \code{\link{correct.merge.IOP.profile}}.
#'
#'@param dirdat is the current directory path contaning the RData file to plot.
#'
#'@details
#'The program edit a template named IOP_Station_TEMPLATE.Rnw located in the package data folder.
#'It will extract a number of informations from the current
#'data directory and RData files, cast.info.dat and instruments.dat files. For example,
#'the Latitude/Longitude are extracted from the cast.info.dat file to
#'create a map (Figure 1 of the report).
#'Next it uses the brew package to update the Rnw files.
#'
#'The Rnw files are converted into PDF using knitr package. Both files will
#'be saved in dirdat and a "figures" folder is created with
#'a PDF file for each figures found in the report.
#'
#'@seealso \code{\link{correct.merge.IOP.profile}}, \code{\link{IOPs.go}}
#'@author Simon Belanger
#'@export

create.report <- function(dirdat){

  report.template  <- paste( Sys.getenv("R_IOPs_DATA_DIR"), "IOP_Station_TEMPLATE.Rnw", sep = "/")

  print("Use knirt report template located at:")
  print(report.template)

  # get information for the report
  # extract Station name and date from the directory name
  res=unlist(strsplit(dirdat, "/"))
  for (i in 1:length(res)){
    xx = str_locate(res[i], "_Station")
    if (!is.na(xx[1])) {
      datestation=unlist(strsplit(res[i], "_Station"))
      # Remove "_" from station name to avoid error with Latex
      if (str_detect(datestation[2], "_")) {
        yy = unlist(strsplit(datestation[2], "_"))

        Station = paste(yy, collapse=" ")
      }   else Station =   datestation[2]
      Date = datestation[1]
    }
  }

    # Extract data from the cast file
    # Cast info file
    cast.info.file <- paste(dirdat, "cast.info.dat", sep = "/")
    if(!file.exists(cast.info.file)) {
      print(paste("PROBLEM: no cast.info.dat file found in ", dirdat))
      return(0)
    }
    instrument.file <- paste(dirdat, "instrument.dat", sep = "/")
    if(!file.exists(instrument.file)) {
      print(paste("PROBLEM: no instrument.dat file found in ", dirdat))
      return(0)
    }

    instrument <- read.instrument(instrument.file)
    if (!is.null(instrument$HS6)) HS6 = instrument$HS6 else HS6 = 0
    if (!is.null(instrument$FLECO)) FLECO = instrument$FLECO else FLECO = 0
    if (!is.null(instrument$ASPH)) ASPH = instrument$ASPH else ASPH = 0
    if (!is.null(instrument$ACS)) ACS = instrument$ACS else ACS = 0
    if (!is.null(instrument$BB9)) BB9 = instrument$BB9 else BB9 = 0
    if (!is.null(instrument$LISST)) LISST = instrument$LISST else LISST = 0
    if (!is.null(instrument$BB3)) BB3 = instrument$BB3 else BB3 = 0
    if (!is.null(instrument$FLBBCD)) FLBBCD = instrument$FLBBCD else FLBBCD = 0
    if (!is.null(instrument$FLCHL)) FLCHL = instrument$FLCHL else FLCHL = 0

    cast.info <- read.cast.info(cast.info.file)
    lon = cast.info$lon
    lat = cast.info$lat
    maxbb = cast.info$maxbb
    Ndepth.to.plot = cast.info$Ndepth.to.plot

  reportname <- "IOPs.plot.Rnw"
  brew(report.template,reportname)

  knit2pdf(reportname, quiet = T)

  file.remove("IOPs.plot.log")
  file.remove("IOPs.plot.aux")
  file.remove("IOPs.plot.tex")

  return(1)
}
belasi01/Riops documentation built on Sept. 5, 2022, 6:38 p.m.