Skip to content

Commit fa5e898

Browse files
Make geophires_monte_carlo callable with CLI with path correction. Includes example usage in MC user manual
1 parent 7c8645c commit fa5e898

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

docs/Monte-Carlo-User-Guide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ bin edges: [-43.34 -43.2258 -43.1116 -42.9974 -42.8832 -42.769 -42.6548 -42.5
109109
110110
```
111111

112+
You may also run from the command line:
113+
114+
```
115+
python -mgeophires_monte_carlo GEOPHIRESv3.py GEOPHIRES-example1.txt MC_GEOPHIRES_Settings_file.txt MC_GEOPHIRES_Result.txt
116+
```
117+
112118
## Documentation
113119

114120
See [module documentation](reference/geophires_monte_carlo.html)

src/geophires_monte_carlo/MC_GeoPHIRES3.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,6 @@ def main(command_line_args=None):
240240
# keep track of execution time
241241
tic = time.time()
242242

243-
# set the starting directory to be the directory that this file is in
244-
working_dir = os.path.dirname(os.path.abspath(__file__))
245-
os.chdir(working_dir)
246-
working_dir = working_dir + os.sep
247-
248243
# get the values off the command line
249244
parser = argparse.ArgumentParser()
250245
parser.add_argument('Code_File', help='Code File')
@@ -268,7 +263,11 @@ def main(command_line_args=None):
268263
inputs = []
269264
outputs = []
270265
iterations = 0
271-
output_file = args.MC_OUTPUT_FILE if 'MC_OUTPUT_FILE' in args and args.MC_OUTPUT_FILE is not None else ''
266+
output_file = (
267+
args.MC_OUTPUT_FILE
268+
if 'MC_OUTPUT_FILE' in args and args.MC_OUTPUT_FILE is not None
269+
else str(Path(Path(args.Input_file).parent, 'MC_Result.txt').absolute())
270+
)
272271
python_path = 'python'
273272

274273
for line in flist:
@@ -312,6 +311,11 @@ def main(command_line_args=None):
312311
with open(output_file, 'w') as f:
313312
f.write(s)
314313

314+
# set the starting directory to be the directory that this file is in
315+
working_dir = os.path.dirname(os.path.abspath(__file__))
316+
os.chdir(working_dir)
317+
working_dir = working_dir + os.sep
318+
315319
# build the args list
316320
pass_list = [inputs, outputs, args, output_file, working_dir, python_path] # this list never changes
317321

src/geophires_monte_carlo/__main__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
5+
from geophires_monte_carlo import MC_GeoPHIRES3
6+
from geophires_monte_carlo import common
7+
8+
if __name__ == '__main__':
9+
log = common._get_logger()
10+
stash_cwd = Path.cwd()
11+
12+
try:
13+
command_line_args = []
14+
for i in range(len(sys.argv[1:])):
15+
arg = sys.argv[i + 1]
16+
if Path(arg).exists():
17+
arg = str(Path(arg).absolute())
18+
log.info(f'Converted arg to absolute path: {arg}')
19+
20+
command_line_args.append(arg)
21+
22+
MC_GeoPHIRES3.main(command_line_args=command_line_args)
23+
except Exception as e:
24+
raise RuntimeError(f'Monte Carlo encountered an exception: {e!s}') from e
25+
except SystemExit:
26+
raise RuntimeError('Monte Carlo exited without giving a reason') from None
27+
finally:
28+
# Undo MC internal global settings changes
29+
os.chdir(stash_cwd)

tests/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
HIP.out
22
*.log
3+
MC_Result.json
4+
MC_Result.txt
5+
*.png

0 commit comments

Comments
 (0)