-
Notifications
You must be signed in to change notification settings - Fork 1
/
observability.py
247 lines (207 loc) · 7.85 KB
/
observability.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from astropy.coordinates import SkyCoord, EarthLocation, Angle
from astropy.time import Time
import astropy.units as u
from astroplan import Observer, FixedTarget, is_observable, observability_table
from astroplan import AtNightConstraint, AltitudeConstraint
from astroplan.plots import plot_airmass
from astroplan.utils import time_grid_from_range
def defineCAHA():
"""
Define Calar Alto observational site.
Parameters
----------
None
Returns
-------
CAHA : astroplan Observer object
Calar Alto Observatory, Spain
"""
return Observer(longitude=Angle('-2d32.8m'), latitude=Angle('37d13.4m'),
elevation=2168*u.m, name="CAHA", timezone="Europe/Madrid")
def targetsFromCSV(path, minUpdated=None, **read_csv_kwargs):
"""
Obtain a target list from a csv file.
Reads a csv file with the TESS alerts as downloaded from the TEV homepage,
extracts coordinates and names, and transforms the entries to astroplan
target objects.
Parameters
----------
path : str
path to the csv file
minUpdated : str, optional
earliest time in column "Updated" to be considered
Format: YYYY-MM-DD
read_csv_kwargs : keyword arguments, optional
keyword arguments to pass to pandas' read_csv function
Returns
-------
alerts : pandas DataFrame
table containing alerts
targets : list
list containing astroplan targets
"""
alerts = pd.read_csv(path, **read_csv_kwargs)
alerts.rename(columns={'Full TOI ID': 'TOI'}, inplace=True)
targets = []
if minUpdated is not None:
if not 'Updated' in alerts.columns:
# newer alert files contain "Alerted" instead of "Updated"
alerts.rename(columns={'Alerted':'Updated'}, inplace=True)
alerts.Updated = pd.to_datetime(alerts.Updated)
alerts.Updated = alerts.Updated.dt.tz_localize(None) # strip timezone
minUpdated = np.datetime64(minUpdated)
alerts = alerts[alerts.Updated > minUpdated]
for toi in alerts.iterrows():
target = toi[1]
coords = SkyCoord(target['TIC Right Ascension'], target['TIC Declination'],
unit=(u.deg, u.deg))
targets.append(FixedTarget(coord=coords, name=target['TOI']))
return alerts, targets
def define_constraints(minAltitude, earliestObs, latestObs):
"""
Define various constraints to define 'observability'.
Format for times: YYYY-MM-DD
Parameters
----------
minAltitude : int
minimum local elevation in degree
earliestObs : str
Earliest time of observation
latestObs : str
Latest time of observation
Returns
-------
constraints : list
list of constraints
earliestObs : Time object
Earliest time of observation
latestObs : Time object
Latest time of observation
"""
constraints = [AtNightConstraint.twilight_astronomical(),
AltitudeConstraint(min=minAltitude*u.deg)]
return constraints, Time(earliestObs), Time(latestObs)
def check_observability(alerts, constraints, observer, targets, earliestObs,
latestObs):
"""
Check observability from observatory given the observational constraints.
Parameters
----------
alerts : pandas DataFrame
table containing alerts
constraints : list
list of constraints
observer : astroplan Observer object
e.g. Calar Alto Observatory, Spain
targets : list
list containing astroplan targets
earliestObs : Time object
Earliest time of observation
latestObs : Time object
Latest time of observation
Returns
-------
alerts : pandas DataFrame
filtered alerts table containing observable targets
"""
observabilityMask = is_observable(constraints, observer,
targets, time_range=[earliestObs, latestObs])
return alerts[observabilityMask]
def MdwarfFilter(candidates, maxTeff=3800):
"""
Filter candidates list for M dwarfs.
Parameters
----------
candidates : pandas DataFrame
Table containing candidates. Must contain a column "Teff".
maxTeff : int
maximum effective temperature in Kelvin
Returns
-------
candidates : pandas DataFrame
filtered candidates Table containing only M dwarfs
"""
Mcandidates = candidates[candidates.Teff < maxTeff]
def plot_observability(candidates, constraints, observer, earliestObs,
latestObs, timeRes=24*u.hour, timeSubRes=.2*u.hour,
fig=None, ax=None, **kwargs):
""" Visualize long-term observability of a set of targets.
Parameters
----------
candidates : pandas DataFrame
Table containing candidates. Must contain a column "Teff".
constraints : list
list of constraints
observer : astroplan Observer object
e.g. Calar Alto Observatory, Spain
earliestObs : Time object
Earliest time of observation
latestObs : Time object
Latest time of observation
timeRes : quantity
time-resolution of the resulting plot, should be greater than 24h
timeSubRes : quantity
fine resolution used for computation of observable time fractions at
each resolved time in the plot. Has to be (more than an order of
magnitude) smaller than 'timeRes' for reasonable results.
fig : matplotlib figure object, optional
figure to plot on
ax : matplotlib axis object, optional
axis to plot on
**kwargs : keyword arguments to pass to matplotlib's 'imshow'
Returns
-------
fig : matplotlib figure
figure containing the plot
ax : matplotlib axis
axis containing the plot
"""
# flip candidate list
candidates = candidates[::-1]
# prepare the grid
rough_grid = time_grid_from_range([earliestObs, latestObs],
time_resolution=timeRes)
observability_grid = np.zeros((len(candidates), len(rough_grid) - 1))
for i, t in enumerate(rough_grid[:-1]):
obsTable = observability_table(constraints, observer, candidates,
time_grid_from_range([rough_grid[i],
rough_grid[i+1]],
time_resolution=timeSubRes))
hoursObservable = obsTable['fraction of time observable']*24*u.hour
observability_grid[:,i] = hoursObservable
# now for the actual plot
if ax == None:
fig, ax = plt.subplots()
dates = rough_grid.plot_date
extent = [dates[0], dates[-1], -.5, len(candidates) -.5]
im = ax.imshow(observability_grid, cmap='inferno', aspect='auto',
extent=extent, origin='lower', **kwargs)
# get date ticks right
ax.xaxis_date()
date_format = mdates.DateFormatter('%b \'%y')
ax.xaxis.set_major_formatter(date_format)
ax.tick_params(rotation=45, axis='x', which='both', length=3, direction='out',
pad=2)
for tick in ax.xaxis.get_major_ticks():
tick.label1.set_horizontalalignment('left')
# some eyecandy
ax.set_yticks(range(len(candidates)))
ax.set_yticklabels([c.name for c in candidates])
ax.set_xlabel('Date', labelpad=9)
ax.set_ylabel('TOI')
ax.grid(False)
fig.colorbar(im).set_label('Hours Observable per Night', labelpad=15)
return fig, ax
def defaultRun():
CAHA = CAHA()
alerts, targets = targetsFromCSV('data/toi-2019-01-25.csv')
constraints, earliestObs, latestObs = define_constraints(30,
'2019-02-08 12:00', '2019-12-31 12:00')
observables = check_observability(alerts, constraints, CAHA, targets, earliestObs,
latestObs)
if __name__ == "__main__":
defaultRun()