Conditions | 4 |
Total Lines | 83 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import numpy as np |
||
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 | |||
136 |