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