-
Notifications
You must be signed in to change notification settings - Fork 2
/
dwarf_redux.py
1124 lines (913 loc) · 40.5 KB
/
dwarf_redux.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
import sys
from mpfit import mpfit
import astropy.io.fits as pyfits
import numpy as np
import matplotlib.pyplot as plt
import emcee
from scipy import stats
import os
from scipy import ndimage
from numpy.polynomial.legendre import legfit, legval
import time
from scipy.interpolate import CubicSpline
from schwimmbad import MultiPool
import warnings
warnings.filterwarnings("ignore")
#################################################
######### SET PARAMETERS TILL LINE 100 ##########
#################################################
# number of mcmc steps. 1000 for official runs, 100 for test runs to save time.
nsam=1000
# snr threshold, any spectra with snr below snr_min will be skipped for RV or EW fit.
snr_min = 2
# normalize the spectra before combining the spectra (default: 1), 0 was used in the past (i.e. normalization done after combine)
normalizeb4combine = 1
# resample spec or not, default: uniform_resample = 0
# in the past, uniform_resample = 1 was used
uniform_resample = 0
# resample step, if uniform_resample = 0, then this number is not used in the program since no resampling will be done.
# used 0.1 A in the past, should use 0.19 A in the future since that is the actual grid if uniform_resample = 1.
# if using step other than 0.19, then the error spectra will be wrong. TODO, rescale the error spectra for a different step size.
resample_step = 0.19
# if doing cubicspline interpolation, then cubic = 1. If linear then cubic = 0
cubic = 1
#save output catalog or not
savedata = 1
#show the combine spectra that is used for the fit
showcombinedspectra = 0
#show plots for how normalization is done
show_normalization = 0
#show the spectra with the best fit RV templates around CaT lines
showrvplot = 0
#save the plot above
savervplot = 1
#show the spectra with the best fit CaT EW
showewplot = 0
#save the plot above
saveewplot = 1
#display the parameters from the EW fit in the terminal (but not saved)
dispara = 0
#do you want to assess the fitting quality manually? if yes, then zquality = 1 and it will ask you to enter 1 or 0 during the run, usually 1 = good fit, 0 = bad fit
zquality = 0
#write the combined and normalized spectra to a txt file (including all wavelength, not just the spectral fitting window)
#saved txt file will be in the same folder as the input fits file, i.e. objdir (see below)
writespec = 0
#directory for output catalog and figure
outputdir = './'
#directory for saved figures
figdir = outputdir+'fig_delve2r1_oct9_2021_spec1d/'
#file name for the output catalog
outputfile = outputdir+'delve2r1_oct9_2021_spec1d.txt'
#input directory for the 1D spectra. it should be a directory and the code will run on all spectra (filename ended with .fits) in this directory
objdir = '../spec_1d/delve2r1_oct9_2021_spec1d/'
#path for the rv template and telluric template
rv_fname = '/Users/tingli/Dropbox/dwarfgalaxy/Magellan/stds/imacs-030817.fits'
telluric_fname = '/Users/tingli/Dropbox/dwarfgalaxy/Magellan/stds/imacs-aband-063016.fits'
# number of pixels to be removed at the edge since the spectra near the edge are sometimes bad (default = 5)
# you may want to decrease this number if a line of interest (e.g. CaT) is near chip gap
# nbuff cannot be zero due to how the code is written, 1 is the minimal.
nbuff = 5
###########################
###### SINGLE MODE #######
#running with one specific spectrum or not, if single = 1, then the file with object_fname_single will run, not the objdir files
#if single = 1 and bhb = 1, then only BHB template will be fit, not the other two templates
#bhb=1 only work when single=1 (i.e. bhb=1 won't work during batch mode above)
single = 0
bhb = 0
object_fname_single = \
'/Users/tingli/Dropbox/dwarfgalaxy/Magellan/Car23_IMACS/1Dspec_new/car2r1_spec1d_n4only/spec1d.car2r1xx.2017239293.fits'
# fit the EW with gaussian + Lorentzian for 0, and only fit with gaussian for 1. (EW' = EW*1.1 if gaussian only)
gaussianonly = 0
##########################
##########################
##############################################
######## END OF PARAMETER SETTING HERE #######
##############################################
# spectra display window (just for plotting)
CaT1min=8480
CaT1max=8520
CaT2min=8520
CaT2max=8565
CaT3min=8640
CaT3max=8680
# spectra fitting window
wlmaskmin = 8450
wlmaskmax = 8685 # was using 8700 in the past but now switch to match with Josh's
# BHB spectra fitting window
wlmaskmin_bhb = 8450
wlmaskmax_bhb = 8900
#speed of light
c = 2.99792458e5
#create the path for storing the figures if not exist
if not os.path.exists(figdir):
os.makedirs(figdir)
def normalize_spec(wl, spec, dspec):
"""
normalize the spectra with a legendre polynomial
"""
idx = (np.isnan(spec))
spec[idx] = 0
dspec[idx] = 1e15
idx = np.isnan(dspec)
dspec[idx] = 1e15
idx = spec < 0
dspec[idx] = 1e15
idx = dspec < 0
dspec[idx] = 1e15
snr = np.median(spec/dspec)
thlow = 0.8
thhigh = 1.2
maxiter = 10
cont = np.median(spec)
idx1 = (spec/cont > thlow) & (spec/cont < thhigh)
#if wl.min() < 7530:
# lowbound = 7530
#else:
# lowbound = 7580
lowbound = 7580
if snr > 7:
for i in range(maxiter):
idx1 = idx1 & ((wl < lowbound) | (wl > 7700))
#plt.plot(wl[idx1], spec[idx1], lw = 2)
z = legfit(wl[idx1], spec[idx1], 2, w = 1./dspec[idx1])
cont = legval(wl,z)
idx2 = (spec/cont > thlow) & (spec/cont < thhigh) & ((wl < lowbound) | (wl > 7700))
if all(idx1 == idx2):
break
else:
idx1 = idx2
else:
z = legfit(wl[idx1], spec[idx1], 0, w = 1./dspec[idx1])
cont = legval(wl,z)
if show_normalization:
plt.show()
plt.figure()
plt.plot(wl, spec)
plt.plot(wl[idx1], spec[idx1], lw = 2)
plt.plot(wl, cont)
plt.show()
spec = spec/cont
dspec = dspec/cont
return spec,dspec
'''
def normalize_spec(wl, spec, dspec):
idx = (np.isnan(spec))
spec[idx] = 0
dspec[idx] = 1e15
idx = np.isnan(dspec)
dspec[idx] = 1e15
idx = spec < 0
dspec[idx] = 1e15
idx = dspec < 0
dspec[idx] = 1e15
scale = np.median(spec)
spec = spec / scale
dspec = dspec / scale
idx = spec > 0.9
if np.median(spec / dspec) > 7:
z = np.polyfit(wl[idx], spec[idx], 2, w=1. / dspec[idx])
else:
z = np.polyfit(wl[idx], spec[idx], 0)
p = np.poly1d(z)
spec = spec / p(wl)
dspec = dspec / p(wl)
# plt.plot(pixels, p(pixels)/max(p(pixels)))
# plt.show()
return spec, dspec
'''
def lp_post(rv, rvmin, rvmax, mask, wl, model, obj, objerr):
lp = -np.inf
if rv < rvmax and rv > rvmin:
z = rv/c
lp_prior=0.0
new_wl = wl*(1+z)
if cubic:
p = CubicSpline(new_wl,model)
model = p(wl)
else:
model = np.interp(wl,new_wl,model)
model = model[mask]
obj = obj[mask]
objerr = objerr[mask]
lp_post= - np.sum((obj-model)**2/(2.0*(objerr**2)))
if np.isfinite(lp_post):
lp=lp_post+lp_prior
return lp
def chi2cal(theta, mask, wl, model, obj, objerr):
rv = theta
z = rv/c
new_wl = wl*(1+z)
if cubic:
p = CubicSpline(new_wl,model)
model = p(wl)
else:
model = np.interp(wl,new_wl,model)
model = model[mask]
obj = obj[mask]
objerr = objerr[mask]
chi2 = np.sum((obj-model)**2/(objerr**2))
return chi2
def get_snr(filename):
data = pyfits.open(filename)
data[7].verify('fix')
temp = data[7].data
snr = np.median(temp['SPEC'].flatten()/np.sqrt(1/(abs(temp['IVAR']).flatten())))
return snr
def combine_imacs_spec_resample_uniform(filename, nbuff=3):
"""
resample to a common grid with 0.19 step size
"""
data = pyfits.open(filename)
wl = np.arange(7400, 9000, resample_step)
spec = np.zeros([len(wl), 4])
dspec = np.zeros([len(wl), 4])
spec_wgt = np.zeros([len(wl), 4])
k = 0
for i in range(5, 9):
data[i].verify('fix')
temp = data[i].data
wltemp = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
spectemp = temp['SPEC'].flatten()[::-1][nbuff:-nbuff]
dspectemp = np.sqrt(1. / (abs(temp['IVAR']).flatten()))[::-1][nbuff:-nbuff]
if normalizeb4combine:
spectemp, dspectemp = normalize_spec(wltemp, spectemp, dspectemp)
# plt.plot(wltemp, spectemp,'k')
spec[:, k] = np.interp(wl, wltemp, spectemp, left=0., right=0.)
dspec[:, k] = np.interp(wl, wltemp, dspectemp, left=1.e99, right=1.e99)
spec_wgt[:, k] = 1. / dspec[:, k] ** 2
k = k + 1
speccombine = np.sum(spec * spec_wgt, axis=1) / np.sum(spec_wgt, axis=1)
dspeccombine = np.sqrt(1. / np.sum(spec_wgt, axis=1))
if writespec:
np.savetxt(filename+'.txt', np.column_stack((wl,speccombine)))
print('save to', filename+'.txt')
if showcombinedspectra:
plt.plot(wl, speccombine/np.median(speccombine),'k')
plt.plot(wl, dspeccombine/np.median(speccombine),'b')
plt.ylim(-1,2)
#plt.xlim(wlmaskmin, wlmaskmax)
plt.show()
return wl, speccombine, dspeccombine
def combine_imacs_spec_resample(filename, nbuff=3):
"""
resample to a common grid defined by the raw 1D spectra.
for the overlap region, the redder spectra's grid is used
"""
data = pyfits.open(filename)
temp = data[5].verify('fix')
temp = data[5].data
wl = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
for i in range(6, 9):
temp = data[i].verify('fix')
temp = data[i].data
wltemp = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
wl = np.concatenate((wl[wl < wltemp[0]],wltemp))
spec = np.zeros([len(wl), 4])
dspec = np.zeros([len(wl), 4])
spec_wgt = np.zeros([len(wl), 4])
k = 0
for i in range(5, 9):
data[i].verify('fix')
temp = data[i].data
wltemp = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
spectemp = temp['SPEC'].flatten()[::-1][nbuff:-nbuff]
dspectemp = np.sqrt(1. / (abs(temp['IVAR']).flatten()))[::-1][nbuff:-nbuff]
if normalizeb4combine:
spectemp, dspectemp = normalize_spec(wltemp, spectemp, dspectemp)
spec[:, k] = np.interp(wl, wltemp, spectemp, left=0., right=0.)
dspec[:, k] = np.interp(wl, wltemp, dspectemp, left=1.e99, right=1.e99)
spec_wgt[:, k] = 1. / dspec[:, k] ** 2
k = k + 1
speccombine = np.sum(spec * spec_wgt, axis=1) / np.sum(spec_wgt, axis=1)
dspeccombine = np.sqrt(1. / np.sum(spec_wgt, axis=1))
return wl, speccombine, dspeccombine
def combine_imacs_spec(filename, nbuff=3):
'''
combine the four chips. Use this as default
'''
data = pyfits.open(filename)
data[5].verify('fix')
temp = data[5].data
wl = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
spec = temp['SPEC'].flatten()[::-1][nbuff:-nbuff]
dspec = np.sqrt(1. / (abs(temp['IVAR']).flatten()))[::-1][nbuff:-nbuff]
if normalizeb4combine:
spec, dspec = normalize_spec(wl, spec, dspec)
if showcombinedspectra:
plt.plot(wl, spec)
overlap = 0
for i in range(6, 9):
data[i].verify('fix')
temp = data[i].data
wltemp = temp['LAMBDA'].flatten()[::-1][nbuff:-nbuff]
spectemp = temp['SPEC'].flatten()[::-1][nbuff:-nbuff]
dspectemp = np.sqrt(1. / (abs(temp['IVAR']).flatten()))[::-1][nbuff:-nbuff]
if normalizeb4combine:
spectemp, dspectemp = normalize_spec(wltemp, spectemp, dspectemp)
if showcombinedspectra:
plt.plot(wltemp, spectemp)
if wltemp[0]-wl[-1] > 0.19:
wl_gap = np.arange(wl[-1]+0.19, wltemp[0], 0.19)
spec_gap = np.zeros_like(wl_gap)
dspec_gap = np.zeros_like(wl_gap)+1e99
wl = np.concatenate((wl, wl_gap, wltemp))
spec = np.concatenate((spec, spec_gap, spectemp))
dspec = np.concatenate((dspec, dspec_gap, dspectemp))
elif (wltemp[0] - wl[-1] <= 0.19) & (wltemp[0] - wl[-1] > 0):
wl = np.concatenate((wl, wltemp))
spec = np.concatenate((spec, spectemp))
dspec = np.concatenate((dspec, dspectemp))
else:
overlap = 1
print('THERE ARE OVERLAPS BETWEEN CHIPS!')
if overlap == 1:
wl,speccombine,dspeccombine = combine_imacs_spec_resample(filename, nbuff = nbuff)
else:
speccombine = spec
dspeccombine = dspec
if writespec:
np.savetxt(filename+'.txt', np.column_stack((wl,speccombine)))
print('save to', filename+'.txt')
if showcombinedspectra:
plt.plot(wl, speccombine/np.median(speccombine),'k')
plt.plot(wl, dspeccombine/np.median(speccombine),'b')
plt.ylim(-1,2)
#plt.xlim(wlmaskmin, wlmaskmax)
plt.show()
return wl, speccombine, dspeccombine
def read_rv_stds(filename, stdnum):
temp = pyfits.open(filename)[0].data[stdnum]
hdr = pyfits.open(filename)[0].header
coeff0 = hdr['COEFF0']
coeff1 = hdr['COEFF1']
rvwl = 10**(coeff0 + coeff1 * np.arange(len(temp)))
rvspec = temp.astype('float')
return rvwl, rvspec
#new_rvwl = np.arange(7500,9000.1,0.1)
#new_rvspec = np.interp(new_rvwl, rvwl, rvspec)
#return new_rvwl, new_rvspec
def read_tell_stds(filename):
temp = pyfits.open(filename)[0].data
hdr = pyfits.open(filename)[0].header
coeff0 = hdr['COEFF0']
coeff1 = hdr['COEFF1']
rvwl = 10**(coeff0 + coeff1 * np.arange(len(temp)))
rvspec = temp.astype('float')
return rvwl, rvspec
#new_rvwl = np.arange(7500,9000.1,0.1)
#new_rvspec = np.interp(new_rvwl, rvwl, rvspec)
#return new_rvwl, new_rvspec
def get_rv(wl, spec, dspec, rvwl, rvspec, object, rvstar):
if single and bhb:
fitstart = (np.abs(wl-8400)).argmin()
fitend = (np.abs(wl-9000)).argmin()
else:
fitstart = (np.abs(wl-8400)).argmin()
fitend = (np.abs(wl-8700)).argmin()
spec = spec[fitstart:fitend]
dspec = dspec[fitstart:fitend]
wl = wl[fitstart:fitend]
spec,dspec = normalize_spec(wl, spec, dspec)
ndim=1
nwalkers=20
rvmin = -800
rvmax = 800
nstars = len(rvstar)
rvdist = np.zeros([nstars, nwalkers * nsam])
chi2rv = np.zeros(nstars)
rvspec_temp = np.zeros([nstars, len(wl)])
# MCMC needs some time to produce reasonable "d" from the likelihood, which is called the "burn-in" period.
# Adjusting the "burn-in" period is quite empirical.
nburn=50
for kk in range(0, nstars, 1):
if cubic:
p = CubicSpline(rvwl[kk], rvspec[kk])
tempspec = p(wl)
else:
tempspec = np.interp(wl, rvwl[kk], rvspec[kk])
rvspec_temp[kk] = tempspec
rvspec = rvspec_temp
if single and bhb:
wlmask = (wl > wlmaskmin_bhb) & (wl < wlmaskmax_bhb)
else:
wlmask = (wl > wlmaskmin) & (wl < wlmaskmax)
snr = np.nanmedian(spec[wlmask]/dspec[wlmask])
print('SNR = '+str(snr))
for kk in range(0, nstars, 1):
# here we use the chi-square minimization to find the starting p0
rvarr = np.arange(rvmin,rvmax)
likearr = np.array([lp_post(i,rvmin, rvmax, wlmask, wl, rvspec[kk], spec, dspec) for i in rvarr])
p0 = np.random.rand(ndim * nwalkers).reshape((nwalkers, ndim))
#p0= p0 * rvmax * 2 - rvmax
p0 = p0 + rvarr[max(likearr) == likearr][0]
with MultiPool() as pool:
sampler = emcee.EnsembleSampler(nwalkers, ndim, lp_post, args=(rvmin, rvmax, wlmask, wl, rvspec[kk], spec, dspec), pool=pool)
pos, prob, state = sampler.run_mcmc(p0, nburn)
sampler.reset()
sampler.run_mcmc(pos, nsam)
rvdist[kk, :] = sampler.flatchain[:, 0]
rv_mean = np.nanmedian(rvdist[kk, :])
rv_std = np.std(rvdist[kk, :])
masklen = len(wl[wlmask])
chi2rv[kk] = chi2cal(rv_mean, wlmask, wl, rvspec[kk], spec, dspec) / masklen
print(rv_mean, rv_std, chi2rv[kk], rvstar[kk])
chi2rv[chi2rv == 0] = 1e10
rvidx = (chi2rv == np.min(chi2rv))
jj = np.arange(0, nstars)[rvidx][0]
temp = rvdist[jj]
#temp_mean = np.nanmedian(temp)
#if np.std(temp) > 10: temp = temp[(temp < temp_mean + 150) & (temp > temp_mean - 150)]
temp = stats.sigmaclip(temp, low=5, high=5)[0]
rv_mean = np.nanmedian(temp)
#rv_std = np.std(temp)
rv_std = 0.5 * (np.percentile(temp, 84) - np.percentile(temp, 16))
print('best fit', rv_mean, rv_std, chi2rv[jj], rvstar[jj])
# Plot the result.
if single and bhb:
fig, axarr = plt.subplots(1, 2, figsize=(15,6))
axarr[0].hist(temp, 100, color="k", histtype="step", range = [rv_mean - 5*rv_std, rv_mean + 5*rv_std])
axarr[0].set_title('RV Histogram', fontsize=16)
axarr[0].xaxis.set_major_locator(plt.MultipleLocator(rv_std*3))
axarr[0].set_xlabel('RV')
axarr[0].set_xlim(rv_mean - 5*rv_std, rv_mean + 5*rv_std)
#axarr[0].axvline(np.percentile(temp, 16), ls='--', color='r')
#axarr[0].axvline(np.percentile(temp, 84), ls='--', color='r')
axarr[0].axvline(np.percentile(temp, 50), ls='--', color='r')
axarr[1].plot(wl[wlmask], spec[wlmask], 'm',lw=0.5)
axarr[1].plot(wl[wlmask]*(1+rv_mean/c), rvspec[jj][wlmask], 'b')
axarr[1].set_xlim(wlmaskmin_bhb,wlmaskmax_bhb)
axarr[1].set_ylim(-0.5,1.5)
axarr[1].set_title(object+'+'+rvstar[jj])
else:
fig, axarr = plt.subplots(1, 4, figsize=(15,6))
axarr[0].hist(temp, 100, color="k", histtype="step", range = [rv_mean - 5*rv_std, rv_mean + 5*rv_std])
axarr[0].set_title('RV Histogram', fontsize=16)
axarr[0].xaxis.set_major_locator(plt.MultipleLocator(rv_std*3))
axarr[0].set_xlabel('RV')
axarr[0].set_xlim(rv_mean - 5*rv_std, rv_mean + 5*rv_std)
#axarr[0].axvline(np.percentile(temp, 16), ls='--', color='r')
#axarr[0].axvline(np.percentile(temp, 84), ls='--', color='r')
axarr[0].axvline(np.percentile(temp, 50), ls='--', color='r')
axarr[1].plot(wl[wlmask], spec[wlmask], 'm',lw=0.5)
axarr[1].plot(wl[wlmask]*(1+rv_mean/c), rvspec[jj][wlmask], 'b')
axarr[1].set_xlim(CaT1min*(1+rv_mean/c),CaT1max*(1+rv_mean/c))
axarr[1].set_ylim(-0.5,1.5)
axarr[1].xaxis.set_major_locator(plt.MultipleLocator(10))
axarr[1].axvline(8498.03*(1+rv_mean/c), ls='--', color='r')
axarr[2].plot(wl[wlmask], spec[wlmask], 'm',lw=0.5)
axarr[2].plot(wl[wlmask]*(1+rv_mean/c), rvspec[jj][wlmask], 'b')
axarr[2].set_xlim(CaT2min*(1+rv_mean/c),CaT2max*(1+rv_mean/c))
axarr[2].set_ylim(-0.5,1.5)
axarr[2].xaxis.set_major_locator(plt.MultipleLocator(10))
axarr[2].axvline(8542.09*(1+rv_mean/c), ls='--', color='r')
axarr[2].set_title(object+'+'+rvstar[jj])
axarr[2].set_xlabel('Wavelength')
axarr[3].plot(wl[wlmask], spec[wlmask], 'm',lw=0.5)
axarr[3].plot(wl[wlmask]*(1+rv_mean/c), rvspec[jj][wlmask], 'b')
axarr[3].set_xlim(CaT3min*(1+rv_mean/c),CaT3max*(1+rv_mean/c))
axarr[3].set_ylim(-0.5,1.5)
axarr[3].axvline(8662.14*(1+rv_mean/c), ls='--', color='r')
axarr[3].xaxis.set_major_locator(plt.MultipleLocator(10))
axarr[3].set_title('snr ='+str(snr))
if savervplot:
plt.savefig(figdir+str(object)+'_rv.png')
if showrvplot:
plt.show()
return rv_mean, rv_std, chi2rv[jj], snr, rvstar[jj]
def get_telluric_corr(wl, spec, dspec, rvwl, rvspec, object, rv):
ndim=1
nwalkers=20
rvmin = -10
rvmax = 10
p0=np.random.rand(ndim * nwalkers).reshape((nwalkers, ndim))
p0= p0 * rvmax * 2 - rvmax
wl0 = wl
spec0 = spec
dspec0 = dspec
rvspec0 = np.interp(wl0, rvwl, rvspec)
spec = spec[(wl > 7530) & (wl < 7720)]
dspec = dspec[(wl > 7530) & (wl < 7720)]
wl = wl[(wl > 7530) & (wl < 7720)]
# MCMC needs some time to produce reasonable "d" from the likelihood, which is called the "burn-in" period.
# Adjusting the "burn-in" period is quite empirical.
nburn=50
if cubic:
p = CubicSpline(rvwl, rvspec)
rvspec = p(wl)
else:
rvspec = np.interp(wl, rvwl, rvspec)
#spec = np.interp(rvwl, wl, spec)
#dspec = np.interp(rvwl, wl, dspec)
#wlmask = (rvwl > 7550) & (rvwl < 7700)
wlmask = (wl > 7550) & (wl < 7700)
snr = np.median(spec[wlmask]/dspec[wlmask])
print('SNR = '+str(snr))
#MCMC
with MultiPool() as pool:
sampler = emcee.EnsembleSampler(nwalkers, ndim, lp_post, args=(rvmin, rvmax, wlmask, wl, rvspec, spec, dspec), pool=pool)
pos, prob, state = sampler.run_mcmc(p0, nburn)
sampler.reset()
sampler.run_mcmc(pos, nsam)
rvdist = sampler.flatchain[:,0]
temp = rvdist
temp_mean = np.nanmedian(temp)
if np.std(temp) > 10 : temp = temp[(temp < temp_mean + 70) & (temp > temp_mean -70)]
temp = stats.sigmaclip(temp,low=5, high=5)[0]
rv_mean = np.nanmedian(temp)
#rv_std = np.std(temp)
rv_std = 0.5 * (np.percentile(temp, 84) - np.percentile(temp, 16))
#masklen=len(rvwl[wlmask])
masklen = len(wl[wlmask])
chi2rv = chi2cal(rv_mean, wlmask, wl, rvspec, spec, dspec) / masklen
if True:
# Plot the result.
fig, axarr = plt.subplots(1, 4, figsize=(15,6))
axarr[0].hist(temp, 100, color="k", histtype="step", range = [rv_mean - 5*rv_std, rv_mean + 5*rv_std])
axarr[0].set_title('aband RV Histogram', fontsize=16)
axarr[0].xaxis.set_major_locator(plt.MultipleLocator(rv_std*3))
axarr[0].set_xlabel('RV')
axarr[0].set_xlim(rv_mean - 5*rv_std, rv_mean + 5*rv_std)
#axarr[0].axvline(np.percentile(temp, 16), ls='--', color='r')
#axarr[0].axvline(np.percentile(temp, 84), ls='--', color='r')
axarr[0].axvline(np.percentile(temp, 50), ls='--', color='b')
axarr[1].plot(wl[wlmask], spec[wlmask], 'm',lw=0.5)
axarr[1].plot(wl[wlmask]*(1+rv_mean/c), rvspec[wlmask], 'b')
axarr[1].set_xlim(7550,7700)
axarr[1].set_ylim(-0.5,1.5)
axarr[1].set_title(str(object)+' telluric', fontsize=16)
axarr[1].set_xlabel('Wavelength')
axarr[1].xaxis.set_major_locator(plt.MultipleLocator(40))
axarr[2].plot(wl0, spec0, 'm',lw=0.5)
axarr[2].plot(wl0*(1+rv_mean/c), rvspec0, 'b')
axarr[2].axvline(8183.25*(1+rv/c), ls='--', color='r')
axarr[2].axvline(8194.79*(1+rv/c), ls='--', color='r')
axarr[2].set_xlim(8160,8220)
axarr[2].set_ylim(-0.5,1.5)
axarr[2].xaxis.set_major_locator(plt.MultipleLocator(30))
axarr[2].set_title('Na I at 8200A')
axarr[3].plot(wl0, spec0, 'm',lw=0.5)
axarr[3].plot(wl0*(1+rv_mean/c), rvspec0, 'b')
axarr[3].axvline(8806.8*(1+rv/c), ls='--', color='r')
axarr[3].set_xlim(8790*(1+rv/c),8820*(1+rv/c))
axarr[3].set_ylim(-0.5,1.5)
axarr[3].xaxis.set_major_locator(plt.MultipleLocator(30))
axarr[3].set_title('Mg I at 8807A')
if savervplot:
plt.savefig(figdir+str(object)+'_tell.png')
if showrvplot:
plt.show()
return rv_mean, rv_std, chi2rv, snr
def Flin(x,p):
#P[0] = CONTINUUM LEVEL
#P[1] = GAUSSIAN HEIGHT/DEPTH FOR MIDDLE CAT LINE
#P[2] = LINE POSITION
#P[3] = GAUSSIAN WIDTH
#P[4] = LORENTZIAN HEIGHT/DEPTH FOR MIDDLE CAT LINE
#P[5] = LORENTZIAN WIDTH
#P[6] = GAUSSIAN HEIGHT/DEPTH FOR 8498 CAT LINE
#P[7] = GAUSSIAN HEIGHT/DEPTH FOR 8662 CAT LINE
#P[8] = LORENTZIAN HEIGHT/DEPTH FOR 8498 CAT LINE
#P[9] = LORENTZIAN HEIGHT/DEPTH FOR 8662 CAT LINE
gauss = p[1]*np.exp(-0.5*((x-p[2])/p[3])**2)+ \
p[6]*np.exp(-0.5*( (x-p[2]*0.994841)/p[3] )**2) + \
p[7]*np.exp(-0.5*( (x-p[2]*1.01405)/p[3] )**2)
lorentz = p[4]*p[5]/( (x-p[2])**2 + (p[5]/2.)**2 ) + \
p[8]*p[5]/( (x-p[2]*0.994841)**2 + (p[5]/2.)**2 ) + \
p[9]*p[5]/( (x-p[2]*1.01405)**2 + (p[5]/2.)**2 )
return p[0] * (1 + gauss + lorentz)
'''
# if the relative ratio between three lines are fixed, then use this Flin(x,p)
def Flin(x,p):
gauss = p[1]*np.exp(-0.5*((x-p[2])/p[3])**2)+ \
0.6 * p[1]*np.exp(-0.5*( (x-p[2]*0.994841)/p[3] )**2) + \
0.9 * p[1]*np.exp(-0.5*( (x-p[2]*1.01405)/p[3] )**2)
lorentz = p[4]*p[5]/( (x-p[2])**2 + (p[5]/2.)**2 ) + \
0.6 * p[4]*p[5]/( (x-p[2]*0.994841)**2 + (p[5]/2.)**2 ) + \
0.9 * p[4]*p[5]/( (x-p[2]*1.01405)**2 + (p[5]/2.)**2 )
return p[0] + gauss + lorentz
'''
def myfunctlin(p, fjac=None, x=None, y=None, err=None):
model = Flin(x, p)
status = 0
#return [status, (y-model)**2/(2*err**2)]
return [status, ((y-model)/err)]
def get_ew(object, wl, spec, dspec, rv, gaussianonly = 0):
fitstart = (np.abs(wl-8400)).argmin()
fitend = (np.abs(wl-8700)).argmin()
spec = spec[fitstart:fitend]
dspec = dspec[fitstart:fitend]
wl = wl[fitstart:fitend]
spec,dspec = normalize_spec(wl, spec, dspec)
fitstart = (np.abs(wl-8483*(1+rv/c))).argmin()
fitend = (np.abs(wl-8677*(1+rv/c))).argmin()
#orignal
#fitstart = (np.abs(w-8484)).argmin()
#fitend = (np.abs(w-8682)).argmin()
contstart = (np.abs(wl-8563*(1+rv/c))).argmin()
contend = (np.abs(wl-8577*(1+rv/c))).argmin()
peakfindstart = (np.abs(wl-8538.09*(1+rv/c))).argmin()
peakfindend = (np.abs(wl-8546.09*(1+rv/c))).argmin()
sn = np.median(spec[fitstart:fitend]/dspec[fitstart:fitend])
contlevel = np.nanmedian(spec[contstart:contend][spec[contstart:contend]>0])
if np.isnan(contlevel) :
contstart = (np.abs(wl-8590*(1+rv/c))).argmin()
contend = (np.abs(wl-8610*(1+rv/c))).argmin()
contlevel = np.nanmedian(spec[contstart:contend][spec[contstart:contend]>0])
spec = spec / contlevel
dspec = dspec / contlevel
smoothspec = ndimage.filters.uniform_filter(spec,size=5)
linepos = smoothspec[peakfindstart:peakfindend].argmin()
depth = min(smoothspec[peakfindstart:peakfindend]) - np.median(spec[fitstart:fitend])
initial_guesses = np.zeros(10)
param_control = [{'fixed':0, 'limited':[0,0], 'limits':[0.,0.]} for i in range(10)]
initial_guesses[0] = np.median(spec[contstart:contend])
initial_guesses[1] = 0.5*depth
#initial_guesses[2] = (wl[fitstart:fitend])[linepos+peakfindstart-fitstart]
initial_guesses[2] = 8542.09*(1+rv/c) # changed this to fix the chip gap issue
initial_guesses[3] = 1.0
initial_guesses[4] = 0.3*depth
initial_guesses[5] = 1.0
initial_guesses[6] = 0.25*depth
initial_guesses[7] = 0.4*depth
initial_guesses[8] = 0.15*depth
initial_guesses[9] = 0.24*depth
param_control[1]['limited'][1] = 1
param_control[1]['limits'][1] = 0.
param_control[1]['limited'][0] = 1
param_control[1]['limits'][0] = -1.
param_control[4]['limited'][1] = 1
param_control[4]['limits'][1] = 0.
param_control[4]['limited'][0] = 1
param_control[4]['limits'][0] = -1.
param_control[6]['limited'][1] = 1
param_control[6]['limits'][1] = 0.
param_control[6]['limited'][0] = 1
param_control[6]['limits'][0] = -1.
param_control[7]['limited'][1] = 1
param_control[7]['limits'][1] = 0.
param_control[7]['limited'][0] = 1
param_control[7]['limits'][0] = -1.
param_control[8]['limited'][1] = 1
param_control[8]['limits'][1] = 0.
param_control[8]['limited'][0] = 1
param_control[8]['limits'][0] = -1.
param_control[9]['limited'][1] = 1
param_control[9]['limits'][1] = 0.
param_control[9]['limited'][0] = 1
param_control[9]['limits'][0] = -1.
#FORCE LINE WIDTHS TO BE AT LEAST 1 RESOLUTION ELEMENT (0.8AA??) AND LESS THAN 300 KM/S
param_control[3]['limited'][0] = 1
param_control[3]['limits'][0] = 0.2 #0.8/2.35
param_control[3]['limited'][1] = 1
param_control[3]['limits'][1] = 3.63
param_control[5]['limited'][0] = 1
param_control[5]['limits'][0] = 0.2 #0.8
param_control[5]['limited'][1] = 1
param_control[5]['limits'][1] = 3.63
if gaussianonly:
initial_guesses[4] = 0.
initial_guesses[8] = 0.
initial_guesses[9] = 0.
initial_guesses[5] = 1.0
param_control[4]['fixed'] = 1
param_control[8]['fixed'] = 1
param_control[9]['fixed'] = 1
param_control[5]['fixed'] = 1
fa = {'x':wl[fitstart:fitend], 'y': spec[fitstart:fitend], 'err':dspec[fitstart:fitend]}
try:
m = mpfit(myfunctlin, initial_guesses, functkw=fa, quiet=1, parinfo=param_control, xtol = 1.0e-15)
except ValueError:
print("Oops! Something wrong.")
modelgl = Flin(wl[fitstart:fitend], m.params)
covargl = m.covar
errmsg = m.errmsg
status = m.status
print('iter', m.niter)
if m.niter <= 2:
print('MPFIT STUCK! RESULTS MAY BE WRONG')
niter = m.niter
perrorgl = m.perror
chisqgl = sum((spec[fitstart:fitend]-modelgl)**2/dspec[fitstart:fitend]**2)
outparams = m.params
lineparams=outparams
if perrorgl is None:
perrorgl = np.zeros(10) + 1
covargl = np.zeros([10, 10]) + 1
niter = 2
if dispara:
print(' CHI-SQUARE = %10.1f' %chisqgl)
print(' DOF = %10.1f' %(fitend-fitstart+1-len(outparams)))
print(' P(0) = %7.3f +/- %7.3f' %(outparams[0],perrorgl[0]))
print(' P(1) = %7.3f +/- %7.3f' %(outparams[1],perrorgl[1]))
print(' P(2) = %10.4f +/- %10.4f' %(outparams[2],perrorgl[2]))
print(' P(3) = %7.3f +/- %7.3f' %(outparams[3],perrorgl[3]))
print(' P(4) = %7.3f +/- %7.3f' %(outparams[4],perrorgl[4]))
print(' P(5) = %7.3f +/- %7.3f' %(outparams[5],perrorgl[5]))
print(' P(6) = %7.3f +/- %7.3f' %(outparams[6],perrorgl[6]))
print(' P(7) = %7.3f +/- %7.3f' %(outparams[7],perrorgl[7]))
print(' P(8) = %7.3f +/- %7.3f' %(outparams[8],perrorgl[8]))
print(' P(9) = %7.3f +/- %7.3f' %(outparams[9],perrorgl[9]))
if True:
# Plot the result.
fig, axarr = plt.subplots(1, 3, figsize=(17,6))
axarr[0].plot(wl[fitstart:fitend],spec[fitstart:fitend],c='m')
axarr[0].plot(wl[fitstart:fitend],spec[fitstart:fitend]-modelgl,c='y')
axarr[0].plot(wl[fitstart:fitend],modelgl,lw=2,c='k')
axarr[0].set_xlim(CaT1min*(1+rv/c),CaT1max*(1+rv/c))
axarr[0].set_ylim(-0.5,1.5)
axarr[0].xaxis.set_major_locator(plt.MultipleLocator(10))
axarr[1].plot(wl[fitstart:fitend],spec[fitstart:fitend],c='m')
axarr[1].plot(wl[fitstart:fitend],spec[fitstart:fitend]-modelgl,c='y')
axarr[1].plot(wl[fitstart:fitend],modelgl,lw=2,c='k')
axarr[1].set_xlim(CaT2min*(1+rv/c),CaT2max*(1+rv/c))
axarr[1].set_ylim(-0.5,1.5)
axarr[1].xaxis.set_major_locator(plt.MultipleLocator(10))
axarr[1].set_title(str(object), fontsize=16)
axarr[1].set_xlabel('Wavelength')
axarr[2].plot(wl[fitstart:fitend],spec[fitstart:fitend],c='m')
axarr[2].plot(wl[fitstart:fitend],spec[fitstart:fitend]-modelgl,c='y')
axarr[2].plot(wl[fitstart:fitend],modelgl,lw=2,c='k')
axarr[2].set_xlim(CaT3min*(1+rv/c),CaT3max*(1+rv/c))
axarr[2].set_ylim(-0.5,1.5)
axarr[2].xaxis.set_major_locator(plt.MultipleLocator(10))
if saveewplot:
plt.savefig(figdir+str(object)+'_ew.png')
if showewplot:
plt.show()
#plt.close()
gaussian_integral = outparams[1] * outparams[3] * np.sqrt(2*np.pi)
dgaussian_integral = np.sqrt((outparams[1] * perrorgl[3] * np.sqrt(2*np.pi))**2 + \
(perrorgl[1] * outparams[3] * np.sqrt(2*np.pi))**2)
lorentzian_integral = 2*np.pi*outparams[4]
dlorentzian_integral = 2*np.pi*perrorgl[4]
ew2_fit = gaussian_integral + lorentzian_integral
dew2_fit = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2)
dew2_fit_covar = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2 + \
2*np.pi*outparams[1]*outparams[3]*covargl[1,3]*perrorgl[1]*perrorgl[3] + \
(2*np.pi)**1.5*outparams[3]*covargl[1,4]*perrorgl[1]*perrorgl[4] + \
(2*np.pi)**1.5*outparams[1]*covargl[3,4]*perrorgl[3]*perrorgl[4])
v2 = (outparams[2] - 8542.09)/8542.09*c
dv2 = perrorgl[2]/8542.09*c
if dispara:
print('V_CaT2: %10.3f +/- %10.3f' %(v2,dv2))
print('CaT2 (fit): %10.3f +/- %10.3f' %(ew2_fit,dew2_fit))
print('CaT2 (fit, covar) %10.3f +/- %10.3f' %(ew2_fit,dew2_fit_covar))
gaussian_integral = outparams[6] * outparams[3] * np.sqrt(2*np.pi)
dgaussian_integral = np.sqrt( (outparams[6] * perrorgl[3] * np.sqrt(2*np.pi))**2 + \
(perrorgl[6] * outparams[3] * np.sqrt(2*np.pi))**2 )
lorentzian_integral = 2*np.pi*outparams[8]
dlorentzian_integral = 2*np.pi*perrorgl[8]
ew1_fit = gaussian_integral + lorentzian_integral
dew1_fit = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2)
dew1_fit_covar = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2 + \
2*np.pi*outparams[6]*outparams[3]*covargl[6,3]*perrorgl[6]*perrorgl[3] + \
(2*np.pi)**1.5*outparams[3]*covargl[6,8]*perrorgl[6]*perrorgl[8] + \
(2*np.pi)**1.5*outparams[6]*covargl[3,8]*perrorgl[3]*perrorgl[8])
if dispara:
print('CaT1 (fit): %10.3f +/- %10.3f' %(ew1_fit,dew1_fit))
print('CaT1 (fit, covar) %10.3f +/- %10.3f' %(ew1_fit,dew1_fit_covar))
print('CaT1 (fit): %10.3f +/- %10.3f' %(0.6 * ew2_fit, 0.6 * dew2_fit))
gaussian_integral = outparams[7] * outparams[3] * np.sqrt(2*np.pi)
dgaussian_integral = np.sqrt( (outparams[7] * perrorgl[3] * np.sqrt(2*np.pi))**2 + \
(perrorgl[7] * outparams[3] * np.sqrt(2*np.pi))**2 )
lorentzian_integral = 2*np.pi*outparams[9]
dlorentzian_integral = 2*np.pi*perrorgl[9]
ew3_fit = gaussian_integral + lorentzian_integral
dew3_fit = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2)
dew3_fit_covar = np.sqrt(dgaussian_integral**2 + dlorentzian_integral**2 + \
2*np.pi*outparams[7]*outparams[3]*covargl[7,3]*perrorgl[7]*perrorgl[3] + \
(2*np.pi)**1.5*outparams[3]*covargl[7,9]*perrorgl[7]*perrorgl[9] + \
(2*np.pi)**1.5*outparams[7]*covargl[3,9]*perrorgl[3]*perrorgl[9])
if dispara:
print('CaT3 (fit): %10.3f +/- %10.3f' %(ew3_fit,dew3_fit))
print('CaT3 (fit, covar) %10.3f +/- %10.3f' %(ew3_fit,dew3_fit_covar))
print('CaT3 (fit): %10.3f +/- %10.3f' %(0.9 * ew2_fit, 0.9 * dew2_fit))
ews = ew1_fit+ew2_fit+ew3_fit
dews = np.sqrt(dew1_fit**2+dew2_fit**2+dew3_fit**2)
vcat = v2
if gaussianonly:
return -ew1_fit*1.1, dew1_fit*1.1, -ew2_fit*1.1, dew2_fit*1.1, -ew3_fit*1.1, dew3_fit*1.1, -ews*1.1, dews*1.1, vcat, niter
else:
return -ew1_fit, dew1_fit, -ew2_fit, dew2_fit, -ew3_fit, dew3_fit, -ews, dews, vcat, niter
def helio2gsr(vhelio, l, b):
usol=11.1
vsol=12.24
wsol=7.25
theta=220.0
vcirc=vsol+theta
vgsr = vhelio+usol*np.cos(b*np.pi/180.)*np.cos(l*np.pi/180.)+vcirc*np.cos(b*np.pi/180.)*np.sin(l*np.pi/180.)+wsol*np.sin(b*np.pi/180)
return vgsr
if __name__ == "__main__":
rvwl1, rvspec1 = read_rv_stds(rv_fname, 0)
rvwl2, rvspec2 = read_rv_stds(rv_fname, 1)
rvwl3, rvspec3 = read_rv_stds(rv_fname, 8)
if not (all(rvwl1 == rvwl2) and all(rvwl2 == rvwl3)):
print("SOMETHING WRONG WITH STELLAR TEMPLATES")
rvspec1[rvspec1 == 0] = 1
rvspec2[rvspec2 == 0] = 1
rvspec3[rvspec3 == 0] = 1
if bhb and single:
rvstar = np.array(['HD161817'])
else:
rvstar = np.array(['HD122563', 'HD26297', 'HD161817'])
num = len(rvwl1)
nstars = len(rvstar)
rvwl = np.zeros([nstars, num])
rvspec = np.zeros([nstars, num])
if bhb and single:
rvwl[0] = rvwl3