-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_transits.py
2706 lines (2062 loc) · 88.5 KB
/
model_transits.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
#
# We NOTE that this function is written by Ian Crossfield and is not as such part of this K2 pipeline. It is only included here for ease of fitting simple transit models when wanted
#
-------------------------------------------------------
The Mandel & Agol (2002) transit light curve equations.
-------------------------------------------------------
:FUNCTIONS:
:func:`occultuniform` -- uniform-disk transit light curve
:func:`occultquad` -- quadratic limb-darkening
:func:`occultnonlin` -- full (4-parameter) nonlinear limb-darkening
:func:`occultnonlin_small` -- small-planet approximation with full
nonlinear limb-darkening.
:func:`t2z` -- convert dates to transiting z-parameter for circular
orbits.
:REQUIREMENTS:
`numpy <http://www.numpy.org/>`_
`scipy.special <http://www.scipy.org/>`_
:NOTES:
Certain values of p (<0.09, >0.5) cause some routines to hang;
your mileage may vary. If you find out why, please let me know!
Cursory testing suggests that the Python routines contained within
are slower than the corresponding IDL code by a factor of 5-10.
For :func:`occultquad` I relied heavily on the IDL code of E. Agol
and J. Eastman.
Function :func:`appellf1` comes from the mpmath compilation, and
is adopted (with modification) for use herein in compliance with
its BSD license (see function documentation for more details).
:REFERENCE:
The main reference is that seminal work by `Mandel and Agol (2002)
<http://adsabs.harvard.edu/abs/2002ApJ...580L.171M>`_.
:LICENSE:
Created by `Ian Crossfield <http://www.astro.ucla.edu/~ianc/>`_ at
UCLA. The code contained herein may be reused, adapted, or
modified so long as proper attribution is made to the original
authors.
:REVISIONS:
2011-04-22 11:08 IJMC: Finished, renamed occultation functions.
Cleaned up documentation. Published to
website.
2011-04-25 17:32 IJMC: Fixed bug in :func:`ellpic_bulirsch`.
2012-03-09 08:38 IJMC: Several major bugs fixed, courtesy of
S. Aigrain at Oxford U.
2012-03-20 14:12 IJMC: Fixed modeleclipse_simple based on new
format of :func:`occultuniform. `
"""
import numpy as np
from scipy import special, misc
eps = np.finfo(float).eps
zeroval = eps*1e6
try:
import _integral_smallplanet_nonlinear
c_integral_smallplanet_nonlinear = True
except:
c_integral_smallplanet_nonlinear = False
def appelf1_ac(a, b1, b2, c, z1, z2, **kwargs):
"""Analytic continuations of the Appell hypergeometric function of 2 variables.
:REFERENCE:
Olsson 1964, Colavecchia et al. 2001
"""
# 2012-03-09 12:05 IJMC: Created
def appellf1(a,b1,b2,c,z1,z2,**kwargs):
"""Give the Appell hypergeometric function of two variables.
:INPUTS:
six parameters, all scalars.
:OPTIONS:
eps -- scalar, machine tolerance precision. Defaults to 1e-10.
:NOTES:
Adapted from the `mpmath <http://code.google.com/p/mpmath/>`_
module, but using the scipy (instead of mpmath) Gauss
hypergeometric function speeds things up.
:LICENSE:
MPMATH Copyright (c) 2005-2010 Fredrik Johansson and mpmath
contributors. All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
a. Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
b. 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.
c. Neither the name of mpmath 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 REGENTS OR CONTRIBUTORS 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.
"""
#2011-04-22 10:15 IJMC: Adapted from mpmath, but using scipy Gauss
# hypergeo. function
# 2013-03-11 13:34 IJMC: Added a small error-trap for 'nan' hypgf values
if kwargs.has_key('eps'):
eps = kwargs['eps']
else:
eps = 1e-9
# Assume z1 smaller
# We will use z1 for the outer loop
if abs(z1) > abs(z2):
z1, z2 = z2, z1
b1, b2 = b2, b1
def ok(x):
return abs(x) < 0.99
# IJMC: Ignore the finite cases for now....
## Finite cases
#if ctx.isnpint(a):
# pass
#elif ctx.isnpint(b1):
# pass
#elif ctx.isnpint(b2):
# z1, z2, b1, b2 = z2, z1, b2, b1
#else:
# #print z1, z2
# # Note: ok if |z2| > 1, because
# # 2F1 implements analytic continuation
if not ok(z1):
u1 = (z1-z2)/(z1-1)
if not ok(u1):
raise ValueError("Analytic continuation not implemented")
#print "Using analytic continuation"
return (1-z1)**(-b1)*(1-z2)**(c-a-b2)*\
appellf1(c-a,b1,c-b1-b2,c,u1,z2,**kwargs)
#print "inner is", a, b2, c
##one = ctx.one
s = 0
t = 1
k = 0
while 1:
#h = ctx.hyp2f1(a,b2,c,z2,zeroprec=ctx.prec,**kwargs)
#print a.__class__, b2.__class__, c.__class__, z2.__class__
h = special.hyp2f1(float(a), float(b2), float(c), float(z2))
if not np.isfinite(h):
break
term = t * h
if abs(term) < eps and abs(h) > 10*eps:
break
s += term
k += 1
t = (t*a*b1*z1) / (c*k)
c += 1 # one
a += 1 # one
b1 += 1 # one
#print k, h, term, s
#if (k/200.)==int(k/200.) or k==171: pdb.set_trace()
return s
def ellke2(k, tol=100*eps, maxiter=100):
"""Compute complete elliptic integrals of the first kind (K) and
second kind (E) using the series expansions."""
# 2011-04-24 21:14 IJMC: Created
k = np.array(k, copy=False)
ksum = 0*k
kprevsum = ksum.copy()
kresidual = ksum + 1
#esum = 0*k
#eprevsum = esum.copy()
#eresidual = esum + 1
n = 0
sqrtpi = np.sqrt(np.pi)
#kpow = k**0
#ksq = k*k
while (np.abs(kresidual) > tol).any() and n <= maxiter:
#kpow *= (ksq)
#print kpow==(k**(2*n))
ksum += ((misc.factorial2(2*n - 1)/misc.factorial2(2*n))**2) * k**(2*n)
#ksum += (special.gamma(n + 0.5)/special.gamma(n + 1) / sqrtpi) * k**(2*n)
kresidual = ksum - kprevsum
kprevsum = ksum.copy()
n += 1
#print n, kresidual
return ksum * (np.pi/2.)
def ellke(k):
"""Compute Hasting's polynomial approximation for the complete
elliptic integral of the first (ek) and second (kk) kind.
:INPUTS:
k -- scalar or Numpy array
:OUTPUTS:
ek, kk
:NOTES:
Adapted from the IDL function of the same name by J. Eastman (OSU).
"""
# 2011-04-19 09:15 IJC: Adapted from J. Eastman's IDL code.
m1 = 1. - k**2
logm1 = np.log(m1)
# First kind:
a1 = 0.44325141463
a2 = 0.06260601220
a3 = 0.04757383546
a4 = 0.01736506451
b1 = 0.24998368310
b2 = 0.09200180037
b3 = 0.04069697526
b4 = 0.00526449639
ee1 = 1. + m1*(a1 + m1*(a2 + m1*(a3 + m1*a4)))
ee2 = m1 * (b1 + m1*(b2 + m1*(b3 + m1*b4))) * (-logm1)
# Second kind:
a0 = 1.38629436112
a1 = 0.09666344259
a2 = 0.03590092383
a3 = 0.03742563713
a4 = 0.01451196212
b0 = 0.5
b1 = 0.12498593597
b2 = 0.06880248576
b3 = 0.03328355346
b4 = 0.00441787012
ek1 = a0 + m1*(a1 + m1*(a2 + m1*(a3 + m1*a4)))
ek2 = (b0 + m1*(b1 + m1*(b2 + m1*(b3 + m1*b4)))) * logm1
return ee1 + ee2, ek1 - ek2
def ellpic_bulirsch(n, k, tol=1000*eps, maxiter=1e4):
"""Compute the complete elliptical integral of the third kind
using the algorithm of Bulirsch (1965).
:INPUTS:
n -- scalar or Numpy array
k-- scalar or Numpy array
:NOTES:
Adapted from the IDL function of the same name by J. Eastman (OSU).
"""
# 2011-04-19 09:15 IJMC: Adapted from J. Eastman's IDL code.
# 2011-04-25 11:40 IJMC: Set a more stringent tolerance (from 1e-8
# to 1e-14), and fixed tolerance flag to the
# maximum of all residuals.
# 2013-04-13 21:31 IJMC: Changed 'max' call to 'any'; minor speed boost.
# Make p, k into vectors:
#if not hasattr(n, '__iter__'):
# n = array([n])
#if not hasattr(k, '__iter__'):
# k = array([k])
if not hasattr(n,'__iter__'):
n = np.array([n])
if not hasattr(k,'__iter__'):
k = np.array([k])
if len(n)==0 or len(k)==0:
return np.array([])
kc = np.sqrt(1. - k**2)
p = n + 1.
if min(p) < 0:
print "Negative p"
# Initialize:
m0 = np.array(1.)
c = np.array(1.)
p = np.sqrt(p)
d = 1./p
e = kc.copy()
outsideTolerance = True
iter = 0
while outsideTolerance and iter<maxiter:
f = c.copy()
c = d/p + c
g = e/p
d = 2. * (f*g + d)
p = g + p;
g = m0.copy()
m0 = kc + m0
if ((np.abs(1. - kc/g)) > tol).any():
kc = 2. * np.sqrt(e)
e = kc * m0
iter += 1
else:
outsideTolerance = False
#if (iter/10.) == (iter/10):
# print iter, (np.abs(1. - kc/g))
#pdb.set_trace()
## For debugging:
#print min(np.abs(1. - kc/g)) > tol
#print 'tolerance>>', tol
#print 'minimum>> ', min(np.abs(1. - kc/g))
#print 'maximum>> ', max(np.abs(1. - kc/g)) #, (np.abs(1. - kc/g))
return .5 * np.pi * (c*m0 + d) / (m0 * (m0 + p))
def z2dt_circular(per, inc, ars, z):
""" Convert transit crossing parameter z to a time offset for circular orbits.
:INPUTS:
per -- scalar. planetary orbital period
inc -- scalar. orbital inclination (in degrees)
ars -- scalar. ratio a/Rs, orbital semimajor axis over stellar radius
z -- scalar or array; transit crossing parameter z.
:RETURNS:
|dt| -- magnitude of the time offset from transit center at
which specified z occurs.
"""
# 2011-06-14 11:26 IJMC: Created.
numer = (z / ars)**2 - 1.
denom = np.cos(inc*np.pi/180.)**2 - 1.
dt = (per / (2*np.pi)) * np.arccos(np.sqrt(numer / denom))
return dt
def t2z(tt, per, inc, hjd, ars, ecc=0, longperi=0, transitonly=False, occultationonly=False):
"""Convert HJD (time) to transit crossing parameter z.
:INPUTS:
tt -- scalar. transit ephemeris
per -- scalar. planetary orbital period
inc -- scalar. orbital inclination (in degrees)
hjd -- scalar or array of times, typically heliocentric or
barycentric julian date.
ars -- scalar. ratio a/Rs, orbital semimajor axis over stellar radius
ecc -- scalar. orbital eccentricity.
longperi=0 scalar. longitude of periapse (in radians)
transitonly : bool
If False, both transits and occultations have z=0. But this
means that other routines (e.g., :func:`occultuniform`)
model two eclipse events per orbit. As a kludge, set
transitonly=True -- it sets all values of 'z' near
occultations to be very large, so that they are not modeled
as eclipses.
occultationonly : bool
Same as transitonly, but for occultations (secondary eclipses)
:ALGORITHM:
At zero eccentricity, z relates to physical quantities by:
z = (a/Rs) * sqrt(sin[w*(t-t0)]**2+[cos(i)*cos(w*[t-t0])]**2)
"""
# 2010-01-11 18:18 IJC: Created
# 2011-04-19 15:20 IJMC: Updated documentation.
# 2011-04-22 11:27 IJMC: Updated to avoid reliance on planet objects.
# 2011-05-22 16:51 IJMC: Temporarily removed eccentricity
# dependence... I'll deal with that later.
# 2013-10-12 22:58 IJMC: Added transitonly, occultationonly options
#if not p.transit:
# print "Must use a transiting exoplanet!"
# return False
#import analysis as an ## NEED TO IMPORT THIS FOR NON ZERO ECC
if ecc <> 0:
ecc = 0
print "WARNING: setting ecc=0 for now until I get this function working"
if ecc==0:
omega_orb = 2*np.pi/per
omega_tdiff = omega_orb * (hjd - tt)
cosom = np.cos(omega_tdiff)
z = ars * np.sqrt(np.sin(omega_tdiff)**2 + \
(np.cos(inc*np.pi/180.)*cosom)**2)
if transitonly:
z[cosom<0] = 100.
if occultationonly:
z[cosom>0] = 100.
else:
print "WARNING: this hasn't been tested!!! Get it working someday."
if longperi is None:
longperi = 180.
f = an.trueanomaly(ecc, (2*np.pi/per) * (hjd - tt))
z = ars * (1. - ecc**2) * np.sqrt(1. - (np.sin(longperi + f) * np.sin(inc*np.pi/180.))**2) / \
(1. + ecc * np.cos(f))
return z
def uniform(*arg, **kw):
"""Placeholder for my old code; the new function is called
:func:`occultuniform`.
"""
# 2011-04-19 15:06 IJMC: Created
print "The function 'transit.uniform()' is deprecated."
print "Please use transit.occultuniform() in the future."
return occultuniform(*arg, **kw)
def occultuniform(z, p, complement=False, verbose=False):
"""Uniform-disk transit light curve (i.e., no limb darkening).
:INPUTS:
z -- scalar or sequence; positional offset values of planet in
units of the stellar radius.
p -- scalar; planet/star radius ratio.
complement : bool
If True, return (1 - occultuniform(z, p))
:SEE ALSO: :func:`t2z`, :func:`occultquad`, :func:`occultnonlin_small`
"""
# 2011-04-15 16:56 IJC: Added a tad of documentation
# 2011-04-19 15:21 IJMC: Cleaned up documentation.
# 2011-04-25 11:07 IJMC: Can now handle scalar z input.
# 2011-05-15 10:20 IJMC: Fixed indexing check (size, not len)
# 2012-03-09 08:30 IJMC: Added "complement" argument for backwards
# compatibility, and fixed arccos error at
# 1st/4th contact point (credit to
# S. Aigrain @ Oxford)
# 2013-04-13 21:28 IJMC: Some code optimization; ~20% gain.
z = np.abs(np.array(z,copy=True))
fsecondary = np.zeros(z.shape,float)
if p < 0:
pneg = True
p = np.abs(p)
else:
pneg = False
p2 = p*p
if len(z.shape)>0: # array entered
i1 = (1+p)<z
i2 = (np.abs(1-p) < z) * (z<= (1+p))
i3 = z<= (1-p)
i4 = z<=(p-1)
any2 = i2.any()
any3 = i3.any()
any4 = i4.any()
#print i1.sum(),i2.sum(),i3.sum(),i4.sum()
if any2:
zi2 = z[i2]
zi2sq = zi2*zi2
arg1 = 1 - p2 + zi2sq
acosarg1 = (p2+zi2sq-1)/(2.*p*zi2)
acosarg2 = arg1/(2*zi2)
acosarg1[acosarg1 > 1] = 1. # quick fix for numerical precision errors
acosarg2[acosarg2 > 1] = 1. # quick fix for numerical precision errors
k0 = np.arccos(acosarg1)
k1 = np.arccos(acosarg2)
k2 = 0.5*np.sqrt(4*zi2sq-arg1*arg1)
fsecondary[i2] = (1./np.pi)*(p2*k0 + k1 - k2)
fsecondary[i1] = 0.
if any3: fsecondary[i3] = p2
if any4: fsecondary[i4] = 1.
if verbose:
if not (i1+i2+i3+i4).all():
print "warning -- some input values not indexed!"
if (i1.sum()+i2.sum()+i3.sum()+i4.sum() <> z.size):
print "warning -- indexing didn't get the right number of values"
else: # scalar entered
if (1+p)<=z:
fsecondary = 0.
elif (np.abs(1-p) < z) * (z<= (1+p)):
z2 = z*z
k0 = np.arccos((p2+z2-1)/(2.*p*z))
k1 = np.arccos((1-p2+z2)/(2*z))
k2 = 0.5*np.sqrt(4*z2-(1+z2-p2)**2)
fsecondary = (1./np.pi)*(p2*k0 + k1 - k2)
elif z<= (1-p):
fsecondary = p2
elif z<=(p-1):
fsecondary = 1.
if pneg:
fsecondary *= -1
if complement:
return fsecondary
else:
return 1. - fsecondary
def depthchisq(z, planet, data, ddepth=[-.1,.1], ndepth=20, w=None):
#z = transit.t2z(planet, planet.i, hjd, 0.211)
nobs = z.size
depths = np.linspace(ddepth[0],ddepth[1], ndepth)
print depths
chisq = np.zeros(ndepth, float)
for ii in range(ndepth):
tr = -(transit.occultuniform(z, np.sqrt(planet.depth))/depths[ii])
if w is None:
w = np.ones(nobs,float)/data[tr==0].std()
print 'w>>',w[0]
baseline = np.ones(nobs,float) * an.wmean(data[tr==0], w[tr==0])
print 'b>>',baseline[0]
print 'd[ii]>>',depths[ii]
model = baseline + tr*depths[ii]
plot(model)
chisq[ii] = (w*(model-data)**2).sum()
return depths, chisq
def integral_smallplanet_nonlinear(z, p, cn, lower, upper):
"""Return the integral in I*(z) in Eqn. 8 of Mandel & Agol (2002).
-- Int[I(r) 2r dr]_{z-p}^{1}, where:
:INPUTS:
z = scalar or array. Distance between center of star &
planet, normalized by the stellar radius.
p = scalar. Planet/star radius ratio.
cn = 4-sequence. Nonlinear limb-darkening coefficients,
e.g. from Claret 2000.
lower, upper -- floats. Limits of integration in units of mu
:RETURNS:
value of the integral at specified z.
"""
# 2010-11-06 14:12 IJC: Created
# 2012-03-09 08:54 IJMC: Added a cheat for z very close to zero
#import pdb
#z = np.array(z, copy=True)
#z[z==0] = zeroval
#a = (z - p)**2
lower = np.array(lower, copy=True)
upper = np.array(upper, copy=True)
return eval_int_at_limit(upper, cn) - eval_int_at_limit(lower, cn)
def eval_int_at_limit(limit, cn):
"""Evaluate the integral at a specified limit (upper or lower)"""
# 2013-04-17 22:27 IJMC: Implemented some speed boosts; added a
# bug; fixed it again.
# The old way:
#term1 = cn[0] * (1. - 0.8 * np.sqrt(limit))
#term2 = cn[1] * (1. - (2./3.) * limit)
#term3 = cn[2] * (1. - (4./7.) * limit**1.5)
#term4 = cn[3] * (1. - 0.5 * limit**2)
#goodret = -(limit**2) * (1. - term1 - term2 - term3 - term4)
# The new, somewhat faster, way:
sqrtlimit = np.sqrt(limit)
sqlimit = limit*limit
total = 1. - cn[0] * (1. - 0.8 * sqrtlimit)
total -= cn[1] * (1. - (2./3.) * limit)
total -= cn[2] * (1. - (4./7.) * limit*sqrtlimit)
total -= cn[3] * (1. - 0.5 * sqlimit)
ret = -(sqlimit) * total
return ret
def smallplanet_nonlinear(*arg, **kw):
"""Placeholder for backwards compatibility with my old code. The
function is now called :func:`occultnonlin_small`.
"""
# 2011-04-19 15:10 IJMC: Created
print "The function 'transit.smallplanet_nonlinear()' is deprecated."
print "Please use transit.occultnonlin_small() in the future."
return occultnonlin_small(*arg, **kw)
def occultnonlin_small(z,p, cn):
"""Nonlinear limb-darkening light curve in the small-planet
approximation (section 5 of Mandel & Agol 2002).
:INPUTS:
z -- sequence of positional offset values
p -- planet/star radius ratio
cn -- four-sequence nonlinear limb darkening coefficients. If
a shorter sequence is entered, the later values will be
set to zero.
:NOTE:
I had to divide the effect at the near-edge of the light curve
by pi for consistency; this factor was not in Mandel & Agol, so
I may have coded something incorrectly (or there was a typo).
:EXAMPLE:
::
# Reproduce Figure 2 of Mandel & Agol (2002):
from pylab import *
import transit
z = linspace(0, 1.2, 100)
cns = vstack((zeros(4), eye(4)))
figure()
for coef in cns:
f = transit.occultnonlin_small(z, 0.1, coef)
plot(z, f, '--')
:SEE ALSO:
:func:`t2z`
"""
# 2010-11-06 14:23 IJC: Created
# 2011-04-19 15:22 IJMC: Updated documentation. Renamed.
# 2011-05-24 14:00 IJMC: Now check the size of cn.
# 2012-03-09 08:54 IJMC: Added a cheat for z very close to zero
# 2013-04-17 10:51 IJMC: Mild code optimization
#import pdb
cn = np.array([cn], copy=False).ravel()
if cn.size < 4:
cn = np.concatenate((cn, [0.]*(4-cn.size)))
z = np.array(z, copy=False)
F = np.ones(z.shape, float)
z[z==0] = zeroval # cheat!
a = (z - p)**2
b = (z + p)**2
c0 = 1. - np.sum(cn)
Omega = 0.25 * c0 + np.sum( cn / np.arange(5., 9.) )
ind1 = ((1. - p) < z) * ((1. + p) > z)
ind2 = z <= (1. - p)
# Need to specify limits of integration in terms of mu (not r)
aind1 = 1. - a[ind1]
zind1m1 = z[ind1] - 1.
#pdb.set_trace()
if c_integral_smallplanet_nonlinear:
#print 'do it the C way'
Istar_edge = _integral_smallplanet_nonlinear.integral_smallplanet_nonlinear(cn, np.sqrt(aind1), np.array([0.])) / aind1
Istar_inside = _integral_smallplanet_nonlinear.integral_smallplanet_nonlinear(cn, np.sqrt(1. - a[ind2]), np.sqrt(1. - b[ind2])) / z[ind2]
else:
Istar_edge = integral_smallplanet_nonlinear(None, p, cn, \
np.sqrt(aind1), np.array([0.])) / aind1
Istar_inside = integral_smallplanet_nonlinear(None, p, cn, \
np.sqrt(1. - a[ind2]), \
np.sqrt(1. - b[ind2])) / \
(z[ind2])
term1 = 0.25 * Istar_edge / (np.pi * Omega)
term2 = p*p * np.arccos((zind1m1) / p)
term3 = (zind1m1) * np.sqrt(p*p - (zind1m1*zind1m1))
F[ind1] = 1. - term1 * (term2 - term3)
F[ind2] = 1. - 0.0625 * p * Istar_inside / Omega
#pdb.set_trace()
return F
def occultquad(z,p0, gamma, retall=False, verbose=False):
"""Quadratic limb-darkening light curve; cf. Section 4 of Mandel & Agol (2002).
:INPUTS:
z -- sequence of positional offset values
p0 -- planet/star radius ratio
gamma -- two-sequence.
quadratic limb darkening coefficients. (c1=c3=0; c2 =
gamma[0] + 2*gamma[1], c4 = -gamma[1]). If only a single
gamma is used, then you're assuming linear limb-darkening.
:OPTIONS:
retall -- bool.
If True, in addition to the light curve return the
uniform-disk light curve, lambda^d, and eta^d parameters.
Using these quantities allows for quicker model generation
with new limb-darkening coefficients -- the speed boost is
roughly a factor of 50. See the second example below.
:EXAMPLE:
::
# Reproduce Figure 2 of Mandel & Agol (2002):
from pylab import *
import transit
z = linspace(0, 1.2, 100)
gammavals = [[0., 0.], [1., 0.], [2., -1.]]
figure()
for gammas in gammavals:
f = transit.occultquad(z, 0.1, gammas)
plot(z, f)
::
# Calculate the same geometric transit with two different
# sets of limb darkening coefficients:
from pylab import *
import transit
p, b = 0.1, 0.5
x = (arange(300.)/299. - 0.5)*2.
z = sqrt(x**2 + b**2)
gammas = [.25, .75]
F1, Funi, lambdad, etad = transit.occultquad(z, p, gammas, retall=True)
gammas = [.35, .55]
F2 = 1. - ((1. - gammas[0] - 2.*gammas[1])*(1. - F1) +
(gammas[0] + 2.*gammas[1])*(lambdad + 2./3.*(p > z)) + gammas[1]*etad) /
(1. - gammas[0]/3. - gammas[1]/6.)
figure()
plot(x, F1, x, F2)
legend(['F1', 'F2'])
:SEE ALSO:
:func:`t2z`, :func:`occultnonlin_small`, :func:`occultuniform`
:NOTES:
In writing this I relied heavily on the occultquad IDL routine
by E. Agol and J. Eastman, especially for efficient computation
of elliptical integrals and for identification of several
apparent typographic errors in the 2002 paper (see comments in
the source code).
From some cursory testing, this routine appears about 9 times
slower than the IDL version. The difference drops only
slightly when using precomputed quantities (i.e., retall=True).
A large portion of time is taken up in :func:`ellpic_bulirsch`
and :func:`ellke`, but at least as much is taken up by this
function itself. More optimization (or a C wrapper) is desired!
"""
# 2011-04-15 15:58 IJC: Created; forking from smallplanet_nonlinear
# 2011-05-14 22:03 IJMC: Now linear-limb-darkening is allowed with
# a single parameter passed in.
# 2013-04-13 21:06 IJMC: Various code tweaks; speed increased by
# ~20% in some cases.
#import pdb
# Initialize:
z = np.array(z, copy=False)
lambdad = np.zeros(z.shape, float)
etad = np.zeros(z.shape, float)
F = np.ones(z.shape, float)
p = np.abs(p0) # Save the original input
# Define limb-darkening coefficients:
if len(gamma) < 2 or not hasattr(gamma, '__iter__'): # Linear limb-darkening
gamma = np.concatenate([gamma.ravel(), [0.]])
c2 = gamma[0]
else:
c2 = gamma[0] + 2 * gamma[1]
c4 = -gamma[1]
# Test the simplest case (a zero-sized planet):
if p==0:
if retall:
ret = np.ones(z.shape, float), np.ones(z.shape, float), \
np.zeros(z.shape, float), np.zeros(z.shape, float)
else:
ret = np.ones(z.shape, float)
return ret
# Define useful constants:
fourOmega = 1. - gamma[0]/3. - gamma[1]/6. # Actually 4*Omega
a = (z - p)*(z - p)
b = (z + p)*(z + p)
k = 0.5 * np.sqrt((1. - a) / (z * p)) # 8%
p2 = p*p
z2 = z*z
ninePi = 9*np.pi
# Define the many necessary indices for the different cases:
pgt0 = p > 0
i01 = pgt0 * (z >= (1. + p))
i02 = pgt0 * (z > (.5 + np.abs(p - 0.5))) * (z < (1. + p))
i03 = pgt0 * (p < 0.5) * (z > p) * (z < (1. - p))
i04 = pgt0 * (p < 0.5) * (z == (1. - p))
i05 = pgt0 * (p < 0.5) * (z == p)
i06 = (p == 0.5) * (z == 0.5)
i07 = (p > 0.5) * (z == p)
i08 = (p > 0.5) * (z >= np.abs(1. - p)) * (z < p)
i09 = pgt0 * (p < 1) * (z > 0) * (z < (0.5 - np.abs(p - 0.5)))
i10 = pgt0 * (p < 1) * (z == 0)
i11 = (p > 1) * (z >= 0.) * (z < (p - 1.))
#any01 = i01.any()
#any02 = i02.any()
#any03 = i03.any()
any04 = i04.any()
any05 = i05.any()
any06 = i06.any()
any07 = i07.any()
#any08 = i08.any()
#any09 = i09.any()
any10 = i10.any()
any11 = i11.any()
#print n01, n02, n03, n04, n05, n06, n07, n08, n09, n10, n11
if verbose:
allind = i01 + i02 + i03 + i04 + i05 + i06 + i07 + i08 + i09 + i10 + i11
nused = (i01.sum() + i02.sum() + i03.sum() + i04.sum() + \
i05.sum() + i06.sum() + i07.sum() + i08.sum() + \
i09.sum() + i10.sum() + i11.sum())
print "%i/%i indices used" % (nused, i01.size)
if not allind.all():
print "Some indices not used!"
#pdb.set_trace()
# Lambda^e and eta^d are more tricky:
# Simple cases:
lambdad[i01] = 0.
etad[i01] = 0.
if any06:
lambdad[i06] = 1./3. - 4./ninePi
etad[i06] = 0.09375 # = 3./32.
if any11:
lambdad[i11] = 1.
# etad[i11] = 1. # This is what the paper says
etad[i11] = 0.5 # Typo in paper (according to J. Eastman)
# Lambda_1:
ilam1 = i02 + i08
q1 = p2 - z2[ilam1]
## This is what the paper says:
#ellippi = ellpic_bulirsch(1. - 1./a[ilam1], k[ilam1])
# ellipe, ellipk = ellke(k[ilam1])
# This is what J. Eastman's code has:
# 2011-04-24 20:32 IJMC: The following codes act funny when
# sqrt((1-a)/(b-a)) approaches unity.
qq = np.sqrt((1. - a[ilam1]) / (b[ilam1] - a[ilam1]))
ellippi = ellpic_bulirsch(1./a[ilam1] - 1., qq)
ellipe, ellipk = ellke(qq)
lambdad[ilam1] = (1./ (ninePi*np.sqrt(p*z[ilam1]))) * \
( ((1. - b[ilam1])*(2*b[ilam1] + a[ilam1] - 3) - \
3*q1*(b[ilam1] - 2.)) * ellipk + \
4*p*z[ilam1]*(z2[ilam1] + 7*p2 - 4.) * ellipe - \
3*(q1/a[ilam1])*ellippi)
# Lambda_2:
ilam2 = i03 + i09
q2 = p2 - z2[ilam2]
## This is what the paper says:
#ellippi = ellpic_bulirsch(1. - b[ilam2]/a[ilam2], 1./k[ilam2])
# ellipe, ellipk = ellke(1./k[ilam2])
# This is what J. Eastman's code has:
ailam2 = a[ilam2] # Pre-cached for speed
bilam2 = b[ilam2] # Pre-cached for speed
omailam2 = 1. - ailam2 # Pre-cached for speed
ellippi = ellpic_bulirsch(bilam2/ailam2 - 1, np.sqrt((bilam2 - ailam2)/(omailam2)))
ellipe, ellipk = ellke(np.sqrt((bilam2 - ailam2)/(omailam2)))
lambdad[ilam2] = (2. / (ninePi*np.sqrt(omailam2))) * \
((1. - 5*z2[ilam2] + p2 + q2*q2) * ellipk + \
(omailam2)*(z2[ilam2] + 7*p2 - 4.) * ellipe - \
3*(q2/ailam2)*ellippi)
# Lambda_3:
#ellipe, ellipk = ellke(0.5/ k) # This is what the paper says
if any07:
ellipe, ellipk = ellke(0.5/ p) # Corrected typo (1/2k -> 1/2p), according to J. Eastman
lambdad[i07] = 1./3. + (16.*p*(2*p2 - 1.)*ellipe -
(1. - 4*p2)*(3. - 8*p2)*ellipk / p) / ninePi
# Lambda_4
#ellipe, ellipk = ellke(2. * k) # This is what the paper says
if any05:
ellipe, ellipk = ellke(2. * p) # Corrected typo (2k -> 2p), according to J. Eastman
lambdad[i05] = 1./3. + (2./ninePi) * (4*(2*p2 - 1.)*ellipe + (1. - 4*p2)*ellipk)
# Lambda_5
## The following line is what the 2002 paper says:
#lambdad[i04] = (2./(3*np.pi)) * (np.arccos(1 - 2*p) - (2./3.) * (3. + 2*p - 8*p2))
# The following line is what J. Eastman's code says:
if any04:
lambdad[i04] = (2./3.) * (np.arccos(1. - 2*p)/np.pi - \
(6./ninePi) * np.sqrt(p * (1.-p)) * \
(3. + 2*p - 8*p2) - \
float(p > 0.5))
# Lambda_6
if any10:
lambdad[i10] = -(2./3.) * (1. - p2)**1.5
# Eta_1:
ilam3 = ilam1 + i07 # = i02 + i07 + i08
z2ilam3 = z2[ilam3] # pre-cache for better speed
twoZilam3 = 2*z[ilam3] # pre-cache for better speed
#kappa0 = np.arccos((p2+z2ilam3-1)/(p*twoZilam3))
#kappa1 = np.arccos((1-p2+z2ilam3)/(twoZilam3))
#etad[ilam3] = \
# (0.5/np.pi) * (kappa1 + kappa0*p2*(p2 + 2*z2ilam3) - \
# 0.25*(1. + 5*p2 + z2ilam3) * \
# np.sqrt((1. - a[ilam3]) * (b[ilam3] - 1.)))
etad[ilam3] = \
(0.5/np.pi) * ((np.arccos((1-p2+z2ilam3)/(twoZilam3))) + (np.arccos((p2+z2ilam3-1)/(p*twoZilam3)))*p2*(p2 + 2*z2ilam3) - \
0.25*(1. + 5*p2 + z2ilam3) * \
np.sqrt((1. - a[ilam3]) * (b[ilam3] - 1.)))
# Eta_2:
etad[ilam2 + i04 + i05 + i10] = 0.5 * p2 * (p2 + 2. * z2[ilam2 + i04 + i05 + i10])
# We're done!