forked from christinahedges/kepFGS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FGS.py
271 lines (239 loc) · 9.68 KB
/
FGS.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
import pandas as pd
import glob
import urllib
import numpy as np
import os
from progressbar import Percentage, ProgressBar, Bar, ETA, FileTransferSpeed
import progressbar
import tarfile
import datetime
from astropy.time import Time
def download(url, fileName, progress=True):
'''
Download a file from http either with or without a progress bar
url = url to download
fileName = filename to save to
progress = display progress bar (True/False)
'''
if progress is True:
widgets = ['Test: ', Percentage(), ' ', Bar(), ' ', ETA(), ' ',
FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets)
def dlProgress(count, blockSize, totalSize):
if pbar.maxval is None:
pbar.maxval = totalSize
pbar.start()
pbar.update(min(count * blockSize, totalSize))
urllib.urlretrieve(url, fileName, reporthook=dlProgress)
pbar.finish()
else:
urllib.urlretrieve(url, fileName)
def get_data(datadir='', mission='kepler', quarters=None):
'''
Obtain the FGS data for Kepler or K2 directly from MAST
mission = kepler,k2
'''
if isinstance(quarters, int):
quarters = [quarters]
#Find the data for the right mission
if mission == 'kepler':
prefix = 'kplr'
pref = 'q'
if mission == 'k2':
prefix = 'ktwo'
pref = 'c'
if mission != 'kepler' and mission != 'k2':
print("Choose 'kepler' or 'k2' for mission.")
return
#Get ancilliary table
if glob.glob(datadir + prefix + '-anc-2017013163000.txt') == []:
url = ('https://archive.stsci.edu/missions/{}/fgs/flc/'
'{}-anc-2017013163000.txt'.format(mission, prefix))
fileName = '{}{}-anc-2017013163000.txt'.format(datadir, prefix)
download(url, fileName, progress=False)
#Find all the unique stars
stars = pd.read_csv('{}{}-anc-2017013163000.txt'.format(datadir, prefix),
skiprows=[0, 1, 3])
if quarters is None:
quarters = np.unique(stars[stars.keys()[0]])
for q in quarters:
fname = '' + prefix + '-flc-' + pref + str(q).zfill(2) + '-xx.txt'
if glob.glob(datadir + fname) == []:
print('Downloading Quarter ' + str(q))
url = ('https://archive.stsci.edu/missions/{}/fgs/flc/'
'{}.tar.gz'.format(mission, fname))
fileName = '{}{}.tar.gz'.format(datadir, fname)
download(url, fileName)
tar = tarfile.open(fileName, "r:gz")
tar.extractall(datadir + fname)
tar.close()
os.remove(fileName)
def table(datadir='', return_loc=False):
if glob.glob(datadir + 'kplr-anc-2017013163000.txt') == []:
url = 'https://archive.stsci.edu/missions/kepler/fgs/flc/kplr-anc-2017013163000.txt'
fileName = '{}kplr-anc-2017013163000.txt'.format(datadir)
try:
download(url, fileName, progress=False)
except:
print('Cannot download data')
if glob.glob(fileName) == []:
print('No Kepler ancilliary data.')
return None
if glob.glob('{}ktwo-anc-2017013163000.txt'.format(datadir)) == []:
url = 'https://archive.stsci.edu/missions/k2/fgs/flc/ktwo-anc-2017013163000.txt'
fileName = '{}ktwo-anc-2017013163000.txt'.format(datadir)
try:
download(url, fileName, progress=False)
except:
print('Cannot download data')
if glob.glob(fileName) == []:
print('No K2 ancilliary data.')
return None
keys = ['KEPLER_ID', 'RA', 'DEC', 'KEPMAG', 'mission', 'QUARTER',
'FGS_MODULE', 'STAR_INDEX']
k2 = pd.read_csv('ktwo-anc-2017013163000.txt', skiprows=[0, 1, 3])
#k2=k2.loc[np.unique(k2['RA'],return_index=True)[1]].reset_index(drop=True)
k2.columns = np.append('QUARTER', k2.keys()[1:])
k2['mission'] = 'k2'
k2 = k2[keys]
kepler = pd.read_csv('kplr-anc-2017013163000.txt', skiprows=[0, 1, 3])
#kepler=kepler.loc[np.unique(k2['RA'],return_index=True)[1]].reset_index(drop=True)
kepler['mission']='kepler'
kepler.columns=np.append('QUARTER', kepler.keys()[1:])
kepler = kepler[keys]
tab = k2.append(kepler).reset_index(drop=True)
tab = tab[tab.KEPLER_ID != -1].reset_index(drop=True)
if return_loc is False:
tab = tab.loc[np.unique(tab.RA, return_index=True)[1]].reset_index(drop=True)
tab = tab.sort_values('mission')
tab = tab[keys[0:-3]]
tab = tab.reset_index(drop=True)
return tab
def fgs_lc(datadir, quarter, module, starno, div_trend=False, norm=True,
raw=False, npoly=1, nsig=5., mission='kepler'):
'''
Reads, cleans and returns an FGS light curve based on quarter, module, star number
npoly = Order of polynomial to remove from flux
nsig = sigma level to clip data
raw = returns raw data with no cleaning
norm = normalise data
'''
if isinstance(quarter, int):
quarter = str(quarter).zfill(2)
if isinstance(module, int):
module = str(module).zfill(2)
if mission == 'kepler':
prefix = 'kplr'
pref = 'q'
if mission == 'k2':
prefix = 'ktwo'
pref = 'c'
mods = np.asarray(['01', '05', '21', '25'], dtype=str)
fnames = glob.glob(datadir + prefix + '-flc-' + pref + quarter + '-xx.txt/*')
if fnames == []:
try:
get_data(datadir, mission=mission, quarters=[np.int(quarter)])
except:
print('Cannot download data')
return [], [], [], []
fnames = glob.glob(datadir + prefix + '-flc-' + pref + quarter + '-xx.txt/*')
if fnames == []:
print('No data')
return None, None, None, None
f = fnames[np.where(mods==module)[0][0]]
df = pd.read_csv(f, skiprows=[0, 2])
if (int(starno) * 3) + 3 > len(df.keys()):
print('No such star')
return None, None, None, None
#Clear out nans from the data
#----------------------------
keys = df.keys()[[0, (int(starno) * 3) + 1, (int(starno) * 3) + 2, (int(starno) * 3) + 3]]
df = df[keys].dropna().reset_index(drop=True)
dates = [datetime.datetime.strptime(d, '%m/%d/%y %H:%M:%S.%f') for d in df[df.keys()[0]]]
t = Time(dates).jd
pref = df.keys()[1][0:5]
counts, col, row = np.asarray(df[keys[1]]), np.asarray(df[keys[2]]), np.asarray(df[keys[3]])
if raw is True:
return t, counts, col, row
#Get rid of bad pointings
#------------------------
#If pointing drifts more than a few pixels it is likely wrong
good = (np.where((abs(col - np.median(col)) < 5.)
& (abs(row - np.median(row)) < 5.))[0])
#Has to have at some values
good = good[np.where(counts[good] >= 250.)[0]]
#Sigma to clip each quarter
l = np.polyfit(t[good], col[good], npoly)
line = np.polyval(l, t[good])
good = good[np.where(np.abs(col[good] - line) <= nsig * np.std(col[good] - line))[0]]
l = np.polyfit(t[good], row[good], npoly)
line = np.polyval(l, t[good])
good = good[np.where(np.abs(row[good] - line) <= nsig * np.std(row[good] - line))[0]]
l = np.polyfit(t[good], counts[good], npoly)
line = np.polyval(l, t[good])
good = good[np.where(np.abs(counts[good] - line) <= nsig * np.std(counts[good] - line))[0]]
if norm is True:
counts /= np.nanmedian(counts[good])
if div_trend is True:
l = np.polyfit(t[good], counts[good], npoly)
line = np.polyval(l, t)
counts /= line
return t[good], counts[good], col[good], row[good]
def gen_lc(datadir='', ID=None, norm=True, div_trend=False, quarters=None,
raw=False, npoly=1, nsig=5):
'''
Generate light curve for FGS data. Reads data from multiple modules and stitches it together.
'''
tab = table(return_loc=True)
tab = tab.sort_values('QUARTER')
tab = tab.reset_index(drop=True)
loc = np.where(tab.KEPLER_ID == ID)[0]
if len(loc) == 0:
print('No such star')
return
if len(loc) > 1:
loc = [loc[0]]
mission = np.asarray(tab.loc[loc, 'mission'])[0]
if mission == 'kepler':
prefix = 'kplr'
if mission == 'k2':
prefix = 'ktwo'
if mission != 'kepler' and mission != 'k2':
print("Choose 'kepler' or 'k2' for mission.")
return
if isinstance(quarters, int):
quarters = [quarters]
#Get ancilliary table
if glob.glob(datadir+prefix+'-anc-2017013163000.txt') == []:
url = ('https://archive.stsci.edu/missions/{}/fgs/flc/'
'{}-anc-2017013163000.txt'.format(mission, prefix))
fileName = '{}{}-anc-2017013163000.txt'.format(datadir, prefix)
try:
download(url, fileName, progress=False)
except:
print('Cannot download data')
return
if ID is None:
print('Please provide an ID')
return [], [], [], []
x, y, cols, rows = np.zeros(0), np.zeros(0), np.zeros(0), np.zeros(0)
s_tab = tab[tab.KEPLER_ID == ID].reset_index(drop=True)
s_qs = np.asarray(s_tab['QUARTER'], dtype=int)
if quarters is None:
quarters = s_qs
for q in quarters:
quarter = str(q).zfill(2)
pos = np.where(s_qs == q)[0]
if len(pos) == 0:
continue
pos = pos[0]
module = str(s_tab.loc[pos, 'FGS_MODULE']).zfill(2)
starno = s_tab.loc[pos, 'STAR_INDEX'] - 1
t, counts, col, row = fgs_lc(datadir, quarter, module, starno, raw=raw,
norm=norm, mission=mission, npoly=npoly,
nsig=nsig)
x = np.append(x, t, axis=0)
y = np.append(y, counts, axis=0)
cols = np.append(cols, col, axis=0)
rows = np.append(rows, row, axis=0)
return x, y, cols, rows