-
Notifications
You must be signed in to change notification settings - Fork 35
/
SS_write.R
146 lines (138 loc) · 3.95 KB
/
SS_write.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' Write all Stock Synthesis input files for a model
#'
#' Writes all the input files for a Stock Synthesis model using the list
#' created by [SS_read()]
#' (presumably after modification of one or more elements)
#' using the `SS_write*()` functions for the four to
#' six model input files.
#'
#' @param inputlist list created by [SS_read()]
#' @template dir
#' @template overwrite
#' @template verbose
#'
#' @author Ian G. Taylor
#'
#' @export
#' @seealso [SS_read()] creates the list that is used by this function.
#' @family read/write functions
#'
#' @examples
#' \dontrun{
#' # read inputlist to modify the data file
#' inputlist <- SS_read(
#' dir = system.file("extdata", "simple_small", package = "r4ss")
#' )
#'
#' # modify the starter file (use the par file)
#' inputlist[["start"]][["init_values_src"]] <- 1
#'
#' # modify the data file (remove age comps from years prior to 1990)
#' inputlist[["dat"]][["agecomp"]] <- inputlist[["dat"]][["agecomp"]] %>%
#' dplyr::filter(Yr >= 1990)
#'
#' # modify the control file (turn off early recdevs and change range of yrs)
#' inputlist[["ctl"]][["recdev_early_phase"]] <-
#' -abs(inputlist[["ctl"]][["recdev_early_phase"]])
#' inputlist[["ctl"]][["MainRdevYrFirst"]] <- 1980
#'
#' # write the files to a new folder within the source directory
#' SS_write(
#' inputlist = inputlist,
#' dir = file.path(inputlist[["dir"]], "modified_inputs")
#' )
#' }
SS_write <- function(inputlist,
dir = "",
overwrite = FALSE,
verbose = FALSE) {
# check for contents of inputlist
check_inputlist(inputlist)
# create directory if not already there
if (dir != "") {
if (!dir.exists(dir)) {
if (verbose) {
message("Creating new directory: ", dir)
}
dir.create(dir, recursive = TRUE)
}
}
# write starter file
if ("start" %in% names(inputlist)) {
r4ss::SS_writestarter(
mylist = inputlist[["start"]],
dir = dir,
overwrite = overwrite,
verbose = verbose
)
} else {
stop(
'"start" element of input list not available ',
"(required to get names of data and control files)"
)
}
# write data file
if ("dat" %in% names(inputlist)) {
r4ss::SS_writedat(
datlist = inputlist[["dat"]],
outfile = file.path(dir, inputlist[["start"]][["datfile"]]),
overwrite = overwrite,
verbose = verbose
)
}
# write control file
if ("ctl" %in% names(inputlist)) {
r4ss::SS_writectl(
ctllist = inputlist[["ctl"]],
outfile = file.path(dir, inputlist[["start"]][["ctlfile"]]),
overwrite = overwrite,
verbose = verbose
)
}
# write forecast file
if ("fore" %in% names(inputlist)) {
r4ss::SS_writeforecast(
mylist = inputlist[["fore"]],
dir = dir,
overwrite = overwrite,
verbose = verbose
)
}
# write wtatage file (if needed)
if ("wtatage" %in% names(inputlist) &
inputlist[["ctl"]][["EmpiricalWAA"]]) {
r4ss::SS_writewtatage(
mylist = inputlist[["wtatage"]],
dir = dir,
overwrite = overwrite,
verbose = verbose
)
}
# only write par file if present in list
if (!is.null(inputlist[["par"]])) {
# assume default name if not present--would have been added by SS_read()
if (is.null(inputlist[["par"]][["parfile"]])) {
inputlist[["par"]][["parfile"]] <- "ss3.par"
}
try(
{
if (inputlist[["ctl"]][["ReadVersion"]] == "3.24") {
par <- r4ss::SS_writepar_3.24(inputlist[["par"]],
outfile = file.path(dir, inputlist[["par"]][["parfile"]]),
verbose = verbose
)
} else {
par <- r4ss::SS_writepar_3.30(inputlist[["par"]],
outfile = file.path(dir, inputlist[["par"]][["parfile"]]),
verbose = verbose
)
}
},
silent = !verbose
)
}
# message noting that all files have been written
if (verbose) {
message("Wrote all input files to ", dir)
}
}