/usr/lib/python2.7/dist-packages/astroplan/constraints.py is in python-astroplan 0.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Specify and constraints to determine which targets are observable for
an observer.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
# Standard library
from abc import ABCMeta, abstractmethod
import datetime
import warnings
# Third-party
from astropy.time import Time
import astropy.units as u
from astropy.coordinates import get_body, get_sun, get_moon, SkyCoord
from astropy import table
import numpy as np
from numpy.lib.stride_tricks import as_strided
# Package
from .moon import moon_illumination
from .utils import time_grid_from_range
from .target import get_skycoord
__all__ = ["AltitudeConstraint", "AirmassConstraint", "AtNightConstraint",
"is_observable", "is_always_observable", "time_grid_from_range",
"SunSeparationConstraint", "MoonSeparationConstraint",
"MoonIlluminationConstraint", "LocalTimeConstraint",
"PrimaryEclipseConstraint", "SecondaryEclipseConstraint",
"Constraint", "TimeConstraint", "observability_table",
"months_observable", "max_best_rescale", "min_best_rescale",
"PhaseConstraint", "is_event_observable"]
def _make_cache_key(times, targets):
"""
Make a unique key to reference this combination of ``times`` and ``targets``.
Often, we wish to store expensive calculations for a combination of
``targets`` and ``times`` in a cache on an ``observer``` object. This
routine will provide an appropriate, hashable, key to store these
calculations in a dictionary.
Parameters
----------
times : `~astropy.time.Time`
Array of times on which to test the constraint.
targets : `~astropy.coordinates.SkyCoord`
Target or list of targets.
Returns
-------
cache_key : tuple
A hashable tuple for use as a cache key
"""
# make a tuple from times
try:
timekey = tuple(times.jd) + times.shape
except BaseException: # must be scalar
timekey = (times.jd,)
# make hashable thing from targets coords
try:
if hasattr(targets, 'frame'):
# treat as a SkyCoord object. Accessing the longitude
# attribute of the frame data should be unique and is
# quicker than accessing the ra attribute.
targkey = tuple(targets.frame.data.lon.value.ravel()) + targets.shape
else:
# assume targets is a string.
targkey = (targets,)
except BaseException:
targkey = (targets.frame.data.lon,)
return timekey + targkey
def _get_altaz(times, observer, targets, force_zero_pressure=False):
"""
Calculate alt/az for ``target`` at times linearly spaced between
the two times in ``time_range`` with grid spacing ``time_resolution``
for ``observer``.
Cache the result on the ``observer`` object.
Parameters
----------
times : `~astropy.time.Time`
Array of times on which to test the constraint.
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets.
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``.
force_zero_pressure : bool
Forcefully use 0 pressure.
Returns
-------
altaz_dict : dict
Dictionary containing two key-value pairs. (1) 'times' contains the
times for the alt/az computations, (2) 'altaz' contains the
corresponding alt/az coordinates at those times.
"""
if not hasattr(observer, '_altaz_cache'):
observer._altaz_cache = {}
# convert times, targets to tuple for hashing
aakey = _make_cache_key(times, targets)
if aakey not in observer._altaz_cache:
try:
if force_zero_pressure:
observer_old_pressure = observer.pressure
observer.pressure = 0
altaz = observer.altaz(times, targets, grid_times_targets=False)
observer._altaz_cache[aakey] = dict(times=times,
altaz=altaz)
finally:
if force_zero_pressure:
observer.pressure = observer_old_pressure
return observer._altaz_cache[aakey]
def _get_moon_data(times, observer, force_zero_pressure=False):
"""
Calculate moon altitude az and illumination for an array of times for
``observer``.
Cache the result on the ``observer`` object.
Parameters
----------
times : `~astropy.time.Time`
Array of times on which to test the constraint.
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``.
force_zero_pressure : bool
Forcefully use 0 pressure.
Returns
-------
moon_dict : dict
Dictionary containing three key-value pairs. (1) 'times' contains the
times for the computations, (2) 'altaz' contains the
corresponding alt/az coordinates at those times and (3) contains
the moon illumination for those times.
"""
if not hasattr(observer, '_moon_cache'):
observer._moon_cache = {}
# convert times to tuple for hashing
aakey = _make_cache_key(times, 'moon')
if aakey not in observer._moon_cache:
try:
if force_zero_pressure:
observer_old_pressure = observer.pressure
observer.pressure = 0
altaz = observer.moon_altaz(times)
illumination = np.array(moon_illumination(times))
observer._moon_cache[aakey] = dict(times=times,
illum=illumination,
altaz=altaz)
finally:
if force_zero_pressure:
observer.pressure = observer_old_pressure
return observer._moon_cache[aakey]
def _get_meridian_transit_times(times, observer, targets):
"""
Calculate next meridian transit for an array of times for ``targets`` and
``observer``.
Cache the result on the ``observer`` object.
Parameters
----------
times : `~astropy.time.Time`
Array of times on which to test the constraint
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets
Returns
-------
time_dict : dict
Dictionary containing a key-value pair. 'times' contains the
meridian_transit times.
"""
if not hasattr(observer, '_meridian_transit_cache'):
observer._meridian_transit_cache = {}
# convert times to tuple for hashing
aakey = _make_cache_key(times, targets)
if aakey not in observer._meridian_transit_cache:
meridian_transit_times = observer.target_meridian_transit_time(times, targets)
observer._meridian_transit_cache[aakey] = dict(times=meridian_transit_times)
return observer._meridian_transit_cache[aakey]
@abstractmethod
class Constraint(object):
"""
Abstract class for objects defining observational constraints.
"""
__metaclass__ = ABCMeta
def __call__(self, observer, targets, times=None,
time_range=None, time_grid_resolution=0.5*u.hour,
grid_times_targets=False):
"""
Compute the constraint for this class
Parameters
----------
observer : `~astroplan.Observer`
the observation location from which to apply the constraints
targets : sequence of `~astroplan.Target`
The targets on which to apply the constraints.
times : `~astropy.time.Time`
The times to compute the constraint.
WHAT HAPPENS WHEN BOTH TIMES AND TIME_RANGE ARE SET?
time_range : `~astropy.time.Time` (length = 2)
Lower and upper bounds on time sequence.
time_grid_resolution : `~astropy.units.quantity`
Time-grid spacing
grid_times_targets : bool
if True, grids the constraint result with targets along the first
index and times along the second. Otherwise, we rely on broadcasting
the shapes together using standard numpy rules.
Returns
-------
constraint_result : 1D or 2D array of float or bool
The constraints. If 2D with targets along the first index and times along
the second.
"""
if times is None and time_range is not None:
times = time_grid_from_range(time_range,
time_resolution=time_grid_resolution)
if grid_times_targets:
targets = get_skycoord(targets)
# TODO: these broadcasting operations are relatively slow
# but there is potential for huge speedup if the end user
# disables gridding and re-shapes the coords themselves
# prior to evaluating multiple constraints.
if targets.isscalar:
# ensure we have a (1, 1) shape coord
targets = SkyCoord(np.tile(targets, 1))[:, np.newaxis]
else:
targets = targets[..., np.newaxis]
times, targets = observer._preprocess_inputs(times, targets, grid_times_targets=False)
result = self.compute_constraint(times, observer, targets)
# make sure the output has the same shape as would result from
# broadcasting times and targets against each other
if targets is not None:
# broadcasting times v targets is slow due to
# complex nature of these objects. We make
# to simple numpy arrays of the same shape and
# broadcast these to find the correct shape
shp1, shp2 = times.shape, targets.shape
x = np.array([1])
a = as_strided(x, shape=shp1, strides=[0] * len(shp1))
b = as_strided(x, shape=shp2, strides=[0] * len(shp2))
output_shape = np.broadcast(a, b).shape
if output_shape != np.array(result).shape:
result = np.broadcast_to(result, output_shape)
return result
@abstractmethod
def compute_constraint(self, times, observer, targets):
"""
Actually do the real work of computing the constraint. Subclasses
override this.
Parameters
----------
times : `~astropy.time.Time`
The times to compute the constraint
observer : `~astroplan.Observer`
the observaton location from which to apply the constraints
targets : sequence of `~astroplan.Target`
The targets on which to apply the constraints.
Returns
-------
constraint_result : 2D array of float or bool
The constraints, with targets along the first index and times along
the second.
"""
# Should be implemented on each subclass of Constraint
raise NotImplementedError
class AltitudeConstraint(Constraint):
"""
Constrain the altitude of the target.
.. note::
This can misbehave if you try to constrain negative altitudes, as
the `~astropy.coordinates.AltAz` frame tends to mishandle negative
Parameters
----------
min : `~astropy.units.Quantity` or `None`
Minimum altitude of the target (inclusive). `None` indicates no limit.
max : `~astropy.units.Quantity` or `None`
Maximum altitude of the target (inclusive). `None` indicates no limit.
boolean_constraint : bool
If True, the constraint is treated as a boolean (True for within the
limits and False for outside). If False, the constraint returns a
float on [0, 1], where 0 is the min altitude and 1 is the max.
"""
def __init__(self, min=None, max=None, boolean_constraint=True):
if min is None:
self.min = -90*u.deg
else:
self.min = min
if max is None:
self.max = 90*u.deg
else:
self.max = max
self.boolean_constraint = boolean_constraint
def compute_constraint(self, times, observer, targets):
cached_altaz = _get_altaz(times, observer, targets)
alt = cached_altaz['altaz'].alt
if self.boolean_constraint:
lowermask = self.min <= alt
uppermask = alt <= self.max
return lowermask & uppermask
else:
return max_best_rescale(alt, self.min, self.max)
class AirmassConstraint(AltitudeConstraint):
"""
Constrain the airmass of a target.
In the current implementation the airmass is approximated by the secant of
the zenith angle.
.. note::
The ``max`` and ``min`` arguments appear in the order (max, min)
in this initializer to support the common case for users who care
about the upper limit on the airmass (``max``) and not the lower
limit.
Parameters
----------
max : float or `None`
Maximum airmass of the target. `None` indicates no limit.
min : float or `None`
Minimum airmass of the target. `None` indicates no limit.
boolean_contstraint : bool
Examples
--------
To create a constraint that requires the airmass be "better than 2",
i.e. at a higher altitude than airmass=2::
AirmassConstraint(2)
"""
def __init__(self, max=None, min=1, boolean_constraint=True):
self.min = min
self.max = max
self.boolean_constraint = boolean_constraint
def compute_constraint(self, times, observer, targets):
cached_altaz = _get_altaz(times, observer, targets)
secz = cached_altaz['altaz'].secz.value
if self.boolean_constraint:
if self.min is None and self.max is not None:
mask = secz <= self.max
elif self.max is None and self.min is not None:
mask = self.min <= secz
elif self.min is not None and self.max is not None:
mask = (self.min <= secz) & (secz <= self.max)
else:
raise ValueError("No max and/or min specified in "
"AirmassConstraint.")
return mask
else:
if self.max is None:
raise ValueError("Cannot have a float AirmassConstraint if max is None.")
else:
mx = self.max
mi = 1 if self.min is None else self.min
# values below 1 should be disregarded
return min_best_rescale(secz, mi, mx, less_than_min=0)
class AtNightConstraint(Constraint):
"""
Constrain the Sun to be below ``horizon``.
"""
@u.quantity_input(horizon=u.deg)
def __init__(self, max_solar_altitude=0*u.deg, force_pressure_zero=True):
"""
Parameters
----------
max_solar_altitude : `~astropy.units.Quantity`
The altitude of the sun below which it is considered to be "night"
(inclusive).
force_pressure_zero : bool (optional)
Force the pressure to zero for solar altitude calculations. This
avoids errors in the altitude of the Sun that can occur when the
Sun is below the horizon and the corrections for atmospheric
refraction return nonsense values.
"""
self.max_solar_altitude = max_solar_altitude
self.force_pressure_zero = force_pressure_zero
@classmethod
def twilight_civil(cls, **kwargs):
"""
Consider nighttime as time between civil twilights (-6 degrees).
"""
return cls(max_solar_altitude=-6*u.deg, **kwargs)
@classmethod
def twilight_nautical(cls, **kwargs):
"""
Consider nighttime as time between nautical twilights (-12 degrees).
"""
return cls(max_solar_altitude=-12*u.deg, **kwargs)
@classmethod
def twilight_astronomical(cls, **kwargs):
"""
Consider nighttime as time between astronomical twilights (-18 degrees).
"""
return cls(max_solar_altitude=-18*u.deg, **kwargs)
def _get_solar_altitudes(self, times, observer, targets):
if not hasattr(observer, '_altaz_cache'):
observer._altaz_cache = {}
aakey = _make_cache_key(times, 'sun')
if aakey not in observer._altaz_cache:
try:
if self.force_pressure_zero:
observer_old_pressure = observer.pressure
observer.pressure = 0
# find solar altitude at these times
altaz = observer.altaz(times, get_sun(times))
altitude = altaz.alt
# cache the altitude
observer._altaz_cache[aakey] = dict(times=times,
altitude=altitude)
finally:
if self.force_pressure_zero:
observer.pressure = observer_old_pressure
else:
altitude = observer._altaz_cache[aakey]['altitude']
return altitude
def compute_constraint(self, times, observer, targets):
solar_altitude = self._get_solar_altitudes(times, observer, targets)
mask = solar_altitude <= self.max_solar_altitude
return mask
class SunSeparationConstraint(Constraint):
"""
Constrain the distance between the Sun and some targets.
"""
def __init__(self, min=None, max=None):
"""
Parameters
----------
min : `~astropy.units.Quantity` or `None` (optional)
Minimum acceptable separation between Sun and target (inclusive).
`None` indicates no limit.
max : `~astropy.units.Quantity` or `None` (optional)
Minimum acceptable separation between Sun and target (inclusive).
`None` indicates no limit.
"""
self.min = min
self.max = max
def compute_constraint(self, times, observer, targets):
# use get_body rather than get sun here, since
# it returns the Sun's coordinates in an observer
# centred frame, so the separation is as-seen
# by the observer.
# 'get_sun' returns ICRS coords.
sun = get_body('sun', times, location=observer.location)
solar_separation = sun.separation(targets)
if self.min is None and self.max is not None:
mask = self.max >= solar_separation
elif self.max is None and self.min is not None:
mask = self.min <= solar_separation
elif self.min is not None and self.max is not None:
mask = ((self.min <= solar_separation) &
(solar_separation <= self.max))
else:
raise ValueError("No max and/or min specified in "
"SunSeparationConstraint.")
return mask
class MoonSeparationConstraint(Constraint):
"""
Constrain the distance between the Earth's moon and some targets.
"""
def __init__(self, min=None, max=None, ephemeris=None):
"""
Parameters
----------
min : `~astropy.units.Quantity` or `None` (optional)
Minimum acceptable separation between moon and target (inclusive).
`None` indicates no limit.
max : `~astropy.units.Quantity` or `None` (optional)
Maximum acceptable separation between moon and target (inclusive).
`None` indicates no limit.
ephemeris : str, optional
Ephemeris to use. If not given, use the one set with
``astropy.coordinates.solar_system_ephemeris.set`` (which is
set to 'builtin' by default).
"""
self.min = min
self.max = max
self.ephemeris = ephemeris
def compute_constraint(self, times, observer, targets):
# removed the location argument here, which causes small <1 deg
# innacuracies, but it is needed until astropy PR #5897 is released
# which should be astropy 1.3.2
moon = get_moon(times,
ephemeris=self.ephemeris)
# note to future editors - the order matters here
# moon.separation(targets) is NOT the same as targets.separation(moon)
# the former calculates the separation in the frame of the moon coord
# which is GCRS, and that is what we want.
moon_separation = moon.separation(targets)
if self.min is None and self.max is not None:
mask = self.max >= moon_separation
elif self.max is None and self.min is not None:
mask = self.min <= moon_separation
elif self.min is not None and self.max is not None:
mask = ((self.min <= moon_separation) &
(moon_separation <= self.max))
else:
raise ValueError("No max and/or min specified in "
"MoonSeparationConstraint.")
return mask
class MoonIlluminationConstraint(Constraint):
"""
Constrain the fractional illumination of the Earth's moon.
Constraint is also satisfied if the Moon has set.
"""
def __init__(self, min=None, max=None, ephemeris=None):
"""
Parameters
----------
min : float or `None` (optional)
Minimum acceptable fractional illumination (inclusive). `None`
indicates no limit.
max : float or `None` (optional)
Maximum acceptable fractional illumination (inclusive). `None`
indicates no limit.
ephemeris : str, optional
Ephemeris to use. If not given, use the one set with
`~astropy.coordinates.solar_system_ephemeris` (which is
set to 'builtin' by default).
"""
self.min = min
self.max = max
self.ephemeris = ephemeris
@classmethod
def dark(cls, min=None, max=0.25, **kwargs):
"""
initialize a `~astroplan.constraints.MoonIlluminationConstraint`
with defaults of no minimum and a maximum of 0.25
Parameters
----------
min : float or `None` (optional)
Minimum acceptable fractional illumination (inclusive). `None`
indicates no limit.
max : float or `None` (optional)
Maximum acceptable fractional illumination (inclusive). `None`
indicates no limit.
"""
return cls(min, max, **kwargs)
@classmethod
def grey(cls, min=0.25, max=0.65, **kwargs):
"""
initialize a `~astroplan.constraints.MoonIlluminationConstraint`
with defaults of a minimum of 0.25 and a maximum of 0.65
Parameters
----------
min : float or `None` (optional)
Minimum acceptable fractional illumination (inclusive). `None`
indicates no limit.
max : float or `None` (optional)
Maximum acceptable fractional illumination (inclusive). `None`
indicates no limit.
"""
return cls(min, max, **kwargs)
@classmethod
def bright(cls, min=0.65, max=None, **kwargs):
"""
initialize a `~astroplan.constraints.MoonIlluminationConstraint`
with defaults of a minimum of 0.65 and no maximum
Parameters
----------
min : float or `None` (optional)
Minimum acceptable fractional illumination (inclusive). `None`
indicates no limit.
max : float or `None` (optional)
Maximum acceptable fractional illumination (inclusive). `None`
indicates no limit.
"""
return cls(min, max, **kwargs)
def compute_constraint(self, times, observer, targets):
# first is the moon up?
cached_moon = _get_moon_data(times, observer)
moon_alt = cached_moon['altaz'].alt
moon_down_mask = moon_alt < 0
moon_up_mask = moon_alt >= 0
illumination = cached_moon['illum']
if self.min is None and self.max is not None:
mask = (self.max >= illumination) | moon_down_mask
elif self.max is None and self.min is not None:
mask = (self.min <= illumination) & moon_up_mask
elif self.min is not None and self.max is not None:
mask = ((self.min <= illumination) &
(illumination <= self.max)) & moon_up_mask
else:
raise ValueError("No max and/or min specified in "
"MoonSeparationConstraint.")
return mask
class LocalTimeConstraint(Constraint):
"""
Constrain the observable hours.
"""
def __init__(self, min=None, max=None):
"""
Parameters
----------
min : `~datetime.time`
Earliest local time (inclusive). `None` indicates no limit.
max : `~datetime.time`
Latest local time (inclusive). `None` indicates no limit.
Examples
--------
Constrain the observations to targets that are observable between
23:50 and 04:08 local time:
>>> from astroplan import Observer
>>> from astroplan.constraints import LocalTimeConstraint
>>> import datetime as dt
>>> subaru = Observer.at_site("Subaru", timezone="US/Hawaii")
>>> # bound times between 23:50 and 04:08 local Hawaiian time
>>> constraint = LocalTimeConstraint(min=dt.time(23,50), max=dt.time(4,8))
"""
self.min = min
self.max = max
if self.min is None and self.max is None:
raise ValueError("You must at least supply either a minimum or a maximum time.")
if self.min is not None:
if not isinstance(self.min, datetime.time):
raise TypeError("Time limits must be specified as datetime.time objects.")
if self.max is not None:
if not isinstance(self.max, datetime.time):
raise TypeError("Time limits must be specified as datetime.time objects.")
def compute_constraint(self, times, observer, targets):
timezone = None
# get timezone from time objects, or from observer
if self.min is not None:
timezone = self.min.tzinfo
elif self.max is not None:
timezone = self.max.tzinfo
if timezone is None:
timezone = observer.timezone
if self.min is not None:
min_time = self.min
else:
min_time = self.min = datetime.time(0, 0, 0)
if self.max is not None:
max_time = self.max
else:
max_time = datetime.time(23, 59, 59)
# If time limits occur on same day:
if self.min < self.max:
try:
mask = np.array([min_time <= t.time() <= max_time for t in times.datetime])
except BaseException: # use np.bool so shape queries don't cause problems
mask = np.bool_(min_time <= times.datetime.time() <= max_time)
# If time boundaries straddle midnight:
else:
try:
mask = np.array([(t.time() >= min_time) or
(t.time() <= max_time) for t in times.datetime])
except BaseException:
mask = np.bool_((times.datetime.time() >= min_time) or
(times.datetime.time() <= max_time))
return mask
class TimeConstraint(Constraint):
"""Constrain the observing time to be within certain time limits.
An example use case for this class would be to associate an acceptable
time range with a specific observing block. This can be useful if not
all observing blocks are valid over the time limits used in calls
to `is_observable` or `is_always_observable`.
"""
def __init__(self, min=None, max=None):
"""
Parameters
----------
min : `~astropy.time.Time`
Earliest time (inclusive). `None` indicates no limit.
max : `~astropy.time.Time`
Latest time (inclusive). `None` indicates no limit.
Examples
--------
Constrain the observations to targets that are observable between
2016-03-28 and 2016-03-30:
>>> from astroplan import Observer
>>> from astropy.time import Time
>>> subaru = Observer.at_site("Subaru")
>>> t1 = Time("2016-03-28T12:00:00")
>>> t2 = Time("2016-03-30T12:00:00")
>>> constraint = TimeConstraint(t1,t2)
"""
self.min = min
self.max = max
if self.min is None and self.max is None:
raise ValueError("You must at least supply either a minimum or a "
"maximum time.")
if self.min is not None:
if not isinstance(self.min, Time):
raise TypeError("Time limits must be specified as "
"astropy.time.Time objects.")
if self.max is not None:
if not isinstance(self.max, Time):
raise TypeError("Time limits must be specified as "
"astropy.time.Time objects.")
def compute_constraint(self, times, observer, targets):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
min_time = Time("1950-01-01T00:00:00") if self.min is None else self.min
max_time = Time("2120-01-01T00:00:00") if self.max is None else self.max
mask = np.logical_and(times > min_time, times < max_time)
return mask
class PrimaryEclipseConstraint(Constraint):
"""
Constrain observations to times during primary eclipse.
"""
def __init__(self, eclipsing_system):
"""
Parameters
----------
eclipsing_system : `~astroplan.periodic.EclipsingSystem`
System which must be in primary eclipse.
"""
self.eclipsing_system = eclipsing_system
def compute_constraint(self, times, observer=None, targets=None):
mask = self.eclipsing_system.in_primary_eclipse(times)
return mask
class SecondaryEclipseConstraint(Constraint):
"""
Constrain observations to times during secondary eclipse.
"""
def __init__(self, eclipsing_system):
"""
Parameters
----------
eclipsing_system : `~astroplan.periodic.EclipsingSystem`
System which must be in secondary eclipse.
"""
self.eclipsing_system = eclipsing_system
def compute_constraint(self, times, observer=None, targets=None):
mask = self.eclipsing_system.in_secondary_eclipse(times)
return mask
class PhaseConstraint(Constraint):
"""
Constrain observations to times in some range of phases for a periodic event
(e.g.~transiting exoplanets, eclipsing binaries).
"""
def __init__(self, periodic_event, min=None, max=None):
"""
Parameters
----------
periodic_event : `~astroplan.periodic.PeriodicEvent` or subclass
System on which to compute the phase. For example, the system
could be an eclipsing or non-eclipsing binary, or exoplanet system.
min : float (optional)
Minimum phase (inclusive) on interval [0, 1). Default is zero.
max : float (optional)
Maximum phase (inclusive) on interval [0, 1). Default is one.
Examples
--------
To constrain observations on orbital phases between 0.4 and 0.6,
>>> from astroplan import PeriodicEvent
>>> from astropy.time import Time
>>> import astropy.units as u
>>> binary = PeriodicEvent(epoch=Time('2017-01-01 02:00'), period=1*u.day)
>>> constraint = PhaseConstraint(binary, min=0.4, max=0.6)
The minimum and maximum phase must be described on the interval [0, 1).
To constrain observations on orbital phases between 0.6 and 1.2, for
example, you should subtract one from the second number:
>>> constraint = PhaseConstraint(binary, min=0.6, max=0.2)
"""
self.periodic_event = periodic_event
if (min < 0) or (min > 1) or (max < 0) or (max > 1):
raise ValueError('The minimum of the PhaseConstraint must be within'
' the interval [0, 1).')
self.min = min if min is not None else 0.0
self.max = max if max is not None else 1.0
def compute_constraint(self, times, observer=None, targets=None):
phase = self.periodic_event.phase(times)
mask = np.where(self.max > self.min,
(phase >= self.min) & (phase <= self.max),
(phase >= self.min) | (phase <= self.max))
return mask
def is_always_observable(constraints, observer, targets, times=None,
time_range=None, time_grid_resolution=0.5*u.hour):
"""
A function to determine whether ``targets`` are always observable throughout
``time_range`` given constraints in the ``constraints_list`` for a
particular ``observer``.
Parameters
----------
constraints : list or `~astroplan.constraints.Constraint`
Observational constraint(s)
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets
times : `~astropy.time.Time` (optional)
Array of times on which to test the constraint
time_range : `~astropy.time.Time` (optional)
Lower and upper bounds on time sequence, with spacing
``time_resolution``. This will be passed as the first argument into
`~astroplan.time_grid_from_range`.
time_grid_resolution : `~astropy.units.Quantity` (optional)
If ``time_range`` is specified, determine whether constraints are met
between test times in ``time_range`` by checking constraint at
linearly-spaced times separated by ``time_resolution``. Default is 0.5
hours.
Returns
-------
ever_observable : list
List of booleans of same length as ``targets`` for whether or not each
target is observable in the time range given the constraints.
"""
if not hasattr(constraints, '__len__'):
constraints = [constraints]
applied_constraints = [constraint(observer, targets, times=times,
time_range=time_range,
time_grid_resolution=time_grid_resolution,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and.reduce(applied_constraints)
return np.all(constraint_arr, axis=1)
def is_observable(constraints, observer, targets, times=None,
time_range=None, time_grid_resolution=0.5*u.hour):
"""
Determines if the ``targets`` are observable during ``time_range`` given
constraints in ``constraints_list`` for a particular ``observer``.
Parameters
----------
constraints : list or `~astroplan.constraints.Constraint`
Observational constraint(s)
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets
times : `~astropy.time.Time` (optional)
Array of times on which to test the constraint
time_range : `~astropy.time.Time` (optional)
Lower and upper bounds on time sequence, with spacing
``time_resolution``. This will be passed as the first argument into
`~astroplan.time_grid_from_range`.
time_grid_resolution : `~astropy.units.Quantity` (optional)
If ``time_range`` is specified, determine whether constraints are met
between test times in ``time_range`` by checking constraint at
linearly-spaced times separated by ``time_resolution``. Default is 0.5
hours.
Returns
-------
ever_observable : list
List of booleans of same length as ``targets`` for whether or not each
target is ever observable in the time range given the constraints.
"""
if not hasattr(constraints, '__len__'):
constraints = [constraints]
applied_constraints = [constraint(observer, targets, times=times,
time_range=time_range,
time_grid_resolution=time_grid_resolution,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and.reduce(applied_constraints)
return np.any(constraint_arr, axis=1)
def is_event_observable(constraints, observer, target, times=None,
times_ingress_egress=None):
"""
Determines if the ``target`` is observable at each time in ``times``, given
constraints in ``constraints`` for a particular ``observer``.
Parameters
----------
constraints : list or `~astroplan.constraints.Constraint`
Observational constraint(s)
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
target : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target
times : `~astropy.time.Time` (optional)
Array of mid-event times on which to test the constraints
times_ingress_egress : `~astropy.time.Time` (optional)
Array of ingress and egress times for ``N`` events, with shape
(``N``, 2).
Returns
-------
event_observable : `~numpy.ndarray`
Array of booleans of same length as ``times`` for whether or not the
target is ever observable at each time, given the constraints.
"""
if not hasattr(constraints, '__len__'):
constraints = [constraints]
if times is not None:
applied_constraints = [constraint(observer, target, times=times,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and.reduce(applied_constraints)
else:
times_ing = times_ingress_egress[:, 0]
times_egr = times_ingress_egress[:, 1]
applied_constraints_ing = [constraint(observer, target, times=times_ing,
grid_times_targets=True)
for constraint in constraints]
applied_constraints_egr = [constraint(observer, target, times=times_egr,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and(np.logical_and.reduce(applied_constraints_ing),
np.logical_and.reduce(applied_constraints_egr))
return constraint_arr
def months_observable(constraints, observer, targets,
time_grid_resolution=0.5*u.hour):
"""
Determines which month the specified ``targets`` are observable for a
specific ``observer``, given the supplied ``constriants``.
Parameters
----------
constraints : list or `~astroplan.constraints.Constraint`
Observational constraint(s)
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets
time_grid_resolution : `~astropy.units.Quantity` (optional)
If ``time_range`` is specified, determine whether constraints are met
between test times in ``time_range`` by checking constraint at
linearly-spaced times separated by ``time_resolution``. Default is 0.5
hours.
Returns
-------
observable_months : list
List of sets of unique integers representing each month that a target is
observable, one set per target. These integers are 1-based so that
January maps to 1, February maps to 2, etc.
"""
# TODO: This method could be sped up a lot by dropping to the trigonometric
# altitude calculations.
if not hasattr(constraints, '__len__'):
constraints = [constraints]
# Calculate throughout the year of 2014 so as not to require forward
# extrapolation off of the IERS tables
time_range = Time(['2014-01-01', '2014-12-31'])
times = time_grid_from_range(time_range, time_grid_resolution)
# TODO: This method could be sped up a lot by dropping to the trigonometric
# altitude calculations.
applied_constraints = [constraint(observer, targets,
times=times,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and.reduce(applied_constraints)
months_observable = []
for target, observable in zip(targets, constraint_arr):
s = set([t.datetime.month for t in times[observable]])
months_observable.append(s)
return months_observable
def observability_table(constraints, observer, targets, times=None,
time_range=None, time_grid_resolution=0.5*u.hour):
"""
Creates a table with information about observability for all the ``targets``
over the requested ``time_range``, given the constraints in
``constraints_list`` for ``observer``.
Parameters
----------
constraints : list or `~astroplan.constraints.Constraint`
Observational constraint(s)
observer : `~astroplan.Observer`
The observer who has constraints ``constraints``
targets : {list, `~astropy.coordinates.SkyCoord`, `~astroplan.FixedTarget`}
Target or list of targets
times : `~astropy.time.Time` (optional)
Array of times on which to test the constraint
time_range : `~astropy.time.Time` (optional)
Lower and upper bounds on time sequence, with spacing
``time_resolution``. This will be passed as the first argument into
`~astroplan.time_grid_from_range`.
time_grid_resolution : `~astropy.units.Quantity` (optional)
If ``time_range`` is specified, determine whether constraints are met
between test times in ``time_range`` by checking constraint at
linearly-spaced times separated by ``time_resolution``. Default is 0.5
hours.
Returns
-------
observability_table : `~astropy.table.Table`
A Table containing the observability information for each of the
``targets``. The table contains four columns with information about the
target and it's observability: ``'target name'``, ``'ever observable'``,
``'always observable'``, and ``'fraction of time observable'``. It also
contains metadata entries ``'times'`` (with an array of all the times),
``'observer'`` (the `~astroplan.Observer` object), and ``'constraints'``
(containing the supplied ``constraints``).
"""
if not hasattr(constraints, '__len__'):
constraints = [constraints]
applied_constraints = [constraint(observer, targets, times=times,
time_range=time_range,
time_grid_resolution=time_grid_resolution,
grid_times_targets=True)
for constraint in constraints]
constraint_arr = np.logical_and.reduce(applied_constraints)
colnames = ['target name', 'ever observable', 'always observable',
'fraction of time observable']
target_names = [target.name for target in targets]
ever_obs = np.any(constraint_arr, axis=1)
always_obs = np.all(constraint_arr, axis=1)
frac_obs = np.sum(constraint_arr, axis=1) / constraint_arr.shape[1]
tab = table.Table(names=colnames, data=[target_names, ever_obs, always_obs,
frac_obs])
if times is None and time_range is not None:
times = time_grid_from_range(time_range,
time_resolution=time_grid_resolution)
tab.meta['times'] = times.datetime
tab.meta['observer'] = observer
tab.meta['constraints'] = constraints
return tab
def min_best_rescale(vals, min_val, max_val, less_than_min=1):
"""
rescales an input array ``vals`` to be a score (between zero and one),
where the ``min_val`` goes to one, and the ``max_val`` goes to zero.
Parameters
----------
vals : array-like
the values that need to be rescaled to be between 0 and 1
min_val : float
worst acceptable value (rescales to 0)
max_val : float
best value cared about (rescales to 1)
less_than_min : 0 or 1
what is returned for ``vals`` below ``min_val``. (in some cases
anything less than ``min_val`` should also return one,
in some cases it should return zero)
Returns
-------
array of floats between 0 and 1 inclusive rescaled so that
``vals`` equal to ``max_val`` equal 0 and those equal to
``min_val`` equal 1
Examples
--------
rescale airmasses to between 0 and 1, with the best (1)
and worst (2.25). All values outside the range should
return 0.
>>> from astroplan.constraints import min_best_rescale
>>> import numpy as np
>>> airmasses = np.array([1, 1.5, 2, 3, 0])
>>> min_best_rescale(airmasses, 1, 2.25, less_than_min = 0)
array([ 1. , 0.6, 0.2, 0. , 0. ])
"""
rescaled = (vals - max_val) / (min_val - max_val)
below = vals < min_val
above = vals > max_val
rescaled[below] = less_than_min
rescaled[above] = 0
return rescaled
def max_best_rescale(vals, min_val, max_val, greater_than_max=1):
"""
rescales an input array ``vals`` to be a score (between zero and one),
where the ``max_val`` goes to one, and the ``min_val`` goes to zero.
Parameters
----------
vals : array-like
the values that need to be rescaled to be between 0 and 1
min_val : float
worst acceptable value (rescales to 0)
max_val : float
best value cared about (rescales to 1)
greater_than_max : 0 or 1
what is returned for ``vals`` above ``max_val``. (in some cases
anything higher than ``max_val`` should also return one,
in some cases it should return zero)
Returns
-------
array of floats between 0 and 1 inclusive rescaled so that
``vals`` equal to ``min_val`` equal 0 and those equal to
``max_val`` equal 1
Examples
--------
rescale an array of altitudes to be between 0 and 1,
with the best (60) going to 1 and worst (35) going to
0. For values outside the range, the rescale should
return 0 below 35 and 1 above 60.
>>> from astroplan.constraints import max_best_rescale
>>> import numpy as np
>>> altitudes = np.array([20, 30, 40, 45, 55, 70])
>>> max_best_rescale(altitudes, 35, 60)
array([ 0. , 0. , 0.2, 0.4, 0.8, 1. ])
"""
rescaled = (vals - min_val) / (max_val - min_val)
below = vals < min_val
above = vals > max_val
rescaled[below] = 0
rescaled[above] = greater_than_max
return rescaled
|