R/utils.R

Defines functions get_data guess_ext guess_ctl triage_ctl parsed_ctl ctl_lines

ctl_lines <- function(loc) {
  file <- guess_ctl(loc)
  if(is.null(file)) return(character(0))
  return(readLines(file))
}

parsed_ctl <- function(loc) {
  parsed <- parse_ctl(ctl_lines(loc))
  parsed[["meta"]] <- list()
  parsed[["meta"]][["path"]] <- loc
  parsed <- triage_ctl(parsed)
  parsed
}

triage_ctl <- function(parsed) {
  parsed$meta$nest <- length(grep("^EST", names(parsed)))
  parsed$meta$nsims <- length(grep("^SIM", names(parsed)))
  parsed$meta$ntable <- length(grep("^TAB", names(parsed)))
  parsed$meta$cov <- any(grepl("^COV", names(parsed)))
  parsed
}

guess_ctl <- function(loc) {
  run <- basename(loc)
  guess <- file.path(loc, paste0(run, ".ctl"))
  if(file.exists(guess)) return(guess)
  return(NULL)
}

guess_ext <- function(loc) {
  run <- basename(loc)
  guess <- file.path(loc, paste0(run, ".ext"))
  if(file.exists(guess)) return(guess)
  stop("couldn't guess ext file name")
}

get_data <- function(parsed) {
  cwd <- getwd()
  on.exit(setwd(cwd))
  setwd(parsed$meta$path)
  data <- trimws(parsed$DATA)
  data <- strsplit(data, "\\s+")[[1]]
  data <- normalizePath(data[[1]], mustWork=FALSE)
  parsed$meta$data <- data
  parsed
}
kylebaron/nmlist documentation built on Dec. 21, 2021, 8:45 a.m.