Skip to content

Commit

Permalink
Adding arguments to CLI to manage terminal outputs (#315)
Browse files Browse the repository at this point in the history
* adding arguments

* adding docstring

* fixing docstring

* changed docstrings

---------

Co-authored-by: Nicolas Linden <[email protected]>
Co-authored-by: Luca Fiorito <[email protected]>
  • Loading branch information
3 people authored Jul 11, 2024
1 parent 986e84a commit 6379c48
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
22 changes: 18 additions & 4 deletions sandy/njoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def _groupr_input(endfin, pendfin, gendfout, mat,
return "\n".join(text) + "\n"


def _run_njoy(text, endf, pendf=None, exe=None):
def _run_njoy(text, endf, pendf=None, exe=None, njoy_output=None):
"""
Run njoy executable for given input.
Expand All @@ -1070,10 +1070,14 @@ def _run_njoy(text, endf, pendf=None, exe=None):
njoy input file passed to `Popen` as `stdin` (it must be encoded first)
exe : `str`, optional, default is `None`
njoy executable: if `None` (default) get it from `NJOY` env variable
njoy_output : `int`, optional, default is `None`
target of stderr and stdout for the NJOY process. If `None`, NJOY will
print its output in the terminal. If `subprocess.DEVNULL`, NJOY
output will be suppressed, default is `None`.
"""
if exe is None:
exe = get_njoy()
stdout = stderr = None
stdout = stderr = njoy_output
stdin = text.encode()
with TemporaryDirectory() as tmpdir:
shutil.copy(endf, os.path.join(tmpdir, "tape20"))
Expand Down Expand Up @@ -1320,6 +1324,7 @@ def process_neutron(
exe=None,
verbose=True,
dryrun=False,
njoy_output=None,
**kwargs,
):
"""
Expand All @@ -1338,6 +1343,10 @@ def process_neutron(
njoy executable (with path)
.. note:: if no executable is given, SANDY looks for a default
executable in `PATH` and in env variable `NJOY`
njoy_output : `int`, optional, default is `None`
target of stderr and stdout for the NJOY process. If `None`, NJOY will
print its output in the terminal. If `subprocess.DEVNULL`, NJOY
output will be suppressed, default is `None`.
route : `str`, optional, default is `0`
xsdir "route" parameter
suffixes : iterable of `int`, optional, default is `None`
Expand Down Expand Up @@ -1377,7 +1386,7 @@ def process_neutron(
# Run njoy
if dryrun:
return text
outputs = _run_njoy(text, endftape, pendftape, exe=exe)
outputs = _run_njoy(text, endftape, pendftape, exe=exe, njoy_output=njoy_output)

# Minimal output post-processing
if "xsdir" in outputs:
Expand All @@ -1401,6 +1410,7 @@ def process_proton(
tag="",
exe=None,
route="0",
njoy_output=None,
**kwargs,
):
"""Run sequence to process proton file with njoy.
Expand All @@ -1416,6 +1426,10 @@ def process_proton(
any `xsdir` file
dryrun : `bool`
option to produce the njoy input file without running njoy
njoy_output : `int`, optional, default is `None`
target of stderr and stdout for the NJOY process. If `None`, NJOY will
print its output in the terminal. If `subprocess.DEVNULL`, NJOY
output will be suppressed, default is `None`.
tag : `str`
tag to append to each output filename beofre the extension
(default is `None`)
Expand Down Expand Up @@ -1455,7 +1469,7 @@ def process_proton(
outputs["tape70"] = join(wdir, "{}{}{}h.xsd".format(za_new, tag, suff))
text += "stop"
if not dryrun:
_run_njoy(text, inputs, outputs, exe=exe)
_run_njoy(text, inputs, outputs, exe=exe, njoy_output=njoy_output)
# Change route and filename in xsdir file.
acefile = outputs["tape50"]
xsdfile = outputs["tape70"]
Expand Down
30 changes: 28 additions & 2 deletions sandy/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import filecmp
import pandas as pd
import subprocess as sp

import sandy
from sandy.tools import is_valid_file
Expand Down Expand Up @@ -60,6 +61,13 @@ def parse(iargs=None):
" - first perturbation coefficient to consider\n"
" - last perturbation coefficient to consider")

parser.add_argument('--loglevel',
type=str,
default="info",
action='store',
metavar="{debug, info, warning, error, critical}",
help="Set the logger verbosity level.")

parser.add_argument('--mat',
type=int,
default=list(range(1, 10000)),
Expand Down Expand Up @@ -144,6 +152,11 @@ def parse(iargs=None):
help="seed for random sampling of MF35 covariance "
"matrix (default = random)")

parser.add_argument('--supressnjoy',
default=False,
action="store_true",
help="Supress NJOY ouputs.")

parser.add_argument('--temperatures', '-T',
default=None,
type=float,
Expand All @@ -158,7 +171,6 @@ def parse(iargs=None):
version='%(prog)s {}'.format(sandy.__version__),
help="SANDY's version.")


init = parser.parse_known_args(args=iargs)[0]
if init.acer and not init.temperatures:
parser.error("--acer requires --temperatures")
Expand Down Expand Up @@ -310,6 +322,15 @@ def run(iargs):
-------
None.
"""

loglevels = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL,
}
logging.getLogger().setLevel(loglevels[iargs.loglevel])
logging.info(f"processing file: '{iargs.file}'")

err_pendf = 0.01
Expand All @@ -320,6 +341,8 @@ def run(iargs):

endf6 = sandy.Endf6.from_file(iargs.file)

njoy_output = sp.DEVNULL if iargs.supressnjoy else None

# ERRORR KEYWORDS
nubar = bool(31 in iargs.mf) and (31 in endf6.mf)
xs = bool(33 in iargs.mf) and (33 in endf6.mf or 32 in endf6.mf) # this handle together MF32 and MF33
Expand All @@ -333,7 +356,8 @@ def run(iargs):
chi=chi,
mubar=mubar,
groupr_kws=dict(nubar=nubar, chi=chi, mubar=mubar, ign=2),
errorr_kws=dict(ign=2)
errorr_kws=dict(ign=2),
njoy_output=njoy_output
)
if iargs.mt33:
errorr_kws["errorr33_kws"] = dict(mt=iargs.mt33)
Expand Down Expand Up @@ -376,6 +400,7 @@ def run(iargs):
verbose=iargs.debug,
err=err_pendf,
minimal_processing=iargs.debug,
njoy_output=njoy_output,
)

# ACE KEYWORDS
Expand All @@ -385,6 +410,7 @@ def run(iargs):
minimal_processing=iargs.debug,
temperature=temperature,
purr=False,
njoy_output=njoy_output,
)


Expand Down

0 comments on commit 6379c48

Please sign in to comment.