Ejemplo n.º 1
0
    def _test_get_onoff_obs(self):
        """
        Test get_onoff_obs() function
        """
        # Set-up unbinned cslightcrv (is not run here!!!)
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']    = self._events
        lcrv['inmodel']  = self._model
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'LIN'
        lcrv['tmin']     = '2020-01-01T00:00:00'
        lcrv['tmax']     = '2020-01-01T00:05:00'
        lcrv['tbins']    = 2
        lcrv['method']   = 'ONOFF'
        lcrv['emin']     = 1.0
        lcrv['emax']     = 100.0
        lcrv['enumbins'] = 2
        lcrv['coordsys'] = 'CEL'
        lcrv['xref']     = 83.63
        lcrv['yref']     = 22.01
        lcrv['rad']      = 0.2
        lcrv['outfile']  = 'obsutils_cslightcrv_py2.fits'
        lcrv['logfile']  = 'obsutils_cslightcrv_py2.log'
        lcrv._get_parameters()

        # Get on/off observation container
        res = obsutils.get_onoff_obs(lcrv, lcrv.obs())

        # Check result
        self.test_value(res.size(), 1, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 91, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')

        # Use galactic coordinates
        lcrv['coordsys'] = 'GAL'
        lcrv['xref']     = 184.5597
        lcrv['yref']     =  -5.7892
        lcrv['chatter']  = 4

        # Get on/off observation container
        res = obsutils.get_onoff_obs(lcrv, lcrv.obs())

        # Check result
        self.test_value(res.size(), 1, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 91, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')

        # Return
        return
Ejemplo n.º 2
0
    def _test_pickeling(self):
        """
        Test cslightcrv pickeling
        """
        # Perform pickeling tests of empty class
        self._pickeling(cscripts.cslightcrv())

        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = '2020-01-01T00:00:00'
        lcrv['tmax'] = '2020-01-01T00:05:00'
        lcrv['tbins'] = 2
        lcrv['method'] = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py1_pickle.fits'
        lcrv['logfile'] = 'cslightcrv_py1_pickle.log'
        lcrv['chatter'] = 2
        lcrv['publish'] = True

        # Perform pickeling tests of filled class
        obj = self._pickeling(lcrv)

        # Run cslightcrv script and save light curve
        obj.logFileOpen()  # Make sure we get a log file
        obj.run()
        obj.save()

        # Check light curve
        self._check_light_curve('cslightcrv_py1_pickle.fits', 2)

        # Return
        return
Ejemplo n.º 3
0
def lightcurve(inobs,
               inmodel,
               srcname,
               outfile,
               map_reso,
               map_coord,
               map_fov,
               xref,
               yref,
               caldb=None,
               irf=None,
               inexclusion=None,
               edisp=False,
               tbinalg='LIN',
               tmin=None,
               tmax=None,
               mjdref=51544.5,
               tbins=20,
               tbinfile=None,
               method='3D',
               emin=1e-2,
               emax=1e+3,
               enumbins=20,
               rad=1.0,
               bkgregmin=2,
               use_model_bkg=True,
               maxoffset=4.0,
               etruemin=0.01,
               etruemax=300,
               etruebins=30,
               statistic='DEFAULT',
               calc_ts=True,
               calc_ulim=True,
               fix_srcs=True,
               fix_bkg=False,
               logfile=None,
               silent=False):
    """
    Compute a lightcurve for a given source.
    See http://cta.irap.omp.eu/ctools/users/reference_manual/cslightcrv.html

    Parameters
    ----------
    - inobs (str): Input event list or observation definition XML file.
    - inmodel (string): input model
    - srcname (str): name of the source to compute
    - outfile (str): output lightcurve file
    - map_reso (float): the resolution of the map (can be an
    astropy.unit object, or in deg)
    - map_coord (float): a skycoord object that give the center of the map
    - map_fov (float): the field of view of the map (can be an 
    astropy.unit object, or in deg)
    - xref/yref (float): the coordinates of the source (on/off) or the map. 
    - caldb (string): calibration database
    - irf (string): instrument response function
    - inexclusion (string): Optional FITS file containing a WCS map that 
    defines sky regions not to be used for background estimation (where 
    map value != 0). If the file contains multiple extensions the user may 
    specify which one to use. Otherwise, the extention EXCLUSION will be 
    used if available, or else the primary extension will be used.
    - edisp (bool): apply energy dispersion
    - tbinalg (string): <FILE|LIN|GTI> Algorithm for defining time bins.
    - tmin/tmax: Lightcurve start/stop time (UTC string, JD, MJD or MET in seconds).
    - mjdref (float): Reference Modified Julian Day (MJD) for Mission Elapsed Time (MET).
    - tbins (int): Number of time bins.
    - tbinfile (str): File defining the time binning.
    - method (string): <3D|ONOFF> Selects between 3D analysis (3D spatial/energy 
    likelihood) and ONOFF analysis (1D likelihood with background from Off regions).
    - emin,emax (float) min and max energy considered in TeV
    - enumbins (int): number of energy bins
    - rad (float): Radius of source region circle for On/Off analysis (deg)
    - bkgregmin (int): Minimum number of background regions that are required 
    for an observation in ONOFF analysis. If this number of background regions 
    is not available the observation is skipped.
    - use_model_bkg (bool): Specifies whether the background model should 
    be used for the computation of the alpha parameter and the predicted 
    background rate in the Off region that is stored in the BACKRESP column 
    of the Off spectrum when using the ONOFF method.
    - maxoffset(float): Maximum offset in degrees of source from camera 
    center to accept the observation for On/Off analysis.
    - etruemin (float): Minimum true energy to evaluate instrumental response 
    in On/Off analysis (TeV).
    - etruemax (float): Maximum true energy to evaluate instrumental response 
    in On/Off analysis (TeV).
    - etruebins (float): Number of bins per decade for true energy bins to 
    evaluate instrumental response in On/Off analysis.
    - statistic (str): Optimization statistic.
    - calc_ts (bool): Compute TS for each spectral point?
    - calc_ulim (bool): Compute upper limit for each spectral point?
    - fix_srcs (bool): Fix other sky model parameters?
    - fix_bkg (bool): Fix background model parameters?

    - silent (bool): print information or not

    Outputs
    --------
    - create lightcurve fits
    - return a lightcurve object

    """

    npix = utilities.npix_from_fov_def(map_fov, map_reso)

    lc = cscripts.cslightcrv()

    lc['inobs'] = inobs
    lc['inmodel'] = inmodel
    lc['srcname'] = srcname
    lc['outfile'] = outfile
    if caldb is not None: lc['caldb'] = caldb
    if irf is not None: lc['irf'] = irf
    if inexclusion is not None: lc['inexclusion'] = inexclusion
    lc['edisp'] = edisp
    lc['tbinalg'] = tbinalg
    if tmin is not None: lc['tmin'] = tmin
    if tmax is not None: lc['tmax'] = tmax
    if tbinfile is not None: lc['tbinfile'] = tbinfile
    lc['mjdref'] = mjdref
    lc['tbins'] = tbins
    lc['method'] = method
    lc['emin'] = emin
    lc['emax'] = emax
    lc['enumbins'] = enumbins
    lc['nxpix'] = npix
    lc['nypix'] = npix
    lc['binsz'] = map_reso.to_value('deg')
    lc['coordsys'] = 'CEL'
    lc['proj'] = 'TAN'
    lc['xref'] = xref
    lc['yref'] = yref
    lc['srcshape'] = 'CIRCLE'
    lc['rad'] = rad
    lc['bkgmethod'] = 'REFLECTED'
    lc['bkgregmin'] = bkgregmin
    lc['use_model_bkg'] = use_model_bkg
    lc['maxoffset'] = maxoffset
    lc['etruemin'] = etruemin
    lc['etruemax'] = etruemax
    lc['etruebins'] = etruebins
    lc['statistic'] = statistic
    lc['calc_ts'] = calc_ts
    lc['calc_ulim'] = calc_ulim
    lc['fix_srcs'] = fix_srcs
    lc['fix_bkg'] = fix_bkg

    if logfile is not None: lc['logfile'] = logfile

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

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

    return lc
Ejemplo n.º 4
0
def lightcurve(obsname, bkgname, srcmodel, spec, method, pars,
               emin=0.7, emax=10.0):
    """
    Generate lightcurve

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

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

    # Set file names
    if method == 'ONOFF':
        outfile = 'pks_lightcrv_%s_%s_%dbins.fits' % (analysis, method, pars['ebins'])
        logfile = 'pks_lightcrv_%s_%s_%dbins.log'  % (analysis, method, pars['ebins'])
    else:
        outfile = 'pks_lightcrv_%s_%s.fits' % (analysis, method)
        logfile = 'pks_lightcrv_%s_%s.log'  % (analysis, method)

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

        # Create GTIs for light curve
        create_lightcurve_gti(obsname_t700, 'lightcurve_gti.fits')

        # Load observations
        obs = gammalib.GObservations(_obsname)

        # Set models
        models = set_model(srcmodel, spec, bkgname)

        # Set and fix source position
        models['PKS 2155-304']['RA'].value(329.71694)   # true
        models['PKS 2155-304']['DEC'].value(-30.225588) # true
        models['PKS 2155-304']['RA'].fix()
        models['PKS 2155-304']['DEC'].fix()

        # Set and fix spectral index
        models['PKS 2155-304']['Index'].value(-3.4)
        models['PKS 2155-304']['Index'].fix()

        # Attach models
        obs.models(models)

        # Generate light curve
        lightcurve = cscripts.cslightcrv(obs)
        lightcurve['srcname']   = 'PKS 2155-304'
        lightcurve['edisp']     = pars['edisp']
        lightcurve['outfile']   = outfile
        lightcurve['tbinalg']   = 'FILE'
        lightcurve['tbinfile']  = 'lightcurve_gti.fits'
        lightcurve['method']    = method
        lightcurve['emin']      = emin
        lightcurve['emax']      = emax
        lightcurve['coordsys']  = 'CEL'
        lightcurve['xref']      = 329.71694
        lightcurve['yref']      = -30.22559
        lightcurve['rad']       =   0.2
        lightcurve['enumbins']  = pars['ebins']
        lightcurve['etruebins'] = 100
        lightcurve['logfile']   = logfile
        lightcurve['clobber']   = 4
        lightcurve['debug']     = True
        if method == 'ONOFF':
            lightcurve['statistic']     = 'WSTAT'
            lightcurve['use_model_bkg'] = False
        lightcurve.logFileOpen()
        lightcurve.execute()

    # Return
    return
Ejemplo n.º 5
0
    def _test_python(self):
        """
        Test cslightcrv from Python
        """
        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']    = self._events
        lcrv['inmodel']  = self._model
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'LIN'
        lcrv['tmin']     = '2020-01-01T00:00:00'
        lcrv['tmax']     = '2020-01-01T00:05:00'
        lcrv['tbins']    = 3
        lcrv['method']   = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin']     = 0.1
        lcrv['emax']     = 100.0
        lcrv['outfile']  = 'cslightcrv_py1.fits'
        lcrv['logfile']  = 'cslightcrv_py1.log'
        lcrv['chatter']  = 2
        lcrv['publish']  = True

        # Run cslightcrv script and save light curve
        lcrv.logFileOpen()   # Make sure we get a log file
        lcrv.run()
        lcrv.save()

        # Check light curve
        self._check_light_curve('cslightcrv_py1.fits', 3)

        # Now use FILE as time bin algorithm. For this we need first to
        # create an ASCII file. We use now 6 time bins. The ASCII file
        # is saved into the file "lightcurve_py2.dat".
        csv    = gammalib.GCsv(2,2)
        tmin   = 58849.00
        tdelta = 0.0017361
        for i in range(csv.nrows()):
            csv[i,0] = '%.5f' % (tmin +  i    * tdelta)
            csv[i,1] = '%.5f' % (tmin + (i+1) * tdelta)
        csv.save('cslightcrv_py2.dat', ' ', True)

        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']    = self._events
        lcrv['inmodel']  = self._model
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'FILE'
        lcrv['tbinfile'] = 'cslightcrv_py2.dat'
        lcrv['method']   = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin']     = 0.1
        lcrv['emax']     = 100.0
        lcrv['fix_bkg']  =  True
        lcrv['outfile']  = 'cslightcrv_py2.fits'
        lcrv['logfile']  = 'cslightcrv_py2.log'
        lcrv['chatter']  = 3

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py2.fits', 2)

        # Now we setup an observation container on input. We attached the
        # model to the observation container so that cslightcrv should
        # no longer query for the parameter.
        cta = gammalib.GCTAObservation(self._events)
        obs = gammalib.GObservations()
        obs.append(cta)
        obs.models(self._model)

        # Set-up unbinned cslightcrv from observation container. Now use
        # the GTI algorithm so that we test all timing algorithms.
        lcrv = cscripts.cslightcrv(obs)
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'GTI'
        lcrv['method']   = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin']     = 0.1
        lcrv['emax']     = 100.0
        lcrv['outfile']  = 'cslightcrv_py3.fits'
        lcrv['logfile']  = 'cslightcrv_py3.log'
        lcrv['chatter']  = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py3.fits', 1)

        # Binned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']    = self._events
        lcrv['inmodel']  = self._model
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'LIN'
        lcrv['tmin']     = '2020-01-01T00:00:00'
        lcrv['tmax']     = '2020-01-01T00:05:00'
        lcrv['tbins']    = 2
        lcrv['method']   = '3D'
        lcrv['emin']     = 0.1
        lcrv['emax']     = 100.0
        lcrv['enumbins'] = 10
        lcrv['coordsys'] = 'CEL'
        lcrv['proj']     = 'TAN'
        lcrv['xref']     = 83.63
        lcrv['yref']     = 22.01
        lcrv['nxpix']    = 20
        lcrv['nypix']    = 20
        lcrv['binsz']    = 0.02
        lcrv['outfile']  = 'cslightcrv_py4.fits'
        lcrv['logfile']  = 'cslightcrv_py4.log'
        lcrv['chatter']  = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py4.fits', 2)

        # cslightcrv with classical analysis
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']     = self._offaxis_events
        lcrv['inmodel']   = self._model_onoff
        lcrv['srcname']   = 'Crab'
        lcrv['caldb']     = self._caldb
        lcrv['irf']       = self._irf
        lcrv['tbinalg']   = 'LIN'
        lcrv['tmin']      = '2020-01-01T00:00:00'
        lcrv['tmax']      = '2020-01-01T00:05:00'
        lcrv['tbins']     = 2
        lcrv['method']    = 'ONOFF'
        lcrv['emin']      = 0.1
        lcrv['emax']      = 100.0
        lcrv['enumbins']  = 10
        lcrv['coordsys']  = 'CEL'
        lcrv['xref']      = 83.63
        lcrv['yref']      = 22.01
        lcrv['rad']       = 0.2
        lcrv['etruemin']  = 0.05
        lcrv['etruemax']  = 150.0
        lcrv['etruebins'] = 5
        lcrv['statistic'] = 'WSTAT'
        lcrv['outfile']   = 'cslightcrv_py5.fits'
        lcrv['logfile']   = 'cslightcrv_py5.log'
        lcrv['chatter']   = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py5.fits', 2)

        # Return
        return
Ejemplo n.º 6
0
    def _test_python(self):
        """
        Test cslightcrv from Python
        """
        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = '2020-01-01T00:00:00'
        lcrv['tmax'] = '2020-01-01T00:05:00'
        lcrv['tbins'] = 2
        lcrv['method'] = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py1.fits'
        lcrv['logfile'] = 'cslightcrv_py1.log'
        lcrv['chatter'] = 2
        lcrv['publish'] = True

        # Run cslightcrv script and save light curve
        lcrv.logFileOpen()  # Make sure we get a log file
        lcrv.run()
        lcrv.save()

        # Check light curve
        self._check_light_curve('cslightcrv_py1.fits', 2)

        # Now use FILE as time bin algorithm. For this we need first to
        # create an ASCII file. We use now 2 time bins. The ASCII file
        # is saved into the file "lightcurve_py2.dat".
        csv = gammalib.GCsv(2, 2)
        tmin = 58849.00
        tdelta = 0.0017361
        for i in range(csv.nrows()):
            csv[i, 0] = '%.5f' % (tmin + i * tdelta)
            csv[i, 1] = '%.5f' % (tmin + (i + 1) * tdelta)
        csv.save('cslightcrv_py2.dat', ' ', True)

        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'FILE'
        lcrv['tbinfile'] = 'cslightcrv_py2.dat'
        lcrv['method'] = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['fix_bkg'] = True
        lcrv['outfile'] = 'cslightcrv_py2.fits'
        lcrv['logfile'] = 'cslightcrv_py2.log'
        lcrv['chatter'] = 3

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py2.fits', 2)

        # Now we setup an observation container on input. We attached the
        # model to the observation container so that cslightcrv should
        # no longer query for the parameter.
        cta = gammalib.GCTAObservation(self._events)
        obs = gammalib.GObservations()
        obs.append(cta)
        obs.models(self._model)

        # Set-up unbinned cslightcrv from observation container. Now use
        # the GTI algorithm so that we test all timing algorithms.
        lcrv = cscripts.cslightcrv(obs)
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'GTI'
        lcrv['method'] = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py3.fits'
        lcrv['logfile'] = 'cslightcrv_py3.log'
        lcrv['chatter'] = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py3.fits', 1)

        # Binned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = '2020-01-01T00:00:00'
        lcrv['tmax'] = '2020-01-01T00:05:00'
        lcrv['tbins'] = 2
        lcrv['method'] = '3D'
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['enumbins'] = 3
        lcrv['coordsys'] = 'CEL'
        lcrv['proj'] = 'TAN'
        lcrv['xref'] = 83.63
        lcrv['yref'] = 22.01
        lcrv['nxpix'] = 10
        lcrv['nypix'] = 10
        lcrv['binsz'] = 0.04
        lcrv['outfile'] = 'cslightcrv_py4.fits'
        lcrv['logfile'] = 'cslightcrv_py4.log'
        lcrv['chatter'] = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py4.fits', 2)

        # cslightcrv with classical analysis
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._offaxis_events
        lcrv['inmodel'] = self._model_onoff
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = '2020-01-01T00:00:00'
        lcrv['tmax'] = '2020-01-01T00:05:00'
        lcrv['tbins'] = 2
        lcrv['method'] = 'ONOFF'
        lcrv['use_model_bkg'] = False  # Needed even if WSTAT
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['enumbins'] = 2
        lcrv['coordsys'] = 'CEL'
        lcrv['xref'] = 83.63
        lcrv['yref'] = 22.01
        lcrv['rad'] = 0.2
        lcrv['etruemin'] = 1.0
        lcrv['etruemax'] = 100.0
        lcrv['etruebins'] = 5
        lcrv['statistic'] = 'WSTAT'
        lcrv['outfile'] = 'cslightcrv_py5.fits'
        lcrv['logfile'] = 'cslightcrv_py5.log'
        lcrv['chatter'] = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py5.fits', 2)

        # Set-up cslightcrv without multiprocessing
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = '2020-01-01T00:00:00'
        lcrv['tmax'] = '2020-01-01T00:05:00'
        lcrv['tbins'] = 2
        lcrv['method'] = '3D'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 1.0
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py6.fits'
        lcrv['logfile'] = 'cslightcrv_py6.log'
        lcrv['chatter'] = 2
        lcrv['publish'] = True
        lcrv['nthreads'] = 1

        # Run cslightcrv script and save light curve
        lcrv.logFileOpen()  # Make sure we get a log file
        lcrv.run()
        lcrv.save()

        # Check light curve
        self._check_light_curve('cslightcrv_py6.fits', 2)

        # Return
        return
Ejemplo n.º 7
0
    def _test_python(self):
        """
        Test cslightcrv from Python
        """
        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = 51544.50
        lcrv['tmax'] = 51544.53
        lcrv['tbins'] = 3
        lcrv['enumbins'] = 0
        lcrv['emin'] = 0.1
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py1.fits'
        lcrv['logfile'] = 'cslightcrv_py1.log'
        lcrv['chatter'] = 2

        # Run cslightcrv script and save light curve
        lcrv.logFileOpen()  # Make sure we get a log file
        lcrv.run()
        lcrv.save()

        # Check light curve
        self._check_light_curve('cslightcrv_py1.fits', 3)

        # Now use FILE as time bin algorithm. For this we need first to
        # create an ASCII file. We use now 6 time bins. The ASCII file
        # is saved into the file "lightcurve_py2.dat".
        csv = gammalib.GCsv(2, 2)
        tmin = 51544.50
        tdelta = 0.01
        for i in range(csv.nrows()):
            csv[i, 0] = '%.5f' % (tmin + i * tdelta)
            csv[i, 1] = '%.5f' % (tmin + (i + 1) * tdelta)
        csv.save('cslightcrv_py2.dat', ' ', True)

        # Set-up unbinned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'FILE'
        lcrv['tbinfile'] = 'cslightcrv_py2.dat'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 0.1
        lcrv['emax'] = 100.0
        lcrv['fix_bkg'] = True
        lcrv['outfile'] = 'cslightcrv_py2.fits'
        lcrv['logfile'] = 'cslightcrv_py2.log'
        lcrv['chatter'] = 3

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py2.fits', 2)

        # Now we setup an observation container on input. We attached the
        # model to the observation container so that cslightcrv should
        # no longer query for the parameter.
        cta = gammalib.GCTAObservation(self._events)
        obs = gammalib.GObservations()
        obs.append(cta)
        obs.models(self._model)

        # Set-up unbinned cslightcrv from observation container. Now use
        # the GTI algorithm so that we test all timing algorithms.
        lcrv = cscripts.cslightcrv(obs)
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'GTI'
        lcrv['enumbins'] = 0
        lcrv['emin'] = 0.1
        lcrv['emax'] = 100.0
        lcrv['outfile'] = 'cslightcrv_py3.fits'
        lcrv['logfile'] = 'cslightcrv_py3.log'
        lcrv['chatter'] = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py3.fits', 1)

        # Finally we set-up a binned cslightcrv
        lcrv = cscripts.cslightcrv()
        lcrv['inobs'] = self._events
        lcrv['inmodel'] = self._model
        lcrv['srcname'] = 'Crab'
        lcrv['caldb'] = self._caldb
        lcrv['irf'] = self._irf
        lcrv['tbinalg'] = 'LIN'
        lcrv['tmin'] = 51544.50
        lcrv['tmax'] = 51544.53
        lcrv['tbins'] = 2
        lcrv['emin'] = 0.1
        lcrv['emax'] = 100.0
        lcrv['enumbins'] = 10
        lcrv['coordsys'] = 'CEL'
        lcrv['proj'] = 'TAN'
        lcrv['xref'] = 83.63
        lcrv['yref'] = 22.01
        lcrv['nxpix'] = 20
        lcrv['nypix'] = 20
        lcrv['binsz'] = 0.02
        lcrv['outfile'] = 'cslightcrv_py4.fits'
        lcrv['logfile'] = 'cslightcrv_py4.log'
        lcrv['chatter'] = 4

        # Execute cslightcrv script
        lcrv.execute()

        # Check light curve
        self._check_light_curve('cslightcrv_py4.fits', 2)

        # Return
        return
Ejemplo n.º 8
0
    def _test_get_stacked_obs(self):
        """
        Test get_stacked_obs() function
        """
        # Set-up unbinned cslightcrv (is not run here!!!)
        lcrv = cscripts.cslightcrv()
        lcrv['inobs']    = self._events
        lcrv['inmodel']  = self._model
        lcrv['srcname']  = 'Crab'
        lcrv['caldb']    = self._caldb
        lcrv['irf']      = self._irf
        lcrv['tbinalg']  = 'LIN'
        lcrv['tmin']     = '2020-01-01T00:00:00'
        lcrv['tmax']     = '2020-01-01T00:05:00'
        lcrv['tbins']    = 2
        lcrv['method']   = '3D'
        lcrv['emin']     = 1.0
        lcrv['emax']     = 100.0
        lcrv['enumbins'] = 3
        lcrv['coordsys'] = 'CEL'
        lcrv['proj']     = 'TAN'
        lcrv['xref']     = 83.63
        lcrv['yref']     = 22.01
        lcrv['binsz']    = 0.1
        lcrv['nxpix']    = 10
        lcrv['nypix']    = 10
        lcrv['outfile']  = 'obsutils_cslightcrv_py1.fits'
        lcrv['logfile']  = 'obsutils_cslightcrv_py1.log'
        lcrv._get_parameters()

        # Get stacked observation container
        res = obsutils.get_stacked_obs(lcrv, lcrv.obs())

        # Apply energy dispersion
        res[0].response().apply_edisp(True)

        # Check result
        self.test_value(res.size(), 1, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 96, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')
        self.test_assert(not res[0].response().use_edisp(),
                         'Check that no energy dispersion is used')

        # Set energy dispersion
        lcrv['edisp'] = True

        # Get stacked observation container
        res = obsutils.get_stacked_obs(lcrv, lcrv.obs())

        # Apply energy dispersion
        res[0].response().apply_edisp(True)

        # Check result
        self.test_value(res.size(), 1, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 96, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')
        self.test_assert(res[0].response().use_edisp(),
                         'Check that energy dispersion is used')

        # Increase chatter level
        lcrv['edisp']   = False
        lcrv['chatter'] = 4

        # Get stacked observation container
        res = obsutils.get_stacked_obs(lcrv, lcrv.obs())

        # Apply energy dispersion
        res[0].response().apply_edisp(True)

        # Check result
        self.test_value(res.size(), 1, 'Check number of observations')
        self.test_value(res.models().size(), 2, 'Check number of models')
        self.test_value(res.nobserved(), 96, 'Check number of observed events')
        self.test_value(res.npred(), 0.0, 'Check number of predicted events')
        self.test_assert(not res[0].response().use_edisp(),
                         'Check that no energy dispersion is used')

        # Return
        return