Passed
Push — master ( 70a901...767401 )
by Stefan
03:27
created

eppaurora.brems.berger1974()   A

Complexity

Conditions 4

Size

Total Lines 84
Code Lines 31

Duplication

Lines 84
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 31
dl 84
loc 84
rs 9.1359
c 0
b 0
f 0
cc 4
nop 9

How to fix   Long Method    Many Parameters   

Long Method

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# coding: utf-8
2
# Copyright (c) 2020 Stefan Bender
3
#
4
# This file is part of pyeppaurora.
5
# pyeppaurora is free software: you can redistribute it or modify
6
# it under the terms of the GNU General Public License as published
7
# by the Free Software Foundation, version 2.
8
# See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html.
9
"""Atmospheric bremsstrahlung ionization parametrization [1]_
10
11
.. [1] Berger, M.J., Seltzer, S.M., Maeda, K.,
12
	Some new results on electron transport in the atmosphere,
13
	Journal of Atmospheric and Terrestrial Physics, v36, i4, pp. 591--617,
14
	April 1974,
15
	doi: 10.1016/0021-9169(74)90085-3
16
"""
17
18
import numpy as np
19
from scipy import interpolate
20
21
__all__ = ["berger1974"]
22
23
E_BR = [2., 5., 10., 20., 50., 100., 200., 500., 1000., 2000.]
24
25
Z_BR = [
26
	2e-6, 4e-6, 8e-6,
27
	2e-5, 4e-5, 8e-5,
28
	2e-4, 4e-4, 8e-4,
29
	2e-3, 4e-3, 8e-3,
30
	2e-2, 4e-2, 8e-2,
31
	2e-1, 4e-1,
32
]
33
34
A_BR = [
35
	[np.nan] * 9 + [8.6e-6, 5.1e-7] + [np.nan] * 6,
36
	[np.nan] * 8 + [7.2e-4, 1.4e-4, 3.3e-5, 5.2e-6, 1.1e-7] + [np.nan] * 4,
37
	[np.nan] * 7 + [2.0e-3, 8.8e-4, 2.7e-4, 9.4e-5, 2.8e-5, 3.0e-6, 3.8e-7, 3.9e-8] + [np.nan] * 2,
38
	[np.nan] * 6 + [3.4e-3, 1.7e-3, 8.0e-4, 3.0e-4, 1.3e-4, 5.7e-5, 1.5e-5, 3.3e-6, 5.7e-7, 2.9e-8] + [np.nan],
39
	[np.nan] * 5 + [7.3e-3, 2.2e-3, 1.1e-3, 5.7e-4, 2.3e-4, 1.1e-4, 5.4e-5, 2.0e-5, 8.3e-6, 2.6e-6, 3.1e-7, 2.8e-8],
40
	[np.nan] * 4 + [1.1e-2, 7.9e-3, 1.7e-3, 8.4e-4, 4.4e-4, 1.9e-4, 1.0e-4, 5.4e-5, 2.3e-5, 1.1e-5, 3.9e-6, 5.4e-7, 2.4e-8],
41
	[np.nan] * 3 + [5.0e-3, 5.3e-3, 5.0e-3, 1.8e-3, 6.5e-4, 3.3e-4, 1.5e-4, 8.8e-5, 5.2e-5, 2.4e-5, 1.1e-5, 3.8e-6, 1.7e-7, 5.9e-9],
42
	[np.nan] * 2 + [2.2e-3, 2.4e-3, 2.5e-3, 2.5e-3, 1.6e-3, 5.2e-4, 2.8e-4, 1.5e-4, 9.9e-5, 6.5e-5, 2.8e-5, 9.5e-6, 1.5e-6, 6.0e-9] + [np.nan],
43
	[np.nan] + [1.0e-3, 1.1e-3, 1.2e-3, 1.3e-3, 1.4e-3, 1.2e-3, 5.5e-4, 3.2e-4, 1.9e-4, 1.3e-4, 8.3e-5, 2.8e-5, 5.5e-6, 2.4e-7] + [np.nan] * 2,
44
	[6.8e-4, 7.6e-4, 8.4e-4, 9.3e-4, 9.9e-4, 1.1e-3, 1.1e-3, 6.8e-4, 4.5e-4, 2.9e-4, 1.9e-4, 9.2e-5, 1.7e-5, 1.2e-6] + [np.nan] * 3,
45
]
46
47
48 View Code Duplication
def berger1974(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49
	energy, flux,
50
	scale_height, rho,
51
	ens=E_BR, zm_p_en=Z_BR, coeffs=A_BR,
52
	fillna=None, log3=True,
53
):
54
	"""Bremsstrahlung ionization by secondary electrons
55
56
	Formulae and parameters as described in [2]_.
57
58
	.. [2] Berger, M.J., Seltzer, S.M., Maeda, K.,
59
		Some new results on electron transport in the atmosphere,
60
		Journal of Atmospheric and Terrestrial Physics, v36, i4, pp. 591--617,
61
		April 1974,
62
		doi: 10.1016/0021-9169(74)90085-3
63
64
	Parameters
65
	----------
66
	energy: array_like (M, ...)
67
		Energy E_0 of the mono-energetic electron beam [keV].
68
	flux: array_like (M,...)
69
		Energy flux Q_0 of the mono-energetic electron beam [keV / cm² / s¹].
70
		Has currently no effect, kept for compatibility with the other methods.
71
	scale_height: array_like (N, ...)
72
		The atmospheric scale heights [cm].
73
	rho: array_like (N, ...)
74
		The atmospheric mass density [g / cm³].
75
	ens: array_like (I,), optional
76
		The energies (one axis) of the coefficient array,
77
		used to interpolate the coefficients to `energy`.
78
		Defaults to the Berger [2]_ coefficients.
79
	zm_p_en: array_like (J,), optional
80
		The atmospheric depth (the other axis) of the coefficient array,
81
		used to interpolate the coefficients to `z` = `scale_height` * `rhos`.
82
		Defaults to the Berger [2]_ coefficients.
83
	coeffs: array_like, (J, I), optional
84
		The bremsstrahlung energy dissipation coefficients.
85
		Defaults to the Berger [2]_ coefficients.
86
	fillna: float or None, optional (default `None`)
87
		Value to use for `nan` values in `coeffs`, `None` skips them.
88
	log3: bool, optional (default `True`)
89
		Interpolate the coefficients as log(ens)-log(zm)-log(coeff)
90
		instead of a linear variant.
91
92
	Returns
93
	-------
94
	a_br: array_like (M, N)
95
		[1. / (g cm⁻²)]
96
		Multiply by density [g cm⁻³] and energy flux [keV cm⁻² s⁻¹]
97
		to obtain energy dissipation flux.
98
	"""
99
	ens = np.asarray(ens)
100
	zm_p_en = np.asarray(zm_p_en)
101
102
	coeffs = np.array(coeffs, copy=fillna is not None)
103
	nans = np.isnan(coeffs)
104
	if fillna is not None:
105
		coeffs[nans] = fillna
106
		# update nan positions (should be all False)
107
		nans = np.isnan(coeffs)
108
109
	z = scale_height * rho / energy
110
	if log3:
111
		# use log of everything
112
		energy = np.log(energy)
113
		z = np.log(z)
114
		ens = np.log(ens)
115
		zm_p_en = np.log(zm_p_en)
116
		coeffs = np.log(coeffs)
117
	pts = [
118
		(_e, _z)
119
		for _i, _e in enumerate(ens)
120
		for _j, _z in enumerate(zm_p_en)
121
		if not nans[_i, _j]
122
	]
123
	intp = interpolate.LinearNDInterpolator(
124
		pts,
125
		coeffs[~nans].ravel(),
126
	)
127
	abr_zm = intp((energy, z))
128
129
	if log3:
130
		abr_zm = np.exp(abr_zm)
131
	return abr_zm
132