Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions base/utils/R/write_job_sh.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#' Write a job.sh script for a PEcAn model run
#'
#' This helper function writes the job.sh shell script used to execute
#' a model run. It centralizes duplicated logic previously found in
#' individual write.config.* functions across model packages.
#'
#' @param rundir character. Path to the run directory
#' @param run.id character. The run ID
#' @param jobsh character vector. Lines of the job.sh script content
#' @param chmod logical. Whether to make job.sh executable. Default TRUE
#'
#' @return invisible path to the written job.sh file
#' @export
write_job_sh <- function(rundir, run.id, jobsh, chmod = TRUE) {
job_path <- file.path(rundir, run.id, "job.sh")
writeLines(jobsh, con = job_path)
if (chmod) {
Sys.chmod(job_path)
}
invisible(job_path)
}
45 changes: 45 additions & 0 deletions base/utils/tests/testthat/test.write_job_sh.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test_that("write_job_sh creates job.sh file", {
# Create a temporary directory for testing
tmpdir <- tempfile()
dir.create(tmpdir)
run.id <- "test_run_001"
dir.create(file.path(tmpdir, run.id))

# Define simple job script content
jobsh <- c(
"#!/bin/bash",
"echo 'running model'"
)

# Call the helper function
result <- write_job_sh(tmpdir, run.id, jobsh)

# Test 1 - file exists
expect_true(file.exists(file.path(tmpdir, run.id, "job.sh")))

# Test 2 - file content is correct
written <- readLines(file.path(tmpdir, run.id, "job.sh"))
expect_equal(written, jobsh)

# Test 3 - function returns the path invisibly
expect_equal(result, file.path(tmpdir, run.id, "job.sh"))

# Cleanup
unlink(tmpdir, recursive = TRUE)
})

test_that("write_job_sh chmod parameter works", {
tmpdir <- tempfile()
dir.create(tmpdir)
run.id <- "test_run_002"
dir.create(file.path(tmpdir, run.id))

jobsh <- c("#!/bin/bash", "./model")

# Test with chmod = FALSE
write_job_sh(tmpdir, run.id, jobsh, chmod = FALSE)
expect_true(file.exists(file.path(tmpdir, run.id, "job.sh")))

# Cleanup
unlink(tmpdir, recursive = TRUE)
})
3 changes: 1 addition & 2 deletions models/ed/R/write.configs.ed.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ write.config.ED2 <- function(trait.values, settings, run.id, defaults = settings

jobsh <- write.config.jobsh.ED2(settings = settings, run.id = run.id)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)

## Write ED2 config.xml file
xml <- write.config.xml.ED2(defaults = defaults, settings = settings, trait.values = trait.values)
Expand Down
2 changes: 1 addition & 1 deletion models/ed/R/write_restart.ED2.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ write_restart.ED2 <- function(outdir, runid, start.time, stop.time,
mod2cf_string <- gsub(begin_from, begin_to, mod2cf_string) # e.g. change from (...'1961/01/01', '1963/01/01'...) to (...'1962/01/01', '1963/01/01'...)
jobsh[mod2cf_line] <- mod2cf_string

writeLines(jobsh, file.path(rundir, runid, "job.sh"))
PEcAn.utils::write_job_sh(rundir, runid, jobsh)

PEcAn.logger::logger.info("Finished --", runid)

Expand Down
3 changes: 1 addition & 2 deletions models/fates/R/write.configs.FATES.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ write.config.FATES <- function(defaults, trait.values, settings, run.id){
# jobsh <- gsub('@SITE_MET@', settings$run$inputs$met$path, jobsh)
## FOR FIRST STEP, CAN USE DEFAULT

writeLines(jobsh, con=file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
#
# ## Write PARAMETER file

Expand Down
3 changes: 1 addition & 2 deletions models/gday/R/write.config.GDAY.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,5 @@ write.config.GDAY <- function(defaults, trait.values, settings, run.id) {

jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
} # write.config.GDAY
3 changes: 1 addition & 2 deletions models/jules/R/write.config.JULES.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ write.config.JULES <- function(defaults, trait.values, settings, run.id) {
jobsh <- gsub("@RUNDIR@", rundir, jobsh)
jobsh <- gsub("@RUNID@", run.id, jobsh)
jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)
writeLines(jobsh, con = file.path(local.rundir, "job.sh"))
Sys.chmod(file.path(local.rundir, "job.sh"))
PEcAn.utils::write_job_sh(local.rundir, run.id, jobsh)

#-----------------------------------------------------------------------
### Copy templated NAMELIST files to local rundir
Expand Down
3 changes: 1 addition & 2 deletions models/ldndc/R/write.config.LDNDC.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ write.config.LDNDC <- function(defaults, trait.values, settings, run.id) {
jobsh <- gsub("@DELETE.RAW@", settings$model$delete.raw, jobsh)

# Write job.sh file to rundir
writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(rundir, "job.sh")) # Permissions
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)



Expand Down
3 changes: 1 addition & 2 deletions models/linkages/R/write.config.LINKAGES.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,5 @@ write.config.LINKAGES <- function(defaults = NULL, trait.values, settings, run.i
pft_names <- unlist(sapply(settings$pfts, `[[`, "name"))
pft_names <- paste0("pft_names = c('", paste(pft_names, collapse = "','"), "')")
jobsh <- gsub("@PFT_NAMES@", pft_names, jobsh)
writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
} # write.config.LINKAGES
3 changes: 1 addition & 2 deletions models/lpjguess/R/write.config.LPJGUESS.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ write.config.LPJGUESS <- function(defaults, trait.values, settings, run.id, rest
jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)
jobsh <- gsub("@INSFILE@", settings$model$insfile, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
} # write.config.LPJGUESS

# ==================================================================================================#
Expand Down
3 changes: 1 addition & 2 deletions models/maat/R/write.config.MAAT.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ write.config.MAAT <- function(defaults = NULL, trait.values, settings, run.id) {
} #End if/else

# Write the job.sh script
writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)

} # write.config.MAAT
##-------------------------------------------------------------------------------------------------#
Expand Down
3 changes: 1 addition & 2 deletions models/maespa/R/write.config.MAESPA.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,5 @@ write.config.MAESPA <- function(defaults, trait.values, settings, run.id) {

jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
} # write.config.MAESPA
3 changes: 1 addition & 2 deletions models/peprmt/R/write.configs.peprmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,5 @@ write.config.PEPRMT <- function(defaults, trait.values, settings, run.id) {
dplyr::filter(.data$site == settings$run$site$id)

utils::write.csv(run_data, file.path(rundir, "run_data.csv"), row.names = FALSE)
writeLines(jobsh, con = file.path(rundir, "job.sh"))
Sys.chmod(file.path(rundir, "job.sh"))
PEcAn.utils::write_job_sh(rundir, run.id, jobsh)
} # write.config.PEPRMT
3 changes: 1 addition & 2 deletions models/preles/R/write.config.PRELES.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ write.config.PRELES <- function(defaults, trait.values, settings, run.id) {
"'",settings$run$end.date,"') ",
'" | R --vanilla'
)
writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)
} # write.config.PRELES
3 changes: 1 addition & 2 deletions models/rothc/R/write.config.RothC.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ write.config.RothC <- function(defaults, trait.values, settings, run.id) {

jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)

#-----------------------------------------------------------------------
### Edit a templated config file for runs
Expand Down
4 changes: 2 additions & 2 deletions models/sibcasa/R/write.config.SIBCASA.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


#-------------------------------------------------------------------------------------------------#
#' Writes a SIBCASA config file.
#'
Expand Down Expand Up @@ -60,8 +61,7 @@ write.config.SIBCASA <- function(defaults, trait.values, settings, run.id) {

jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)

#-----------------------------------------------------------------------
### Edit a templated config file for runs
Expand Down
3 changes: 1 addition & 2 deletions models/sipnet/R/write.configs.SIPNET.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ write.config.SIPNET <- function(defaults, trait.values, settings, run.id, inputs
}
jobsh <- gsub("@DELETE.RAW@", settings$model$delete.raw, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)


### Copy event file
Expand Down
3 changes: 1 addition & 2 deletions models/stics/R/write.config.STICS.R
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,7 @@ write.config.STICS <- function(defaults, trait.values, settings, run.id) {
jobsh <- gsub("@MODFILE@", paste0("mod_s", basename(usmdirs[1]), ".sti"), jobsh)
jobsh <- gsub("@STICSEXE@", stics_exe, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)


} # write.config.STICS
Expand Down
3 changes: 1 addition & 2 deletions models/template/R/write.config.MODEL.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ write.config.MODEL <- function(defaults, trait.values, settings, run.id) {

jobsh <- gsub("@BINARY@", settings$model$binary, jobsh)

writeLines(jobsh, con = file.path(settings$rundir, run.id, "job.sh"))
Sys.chmod(file.path(settings$rundir, run.id, "job.sh"))
PEcAn.utils::write_job_sh(settings$rundir, run.id, jobsh)

#-----------------------------------------------------------------------
### Edit a templated config file for runs
Expand Down