Passed
Push — master ( 2a29b5...891cf4 )
by Stefan
03:31
created

eppaurora.protons   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 63
Duplicated Lines 38.1 %

Importance

Changes 0
Metric Value
wmc 1
eloc 28
dl 24
loc 63
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A fang2013_protons() 24 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ionization rate parametrizations
10
11
Includes the atmospheric ionization rate parametrization for auroral
12
proton precipitation [1]_.
13
14
.. [1] Fang, X., Lummerzheim, D., and Jackman, C. H. (2013),
15
	Proton impact ionization and a fast calculation method,
16
	J. Geophys. Res. Space Physics, 118, 5369--5378, doi:10.1002/jgra.50484.
17
"""
18
19
import numpy as np
20
21
POLY_F2013 = np.array([
22
	[ 2.55050e+0,  2.69476e-1, -2.58425e-1,  4.43190e-2],
23
	[ 6.39287e-1, -1.85817e-1, -3.15636e-2,  1.01370e-2],
24
	[ 1.63996e+0,  2.43580e-1,  4.29873e-2,  3.77803e-2],
25
	[-2.13479e-1,  1.42464e-1,  1.55840e-2,  1.97407e-3],
26
	[-1.65764e-1,  3.39654e-1, -9.87971e-3,  4.02411e-3],
27
	[-3.59358e-2,  2.50330e-2, -3.29365e-2,  5.08057e-3],
28
	[-6.26528e-1,  1.46865e+0,  2.51853e-1, -4.57132e-2],
29
	[ 1.01384e+0,  5.94301e-2, -3.27839e-2,  3.42688e-3],
30
	[-1.29454e-6, -1.43623e-1,  2.82583e-1,  8.29809e-2],
31
	[-1.18622e-1,  1.79191e-1,  6.49171e-2, -3.99715e-3],
32
	[ 2.94890e+0, -5.75821e-1,  2.48563e-2,  8.31078e-2],
33
	[-1.89515e-1,  3.53452e-2,  7.77964e-2, -4.06034e-3]
34
])
35
36
vpolyval = np.vectorize(np.polyval, signature='(m,n),()->(n)')
37
38
39 View Code Duplication
def fang2013_protons(energy, flux, scale_height, rho, pij=POLY_F2013):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
40
	"""Proton ionization parametrization by Fang et al., 2013 [1]_
41
42
	.. [1] Fang, X., Lummerzheim, D., and Jackman, C. H. (2013),
43
		Proton impact ionization and a fast calculation method,
44
		J. Geophys. Res. Space Physics, 118, 5369--5378, doi:10.1002/jgra.50484.
45
	"""
46
	def _f_y(_cc, _y):
47
		# Fang et al., 2008, Eq. (6), Fang et al., 2010 Eq. (4)
48
		# Fang et al., 2013, Eqs. (6), (7)
49
		_c = _cc.reshape((12, -1))
50
		return (
51
			_c[0] * (_y**_c[1]) * np.exp(-_c[2] * (_y**_c[3])) +
52
			_c[4] * (_y**_c[5]) * np.exp(-_c[6] * (_y**_c[7])) +
53
			_c[8] * (_y**_c[9]) * np.exp(-_c[10] * (_y**_c[11]))
54
		)
55
	# Fang et al., 2013, Eqs. (6), (7)
56
	_cs = np.exp(vpolyval(pij[:, ::-1].T, np.log(energy))).T
57
	# Fang et al., 2013, Eq. (5)
58
	y = 7.5 / energy * (1e4 * rho * scale_height)**(0.9)
59
	f_y = _f_y(_cs, y)
60
	# Fang et al., 2013, Eq. (3)
61
	en_diss = f_y * flux / scale_height
62
	return en_diss
63