Conditions | 1 |
Total Lines | 182 |
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 | # -*- coding: utf-8 -*- |
||
182 | def read_omnie(omnie_file): |
||
183 | """Read and parse OMNI2 extended files [#]_ |
||
184 | |||
185 | Parses the Omni2 extended data files, available at [#]_, |
||
186 | into a :class:`pandas.DataFrame`. |
||
187 | |||
188 | .. [#] https://omniweb.gsfc.nasa.gov/ow.html |
||
189 | .. [#] https://spdf.gsfc.nasa.gov/pub/data/omni/low_res_omni/extended/ |
||
190 | |||
191 | Parameters |
||
192 | ---------- |
||
193 | omnie_file: str |
||
194 | File to parse, absolute path or relative to the current dir. |
||
195 | |||
196 | Returns |
||
197 | ------- |
||
198 | sw_df: pandas.DataFrame |
||
199 | The parsed OMNI2 space weather data (hourly values). |
||
200 | Details in |
||
201 | https://spdf.gsfc.nasa.gov/pub/data/omni/low_res_omni/extended/aareadme_extended |
||
202 | |||
203 | Raises an ``IOError`` if the file is not found. |
||
204 | |||
205 | The dataframe contains the following columns: |
||
206 | |||
207 | year: |
||
208 | The observation year |
||
209 | doy: |
||
210 | Day of the year |
||
211 | hour: |
||
212 | Hour of the day |
||
213 | bsrn: |
||
214 | Bartels Solar Rotation Number. |
||
215 | id_imf: |
||
216 | ID for IMF spacecraft |
||
217 | id_sw: |
||
218 | ID for SW plasma spacecraft |
||
219 | n_imf: |
||
220 | Number of points in IMF averages |
||
221 | n_plasma: |
||
222 | Numberof points in plasma averages |
||
223 | B_mag_avg: |
||
224 | Magnetic field magnitude average B |
||
225 | B_mag: |
||
226 | Magnetic field vector magnitude |
||
227 | theta_B: |
||
228 | Latitude angle of the magnetic field vector |
||
229 | phi_B: |
||
230 | Longitude angle of the magnetic field vector |
||
231 | B_x: |
||
232 | B_x GSE, GSM |
||
233 | B_y_GSE: |
||
234 | B_y GSE |
||
235 | B_z_GSE: |
||
236 | B_z GSE |
||
237 | B_y_GSM: |
||
238 | B_y GSM |
||
239 | B_z_GSM: |
||
240 | B_z GSM |
||
241 | sigma_B_mag_avg: |
||
242 | RMS standard deviation of B_mag_avg |
||
243 | sigma_B_mag: |
||
244 | RMS standard deviation of B_mag |
||
245 | sigma_B_x_GSE: |
||
246 | RMS standard deviation of B_x_GSE |
||
247 | sigma_B_y_GSE: |
||
248 | RMS standard deviation of B_y_GSE |
||
249 | sigma_B_z_GSE: |
||
250 | RMS standard deviation of B_z_GSE |
||
251 | T_p: |
||
252 | Proton temperature |
||
253 | n_p: |
||
254 | Proton density |
||
255 | v_plasma: |
||
256 | Plasma flow speed |
||
257 | phi_v: |
||
258 | Plasma flow longitude angle |
||
259 | theta_v: |
||
260 | Plasma flow latitude angle |
||
261 | n_alpha_n_p: |
||
262 | Alpha/Proton ratio |
||
263 | p_flow: |
||
264 | Flow pressure |
||
265 | sigma_T: |
||
266 | Standard deviation of T_p |
||
267 | sigma_n: |
||
268 | Standard deviation of n_p |
||
269 | sigma_v: |
||
270 | Standard deviation of v_plasma |
||
271 | sigma_phi_v: |
||
272 | Standard deviation of phi_v |
||
273 | sigma_theta_v: |
||
274 | Standard deviation of theta_v |
||
275 | sigma_na_np: |
||
276 | Standard deviation of n_alpha_n_p |
||
277 | E: |
||
278 | Electric field magnitude |
||
279 | beta_plasma: |
||
280 | Plasma beta |
||
281 | mach: |
||
282 | Alfvén Mach number |
||
283 | Kp: |
||
284 | Kp index value |
||
285 | R: |
||
286 | Sunspot number |
||
287 | Dst: |
||
288 | Dst index value |
||
289 | AE: |
||
290 | AE index value |
||
291 | p_01MeV, p_02MeV, p_04MeV, p_10MeV, p_30MeV, p_60MeV: |
||
292 | Proton fluxes >1 MeV, >2 MeV, >4 MeV, >10 MeV, >30 MeV, > 60 MeV |
||
293 | flag: |
||
294 | Flag (-1, ..., 6) |
||
295 | Ap: |
||
296 | Ap index value |
||
297 | f107_adj: |
||
298 | F10.7 radio flux at 1 AU |
||
299 | PC: |
||
300 | PC index value |
||
301 | AL, AU: |
||
302 | AL and AU index values |
||
303 | mach_mag: |
||
304 | Magnetosonic Mach number |
||
305 | |||
306 | The extended dataset contains the addional columns: |
||
307 | |||
308 | Lya: |
||
309 | Solar Lyman-alpha irradiance |
||
310 | QI_p: |
||
311 | Proton QI |
||
312 | """ |
||
313 | _assert_file_exists(omnie_file) |
||
314 | # FORMAT( |
||
315 | # 2I4,I3,I5,2I3,2I4,14F6.1,F9.0,F6.1,F6.0,2F6.1,F6.3,F6.2, |
||
316 | # F9.0,F6.1,F6.0,2F6.1,F6.3,2F7.2,F6.1,I3,I4,I6,I5,F10.2, |
||
317 | # 5F9.2,I3,I4,2F6.1,2I6,F5.1,F9.6,F7.4 |
||
318 | # ) |
||
319 | sw = np.genfromtxt( |
||
320 | omnie_file, |
||
321 | skip_header=0, |
||
322 | delimiter=[ |
||
323 | # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
||
324 | # yy dd hr br i1 i2 n1 n2 B B' tB fB Bx By Bz By Bz sB sB sB |
||
325 | 4, 4, 3, 5, 3, 3, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
||
326 | # 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
||
327 | # sB sB Tp np v fv tv nr p sT sn sv sf st sr E bp M Kp R |
||
328 | 6, 6, 9, 6, 6, 6, 6, 6, 6, 9, 6, 6, 6, 6, 6, 7, 7, 6, 3, 4, |
||
329 | # 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
||
330 | # Ds AE p1 p2 p4p10p30p60 fl Apf10 PC AL AU Mm La QI |
||
331 | 6, 5,10, 9, 9, 9, 9, 9, 3, 4, 6, 6, 6, 6, 5, 9, 7, |
||
332 | ], |
||
333 | dtype=( |
||
334 | "i4,i4,i4,i4,i4,i4,i4,i4,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8," |
||
335 | "f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,i4,i4," |
||
336 | "i4,i4,f8,f8,f8,f8,f8,f8,i4,i4,f8,f8,i4,i4,f8,f8,f8" |
||
337 | ), |
||
338 | names=[ |
||
339 | "year", "doy", "hour", "bsrn", "id_imf", "id_sw", "n_imf", "n_plasma", |
||
340 | "B_mag_avg", "B_mag", "theta_B", "phi_B", |
||
341 | "B_x", "B_y_GSE", "B_z_GSE", "B_y_GSM", "B_z_GSM", |
||
342 | "sigma_B_mag_avg", "sigma_B_mag", |
||
343 | "sigma_B_x_GSE", "sigma_B_y_GSE", "sigma_B_z_GSE", |
||
344 | "T_p", "n_p", "v_plasma", "phi_v", "theta_v", "n_alpha_n_p", "p_flow", |
||
345 | "sigma_T", "sigma_n", "sigma_v", |
||
346 | "sigma_phi_v", "sigma_theta_v", "sigma_na_np", |
||
347 | "E", "beta_plasma", "mach", "Kp", "R", "Dst", "AE", |
||
348 | "p_01MeV", "p_02MeV", "p_04MeV", "p_10MeV", "p_30MeV", "p_60MeV", |
||
349 | "flag", "Ap", "f107_adj", "PC", "AL", "AU", "mach_mag", "Lya", "QI_p", |
||
350 | ] |
||
351 | ) |
||
352 | sw = sw[sw["year"] != -1] |
||
353 | ts = pd.to_datetime( |
||
354 | [ |
||
355 | "{0:04d}.{1:03d} {2:02d}".format(yy, dd, hh) |
||
356 | for yy, dd, hh in sw[["year", "doy", "hour"]] |
||
357 | ], |
||
358 | format="%Y.%j %H", |
||
359 | ) |
||
360 | sw_df = pd.DataFrame(sw, index=ts) |
||
361 | # Adjust Kp to 0...9 |
||
362 | sw_df["Kp"] = 0.1 * sw_df["Kp"] |
||
363 | return sw_df |
||
364 | |||
432 |