Skip to content

Commit

Permalink
Uses r4ss::SS_read()
Browse files Browse the repository at this point in the history
  • Loading branch information
kellijohnson-NOAA committed May 14, 2023
1 parent 1871e34 commit f5e133d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
49 changes: 18 additions & 31 deletions R/create_em.R
Expand Up @@ -51,33 +51,26 @@
#' create_em()
#' # The necessary files are in the following folder
#' dir(file.path(getwd(), "new-em"))
#' \dontshow{
#' testthat::expect_true("forecast.ss" %in% dir("new-em"),
#' label = "forecast file is in 'new-em'"
#' )
#' testthat::expect_true("starter.ss" %in% dir("new-em"),
#' label = "starter file is in 'new-em'"
#' )
#' testthat::expect_true("codEM.ctl" %in% dir("new-em"),
#' label = "codEM file is in 'new-em'"
#' )
#' }
#' # Clean up your directory
#' unlink(file.path(getwd(), "new-em"), recursive = TRUE)
create_em <- function(dir_in = system.file("extdata", "models", "cod-om", package = "ss3sim"),
dir_out = file.path(getwd(), "new-em")) {
dir.create(dir_out, showWarnings = FALSE, recursive = TRUE)

# Control file
ctl_name <- dir(dir_in, "ctl")
ctl_name_out <- gsub("OM", "EM", ctl_name, ignore.case = TRUE)
dat_list <- r4ss::SS_readdat(dir(dir_in, pattern = "dat$", full.names = TRUE),
inputs <- r4ss::SS_read(
dir = dir_in,
ss_new = FALSE,
verbose = FALSE
)
ctl_list <- r4ss::SS_readctl(file.path(dir_in, ctl_name),
verbose = FALSE,
use_datlist = TRUE, datlist = dat_list

# Control file
ctl_name_out <- gsub(
"OM",
"EM",
basename(inputs[["ctl"]][["sourcefile"]]),
ignore.case = TRUE
)
dat_list <- inputs[["dat"]]
ctl_list <- inputs[["ctl"]]
ctl_list$MainRdevYrFirst <- dat_list$Nages
ctl_list$recdev_early_start <- floor(dat_list$Nages * -0.5)
ctl_list$recdev_early_phase <- abs(ctl_list$recdev_early_phase)
Expand All @@ -104,22 +97,16 @@ create_em <- function(dir_in = system.file("extdata", "models", "cod-om", packag
)

# Forecast file
forecast_list <- r4ss::SS_readforecast(
file = dir(dir_in, pattern = "forecast", full.names = TRUE),
Nfleets = dat_list$Nfleets, Nareas = dat_list$Nareas,
nseas = dat_list$nseas,
readAll = TRUE, verbose = FALSE
)
r4ss::SS_writeforecast(forecast_list,
r4ss::SS_writeforecast(
inputs[["fore"]],
dir = dir_out,
writeAll = TRUE, overwrite = TRUE, verbose = FALSE
writeAll = FALSE,
overwrite = TRUE,
verbose = FALSE
)

# Starter file
starter_list <- r4ss::SS_readstarter(
file = dir(dir_in, pattern = "starter", full.names = TRUE),
verbose = FALSE
)
starter_list <- inputs[["start"]]
starter_list$datfile <- "ss3.dat"
starter_list$ctlfile <- ctl_name_out
starter_list$cumreport <- 1
Expand Down
11 changes: 0 additions & 11 deletions man/create_em.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/testthat/test-change_em.R
@@ -0,0 +1,31 @@
test_that("change_em works and model is viable", {
om_dir <- system.file(
"extdata", "models", "cod-om",
package = "ss3sim"
)
em_dir <- file.path(tempdir(), "ss3sim")
dir.create(em_dir, showWarnings = FALSE, recursive = TRUE)
create_em(om_dir, em_dir)
expect_true(all(file.exists(
file.path(em_dir, c("codEM.ctl", "forecast.ss", "starter.ss"))
)))
file.copy(
file.path(om_dir, "codOM.dat"),
file.path(em_dir, "ss3.dat"),
overwrite = TRUE
)
file.copy(
file.path(om_dir, "codOM.dat"),
file.path(em_dir, "ss3.dat"),
overwrite = TRUE
)
results <- r4ss::run(
dir = em_dir,
exe = get_bin(),
skipfinished = FALSE,
extras = "-nohess -stopph 0",
verbose = FALSE,
show_in_console = FALSE
)
expect_true(results == "ran model")
})

0 comments on commit f5e133d

Please sign in to comment.