Skip to content

Instantly share code, notes, and snippets.

@bmorris3
Created August 14, 2015 22:20
Show Gist options
  • Save bmorris3/d2ea6cfee8e8e8a5d81a to your computer and use it in GitHub Desktop.
Save bmorris3/d2ea6cfee8e8e8a5d81a to your computer and use it in GitHub Desktop.
class SeparationConstraint(Constraint):
"""
Constrain the separation between one target and another target.
"""
def __init__(self, separation_target_function, separation_target_args=[],
min=None, max=None):
"""
Parameters
----------
separation_target_function : function
Function that computes the position of the object of avoidance.
separation_target_args : list
Arguments to the function ``separation_target_function``.
min : float or `None`
Minimum separation between the target. `None` indicates no limit.
max : float or `None`
Maximum separation between of the target. `None` indicates no limit.
"""
self.min = min
self.max = max
self.func = separation_target_function
self.args = separation_target_args
def _compute_constraint(self, time_range, observer, targets):
if not any([isinstance(i, Time) for i in self.args]):
separation_target_position = self.func(time_grid_from_range(time_range),
*self.args)
else:
separation_target_position = self.func(self.args)
targets = [target.coord if hasattr(target, 'coord') else target
for target in targets]
separation_degrees = np.array([separation_target_position.separation(target).deg
for target in targets])
print(separation_degrees)
if self.min is None and self.max is not None:
mask = separation_degrees < self.max.to(u.deg).value
elif self.max is None and self.min is not None:
mask = self.min.to(u.deg).value < separation_degrees
elif self.min is not None and self.max is not None:
mask = ((self.min.to(u.deg).value < separation_degrees) &
(separation_degrees < self.max.to(u.deg).value))
else:
raise ValueError("No max and/or min specified in "
"SeparationConstraint.")
return mask
############################################################################
############################################################################
from astroplan.constraints import (SeparationConstraint, is_observable,
is_always_observable)
from astropy.time import Time
from astroplan import Observer, FixedTarget
from astropy.coordinates import get_sun
import astropy.units as u
subaru = Observer.at_site("Subaru")
time_range = Time(["2015-08-01 06:00", "2015-08-01 12:00"])
target_names = ["Polaris", "Vega", "Albireo", "Algol", "Rigel", "Regulus"]
targets = [FixedTarget.from_name(name) for name in target_names]
constraint_list = [SeparationConstraint(get_sun, min=10*u.deg)]
# Are targets *ever* observable in the time range?
ever = is_observable(constraint_list, time_range, targets, subaru)
# Are targets *always* observable in the time range?
always = is_always_observable(constraint_list, time_range, targets, subaru)
print(ever)
print(always)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment