Source code for fermipy.jobs.gtlink

# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Utilities to chain together a series of ScienceTools apps
"""
from __future__ import absolute_import, division, print_function

import sys
import os

from fermipy.jobs.link import Link
import GtApp


[docs]def extract_parameters(pil, keys=None): """Extract and return parameter names and values from a pil object Parameters ---------- pil : `Pil` object keys : list List of parameter names, if None, extact all parameters Returns ------- out_dict : dict Dictionary with parameter name, value pairs """ out_dict = {} if keys is None: keys = pil.keys() for key in keys: try: out_dict[key] = pil[key] except ValueError: out_dict[key] = None return out_dict
[docs]def update_gtapp(gtapp, **kwargs): """Update the parameters of the object that can run ScienceTools applications Parameters ---------- gtapp : `GtApp.GtApp` Object that will run the application in question kwargs : arguments used to invoke the application """ for key, val in kwargs.items(): if key in ['pfiles', 'scratch']: continue if val is None: continue try: gtapp[key] = val except ValueError: raise ValueError( "gtapp failed to set parameter %s %s" % (key, val)) except KeyError: raise KeyError("gtapp failed to set parameter %s %s" % (key, val))
def _set_pfiles(dry_run, **kwargs): """Set the PFILES env var Parameters ---------- dry_run : bool Don't actually run Keyword arguments ----------------- pfiles : str Value to set PFILES Returns ------- pfiles_orig : str Current value of PFILES envar """ pfiles_orig = os.environ['PFILES'] pfiles = kwargs.get('pfiles', None) if pfiles: if dry_run: print("mkdir %s" % pfiles) else: try: os.makedirs(pfiles) except OSError: pass pfiles = "%s:%s" % (pfiles, pfiles_orig) os.environ['PFILES'] = pfiles return pfiles_orig def _reset_pfiles(pfiles_orig): """Set the PFILES env var Parameters ---------- pfiles_orig : str Original value of PFILES """ os.environ['PFILES'] = pfiles_orig
[docs]def build_gtapp(appname, dry_run, **kwargs): """Build an object that can run ScienceTools application Parameters ---------- appname : str Name of the application (e.g., gtbin) dry_run : bool Print command but do not run it kwargs : arguments used to invoke the application Returns `GtApp.GtApp` object that will run the application in question """ pfiles_orig = _set_pfiles(dry_run, **kwargs) gtapp = GtApp.GtApp(appname) update_gtapp(gtapp, **kwargs) _reset_pfiles(pfiles_orig) return gtapp
[docs]def run_gtapp(gtapp, stream, dry_run, **kwargs): """Runs one on the ScienceTools apps Taken from fermipy.gtanalysis.run_gtapp by Matt Wood Parameters ---------- gtapp : `GtApp.GtApp` object The application (e.g., gtbin) stream : stream object Must have 'write' function dry_run : bool Print command but do not run it kwargs : arguments used to invoke the application """ if stream is None: stream = sys.stdout pfiles_orig = _set_pfiles(dry_run, **kwargs) update_gtapp(gtapp, **kwargs) stream.write("%s\n" % gtapp.command()) stream.flush() if dry_run: _reset_pfiles(pfiles_orig) return 0 try: stdin, stdout = gtapp.runWithOutput(print_command=False) for line in stdout: stream.write(line.strip()) stream.flush() return_code = 0 except: stream.write('Exited with exit code -1\n') return_code = -1 _reset_pfiles(pfiles_orig) return return_code