/
dataclass.py
302 lines (238 loc) · 10.7 KB
/
dataclass.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
import numpy as np
import astropy.units as u
from astropy.coordinates import SkyCoord as sc
from astropy.table import Table, vstack
from astropy.utils.console import ProgressBar
from matplotlib import pyplot as plt
from scipy.stats import chisquare
from astropy.io import fits
from utils import uconv
import pickle
import os
import warnings
from rebin import rebin
class Star(object):
'''
A class to handle individual stars from time series data.
'''
def __init__(self, starfile):
self.filename = os.path.abspath(starfile)
self.datapath = os.path.dirname(os.path.abspath(starfile))+'/'
# self.id = self.filename.split('/')[-1].split('-')[2][8:]
self.get_data()
self.filtered = False
def get_data(self):
with fits.open(self.filename, mode='readonly') as hdulist:
bjd = hdulist[1].data['TIME']
bjd_nan_mask = np.isnan(bjd)
self.time_unit = u.Unit(hdulist[1].header['TIMEUNIT'])
flux = hdulist[1].data['SAP_FLUX']
flux_nan_mask = np.isnan(flux)
err = hdulist[1].data['SAP_FLUX_ERR']
err_nan_mask = np.isnan(err)
flags = hdulist[1].data['QUALITY']
self.ra = hdulist[1].header['RA_OBJ']
self.dec = hdulist[1].header['DEC_OBJ']
self.id = str(hdulist[0].header['TICID'])
self.exposure_time = hdulist[1].header['TIMEDEL'] # days
s = sc(ra=self.ra*u.degree, dec=self.dec*u.degree).to_string('hmsdms')
self.ra_hms = (s.split(' ')[0].split('h')[0]
+ ':'
+ s.split(' ')[0].split('h')[1].split('m')[0]
+ ':'
+ s.split(' ')[0].split('m')[1].split('s')[0])
self.dec_dms = (s.split(' ')[1].split('d')[0]
+ ':'
+ s.split(' ')[1].split('d')[1].split('m')[0]
+ ':'
+ s.split(' ')[1].split('m')[1].split('s')[0])
nan_mask = np.invert(np.logical_or(bjd_nan_mask, flux_nan_mask,
err_nan_mask))
self.bjd = bjd[nan_mask]
self.flux = flux[nan_mask]
self.err = err[nan_mask]
self.flags = flags[nan_mask]
self.data = Table([self.bjd, self.flux, self.err, self.flags],
names=['bjd', 'flux', 'err', 'flags'], masked=True)
def combine(self, *args):
"""
Combine one star object's data with another (or several other) star
object's data.
"""
arglist = [arg.data for arg in args]
arglist.insert(0, self.data)
self.data = vstack([argdata for argdata in arglist])
self.data.sort('bjd')
self.flux = self.data['flux']
self.err = self.data['err']
self.bjd = self.data['bjd']
self.flags = self.data['flags']
def filter(self, tolerance=0.1):
# Filtering based on bad data flags (FOR TESS DATA ONLY)
# Flags commented out of the list below are kept
flags_key = [
# 0, # None
8, # Spacecraft is in Earth point
32, # Reaction wheel desaturation event
128, # manual exclude (anomaly)
512, # Impulsive outlier removed before cotrending
]
# Native flags are powers of two
native_flags = np.array(2)**range(12)
native_flags = np.insert(native_flags, 0, 0, axis=0)
for flag in set(self.data['flags']):
if flag in flags_key:
self.data.remove_rows(self.data['flags'] == flag)
elif flag not in native_flags:
self.data.remove_rows(self.data['flags'] == flag)
self.data.sort('err')
self.data.reverse()
n_to_kill = int(np.round(tolerance*len(self.data)))
self.data.remove_rows(range(n_to_kill))
self.data.sort('bjd')
self.filtered = True
def inject(self, amplitude, period):
omega = 2.*np.pi/period
t = np.array(self.data['bjd'])
sinflux = amplitude*np.sin(omega*t)
sumflux = np.array(self.data['flux']) + sinflux
self.data['flux'] = sumflux
self.flux = sumflux
def addnoise(self, amplitude, plot=False):
n = len(self.data['flux'])
noise = amplitude*np.random.random(n) - 0.5*amplitude
self.data['flux'] = self.data['flux'] + noise
self.flux = self.data['flux']
if plot:
plt.hist(noise)
plt.title('White noise added to flux points')
plt.xlabel('Flux (e-/s)')
plt.ylabel('n')
plt.show()
def prepare(self):
if self.filtered == False:
print('Filtering data...')
self.filter()
print('Rebinning flux...')
rebin_flux = rebin(self.data['bjd','flux'], binwidth=2./60./24.,
exptime=2./60./24., timestamp_position=0.5,
median_replace=True)
print('Rebinning error...')
rebin_err = rebin(self.data['bjd','err'], binwidth=2./60./24.,
exptime=2./60./24., timestamp_position=0.5,
median_replace=True)
new_bjd = rebin_flux[:,0]
new_flux = rebin_flux[:,1]
new_err = rebin_err[:,1]
self.bjd = new_bjd
self.flux = new_flux
self.err = new_err
self.data = Table([new_bjd, new_flux, new_err],
names=['bjd', 'flux', 'err'])
print('Done!')
def plot(self, filename=None, chsq=True):
"""
Plot the light curve.
Parameters
----------
filename : str, optional
The filename of the pickled matplotlib figure to save. If not
provided, the figure will be displayed without saving.
chsq : boolean, optional
If true, the light curve will be fitted to a 0-order polynomial
to measure the reduced chi squared. This is intended to be a
measure of how variable or non-variable the light curve is.
Returns
-------
None : None
None is returned if chsq is set to False.
chsq : float
The reduced chi squared of the light curve, if chsq is set to True.
"""
fig = plt.figure(figsize=[10,4])
ax = fig.add_subplot(111)
ax.errorbar(self.data['bjd'], self.data['flux'], alpha=0.5,
yerr=self.data['err'], ms=2, fmt='ko', elinewidth=.5)
if chsq is True:
x = np.linspace(0, len(self.data) - 1, len(self.data))
z = np.polyfit(x, self.data['flux'], 1)
chsq = np.sum(((self.data['flux'] - np.polyval(z, x)) ** 2.)
/ self.data['err']**2.)/(len(self.data) - 1)
p = np.poly1d(z)
ax.plot(self.data['bjd'], p(self.data['bjd']), 'b--')
ax.text(0.1, 0.9, "Reduced Chi Squared: {:.4f}".format(chsq),
transform=ax.transAxes)
ax.set_xlabel('BJD (Days)')
ax.set_ylabel('Flux (e-/s)')
ax.set_title('ID: {} Avg Brightness = {:.2f} e-/s'
.format(str(self.id), np.mean(self.data['flux'])))
if filename is not None:
pickle.dump(ax, open(filename, 'wb'))
else:
plt.show()
plt.close()
return chsq
def split_nights(self):
"""
Split a star's time series data by night of observation. Intended for
use only with ground-based telescopes.
Parameters
----------
None
Returns
-------
nights: list
List of astropy.table.Table objects, one for each night of
observations.
"""
bjds = np.array(list(self.data['bjd']))
gaps = bjds[1:] - bjds[:-1] # Time between consecutive data points
gap_indices = np.where(gaps > 0.01)[0]
nights = [self.data[:gap_indices[0]]]
for i in range(1, len(gap_indices)-1):
nights.append(self.data[gap_indices[i]+1:gap_indices[i+1]])
nights.append(self.data[(gap_indices[-1]+1):])
return nights
def export(self, filename=None):
if filename is None:
filename = './'+str(self.id)
else:
filename = os.path.splitext(filename)[0]
flux = self.data['flux']*10e6 # RESCALE
zero_loc = np.where(flux == 0.)
nonzero_loc = np.where(flux != 0.)
median = np.median(flux[nonzero_loc])
flux[zero_loc] = median
flux.astype(np.float32).tofile(filename+'.dat')
descriptors = np.array([' Data file name without suffix = ',
' Telescope used = ',
' Instrument used = ',
' Object being observed = ',
' J2000 Right Ascension (hh:mm:ss.ssss) = ',
' J2000 Declination (dd:mm:ss.ssss) = ',
' Data observed by = ',
' Epoch of observation (MJD) = ',
' Barycentered? (1 yes, 0 no) = ',
' Number of bins in the time series = ',
' Width of each time series bin (sec) = ',
' Any breaks in the data? (1 yes, 0 no) = ',
' Type of observation (EM band) = ',
' Photometric filter used = ',
' Field-of-view diameter (arcsec) = ',
' Central wavelength (nm) = ',
' Bandpass (nm) = ',
' Data analyzed by = ',
' Any additional notes:',
' none'])
values = np.array([str(self.id), 'TESS', 'unset', str(self.id),
self.ra_hms, self.dec_dms, 'unset',
str(np.min(self.data['bjd'])), '0',
str(len(self.flux)),
(self.exposure_time*self.time_unit).to('s'),
'0', 'Optical', 'Other', '180.00', '500.0', '400.0',
'unset', '', ''],
dtype=str)
inf = np.core.defchararray.add(descriptors, values)
np.savetxt(filename+'.inf', inf, fmt='%s')
if __name__ == '__main__':
pass