polarpy.polar_data   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A POLARData.n_channels() 0 4 1
A POLARData.scattering_angle_dead_time_fraction() 0 3 1
A POLARData.__init__() 0 83 4
A POLARData.dead_time_fraction() 0 3 1
A POLARData.scattering_angle_time() 0 4 1
A POLARData.scattering_angles() 0 4 1
A POLARData.n_scattering_bins() 0 4 1
A POLARData.scattering_edges() 0 4 1
A POLARData.time() 0 3 1
A POLARData.rsp() 0 3 1
A POLARData.pha() 0 3 1
1
import numpy as np
2
from threeML.utils.OGIP.response import InstrumentResponse
3
import h5py
4
5
6
class POLARData(object):
7
8
    def __init__(self, polar_hdf5_file, polar_hdf5_response=None, reference_time=0.):
9
        """
10
        container class that converts raw POLAR HDF5 data into useful python
11
        variables
12
13
        This can build both the polarization and spectral data
14
        
15
16
        :param polar_root_file: path to polar event file
17
        :param reference_time: reference time of the events (tunix?)
18
        :param rsp_file: path to rsp file
19
        """
20
21
        with h5py.File(polar_hdf5_file, 'r') as f:
22
23
            # This gets the spectral response
24
            rsp_grp = f['rsp']
25
26
            matrix = rsp_grp['matrix'].value
27
            ebounds = rsp_grp['ebounds'].value
28
            mc_low = rsp_grp['mc_low'].value
29
            mc_high = rsp_grp['mc_high'].value
30
31
            # open the event file
32
33
            # extract the pedestal corrected ADC channels
34
            # which are non-integer and possibly
35
            # less than zero
36
            pha = f['energy'].value
37
38
            # non-zero ADC channels are invalid
39
            idx = pha >= 0
40
            #pha = pha[idx]
41
42
            idx2 = (pha <= ebounds.max()) & (pha >= ebounds.min())
43
44
            pha = pha[idx2 & idx]
45
46
            # get the dead time fraction
47
            self._dead_time_fraction = (f['dead_ratio'].value)[idx & idx2]
48
49
            # get the arrival time, in tunix of the events
50
            self._time = (f['time'].value)[idx & idx2] - reference_time
51
52
            # digitize the ADC channels into bins
53
            # these bins are preliminary
54
55
            # now do the scattering angles
56
57
            scattering_angles = f['scatter_angle'].value
58
59
            # clear the bad scattering angles
60
            idx = scattering_angles != -1
61
62
            self._scattering_angle_time = (f['time'].value)[idx] - reference_time
63
            self._scattering_angle_dead_time_fraction = (f['dead_ratio'].value)[idx]
64
            self._scattering_angles = scattering_angles[idx]
65
66
        # build the POLAR response
67
68
        mc_energies = np.append(mc_low, mc_high[-1])
69
70
        self._rsp = InstrumentResponse(matrix=matrix, ebounds=ebounds, monte_carlo_energies=mc_energies)
71
72
        # bin the ADC channels
73
74
        self._binned_pha = np.digitize(pha, ebounds)
75
76
        # bin the scattering_angles
77
78
        if polar_hdf5_response is not None:
79
80
            with h5py.File(polar_hdf5_response, 'r') as f:
81
82
                scatter_bounds = f['bins'].value
83
84
            self._scattering_bins = scatter_bounds
85
            self._binned_scattering_angles = np.digitize(self._scattering_angles, scatter_bounds)
86
87
        else:
88
89
            self._scattering_bins = None
90
            self._binned_scattering_angles = None
91
92
    @property
93
    def pha(self):
94
        return self._binned_pha
95
96
    @property
97
    def time(self):
98
        return self._time
99
100
    @property
101
    def dead_time_fraction(self):
102
        return self._dead_time_fraction
103
104
    @property
105
    def rsp(self):
106
        return self._rsp
107
108
    @property
109
    def n_channels(self):
110
111
        return len(self._rsp.ebounds) - 1
112
113
    @property
114
    def scattering_angles(self):
115
116
        return self._binned_scattering_angles
117
118
    @property
119
    def scattering_angle_time(self):
120
121
        return self._scattering_angle_time
122
123
    @property
124
    def scattering_angle_dead_time_fraction(self):
125
        return self._scattering_angle_dead_time_fraction
126
127
    @property
128
    def n_scattering_bins(self):
129
130
        return len(self._scattering_bins) - 1
131
132
    @property
133
    def scattering_edges(self):
134
135
        return self._scattering_bins
136