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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
.. include:: ../references.txt
.. doctest-skip-all
.. _constraints:
******************************
Defining Observing Constraints
******************************
Contents
========
* :ref:`constraints-built_in_constraints`
* :ref:`constraints-visualize_constraints`
* :ref:`constraints-user_defined_constraints`
.. _constraints-built_in_constraints:
Introduction to Built-In Constraints
====================================
Frequently, we have a long list of targets that we want to observe, and we need
to know which ones are observable given a set of constraints imposed on our
observations by a wide range of limitations. For example, your telescope may
only point over a limited range of altitudes, your targets are only useful
in a range of airmasses, and they must be separated from the moon by some
large angle. The ``constraints`` module is here to help!
Say we're planning to observe from Subaru Observatory in Hawaii on August 1,
2015 from 06:00-12:00 UTC. First, let's set up an `~astroplan.Observer` object::
from astroplan import Observer, FixedTarget
from astropy.time import Time
subaru = Observer.at_site("Subaru")
time_range = Time(["2015-08-01 06:00", "2015-08-01 12:00"])
We're keeping a list of targets in a text file called ``targets.txt``, which
looks like this::
# name ra_degrees dec_degrees
Polaris 37.95456067 89.26410897
Vega 279.234734787 38.783688956
Albireo 292.68033548 27.959680072
Algol 47.042218553 40.955646675
Rigel 78.634467067 -8.201638365
Regulus 152.092962438 11.967208776
We'll read in this list of targets using `astropy.table`, and create a list
of `~astroplan.FixedTarget` objects out of them::
# Read in the table of targets
from astropy.table import Table
target_table = Table.read('targets.txt', format='ascii')
# Create astroplan.FixedTarget objects for each one in the table
from astropy.coordinates import SkyCoord
import astropy.units as u
targets = [FixedTarget(coord=SkyCoord(ra=ra*u.deg, dec=dec*u.deg), name=name)
for name, ra, dec in target_table]
We will build a bulleted list of our constraints first, then implement them in
code below.
* Our observations with Subaru can only occur between altitudes of ~10-80
degrees, which we can define using the
`~astroplan.constraints.AltitudeConstraint` class.
* We place an upper limit on the airmass of each target during observations
using the `~astroplan.constraints.AirmassConstraint` class.
* Since we're optical observers, we only want to observe targets at night, so
we'll also call the `~astroplan.constraints.AtNightConstraint` class. We're
not terribly worried about sky brightness for these bright stars, so we'll
define "night" times as those between civil twilights by using the class
method `~astroplan.AtNightConstraint.twilight_civil`:
.. code-block:: python
from astroplan import (AltitudeConstraint, AirmassConstraint,
AtNightConstraint)
constraints = [AltitudeConstraint(10*u.deg, 80*u.deg),
AirmassConstraint(5), AtNightConstraint.twilight_civil()]
This list of constraints can now be applied to our target list to determine:
* whether the targets are observable given the constraints at *any* times in the
time range, using `~astroplan.is_observable`,
* whether the targets are observable given the constraints at *all* times in the
time range, using `~astroplan.is_always_observable`
* during what months the targets are *ever* observable given the constraints,
using `~astroplan.months_observable`::
from astroplan import is_observable, is_always_observable, months_observable
# Are targets *ever* observable in the time range?
ever_observable = is_observable(constraints, subaru, targets, time_range=time_range)
# Are targets *always* observable in the time range?
always_observable = is_always_observable(constraints, subaru, targets, time_range=time_range)
# During what months are the targets ever observable?
best_months = months_observable(constraints, subaru, targets, time_range)
The `~astroplan.is_observable` and `~astroplan.is_always_observable` functions
will return boolean arrays which tell you whether or not each target is
observable given your constraints. Let's print these results in tabular form:
>>> from astropy.table import Table
>>> import numpy as np
>>> observability_table = Table()
>>> observability_table['targets'] = [target.name for target in targets]
>>> observability_table['ever_observable'] = ever_observable
>>> observability_table['always_observable'] = always_observable
>>> print(observability_table)
<Table length=6>
targets ever_observable always_observable
str7 bool bool
------- --------------- -----------------
Polaris True True
Vega True True
Albireo True False
Algol True False
Rigel False False
Regulus False False
Now we can see which targets are observable! You can also use the
`~astroplan.observability_table` method to do the same calculations and
store the results in a table, all in one step::
>>> from astroplan import observability_table
>>> table = observability_table(constraints, subaru, targets, time_range=time_range)
>>> print(table)
target name ever observable always observable fraction of time observable
----------- --------------- ----------------- ---------------------------
Polaris True True 1.0
Vega True True 1.0
Albireo True False 0.833333333333
Algol True False 0.166666666667
Rigel False False 0.0
Regulus False False 0.0
Let's sanity-check these results using `~astroplan.plots.plot_sky` to plot
the positions of the targets throughout the time range:
.. plot::
from astroplan.plots import plot_sky
from astroplan import Observer, FixedTarget
import matplotlib.pyplot as plt
from matplotlib import cm
from astropy.time import Time
from astropy.coordinates import SkyCoord
import astropy.units as u
# Get grid of times within the time_range limits
from astroplan import time_grid_from_range
time_range = Time(["2015-08-01 06:00", "2015-08-01 12:00"])
time_grid = time_grid_from_range(time_range)
subaru = Observer.at_site("Subaru")
target_table_string = """# name ra_degrees dec_degrees
Polaris 37.95456067 89.26410897
Vega 279.234734787 38.783688956
Albireo 292.68033548 27.959680072
Algol 47.042218553 40.955646675
Rigel 78.634467067 -8.201638365
Regulus 152.092962438 11.967208776"""
# Read in the table of targets
from astropy.io import ascii
target_table = ascii.read(target_table_string)
targets = [FixedTarget(coord=SkyCoord(ra=ra*u.deg, dec=dec*u.deg), name=name)
for name, ra, dec in target_table]
plt.figure(figsize=(6,6))
cmap = cm.Set1 # Cycle through this colormap
for i, target in enumerate(targets):
ax = plot_sky(target, subaru, time_grid,
style_kwargs=dict(color=cmap(float(i)/len(targets)),
label=target.name))
legend = ax.legend(loc='lower center')
legend.get_frame().set_facecolor('w')
plt.show()
We can see that Vega is in the sweet spot in altitude and azimuth for this
time range and is always observable. Albireo is not always observable given
these criteria because it rises above 80 degrees altitude. Polaris hardly moves
and is therefore always observable, and Algol starts out observable but sets
below the lower altitude limit, and then the airmass limit. Rigel and Regulus
never rise above those limits within the time range.
.. _constraints-visualize_constraints:
Visualizing Constraints
=======================
Suppose an observer is planning to observe low-mass stars in Praesepe in the
optical and infrared from the W.M. Keck Observatory. The observing constraints
require all observations to occur (i) between astronomical twilights; (ii)
while the Moon is separated from Praesepe by at least 45 degrees; and (iii)
while Praesepe is above the lower elevation limit of Keck I, about 33 degrees.
These observing constraints can be specified with the
`~astroplan.AtNightConstraint`, `~astroplan.MoonSeparationConstraint`, and
`~astroplan.AltitudeConstraint` objects, like this:
.. code-block::python
from astroplan import (FixedTarget, Observer, AltitudeConstraint,
AtNightConstraint, MoonSeparationConstraint)
from astropy.time import Time
from astroplan.utils import time_grid_from_range
import astropy.units as u
import numpy as np
import matplotlib.pyplot as plt
# Specify observer at Keck Observatory:
keck = Observer.at_site('Keck')
# Use Sesame name resolver to get coordinates for Praesepe:
target = FixedTarget.from_name("Praesepe")
# Define observing constraints:
constraints = [AtNightConstraint.twilight_astronomical(),
MoonSeparationConstraint(min=45 * u.deg),
AltitudeConstraint(min=33 * u.deg)]
We can evaluate the constraints at one hour intervals in a loop, to create an
observability grid like so:
.. code-block::python
# Define range of times to observe between
start_time = Time('2017-01-01 04:00:01')
end_time = Time('2017-01-01 11:00:01')
time_resolution = 1 * u.hour
# Create grid of times from ``start_time`` to ``end_time``
# with resolution ``time_resolution``
time_grid = time_grid_from_range([start_time, end_time],
time_resolution=time_resolution)
observability_grid = np.zeros((len(constraints), len(time_grid)))
for i, constraint in enumerate(constraints):
# Evaluate each constraint
observability_grid[i, :] = constraint(keck, target, times=time_grid)
This kind of grid can be useful for visualizing what's happening under-the-hood
when you use `~astroplan.is_observable` or `~astroplan.is_always_observable`.
Click the link below for the source code to produce the observability grid shown
below. Dark squares represent times when the observing constraint is not
satisfied.
.. plot::
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from astroplan import (FixedTarget, Observer, AltitudeConstraint,
AtNightConstraint, MoonSeparationConstraint)
from astropy.time import Time
from astroplan.utils import time_grid_from_range
import astropy.units as u
import numpy as np
import matplotlib.pyplot as plt
# Specify observer at Keck Observatory:
keck = Observer.at_site('Keck')
# Use Sesame name resolver to get coordinates for Praesepe:
target = FixedTarget.from_name("Praesepe")
# Define observing constraints:
constraints = [AtNightConstraint.twilight_astronomical(),
MoonSeparationConstraint(min=45 * u.deg),
AltitudeConstraint(min=33 * u.deg)]
# Define range of times to observe between
start_time = Time('2017-01-01 04:00:01')
end_time = Time('2017-01-01 11:00:01')
time_resolution = 1 * u.hour
# Create grid of times from ``start_time`` to ``end_time``
# with resolution ``time_resolution``
time_grid = time_grid_from_range([start_time, end_time],
time_resolution=time_resolution)
observability_grid = np.zeros((len(constraints), len(time_grid)))
for i, constraint in enumerate(constraints):
# Evaluate each constraint
observability_grid[i, :] = constraint(keck, target, times=time_grid)
# Create plot showing observability of the target:
extent = [-0.5, -0.5+len(time_grid), -0.5, 2.5]
fig, ax = plt.subplots()
ax.imshow(observability_grid, extent=extent)
ax.set_yticks(range(0, 3))
ax.set_yticklabels([c.__class__.__name__ for c in constraints])
ax.set_xticks(range(len(time_grid)))
ax.set_xticklabels([t.datetime.strftime("%H:%M") for t in time_grid])
ax.set_xticks(np.arange(extent[0], extent[1]), minor=True)
ax.set_yticks(np.arange(extent[2], extent[3]), minor=True)
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)
ax.tick_params(axis='x', which='minor', bottom='off')
plt.setp(ax.get_xticklabels(), rotation=30, ha='right')
ax.tick_params(axis='y', which='minor', left='off')
ax.set_xlabel('Time on {0} UTC'.format(time_grid[0].datetime.date()))
fig.subplots_adjust(left=0.35, right=0.9, top=0.9, bottom=0.1)
plt.show()
.. _constraints-user_defined_constraints:
User-Defined Constraints
========================
There are many possible constraints that you could find useful which have
not been implemented (yet) in astroplan. This example will walk you through
creating your own constraint which will be compatible with the tools in the
``constraints`` module.
We will begin by defining an observer at Subaru and reading the text file of
stellar coordinates defined in the example above::
from astroplan import Observer, FixedTarget
from astropy.time import Time
subaru = Observer.at_site("Subaru")
time_range = Time(["2015-08-01 06:00", "2015-08-01 12:00"])
# Read in the table of targets
from astropy.io import ascii
target_table = ascii.read('targets.txt')
# Create astroplan.FixedTarget objects for each one in the table
from astropy.coordinates import SkyCoord
import astropy.units as u
targets = [FixedTarget(coord=SkyCoord(ra=ra*u.deg, dec=dec*u.deg), name=name)
for name, ra, dec in target_table]
In the previous section, you may have noticed that constraints are assembled by
making a list of calls to the initializers for classes like
`~astroplan.AltitudeConstraint` and `~astroplan.AirmassConstraint`. Each of
those constraint classes is subclassed from the abstract
`~astroplan.Constraint` class, and the custom constraint that we're going to
write must be as well.
In this example, let's design our constraint to ensure that all targets must
be within some angular separation from Vega – we'll call it
``VegaSeparationConstraint``. Two methods, ``__init__`` and
``compute_constraint`` must be written for our constraint to work:
* The ``__init__`` method will accept the minimum and maximum acceptable separations
a target could have from Vega.
* We'll also define a method ``compute_constraints`` which takes three
arguments: a `~astropy.time.Time` or array of times to test,
an `~astroplan.Observer` object, and some targets (a `~astropy.coordinates.SkyCoord`
object representing a single target or a list of targets).
``compute_constraints`` will return an array of booleans that describe whether
or not each target meets the constraints. The super class `~astroplan.Constraint` has a
``__call__`` method which will run your custom class's ``compute_constraints`` method
when you check if a target is observable using `~astroplan.is_observable`
or `~astroplan.is_always_observable`. This ``__call__`` method also checks the
arguments, converting single `~astroplan.FixedTarget` or lists of `~astroplan.FixedTarget`
objects into an `~astropy.coordinates.SkyCoord` object. The ``__call__`` method ensures the
returned array of booleans is the correct shape, so ``compute_constraints`` should not
normally be called directly - use the ``__call__`` method instead.
* We also want to provide the option of having the constraint output
a non-boolean score. Where being closer to the minimum separation
returns a higher score than being closer to the maximum separation.
Here's our ``VegaSeparationConstraint`` implementation::
from astroplan import Constraint, is_observable, min_best_rescale
from astropy.coordinates import Angle
import astropy.units as u
class VegaSeparationConstraint(Constraint):
"""
Constraint the separation from Vega
"""
def __init__(self, min=None, max=None, boolean_constraint=True):
"""
min : `~astropy.units.Quantity` or `None` (optional)
Minimum acceptable separation between Vega and target. `None`
indicates no limit.
max : `~astropy.units.Quantity` or `None` (optional)
Minimum acceptable separation between Vega and target. `None`
indicates no limit.
"""
self.min = min if min is not None else 0*u.deg
self.max = max if max is not None else 180*u.deg
self.boolean_constraint = boolean_constraint
def compute_constraint(self, times, observer, targets):
vega = SkyCoord(ra=279.23473479*u.deg, dec=38.78368896*u.deg)
# Calculate separation between target and vega
# Targets are automatically converted to SkyCoord objects
# by __call__ before compute_constraint is called.
vega_separation = vega.separation(targets)
if self.boolean_constraint:
mask = ((self.min < vega_separation) & (vega_separation < self.max))
return mask
# if we want to return a non-boolean score
else:
# rescale the vega_separation values so that they become
# scores between zero and one
rescale = min_best_rescale(vega_separation, self.min,
self.max, less_than_min=0)
return rescale
Then as in the earlier example, we can call our constraint::
>>> constraints = [VegaSeparationConstraint(min=5*u.deg, max=30*u.deg)]
>>> observability = is_observable(constraints, subaru, targets,
... time_range=time_range)
>>> print(observability)
[False False True False False False]
The resulting list of booleans indicates that the only target separated by
5 and 30 degrees from Vega is Albireo. Following this pattern, you can design
arbitrarily complex criteria for constraints.
By default, calling a constraint will try to broadcast the time and target arrays
against each other, and raise a `ValueError` if this is not possible. To see the
(target x time) array for the constraint, there is an optional ``grid_times_targets``
argument. Here we find the (target x time) array for the non-boolean score::
>>> constraint = VegaSeparationConstraint(min=5*u.deg, max=30*u.deg,
... boolean_constraint=False)
>>> print(constraint(subaru, targets, time_range=time_range,
... grid_times_targets=True))
[[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. ]
[ 0.57748686 0.57748686 0.57748686 0.57748686 0.57748686 0.57748686
0.57748686 0.57748686 0.57748686 0.57748686 0.57748686 0.57748686]
[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. ]]
The score of .5775 for Albireo indicates that it is slightly closer to
the 5 degree minimum than to the 30 degree maximum.
|