1- source(" /app/prediagnostics/config.R" )
1+ library(optparse )
2+ library(reticulate )
3+
24source(" /app/prediagnostics/input.R" )
3- source(" /app/prediagnostics/prediagnostics.R" )
45source(" /app/prediagnostics/output.R" )
6+ source(" /app/prediagnostics/prediagnostics.R" )
7+
8+ PYTHON_EXE = " /usr/bin/python3"
9+ PYTHON_FILE = " /app/prediagnostics/sos_read/sos_read.py"
10+ TMP_PATH = " /tmp"
511
612start = Sys.time()
713
@@ -10,29 +16,45 @@ input_dir = file.path("/mnt", "data", "input")
1016output_dir = file.path(" /mnt" , " data" , " output" )
1117
1218# Command line arguments
13- args = commandArgs(trailingOnly = TRUE )
14- # we want to specify index and reach json for local run
15- if (length(args )> = 2 ){
16- index = strtoi(args [1 ]) + 1
17- reaches_json = file.path(input_dir , paste(args [2 ]))
18-
19- # we want to specify reach json for aws run
20- if (length(args )> = 3 ){
21- index = strtoi(Sys.getenv(" AWS_BATCH_JOB_ARRAY_INDEX" )) + 1
22- }
23-
24- # we want to specify only index for local run
25- } else if (length(args )> = 1 ) {
26- index = strtoi(args [1 ]) + 1
27- reaches_json = file.path(input_dir , ' reaches.json' )
28- # we want to run on default settings for aws
29- } else {
30- index = strtoi(Sys.getenv(" AWS_BATCH_JOB_ARRAY_INDEX" )) + 1
31- reaches_json = file.path(input_dir , ' reaches.json' )
19+ option_list <- list (
20+ make_option(c(" -i" , " --index" ), type = " integer" , default = NULL , help = " Index to run on" ),
21+ make_option(c(" -b" , " --config_bucket" ), type = " character" , default = " " , help = " Bucket key to find the sos" ),
22+ make_option(c(" -r" , " --reaches_json" ), type = " character" , default = " reaches.json" , help = " Name of reaches.json" )
23+ )
24+ opt_parser <- OptionParser(option_list = option_list )
25+ opts <- parse_args(opt_parser )
26+
27+ index <- opts $ index
28+ if (index == - 256 ){
29+ index <- strtoi(Sys.getenv(" AWS_BATCH_JOB_ARRAY_INDEX" ))
3230}
31+ index <- index + 1 # Add 1 to AWS 0-based index
3332
34- # Run Diagnostics
35- output = run_diagnostics(input_dir , reaches_json , index , output_dir )
33+ config_bucket <- opts $ config_bucket
34+ reaches_json = file.path(input_dir , opts $ reaches_json )
35+
36+ # Load Config file from S3
37+ if (config_bucket != " " ) {
38+ use_python(PYTHON_EXE )
39+ source_python(PYTHON_FILE )
40+
41+ config_filepath = file.path(TMP_PATH , " config.R" )
42+ download_sos(config_bucket , config_filepath )
43+
44+ # Run Diagnostics on S3 config file
45+ if (file.exists(config_filepath )) {
46+ source(config_filepath )
47+ output = run_diagnostics(input_dir , reaches_json , index , output_dir )
48+ } else {
49+ print(" Config file could not be downloaded and prediagnostics will not run." )
50+ }
51+
52+ # Run Diagnostics on local config file
53+ } else {
54+ print(" Config file will be run on local config: '/app/prediagnostics/config.R'" )
55+ source(" /app/prediagnostics/config.R" )
56+ output = run_diagnostics(input_dir , reaches_json , index , output_dir )
57+ }
3658
3759end = Sys.time()
3860print(paste0(" Execution time: " , end - start ))
0 commit comments