Exemplo n.º 1
0
def get_config(infile, configspec=join(CONFIG_DIR, 'default.conf')):
    """Parse config file, and in addition:
    - include default options
    - exit with an error if a required option is missing"""
    config = ConfigObj(infile, configspec=configspec, file_error=True)

    validator = Validator()
    # @todo: I'm not sure we always want to copy all default options here
    results = config.validate(validator, copy=True)
    mes = Loggin.Message()

    utils.Checkevtclass(config['event']['evclass'])

    if results != True:
        for (section_list, key, _) in flatten_errors(config, results):
            if key is not None:
                mes.warning(
                    'The "%s" key in the section "%s" failed validation' %
                    (key, ', '.join(section_list)))
            else:
                mes.warning('The following section was missing:%s ' %
                            ', '.join(section_list))
        mes.warning('   Please check your config file for missing '
                    'and wrong options!')
        mes.error('Config file is not valid.')

    return config
Exemplo n.º 2
0
def get_config(infile, configspec=join(CONFIG_DIR, 'default.conf')):
    """Parse config file, and in addition:
    - include default options
    - exit with an error if a required option is missing"""
    config = ConfigObj(infile, configspec=configspec,
                       file_error=True)

    validator = Validator()
    # @todo: I'm not sure we always want to copy all default options here
    results = config.validate(validator, copy=True)
    mes = Loggin.Message()


    utils.Checkevtclass(config['event']['evclass'])

    if results != True:
        for (section_list, key, _) in flatten_errors(config, results):
            if key is not None:
                mes.warning('The "%s" key in the section "%s" failed validation' %
                      (key, ', '.join(section_list)))
            else:
                mes.warning('The following section was missing:%s ' %
                      ', '.join(section_list))
        mes.warning('   Please check your config file for missing '
              'and wrong options!')
        mes.error('Config file is not valid.')

    return config
Exemplo n.º 3
0
def GenAnalysisObjects(config, verbose=1, xmlfile=""):

    mes = Loggin.Message()
    #check is the summed likelihood method should be used and get the
    #Analysis objects (observation and (Un)BinnedAnalysis objects)
    folder = config['out']

    # If there are no xml files, create it and print a warning
    if len(glob.glob(config['file']['xml'].replace('.xml', '*.xml'))) == 0:
        mes.warning("Xml not found, creating one for the given config %s" %
                    config['file']['xml'])
        XmlMaker(config)

    Fit = SummedLikelihood.SummedLikelihood()
    if hasKey(config, 'ComponentAnalysis') == True:
        # Create one obs instance for each component
        configs = [None] * 4
        Fits = [None] * 4
        Analyses = [None] * 4
        if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes':
            from enrico.data import fermievtypes
            mes.info("Breaking the analysis in Front/Back events")
            # Set Summed Likelihood to True
            oldxml = config['file']['xml']
            for k, TYPE in enumerate(["FRONT", "BACK"]):
                configs[k] = ConfigObj(config)
                configs[k]['event']['evtype'] = fermievtypes[TYPE]
                try:
                    Analyses[k] = Analysis(folder, configs[k], \
                        configgeneric=config,\
                        tag=TYPE, verbose = verbose)
                    if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile
                    Fits[k] = Analyses[k].CreateLikeObject()
                    Fit.addComponent(Fits[k])
                except RuntimeError, e:
                    if 'RuntimeError: gtltcube execution failed' in str(e):
                        mes.warning("Event type %s is empty! Error is %s" %
                                    (TYPE, str(e)))
            FitRunner = Analyses[0]
Exemplo n.º 4
0
def query_config():
    import os
    """Make a new config object, asking the user for required options"""
    config = ConfigObj(indent_type='\t')
    mes = Loggin.Message()
    mes.info('Please provide the following required options [default] :')
    config['out'] = os.getcwd()
    out = raw_input('Output directory [' + config['out'] + '] : ')
    if not (out == ''):
        config['out'] = out

#    Informations about the source
    config['target'] = {}
    config['target']['name'] = raw_input('Target Name : ')
    config['target']['ra'] = raw_input('Right Ascension: ')
    config['target']['dec'] = raw_input('Declination: ')

    config['target']['redshift'] = '0'
    redshift = raw_input('redshift, no effect if null [0] : ')
    if not (redshift == ''):
        config['target']['redshift'] = redshift
        config['target']['ebl_model'] = raw_input(
            'ebl model to used\n'
            '0=Kneiske, 1=Primack05, 2=Kneiske_HighUV, 3=Stecker05, '
            '4=Franceschini, 5=Finke, 6=Gilmore : ')

    message = (
        'Options are : PowerLaw, PowerLaw2, LogParabola, '
        'PLExpCutoff, Generic\nGeneric is design to allow the user to fit with non-supported models\n'
        'EBL absorption can be added for PowerLaw2, LogParabola, PLExpCutoff\n'
        'Spectral Model [PowerLaw] : ')
    config['target']['spectrum'] = 'PowerLaw'
    model = raw_input(message)
    if not (model == ''):
        config['target']['spectrum'] = model

#    informations about the ROI
    config['space'] = {}
    config['space']['xref'] = config['target']['ra']
    config['space']['yref'] = config['target']['dec']
    config['space']['rad'] = '15'
    roi = raw_input('ROI Size [15] : ')
    if not (roi == ''):
        config['space']['rad'] = roi

#    informations about the input files
    config['file'] = {}
    if USE_FULLMISSION_SPACECRAFT:
        config['file'][
            'spacecraft'] = DOWNLOAD_DIR + '/lat_spacecraft_merged.fits'
    else:
        config['file']['spacecraft'] = '@' + DOWNLOAD_DIR + '/spacecraft.lis'
    ft2 = raw_input('FT2 file [' + config['file']['spacecraft'] + '] : ')
    if not (ft2 == ''):
        config['file']['spacecraft'] = ft2
    config['file']['event'] = DOWNLOAD_DIR + '/events.lis'
    ft1list = raw_input('FT1 list of files [' + config['file']['event'] +
                        '] : ')
    if not (ft1list == ''):
        config['file']['event'] = ft1list
    config['file']['xml'] = config['out'] + '/' + config['target'][
        'name'] + '_' + config['target']['spectrum'] + '_model.xml'
    tag = raw_input('tag [LAT_Analysis] : ')
    if not (tag == ''):
        config['file']['tag'] = tag
    else:
        config['file']['tag'] = 'LAT_Analysis'

#    informations about the time
    config['time'] = {}
    tmin = raw_input('Start time [-1=START] : ')
    ft2 = config['file']['spacecraft']
    if not (tmin == '') and float(tmin) >= 0:
        config['time']['tmin'] = tmin
    else:
        config['time']['tmin'] = get_times_from_spacecraft(ft2,
                                                           target=['tmin'])[0]
    tmax = raw_input('End time [-1=END] : ')
    if not (tmax == '') and float(tmax) >= 0:
        config['time']['tmax'] = tmax
    else:
        config['time']['tmax'] = get_times_from_spacecraft(ft2,
                                                           target=['tmax'])[1]

#    informations about the energy
    config['energy'] = {}
    emin = raw_input('Emin [100] : ')
    if not (emin == ''):
        config['energy']['emin'] = emin
    else:
        config['energy']['emin'] = '100'
    emax = raw_input('Emax [300000] : ')
    if not (emax == ''):
        config['energy']['emax'] = emax
    else:
        config['energy']['emax'] = '300000'

#    informations about the event class
    config['event'] = {}
    irfs = raw_input('IRFs [CALDB] : ')
    if not (irfs == ''):
        config['event']['irfs'] = irfs
    else:
        config['event']['irfs'] = 'CALDB'

    if irfs == '':
        ok = False
        while not (ok):
            evclass = raw_input('evclass [128] : ')
            if not (evclass == ''):
                config['event']['evclass'] = evclass
            else:
                config['event']['evclass'] = '128'

            evtype = raw_input('evtype [3] : ')
            if not (evtype == ''):
                config['event']['evtype'] = evtype
            else:
                config['event']['evtype'] = '3'
            print "Corresponding IRFs\t=\t", utils.GetIRFS(
                float(config['event']['evclass']),
                float(config['event']['evtype']))
            ans = raw_input('Is this ok? [y] : ')
            if ans == "y" or ans == '':
                ok = True

    config['analysis'] = {}
    zmax = utils.GetZenithCut(float(config['event']['evclass']),
                              float(config['event']['evtype']),
                              float(config['energy']['emin']))
    print "Corresponding zmax = ", zmax
    config["analysis"]["zmax"] = zmax

    return get_config(config)
Exemplo n.º 5
0
def get_default_config(configspec=join(CONFIG_DIR, 'default.conf')):
    return ConfigObj(None, configspec=configspec)
Exemplo n.º 6
0
  def write_config(self):
    verbose("-> Write config file for %s" %(self.name))
    import enrico
    from enrico.config import get_config
    # Create object
    config = ConfigObj(indent_type='\t')
    mes = Loggin.Message()
    config['out']    = self.output+'/%s_%s/'%(self.name,self.band)
    config['Submit'] = 'no'
    # target
    config['target'] = {}
    config['target']['name'] = self.name
    config['target']['ra']   = str("%.4f" %self.RA)
    config['target']['dec']  = str("%.4f" %self.DEC)
    config['target']['redshift'] = '0'
    config['target']['spectrum'] = 'PowerLaw'
    # space
    config['space'] = {}
    config['space']['xref'] = config['target']['ra']
    config['space']['yref'] = config['target']['dec']
    config['space']['rad']  = self.roi
    # files
    #basepath = "/".join(enrico.__path__[0].split("/")[0:-1])
    #datapath = basepath+"/Data/download/"
    config['file'] = {}
    #config['file']['spacecraft'] = str("%s/lat_spacecraft_merged.fits" %datapath)
    #config['file']['event']      = str("%s/event.list" %datapath)
    config['file']['spacecraft'] = self.SCfile
    config['file']['event']      = self.PHfile
    config['file']['tag']        = 'fast'
    # time
    config['time'] = {}
    self.calculate_times()
    config['time']['tmin'] = self.timemin
    config['time']['tmax'] = self.timemax
    # energy
    config['energy'] = {}
    if (self.band=='lo'):
      config['energy']['emin'] = self.energymin
      config['energy']['emax'] = self.energycut
    elif (self.band=='hi'):
      config['energy']['emin'] = self.energycut
      config['energy']['emax'] = self.energymax
    else:
      config['energy']['emin'] = self.energymin
      config['energy']['emax'] = self.energymax

    config['energy']['enumbins_per_decade'] = 5
    # event class
    config['event'] = {}
    config['event']['irfs'] = 'CALDB'
    config['event']['evclass'] = '64' #'128' #64=transients, 128=source
    config['event']['evtype'] = '3'
    # analysis
    config['analysis'] = {}
    config['analysis']['zmax'] = 100
    # Validate
    verbose(config)
    # Add the rest of the values
    config = get_config(config)
    # Tune the remaining variables
    config['AppLC']['index'] = self.spindex
    config['AppLC']['NLCbin'] = int(24*self.timewindow/self.binsize+0.5)
    config['AppLC']['rad']  = self.roi
    # Write config file
    self.configfile = str("%s/%s_%sE.conf" %(self.output,self.name,self.band))
    with open(self.configfile,'w') as f:
      config.write(f)
Exemplo n.º 7
0
def GenAnalysisObjects(config, verbose=1, xmlfile=""):
    import os
    import os.path
    import math
    import SummedLikelihood
    from enrico.xml_model import XmlMaker
    from enrico.extern.configobj import ConfigObj
    from utils import hasKey, isKey
    import Loggin
    mes = Loggin.Message()
    #check is the summed likelihood method should be used and get the
    #Analysis objects (observation and (Un)BinnedAnalysis objects)
    SummedLike = config['Spectrum']['SummedLike']
    folder = config['out']

    # If there is no xml file, create it and print a warning
    if (not os.path.isfile(config['file']['xml'])):
        mes.warning("Xml not found, creating one for the given config %s" %
                    config['file']['xml'])
        XmlMaker(config)

    Fit = SummedLikelihood.SummedLikelihood()
    if hasKey(config, 'ComponentAnalysis') == True:
        # Create one obs instance for each component
        configs = [None] * 4
        Fits = [None] * 4
        Analyses = [None] * 4
        if isKey(config['ComponentAnalysis'], 'FrontBack') == 'yes':
            from enrico.data import fermievtypes
            mes.info("Breaking the analysis in Front/Back events")
            # Set Summed Likelihood to True
            config['Spectrum']['SummedLike'] = 'yes'
            oldxml = config['file']['xml']
            for k, TYPE in enumerate(["FRONT", "BACK"]):
                configs[k] = ConfigObj(config)
                configs[k]['event']['evtype'] = fermievtypes[TYPE]
                try:
                    Analyses[k] = Analysis(folder, configs[k], \
                        configgeneric=config,\
                        tag=TYPE, verbose = verbose)
                    if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile
                    Fits[k] = Analyses[k].CreateLikeObject()
                    Fit.addComponent(Fits[k])
                except RuntimeError, e:
                    if 'RuntimeError: gtltcube execution failed' in str(e):
                        mes.warning("Event type %s is empty! Error is %s" %
                                    (TYPE, str(e)))
            FitRunner = Analyses[0]

        elif isKey(config['ComponentAnalysis'], 'PSF') == 'yes':
            from enrico.data import fermievtypes
            mes.info("Breaking the analysis in PSF 0,1,2,3.")
            # Clone the configs
            # Set Summed Likelihood to True
            config['Spectrum']['SummedLike'] = 'yes'
            for k, TYPE in enumerate(["PSF0", "PSF1", "PSF2", "PSF3"]):
                configs[k] = ConfigObj(config)
                configs[k]['event']['evtype'] = fermievtypes[TYPE]
                try:
                    Analyses[k] = Analysis(folder, configs[k], \
                        configgeneric=config,\
                        tag=TYPE, verbose = verbose)
                    if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile
                    Fits[k] = Analyses[k].CreateLikeObject()
                    Fit.addComponent(Fits[k])
                except RuntimeError, e:
                    if 'RuntimeError: gtltcube execution failed' in str(e):
                        mes.warning("Event type %s is empty! Error is %s" %
                                    (TYPE, str(e)))
            FitRunner = Analyses[0]
Exemplo n.º 8
0
                    Fits[k] = Analyses[k].CreateLikeObject()
                    Fit.addComponent(Fits[k])
                except RuntimeError, e:
                    if 'RuntimeError: gtltcube execution failed' in str(e):
                        mes.warning("Event type %s is empty! Error is %s" %
                                    (TYPE, str(e)))
            FitRunner = Analyses[0]

        elif isKey(config['ComponentAnalysis'], 'EDISP') == 'yes':
            from enrico.data import fermievtypes
            mes.info("Breaking the analysis in EDISP 0,1,2,3.")
            # Clone the configs
            # Set Summed Likelihood to True
            config['Spectrum']['SummedLike'] = 'yes'
            for k, TYPE in enumerate(["EDISP0", "EDISP1", "EDISP2", "EDISP3"]):
                configs[k] = ConfigObj(config)
                configs[k]['event']['evtype'] = fermievtypes[TYPE]
                try:
                    Analyses[k] = Analysis(folder, configs[k], \
                        configgeneric=config,\
                        tag=TYPE, verbose = verbose)
                    if not (xmlfile == ""): Analyses[k].obs.xmlfile = xmlfile
                    Fits[k] = Analyses[k].CreateLikeObject()
                    Fit.addComponent(Fits[k])
                except RuntimeError, e:
                    if 'RuntimeError: gtltcube execution failed' in str(e):
                        mes.warning("Event type %s is empty! Error is %s" %
                                    (TYPE, str(e)))
            FitRunner = Analyses[0]

        elif isKey(config['ComponentAnalysis'], 'EUnBinned') >= 0:
Exemplo n.º 9
0
def query_config():
    import os
    """Make a new config object, asking the user for required options"""
    config = ConfigObj(indent_type='\t')
    print('Please provide the following required options [default] :')
    config['out'] = os.getcwd()
    out = raw_input('Output directory [' + config['out'] + '] : ')
    if not (out == ''):
        config['out'] = out

#    Informations about the source
    config['target'] = {}
    config['target']['name'] = raw_input('Target Name : ')
    config['target']['ra'] = raw_input('Right Ascension: ')
    config['target']['dec'] = raw_input('Declination: ')
    message = (
        'Options are : PowerLaw, PowerLaw2, LogParabola, '
        'PLExpCutoff, Generic\nGeneric is design to allow the user to fit with non-supported models\n'
        'Spectral Model [PowerLaw] : ')
    config['target']['spectrum'] = 'PowerLaw'
    model = raw_input(message)
    if not (model == ''):
        config['target']['spectrum'] = model

#    informations about the ROI
    config['space'] = {}
    config['space']['xref'] = config['target']['ra']
    config['space']['yref'] = config['target']['dec']
    config['space']['rad'] = '15'
    roi = raw_input('ROI Size [15] : ')
    if not (roi == ''):
        config['space']['rad'] = roi

#    informations about the input files
    config['file'] = {}
    config['file']['spacecraft'] = DOWNLOAD_DIR + '/lat_spacecraft_merged.fits'
    ft2 = raw_input('FT2 file [' + config['file']['spacecraft'] + '] : ')
    if not (ft2 == ''):
        config['file']['spacecraft'] = ft2
    config['file']['event'] = DOWNLOAD_DIR + '/events.lis'
    ft1list = raw_input('FT1 list of files [' + config['file']['event'] +
                        '] : ')
    if not (ft1list == ''):
        config['file']['event'] = ft1list
    config['file']['xml'] = config['out'] + '/' + config['target'][
        'name'] + '_' + config['target']['spectrum'] + '_model.xml'
    tag = raw_input('tag [LAT_Analysis] : ')
    if not (tag == ''):
        config['file']['tag'] = tag
    else:
        config['file']['tag'] = 'LAT_Analysis'

#    informations about the time
    config['time'] = {}
    tmin = raw_input('Start time [239557418] : ')
    if not (tmin == ''):
        config['time']['tmin'] = tmin
    else:
        config['time']['tmin'] = '239557418'
    tmax = raw_input('End time [334165418] : ')
    if not (tmax == ''):
        config['time']['tmax'] = tmax
    else:
        config['time']['tmax'] = '334165418'

#    informations about the energy
    config['energy'] = {}
    emin = raw_input('Emin [100] : ')
    if not (emin == ''):
        config['energy']['emin'] = emin
    else:
        config['energy']['emin'] = '100'
    emax = raw_input('Emax [300000] : ')
    if not (emax == ''):
        config['energy']['emax'] = emax
    else:
        config['energy']['emax'] = '300000'

    return get_config(config)