-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaflare.py
97 lines (79 loc) · 3.65 KB
/
aflare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'''
Hold the analytic flare model
Could also put other simple flare models in here, e.g. polynomial from Balona (2015)
'''
import numpy as np
def aflare(t, p):
"""
This is the Analytic Flare Model from the flare-morphology paper.
Reference Davenport et al. (2014) http://arxiv.org/abs/1411.3723
Note: this model assumes the flux before the flare is zero centered
Note: many sub-flares can be modeled by this method by changing the
number of parameters in "p". As a result, this routine may not work
for fitting with methods like scipy.optimize.curve_fit, which require
a fixed number of free parameters. Instead, for fitting a single peak
use the aflare1 method.
Parameters
----------
t : 1-d array
The time array to evaluate the flare over
p : 1-d array
p == [tpeak, fwhm (units of time), amplitude (units of flux)] x N
Returns
-------
flare : 1-d array
The flux of the flare model evaluated at each time
"""
_fr = [1.00000, 1.94053, -0.175084, -2.24588, -1.12498]
_fd = [0.689008, -1.60053, 0.302963, -0.278318]
Nflare = int( np.floor( (len(p)/3.0) ) )
flare = np.zeros_like(t)
# compute the flare model for each flare
for i in range(Nflare):
outm = np.piecewise(t, [(t<= p[0+i*3]) * (t-p[0+i*3])/p[1+i*3] > -1.,
(t > p[0+i*3])],
[lambda x: (_fr[0]+ # 0th order
_fr[1]*((x-p[0+i*3])/p[1+i*3])+ # 1st order
_fr[2]*((x-p[0+i*3])/p[1+i*3])**2.+ # 2nd order
_fr[3]*((x-p[0+i*3])/p[1+i*3])**3.+ # 3rd order
_fr[4]*((x-p[0+i*3])/p[1+i*3])**4. ),# 4th order
lambda x: (_fd[0]*np.exp( ((x-p[0+i*3])/p[1+i*3])*_fd[1] ) +
_fd[2]*np.exp( ((x-p[0+i*3])/p[1+i*3])*_fd[3] ))]
) * p[2+i*3] # amplitude
flare = flare + outm
return flare
def aflare1(t, tpeak, fwhm, ampl):
'''
The Analytic Flare Model evaluated for a single-peak (classical).
Reference Davenport et al. (2014) http://arxiv.org/abs/1411.3723
Use this function for fitting classical flares with most curve_fit
tools.
Note: this model assumes the flux before the flare is zero centered
Parameters
----------
t : 1-d array
The time array to evaluate the flare over
tpeak : float
The time of the flare peak
fwhm : float
The "Full Width at Half Maximum", timescale of the flare
ampl : float
The amplitude of the flare
Returns
-------
flare : 1-d array
The flux of the flare model evaluated at each time
'''
_fr = [1.00000, 1.94053, -0.175084, -2.24588, -1.12498]
_fd = [0.689008, -1.60053, 0.302963, -0.278318]
flare = np.piecewise(t, [(t<= tpeak) * (t-tpeak)/fwhm > -1.,
(t > tpeak)],
[lambda x: (_fr[0]+ # 0th order
_fr[1]*((x-tpeak)/fwhm)+ # 1st order
_fr[2]*((x-tpeak)/fwhm)**2.+ # 2nd order
_fr[3]*((x-tpeak)/fwhm)**3.+ # 3rd order
_fr[4]*((x-tpeak)/fwhm)**4. ),# 4th order
lambda x: (_fd[0]*np.exp( ((x-tpeak)/fwhm)*_fd[1] ) +
_fd[2]*np.exp( ((x-tpeak)/fwhm)*_fd[3] ))]
) * np.abs(ampl) # amplitude
return flare