/
TESS-LS.py
415 lines (332 loc) · 14.6 KB
/
TESS-LS.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
# This code retrieves TESS light curves for an object given its TIC number.
# It will combine data for all existing sectors and perform a period search
# using a Lomb-Scargle periodogram.
# It all searches for the object in Gaia DR2.
# The output is a plot showing the periodogram, the Gaia CMD, and the phase-
# folded light to both the period and twice the period (useful for binary
# systems where the dominant peak is often an alias).
__version__ = '2.0'
__author__ = 'Ingrid Pelisoli'
##### IMPORTING PACKAGES ######
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from astropy.io import ascii
from astropy.io import fits
from astropy.io.votable import parse_single_table
from astroquery.mast import Observations
from astroquery.gaia import Gaia
from astropy import wcs
from astropy.coordinates import SkyCoord, Distance, Angle
from astropy.time import Time
from astropy.visualization.mpl_normalize import ImageNormalize
import astropy.visualization as stretching
from lightkurve import search_targetpixelfile
import astropy.units as u
import sys
import TESSutils as tul
################################
### FUNCTION FOR PLOTTING ####
def make_plot(f, pow, fap, bjd0, flux0, bjd, flux, phi,
flux_phi, fit, phi2, flux_phi2, fit2, period, crowd, ns):
fig = plt.figure(figsize=(24,15))
plt.rcParams.update({'font.size': 22})
gridspec.GridSpec(6,10)
plt.subplot2grid((6,10), (0,0), colspan=2, rowspan=2)
plt.title('TIC %d'%(TIC))
mean_tpf = np.mean(tpf.flux.value,axis=0)
nx,ny = np.shape(mean_tpf)
norm = ImageNormalize(stretch=stretching.LogStretch())
division = int(np.log10(np.nanmax(np.nanmean(tpf.flux.value,axis=0))))
plt.imshow(np.nanmean(tpf.flux.value,axis=0)/10**division,norm=norm,extent=[tpf.column,tpf.column+ny,tpf.row,tpf.row+nx],origin='lower', zorder=0)
plt.xlim(tpf.column,tpf.column+10)
plt.ylim(tpf.row,tpf.row+10)
if not warning:
x = coords[:, 0]+tpf.column+0.5
y = coords[:, 1]+tpf.row+0.5
plt.scatter(x, y, c='firebrick', alpha=0.5, edgecolors='r', s=sizes)
plt.scatter(x, y, c='None', edgecolors='r', s=sizes)
plt.scatter(x[idx], y[idx], marker='x', c='white')
plt.text(tpf.column, tpf.row, 'crowdsap = %4.2f' % np.mean(crowd), color='w')
plt.ylabel('Pixel count')
plt.xlabel('Pixel count')
plt.subplot2grid((6,10), (0,2), colspan=2, rowspan=2)
plt.scatter(s_bprp, s_MG, c='0.75', s=0.5, zorder=0)
if (len(gaia)>1):
plt.scatter(bprp_all, MG_all, marker='s',c='b', s=10, zorder=1)
plt.gca().invert_yaxis()
plt.title('$Gaia$ HR-diagram')
if not warning:
plt.plot(bprp, MG, 'or',markersize=10,zorder=2)
plt.ylabel('$M_G$')
plt.xlabel('$G_{BP}-G_{RP}$')
plt.subplot2grid((6,10), (2,0), colspan=4, rowspan=2)
plt.title("Period = %5.2f h"%period)
plt.plot(1.0/f, pow, color ='k')
plt.xlim(min(1.0/f), max(1.0/f))
plt.axhline(fap, color='b')
plt.axvline(period, color='r', ls='--', zorder=0)
#plt.axvspan(100., max(1.0/freq), alpha=0.5, color='red')
plt.xscale('log')
plt.xlabel('P [h]')
plt.ylabel('Power')
plt.subplot2grid((6,10), (4,0), colspan=4, rowspan=2)
plt.title('%s sector/s'% ns)
plt.xlabel("BJD - 2457000")
plt.ylabel('Relative flux')
plt.xlim(np.min(bjd), np.max(bjd))
plt.scatter(bjd0, flux0, c='0.25', zorder=1, s = 0.5)
plt.scatter(bjd, flux, c='k', zorder=1, s = 0.5)
phi_avg = tul.avg_array(phi,50)
fphi_avg = tul.avg_array(flux_phi,50)
plt.subplot2grid((6,10), (0,4), colspan=6, rowspan=3)
plt.title('Phased to dominant peak')
plt.xlabel('Phase')
plt.ylabel('Relative flux')
plt.xlim(0,2)
#plt.errorbar(phi, flux_phi, fmt='.', color='0.5', markersize=0.75, elinewidth=0.5, zorder=0)
plt.scatter(phi_avg, fphi_avg, marker='.', color='0.5', zorder=0)
plt.plot(tul.running_mean(phi_avg,15), tul.running_mean(fphi_avg,15),'.k', zorder=1)
plt.plot(phi, fit, 'r--', lw = 3, zorder=2)
#plt.errorbar(phi+1.0, flux_phi, fmt='.', color='0.5', markersize=0.75, elinewidth=0.5, zorder=0)
plt.scatter(phi_avg+1.0, fphi_avg, marker='.', color='0.5', zorder=0)
plt.plot(tul.running_mean(phi_avg,15)+1.0, tul.running_mean(fphi_avg,15),'.k', zorder=1)
plt.plot(phi + 1.0, fit,'r--', lw = 3, zorder=2)
phi_avg = tul.avg_array(phi2,50)
fphi_avg = tul.avg_array(flux_phi2,50)
plt.subplot2grid((6,10), (3,4), colspan=6, rowspan=3)
plt.title('Phased to twice the peak')
plt.xlabel('Phase')
plt.ylabel('Relative flux')
plt.xlim(0,2)
#plt.errorbar(phi2, flux_phi2, fmt='.', color='0.5', markersize=0.75, elinewidth=0.5, zorder=0)
plt.scatter(phi_avg, fphi_avg, marker='.', color='0.5', zorder=0)
plt.plot(tul.running_mean(phi_avg,15), tul.running_mean(fphi_avg,15),'.k', zorder=1)
plt.plot(phi2, fit2, 'r--', lw = 3, zorder=2)
#plt.errorbar(phi2+1.0, flux_phi2, fmt='.', color='0.5', markersize=0.75, elinewidth=0.5, zorder=0)
plt.scatter(phi_avg+1.0, fphi_avg, marker='.', color='0.5', zorder=0)
plt.plot(tul.running_mean(phi_avg,15)+1.0, tul.running_mean(fphi_avg,15),'.k', zorder=1)
plt.plot(phi2 + 1.0, fit2, 'r--', lw = 3, zorder=2)
plt.tight_layout()
return fig
################################
######### USER INPUT #########
# Define the object name using the TIC
TIC = int(sys.argv[1])
obj_name = "TIC " + str(TIC)
# Output ascii light curve?
flag_lc = int(input("Would you like an ascii file of the processed light curve?\n0 = no, 1 = yes: "))
# Output ascii periodogram?
flag_ls = int(input("Would you like an ascii file of the Lomb-Scargle periodogram?\n0 = no, 1 = yes: "))
# Output ascii phase?
flag_ph = int(input("Would you like an ascii file of the phased data?\n0 = no, 1 = yes: "))
# Is the period actually 2*P?
flag_p2 = int(input("Would you like to multiply the period by two?\n"
"(useful for ellipsoidal variables and some eclipsing systems)\n"
"0 = no, 1 = yes: "))
################################
####### DOWNLOAD DATA ########
# Searching for data at MAST
obsTable = Observations.query_criteria(dataproduct_type="timeseries",
project="TESS",
target_name=TIC)
# Download the 2-minute cadence light curves
try:
data = Observations.get_product_list(obsTable)
except:
log = open('TIC%09d_NO_DATA.log'%(TIC), "w")
log.write("No data found for TIC %09d\n"%(TIC))
log.close()
sys.exit()
download_lc = Observations.download_products(data, productSubGroupDescription="LC")
infile = download_lc[0][:]
n_slow = len(infile)
print("I have found a total of " + str(len(infile)) + " 2-min light curve(s).")
# Download the 20-second cadence light curves
download_fast_lc = Observations.download_products(data,
productSubGroupDescription="FAST-LC")
if download_fast_lc is None:
print("I have found no 20-sec light curves.")
fast = False
else:
infile_fast = download_fast_lc[0][:]
n_fast = len(infile_fast)
print("I have found a total of " + str(len(download_fast_lc[0][:])) + " 20-sec light curve(s).")
fast = True
# Dowload target pixel file for plotting
tpf = search_targetpixelfile("TIC "+str(TIC), mission='TESS').download()
################################
######### GAIA MATCH #########
# First do a large search using 6 pixels
coord = SkyCoord(ra=obsTable[0]['s_ra'], dec=obsTable[0]['s_dec'],
unit=(u.degree, u.degree), frame='icrs')
radius = u.Quantity(126.0, u.arcsec)
q = Gaia.cone_search_async(coord, radius)
gaia = q.get_results()
# Select only those brighter than 18.
gaia = gaia[gaia['phot_g_mean_mag'] < 18.]
gaia = gaia[ np.nan_to_num(gaia['parallax']) > 0 ]
warning = (len(gaia) == 0)
# Then propagate the Gaia coordinates to 2000, and find the best match to the
# input coordinates
if not warning:
ra2015 = np.array(gaia['ra']) * u.deg
dec2015 = np.array(gaia['dec']) * u.deg
parallax = np.array(gaia['parallax']) * u.mas
pmra = np.array(gaia['pmra']) * u.mas/u.yr
pmdec = np.array(gaia['pmdec']) * u.mas/u.yr
c2015 = SkyCoord(ra=ra2015, dec=dec2015,
distance=Distance(parallax=parallax, allow_negative=True),
pm_ra_cosdec=pmra, pm_dec=pmdec,
obstime=Time(2015.5, format='decimalyear'))
c2000 = c2015.apply_space_motion(dt=-15.5 * u.year)
idx, sep, _ = coord.match_to_catalog_sky(c2000)
# All objects
id_all = gaia['source_id']
plx_all = np.array(gaia['parallax'])
g_all = np.array(gaia['phot_g_mean_mag'])
MG_all = 5 + 5*np.log10(plx_all/1000) + g_all
bprp_all = np.array(gaia['bp_rp'])
id_all = np.array(id_all)
g_all = np.array(gaia['phot_g_mean_mag'])
MG_all = np.array(MG_all)
bprp_all = np.array(bprp_all)
# The best match object
best = gaia[idx]
gaia_id = best['source_id']
MG = 5 + 5*np.log10(best['parallax']/1000) + best['phot_g_mean_mag']
bprp = best['bp_rp']
gaia_id = int(gaia_id)
G = float(best['phot_g_mean_mag'])
MG = float(MG)
bprp = float(bprp)
# Coordinates for plotting
radecs = np.vstack([c2000.ra, c2000.dec]).T
coords = tpf.wcs.all_world2pix(radecs, 0.5)
sizes = 128.0 / 2**((g_all-best['phot_g_mean_mag']))
# Reference sample
table = parse_single_table("SampleC.vot")
data = table.array
s_MG = 5 + 5*np.log10(table.array['parallax']/1000) + table.array['phot_g_mean_mag']
s_bprp = table.array['bp_rp']
################################
####### 2-MINUTE DATA ########
slow_lc = tul.LCdata(TIC)
slow_lc.read_data(infile)
BJD_or = slow_lc.bjd
flux_or = slow_lc.flux
slow_lc.clean_data()
if (flag_lc == 1):
ascii.write([slow_lc.bjd, slow_lc.flux, slow_lc.flux_err],
'TIC%09d_lc.dat'%(TIC), names=['BJD','RelativeFlux','Error'],
overwrite=True)
# Calculates the periodogram
slow_lc.periodogram()
if (flag_ls == 1):
ascii.write([1/slow_lc.freq, slow_lc.power], 'TIC%09d_ls.dat'%(TIC),
names=['Period[h]','Power'], overwrite=True)
# Folds the data to the dominant peak
slow_lc.phase_data(1.0)
phase = slow_lc.phase
flux_phased = slow_lc.flux_phased
flux_err_phased = slow_lc.flux_err_phased
flux_fit = slow_lc.flux_fit
amp = slow_lc.amp
# Folds the data to twice the dominant peak
slow_lc.phase_data(2.0)
phase2 = slow_lc.phase
flux_phased2 = slow_lc.flux_phased
flux_err_phased2 = slow_lc.flux_err_phased
flux_fit2 = slow_lc.flux_fit
amp2 = slow_lc.amp
if (flag_ph == 1):
if (flag_p2 == 1):
ascii.write([phase2, flux_phased2, flux_err_phased2], 'TIC%09d_phase.dat'%(TIC),
names=['Phase','RelativeFlux','Error'], overwrite=True)
else:
ascii.write([phase, flux_phased, flux_err_phased], 'TIC%09d_phase.dat'%(TIC),
names=['Phase','RelativeFlux','Error'], overwrite=True)
plot = make_plot(slow_lc.freq, slow_lc.power, slow_lc.fap_001, BJD_or, flux_or,
slow_lc.bjd, slow_lc.flux, phase, flux_phased, flux_fit,
phase2, flux_phased2, flux_fit2, slow_lc.period, slow_lc.crowdsap,
n_slow)
plot.savefig('TIC%09d.png'%(TIC))
################################
###### 20-SECOND DATA ########
if fast:
fast_lc = tul.LCdata(TIC)
fast_lc.read_data(infile_fast)
BJD_or = fast_lc.bjd
flux_or = fast_lc.flux
fast_lc.clean_data()
if (flag_lc == 1):
ascii.write([fast_lc.bjd, fast_lc.flux, fast_lc.flux_err],
'TIC%09d_lc_fast.dat'%(TIC), names=['BJD','RelativeFlux','Error'],
overwrite=True)
# Calculates the periodogram
fast_lc.periodogram()
if (flag_ls == 1):
ascii.write([1/fast_lc.freq, fast_lc.power], 'TIC%09d_ls_fast.dat'%(TIC),
names=['Period[h]','Power'], overwrite=True)
# Folds the data to the dominant peak
fast_lc.phase_data(1.0)
phase = fast_lc.phase
flux_phased = fast_lc.flux_phased
flux_err_phased = fast_lc.flux_err_phased
flux_fit = fast_lc.flux_fit
fast_amp = fast_lc.amp
# Folds the data to twice the dominant peak
fast_lc.phase_data(2.0)
phase2 = fast_lc.phase
flux_phased2 = fast_lc.flux_phased
flux_err_phased2 = fast_lc.flux_err_phased
flux_fit2 = fast_lc.flux_fit
fast_amp2 = fast_lc.amp
if (flag_ph == 1):
if (flag_p2 == 1):
ascii.write([phase2, flux_phased2, flux_err_phased2], 'TIC%09d_phase_fast.dat'%(TIC),
names=['Phase','RelativeFlux','Error'], overwrite=True)
else:
ascii.write([phase, flux_phased, flux_err_phased], 'TIC%09d_phase_fast.dat'%(TIC),
names=['Phase','RelativeFlux','Error'], overwrite=True)
plot_fast = make_plot(fast_lc.freq, fast_lc.power, fast_lc.fap_001, BJD_or, flux_or,
fast_lc.bjd, fast_lc.flux, phase, flux_phased, flux_fit,
phase2, flux_phased2, flux_fit2, fast_lc.period, fast_lc.crowdsap,
n_fast)
plot_fast.savefig('TIC%09d_fast.png'%(TIC))
################################
######### WRITE LOG ##########
log = open('TIC%09d.log'%(TIC), "w")
log.write("TIC %09d\n\n"%(TIC))
if warning:
log.write("Warning! No object with measured parallax within 30 arcsec.\n")
else:
log.write("Gaia DR2 source_id = %20d\n"%(gaia_id))
log.write("G = %6.3f, MG = %6.3f, bp_rp = %6.3f\n\n"%(G, MG, bprp))
log.write("2-minute cadence data\n")
log.write("Number of sectors: %2d\n"%(len(infile)))
log.write("CROWDSAP: %5.3f\n"%(np.mean(slow_lc.crowdsap)))
if (flag_p2 == 1):
log.write("Period = %9.5f hours, Amplitude = %7.5f per cent\n"%(2.0*slow_lc.period, 100*abs(amp2)))
else:
log.write("Best period = %9.5f hours, Amplitude = %7.5f per cent\n"%(slow_lc.period, 100*abs(amp)))
log.write("FAP = %7.5e\n\n"%(slow_lc.fap_p))
if fast:
log.write("20-second cadence data\n")
log.write("Number of sectors: %2d\n"%(len(infile_fast)))
log.write("CROWDSAP: %5.3f\n"%(np.mean(fast_lc.crowdsap)))
if (flag_p2 == 1):
log.write("Period = %9.5f hours, Amplitude = %7.5f per cent\n"%(2.0*fast_lc.period, 100*abs(fast_amp2)))
else:
log.write("Best period = %9.5f hours, Amplitude = %7.5f per cent\n"%(fast_lc.period, 100*abs(fast_amp)))
log.write("FAP = %7.5e\n\n"%(fast_lc.fap_p))
else:
log.write("No fast-candence data available.\n\n")
if (len(gaia)>0):
log.write("Other G < 18. matches within 5 pixels:\n")
log.write("source_id G MG bp_rp\n")
for i in range(0, len(gaia)):
if i != idx:
log.write("%20d %6.3f %6.3f %6.3f\n"%(id_all[i], g_all[i], MG_all[i], bprp_all[i]))
log.close()
################################