-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnestkit.py
1267 lines (1096 loc) · 49.5 KB
/
nestkit.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
#!/usr/bin/env python
""" Module of bits to plug into Brewster """
from __future__ import print_function
import math
import time
import gc
import numpy as np
import scipy as sp
import pickle
import forwardmodel
import cloudnest
import TPmod
import settings
from scipy import interpolate
from scipy.interpolate import InterpolatedUnivariateSpline
from scipy.interpolate import interp1d
from astropy.convolution import convolve, convolve_fft
from astropy.convolution import Gaussian1DKernel
from bensconv import prism_non_uniform
from bensconv import conv_uniform_R
from bensconv import conv_uniform_FWHM
__author__ = "Ben Burningham"
__copyright__ = "Copyright 2015 - Ben Burningham"
__credits__ = ["Ben Burningham"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Ben Burningham"
__email__ = "[email protected]"
__status__ = "Development"
def priormap(theta):
"""This function translates sample points in the n-dimensional hypercube from PyMultiNest into a set of parameters
for lnlike (and with that the forward model). By default the priors are uniform, so the prior-map carries out a
relatively simple task in translating the values from the sampler’s live points that lie between 0 and 1.
For example, the sample points for uniform-with-altitude gas volume mixing ratios must simply be multiplied by -12
to translate them into log10(gas-fraction) values within our uniform prior range -12 to 0. Similar translations
are carried out for all other parameters. If you wish to add a non-uniform prior, this is where you should do it.
Be careful not to mess up the counting that keeps track of unpacking the 1D state vector."""
gases_myP,chemeq,dist,dist_err,cloudtype, do_clouds,gasnum,gaslist,cloudnum,inlinetemps,coarsePress,press,inwavenum,linelist,cia,ciatemps,use_disort,fwhm,obspec,proftype,do_fudge,prof,do_bff,bff_raw,ceTgrid,metscale,coscale = settings.runargs
phi = np.zeros_like(theta)
ndim = phi.size
# here are some priors you may want to edit:
max_mass = 80. # jupiters
min_mass = 1.0 # jupiters
min_rad = 0.5 # jupiters
max_rad = 2.5 # jupiters
# first the gases
if (chemeq != 0):
phi[0] = (theta[0] * (metscale[-1] - metscale[0])) + metscale[0]
phi[1] = (theta[1] * (coscale[-1] - coscale[0])) + coscale[0]
ng = 2
else:
if (gasnum[gasnum.size-1] == 22):
ng = gasnum.size - 1
rem = 1.0
for i in range(0, ng):
phi[i] = np.log10(rem) - (theta[i] * 12.)
rem = rem - (10**phi[i])
elif (gasnum[gasnum.size-1] == 24):
ng = gasnum.size - 2
rem = 1.0
for i in range(0, ng):
phi[i] = np.log10(rem) - (theta[i] * 12.)
rem = rem - (10**phi[i])
else:
ng = gasnum.size
rem = 1.0
for i in range(0, ng):
phi[i] = np.log10(rem) - (theta[i] * 12.)
rem = rem - (10**phi[i])
# this is a simple uniform prior on mass
# we want to use the radius, to set a mass prior.
# this will correlate these parameters??? Yes. which is fine.
phi[ng] = (theta[ng] * (max_mass - min_mass)) + min_mass
# this is if we want log g prior: phi[ng] = theta[ng] * 5.5
# now we retrieve radius in R_jup
R_j = ((max_rad - min_rad)*theta[ng+1]) + min_rad
phi[ng+1] = R_j
# need to deal with options for multi instrument setups now
if (fwhm < 0.0):
if (fwhm == -1 or fwhm == -3 or fwhm == -4 or fwhm == -8):
s1 = np.where(obspec[0,:] < 2.5)
s2 = np.where(np.logical_and(obspec[0,:] > 2.5,obspec[0,:] < 5.0))
s3 = np.where(obspec[0,:] > 5.)
# scale parameters here - generous factor 2 either side??
phi[ng+2] = (theta[ng+2] * 1.5) + 0.5
phi[ng+3] = (theta[ng+3] * 1.5) + 0.5
# now dlam
phi[ng+4] = (theta[ng+4] * 0.02) - 0.01
if (do_fudge == 1):
# These are tolerances for each band due to difference SNRs
minerr_s1 =np.log10((0.01 * np.min(obspec[2,s1]))**2.)
maxerr_s1 =np.log10((100.*np.max(obspec[2,s1]))**2.)
phi[ng+5] = (theta[ng+5] * (maxerr_s1 - minerr_s1)) + minerr_s1
minerr_s2 =np.log10((0.01 * np.min(obspec[2,s2]))**2.)
maxerr_s2 =np.log10((100.*np.max(obspec[2,s2]))**2.)
phi[ng+6] = (theta[ng+6] * (maxerr_s2 - minerr_s2)) + minerr_s2
minerr_s3 =np.log10((0.01 * np.min(obspec[2,s3]))**2.)
maxerr_s3 = np.log10((100.*np.max(obspec[2,s3]))**2.)
phi[ng+7] = (theta[ng+7] * (maxerr_s3 - minerr_s3)) + minerr_s3
pc = ng+8
else:
pc = ng + 5
elif (fwhm == -2):
s1 = np.where(obspec[0,:] < 2.5)
s3 = np.where(obspec[0,:] > 5.)
# scale parameter
phi[ng+2] = (theta[ng+2] * 1.5) + 0.5
# dlam now:
phi[ng+3] = (theta[ng+3] * 0.02) - 0.01
if (do_fudge == 1):
# These are tolerances for each band due to difference SNR
minerr_s1 = np.log10((0.01 * np.min(obspec[2,s1]))**2.)
maxerr_s1 = np.log10((100.*np.max(obspec[2,s1]))**2.)
phi[ng+4] = (theta[ng+4] * (maxerr_s1 - minerr_s1)) + minerr_s1
minerr_s3 = np.log10((0.01 * np.min(obspec[2,s3]))**2.)
maxerr_s3 = np.log10((100.*np.max(obspec[2,s3]))**2.)
phi[ng+5] = (theta[ng+5] * (maxerr_s3 - minerr_s3)) + minerr_s3
pc = ng+6
else:
pc = ng + 4
elif (fwhm == -6):
##Geballe NIR CGS4 data
s1 = np.where(obspec[0,:] < 1.585)
s3 = np.where(obspec[0,:] > 1.585)
#not including relative scale factor since data is calibrated to the same photometry
#dlam:
phi[ng+2] = (theta[ng+2]*0.02)-0.01
#Tolerance parameter (only one):
if (do_fudge==1):
minerr = np.log10((0.01 * np.min(obspec[2,:]))**2.)
maxerr = np.log10((100.*np.max(obspec[2,:]))**2.)
phi[ng+3] = (theta[ng+3] * (maxerr - minerr)) + minerr
pc = ng+4
else:
pc=ng+3
elif (fwhm == -7): #Geballe NIR + NIRC + CGS4 MIR
s1 = np.where(obspec[0, :] < 1.585)
s2 = np.where(obspec[0, :] > 1.585)
s3 = np.where(np.logical_and(obspec[0, :] > 2.52, obspec[0, :] < 4.2)) #NIRC
s4 = np.where(obspec[0, :] > 4.2) #CGS4
# scale parameter
phi[ng + 2] = (theta[ng + 2] * 1.5) + 0.5
phi[ng + 3] = (theta[ng + 3] * 1.5) + 0.5
#dlam
phi[ng + 4] = (theta[ng + 4] * 0.02) - 0.01
if (do_fudge == 1):
# These are tolerances for each band due to difference SNRs
minerr_s1 =np.log10((0.01 * np.min(obspec[2,s1]))**2.)
maxerr_s1 =np.log10((100.*np.max(obspec[2,s1]))**2.)
phi[ng+5] = (theta[ng+5] * (maxerr_s1 - minerr_s1)) + minerr_s1
minerr_s2 =np.log10((0.01 * np.min(obspec[2,s2]))**2.)
maxerr_s2 =np.log10((100.*np.max(obspec[2,s2]))**2.)
phi[ng+6] = (theta[ng+6] * (maxerr_s2 - minerr_s2)) + minerr_s2
minerr_s3 =np.log10((0.01 * np.min(obspec[2,s3]))**2.)
maxerr_s3 = np.log10((100.*np.max(obspec[2,s3]))**2.)
phi[ng+7] = (theta[ng+7] * (maxerr_s3 - minerr_s3)) + minerr_s3
pc = ng+8
else:
pc = ng + 5
else:
# this just copes with normal, single instrument data
# so do dlam next
phi[ng+2] = (theta[ng+2] * 0.02) - 0.01
# now fudge
if (do_fudge == 1):
# logf here
minerr =np.log10((0.01 * np.min(obspec[2,:]))**2.)
maxerr = np.log10((100.*np.max(obspec[2,:]))**2.)
phi[ng+3] = (theta[ng+3] * (maxerr - minerr)) + minerr
pc = ng + 4
else:
pc = ng + 3
npatches = do_clouds.size
# only really ready for 2 patches here
if (npatches > 1):
phi[pc] = theta[pc]
pc = pc + 1
if (cloudtype.size > cloudtype.shape[1]):
nclouds = cloudtype.shape[1]
else:
nclouds = cloudtype.size
nc = 0
# use correct unpack method depending on clouds situation
stop_cloud = 0
for patch in range(0,npatches):
# only set up for two patches: cloud & clear, or slab + deck, deck
# so we stop reading cloud parameters after first pass through here.
if stop_cloud == 1:
break
elif (do_clouds[patch] != 0):
for cloud in range(0,nclouds):
if (cloudtype[patch,cloud] == 1):
if (cloudnum[patch,cloud] == 89):
# cloud tau
phi[pc+nc] = theta[pc+nc]*100.
#cloud base
phi[pc+nc+1] = \
(theta[pc+nc+1]*\
(np.log10(press[-1]) - np.log10(press[0]))) \
+ np.log10(press[0])
# cloud height
phi[pc+nc+2] = \
theta[pc+nc+2] * (phi[pc+nc+1] \
- np.log10(press[0]))
# power law
phi[pc+nc+3] = (theta[pc+nc+3] * 20.) - 10.
nc = nc + 4
stop_cloud == 1
elif (cloudnum[patch,cloud] == 99):
# cloud tau
phi[pc+nc] = theta[pc+nc]*100.
#cloud base
phi[pc+nc+1] = \
(theta[pc+nc+1] *(np.log10(press[-1]) \
- np.log10(press[0]))) \
+ np.log10(press[0])
# cloud height
phi[pc+nc+2] = theta[pc+nc+2] *\
(phi[pc+nc+1] - np.log10(press[0]))
nc = nc + 3
stop_cloud == 1
elif (cloudnum[patch,cloud] < 80):
# cloud tau
phi[pc+nc] = theta[pc+nc]*100.
#cloud base
phi[pc+nc+1] = \
(theta[pc+nc+1] *(np.log10(press[-1]) \
- np.log10(press[0]))) \
+ np.log10(press[0])
# cloud height
phi[pc+nc+2] = theta[pc+nc+2] * \
(phi[pc+nc+1] - np.log10(press[0]))
# particle effective radius
phi[pc+nc+3] = (theta[pc+nc+3] * 6.) - 3.
# particle spread
phi[pc+nc+4] = theta[pc+nc+4]
nc = nc + 5
stop_cloud == 1
elif (cloudtype[patch,cloud] == 2):
if (cloudnum[patch,cloud] == 89):
#cloud top
phi[pc+nc] = \
(theta[pc+nc] *(np.log10(press[-1]) \
- np.log10(press[0]))) \
+ np.log10(press[0])
# cloud height
phi[pc+nc+1] = theta[pc+nc+1] * 7.
# power law
phi[pc+nc+2] = (theta[pc+nc+2] * 20.) - 10.
nc = nc + 3
stop_cloud == 1
elif (cloudnum[patch,cloud] == 99):
#cloud top
phi[pc+nc] = \
(theta[pc+nc] *(np.log10(press[-1]) \
- np.log10(press[0])))\
+ np.log10(press[0])
# cloud height
phi[pc+nc+1] = theta[pc+nc+1] * 7.
nc = nc + 2
stop_cloud == 1
elif (cloudnum[patch,cloud] < 80):
#cloud base
phi[pc+nc] = \
(theta[pc+nc] *(np.log10(press[-1]) \
- np.log10(press[0])))\
+ np.log10(press[0])
# cloud height
phi[pc+nc+1] = theta[pc+nc+1] * 7.
# particle effective radius
phi[pc+nc+2] = (theta[pc+nc+2] * 6.) - 3.
# particle spread
phi[pc+nc+3] = theta[pc+nc+3]
nc = nc + 4
stop_cloud == 1
# types 3 and 4 to be added
elif (cloudtype[patch,cloud] > 2):
print("cloudtypes 3 and 4 not yet implemented here")
sys.exit()
if (proftype == 1):
# This is not Mike Line's profile. This is a simple 5 point spline.
# We get the bottom and top of the atmosphere,
# then we get a mid point
# then two quartile points
# These define T at define pressure points
# This does not allow reversals
knots = len(coarsePress)
phi[ndim-1] = theta[ndim-1] * 5000.
# now bottom up for all the knots
for i in range(2,knots+1):
phi[ndim - i] = theta[ndim - i] * phi[(ndim - i) + 1]
return phi
if (proftype == 2):
# a1
phi[pc+nc] = 0.25 + (theta[pc+nc]*0.25)
# a2
phi[pc+nc+1] = 0.1 + (theta[pc+1+nc] * 0.1)
#P1
phi[pc+nc+2] = (theta[pc+2+nc]* \
(np.log10(press[-1]) - np.log10(press[0]))) + np.log10(press[0])
#P3
#P3 must be greater than P1
phi[pc+nc+3] = (theta[pc+nc+3] * \
(np.log10(press[-1]) - phi[pc+nc+2])) + phi[pc+nc+2]
#T3
phi[pc+nc+4] = (theta[pc+4+nc] * 3000.) + 1500.0
return phi
elif (proftype == 3):
# a1
phi[pc+nc] = 0.25 + (theta[pc+nc]*0.25)
# a2
phi[pc+nc+1] = 0.1 * (theta[pc+1+nc] * 0.1)
#P1
phi[pc+nc+2] = (theta[pc+2+nc]* (np.log10(press[-1]) - np.log10(press[0]))) + np.log10(press[0])
#P2 must be greater than or equal P1
phi[pc+nc+3] = (theta[pc+nc+3] * (np.log10(press[-1]) - phi[pc+nc+2])) + phi[pc+nc+2]
#P3 must be greater than or equal P2
phi[pc+nc+4] = (theta[pc+nc+4] * (np.log10(press[-1]) - phi[pc+nc+2])) + phi[pc+nc+3]
#T3
phi[pc+nc+5] = (theta[pc+nc5+5] * 3000.) + 1500.
return phi
elif (proftype == 6):
# This is not Mike Line's profile. This is a simple n point spline.
# each layer is completely free, does not have to be declining in T.
knots = len(coarsePress)
phi[ndim-knots:] = theta[ndim-knots:] * 4000.
return phi
elif (proftype == 7):
# Tint - prior following Molliere+2020
phi[pc+nc] = 300 + (theta[pc+nc] * 2000)
# alpha, between 1 and 2
phi[pc+nc+1] = theta[pc+nc+1] + 1.
# delta
plen = np.log10(press[-1]) - np.log10(press[0])
logPphot = np.log10(press[0]+(theta[pc+nc+2]*plen))
phi[pc+nc+2] = 10**(-phi[pc+nc+1]*logPphot)
# get Tconnect from Tint get prior range following Molliere+2020
Tint = phi[pc+nc]
Tconnect = (((3/4) * Tint**4) * ((2/3) + (0.1)))**(1/4)
# now get T3
phi[pc+nc+5] = 10. + (theta[pc+nc+5] * Tconnect)
# T2
phi[pc+nc+4] = 10. + (theta[pc+nc+4] * phi[pc+nc+5])
# T1
phi[pc+nc+3] = 10.+ (theta[pc+nc+3] * phi[pc+nc+4])
return phi
elif (proftype == 9):
return phi
def lnlike(theta):
gases_myP,chemeq,dist,dist_err,cloudtype, do_clouds,gasnum,gaslist,cloudnum,inlinetemps,coarsePress,press,inwavenum,linelist,cia,ciatemps,use_disort,fwhm,obspec,proftype,do_fudge,prof,do_bff,bff_raw,ceTgrid,metscale,coscale = settings.runargs
# get the spectrum
# for MCMC runs we don't want diagnostics
gnostics = 0
shiftspec, photspec,tauspec,cfunc = modelspec(theta)
# Check if CE or VMR methods
if chemeq == 0:
if (gasnum[gasnum.size-1] == 22):
ng = gasnum.size - 1
elif (gasnum[gasnum.size-1] == 24):
ng = gasnum.size -2
else:
ng = gasnum.size
invmr = theta[0:ng]
else:
ng = 2
# Get the scaling factors for the spectra. What is the FWHM? Negative number: preset combination of instruments
if (fwhm < 0.0):
if (fwhm == -1 or fwhm == -3 or fwhm == -4 or fwhm == -7 or fwhm == -8):
scale1 = theta[ng+2]
scale2 = theta[ng+3]
if (do_fudge == 1):
logf = theta[ng+5:ng+8]
nb = 8
else:
nb = 5
elif (fwhm == -2):
scale1 = theta[ng+2]
if (do_fudge == 1):
logf = theta[ng+4:ng+6]
nb = 6
else:
nb = 4
elif (fwhm == -6):
if (do_fudge == 1):
logf = theta[ng+3]
nb = 4
else:
nb = 3
else:
if (do_fudge == 1):
logf = theta[ng+3]
nb = 4
else:
nb = 3
modspec = np.array([shiftspec[0,::-1],shiftspec[1,::-1]])
# If we've set a value for FWHM that we're using...
if (fwhm > 0.00 and fwhm < 1.00):
# this is a uniform FWHM in microns
spec = conv_uniform_FWHM(obspec,modspec,fwhm)
if (do_fudge == 1):
s2=obspec[2,:]**2 + 10.**logf
else:
s2 = obspec[2,:]**2
lnLik=-0.5*np.sum((((obspec[1,:] - spec[:])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm > 10.00):
# this is a uniform resolving power R.
Res = fwhm
spec = conv_uniform_R(obspec,modspec,Res)
if (do_fudge == 1):
s2=obspec[2,:]**2 + 10.**logf
else:
s2 = obspec[2,:]**2
lnLik=-0.5*np.sum((((obspec[1,:] - spec[:])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm == 0.0):
# Use convolution for Spex
spec = prism_non_uniform(obspec,modspec,3.3)
if (do_fudge == 1):
s2=obspec[2,::3]**2 + 10.**logf
else:
s2 = obspec[2,::3]**2
lnLik=-0.5*np.sum((((obspec[1,::3] - spec[::3])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm == 1.0):
# Use convolution for JWST-NIRSpec PRISM
spec = prism_non_uniform(obspec,modspec,2.2)
if (do_fudge == 1):
s2=obspec[2,::2]**2 + 10.**logf
else:
s2 = obspec[2,::2]**2
lnLik=-0.5*np.sum((((obspec[1,::2] - spec[::2])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm == 2.0):
# combo of JWST-NIRSpec PRISM + G395H grism
# single scaling & single fudge factor
spec = np.zeros_like(obspec[0,:])
# first convolution for JWST-NIRSpec PRISM
or1 = np.where(obspec[0,:] < 2.9)
spec[or1] = prism_non_uniform(obspec[:,or1],modspec,2.2)
# now 1st grism bit
dL = 0.0015
or2 = np.where(np.logical_and(obspec[0,:] > 2.9,obspec[0,:] < 3.69))
spec[or2] = conv_uniform_FWHM(obspec[:,or2],modspec,dL)
# a bit more prism
or3 = np.where(np.logical_and(obspec[0,:] > 3.69,obspec[0,:] < 3.785))
spec[or3] = prism_non_uniform(obspec[:,or3],modspec,2.2)
# 2nd bit of grism
or4 = np.where(np.logical_and(obspec[0,:] > 3.785,obspec[0,:] < 5.14))
spec[or4] = conv_uniform_FWHM(obspec[:,or4],modspec,dL)
# the rest of prism
or5 = np.where(obspec[0,:] > 5.14)
spec[or5] = prism_non_uniform(obspec[:,or5],modspec,2.2)
if (do_fudge == 1):
s2=obspec[2,:]**2 + 10.**logf
else:
s2 = obspec[2,:]**2
lnLik=-0.5*np.sum((((obspec[1,:] - spec[:])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm == 3.0):
# JWST NIRSpec G395H data with 2.2 pixels per resolving element
# Use convolution for Spex
spec = prism_non_uniform(obspec,modspec,2.2)
if (do_fudge == 1):
s2=obspec[2,::3]**2 + 10.**logf
else:
s2 = obspec[2,::3]**2
lnLik=-0.5*np.sum((((obspec[1,::3] - spec[::3])**2) / s2) + np.log(2.*np.pi*s2))
elif (fwhm < 0.0):
lnLik = 0.0
# This is for multi-instrument cases
# -1: spex + akari + IRS
# -2: spex + IRS
# -3: spex + Lband + IRS
# -7: cgs4 nir + nirc l band + cgs4 m band
if (fwhm == -1):
# Spex
mr1 = np.where(shiftspec[0,:] < 2.5)
or1 = np.where(obspec[0,:] < 2.5)
wno = 1e4 / shiftspec[0,mr1]
spec1 = prism_non_uniform(obspec[:,or1],modspec,3.3)
# AKARI IRC
# dispersion constant across order 0.0097um
# R = 100 at 3.6um for emission lines
# dL ~constant at 3.6 / 120
dL = 0.03
#mr2 = np.where(np.logical_and(modspec[0,:] > 2.5,modspec[0,:] < 5.0))
or2 = np.where(np.logical_and(obspec[0,:] > 2.5,obspec[0,:] < 5.0))
spec2 = scale1 * conv_uniform_FWHM(obspec[:,or2],modspec,dL)
# Spitzer IRS
# R roughly constant within orders, and orders both appear to
# have R ~ 100
R = 100.0
#mr3 = np.where(modspec[0,:] > 5.0)
or3 = np.where(obspec[0,:] > 5.0)
spec3 = scale2 * conv_uniform_R(obspec[:,or3],modspec,R)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf[0]
s2 = obspec[2,or2]**2 + 10.**logf[1]
s3 = obspec[2,or3]**2 + 10.**logf[2]
else:
s1 = obspec[2,or1]**2
s2 = obspec[2,or2]**2
s3 = obspec[2,or3]**2
lnLik1=-0.5*np.sum((((obspec[1,or1] - spec1)**2) / s1) + np.log(2.*np.pi*s1))
lnLik2=-0.5*np.sum((((obspec[1,or2] - spec2)**2) / s2) + np.log(2.*np.pi*s2))
lnLik3=-0.5*np.sum((((obspec[1,or3] - spec3)**2) / s3) + np.log(2.*np.pi*s3))
lnLik = lnLik1 + lnLik2 + lnLik3
elif (fwhm == -2):
# This is just spex + IRS
# Spex
#mr1 = np.where(shiftspec[0,:] < 2.5)
or1 = np.where(obspec[0,:] < 2.5)
spec1 = prism_non_uniform(obspec[:,or1],modspec,3.3)
# Spitzer IRS
# R roughly constant within orders, and orders both appear to
# have R ~ 100
R = 100.0
#mr3 = np.where(modspec[0,:] > 5.0)
or3 = np.where(obspec[0,:] > 5.0)
spec3 = scale1 * conv_uniform_R(obspec[:,or3],modspec,R)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf[0]
s3 = obspec[2,or3]**2 + 10.**logf[1]
else:
s1 = obspec[2,or1]**2
s3 = obspec[2,or3]**2
lnLik1=-0.5*np.sum((((obspec[1,or1] - spec1)**2) / s1) + np.log(2.*np.pi*s1))
lnLik3=-0.5*np.sum((((obspec[1,or3] - spec3)**2) / s3) + np.log(2.*np.pi*s3))
lnLik = lnLik1 + lnLik3
elif (fwhm == -3):
# This is spex + Mike Cushing's L band R = 425 + IRS
# Spex
mr1 = np.where(shiftspec[0,:] < 2.5)
or1 = np.where(obspec[0,:] < 2.5)
wno = 1e4 / shiftspec[0,mr1]
spec1 = prism_non_uniform(obspec[:,or1],modspec, 3.3)
modspec = np.array([shiftspec[0,::-1],shiftspec[1,::-1]])
# Mike Cushing supplied L band R = 425
# dispersion constant across order 0.0097um
# Res = 425
Res = 425
#mr2 = np.where(np.logical_and(modspec[0,:] > 2.5,modspec[0,:] < 5.0))
or2 = np.where(np.logical_and(obspec[0,:] > 2.5,obspec[0,:] < 5.0))
spec2 = scale1 * conv_uniform_R(obspec[:,or2],modspec,Res)
# Spitzer IRS
# R roughly constant within orders, and orders both appear to
# have Res ~ 100
Res = 100.0
#mr3 = np.where(modspec[0,:] > 5.0)
or3 = np.where(obspec[0,:] > 5.0)
spec3 = scale2 * conv_uniform_R(obspec[:,or3],modspec,Res)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf[0]
s2 = obspec[2,or2]**2 + 10.**logf[1]
s3 = obspec[2,or3]**2 + 10.**logf[2]
else:
s1 = obspec[2,or1]**2
s2 = obspec[2,or2]**2
s3 = obspec[2,or3]**2
lnLik1=-0.5*np.sum((((obspec[1,or1] - spec1)**2) / s1) + np.log(2.*np.pi*s1))
lnLik2=-0.5*np.sum((((obspec[1,or2] - spec2)**2) / s2) + np.log(2.*np.pi*s2))
lnLik3=-0.5*np.sum((((obspec[1,or3] - spec3)**2) / s3) + np.log(2.*np.pi*s3))
lnLik = lnLik1 + lnLik2 + lnLik3
elif (fwhm == -4):
# This is spex + GNIRS L band R = 600 + IRS
# Spex
mr1 = np.where(shiftspec[0,:] < 2.5)
or1 = np.where(obspec[0,:] < 2.5)
wno = 1e4 / shiftspec[0,mr1]
spec1 = prism_non_uniform(obspec[:,or1],modspec,3.3)
modspec = np.array([shiftspec[0,::-1],shiftspec[1,::-1]])
# Katelyn Allers spectrum of GNIRS R = 600
# R = 600 @ 3.5um linearly increading across order
# i.e. FWHM - 0.005833
dL = 0.005833
#dL = 0.0097
or2 = np.where(np.logical_and(obspec[0,:] > 2.5,obspec[0,:] < 5.0))
spec2 = scale1 * conv_uniform_FWHM(obspec[:,or2],modspec,dL)
# Spitzer IRS
# R roughly constant within orders, and orders both appear to
# have R ~ 100
R = 100.0
#mr3 = np.where(modspec[0,:] > 5.0)
or3 = np.where(obspec[0,:] > 5.0)
spec3 = scale2 * conv_uniform_R(obspec[:,or3],modspec,R)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf[0]
s2 = obspec[2,or2]**2 + 10.**logf[1]
s3 = obspec[2,or3]**2 + 10.**logf[2]
else:
s1 = obspec[2,or1]**2
s2 = obspec[2,or2]**2
s3 = obspec[2,or3]**2
lnLik1=-0.5*np.sum((((obspec[1,or1] - spec1)**2) / s1) + np.log(2.*np.pi*s1))
lnLik2=-0.5*np.sum((((obspec[1,or2] - spec2)**2) / s2) + np.log(2.*np.pi*s2))
lnLik3=-0.5*np.sum((((obspec[1,or3] - spec3)**2) / s3) + np.log(2.*np.pi*s3))
lnLik = lnLik1 + lnLik2 + lnLik3
elif (fwhm == -6):
# This is UKIRT orders 1 and 2 based on Geballe 1996 cuts
# Second Order
# R ~ 780 x Lambda (linear increase across order)
# Order 2 (0.95 - 1.40 um)
# FWHM ~ 1.175/780 = 0.001506
dL1 = 0.001506
or1 = np.where(obspec[0,:] < 1.585)
spec1 = conv_uniform_FWHM(obspec[:,or1],modspec,dL1)
# First Order
# R ~ 390 x Lambda (linear increase across order)
# Order 1 (1.30 - 5.50 um)
# FWHM ~ 3.4/390 = 0.008717
dL2 = 0.008717
or2 = np.where(obspec[0,:] > 1.585)
spec2 = conv_uniform_FWHM(obspec[:,or2],modspec,dL2)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf
s3 = obspec[2,or2]**2 + 10.**logf
else:
s1 = obspec[2,or1]**2
s3 = obspec[2,or2]**2
lnLik1=-0.5*np.sum((((obspec[1,or1[0][::7]] - spec1[::7])**2) / s1[0][::7]) + np.log(2.*np.pi*s1[0][::7]))
lnLik3=-0.5*np.sum((((obspec[1,or2[0][::3]] - spec2[::3])**2) / s3[0][::3]) + np.log(2.*np.pi*s3[0][::3]))
lnLik = lnLik1 + lnLik3
elif (fwhm == -7):
#This is CGS4 NIR + NIRC Lband * CGS4 Mband
# CGS4 Second order R = 780xLambda
dL1 = 0.001506
or1 = np.where(obspec[0, :] < 1.585)
spec1 = conv_uniform_FWHM(obspec[:, or1], modspec, dL1)
# CGS4 First order R = 390xLambda
dL2 = 0.008717
or2 = np.where(np.logical_and(obspec[0, :] > 1.585, obspec[0, :] < 2.52))
spec2 = conv_uniform_FWHM(obspec[:, or2], modspec, dL2)
# Oppenheimer 1998 NIRC L band spectrum
###EDIT### Central wavelength @ 3.492 with FWHM=1.490 for lw band
# Using R=164
#dL3 = 0.0213
R=164.0
or3 = np.where(np.logical_and(obspec[0, :] > 2.52, obspec[0, :] < 4.15))
spec3 = scale1 * conv_uniform_R(obspec[:, or3], modspec, R)
# CGS4 M band
# Order 1 using 1".2 slit, 75 line/mm grating, 150 mm focal length camera
###EDIT### R=400xLambda
dL4 = 0.0085
or4 = np.where(obspec[0, :] > 4.15)
spec4 = scale2 * conv_uniform_FWHM(obspec[:, or4], modspec, dL4)
if (do_fudge == 1):
s1 = obspec[2, or1] ** 2 + 10. ** logf[0]
s2 = obspec[2, or2] ** 2 + 10. ** logf[0]
s3 = obspec[2, or3] ** 2 + 10. ** logf[1]
s4 = obspec[2, or4] ** 2 + 10. ** logf[2]
else:
s1 = obspec[2, or1] ** 2
s2 = obspec[2, or2] ** 2
s3 = obspec[2, or3] ** 2
s4 = obspec[2, or4] ** 2
lnLik1 = -0.5 * np.sum((((obspec[1, or1[0][::7]] - spec1[::7]) ** 2) / s1[0][::7]) + np.log(2. * np.pi * s1[0][::7]))
lnLik2 = -0.5 * np.sum((((obspec[1, or2[0][::3]] - spec2[::3]) ** 2) / s2[0][::3]) + np.log(2. * np.pi * s2[0][::3]))
lnLik3 = -0.5 * np.sum((((obspec[1, or3] - spec3) ** 2) / s3) + np.log(2. * np.pi * s3))
lnLik4 = -0.5 * np.sum((((obspec[1, or4] - spec4) ** 2) / s4) + np.log(2. * np.pi * s4))
lnLik = lnLik1 + lnLik2 + lnLik3 + lnLik4
elif (fwhm == -8):
# This is spex + JWST G395H R 2700 + IRS
# Spex
mr1 = np.where(shiftspec[0,:] < 2.5)
or1 = np.where(obspec[0,:] < 2.5)
wno = 1e4 / shiftspec[0,mr1]
spec1 = prism_non_uniform(obspec[:,or1],modspec,3.3)
modspec = np.array([shiftspec[0,::-1],shiftspec[1,::-1]])
# JWST G395H
R = 2700
or2 = np.where(np.logical_and(obspec[0,:] > 2.5,obspec[0,:] < 5.2))
spec2 = scale1 * conv_uniform_R(obspec[:,or2],modspec,R)
# Spitzer IRS
# R roughly constant within orders, and orders both appear to
# have R ~ 100
R = 100.0
#mr3 = np.where(modspec[0,:] > 5.0)
or3 = np.where(obspec[0,:] > 5.2)
spec3 = scale2 * conv_uniform_R(obspec[:,or3],modspec,R)
if (do_fudge == 1):
s1 = obspec[2,or1]**2 + 10.**logf[0]
s2 = obspec[2,or2]**2 + 10.**logf[1]
s3 = obspec[2,or3]**2 + 10.**logf[2]
else:
s1 = obspec[2,or1]**2
s2 = obspec[2,or2]**2
s3 = obspec[2,or3]**2
lnLik1=-0.5*np.sum((((obspec[1,or1] - spec1)**2) / s1) + np.log(2.*np.pi*s1))
lnLik2=-0.5*np.sum((((obspec[1,or2] - spec2)**2) / s2) + np.log(2.*np.pi*s2))
lnLik3=-0.5*np.sum((((obspec[1,or3] - spec3)**2) / s3) + np.log(2.*np.pi*s3))
lnLik = lnLik1 + lnLik2 + lnLik3
if np.isnan(lnLik):
lnLik = -np.inf
return lnLik
def modelspec(theta,args=None, gnostics=0):
if args is None:
gases_myP,chemeq,dist,dist_err,cloudtype, do_clouds,gasnum,gaslist,cloudnum,inlinetemps,coarsePress,press,inwavenum,linelist,cia,ciatemps,use_disort,fwhm,obspec,proftype,do_fudge,prof,do_bff,bff_raw,ceTgrid,metscale,coscale = settings.runargs
else:
gases_myP,chemeq,dist,dist_err,cloudtype, do_clouds,gasnum,gaslist,cloudnum,inlinetemps,coarsePress,press,inwavenum,linelist,cia,ciatemps,use_disort,fwhm,obspec,proftype,do_fudge,prof,do_bff,bff_raw,ceTgrid,metscale,coscale = args
nlayers = press.size
if chemeq == 0:
if (gasnum[gasnum.size-1] == 22):
ng = gasnum.size - 1
elif (gasnum[gasnum.size-1] == 24):
ng = gasnum.size -2
else:
ng = gasnum.size
invmr = theta[0:ng]
else:
mh = theta[0]
co = theta[1]
ng = 2
mfit = interp1d(metscale,gases_myP,axis=0)
gases_myM = mfit(mh)
cfit = interp1d(coscale,gases_myM,axis=0)
invmr = cfit(co)
GM = (6.67E-11 * theta[ng]*1.898e27)
R = theta[ng+1] * 69911e3
logg = np.log10(100.* GM / R**2.)
D = (dist + (np.random.randn()*dist_err)) * 3.086e16
R2D2 = R**2. / D**2.
if (fwhm < 0.0):
if (fwhm == -1 or fwhm == -3 or fwhm == -4 or fwhm == -7 or fwhm == -8):
dlam = theta[ng+4]
if (do_fudge == 1):
logf = theta[ng+5:ng+8]
nb = 8
else:
nb = 5
elif (fwhm == -2):
dlam = theta[ng+3]
if (do_fudge == 1):
logf = theta[ng+4:ng+6]
nb = 6
else:
nb = 4
elif (fwhm == -6):
dlam = theta[ng+2]
if (do_fudge == 1):
logf = theta[ng+3]
nb = 4
else:
nb = 3
else:
dlam = theta[ng+2]
if (do_fudge == 1):
logf = theta[ng+3]
nb = 4
else:
nb = 3
npatches = do_clouds.size
if (npatches > 1):
prat = theta[ng+nb]
pcover = np.array([prat,(1.-prat)])
pc = ng + nb + 1
else:
pc = ng + nb
pcover = 1.0
# use correct unpack method depending on situation
if ((npatches > 1) and np.all(do_clouds != 0)):
cloudparams, nc = cloudnest.unpack_patchy(theta,pc,cloudtype,cloudnum,do_clouds)
else:
cloudparams, nc = cloudnest.unpack_default(theta,pc,cloudtype,cloudnum,do_clouds)
if (proftype < 8 ):
intemp = theta[pc+nc:]
elif (proftype == 9):
intemp = prof
else:
raise ValueError("not valid profile type %proftype" % (char, string))
# set the profile
temp = TPmod.set_prof(proftype,coarsePress,press,intemp)
ngas = gasnum.size
bff = np.zeros([3,nlayers],dtype="float64")
# check if its a fixed VMR or a profile from chem equilibrium
# VMR is log10(VMR) !!!
if chemeq == 1:
# this case is a profile
ng = invmr.shape[2]
ngas = ng - 3
logVMR = np.zeros([ngas,nlayers],dtype='d')
for p in range(0,nlayers):
for g in range(0,ng):
tfit = InterpolatedUnivariateSpline(ceTgrid,invmr[:,p,g])
if (g < 3):
bff[g,p] = tfit(temp[p])
else:
logVMR[g-3,p]= tfit(temp[p])
else:
# This case is fixed VMR
# chemeq = 0
logVMR = np.empty((ngas,nlayers),dtype='d')
alkratio = 16.2 # from Asplund et al (2009)
# now sort Na and K
# get the ngas for forward model (ngas, not ng
if (gasnum[gasnum.size-1] == 22):
ngas = invmr.shape[0] + 1
elif (gasnum[gasnum.size-1] == 24):
ngas = invmr.shape[0] + 2
else:
ngas = invmr.shape[0]
tmpvmr = np.empty(ngas,dtype='d')
if (gasnum[gasnum.size-1] == 22):
tmpvmr[0:(ngas-2)] = invmr[0:(ngas-2)]
tmpvmr[ngas-2] = np.log10(10.**invmr[ngas-2] / (alkratio+1.)) # K
tmpvmr[ngas-1] = np.log10(10.**invmr[ngas-2] * (alkratio / (alkratio+1.))) # Na
elif (gasnum[gasnum.size-1] == 24):
#f values are ratios between Na and (K+Cs) and K and Cs respectively
f1 = 1.348
f2 = 8912.5
tmpvmr[0:(ngas-3)] = invmr[0:(ngas-3)]
tmpvmr[ngas-1] = np.log10(10.**invmr[ngas-3] / ((f1+1)*(f2+1))) # Cs
tmpvmr[ngas-2] = np.log10(10.**invmr[ngas-3] * (f1 /(f1+1)) ) # Na
tmpvmr[ngas-3] = np.log10(10.**invmr[ngas-3] - 10.**tmpvmr[ngas-2] - 10.**tmpvmr[ngas-1]) #K
else:
tmpvmr[0:ngas] = invmr[0:ngas]
for i in range(0,ngas):
logVMR[i,:] = tmpvmr[i]
# now need to translate cloudparams in to cloud profile even
# if do_clouds is zero..
cloudprof,cloudrad,cloudsig = cloudnest.atlas(do_clouds,cloudnum,cloudtype,cloudparams,press)
cloudprof = np.asfortranarray(cloudprof,dtype = 'float64')
cloudrad = np.asfortranarray(cloudrad,dtype = 'float64')
cloudsig = np.asfortranarray(cloudsig,dtype = 'float64')
pcover = np.asfortranarray(pcover,dtype = 'float32')
cloudnum = np.asfortranarray(cloudnum,dtype='i')
do_clouds = np.asfortranarray(do_clouds,dtype = 'i')
# Now get the BFF stuff sorted
if (chemeq == 0 and do_bff == 1):
for gas in range(0,3):
for i in range(0,nlayers):
tfit = InterpolatedUnivariateSpline(ceTgrid,bff_raw[:,i,gas],k=1)
bff[gas,i] = tfit(temp[i])
bff = np.asfortranarray(bff, dtype='float64')
press = np.asfortranarray(press,dtype='float32')
temp = np.asfortranarray(temp,dtype='float64')
logVMR = np.asfortranarray(logVMR,dtype='float64')
# Diagnostics below.
# make_cf = get a contribution function
# clphot = get pressure for cloud_tau = 1.0 as function of wavelength
# ^^ i.e the cloud photosphere
# ophot = get pressures for tau(not cloud) = 1.0 as function of wavelength]
# ^^ i.e. the photosphere due to other (gas phase) opacities)
# Set clphot,ophot and cfunc as we don't need these in the emcee run
if (gnostics == 0):
clphot = 0
ophot = 0
make_cf = 0
else:
clphot = 1
ophot = 1
make_cf = 1
# now we can call the forward model
outspec,tmpclphotspec,tmpophotspec,cf = forwardmodel.marv(temp,logg,R2D2,gasnum,logVMR,pcover,do_clouds,cloudnum,cloudrad,cloudsig,cloudprof,inlinetemps,press,inwavenum,linelist,cia,ciatemps,use_disort,clphot,ophot,make_cf,do_bff,bff)
# Trim to length where it is defined.
nwave = inwavenum.size
trimspec = np.zeros([2,nwave],dtype='d')
trimspec = outspec[:,:nwave]
cloud_phot_press = tmpclphotspec[0:npatches,:nwave].reshape(npatches,nwave)
other_phot_press = tmpophotspec[0:npatches,:nwave].reshape(npatches,nwave)
cfunc = np.zeros([npatches,nwave,nlayers],dtype='d')
cfunc = cf[:npatches,:nwave,:nlayers].reshape(npatches,nwave,nlayers)
# now shift wavelen by delta_lambda
shiftspec = np.empty_like(trimspec)
shiftspec[0,:] = trimspec[0,:] + dlam
shiftspec[1,:] = trimspec[1,:]
return shiftspec, cloud_phot_press,other_phot_press,cfunc
def get_opacities(gaslist,w1,w2,press,xpath='../Linelists',xlist='gaslistR10K.dat',malk=0):
# Now we'll get the opacity files into an array
ngas = len(gaslist)
totgas = 0
gasdata = []
with open(xlist) as fa:
for line_aa in fa.readlines():