-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexofast_chi2v2.pro
1739 lines (1503 loc) · 80.7 KB
/
exofast_chi2v2.pro
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
;+
; NAME:
; EXOFAST_CHI2
;
; PURPOSE:
; Computes the chi2 for a transit and/or RV for a single planet
;
; CALLING SEQUENCE:
; chi2 = exofast_chi2v2(pars)
;
; INPUTS:
;
; PARS - An array of parameters that describes the model. There
; should be one for each parameter in the structure where
; fit==true. This array should be generated by str2pars.pro.
;
; OPTIONAL INPUTS:
;
; PSNAME - If specified, this routine will generate
; postscript figure for each model generated with a
; base of PSNAME.
;
; OPTIONAL OUTPUTS:
;
; DETERMINANT - The determinant of the Jacobian to weight the
; acceptance likelihood in order to impose
; non-uniform priors in the stepping parameters. This
; is equal to 1 (no transformation) unless MIST
; models are used. If MIST models are used,
; DETERMINANT is equal to d(EEP)/d(Age) to transform
; the uniform EEP prior into a uniform Age prior.
; MODELRV - The RV model given the parameters
; MODELFLUX - The Transit model given the parameters
; DERIVED - An array of derived parameters. Nothing is ever
; returned in DERIVED, but this keyword is required
; by EXOFAST_DEMC.
;
; RESULT:
; The chi^2 of the model given the data and parameters.
;
; COMMON BLOCKS:
; CHI2_BLOCK - See exofastv2.pro for definition
;
; MODIFICATION HISTORY
;
; 2018/03 -- Create documentation -- Jason Eastman (CfA)
;-
function exofast_chi2v2, pars, determinant=determinant, $
modelrv=modelrv, modelflux=modelflux, psname=psname, $
derived=derived, loadss=loadss, ss0=ss0
COMMON chi2_block, ss
;; populate the structure with the new parameters
if keyword_set(loadss) then begin
ss = ss0
return, -1
endif
;; this is just a time sink for debugging/evaluating multithreading
;; ss.delay should never be > 0 in production code!!
;for i=0L, 2.5d6 do t = i ;; this takes 0.05 seconds on my machine
;for i=0L, 2.5d7 do t = i ;; this takes 0.5 seconds on my machine
for i=0L, ss.delay do t = i
if n_elements(pars) ne 0 then pars2str, pars, ss
au = ss.constants.au/ss.constants.rsun
;; derive all required parameters
;; (this may change depending on parameterization)
;derivepars, ss
;; initialize the determinant and chi^2
chi2 = 0.d0
determinant = 1d0
;; if values are linked, we must propagate them before we check boundaries
;; if value is a map, and the variable is fixed, we must propagate it here
for i=0L, n_elements(*ss.priors)-1 do begin
;; Skip priors with penalties -- the penalties will guide them to agree
prior = (*ss.priors)[i]
;; if it's not fixed to a linked parameter, skip it. The penalties will sort it out
if prior.gaussian_width ne 0d0 or prior.value[1] eq -1 then continue
;; the prior is linked to another variable -- get its value
if prior.value[4] ne -1 then begin
value = (*ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]]).(prior.value[4])[prior.value[5]].value
label = (*ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]]).(prior.value[4])[prior.value[5]].label
endif else if prior.value[2] ne -1 then begin
value = ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]].value
label = ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]].label
endif else begin
value = ss.(prior.value[0])[prior.value[1]].value
label = ss.(prior.value[0])[prior.value[1]].label
endelse
;; assign the value
if prior.map[4] ne -1 then begin
(*ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]]).(prior.map[4])[prior.map[5]].value = value
endif else if prior.map[2] ne -1 then begin
ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]].value = value
endif else if prior.map[0] ne -1 then begin
ss.(prior.map[0])[prior.map[1]].value = value
endif
if ss.verbose then printandlog, $
'linking ' + prior.name + ' to ' + label
endfor
;; *** First make sure step parameters are in bounds ***
;; physical limb darkening (Kipping, 2013)
;; http://adsabs.harvard.edu/abs/2013MNRAS.435.2152K, eq 8
if (where(ss.planet.fittran))[0] ne -1 then begin
bad = where(ss.band.u1.value + ss.band.u2.value gt 1d0 or $
ss.band.u1.value lt 0d0 or $
ss.band.u1.value + 2d0*ss.band.u2.value lt 0d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, $
strtrim(nbad,2) + ' limb darkening parameters are bad (' + strtrim(ss.band[bad[0]].u1.value,2) + ', ' + strtrim(ss.band[bad[0]].u2.value,2) + ')',ss.logname
return, !values.d_infinity
endif
endif
;; -180 < phase shift < 180
bad = where(abs(ss.band.phaseshift.value) gt 180d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'phase shift is bad (' + strtrim(bad,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 0.1 < Period < 10^13 (~age of the universe)
bad = where(ss.planet.logp.value gt 13d0 or ss.planet.logp.value lt -1d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'logP is bad (' + strtrim(bad,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -max(period)/2 < ttv < max(period)/2
bad = where(abs(ss.transit.ttv.value) gt max(ss.planet.period.value)/2d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'ttv is bad (' + strtrim(ss.transit[bad].ttv.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; older than the universe (too conservative?)
bad = where(ss.star.age.value gt 13.82d0 or ss.star.age.value lt 0d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'age is bad (' + strtrim(ss.star[bad].age.value,2) +')',ss.logname
return, !values.d_infinity
endif
;; positive extinction
bad = where(ss.star.av.value lt 0 or ss.star.av.value gt 1d3, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'extinction is bad (' + strtrim(ss.star[bad].av.value,2) + ')',ss.logname
return, !values.d_infinity
endif
;; can't have more than +/-100% dilution
;bad = where(ss.band.dilute.fit and (ss.band.dilute.value le -1d0 or ss.band.dilute.value ge 1d0),nbad)
bad = where(ss.transit.dilute.fit and (ss.transit.dilute.value le -1d0 or ss.transit.dilute.value ge 1d0),nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'dilution is bad (' + strtrim(ss.transit[bad].dilute.value,2) + ')',ss.logname
return, !values.d_infinity
endif
;; 0.9 AU to the size of the universe
bad = where(ss.star.distance.value lt 4d-6 or ss.star.distance.value gt 3d10,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'distance is bad (' + strtrim(ss.star[bad].distance.value,2) + ')',ss.logname
return, !values.d_infinity
endif
;; check vcve
bad = where(ss.planet.vcve.value lt 0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'vcve is bad (' + strtrim(ss.planet[bad].vcve.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; check sign
bad = where(ss.planet.sign.value lt 0 or ss.planet.sign.value ge 2 or ss.planet.sign2.value lt 0 or ss.planet.sign2.value ge 2, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'sign is bad (' + strtrim(ss.planet[bad].sign.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; Omega boundary checking
bad = where(ss.planet.lsinbigomega.value^2 + ss.planet.lsinbigomega.value^2 gt 1d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'bigomega is bad', ss.logname
return, !values.d_infinity
endif
;; limit 0 < Omega < 180 if RVs or Transits not fit
bad = where((ss.planet.lsinbigomega.value lt 0d0 or ss.planet.lcosbigomega.value lt 0d0) and (ss.fitrv or ss.fittran), nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'bigomega is bad', ss.logname
return, !values.d_infinity
endif
;; lambda boundary checking
bad = where(ss.planet.lsinlambda.value^2 + ss.planet.lcoslambda.value^2 gt 1d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'lambda is bad', ss.logname
return, !values.d_infinity
endif
;; ramp boundary checking
rampfit = where(ss.transit.rampexp.fit)
if rampfit[0] ne -1 then begin
for i=0L, n_elements(rampfit)-1 do begin
transit = *(ss.transit[rampfit[i]].transitptrs)
if ss.transit[rampfit[i]].rampexp.value le 0d0 or ss.transit[rampfit[i]].rampexp.value gt transit.timerange*10d0 then begin
if ss.debug or ss.verbose then printandlog, 'rampexp ' + strtrim(rampfit[i],2) + ' is bad (' + strtrim(ss.transit[rampfit[i]].rampexp.value,2) + ')', ss.logname
return, !values.d_infinity
endif
endfor
endif
;; -c < gamma < c
bad = where(abs(ss.telescope.gamma.value) gt ss.constants.c/ss.constants.meter,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'gamma is bad (' + strtrim(ss.telescope[bad].gamma.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; excludes non-transiting planets where the transit is fit
;; NOTE: chord is not derived by step2pars, only check if chord is fit
bad = where((ss.planet.chord.value le 0d0 or ss.planet.chord.value gt (1d0+ss.planet.p.value)) and ss.planet.chord.fit,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'chord is bad (' + strtrim(bad,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -c^2 < jitter^2 < c^2
bad = where(abs(ss.telescope.jittervar.value) gt (ss.constants.c/ss.constants.meter)^2,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'jittervar is bad (' + strtrim(ss.telescope[bad].jittervar.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; abs(slope) < 1d5 m/s/day
bad = where(abs(ss.star.slope.value) gt 1d5,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'slope is bad (' + strtrim(ss.star[bad].slope.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; abs(quad) < 1d5 m/s/day^2
bad = where(abs(ss.star.quad.value) gt 1d5,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'quad is bad (' + strtrim(ss.star[bad].quad.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 0.01 < SED error scaling < 100
bad = where(ss.star.errscale.value lt 1d-2 or ss.star.errscale.value gt 1d2, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'error scale is bad (' + strtrim(ss.star[bad].errscale.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 0.01 < Specphot error scaling < 10000
bad = where(ss.specphot.sperrscale.value lt 1d-2 or ss.specphot.sperrscale.value gt 1d5, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'spectrophotometry error scale is bad (' + strtrim(ss.specphot[bad].sperrscale.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -0.2 < spzeropoint < 0.2
;bad = where(ss.specphot.spzeropoint.value lt -0.3d0 or ss.specphot.spzeropoint.value gt 0.3d0, nbad)
bad = where(ss.specphot.spzeropoint.value lt -0.999d0 or ss.specphot.spzeropoint.value gt 999d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'spectrophotometry zero point is bad (' + strtrim(ss.specphot[bad].spzeropoint.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -10 < [Fe/H] < 2
bad = where(ss.star.feh.value lt -10d0 or ss.star.feh.value gt 2d0 ,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'teff is bad (' + strtrim(ss.star[bad].teff.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 100 < Teff < 50000
bad = where(ss.star.teff.value lt 100 or ss.star.teff.value gt 250000,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'teff is bad (' + strtrim(ss.star[bad].teff.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 100 < Teff < 50000
bad = where(ss.star.teffsed.value lt 100 or ss.star.teffsed.value gt 250000,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'teffsed is bad (' + strtrim(ss.star[bad].teffsed.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 10^-6 < rstar < 2000
bad = where(ss.star.rstar.value lt 1d-6 or ss.star.rstar.value gt 2000d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'rstar is bad (' + strtrim(ss.star[bad].rstar.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 10^-6 < rstar < 2000
bad = where(ss.star.rstarsed.value lt 1d-6 or ss.star.rstarsed.value gt 2000d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'rstarsed is bad (' + strtrim(ss.star[bad].rstar.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 10^-3 < mstar < 500
bad = where(ss.star.logmstar.value lt -3 or ss.star.logmstar.value gt 2.6989700d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'mstar is bad (' + strtrim(ss.star[bad].mstar.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; bound marginally detected planets to limit (infinite) parameter space at low logmp
;; conservative lower limit of 1 Ceres in 1 year orbit around sun = 10 um/s
;; conservative upper limit corresponds to 500 solar masses (larger
;; than the largest known star)
bad = where((ss.planet.logmp.value lt -9.4d0 or ss.planet.logmp.value gt 2.7d0) and ss.planet.logmp.fit, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'logmp is bad (' + strtrim(ss.planet[bad].logmp.value,2) + ')', ss.logname
return, !values.d_infinity
endif
if ss.nastrom gt 0 then begin
;; 0.01 < astrometric error scaling < 100
bad = where(ss.astrom.astromscale.value lt 1d-2 or ss.astrom.astromscale.value gt 1d2, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'astrometry error scale is bad (' + strtrim(ss.astrom[bad].errscale.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; 0 <= ra+raoffset <= 360
bad = where(ss.star[ss.astrom.starndx].ra.value + ss.astrom.raoffset.value lt 0 or ss.star[ss.astrom.starndx].ra.value + ss.astrom.raoffset.value gt 360,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'ra is bad (' + strtrim(ss.star[ss.astrom.starndx].ra.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -90 <= dec+decoffset <= 90
bad = where(abs(ss.star[ss.astrom.starndx].dec.value + ss.astrom.decoffset.value) gt 90,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'dec is bad (' + strtrim(ss.star[ss.astrom.starndx].dec.value +ss.astrom[bad].decoffset.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -20000 <= pmra <= 20000 (2x barnard's star)
if abs(ss.star[ss.astrom.starndx].pmra.value) gt 2d4 then begin
if ss.debug or ss.verbose then printandlog, 'pmra is bad (' + strtrim(ss.star[ss.astrom.starndx].pmra.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -20000 <= pmdec <= 20000 (2x barnard's star)
if abs(ss.star[ss.astrom.starndx].pmdec.value) gt 2d4 then begin
if ss.debug or ss.verbose then printandlog, 'pmdec is bad (' + strtrim(ss.star[ss.astrom.starndx].pmdec.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; -c <= rvabs <= c
if abs(ss.star[ss.astrom.starndx].rvabs.value) gt ss.constants.c/ss.constants.meter then begin
if ss.debug or ss.verbose then printandlog, 'pmdec is bad (' + strtrim(ss.star[ss.astrom.starndx].pmdec.value,2) + ')', ss.logname
return, !values.d_infinity
endif
endif
;; 0.01 < DT error scaling < 100
bad = where(ss.doptom.dtscale.value le 1d-2 or ss.doptom.dtscale.value gt 1d2, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'dtscale is bad (' + strtrim(ss.doptom[bad].dtscale.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; limit range of alpha abundance (not used yet)
;bad = where(ss.star.alpha.value lt -0.3d0 or ss.star.alpha.value gt 0.7d0,nbad)
;if nbad gt 0 then begin
; if ss.debug or ss.verbose then printandlog, 'alpha is bad (' + strtrim(ss.star[bad].alpha.value,2) + ')', ss.logname
; return, !values.d_infinity
;endif
;; require planets to stay in the same order as they start
if ss.nplanets gt 0 then begin
if max(abs(ss.planetorder - sort(ss.planet.logp.value))) ne 0 then begin
if keyword_set(ss.verbose) then printandlog, 'Planets must remain in the original order! Rejecting step', logname
return, !values.d_infinity
endif
endif
;; **** Now place additional physical constraints on derived parameters ****
;; derive the model parameters from the step parameters
;; (returns -1 if can't be derived due to non-physical inputs)
if step2pars(ss,verbose=(ss.debug or ss.verbose),logname=ss.logname) eq -1 then begin
if ss.debug or ss.verbose then printandlog, 'stellar system is bad', ss.logname
return, !values.d_infinity
endif
;; check angular parameters
;; omega boundary checking
bad = where(ss.planet.lsinw.value^2 + ss.planet.lcosw.value^2 gt 1d0, nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'omega is bad', ss.logname
return, !values.d_infinity
endif
;; tighter (empirical) constraint on eccentricity (see Eastman 2013)
if ss.tides then begin
bad = where(ss.planet.e.value gt (1d0-3d0/ss.planet.ar.value), nbad)
if nbad ne 0 then begin
if ss.verbose then $
printandlog, 'TIDES flag set and eccentricity (' + strtrim(ss.planet[bad].e.value,2) + $
') exceeds empirical limit (' + strtrim((1d0-3d0/ss.planet[bad].ar.value),2) + ')', ss.logname
return, !values.d_infinity
endif
endif
;; no transit and we're fitting a transit! This causes major
;; problems; exclude this model (but this biases the significance of
;; the detection)
bad = where(~ss.noprimary and ss.fittran and ss.planet.b.value gt (1d0+ss.planet.p.value), nbad)
if nbad ne 0L then begin
if ss.verbose then printandlog, 'Planet ' + strtrim(bad,2) + ' has no primary transit!', ss.logname
return, !values.d_infinity
endif
bad = where(ss.requiresecondary and ss.planet.bs.value gt (1d0+ss.planet.p.value), nbad)
if nbad ne 0L then begin
if ss.verbose then printandlog, 'Planet ' + strtrim(bad,2) + ' has no secondary eclipse!', ss.logname
return, !values.d_infinity
endif
;; for multi-planet systems, make sure they don't enter each other's hill spheres
;; if mp unknown, mp=0 => hill radius=0 => planets can't cross orbits
;; **** ignores mutual inclination; a priori excludes systems like
;; Neptune and Pluto!! ****
if ~ss.alloworbitcrossing then begin
mindist = dblarr(ss.nplanets>1)
maxdist = dblarr(ss.nplanets>1)
for i=0L, ss.nplanets-1 do begin
hillradius = ((1d0-ss.planet[i].e.value)*ss.planet[i].a.value*(ss.planet[i].mpsun.value/(3d0*ss.star[ss.planet[i].starndx].mstar.value))^(1d0/3d0)) > 0d0
mindist[i] = (1d0-ss.planet[i].e.value)*ss.planet[i].a.value - hillradius
maxdist[i] = (1d0+ss.planet[i].e.value)*ss.planet[i].a.value + hillradius
for j=i-1,0,-1 do begin
if ((mindist[i] ge mindist[j]) and (mindist[i] le maxdist[j])) or $
((maxdist[i] ge mindist[j]) and (maxdist[i] le maxdist[j])) or $
((mindist[j] ge mindist[i]) and (mindist[j] le maxdist[i])) or $
((maxdist[j] ge mindist[i]) and (maxdist[j] le maxdist[i])) then begin
if ss.verbose then printandlog, 'Planets ' + strtrim(j,2) + ' (' + strtrim(mindist[j],2) + ',' + strtrim(maxdist[j],2) + ') and ' + strtrim(i,2) + ' (' + strtrim(mindist[i],2) + ',' + strtrim(maxdist[i],2) + ') cross paths', logname
return, !values.d_infinity
endif
endfor
endfor
endif
;; must do after step2pars -- sometimes tt is derived
;; tt-period/2 < tt < tt+period/2
bad = where(ss.planet.tt.prior ne 0d0 and $
abs(ss.planet.tt.value - ss.planet.tt.prior) gt ss.planet.period.value/2d0,nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'tt is bad (' + strtrim(ss.planet[bad].tt.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; must do after step2pars -- sometimes tc is derived
;; tc-period/2 < tc < tc+period/2
bad = where(ss.planet.tc.prior ne 0d0 and $
abs(ss.planet.tc.value - ss.planet.tc.prior) gt ss.planet.period.value/2d0,nbad)
if nbad gt 0 and ss.nplanets gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'tc is bad (' + strtrim(ss.planet[bad].tc.value,2) + ')', ss.logname
return, !values.d_infinity
endif
;; must do after step2pars -- sometimes cosi is derived
;; 0 <= cosi <= 1 (or -1 <= cosi <= 1 if i180 keyword set or astrometry fit)
bad = where(ss.planet.cosi.value gt 1 or (ss.planet.cosi.value lt 0 and ~ss.planet.i180) or (ss.planet.cosi.value lt -1),nbad)
if nbad gt 0 then begin
if ss.debug or ss.verbose then printandlog, 'cosi is bad (' + strtrim(bad,2) + ')', ss.logname
return, !values.d_infinity
endif
;; *** Now start penalizing the model ***
;; ****************** apply priors ***************************
for i=0L, n_elements(*ss.priors)-1 do begin
prior = (*ss.priors)[i]
;; get the model value
if prior.map[4] ne -1 then begin
model_value = (*ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]]).(prior.map[4])[prior.map[5]].value
unit = strupcase((*ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]]).(prior.map[4])[prior.map[5]].unit)
label = (*ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]]).(prior.map[4])[prior.map[5]].label
endif else if prior.map[2] ne -1 then begin
model_value = ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]].value
unit = strupcase(ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]].unit)
label = ss.(prior.map[0])[prior.map[1]].(prior.map[2])[prior.map[3]].label
endif else if prior.map[0] ne -1 then begin
model_value = ss.(prior.map[0])[prior.map[1]].value
unit = strupcase(ss.(prior.map[0])[prior.map[1]].unit)
label = ss.(prior.map[0])[prior.map[1]].label
endif
;; get the prior value
if prior.value[1] ne -1 then begin
;; this is a link to another variable
if prior.value[4] ne -1 then begin
prior_value = (*ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]]).(prior.value[4])[prior.value[5]].value
endif else if prior.value[2] ne -1 then begin
prior_value = ss.(prior.value[0])[prior.value[1]].(prior.value[2])[prior.value[3]].value
endif else begin
prior_value = ss.(prior.value[0])[prior.value[1]].value
endelse
;; uniform bounds are relative to the other variable
lowerbound = prior_value + prior.lowerbound
upperbound = prior_value + prior.upperbound
endif else begin
;; this is a direct prior
prior_value = prior.value[0]
lowerbound = prior.lowerbound
upperbound = prior.upperbound
endelse
;; if it's a (periodic) time, make sure we propagate to the right epoch
if prior.name eq 'tc' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].tc[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 'tt' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].tt[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 'ts' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].ts[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 't0' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].t0[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 'tp' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].tp[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 'td' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].td[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif else if prior.name eq 'ta' then begin
period = ss.(prior.map[0])[prior.map[1]].period[prior.map[3]].value
time0 = ss.(prior.map[0])[prior.map[1]].ta[prior.map[3]].value
nper = round((prior_value-time0)/period)
prior_value = prior_value + nper*period
upperbound = upperbound + nper*period
lowerbound = lowerbound + nper*period
endif
if model_value gt upperbound or model_value lt lowerbound then begin
if ss.debug or ss.verbose then $
printandlog, label + '( ' + strtrim(model_value,2) + ') is out of user-defined bounds (' +$
strtrim(lowerbound,2) + ',' + strtrim(upperbound,2) + ')',ss.logname
return, !values.d_infinity
endif
;; no gaussian prior, skip
if prior.gaussian_width le 0d0 then continue
;; if it's an angular parameter, make sure we handle the boundary
if unit eq 'RADIANS' then begin
priorchi2 = (atan(sin(model_value-prior_value),cos(model_value-prior_value))/prior.gaussian_width)^2
endif else if unit eq 'DEGREES' then begin
priorchi2 = (atan(sin((model_value-prior_value)*!dpi/180d0),cos((model_value-prior_value)*!dpi/180d0))/(prior.gaussian_width*!dpi/180d0))^2
endif else begin
priorchi2 = ((model_value - prior_value)/prior.gaussian_width)^2
endelse
;; output debugging info if requested
if ss.verbose then begin
str = string(label,priorchi2,model_value,prior_value,prior.gaussian_width, $
format='(a," penalty = ",f0.6," (model value=",f0.6,", prior value=",f0.6,", gaussian width=",f0.6,")")')
printandlog, str, ss.logname
endif
chi2 += priorchi2
endfor
;; ******************************************************************
if 0 then begin
priors = *(ss.priors)
for i=0, n_elements(priors[0,*])-1 do begin
;; if it's not a detrending variable
if priors[3,i] eq -1 then begin
value = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).value
upperbound = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).upperbound
lowerbound = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).lowerbound
label = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).label + '_' + strtrim(priors[1,i],2)
prior = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).prior
priorwidth = ss.(priors[0,i])[priors[1,i]].(priors[2,i]).priorwidth
unit = strupcase(ss.(priors[0,i])[priors[1,i]].(priors[2,i]).unit)
endif else begin
;; otherwise
value = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].value
upperbound = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].upperbound
lowerbound = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].lowerbound
label = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].label + '_' + strtrim(priors[1,i],2)
prior = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].prior
priorwidth = (*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].priorwidth
unit = strupcase((*(ss.(priors[0,i])[priors[1,i]].(priors[2,i]))).(priors[3,i])[priors[4,i]].unit)
endelse
;; apply the bounds
if value gt upperbound or value lt lowerbound then begin
if ss.debug or ss.verbose then $
printandlog, label + '( ' + strtrim(value,2) + ') is out of user-defined bounds (' +$
strtrim(lowerbound,2) + ',' + strtrim(upperbound,2) + ')',ss.logname
return, !values.d_infinity
endif
;; apply the Gaussian prior
;; if it's an angular parameter, make sure we handle the boundary
if unit eq 'RADIANS' then begin
chi2 += (atan(sin(value-prior),cos(value-prior))/priorwidth)^2
endif else if unit eq 'DEGREES' then begin
chi2 += (atan(sin((value-prior)*!dpi/180d0),cos((value-prior)*!dpi/180d0))/(priorwidth*!dpi/180d0))^2
endif else begin
chi2 += ((value - prior)/priorwidth)^2
endelse
;; output debugging info if requested
if ss.verbose then begin
str = string(label,((value - prior)/priorwidth)^2, format='(a," penalty = ",f0.6)')
printandlog, str, ss.logname
endif
endfor
endif
;; **** apply corrections to make priors physical ****
; if fitting in v_c/v_e & omega, correct for a uniform eccentricity prior
for i=0, ss.nplanets-1 do begin
;; vcve jacobian
if ss.planet[i].vcve.fit then begin
dvcvede = abs((-sin(ss.planet[i].omega.value)-ss.planet[i].e.value)/$
(sqrt(1d0-ss.planet[i].e.value^2)*(ss.planet[i].esinw.value+1d0)^2))
determinant *= dvcvede
endif
;; chord jacobian
if ss.planet[i].chord.fit then begin
dchorddcosi = ss.planet[i].b.value^2/(ss.planet[i].cosi.value*ss.planet[i].chord.value)
determinant *= dchorddcosi
endif
;; linear eccentricity prior
;; we never fit in ecosw & esinw; a waste of time
;if ss.planet[i].ecosw.fit and ss.planet[i].esinw.fit then begin
; determinant *= ss.planet[i].e.value
;endif
endfor
;; closer stars should have lower extinction
if keyword_set(avprior) then begin
sorted = sort(ss.star.distance.value)
for i=1L, ss.nstars-1 do begin
if ss.star[sorted[i]].av.value lt ss.star[sorted[i-1]].av.value then begin
if ss.verbose then printandlog, 'Star ' + strtrim(sorted[i],2) + $
' (Av=' + strtrim(ss.star[sorted[i]].av.value,2) + $
', distance=' + strtrim(ss.star[sorted[i]].distance.value,2) + ') '+ $
' has a lower extinction than star ' + strtrim(sorted[i-1],2) + $
' (Av=' + strtrim(ss.star[sorted[i-1]].av.value,2) + $
', distance=' + strtrim(ss.star[sorted[i-1]].distance.value,2) + $
') despite being farther away. ' + $
'Set /NOAVPRIOR to disable this constraint.'
return, !values.d_infinity
endif
endfor
endif
;; *** Now computing model constraints ***
for i=0L, ss.nstars-1 do begin
;; apply YY penalty to constrain stellar parameters
if ss.yy[i] then begin
if keyword_set(psname) then epsname = psname+'.yy.eps'
sigmab = ss.constants.sigmab/ss.constants.lsun*ss.constants.rsun^2
yychi2 = massradius_yy3(ss.star[i].mstar.value, ss.star[i].feh.value, $
ss.star[i].age.value, ss.star[i].teff.value,$
yyrstar=yyrstar, debug=ss.debug, psname=epsname, $
sigmab=sigmab,$
gravitysun=ss.constants.gravitysun)
if ~finite(yychi2) then begin
if ss.debug or ss.verbose then $
printandlog, 'star not on YY tracks', ss.logname
return, !values.d_infinity
endif
yychi2 += ((ss.star[i].rstar.value - yyrstar)/(0.03d0*yyrstar))^2
chi2 += yychi2
if ss.verbose then $
printandlog, 'YY penalty = ' + strtrim(yychi2,2), ss.logname
endif
;; apply PARSEC penalty to constrain stellar parameters
if ss.parsec[i] then begin
if keyword_set(psname) then $
epsname = psname+'.parsec.' + string(i,format='(i03)') + '.eps'
parsecchi2 = massradius_parsec(ss.star[i].eep.value, $
ss.star[i].mstar.value, $
ss.star[i].initfeh.value, $
ss.star[i].age.value, $
ss.star[i].teff.value,$
ss.star[i].rstar.value, $
ss.star[i].feh.value, debug=ss.debug, $
epsname=epsname, $
gravitysun=ss.constants.gravitysun, $
/fitage,$;=ss.star[i].age.fit, $
ageweight=ageweight, $
logname=ss.logname, verbose=ss.verbose,$
tefffloor=ss.teffemfloor, $
fehfloor=ss.fehemfloor, $
rstarfloor=ss.rstaremfloor, $
agefloor=ss.ageemfloor)
chi2 += parsecchi2
if ss.verbose then $
printandlog, 'PARSEC penalty = ' + strtrim(parsecchi2,2), ss.logname
if ~finite(chi2) then begin
if ss.debug or ss.verbose then $
printandlog, 'star not on PARSEC tracks', ss.logname
return, !values.d_infinity
endif
determinant *= ageweight ;; correct uniform EEP prior to uniform Age prior
endif
;; apply MIST penalty to constrain stellar parameters
if ss.mist[i] then begin
if keyword_set(psname) then epsname = psname + '.mist.' + string(i,format='(i03)') + '.eps'
mistchi2 = massradius_mist(ss.star[i].eep.value, $
ss.star[i].mstar.value,$
ss.star[i].initfeh.value, $
ss.star[i].age.value, $
ss.star[i].teff.value,$
ss.star[i].rstar.value, $
ss.star[i].feh.value, $
debug=ss.debug, $
epsname=epsname, $
gravitysun=ss.constants.gravitysun, $
/fitage,$;=ss.star[i].age.fit, $
ageweight=ageweight, $
logname=ss.logname, verbose=ss.verbose,$
tefffloor=ss.teffemfloor, $
fehfloor=ss.fehemfloor, $
rstarfloor=ss.rstaremfloor, $
agefloor=ss.ageemfloor, pngname=pngname,$
range=ss.emrange)
;; if there's more than one star with the same age and initfeh,
;; make an isochrone with all of them on it
if keyword_set(psname) then begin
match = where(ss.star.age.value eq ss.star[i].age.value and $
ss.star.initfeh.value eq ss.star[i].initfeh.value,nmatch)
if nmatch gt 1 and match[0] eq i then begin
isoname = psname + '.mistiso.' +string(i,format='(i03)') + '.eps'
plotisochrone, ss.star[i].age.value, ss.star[i].initfeh.value,$
mstar=ss.star[match].mstar.value,$
teff=ss.star[match].teff.value,$
rstar=ss.star[match].rstar.value,$
/plotmodel,epsname=isoname
endif
endif
chi2 += mistchi2
if ss.verbose then $
printandlog, 'MIST penalty = ' + strtrim(mistchi2,2), ss.logname
if ~finite(chi2) then begin
if ss.debug or ss.verbose then $
printandlog, 'star not on MIST tracks', ss.logname
return, !values.d_infinity
endif
determinant *= ageweight ;; correct uniform EEP prior to uniform Age prior
endif
;; apply TORRES penalty to constrain stellar parameters
if ss.torres[i] then begin
massradius_torres, ss.star[i].logg.value, ss.star[i].teff.value, $
ss.star[i].feh.value, mstar_prior, rstar_prior
umstar = 0.027d0
urstar = 0.014d0
if mstar_prior lt 0.6d0 then printandlog, $
'WARNING: Torres not applicable (mstar = ' + $
strtrim(mstar_prior,2) + '); ignore at beginning. Otherwise, ' + $
'use MIST, YY, PARSEC, or impose a prior on mstar/rstar',ss.logname
;; add "prior" penalty
chi2 += (alog10(ss.star[i].mstar.value/mstar_prior)/umstar)^2
chi2 += (alog10(ss.star[i].rstar.value/rstar_prior)/urstar)^2
if ss.verbose then begin
mstarpenalty = (alog10(ss.star[i].mstar.value/mstar_prior)/umstar)^2
rstarpenalty = (alog10(ss.star[i].rstar.value/rstar_prior)/urstar)^2
msg = 'Torres penalty: ' + string(mstarpenalty,rstarpenalty,format='(f0.6,x,f0.6)')
printandlog, msg, ss.logname
endif
endif
;; apply MANN penalty to constrain stellar parameters
if ss.mannrad[i] or ss.mannmass[i] then begin
massradius_mann, ss.star[i].appks.value, mstar_prior, rstar_prior, $
feh=ss.star[i].feh.value, $
sigma_rstar=urstar, sigma_mstar=umstar,$
distance=ss.star[i].distance.value
if ss.star[i].mstar.value gt 0.7d0 and ss.verbose then $
printandlog, 'WARNING: MANN not applicable (mstar = ' + $
strtrim(ss.star[i].mstar.value,2) + ' > 0.7); ignore at beginning. Otherwise, ' + $
'use MIST, YY, PARSEC, TORRES, or impose a prior on mstar/rstar',ss.logname
;; add "prior" penalties
if ss.mannrad[i] then begin
rstarpenalty = ((ss.star[i].rstar.value - rstar_prior)/urstar)^2
chi2 += rstarpenalty
if ss.verbose then printandlog, 'Mann rstar penalty: ' + strtrim(rstarpenalty,2),ss.logname
endif
if ss.mannmass[i] then begin
mstarpenalty = ((ss.star[i].mstar.value - mstar_prior)/umstar)^2
chi2 += mstarpenalty
if ss.verbose then printandlog, 'Mann mstar penalty: ' + strtrim(mstarpenalty,2),ss.logname
endif
endif
endfor
;; fit the SED with MIST BC tables
if file_test(ss.mistsedfile) or file_test(ss.fluxfile) or file_test(ss.sedfile) then begin
if keyword_set(psname) then epsname = psname+'.sed.eps'
sedchi2 = 0d0
;; the SED can constrain Teff too precisely
;; (they ignore systematics in interferimetric radii on which they're based).
;; Add a floor for the Teff used everywhere else
tofit = where(ss.star.teffsed.fit,nfit)
teffsed = ss.star.teff.value
if nfit gt 0 then begin
teffsed[tofit] = ss.star[tofit].teffsed.value
sedchi2 += total(((ss.star[tofit].teff.value - ss.star[tofit].teffsed.value)/(ss.teffsedfloor*ss.star[tofit].teffsed.value))^2)
endif
tofit = where(ss.star.fehsed.fit,nfit)
fehsed = ss.star.feh.value
if nfit gt 0 then begin
fehsed[tofit] = ss.star[tofit].fehsed.value
sedchi2 += total(((ss.star[tofit].feh.value - ss.star[tofit].fehsed.value)/ss.fehsedfloor)^2)
endif
;; the SED can constrain FBol too precisely
;; Add a floor for the Fbol used everywhere else
tofit = where(ss.star.rstarsed.fit,nfit)
rstarsed = ss.star.rstar.value
lstarsed = 4d0*!dpi*rstarsed^2*teffsed^4*ss.constants.sigmab/ss.constants.lsun*ss.constants.rsun^2 ;; lsun
if nfit gt 0 then begin
rstarsed[tofit] = ss.star[tofit].rstarsed.value
lstarsed[tofit] = 4d0*!dpi*rstarsed^2*teffsed^4*ss.constants.sigmab/ss.constants.lsun*ss.constants.rsun^2 ;; lsun
sedchi2 += total(((lstarsed[tofit] - ss.star[tofit].lstar.value)/(ss.fbolsedfloor*lstarsed[tofit]))^2)
endif
if file_test(ss.mistsedfile) then begin
;; MIST BC SED
if ss.debug or keyword_set(epsname) then begin
atmospheres = dblarr(ss.nstars,24000)
wavelength = findgen(24000)/1000+0.1
readcol,filepath('extinction_law.ascii',root_dir=getenv('EXOFAST_PATH'),subdir='sed'),klam,kkap,/silent
kapv = interpol(kkap,klam,0.55)
kapp1 = interpol(kkap,klam,wavelength)
for i=0L, ss.nstars-1 do begin
exofast_interp_model3d, teffsed[i], ss.star[i].logg.value, $
fehsed[i], junk, lamflam1temp, $
alpha=ss.star[i].alpha.value,/next
lamflam1=lamflam1temp*rstarsed[i]^2*ss.constants.rsun^2/ss.star[i].distance.value^2/ss.constants.pc^2
taul1 = kapp1/kapv/1.086*ss.star[i].av.value
extinct1 = exp(-taul1)
atmospheres[i,*] = lamflam1*extinct1
atmo_file = file_dirname(epsname) + path_sep() + 'modelfiles' + path_sep() + file_basename(epsname,'.sed.eps') + '.atmosphere.' + string(i,format='(i03)') + '.txt'
exofast_forprint, wavelength, atmospheres[i,*],textout=atmo_file,/nocomment
endfor
endif
sedchi2 += mistmultised(teffsed, ss.star.logg.value,fehsed, $
ss.star.av.value, $
ss.star.distance.value, lstarsed, $
ss.star[0].errscale.value, $
ss.mistsedfile, debug=ss.debug, psname=epsname,$
atmospheres=atmospheres, wavelength=wavelength,$
range=ss.sedrange)
endif else if file_test(ss.sedfile) then begin
sedchi2 += exofast_multised(teffsed, ss.star.logg.value,fehsed, $
ss.star.av.value, $
ss.star.distance.value, lstarsed, $
ss.star[0].errscale.value, $
ss.sedfile, rstar=ss.star.rstarsed.value,$
debug=ss.debug, psname=epsname,$
range=ss.sedrange,specphotpath=ss.specphotpath, $
sperrscale=ss.specphot.sperrscale.value,$
spzeropoint=ss.specphot.spzeropoint.value)
endif else begin
;; Keivan Stassun's SED
junk = exofast_sed(ss.star.fluxfile, teffsed, $
rstarsed,$
ss.star.av.value, ss.star.distance.value, $
logg=ss.star.logg.value,met=fehsed,$
alpha=ss.star.alpha.value,verbose=ss.verbose, $
f0=f, fp0=fp, ep0=ep, psname=epsname, $
pc=ss.constants.pc, rsun=ss.constants.rsun, $
logname=logname, debug=ss.debug, oned=ss.oned)
if ~finite(junk) then begin
if ss.debug or ss.verbose then printandlog, 'sed is bad', ss.logname
return, !values.d_infinity
endif
sedchi2 += exofast_like(f-fp,0d0,ss.star.errscale.value*ep,/chi2)
endelse
;; do some error checking
if ~finite(sedchi2) then begin
if ss.debug or ss.verbose then printandlog, 'sed is bad', ss.logname
return, !values.d_infinity
endif
chi2 += sedchi2
if ss.verbose then $
printandlog, 'SED penalty = ' + strtrim(sedchi2,2), ss.logname
endif
;; Apply Chen & Kipping Mass-Radius relation
;; http://adsabs.harvard.edu/abs/2017ApJ...834...17C
for j=0, ss.nplanets-1 do begin
if ss.planet[j].chen then begin
;; negative radii are allowed to assess the significance of the
;; transit depth. That breaks these relations, so exclude them here
if ss.planet[j].rpearth.value le 0d0 then begin
if ss.debug or ss.verbose then printandlog, 'rpearth is bad', ss.logname
return, !values.d_infinity
endif
rp = massradius_chen(ss.planet[j].mpearth.value > 1d-10,rperr=rperr)
;; add a chi2 penalty for deviation from the mass-radius relation
;; if the radius is well-constrained (by transit depth), it
;; becomes an implicit constraint on mass. If the mass is well
;; constrained (by RV), it is an explicit constraint on
;; radius
chi2 += ((rp - ss.planet[j].rpearth.value)/rperr)^2
if ss.verbose then printandlog, 'chen penalty = ' + strtrim(((rp - ss.planet[j].rpearth.value)/rperr)^2,2),ss.logname
endif
endfor
;; RV model (non-interacting planets)
for j=0, ss.ntel-1 do begin
rv = *(ss.telescope[j].rvptrs)