Last modified: 7 November 2022

How can I suppress the screen output from Sherpa commands?


In order to hide the verbose screen output from Sherpa commands, e.g. load_pha, the Python logging infrastructure may be used. The Python logging module may be imported either in a separate script or within the Sherpa interactive session.

Start by importing the module into the interactive session (or add this to the top of your script):

import logging
logger = logging.getLogger("sherpa")

Then, turn off the screen output of Sherpa commands using one of

logger.setLevel(logging.WARN)
logger.setLevel(logging.ERROR)

and turn it back on again with

logger.setLevel(logging.INFO)

The argument to 'logger.setLevel' determines what severity of messages are displayed. The 'logging.INFO' setting is the highest verbosity level, whereas 'logging.WARN' means only warnings and error messages are printed (which excludes the INFO level used for most output from Sherpa).

As an example, compare the screen output for the three load_pha calls below:

% sherpa
-----------------------------------------------------
Welcome to Sherpa: CXC's Modeling and Fitting Package
-----------------------------------------------------
Sherpa 4.14.0

sherpa-1> import logging
sherpa-2> logger = logging.getLogger("sherpa")
sherpa-3> load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
statistical errors were found in file '3c273.pi' but not used; to use them, re-read with use_errors=True
read ARF file 3c273.arf
read RMF file 3c273.rmf
WARNING: systematic errors were not found in file '3c273_bg.pi'
statistical errors were found in file '3c273_bg.pi' but not used; to use them, re-read with use_errors=True
read background file 3c273_bg.pi
sherpa-4> logger.setLevel(logging.WARN)
sherpa-5> load_pha("3c273.pi")
WARNING: systematic errors were not found in file '3c273.pi'
WARNING: systematic errors were not found in file '3c273_bg.pi'
sherpa-6> logger.setLevel(logging.ERROR)
sherpa-7> load_pha("3c273.pi")
sherpa-8> logger.setLevel(logging.INFO)

This can be particularly useful for hiding the lengthy output of commands such as fit(), conf(), or proj().

See the Sherpa FAQ "How can I log the command output from my Sherpa fitting session?" to learn how to use the logging module to record the command output from a Sherpa session to a file.