Conditions | 4 |
Total Lines | 101 |
Code Lines | 33 |
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:
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 | def berger1974( |
||
49 | energy, flux, |
||
50 | scale_height, rho, |
||
51 | ens=None, zm_p_en=None, coeffs=None, |
||
52 | fillna=None, log3=True, |
||
53 | rbf="multiquadric", |
||
54 | ): |
||
55 | """Bremsstrahlung ionization by secondary electrons |
||
56 | |||
57 | Formulae and parameters as described in [#]_. |
||
58 | |||
59 | By default, the `log(coefficients)` are interpolated wrt. `log(energy)` |
||
60 | and `log(zm)` using :class:`scipy.interpolated.Rbf`. |
||
61 | The default "multiquadric" should work fine, if not consider |
||
62 | using "thin-plate" splines. |
||
63 | |||
64 | Parameters |
||
65 | ---------- |
||
66 | energy: array_like (M, ...) |
||
67 | Energy E_0 of the mono-energetic electron beam [keV]. |
||
68 | A scalar (0-D) value is promoted to 1-D with one element. |
||
69 | flux: array_like (M,...) |
||
70 | Energy flux Q_0 of the mono-energetic electron beam [keV / cm² / s¹]. |
||
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 et al., 1974 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 et al., 1974 coefficients. |
||
83 | coeffs: array_like, (J, I), optional |
||
84 | The bremsstrahlung energy dissipation coefficients. |
||
85 | Defaults to the Berger et al., 1974 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 | rbf: str or callable, optional (default "multiquadric") |
||
92 | Radial basis functions to use for :class:`scipy.interpolate.Rbf`. |
||
93 | |||
94 | Returns |
||
95 | ------- |
||
96 | a_br: array_like (M, N) |
||
97 | A scalar (0-D) `energy` is promoted to 1-D, and the result will |
||
98 | have shape (1, N), *not* (N,). |
||
99 | Energy dissipation rate, units: [keV cm⁻³ s⁻¹] |
||
100 | |||
101 | References |
||
102 | ---------- |
||
103 | .. [#] Berger, M.J., Seltzer, S.M., Maeda, K., |
||
104 | Some new results on electron transport in the atmosphere, |
||
105 | Journal of Atmospheric and Terrestrial Physics, v36, i4, pp. 591--617, |
||
106 | April 1974, |
||
107 | doi: 10.1016/0021-9169(74)90085-3 |
||
108 | |||
109 | See also |
||
110 | -------- |
||
111 | scipy.interpolate.Rbf |
||
112 | """ |
||
113 | energy = np.atleast_1d(np.asarray(energy, dtype=float)) |
||
114 | ens = np.asarray(ens) or np.asarray(E_BR) |
||
115 | zm_p_en = np.asarray(zm_p_en) or np.asarray(Z_BR) |
||
116 | |||
117 | coeffs = ( |
||
118 | np.array(coeffs, copy=fillna is not None) |
||
119 | or np.array(A_BR, copy=fillna is not None) |
||
120 | ) |
||
121 | nans = np.isnan(coeffs) |
||
122 | if fillna is not None: |
||
123 | coeffs[nans] = fillna |
||
124 | # update nan positions (should be all False) |
||
125 | nans = np.isnan(coeffs) |
||
126 | |||
127 | z = scale_height * rho / energy |
||
128 | # reshape by `numpy`'s automatic broacdasting to the same shape as z |
||
129 | enp = np.ones_like(z) * energy |
||
130 | |||
131 | pts = [ |
||
132 | (_e, _z, coeffs[_i, _j]) |
||
133 | for _i, _e in enumerate(ens) |
||
134 | for _j, _z in enumerate(zm_p_en) |
||
135 | if not nans[_i, _j] |
||
136 | ] |
||
137 | pts = np.array(pts, copy=False) |
||
138 | |||
139 | if log3: |
||
140 | enp = np.log(enp) |
||
141 | z = np.log(z) |
||
142 | pts = np.log(pts) |
||
143 | intp = interpolate.Rbf(*(pts.T), function=rbf) |
||
144 | abr_zm = intp(enp, z) |
||
145 | |||
146 | if log3: |
||
147 | abr_zm = np.exp(abr_zm) |
||
148 | return abr_zm * rho * flux |
||
149 |