Example #1
0
def show_pha():
    """
    Show PHA spectrum
    """
    # Set usage string
    usage = 'show_pha.py [-p plotfile] file'

    # Set default options
    options = [{'option': '-p', 'value': ''}]

    # Get arguments and options from command line arguments
    args, options = cscripts.ioutils.get_args_options(options, usage)

    # Extract script parameters from options
    plotfile = options[0]['value']

    # Load PHA spectrum
    pha = gammalib.GPha(args[0])

    # Plot PHA spectrum
    plot_pha(pha, plotfile)

    # Return
    return
Example #2
0
        plt.show()

    # Return
    return


# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':

    # Print usage information
    usage = 'Usage: show_pha.py filename [file]'
    if len(sys.argv) < 2:
        print(usage)
        sys.exit()

    # Check if plotting in file is requested
    plotfile = ''
    if len(sys.argv) == 3:
        plotfile = sys.argv[2]

    # Extract parameters
    filename = sys.argv[1]

    # Load PHA spectrum
    pha = gammalib.GPha(filename)

    # Show PHA spectrum
    show_pha(pha, plotfile=plotfile)
Example #3
0
    def _select_onoff_obs(self, obs, emin, emax):
        """
        Select an energy interval from one CTA On/Off observation

        Parameters
        ----------
        obs : `~gammalib.GCTAOnOffObservation`
            Minimum energy
        emin : `~gammalib.GEnergy()`
            Minimum energy
        emax : `~gammalib.GEnergy()`
            Maximum energy

        Returns
        -------
        obs : `~gammalib.GCTAOnOffObservation`
            CTA On/Off observation
        """
        # Select energy bins in etrue and ereco. All etrue energy bins are
        # selected. A 0.1% margin is added for reconstructed energies to
        # accomodate for rounding errors.
        etrue = obs.rmf().etrue()
        ereco = gammalib.GEbounds()
        itrue = [i for i in range(obs.rmf().etrue().size())]
        ireco = []
        for i in range(obs.rmf().emeasured().size()):
            ereco_bin_min = obs.rmf().emeasured().emin(i)
            ereco_bin_max = obs.rmf().emeasured().emax(i)
            if ereco_bin_min * 1.001 >= emin and ereco_bin_max * 0.999 <= emax:
                ereco.append(ereco_bin_min, ereco_bin_max)
                ireco.append(i)

        # Extract PHA
        pha_on = gammalib.GPha(ereco)
        pha_off = gammalib.GPha(ereco)
        pha_on.exposure(obs.on_spec().exposure())
        pha_off.exposure(obs.on_spec().exposure())
        for idst, isrc in enumerate(ireco):
            # On
            pha_on[idst] = obs.on_spec()[isrc]
            pha_on.areascal(idst, obs.on_spec().areascal(isrc))
            pha_on.backscal(idst, obs.on_spec().backscal(isrc))
            # Off
            pha_off[idst] = obs.off_spec()[isrc]
            pha_off.areascal(idst, obs.off_spec().areascal(isrc))
            pha_off.backscal(idst, obs.off_spec().backscal(isrc))

        # Extract BACKRESP
        pha_backresp = obs.off_spec()['BACKRESP']
        backresp = []
        for idst, isrc in enumerate(ireco):
            backresp.append(pha_backresp[isrc])
        pha_off.append('BACKRESP', backresp)

        # Extract ARF
        arf = gammalib.GArf(etrue)
        for idst, isrc in enumerate(itrue):
            arf[idst] = obs.arf()[isrc]

        # Extract RMF
        rmf = gammalib.GRmf(etrue, ereco)
        for idst_true, isrc_true in enumerate(itrue):
            for idst_reco, isrc_reco in enumerate(ireco):
                rmf[idst_true, idst_reco] = obs.rmf()[isrc_true, isrc_reco]

        # Set On/Off observations
        obsid = obs.id()
        statistic = obs.statistic()
        instrument = obs.instrument()
        obs = gammalib.GCTAOnOffObservation(pha_on, pha_off, arf, rmf)
        obs.id(obsid)
        obs.statistic(statistic)
        obs.instrument(instrument)

        # Return observation
        return obs