-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgael_ld.py
257 lines (238 loc) · 9.57 KB
/
gael_ld.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# ########################################################################### #
# Copyright (c) 2019-2020, California Institute of Technology.
# All rights reserved. Based on Government Sponsored Research under
# contracts NNN12AA01C, NAS7-1407 and/or NAS7-03001.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name of the California Institute of
# Technology (Caltech), its operating division the Jet Propulsion
# Laboratory (JPL), the National Aeronautics and Space
# Administration (NASA), nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALIFORNIA
# INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ########################################################################### #
# EXOplanet Transit Interpretation Code (EXOTIC)
# # NOTE: See companion file version.py for version info.
# ########################################################################### #
import logging
import numpy as np
import lmfit as lm
import matplotlib.pyplot as plt
import ldtk
from ldtk import LDPSetCreator, BoxcarFilter
from ldtk.ldmodel import LinearModel, QuadraticModel, NonlinearModel
log = logging.getLogger(__name__)
class LDPSet(ldtk.LDPSet):
"""
A. NIESSNER: INLINE HACK TO ldtk.LDPSet
"""
@staticmethod
def is_mime(): return True
@property
def profile_mu(self): return self._mu
pass
setattr(ldtk, 'LDPSet', LDPSet)
setattr(ldtk.ldtk, 'LDPSet', LDPSet)
def createldgrid(minmu, maxmu, orbp,
ldmodel='nonlinear', phoenixmin=1e-1,
segmentation=int(10), verbose=False):
"""
G. ROUDIER: Wrapper around LDTK downloading tools
LDTK: Parviainen et al. https://github.com/hpparvi/ldtk
"""
tstar = orbp['T*']
terr = np.sqrt(abs(orbp['T*_uperr']*orbp['T*_lowerr']))
fehstar = orbp['FEH*']
feherr = np.sqrt(abs(orbp['FEH*_uperr']*orbp['FEH*_lowerr']))
loggstar = orbp['LOGG*']
loggerr = np.sqrt(abs(orbp['LOGG*_uperr']*orbp['LOGG*_lowerr']))
log.warning(f">-- Temperature: {tstar} +/- {terr}")
log.warning(f">-- Metallicity: {fehstar} +/- {feherr}")
log.warning(f">-- Surface Gravity: {loggstar} +/- {loggerr}")
niter = int(len(minmu)/segmentation) + 1
allcl = None
allel = None
out = {}
avmu = [np.mean([mm, xm]) for mm, xm in zip(minmu, maxmu)]
for i in np.arange(niter):
loweri = i*segmentation
upperi = (i+1)*segmentation
if i == (niter-1):
upperi = len(avmu)
munm = 1e3*np.array(avmu[loweri:upperi])
munmmin = 1e3*np.array(minmu[loweri:upperi])
munmmax = 1e3*np.array(maxmu[loweri:upperi])
filters = [BoxcarFilter(str(mue), mun, mux)
for mue, mun, mux in zip(munm, munmmin, munmmax)]
sc = LDPSetCreator(teff=(tstar, terr), logg=(loggstar, loggerr),
z=(fehstar, feherr), filters=filters)
ps = sc.create_profiles(nsamples=int(1e4))
itpfail = False
for testprof in ps.profile_averages:
if np.all(~np.isfinite(testprof)):
itpfail = True
pass
nfail = 1e0
while itpfail:
nfail *= 2
sc = LDPSetCreator(teff=(tstar, nfail*terr), logg=(loggstar, loggerr),
z=(fehstar, feherr), filters=filters)
ps = sc.create_profiles(nsamples=int(1e4))
itpfail = False
for testprof in ps.profile_averages:
if np.all(~np.isfinite(testprof)):
itpfail = True
pass
pass
cl, el = ldx(ps.profile_mu, ps.profile_averages, ps.profile_uncertainties,
mumin=phoenixmin, debug=verbose, model=ldmodel)
if allcl is None:
allcl = cl
else:
allcl = np.concatenate((allcl, cl), axis=0)
if allel is None:
allel = el
else:
allel = np.concatenate((allel, el), axis=0)
pass
allel[allel > 1.] = 0.
allel[~np.isfinite(allel)] = 0.
out['MU'] = avmu
out['LD'] = allcl.T
out['ERR'] = allel.T
for i in range(0, len(allcl.T)):
log.warning(f">-- LD{int(i)}: {float(allcl.T[i])} +/- {float(allel.T[i])}")
pass
return out
def ldx(psmu, psmean, psstd, mumin=1e-1, debug=False, model='nonlinear'):
"""
G. ROUDIER: Limb darkening coefficient retrievial on PHOENIX GRID models,
OPTIONAL mumin set up on HAT-P-41 stellar class
"""
mup = np.array(psmu).copy()
prfs = np.array(psmean).copy()
sprfs = np.array(psstd).copy()
nwave = prfs.shape[0]
select = (mup > mumin)
fitmup = mup[select]
fitprfs = prfs[:, select]
fitsprfs = sprfs[:, select]
cl = []
el = []
params = lm.Parameters()
params.add('gamma1', value=1e-1)
params.add('gamma2', value=5e-1)
params.add('gamma3', value=1e-1)
params.add('gamma4', expr='1 - gamma1 - gamma2 - gamma3')
if debug:
plt.figure()
for iwave in np.arange(nwave):
select = fitsprfs[iwave] == 0e0
if True in select:
fitsprfs[iwave][select] = 1e-10
if model == 'linear':
params['gamma1'].value = 0
params['gamma1'].vary = False
params['gamma3'].value = 0
params['gamma3'].vary = False
params['gamma4'].value = 0
params['gamma4'].vary = False
out = lm.minimize(ln_ldx, params,
args=(fitmup, fitprfs[iwave], fitsprfs[iwave]))
cl.append([out.params['gamma1'].value])
el.append([out.params['gamma1'].stderr])
pass
if model == 'quadratic':
params['gamma1'].value = 0
params['gamma1'].vary = False
params['gamma3'].value = 0
params['gamma3'].vary = False
out = lm.minimize(qd_ldx, params,
args=(mup, prfs[iwave], sprfs[iwave]))
cl.append([out.params['gamma2'].value, out.params['gamma4'].value])
el.append([out.params['gamma2'].stderr, out.params['gamma4'].stderr])
pass
if model == 'nonlinear':
out = lm.minimize(nl_ldx, params,
args=(fitmup, fitprfs[iwave], fitsprfs[iwave]))
cl.append([out.params['gamma1'].value, out.params['gamma2'].value,
out.params['gamma3'].value, out.params['gamma4'].value])
el.append([out.params['gamma1'].stderr, out.params['gamma2'].stderr,
out.params['gamma3'].stderr, out.params['gamma4'].stderr])
pass
if debug:
plt.plot(mup, prfs[iwave], 'k^')
plt.errorbar(fitmup, fitprfs[iwave], yerr=fitsprfs[iwave], ls='None')
if model == 'linear':
plt.plot(fitmup, ln_ldx(out.params, fitmup))
if model == 'quadratic':
plt.plot(fitmup, qd_ldx(out.params, fitmup))
if model == 'nonlinear':
plt.plot(fitmup, nl_ldx(out.params, fitmup))
pass
pass
if debug:
plt.ylabel('$I(\\mu)$')
plt.xlabel('$\\mu$')
plt.title(model)
plt.show()
pass
return np.array(cl), np.array(el)
def ln_ldx(params, x, data=None, weights=None):
"""
G. ROUDIER: Linear law
"""
gamma1 = params['gamma1'].value
model = LinearModel.evaluate(x, np.array(gamma1))
if data is None:
return model
if weights is None:
return data - model
return (data - model)/weights
def qd_ldx(params, x, data=None, weights=None):
"""
G. ROUDIER: Quadratic law
"""
gamma1 = params['gamma2'].value
gamma2 = params['gamma4'].value
model = QuadraticModel.evaluate(x, np.array((gamma1, gamma2)))
if data is None:
return model
if weights is None:
return data - model
return (data - model)/weights
def nl_ldx(params, x, data=None, weights=None):
"""
G. ROUDIER: Non Linear law
"""
gamma1 = params['gamma1'].value
gamma2 = params['gamma2'].value
gamma3 = params['gamma3'].value
gamma4 = params['gamma4'].value
model = NonlinearModel.evaluate(x, np.array((gamma1, gamma2, gamma3, gamma4)))
if data is None:
return model
if weights is None:
return data - model
return (data - model)/weights