Friday, May 21, 2010

So, can I shut Sherpa up?

Say you want to run a bunch of Sherpa commands (maybe in a script, or in an interactive session) but don't want to see the output from the commands. What should you do?

Well you can take advantage of the not-yet-documented-in-CIAO-but-really-should-be Python logging infrastructure used by Sherpa. Start with

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

and then you turn off the output using one of

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

and turn it back on again with

logger.setLevel(logging.INFO)

The argument determines what severity of messages are displayed (so logging.WARN means warning and above, which excludes the INFO level used for most output from Sherpa).

As an example, compare the screen output for the three load_data() calls below:

% sherpa
-----------------------------------------------------
Welcome to Sherpa: CXC's Modeling and Fitting Package
-----------------------------------------------------
CIAO 4.2 Monday, November 30, 2009

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 on-screen results of a call to fit() or one of the error routines such as conf() and proj(); if you do this though you will probably want to use the corresponding get_xxx_results routine to get at the calculated data!

You can do a lot more with this "logger" object, such as re-directing the output to a file (or even to both the screen and a file). See 'help logging' or this logging tutorial (one of many that are out there for you intrepid readers) for more information.

No comments:

Post a Comment