aboutsummaryrefslogtreecommitdiffstats
path: root/acispy/spectrum.py
blob: 562e73edb37237db61278e37470e1066a659b213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license

"""
Chandra ACIS spectrum.
"""


from astropy.io import fits

from .acis import ACIS


class Spectrum:
    """
    Chandra ACIS spectrum
    """
    def __init__(self, filepath):
        self.filepath = filepath
        self.fitsobj = fits.open(filepath)
        ext_spec = self.fitsobj["SPECTRUM"]
        self.header = ext_spec.header
        # spectral data
        self.channel = ext_spec.data.columns["CHANNEL"].array
        self.counts = ext_spec.data.columns["COUNTS"].array
        # spectral keywords
        self.EXPOSURE = self.header.get("EXPOSURE")
        self.BACKSCAL = self.header.get("BACKSCAL")

    def calc_pb_flux(self, elow=9500, ehigh=12000):
        """
        Calculate the particle background flux:
            flux = counts / exposure / area

        Parameters
        ----------
        elow, ehigh : float, optional
            Lower and upper energy limit for the particle background.
        """
        chlow = ACIS.energy2channel(elow)
        chhigh = ACIS.energy2channel(ehigh)
        counts = self.counts[(chlow-1):chhigh].sum()
        flux = counts / self.EXPOSURE / self.BACKSCAL
        return flux