|
1
|
|
|
import h5py |
|
2
|
|
|
import numpy as np |
|
3
|
|
|
import scipy.interpolate as interpolate |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class PolarResponse(object): |
|
7
|
|
|
|
|
8
|
|
|
def __init__(self, response_file): |
|
9
|
|
|
""" |
|
10
|
|
|
Construct the polar response from the HDF5 response file. |
|
11
|
|
|
This is the POLARIZATION response. |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
:param response_file: |
|
15
|
|
|
:returns: |
|
16
|
|
|
:rtype: |
|
17
|
|
|
|
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
self._rsp_file = response_file |
|
21
|
|
|
|
|
22
|
|
|
# pre interpolate the response for fitting |
|
23
|
|
|
|
|
24
|
|
|
self._interpolate_rsp() |
|
25
|
|
|
|
|
26
|
|
|
def _interpolate_rsp(self): |
|
27
|
|
|
""" |
|
28
|
|
|
Builds the interpolator for the response. This is currently incredibly slow |
|
29
|
|
|
and should be improved |
|
30
|
|
|
|
|
31
|
|
|
""" |
|
32
|
|
|
|
|
33
|
|
|
# now go through the response and extract things |
|
34
|
|
|
|
|
35
|
|
|
with h5py.File(self._rsp_file, 'r') as f: |
|
36
|
|
|
|
|
37
|
|
|
energy = f['energy'].value |
|
38
|
|
|
|
|
39
|
|
|
ene_lo, ene_hi = [], [] |
|
40
|
|
|
|
|
41
|
|
|
# the bin widths are hard coded right now. |
|
42
|
|
|
# this should be IMPROVED! |
|
43
|
|
|
|
|
44
|
|
|
for ene in energy: |
|
45
|
|
|
|
|
46
|
|
|
ene_lo.append(ene - 2.5) |
|
47
|
|
|
ene_hi.append(ene + 2.5) |
|
48
|
|
|
|
|
49
|
|
|
pol_ang = np.array(f['pol_ang'].value) |
|
50
|
|
|
|
|
51
|
|
|
pol_deg = np.array(f['pol_deg'].value) |
|
52
|
|
|
|
|
53
|
|
|
bins = np.array(f['bins'].value) |
|
54
|
|
|
|
|
55
|
|
|
# get the bin centers as these are where things |
|
56
|
|
|
# should be evaluated |
|
57
|
|
|
|
|
58
|
|
|
bin_center = 0.5 * (bins[:-1] + bins[1:]) |
|
59
|
|
|
|
|
60
|
|
|
all_interp = [] |
|
61
|
|
|
|
|
62
|
|
|
# now we construct a series of interpolation |
|
63
|
|
|
# functions that are called during the fit. |
|
64
|
|
|
# we use some nice matrix math to handle this |
|
65
|
|
|
|
|
66
|
|
|
for i, bm in enumerate(bin_center): |
|
67
|
|
|
|
|
68
|
|
|
this_interpolator = interpolate.RegularGridInterpolator( |
|
69
|
|
|
(energy, pol_ang, pol_deg), f['matrix'][..., i]) |
|
70
|
|
|
|
|
71
|
|
|
all_interp.append(this_interpolator) |
|
72
|
|
|
|
|
73
|
|
|
# finally we attach all of this to the class |
|
74
|
|
|
|
|
75
|
|
|
self._all_interp = all_interp |
|
76
|
|
|
|
|
77
|
|
|
self._ene_lo = ene_lo |
|
78
|
|
|
self._ene_hi = ene_hi |
|
79
|
|
|
self._energy_mid = energy |
|
80
|
|
|
|
|
81
|
|
|
self._n_scatter_bins = len(bin_center) |
|
82
|
|
|
self._scattering_bins = bin_center |
|
83
|
|
|
self._scattering_bins_lo = bins[:-1] |
|
84
|
|
|
self._scattering_bins_hi = bins[1:] |
|
85
|
|
|
|
|
86
|
|
|
@property |
|
87
|
|
|
def ene_lo(self): |
|
88
|
|
|
""" |
|
89
|
|
|
The low side of the energy bins |
|
90
|
|
|
|
|
91
|
|
|
:returns: |
|
92
|
|
|
:rtype: |
|
93
|
|
|
|
|
94
|
|
|
""" |
|
95
|
|
|
return self._ene_lo |
|
96
|
|
|
|
|
97
|
|
|
@property |
|
98
|
|
|
def ene_hi(self): |
|
99
|
|
|
""" |
|
100
|
|
|
The high side of the energy bins |
|
101
|
|
|
|
|
102
|
|
|
:returns: |
|
103
|
|
|
:rtype: |
|
104
|
|
|
|
|
105
|
|
|
""" |
|
106
|
|
|
return self._ene_hi |
|
107
|
|
|
|
|
108
|
|
|
@property |
|
109
|
|
|
def energy_mid(self): |
|
110
|
|
|
""" |
|
111
|
|
|
The mid point of the energy bins |
|
112
|
|
|
|
|
113
|
|
|
:returns: |
|
114
|
|
|
:rtype: |
|
115
|
|
|
|
|
116
|
|
|
""" |
|
117
|
|
|
return self._energy_mid |
|
118
|
|
|
|
|
119
|
|
|
@property |
|
120
|
|
|
def n_scattering_bins(self): |
|
121
|
|
|
""" |
|
122
|
|
|
The number of scattering angle bins |
|
123
|
|
|
|
|
124
|
|
|
:returns: |
|
125
|
|
|
:rtype: |
|
126
|
|
|
|
|
127
|
|
|
""" |
|
128
|
|
|
|
|
129
|
|
|
return self._n_scatter_bins |
|
130
|
|
|
|
|
131
|
|
|
@property |
|
132
|
|
|
def scattering_bins(self): |
|
133
|
|
|
""" |
|
134
|
|
|
The scattering angle bin CENTERS. |
|
135
|
|
|
|
|
136
|
|
|
:returns: |
|
137
|
|
|
:rtype: |
|
138
|
|
|
|
|
139
|
|
|
""" |
|
140
|
|
|
|
|
141
|
|
|
return self._scattering_bins |
|
142
|
|
|
|
|
143
|
|
|
@property |
|
144
|
|
|
def scattering_bins_lo(self): |
|
145
|
|
|
""" |
|
146
|
|
|
The low side of the scattering angle bins |
|
147
|
|
|
|
|
148
|
|
|
:returns: |
|
149
|
|
|
:rtype: |
|
150
|
|
|
|
|
151
|
|
|
""" |
|
152
|
|
|
|
|
153
|
|
|
return self._scattering_bins_lo |
|
154
|
|
|
|
|
155
|
|
|
@property |
|
156
|
|
|
def scattering_bins_hi(self): |
|
157
|
|
|
""" |
|
158
|
|
|
The high side of the scattering angle bins |
|
159
|
|
|
|
|
160
|
|
|
:returns: |
|
161
|
|
|
:rtype: |
|
162
|
|
|
|
|
163
|
|
|
""" |
|
164
|
|
|
|
|
165
|
|
|
return self._scattering_bins_hi |
|
166
|
|
|
|
|
167
|
|
|
@property |
|
168
|
|
|
def interpolators(self): |
|
169
|
|
|
""" |
|
170
|
|
|
The series of interpolation functions |
|
171
|
|
|
|
|
172
|
|
|
:Returns: |
|
173
|
|
|
:rtype: |
|
174
|
|
|
|
|
175
|
|
|
""" |
|
176
|
|
|
|
|
177
|
|
|
return self._all_interp |
|
178
|
|
|
|