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.chain 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))
[docs]def build_gtapp(appname, **kwargs): """Build an object that can run ScienceTools application Parameters ---------- appname : str Name of the application (e.g., gtbin) kwargs : arguments used to invoke the application Returns `GtApp.GtApp` object that will run the application in question """ gtapp = GtApp.GtApp(appname) update_gtapp(gtapp, **kwargs) 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 update_gtapp(gtapp, **kwargs) pfiles = kwargs.get('pfiles', None) pfiles_orig = os.environ['PFILES'] if pfiles: if dry_run: print ("mkdir %s" % pfiles) else: try: os.makedirs(pfiles) except OSError: pass pfiles = "%s:%s" % (pfiles, pfiles_orig) print ("Setting PFILES=%s" % pfiles) os.environ['PFILES'] = pfiles stream.write("%s\n" % gtapp.command()) stream.flush() if dry_run: os.environ['PFILES'] = pfiles_orig return stdin, stdout = gtapp.runWithOutput(print_command=False) for line in stdout: stream.write(line.strip()) stream.flush() os.environ['PFILES'] = pfiles_orig