forked from boucherastro/starships
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlation_class.py
1692 lines (1328 loc) · 71.3 KB
/
correlation_class.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 starships.orbite as o
import starships.analysis as a
import starships.correlation as corr
import starships.homemade as hm
import starships.ttest_fcts as nf
import starships.plotting_fcts as pf
from starships import spectrum as spectrum
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
import astropy.units as u
import astropy.constants as const
from scipy.interpolate import interp1d, interp2d
from scipy.optimize import curve_fit, fsolve
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib as mpl
mpl.rc('pdf', fonttype = 42)
class Correlations():
def __init__(self, data, kind=None, rv_grid=None, n_pcas=None, kp_array=None):
self.data = data
self.kind = kind
self.rv_grid = rv_grid
self.n_pcas = n_pcas
self.kp_array = kp_array
def calc_ccf(self, orders=None, N=1, alpha=None, index=None, ccf0=None, rm_vert_mean=False,): #v
if ccf0 is None:
if orders is None:
orders = list(np.arange(49))
# self.orders = orders
# self.ord_frac = 100*len(orders)/(49)#-len(np.where(t.N0.mask.all(axis=0))[0]))
self.ccf0 = np.ma.sum(self.data[:, orders] * N[:,orders], axis=1)
else:
self.ccf0 = ccf0
if rm_vert_mean is True:
n_spec, n_RV = self.ccf0.shape
self.ccf00 = self.ccf0.copy()
yyy=self.ccf0-np.mean(self.ccf0, axis=-1)[:,None]
recon_time = np.ones_like(yyy)*np.nan
x = np.arange(n_spec)
z_t = np.zeros((n_spec, 3))
for col in range(n_RV):
y = yyy[:,col]
if y.mask.all():
continue
idx = np.isfinite(x) & np.isfinite(y)
# Poly.fit(x[idx], y[idx], 2)
z_t = np.polyfit(x[idx], y[idx], 2)
recon_time[idx, col] = np.poly1d(z_t)(x[idx])
recon_time = np.ma.masked_invalid(recon_time)
self.ccf0 = yyy-recon_time
if alpha is None:
alpha = np.ones_like(self.ccf0)
if alpha.shape != self.ccf0.shape:
alpha = alpha[:, None]
self.ccf = self.ccf0.copy()*alpha
if index is not None:
self.ccf[np.unique(index)]=0
# def calc_logl(self, data_obj, icorr=None, orders=None, index=None,
# N=None, nolog=None, alpha=None, inj_alpha='ones', kind_obj='seq', **kwargs):
# if orders is None:
# orders = list(np.arange(49))
# if icorr is None:
# icorr = data_obj.icorr
# if alpha is None:
# if inj_alpha =='alpha':
# alpha = np.ones_like(data_obj.alpha_frac)
# elif inj_alpha == 'ones':
# alpha = data_obj.alpha_frac
# # alpha = np.ones_like(tr.alpha_frac) #tr.alpha_frac # np.ones_like(tr.alpha_frac)
# # print(orders, icorr, alpha)
# # print(self.data)
def calc_logl(self, data_obj, icorr=None, orders=None, index=None,
N=None, nolog=None, alpha=None, inj_alpha='ones', kind_obj='seq', **kwargs):
if orders is None:
orders = list(np.arange(49))
if icorr is None:
if kind_obj == 'seq':
icorr = data_obj.icorr
elif kind_obj == 'dict':
icorr = data_obj['icorr']
if alpha is None:
if inj_alpha =='alpha':
if kind_obj == 'seq':
alpha = np.ones_like(data_obj.alpha_frac)
elif kind_obj == 'dict':
alpha = np.ones_like(data_obj['alpha_frac'])
elif inj_alpha == 'ones':
if kind_obj == 'seq':
alpha = data_obj.alpha_frac
elif kind_obj == 'dict':
alpha = data_obj['alpha_frac']
self.logl0 = np.nansum( self.data[:, orders], axis=1)
self.logl = corr.sum_logl(self.data, icorr, orders, N, alpha=alpha,
axis=0, del_idx=index, nolog=nolog, **kwargs)
def calc_logl_snr(self, n_pca=None, **kwargs):
if ~hasattr(self,'interp_grid'):
self.interp_grid = self.rv_grid
if (n_pca is not None) and (len(self.n_pcas) > 1):
id_pc = np.where(np.array(self.n_pcas) == n_pca)[0]
try:
self.courbe = (self.logl.squeeze()[:,id_pc]).squeeze()
except :
print('The requested PC is not available, but taking ', self.n_pcas[0])
self.courbe = (self.logl.squeeze()[:,0]).squeeze()
else:
self.courbe = self.logl.squeeze()
self.get_snr_1d(**kwargs)
self.find_max()
def plot_multi_npca(self, logl=None, vlines=[0], kind='snr', kind_courbe='bic', ylim=None,
no_signal=None, ground_type='mean',
hlines=None, legend=True, title='', max_rv=None, force_max_rv=None, **kwargs):
lstyles = ['-','--','-.',':']
self.interp_grid = self.rv_grid
# print(self.interp_grid)
if logl is None:
logl = self.logl
# print(logl)
val = []
pos = []
snr = []
courbe = []
fig,ax = plt.subplots(1,2, figsize=(10,4))
for i in range(len(self.n_pcas)):
couleur = (0.5,0.1,i/len(self.n_pcas))
if len(self.n_pcas) > 1:
# print(logl.shape)
self.courbe = (logl[:,:,i]).squeeze()
else:
self.courbe = logl.squeeze()
# print(self.courbe, max_rv)
# if kind == 'courbe':
self.get_snr_1d(max_rv=max_rv, **kwargs)
# self.snr=self.courbe
# print(self.snr[[0,-1]])
if force_max_rv is None:
self.find_max()
else:
if kind == 'snr':
arr = self.snr
elif kind == 'courbe':
arr = self.courbe
fct = interp1d(self.interp_grid, arr)
self.pos = force_max_rv
self.max = fct(force_max_rv)
val.append(self.max)
pos.append(self.pos)
snr.append(self.snr)
courbe.append(self.courbe)
for line in vlines:
ax[1].axvline(line)
if kind == 'snr':
ax[1].plot(self.rv_grid, self.snr, color=couleur, linestyle=lstyles[i%len(lstyles)],
label='{:.1f} / {:.2f} / {:.2f}'.format(self.n_pcas[i], self.max, self.pos))
elif kind == 'courbe':
if kind_courbe == 'bic':
ax[1].plot(self.rv_grid, self.courbe-self.courbe[self.idx_bruit_rv].mean(),
color=couleur, linestyle=lstyles[i%len(lstyles)],
label='{:.1f} / {:.2f} / {:.2f}'.format(self.n_pcas[i], self.max, self.pos))
if kind_courbe == 'abs':
ax[1].plot(self.rv_grid, self.courbe,
color=couleur, linestyle=lstyles[i%len(lstyles)],
label='{:.1f} / {:.2f} / {:.2f}'.format(self.n_pcas[i], self.max, self.pos))
if legend is True:
ax[1].legend(loc='upper left', bbox_to_anchor=(1, 1))
if ylim is not None:
ax[1].set_ylim(*ylim)
val = np.array(val)
self.npc_val = val
self.npc_pos = np.array(pos)
self.npc_snr = np.array(snr)
self.npc_courbe = np.array(courbe)
print('Max value at {} npc = {} at {} km/s'.format(self.n_pcas[np.argmax(val)], \
val.max(), self.npc_pos[np.argmax(val)]))
if hlines is not None:
for line in hlines:
ax[0].axhline(line)
if kind == 'snr':
ax[0].plot(self.n_pcas, val,'o--')
ax[0].plot(self.n_pcas[np.argmax(val)], val[np.argmax(val)],'ro')
elif kind == 'courbe':
idx_pos_max = hm.nearest(self.rv_grid, self.npc_pos)
maxima = [self.npc_courbe[i, idx_pos_max[i]] for i in range(len(self.n_pcas))]
max_val = np.array(maxima)
self.npc_max_abs = max_val
if ground_type == 'mean':
ground_lvl = self.npc_courbe[:, self.idx_bruit_rv].mean(axis=-1)
elif ground_type == 'max_noise':
ground_lvl = np.abs(self.npc_courbe[:, self.idx_bruit_rv]).max(axis=-1)
elif ground_type == 'no_signal_idx':
ground_lvl = self.npc_courbe[no_signal, idx_pos_max]
elif ground_type == 'no_signal_curve':
ground_lvl = [no_signal.npc_courbe[i, idx_pos_max[i]] for i in range(len(self.n_pcas))]
elif ground_type == 'no_signal_value':
ground_lvl = no_signal
delta_bic = 2*(np.array(maxima) - ground_lvl)
self.npc_bic = delta_bic #np.log10(delta_bic)
if kind_courbe == 'bic':
ax[0].plot(self.n_pcas, delta_bic,'o--')
# ax[0].axhline(1)
# ax[0].set_ylim(-10,100)
elif kind_courbe == 'abs':
ax[0].plot(self.n_pcas, max_val,'o--')
ax[0].set_ylabel(title)
def get_snr_2d(self, ccf2d=None, Kp_array=None, interp_grid=None, \
kp_limit=70, rv_limit=15, RV_sys=0, Kp0=151):
if interp_grid is None:
interp_grid = self.interp_grid
else:
self.interp_grid = interp_grid
if Kp_array is None:
Kp_array = self.Kp_array
else:
self.Kp_array = Kp_array
if ccf2d is None:
ccf2d = self.sum_ccf
else:
self.sum_ccf = ccf2d
self.curve_fct2d = interp2d(interp_grid, Kp_array, ccf2d)
idx_bruit_rv = (interp_grid < RV_sys - rv_limit) | (interp_grid > RV_sys + rv_limit)
idx_bruit_kp = (Kp_array < Kp0 - kp_limit) | (Kp_array > Kp0 + kp_limit)
self.idx_bruit_rv = idx_bruit_rv
self.idx_bruit_kp = idx_bruit_kp
self.idx_bruit_rv0 = idx_bruit_rv
bruit2d = np.ma.std(ccf2d[idx_bruit_kp][:, idx_bruit_rv])
self.bruit2d = bruit2d
snr2d = (ccf2d - np.ma.mean(ccf2d[idx_bruit_kp][:, idx_bruit_rv])) / \
np.ma.masked_invalid(bruit2d)
self.snr2d = snr2d
self.snr_fct2d = interp2d(interp_grid, Kp_array, snr2d)
def calc_correl_snr_2d(self, tr, icorr=None, limit_shift=60, interp_size=201, RV_sys=0,
kp0=0, kp1=2, rv_limit=15, kp_limit=70, RV_shift=0, vr_orb=None, vrp_kind='t'):
if isinstance(RV_sys, u.Quantity):
RV_sys = (RV_sys.to(u.km / u.s)).value
if icorr is None:
icorr = tr.icorr
self.icorr = icorr
self.Kp0 = (tr.Kp.to(u.km / u.s)).value
self.RV_shift = RV_shift
interp_grid = np.linspace(-limit_shift - 2 * (tr.vrp)[icorr][0] + RV_sys,
limit_shift - 2 * (tr.vrp)[icorr][-1] + RV_sys,
interp_size).squeeze()
self.interp_grid = interp_grid
Kp_array = np.arange(kp0, int(tr.Kp.value * kp1))
self.Kp_array = Kp_array
sum_ccf = np.zeros((Kp_array.size, interp_grid.size))
for i, Kpi in enumerate(Kp_array):
hm.print_static(i)
if vrp_kind == 'nu':
# print('nu')
vrp_orb = o.rv_theo_nu(Kpi, tr.nu[icorr]*u.rad, tr.planet.w, plnt=True).to(u.km/u.s)
elif vrp_kind == 't':
# print('t')
vrp_orb = o.rv_theo_t(Kpi, tr.t_start[icorr], tr.planet.mid_tr, tr.planet.period, plnt=True).to(u.km/u.s)
# vrp_orb = o.rv_theo_nu(Kpi, tr.nu[icorr] * u.rad, tr.planet.w, plnt=True).to(u.km/u.s)
if vr_orb is None:
vr_orb = (-vrp_orb*(tr.planet.M_pl/tr.planet.M_star).decompose()).to(u.km/u.s)
# vr_orb = tr.vr[icorr] #(Kpi*u.km/u.s/tr.planet.M_star*tr.planet.M_pl).to(u.km/u.s)
# print(vrp_orb.shape,vr_orb.shape,RV_shift[icorr].shape)
shifted_ccf = np.ma.masked_invalid(a.shift_correl(interp_grid, self.ccf[icorr],
self.rv_grid, (vrp_orb-vr_orb).value+RV_shift[icorr]))
sum_ccf[i] = np.ma.sum(shifted_ccf, axis=0)
self.sum_ccf = sum_ccf
self.curve_fct2d = interp2d(interp_grid, Kp_array, sum_ccf)
self.get_snr_2d(kp_limit=kp_limit, rv_limit=rv_limit, RV_sys=RV_sys,
Kp0 = (tr.Kp.to(u.km / u.s)).value)
sum_ccf_nonoise = sum_ccf[~self.idx_bruit_kp][:, ~self.idx_bruit_rv]
self.sum_ccf_nonoise = np.ma.masked_invalid(sum_ccf_nonoise)
self.idx_max = np.where(sum_ccf_nonoise == sum_ccf_nonoise.max())
def find_max(self, interp_grid=None, snr=None, kind='rv',
kind_max='spline', minmax='max', p0_estim=None):
if interp_grid is None:
interp_grid = self.interp_grid
if snr is None:
snr = self.snr
if kind == "rv":
idx_bruit = self.idx_bruit_rv0
elif kind == "kp":
idx_bruit = self.idx_bruit_kp
if kind_max == 'spline':
pos_max = a.find_max_spline(interp_grid[~idx_bruit], snr[~idx_bruit].copy(),
kind=minmax)
elif kind_max == 'gauss':
if p0_estim is None:
p0_estim = [np.std(snr[~idx_bruit]), np.nanmax(snr[~idx_bruit]),0]
pos_max, pos_err = a.find_max_gaussian(interp_grid[~idx_bruit], snr[~idx_bruit].copy(),
kind=minmax, p0_estim=p0_estim, error_gauss=True)
pos_max = (pos_max[2], -pos_max[1])
self.pos_err = pos_err[2]
self.max_err = pos_err[1]
# plt.figure()
# plt.plot(self.interp_grid, self.snr.copy())
# plt.plot(self.interp_grid[~self.idx_bruit_rv],
# self.snr[~self.idx_bruit_rv].copy())
# plt.axvline(pos_max[0])
# plt.axhline(-pos_max[1])
# print("max = ", -pos_max[1])
if kind == "rv":
self.pos = pos_max[0]
self.max = -pos_max[1]
elif kind == "kp":
self.pos_kp = pos_max[0]
self.max_kp = -pos_max[1]
def get_curve_at_slice(self, Kp_slice, **kwargs):
if isinstance(Kp_slice, u.Quantity):
Kp_slice = (Kp_slice.to(u.km / u.s)).value
self.snr = self.snr_fct2d(self.interp_grid, Kp_slice)
self.courbe = self.curve_fct2d(self.interp_grid, Kp_slice)
self.Kp_slice = Kp_slice
self.find_max( **kwargs)
def get_snr_1d(self, interp_grid=None, courbe=None, rv_limit=8,
RV_sys=0, plot=False, debug=False, max_rv=None, add_mask=None): #
if interp_grid is None:
interp_grid = self.interp_grid
else:
self.interp_grid = interp_grid
if courbe is None:
courbe = self.courbe
else:
self.courbe = courbe
if debug is True:
plt.figure()
plt.plot(interp_grid, self.courbe)
if max_rv is not None:
id_sub = (interp_grid <= max_rv) | (interp_grid >= -max_rv)
interp_grid = interp_grid[id_sub]
courbe = courbe[id_sub]
idx_bruit = (interp_grid < RV_sys - rv_limit) | (interp_grid > RV_sys + rv_limit)
idx_bruit0 = idx_bruit.copy()
if add_mask is not None:
idx_bruit &= ((interp_grid < add_mask[0] - add_mask[1]) | (interp_grid > add_mask[0] + add_mask[1]))
if plot is True:
plt.figure()
plt.plot(interp_grid, courbe)
plt.plot(interp_grid[idx_bruit], courbe[idx_bruit])
self.idx_bruit_rv = idx_bruit
self.idx_bruit_rv0 = idx_bruit0
bruit = np.nanstd(courbe[idx_bruit])
self.bruit = bruit
self.snr = (courbe - np.ma.mean(courbe[idx_bruit])) / np.ma.masked_invalid(bruit)
if plot is True:
plt.figure()
plt.plot(interp_grid, self.snr)
plt.plot(interp_grid[idx_bruit], self.snr[idx_bruit])
def calc_correl_snr_1d(self, tr, Kp=None, icorr=None, limit_shift=60, interp_size=201,
RV_sys=0, rv_limit=8, plot=True, RV = 0., RV_shift=0,
vrp_kind = 't', vrp_orb=None, vr_orb=None):
if isinstance(RV_sys, u.Quantity):
RV_sys = (RV_sys.to(u.km / u.s)).value
if icorr is None:
icorr = tr.icorr
if Kp is None:
Kp = tr.Kp.value
if vrp_orb is None:
if vrp_kind == 'nu':
vrp_orb = o.rv_theo_nu(Kp, tr.nu*u.rad, tr.planet.w, plnt=True).to(u.km/u.s)
elif vrp_kind == 't':
vrp_orb = o.rv_theo_t(Kp, tr.t_start, tr.planet.mid_tr, tr.planet.period, plnt=True).to(u.km/u.s)
if vr_orb is None:
vr_orb = (-vrp_orb*(tr.planet.M_pl/tr.planet.M_star).decompose()).to(u.km/u.s)
# dvr = o.rv_theo_nu(Kp, tr.nu * u.rad, tr.planet.w, plnt=True)
interp_grid = np.linspace(-limit_shift + RV_sys, limit_shift + RV_sys, interp_size).squeeze()
self.interp_grid = interp_grid
ccf_shifted = np.ma.masked_invalid(a.shift_correl(interp_grid, self.ccf,
self.rv_grid, (vrp_orb-vr_orb).value+RV_shift))
self.shifted_ccf = ccf_shifted
if plot is True:
self.plot_PRF(tr, RV=RV)
# idx_mid = hm.nearest(self.interp_grid, RV)
# colum3 = np.ma.mean(self.shifted_ccf[:,idx_mid-1:idx_mid+2],axis=-1)
# fig = plt.figure()
# fig.set_figheight(6)
# fig.set_figwidth(10)
# spec = gridspec.GridSpec(ncols=2, nrows=1, width_ratios=[3, 1])
# ax0 = fig.add_subplot(spec[0])
# ax0.pcolormesh(self.interp_grid, np.arange(tr.n_spec)[icorr], self.shifted_ccf[icorr])
# ax0.axvline(RV, alpha=0.5, color='k')
# ax1 = fig.add_subplot(spec[1])
# ax1.plot(colum3[icorr], np.arange(tr.n_spec)[icorr],'o-')
# ax1.set_xlim(colum3[icorr].min()*1.5, colum3[icorr].max()*1.5)
self.courbe = np.ma.sum(ccf_shifted[icorr], axis=0)
self.get_snr_1d(rv_limit=rv_limit, RV_sys=RV_sys)
def find_1sig(self, limit_rv=5., step=1.):
def f(x, lvl):
return fct_snr(x) - lvl
fct_snr=interp1d(self.interp_grid, self.snr, kind='cubic')
down, up = fsolve(f, self.pos-limit_rv, args=(self.max-step)), fsolve(f, self.pos+limit_rv, args=(self.max-step))
print(r'{:.1f} + {:.1f} - {:.1f}'.format(self.pos, (up-self.pos)[0], (self.pos-down)[0]))
return up-self.pos, self.pos-down
def find_1sig_kp(self, limit_rv=15):
self.snr_kp = self.snr_fct2d(self.pos, self.Kp_array).squeeze()
self.find_max(interp_grid=self.Kp_array, snr=self.snr_kp, kind='kp')
# plt.figure()
# plt.plot(self.Kp_array, self.snr_kp)
def f(x, lvl):
return fct_snr(x) - lvl
fct_snr=interp1d(self.Kp_array, self.snr_kp, kind='cubic')
down, up = fsolve(f, self.pos_kp-limit_rv, args=(self.max_kp-1)), \
fsolve(f, self.pos_kp+limit_rv, args=(self.max_kp-1))
print(r'{:.0f} + {:.0f} - {:.0f}'.format(self.pos_kp, (up-self.pos_kp)[0], (self.pos_kp-down)[0]))
return up-self.pos_kp, self.pos_kp-down
def full_plot(self, tr, icorr, wind=None, fit_gauss=False,
fig_larg=8, fig_haut=3, cmap='plasma', fig_name='', tag_max=False,
Kp_slice=None, clim=None, get_logl=False, hline=None, save_fig='', show_legend=True):
figs, (ax1,ax0,ax2) = plt.subplots(3, 1, figsize=(fig_larg, 3 * fig_haut), sharex=False)
axi = [ax0,ax2]
##### LIGNE VERTICALE À VSYS=0 EN POINTILLÉ
###### LIGNE VERTICALE EN TRait plein avec un trou (comme la horizontale)
######## + LIGNE HORIZONTALE EN POINTILLÉE POUR SÉPARER LES 2 TRANSITS
### --- CCF ---
if get_logl is True:
yyy = a.remove_means(self.ccf0[:,None,:], 1).squeeze()
else:
yyy = self.ccf0
im1 = ax1.pcolormesh(self.rv_grid, tr.phase.value, yyy, cmap=cmap, rasterized=True)
if clim is not None:
im1.set_clim(clim[0],clim[1])
ax1.plot((tr.berv-tr.planet.RV_sys.value), tr.phase.value, '--',color='darkred', alpha=0.8, label='BERV')
# ax1.set_xlim(self.interp_grid[0], self.interp_grid[-1])
ax1.set_ylabel(r'$\phi$', fontsize=14)
if fig_name != '':
ax1.set_title(fig_name, fontsize=16)
divider = make_axes_locatable(ax1)
cax = divider.append_axes('right', size='2%', pad=0.05)
cbar = figs.colorbar(im1, ax=ax1, cax=cax)
cbar.set_label('CCF', fontsize=14)
iout = np.delete(np.arange(tr.vrp.size), icorr)
ax1.plot(tr.vrp[iout]+self.RV_shift[iout]+tr.mid_vrp, tr.phase.value[iout],
'k.', alpha=0.5, label=r'Expected $v_{\rm P}$')
ax1.plot(tr.vrp+self.RV_shift+tr.mid_vrp, tr.phase.value,
'k', alpha=0.35, label=r'Expected $v_{\rm P}$')
if hline is not None:
ax1.axhline(hline, color='white', linestyle='-')
# --- SUM CCF 2D ---
# imaxi2 = axi[0].imshow(self.snr2d, origin='lower', aspect='auto',
# extent=(self.interp_grid.min(), self.interp_grid.max(),
# self.Kp_array.min(), self.Kp_array.max()), cmap=cmap)
imaxi2 = axi[0].pcolormesh(self.interp_grid, self.Kp_array, self.snr2d, cmap=cmap, rasterized=True)
axi[0].set_ylabel(r'$K_{\rm P}$ (km s$^{-1}$)', fontsize=14)
self.get_curve_at_slice(self.Kp_array[~self.idx_bruit_kp][self.idx_max[0]])
print('Highest SNR = {} // Kp = {} // RV = {} '.format(self.max,
self.Kp_array[~self.idx_bruit_kp][self.idx_max[0]],
self.pos))
self.get_curve_at_slice(tr.Kp)
print(r'Max SNR = {:.2f}$\sigma$, Max position = {:.2f}'.format(self.max, self.pos))
print('')
# hm.printmd(r'Max SNR = **{:.2f}**$\sigma$, Max position = {:.2f}'.format(self.max, self.pos))
# print('')
if tag_max is True:
axi[0].plot(self.pos, tr.Kp.to(u.km / u.s).value,'k+',
label='Pos = {:.2f} // Max = {:.2f}'.format(self.pos, self.max))
axi[0].legend(loc='best')
axi[0].axhline(tr.Kp.to(u.km / u.s).value, linestyle='-', alpha=0.7, color='indigo',
xmin=0,xmax=hm.nearest(self.interp_grid, self.pos-10)/self.interp_grid.size )
axi[0].axhline(tr.Kp.to(u.km / u.s).value, linestyle='-', alpha=0.7, color='indigo',
xmin=hm.nearest(self.interp_grid, self.pos+10)/self.interp_grid.size ,xmax=1)
# axi[0].axvline(self.pos, linestyle='-', alpha=0.7, color='indigo',
# ymin=0, ymax=(self.Kp_array[~self.idx_bruit_kp][self.idx_max[0]]-50)/self.Kp_array[-1] )
# axi[0].axvline(self.pos, linestyle='-', alpha=0.7, color='indigo',
# ymin=(self.Kp_array[~self.idx_bruit_kp][self.idx_max[0]]+50)/self.Kp_array[-1] , ymax=1)
axi[0].axvline(self.pos, linestyle='-', alpha=0.7, color='indigo',
ymin=0, ymax=(tr.Kp.value - 60)/self.Kp_array[-1] )
axi[0].axvline(self.pos, linestyle='-', alpha=0.7, color='indigo',
ymin=(tr.Kp.value + 60)/self.Kp_array[-1] , ymax=1)
axi[0].axvline(0, color='indigo', linestyle=':')
axi[0].set_ylim(0, self.Kp_array[-1])
if wind is not None:
if wind != 0 :
axi[0].axvline(wind, linestyle=':', color='dodgerblue', label=r'Wind')
divider = make_axes_locatable(axi[0])
cax = divider.append_axes('right', size='2%', pad=0.05)
cbar = figs.colorbar(imaxi2, ax=axi[0], cax=cax)
if clim is not None:
imaxi2.set_clim(clim[0],clim[1])
cbar.set_label('CCF SNR', fontsize=14)
### --- Sum CCF 1D ---
axi[1].plot(self.interp_grid, self.snr.squeeze(), color='indigo', label='Expected Kp')
axi[1].axvline(0, color='indigo', alpha=0.5, linestyle=':', label=r"Planet Rest frame")
# axi[1].axvline(self.pos, color='indigo', alpha=0.5, linestyle='-')
axi[1].axhline(0, color='indigo', linestyle=':', alpha=0.5)
if wind is not None:
if wind != 0:
axi[1].axvline(wind, linestyle=':', color='dodgerblue',
label=str(wind) + ' km/s Wind')
axi[1].set_ylabel('CCF SNR', fontsize=14)
axi[1].set_xlabel(r'$v_{\rm offset}$ (km s$^{-1}$)', fontsize=14)
if clim is not None:
axi[1].set_ylim(clim[0],clim[1])
if Kp_slice is not None:
if isinstance(Kp_slice,list):
Kp_slice = np.array(Kp_slice)
else:
Kp_slice = [Kp_slice]
for k,kpslice in enumerate(Kp_slice):
if isinstance(kpslice, u.Quantity):
kpslice = (kpslice.to(u.km / u.s)).value
if (kpslice < 0):
axi[0].set_ylim(self.Kp_array[0], self.Kp_array[-1])
vrp_orb_slice = o.rv_theo_nu(kpslice, tr.nu * u.rad, tr.planet.w, plnt=True)
ax1.plot(vrp_orb_slice[iout].value, tr.phase.value[iout],
'.', color=(0.3,0, (k+1)/len(Kp_slice)), alpha=0.5, label=r'Kp = {:.2f}'.format(kpslice))
axi[0].axhline(kpslice, linestyle='--', color=(0.3,0, (k+1)/len(Kp_slice)),
label='Kp = {:.2f}'.format(kpslice))
self.get_curve_at_slice(kpslice)
print('Kp Slice = {:.2f} // SNR = {} // RV = {} '.format(kpslice, self.max, self.pos))
axi[1].plot(self.interp_grid, self.snr.squeeze(), color=(0.3,0, (k+1)/len(Kp_slice)),
label='Kp = {:.2f}'.format(kpslice), linestyle='--')
# axi[0].legend(loc='lower left')
# axi[1].legend(loc='lower left')
if show_legend is True:
ax1.legend(loc='best')
figs.tight_layout(pad=1.0)
pos = ax0.get_position()
pos2 = ax2.get_position()
ax2.set_position([pos.x0,pos2.y0,pos.width-0.02, pos2.height])
if save_fig != '':
figs.savefig('/home/boucher/spirou/Figures/CCF_'+save_fig+'.pdf')
# fig.savefig('/home/boucher/spirou/Figures/STEPS'+fig_name+'.pdf')
def fit_gauss_1d(self, Kp=None):
if ~hasattr(self, 'snr'):
if Kp is None:
Kp = self.Kp0
print('Taking slice at {} km/s'.format(Kp))
self.get_curve_at_slice(Kp)
plt.figure()
plt.plot(self.interp_grid, self.snr)
plt.plot(self.interp_grid[~self.idx_bruit_rv], self.snr[~self.idx_bruit_rv])
max_val = self.snr[~self.idx_bruit_rv].max()
try:
param, pcov = curve_fit(a.gauss, self.interp_grid[~self.idx_bruit_rv],
self.snr[~self.idx_bruit_rv], p0=[3, 1, 0])
err_param = np.sqrt(np.diag(pcov))
print('Gauss fit : RV = {:.3f} +/- {:.3f} // Ampli = {:.3f}'.format(param[2],
err_param[2], param[1]))
plt.plot(self.interp_grid[20:-20], a.gauss(self.interp_grid[20:-20], *param),
color='c', label='Gaussian fit', alpha=0.5)
except RuntimeError:
print('Could not fit a gaussian')
# def fit_gauss_1d_kp(self, rv=None):
# if ~hasattr(self, 'pos'):
# if rv is None:
# rv = self.pos
# else:
# rv = 0
# print('Taking slice at {} km/s'.format(rv))
# self.snr_kp = self.snr_fct2d(rv, self.Kp_array)
# pos_max = a.find_max_spline(self.Kp_array[~self.idx_bruit_kp],
# self.snr_kp[~self.idx_bruit_kp].copy(), kind='max')
# self.pos_kp = pos_max[0]
# self.max_kp = -pos_max[1]
# plt.figure()
# plt.plot(self.Kp_array, self.snr_kp)
# plt.plot(self.Kp_array[~self.idx_bruit_kp], self.snr[~self.idx_bruit_kp])
# max_val = self.snr_kp[~self.idx_bruit_kp].max()
# try:
# param, pcov = curve_fit(a.gauss, self.kp_array[~self.idx_bruit_kp],
# self.snr_kp[~self.idx_bruit_kp], p0=[3, 1, 0])
# err_param = np.sqrt(np.diag(pcov))
# print('Gauss fit : Kp = {:.3f} +/- {:.3f} // Ampli = {:.3f}'.format(param[2],
# err_param[2], param[1]))
# plt.plot(self.Kp_array[20:-20], a.gauss(self.Kp_array[20:-20], *param),
# color='c', label='Gaussian fit', alpha=0.5)
# except RuntimeError:
# print('Could not fit a gaussian')
def fit_gauss_2d(self, get_theta=False, limit_rv=10, limit_kp=70):
abs_max_pos_kp = self.Kp_array[~self.idx_bruit_kp][self.idx_max[0]]
abs_max_pos_rv = self.interp_grid[~self.idx_bruit_rv][self.idx_max[1]]
idx_bruit_rv = (self.interp_grid < abs_max_pos_rv - limit_rv) | (self.interp_grid > abs_max_pos_rv + limit_rv)
idx_bruit_kp = (self.Kp_array < abs_max_pos_kp - limit_kp) | \
(self.Kp_array > abs_max_pos_kp + limit_kp)
x = self.interp_grid[~idx_bruit_rv]
y = self.Kp_array[~idx_bruit_kp]
fct_x = interp1d(np.arange(x.size),x, fill_value='extrapolate')
fct_y = interp1d(np.arange(y.size),y, fill_value='extrapolate')
Xin, Yin = np.mgrid[0:x.size, 0:y.size]
data = self.snr2d[~idx_bruit_kp][:, ~idx_bruit_rv]
max_val = data.max()
params, err_params = a.fitgaussian(data, get_theta=get_theta)
if get_theta is True:
fit = a.gaussian2(*params)
else:
fit = a.gaussian(*params)
xc, yc = fct_x(params[2]), fct_y(params[1])
# sig_x, sig_y = np.abs(params[4] * np.diff(x)[0]), np.abs(params[3] * np.diff(y)[0])
err_x = err_params[2]* np.diff(x)[0]
err_y = err_params[1]* np.diff(y)[0]
# try:
print('Gauss 2d fit : RV = {:.3f} +/- {:.3f} // Kp = {:.3f} +/- {:.3f} '.format(xc, err_x, \
yc, err_y))
print('Amp = {:.2f} +/- {:.2f}'.format(params[0], err_params[0]))
print('x0 = {:.2f} +/- {:.2f}'.format(params[2], err_params[2]))
print('y0 = {:.2f} +/- {:.2f}'.format(params[1], err_params[1]))
print('sig_x = {:.2f} +/- {:.2f}'.format(params[4], err_params[4]))
print('sig_y = {:.2f} +/- {:.2f}'.format(params[3], err_params[3]))
print('offset = {:.2f} +/- {:.2f}'.format(params[5], err_params[5]))
fig, ax = plt.subplots(1,2, figsize=(10,6), sharey=True) #plt.figure()
ax[0].pcolormesh(x,y,data)
ax[0].axhline(y[hm.nearest(y, self.Kp0)], linestyle='--', alpha=0.5)
ax[0].axhline(yc, linestyle='-', alpha=0.7)
ax[0].axvline(xc, linestyle='-', alpha=0.7)
ax[1].pcolormesh(x,y,fit(*np.indices(data.shape)))
ax[1].axhline(y[hm.nearest(y, self.Kp0)], linestyle='--', alpha=0.5)
ax[1].axhline(yc, linestyle='-', alpha=0.7)
ax[1].axvline(xc, linestyle='-', alpha=0.7)
ax[0].set_xlabel(r'$v_{\rm offset}$ (km s$^{-1}$)')
ax[0].set_ylabel(r'$K_{\rm P}$ (km s$^{-1}$)')
ax[1].set_xlabel(r'$v_{\rm rad}$ (km s$^{-1}$)')
# x1, y1 = centroid_com(data)
x2, y2 = centroid_quadratic(data)
# x3, y3 = centroid_2dg(data)
marker = '+'
ms, mew = 15, 2.
ax[0].plot(fct_x(x2), fct_y(y2), color='white', marker=marker, ms=ms, mew=mew)
ax[1].plot(fct_x(x2), fct_y(y2), color='white', marker=marker, ms=ms, mew=mew)
plt.figure()
plt.matshow(data, cmap=plt.cm.gist_earth_r, aspect=0.2)
plt.axhline(hm.nearest(y, self.Kp0), linestyle='-', alpha=0.5)
# plt.axvline(hm.nearest(x, RV_sys.value), linestyle='-', alpha=0.5)
plt.contour(fit(*np.indices(data.shape)), cmap=plt.cm.copper)
plt.axhline(params[1], linestyle=':', alpha=0.5)
plt.axvline(params[2], linestyle=':', alpha=0.5)
# plt.invert_yaxis()
plt.figure()
plt.matshow(data, cmap=plt.cm.gist_earth_r, aspect=0.2)
plt.axhline(hm.nearest(y, self.Kp0), linestyle='-', alpha=0.5)
# plt.axvline(hm.nearest(x, RV_sys.value), linestyle='-', alpha=0.5)
plt.contour(fit(*np.indices(data.shape)), cmap=plt.cm.copper)
plt.axhline(params[1], linestyle=':', alpha=0.5)
plt.axvline(params[2], linestyle=':', alpha=0.5)
# plt.invert_yaxis()
def calc_ccf2d(self, tr, ccf=None, kind='logl_corr', id_pc=None,
remove_mean=False, debug=False, index=None, orders=None):
if orders is None:
orders = np.arange(49)
if ccf is None:
if kind == "shift":
ccf = self.shifted_ccf
elif kind == 'logl_corr':
ccf = np.ma.masked_invalid(np.ma.sum(self.data[:, orders], axis=1)).squeeze()
print(ccf.shape)
elif kind == "logl_sig":
nolog_L_sig = np.ma.masked_invalid(np.ma.sum(self.data, axis=1)).squeeze()
ccf = corr.nolog2log(nolog_L_sig, tr.N, sum_N=True).squeeze()
remove_mean = True
if remove_mean is True:
if ccf.ndim == 2:
ccf = ccf-np.nanmedian(ccf,axis=1)[:,None]
elif ccf.ndim == 3:
ccf = ccf-np.nanmedian(ccf,axis=1)[:,None, :]
if ccf.ndim == 3:
if id_pc is None:
ccf = ccf[:,:,0]
else:
ccf = ccf[:,:,id_pc]
# ccf = ccf-np.nanmedian(ccf,axis=-1)[:,None]
# print(ccf.shape)
if debug is True:
plt.figure()
plt.pcolormesh(ccf)
if index is not None:
ccf[index] = 0
self.map_prf = ccf
# print(ccf.mean())
return ccf
def plot_PRF(self, tr, interp_grid=None, ccf=None, orders=None, RV=0., icorr=None, split_fig=[0], peak_center=None,
hlines=None, texts=None, kind='logl_corr', index=None, snr_1d=None, labels=None, clim=None,
fig_name='', extension='.pdf', id_pc=None, map_kind='snr', debug=False, remove_mean=False,
minus_kp=False, figwidth=10):
# if ccf is None:
# if kind == "shift":
# ccf = self.shifted_ccf
# elif kind == 'logl_corr':
# ccf = np.ma.masked_invalid(np.ma.sum(self.data, axis=1)).squeeze()
# # if nolog_L.ndim == 2:
# # ccf = 1/np.ma.sum(tr.N, axis=1)[:,None]*nolog_L
# # elif nolog_L.ndim == 3:
# # ccf = 1/np.ma.sum(tr.N, axis=1)[:,None,None]*nolog_L
# print(ccf.shape)
# elif kind == "logl_sig":
# nolog_L_sig = np.ma.masked_invalid(np.ma.sum(self.data, axis=1)).squeeze()
# ccf = corr.nolog2log(nolog_L_sig, tr.N, sum_N=True).squeeze()
# remove_mean = True
# if remove_mean is True:
# if ccf.ndim == 2:
# ccf = ccf-np.nanmedian(ccf,axis=1)[:,None]
# elif ccf.ndim == 3:
# ccf = ccf-np.nanmedian(ccf,axis=1)[:,None, :]
# if ccf.ndim == 3:
# if id_pc is None:
# ccf = ccf[:,:,0]
# else:
# ccf = ccf[:,:,id_pc]
# # ccf = ccf-np.nanmedian(ccf,axis=-1)[:,None]
# # print(ccf.shape)
# if debug is True:
# plt.figure()
# plt.pcolormesh(ccf)
# self.map_prf = ccf
# print(ccf.mean())
ccf = self.calc_ccf2d(tr, ccf=ccf, kind=kind, id_pc=id_pc,
remove_mean=remove_mean, index=index, orders=orders)
if interp_grid is None:
interp_grid = self.interp_grid
if peak_center is not None:
nb_pix = np.round(peak_center/2.3)
peak_rv = np.arange(-nb_pix*2.3 + self.pos, (nb_pix+1)*2.3 + self.pos, 2.3)
peak_ccf = np.ones((tr.n_spec, peak_rv.size))*np.nan
for n in range(tr.n_spec):
fct = interp1d(self.rv_grid, ccf[n])
peak_ccf[n] = fct(peak_rv)
interp_grid = peak_rv
# rv_grid = peak_rv
ccf = peak_ccf
idx_bruit_rv = (interp_grid < RV - 15) | (interp_grid > RV + 15)
else:
idx_bruit_rv = self.idx_bruit_rv
# if icorr is None:
# icorr = np.arange(tr.n_spec)
idx_mid = hm.nearest(interp_grid, RV)
colum3 = np.ma.mean(ccf[:,idx_mid-1:idx_mid+2]/np.nanstd(ccf[:,idx_bruit_rv]),axis=-1)
if index is not None:
colum3[index] = np.nan
colum3 = np.ma.masked_invalid(colum3)
x = interp_grid
if len(split_fig) > 1:
y = []
z = []
id_range = []
for i in range(len(split_fig))[1:][::-1]:
id_range.append([split_fig[i-1],split_fig[i]])
# print(id_range)
y.append(tr.phase[split_fig[i-1]:split_fig[i]]) #np.arange(tr.n_spec)[:split_fig]
if map_kind == 'snr':
z.append(ccf[split_fig[i-1]:split_fig[i]]/ \
np.nanstd(ccf[:,idx_bruit_rv][split_fig[i-1]:split_fig[i]]))
if map_kind == 'curve':
z.append(ccf[split_fig[i-1]:split_fig[i]])
else:
y=tr.phase #np.arange(split_fig, tr.n_spec)-split_fig
if map_kind == 'snr':
z=ccf/np.nanstd(ccf[:,idx_bruit_rv])
if map_kind == 'curve':
z=ccf
idx_mid = hm.nearest(interp_grid, RV)
colum3 = np.ma.mean(ccf[:,idx_mid-1:idx_mid+2]/np.nanstd(ccf[:,idx_bruit_rv]),axis=-1)
bin_ccf = spectrum.box_binning(colum3, box_size=3)
if len(split_fig) > 1:
height = []
for i in range(len(split_fig)-1):
height.append(np.sum(tr.dt[id_range[i][0]:id_range[i][1]]).to(u.h).value)
height.append(height[0]/2)
fig = plt.figure(constrained_layout=True, figsize=(figwidth,8))
gs = fig.add_gridspec(len(split_fig), 3, width_ratios=[3, 3, 1.3], height_ratios=height)
ax_map = []
ax_ccf = []
for i in range(len(split_fig)-1):
# print(i)
curr_ax_map = fig.add_subplot(gs[i, :-1])
ax_map.append(curr_ax_map)
ax_ccf.append( fig.add_subplot(gs[i, -1], sharey=curr_ax_map))
ax_snr = fig.add_subplot(gs[-1, :-1], sharex=ax_map[0])
else:
fig = plt.figure(constrained_layout=True, figsize=(10,6))
gs = fig.add_gridspec(2, 2, width_ratios=[3, 1.1],
height_ratios=[3, 1])
ax_map = fig.add_subplot(gs[0, :-1])
ax_ccf = fig.add_subplot(gs[0, -1], sharey=ax_map)
ax_snr = fig.add_subplot(gs[-1, :-1], sharex=ax_map)
berv_rv = -tr.vrp-tr.mid_vrp-tr.RV_sys-(tr.mid_vr+tr.vr)-(tr.mid_berv+tr.berv)
if len(split_fig) > 1:
for i in range(len(split_fig))[:-1]: