예제 #1
0
    def _test_python(self):
        """
        Test ctbutterfly from Python
        """
        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs'] = self._events
        butterfly['inmodel'] = self._model
        butterfly['srcname'] = 'Crab'
        butterfly['caldb'] = self._caldb
        butterfly['irf'] = self._irf
        butterfly['emin'] = 0.1
        butterfly['emax'] = 100.0
        butterfly['outfile'] = 'ctbutterfly_py1.dat'
        butterfly['logfile'] = 'ctbutterfly_py1.log'
        butterfly['chatter'] = 2

        # Run ctbutterfly tool
        butterfly.logFileOpen()  # Make sure we get a log file
        butterfly.run()
        butterfly.save()

        # Check result file
        self._check_result_file('ctbutterfly_py1.dat')

        # Return
        return
예제 #2
0
    def test_functional(self):
        """
        Test ctbutterfly functionnality.
        """
        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly["inobs"]   = self.events_name
        butterfly["inmodel"] = self.model_name
        butterfly["srcname"] = "Crab"
        butterfly["caldb"]   = self.caldb
        butterfly["irf"]     = self.irf
        butterfly["emin"]    = 0.1
        butterfly["emax"]    = 100
        butterfly["outfile"] = "butterfly.txt"

        # Run tool
        self.test_try("Run ctbutterfly")
        try:
            butterfly.run()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in butterfly.")

        # Save results
        self.test_try("Save results")
        try:
            butterfly.save()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in saving results.")

        # Return
        return
예제 #3
0
    def ctbutterfly(self,log=False,debug=False, **kwargs):
        '''
        Create butterfly instance with given parameters
        Parameters
        ---------
        log  : save or not the log file
        debug  : debug mode or not. This will print a lot of information
        '''
        self.info("Running ctbuttefly to make butterfly plot")

        self.ctbutterfly = ct.ctbutterfly(self.like.obs())
        
        self._fill_app( self.ctbutterfly,log=log,debug=debug, **kwargs)

        self.ctbutterfly["srcname"]=self.config["target"]["name"]
        self.ctbutterfly["outfile"] = self.config["target"]["name"]+"_butterfly.dat "

        if self.verbose:
            print self.ctbutterfly

        self.ctbutterfly.run()

        self.ctbutterfly.save()
        self.info("Saved butterfly plot to {0:s}".format(self.ctbutterfly["outfile"]))


        self.ctbutterfly
예제 #4
0
def butterfly(obsname, bkgname, srcmodel, spec, pars, emin=0.3, emax=10.0,
              fitname='pks_results', buttername='pks_butterfly'):
    """
    Generate butterfly

    Parameters
    ----------
    obsname : str
        Observation definition XML file
    bkgname : str
        Background model definition XML file
    srcmodel : str
        Source model
    spec : str
        Spectral model
    pars : dict
        Dictionary of analysis parameters
    emin : float, optional
        Minimum energy (TeV)
    emax : float, optional
        Maximum energy (TeV)
    fitname : str, optional
        Fit result prefix
    buttername : str, optional
        Butterfly diagram result prefix
    """
    # Set observation name
    _obsname = set_observation(obsname, srcmodel, bkgname, pars)

    # Set analysis
    analysis = set_analysis(srcmodel, bkgname, spec, pars)

    # Set file names
    inmodel = '%s_%s.xml'   % (fitname, analysis)
    outfile = '%s_%s.txt' % (buttername, analysis)
    logfile = '%s_%s.log' % (buttername, analysis)

    # Continue only if result file does not exist
    if not os.path.isfile(outfile) and os.path.isfile(inmodel):

        # Generate butterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs']    = _obsname
        butterfly['inmodel']  = inmodel
        butterfly['srcname']  = 'PKS 2155-304'
        butterfly['outfile']  = outfile
        butterfly['edisp']    = pars['edisp']
        butterfly['emin']     = emin
        butterfly['emax']     = emax
        butterfly['logfile']  = logfile
        butterfly['debug']    = True
        butterfly.logFileOpen()
        butterfly.execute()

    # Return
    return
예제 #5
0
def butterfly(obsname, bkgname, srcmodel, spec, pars, alpha=1.0):
    """
    Generate butterfly

    Parameters
    ----------
    obsname : str
        Observation definition XML file
    bkgname : str
        Background model definition XML file
    srcmodel : str
        Source model
    spec : str
        Spectral model
    pars : dict
        Dictionary of analysis parameters
    alpha : float, optional
        Template map scaling factor
    """
    # Set observation name
    _obsname = set_observation(obsname, srcmodel, bkgname, pars)

    # Set analysis
    analysis = set_analysis(srcmodel, bkgname, spec, pars, alpha=alpha)

    # Set file names
    inmodel = 'rx_results_%s.xml' % analysis
    outfile = 'rx_butterfly_%s.txt' % analysis
    logfile = 'rx_butterfly_%s.log' % analysis

    # Continue only if result file does not exist
    if not os.path.isfile(outfile) and os.path.isfile(inmodel):

        # Generate butterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs'] = _obsname
        butterfly['inmodel'] = inmodel
        butterfly['srcname'] = 'RX J1713.7-3946'
        butterfly['outfile'] = outfile
        butterfly['edisp'] = pars['edisp']
        butterfly['emin'] = 0.3
        butterfly['emax'] = 50.0
        butterfly['logfile'] = logfile
        butterfly['debug'] = True
        butterfly.logFileOpen()
        butterfly.execute()

    # Return
    return
예제 #6
0
def makeButterfly(cfg):
    """
    Computes butterfly (only for power law, fix in the future?)
    """
    outputdir = cfg.getValue('general', 'outputdir')

    butt = ctools.ctbutterfly()
    if cfg.getValue('general', 'anatype') == 'unbinned':
        butt["inobs"] = outputdir + '/' + cfg.getValue('ctselect', 'output')
        butt["inmodel"] = outputdir + '/' + cfg.getValue('ctlike', 'output')
    elif cfg.getValue('general', 'anatype') == 'unbinned':
        butt["inobs"] = outputdir + '/' + cfg.getValue('ctbin', 'output')
        butt["inmodel"] = outputdir + '/' + cfg.getValue('ctlike', 'output')
        butt["expcube"] = outputdir + '/' + cfg.getValue('ctexpcube', 'output')
        butt["psfcube"] = outputdir + '/' + cfg.getValue('ctpsfcube', 'output')
        butt["bkgcube"] = outputdir + '/' + \
            cfg.getValue('ctbkgcube', 'output_cube')
    else:
        Utilities.warning('Unlnown type: {}'.format(
            cfg.getValue('general', 'anatype')))
        sys.exit()

    butt["outfile"] = outputdir + '/' + cfg.getValue('ctbutterfly', 'output')
    butt["emin"] = cfg.getValue('csspec', 'emin')
    butt["emax"] = cfg.getValue('csspec', 'emax')
    butt["enumbins"] = 100
    butt["srcname"] = ""
    butt["ebinalg"] = "LOG"
    if cfg.getValue('general', 'edisp'):
        butt["edisp"] = True
    else:
        butt["edisp"] = True
    if cfg.getValue('general', 'debug') is True:
        butt["debug"] = True

    butt.run()
    butt.save()
예제 #7
0
def stackedPipeline(
    name="Crab",
    obsfile="index.xml",
    l=0.01,
    b=0.01,
    emin=0.1,
    emax=100.0,
    enumbins=20,
    nxpix=200,
    nypix=200,
    binsz=0.02,
    coordsys="CEL",
    proj="CAR",
    caldb="prod2",
    irf="acdc1a",
    debug=False,
    inmodel="Crab",
    outmodel="results",
):
    """
    Simulation and stacked analysis pipeline
    
    Parameters
    ----------
    obs : `~gammalib.GObservations`
    Observation container
    ra : float, optional
    Right Ascension of counts cube centre (deg)
    dec : float, optional
    Declination of Region of counts cube centre (deg)
    emin : float, optional
    Minimum energy (TeV)
    emax : float, optional
    Maximum energy (TeV)
    enumbins : int, optional
    Number of energy bins
    nxpix : int, optional
    Number of pixels in X axis
    nypix : int, optional
    Number of pixels in Y axis
    binsz : float, optional
    Pixel size (deg)
    coordsys : str, optional
    Coordinate system
    proj : str, optional
    Coordinate projection
    debug : bool, optional
    Debug function
    """

    # Bin events into counts map
    bin = ctools.ctbin()
    bin["inobs"] = obsfile
    bin["ebinalg"] = "LOG"
    bin["emin"] = emin
    bin["emax"] = emax
    bin["enumbins"] = enumbins
    bin["nxpix"] = nxpix
    bin["nypix"] = nypix
    bin["binsz"] = binsz
    bin["coordsys"] = coordsys
    bin["proj"] = proj
    bin["xref"] = l
    bin["yref"] = b
    bin["debug"] = debug
    bin["outobs"] = "cntcube.fits"
    bin.execute()
    print("Datacube : done!")

    # Create exposure cube
    expcube = ctools.ctexpcube()
    # expcube['incube']=bin.obs()
    expcube["inobs"] = obsfile
    expcube["incube"] = "NONE"
    expcube["ebinalg"] = "LOG"
    expcube["caldb"] = caldb
    expcube["irf"] = irf
    expcube["emin"] = emin
    expcube["emax"] = emax
    expcube["enumbins"] = enumbins
    expcube["nxpix"] = nxpix
    expcube["nypix"] = nypix
    expcube["binsz"] = binsz
    expcube["coordsys"] = coordsys
    expcube["proj"] = proj
    expcube["xref"] = l
    expcube["yref"] = b
    expcube["debug"] = debug
    expcube["outcube"] = "cube_exp.fits"
    expcube.execute()
    print("Expcube : done!")

    # Create PSF cube
    psfcube = ctools.ctpsfcube()
    psfcube["inobs"] = obsfile
    psfcube["incube"] = "NONE"
    psfcube["ebinalg"] = "LOG"
    psfcube["caldb"] = caldb
    psfcube["irf"] = irf
    psfcube["emin"] = emin
    psfcube["emax"] = emax
    psfcube["enumbins"] = enumbins
    psfcube["nxpix"] = 10
    psfcube["nypix"] = 10
    psfcube["binsz"] = 1.0
    psfcube["coordsys"] = coordsys
    psfcube["proj"] = proj
    psfcube["xref"] = l
    psfcube["yref"] = b
    psfcube["debug"] = debug
    psfcube["outcube"] = "psf_cube.fits"
    psfcube.execute()
    print("Psfcube : done!")

    edispcube = ctools.ctedispcube()
    edispcube["inobs"] = obsfile
    edispcube["ebinalg"] = "LOG"
    edispcube["incube"] = "NONE"
    edispcube["caldb"] = caldb
    edispcube["irf"] = irf
    edispcube["xref"] = l
    edispcube["yref"] = b
    edispcube["proj"] = proj
    edispcube["coordsys"] = coordsys
    edispcube["binsz"] = 1.0
    edispcube["nxpix"] = 10
    edispcube["nypix"] = 10
    edispcube["emin"] = emin
    edispcube["emax"] = emax
    edispcube["enumbins"] = enumbins
    edispcube["outcube"] = "edisp_cube.fits"
    edispcube["debug"] = debug
    edispcube.execute()
    print("Edispcube : done!")

    # Create background cube
    bkgcube = ctools.ctbkgcube()
    bkgcube["inobs"] = obsfile
    bkgcube["incube"] = "cntcube.fits"
    bkgcube["caldb"] = caldb
    bkgcube["irf"] = irf
    bkgcube["debug"] = debug
    bkgcube["inmodel"] = str(inmodel)
    bkgcube["outcube"] = "bkg_cube.fits"
    bkgcube["outmodel"] = "bkg_cube.xml"
    bkgcube.execute()
    print("Bkgcube : done!")

    # Fix the instrumental background parameters
    bkgcube.models()["BackgroundModel"]["Prefactor"].fix()
    bkgcube.models()["BackgroundModel"]["Index"].fix()
    #
    #    # Attach background model to observation container
    bin.obs().models(bkgcube.models())
    #
    #
    #    # Set Exposure and Psf cube for first CTA observation
    #    # (ctbin will create an observation with a single container)
    bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(),
                          edispcube.edispcube(), bkgcube.bkgcube())

    # Perform maximum likelihood fitting
    like = ctools.ctlike(bin.obs())
    #    like['inmodel']='bkg_cube.xml'
    like["edisp"] = True
    #    like['edispcube'] = 'edisp_cube.fits'
    #    like['expcube'] = 'cube_exp.fits'
    #    like['psfcube'] = 'psf_cube.fits'
    #    like['bkgcube'] = 'bkg_cube.fits'
    like["outmodel"] = str(outmodel)
    #    like['outcovmat']=inmodel+'_covmat.txt'
    like["debug"] = debug  # Switch this always on for results in console
    # like['statistic']='CSTAT'
    like.execute()
    print("Likelihood : done!")

    # Set the best-fit models (from ctlike) for the counts cube
    bin.obs().models(like.obs().models())

    # Obtain the best-fit butterfly
    try:
        butterfly = ctools.ctbutterfly(bin.obs())
        butterfly["srcname"] = name
        butterfly["inmodel"] = str(outmodel)
        butterfly["edisp"] = True
        butterfly["emin"] = emin
        butterfly["emax"] = emax
        butterfly["outfile"] = str(outmodel.parent) + "/" + str(
            outmodel.stem) + ".txt"
        butterfly[
            "debug"] = debug  # Switch this always on for results in console
        # like['statistic']='CSTAT'
        butterfly.execute()
        print("Butterfly : done!")
    except:
        print("I COULDN'T CALCULATE THE BUTTERFLY....")

    # Extract the spectrum
    try:
        csspec = cscripts.csspec(bin.obs())
        csspec["srcname"] = name
        csspec["inmodel"] = str(outmodel)
        csspec["method"] = "AUTO"
        csspec["ebinalg"] = "LOG"
        csspec["emin"] = emin
        csspec["emax"] = emax
        csspec["enumbins"] = 10
        csspec["edisp"] = True
        csspec["outfile"] = str(outmodel.parent) + "/" + str(
            outmodel.stem) + ".fits"
        csspec["debug"] = debug  # Switch this always on for results in console
        csspec.execute()
        print("Csspec : done!")
    except:
        print("I COULDN'T CALCULATE THE SPECTRUM....")

    # Return
    return
예제 #8
0
import gammalib
import ctools
import cscripts

debug = True

butterfly = ctools.ctbutterfly()
butterfly['inobs'] = 'cntcube.fits'
butterfly['inmodel'] = 'stacked_results_iem_add3_ext5.xml'
butterfly['srcname'] = 'Src001'
butterfly['expcube'] = 'expcube.fits'
butterfly['psfcube'] = 'psfcube.fits'
butterfly['bkgcube'] = 'bkgcube.fits'
butterfly['outfile'] = 'butterfly_iem_add3_ext5.txt'
butterfly['emin'] = 0.1
butterfly['emax'] = 100.0
butterfly['debug'] = debug
butterfly.execute()

spec = cscripts.csspec()
spec['inobs'] = 'cntcube.fits'
spec['inmodel'] = 'stacked_results_iem_add3_ext5.xml'
spec['srcname'] = 'Src001'
spec['expcube'] = 'expcube.fits'
spec['psfcube'] = 'psfcube.fits'
spec['bkgcube'] = 'bkgcube.fits'
spec['outfile'] = 'spectrum_gc_iem_add3_ext5.fits'
spec['method'] = 'AUTO'
spec['ebinalg'] = 'LOG'
spec['emin'] = 0.1
spec['emax'] = 100.0
예제 #9
0
def butterfly(inobs,
              inmodel,
              srcname,
              outfile,
              emin=5e-3,
              emax=5e+3,
              enumbins=100,
              ebinalg='LOG',
              expcube=None,
              psfcube=None,
              bkgcube=None,
              edispcube=None,
              caldb=None,
              irf=None,
              edisp=False,
              fit=False,
              method='GAUSSIAN',
              confidence=0.68,
              statistic='DEFAULT',
              like_accuracy=0.005,
              max_iter=50,
              matrix='NONE',
              logfile=None,
              silent=False):
    """
    Computes butterfly diagram for a given spectral model.
    See http://cta.irap.omp.eu/ctools/users/reference_manual/ctbutterfly.html

    Parameters
    ----------
    - inobs (string): input observation file
    - inmodel (string): input model
    - srcname (str): name of the source to compute
    - outfile (str): output spectrum file
    - emin,emax (float) min and max energy considered in TeV
    - enumbins (int): number of energy bins
    - ebinalg (string): energy bining algorithm
    - modcube (string): model map cube
    - expcube (string): exposure map cube
    - psfcube (string): psfcube 
    - bkgcube (string): background cube
    - edispcube (string): energy dispersion cube
    - caldb (string): calibration database
    - irf (string): instrument response function
    - edisp (bool): apply energy dispersion
    - fit (bool): Performs maximum likelihood fitting of input 
    model ignoring any provided covariance matrix.
    - method (string): method for butterfly contours
    - confidence (float): confidence limit
    - statistic (str): Optimization statistic.
    - like_accuracy (float): Absolute accuracy of maximum likelihood value
    - max_iter (int): Maximum number of fit iterations.
    - matrix (string): fir covariance matrix
    - silent (bool): print information or not

    Outputs
    --------
    - create butterfly ascii file

    """

    but = ctools.ctbutterfly()

    but['inobs'] = inobs
    but['inmodel'] = inmodel
    but['srcname'] = srcname
    if expcube is not None: but['expcube'] = expcube
    if psfcube is not None: but['psfcube'] = psfcube
    if edispcube is not None: but['edispcube'] = edispcube
    if bkgcube is not None: but['bkgcube'] = bkgcube
    if caldb is not None: but['caldb'] = caldb
    if irf is not None: but['irf'] = irf
    but['edisp'] = edisp
    but['outfile'] = outfile
    but['fit'] = fit
    but['method'] = method
    but['confidence'] = confidence
    but['statistic'] = statistic
    but['like_accuracy'] = like_accuracy
    but['max_iter'] = max_iter
    but['matrix'] = matrix
    but['ebinalg'] = ebinalg
    but['emin'] = emin
    but['emax'] = emax
    but['enumbins'] = enumbins
    but['ebinfile'] = 'NONE'
    if logfile is not None: but['logfile'] = logfile

    if logfile is not None: but.logFileOpen()
    but.execute()
    if logfile is not None: but.logFileClose()

    if not silent:
        print(but)
        print('')

    return but
예제 #10
0
    def _test_python(self):
        """
        Test ctbutterfly from Python
        """
        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs'] = self._events
        butterfly['inmodel'] = self._model
        butterfly['srcname'] = 'Crab'
        butterfly['caldb'] = self._caldb
        butterfly['irf'] = self._irf
        butterfly['emin'] = 0.1
        butterfly['emax'] = 100.0
        butterfly['outfile'] = 'ctbutterfly_py1.dat'
        butterfly['logfile'] = 'ctbutterfly_py1.log'
        butterfly['chatter'] = 2

        # Run ctbutterfly tool
        butterfly.logFileOpen()  # Make sure we get a log file
        butterfly.run()
        butterfly.save()

        # Check result file
        self._check_result_file('ctbutterfly_py1.dat')

        # Check CSV result
        self.test_value(butterfly.butterfly().nrows(), 100,
                        'Check for 100 rows in CSV file')
        self.test_value(butterfly.butterfly().ncols(), 4,
                        'Check for 4 columns in CSV file')

        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs'] = self._events
        butterfly['inmodel'] = self._model
        butterfly['srcname'] = 'Crab'
        butterfly['caldb'] = self._caldb
        butterfly['irf'] = self._irf
        butterfly['method'] = 'ENVELOPE'
        butterfly['emin'] = 0.1
        butterfly['emax'] = 100.0
        butterfly['fit'] = True
        butterfly['outfile'] = 'ctbutterfly_py2.dat'
        butterfly['logfile'] = 'ctbutterfly_py2.log'
        butterfly['chatter'] = 3

        # Execute ctbutterfly tool
        butterfly.logFileOpen()  # Make sure we get a log file
        butterfly.execute()

        # Check result file
        self._check_result_file('ctbutterfly_py2.dat')

        # Recover observation container
        #obs = butterfly.obs()

        # TODO: Do someting test on observation container

        # Copy ctbkgcube tool and execute copy
        cpy_butterfly = butterfly
        cpy_butterfly['outfile'] = 'ctbutterfly_py3.dat'
        cpy_butterfly['logfile'] = 'ctbutterfly_py3.log'
        cpy_butterfly['chatter'] = 4

        # Execute ctbutterfly tool
        cpy_butterfly.logFileOpen()  # Make sure we get a log file
        cpy_butterfly.execute()

        # Check result file
        self._check_result_file('ctbutterfly_py3.dat')

        # Clear ctbkgcube tool
        butterfly.clear()

        # TODO: Do some test after clearing

        # Return
        return
예제 #11
0
import gammalib
예제 #12
0
    def _test_python(self):
        """
        Test ctbutterfly from Python
        """
        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs']   = self._events
        butterfly['inmodel'] = self._model
        butterfly['srcname'] = 'Crab'
        butterfly['caldb']   = self._caldb
        butterfly['irf']     = self._irf
        butterfly['emin']    = 0.1
        butterfly['emax']    = 100.0
        butterfly['outfile'] = 'ctbutterfly_py1.dat'
        butterfly['logfile'] = 'ctbutterfly_py1.log'
        butterfly['chatter'] = 2

        # Run ctbutterfly tool
        butterfly.logFileOpen()   # Make sure we get a log file
        butterfly.run()
        butterfly.save()

        # Check result file
        self._check_result_file('ctbutterfly_py1.dat')

        # Set-up ctbutterfly
        butterfly = ctools.ctbutterfly()
        butterfly['inobs']   = self._events
        butterfly['inmodel'] = self._model
        butterfly['srcname'] = 'Crab'
        butterfly['caldb']   = self._caldb
        butterfly['irf']     = self._irf
        butterfly['method']  = 'ENVELOPE'
        butterfly['emin']    = 0.1
        butterfly['emax']    = 100.0
        butterfly['fit']     = True
        butterfly['outfile'] = 'ctbutterfly_py2.dat'
        butterfly['logfile'] = 'ctbutterfly_py2.log'
        butterfly['chatter'] = 3

        # Execute ctbutterfly tool
        butterfly.logFileOpen()   # Make sure we get a log file
        butterfly.execute()

        # Check result file
        self._check_result_file('ctbutterfly_py2.dat')

        # Recover observation container
        #obs = butterfly.obs()

        # TODO: Do someting test on observation container

        # Copy ctbkgcube tool and execute copy
        cpy_butterfly = butterfly
        cpy_butterfly['outfile'] = 'ctbutterfly_py3.dat'
        cpy_butterfly['logfile'] = 'ctbutterfly_py3.log'
        cpy_butterfly['chatter'] = 4

        # Execute ctbutterfly tool
        cpy_butterfly.logFileOpen()   # Make sure we get a log file
        cpy_butterfly.execute()

        # Check result file
        self._check_result_file('ctbutterfly_py3.dat')

        # Clear ctbkgcube tool
        butterfly.clear()

        # TODO: Do some test after clearing

        # Return
        return