Conditions | 7 |
Total Lines | 81 |
Code Lines | 32 |
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 h5py |
||
113 | def polar_spectra_to_hdf5(polar_root_file, polar_rsp_root_file, hdf5_out_file): |
||
114 | """ |
||
115 | This function extracts the POLAR spectral information for spectral fitting. |
||
116 | These files can be further reduced the PHA FITS files with 3ML |
||
117 | |||
118 | :param polar_root_file: The spectral ROOT file |
||
119 | :param polar_rsp_root_file: The response ROOT file |
||
120 | :param hdf5_out_file: the name of the output HDF5 file |
||
121 | """ |
||
122 | |||
123 | # extract the info from the crappy root file |
||
124 | |||
125 | with h5py.File(hdf5_out_file, 'w') as outfile: |
||
126 | |||
127 | # first we do the RSP |
||
128 | |||
129 | rsp_grp = outfile.create_group('rsp') |
||
130 | |||
131 | with open_ROOT_file(polar_rsp_root_file) as f: |
||
132 | |||
133 | matrix = th2_to_arrays(f.Get('rsp'))[-1] |
||
134 | |||
135 | rsp_grp.create_dataset('matrix', data=matrix, compression='lzf') |
||
136 | |||
137 | ebounds = th2_to_arrays(f.Get('EM_bounds'))[-1] |
||
138 | |||
139 | rsp_grp.create_dataset('ebounds', data=ebounds, compression='lzf') |
||
140 | |||
141 | mc_low = th2_to_arrays(f.Get('ER_low'))[-1] |
||
142 | |||
143 | rsp_grp.create_dataset('mc_low', data=mc_low, compression='lzf') |
||
144 | |||
145 | mc_high = th2_to_arrays(f.Get('ER_high'))[-1] |
||
146 | |||
147 | rsp_grp.create_dataset('mc_high', data=mc_high, compression='lzf') |
||
148 | |||
149 | # now we get the spectral informations |
||
150 | keys_to_use = ['polar_out'] |
||
151 | |||
152 | f = ROOT.TFile(polar_root_file) |
||
153 | |||
154 | extra_grp = outfile.create_group('extras') |
||
155 | |||
156 | for key in f.GetListOfKeys(): |
||
157 | |||
158 | name = key.GetName() |
||
159 | |||
160 | if name not in keys_to_use: |
||
161 | try: |
||
162 | |||
163 | # first we see if it is a TTree and then |
||
164 | # add a new group and attach its data |
||
165 | |||
166 | tree = tree_to_ndarray(f.Get(name)) |
||
167 | |||
168 | new_grp = extra_grp.create_group(name) |
||
169 | |||
170 | for new_name in tree.dtype.names: |
||
171 | new_grp.create_dataset(new_name, data=tree[new_name], compression='lzf') |
||
172 | |||
173 | except: |
||
174 | |||
175 | # in this case we just want the actual data |
||
176 | |||
177 | data = th2_to_arrays(f.Get(name))[-1] |
||
178 | |||
179 | extra_grp.create_dataset(name, data=data, compression='lzf') |
||
180 | |||
181 | # now we will deal with the data that is important |
||
182 | |||
183 | tmp = tree_to_ndarray(f.Get('polar_out')) |
||
184 | |||
185 | outfile.create_dataset('energy', data=tmp['Energy'], compression='lzf') |
||
186 | |||
187 | outfile.create_dataset('scatter_angle', data=tmp['scatter_angle'], compression='lzf') |
||
188 | |||
189 | outfile.create_dataset('dead_ratio', data=tmp['dead_ratio'], compression='lzf') |
||
190 | |||
191 | outfile.create_dataset('time', data=tmp['tunix'], compression='lzf') |
||
192 | |||
193 | f.Close() |
||
194 |