Conditions | 4 |
Total Lines | 84 |
Code Lines | 31 |
Lines | 84 |
Ratio | 100 % |
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:
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 |
||
48 | View Code Duplication | def berger1974( |
|
|
|||
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 |