-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data_kepler.py
108 lines (90 loc) · 4.48 KB
/
get_data_kepler.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
"""
.. module:: get_data_kepler
:synopsis: Returns Kepler lightcurve data as a JSON string.
.. moduleauthor:: Scott W. Fleming <fleming@stsci.edu>
"""
from astropy.io import fits
from data_series import DataSeries
from parse_obsid_kepler import parse_obsid_kepler
#--------------------
def get_data_kepler(obsid):
"""
Given a Kepler observation ID, returns the lightcurve data.
:param obsid: The Kepler observation ID to retrieve the data from.
:type obsid: str
:returns: JSON -- The lightcurve data for this observation ID.
Error codes:
From parse_obsid_kepler:
0 = No error parsing observation ID.
1 = Could not unpack observation ID into three components.
2 = Kepler ID in Observation ID string not within allowed bounds.
3 = Kepler cadence type in Observation ID not an allowed value.
4 = Kepler Q-code in Observation ID is not exactly 18 characters.
5 = One or more files are missiong on disk based on the Q-code.
From this module:
6 = Could not open one or more FITS file for reading.
"""
# For Kepler, this defines the x-axis and y-axis units as a string.
kepler_xunit = "BJD"
kepler_yunit = "electrons / second"
# Parse the obsID string to determine the paths+files to read. Note:
# this step will assign some of the error codes returned to the top level.
parsed_files_result = parse_obsid_kepler(obsid)
if parsed_files_result.errcode == 0:
# For each file, read in the contents and create a return JSON object.
all_plot_labels = ['']*2*len(parsed_files_result.files)
all_plot_series = ['']*2*len(parsed_files_result.files)
all_plot_xunits = ['']*2*len(parsed_files_result.files)
all_plot_yunits = ['']*2*len(parsed_files_result.files)
# This error code will be used unless there's a problem reading any of
# the FITS files in the list.
errcode = 0
for i, kfile in enumerate(parsed_files_result.files):
try:
with fits.open(kfile) as hdulist:
# Extract time stamps and relevant fluxes.
bjd = [float(x) for x in
(float(hdulist[1].header["BJDREFI"]) +
hdulist[1].header["BJDREFF"] +
hdulist[1].data["TIME"])]
flux_sap = [float(x) for x in hdulist[1].data["SAP_FLUX"]]
flux_pdcsap = [float(x) for x in
hdulist[1].data["PDCSAP_FLUX"]]
# Create the plot label and plot series for the SAP and
# PDCSAPfluxes.
this_plot_label = ('KPLR_' + parsed_files_result.kepid +
' ' + parsed_files_result.cadence.upper()
+ ' Q' +
parsed_files_result.quarters[i])
all_plot_labels[i*2] = this_plot_label + ' SAP'
all_plot_series[i*2] = [x for x in
zip(bjd, flux_sap)]
all_plot_xunits[i*2] = kepler_xunit
all_plot_yunits[i*2] = kepler_yunit
all_plot_labels[i*2+1] = this_plot_label + ' PDCSAP'
all_plot_series[i*2+1] = [x for x in
zip(bjd, flux_pdcsap)]
all_plot_xunits[i*2+1] = kepler_xunit
all_plot_yunits[i*2+1] = kepler_yunit
except IOError:
errcode = 6
all_plot_labels[i*2] = ''
all_plot_series[i*2] = []
all_plot_xunits[i*2] = ''
all_plot_yunits[i*2] = ''
all_plot_labels[i*2+1] = ''
all_plot_series[i*2+1] = []
all_plot_xunits[i*2+1] = ''
all_plot_yunits[i*2+1] = ''
# Create the return DataSeries object.
return_dataseries = DataSeries('kepler', obsid, all_plot_series,
all_plot_labels,
all_plot_xunits, all_plot_yunits,
errcode)
else:
# This is where an error DataSeries object would be returned.
return_dataseries = DataSeries('kepler', obsid, [], [], [], [],
parsed_files_result.errcode)
# Return the DataSeries object back to the calling module.
return return_dataseries
#--------------------