From 7f539a8dc7df39611fc0e9b18067013a73456091 Mon Sep 17 00:00:00 2001 From: Brett Morris Date: Mon, 17 Aug 2015 16:28:44 -0700 Subject: [PATCH 1/4] Beginning to implement a version of NonFixedTarget --- astroplan/core.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/astroplan/core.py b/astroplan/core.py index 0349ddba..df2ba3f8 100644 --- a/astroplan/core.py +++ b/astroplan/core.py @@ -1802,8 +1802,35 @@ def _fixed_target_from_name_mock(cls, name): class NonFixedTarget(Target): """ - Placeholder for future function. + An object that is not "fixed" with respect to the celestial sphere. + + Currently only some celestial objects are supported in this class. """ + def __init__(self, coord_function=None, name=None): + """ + TODO: Docstring. + """ + self.name = name.lower() if name is not None else name + self.coord_function = coord_function + + def at(self, *args, **kwargs): + """ + Get `~astropy.coordinates.SkyCoord` for the `~astroplan.NonFixedTarget` + at a given time, location, etc. + + Parameters + ---------- + Any parameters passed to the + """ + return self.coord_function(*args, **kwargs) + + @classmethod + def from_function(cls, coord_function, name=None): + """ + Initialize a `~astropy.NonFixedTarget` by passing in a function that + computes a `~astropy.coordinates.SkyCoord` for the target object. + """ + return cls(coord_function=coord_function, name=name) class Constraint(object): """ From a2a6a3a85126affafa5fb847f33067bc8983bbf4 Mon Sep 17 00:00:00 2001 From: Brett Morris Date: Mon, 17 Aug 2015 16:53:57 -0700 Subject: [PATCH 2/4] Adding tests for initial NonFixedTarget --- astroplan/core.py | 33 +++++++++++++++++++++------------ astroplan/tests/test_core.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/astroplan/core.py b/astroplan/core.py index df2ba3f8..79124fd9 100644 --- a/astroplan/core.py +++ b/astroplan/core.py @@ -1681,7 +1681,10 @@ def ra(self): """ if isinstance(self, FixedTarget): return self.coord.ra - raise NotImplementedError() + if isinstance(self. NonFixedTarget): + raise ValueError("NonFixedTarget objects require a time and/or " + "other specifications to calculate a position. " + "Did you mean to do NonFixedTarget.at(args).ra?") @property def dec(self): @@ -1690,8 +1693,10 @@ def dec(self): """ if isinstance(self, FixedTarget): return self.coord.dec - raise NotImplementedError() - + if isinstance(self. NonFixedTarget): + raise ValueError("NonFixedTarget objects require a time and/or " + "other specifications to calculate a position. " + "Did you mean to do NonFixedTarget.at(args).dec?") class FixedTarget(Target): """ @@ -1813,6 +1818,14 @@ def __init__(self, coord_function=None, name=None): self.name = name.lower() if name is not None else name self.coord_function = coord_function + @classmethod + def from_function(cls, coord_function, name=None): + """ + Initialize a `~astropy.NonFixedTarget` by passing in a function that + computes a `~astropy.coordinates.SkyCoord` for the target object. + """ + return cls(coord_function=coord_function, name=name) + def at(self, *args, **kwargs): """ Get `~astropy.coordinates.SkyCoord` for the `~astroplan.NonFixedTarget` @@ -1820,17 +1833,13 @@ def at(self, *args, **kwargs): Parameters ---------- - Any parameters passed to the - """ - return self.coord_function(*args, **kwargs) + All parameters passed to ``coord_function``. - @classmethod - def from_function(cls, coord_function, name=None): - """ - Initialize a `~astropy.NonFixedTarget` by passing in a function that - computes a `~astropy.coordinates.SkyCoord` for the target object. + Returns + ------- + A `~astropy.coordinates.SkyCoord` object as specified by the parameters. """ - return cls(coord_function=coord_function, name=name) + return self.coord_function(*args, **kwargs) class Constraint(object): """ diff --git a/astroplan/tests/test_core.py b/astroplan/tests/test_core.py index c80f21c9..7379dd70 100644 --- a/astroplan/tests/test_core.py +++ b/astroplan/tests/test_core.py @@ -2,10 +2,10 @@ unicode_literals) from astropy.coordinates import (EarthLocation, Latitude, Longitude, SkyCoord, - AltAz) + AltAz, get_sun) import astropy.units as u from astropy.time import Time -from astropy.tests.helper import remote_data +from astropy.tests.helper import remote_data, assert_quantity_allclose import numpy as np from numpy.testing import assert_allclose import pytz @@ -14,7 +14,8 @@ from ..sites import get_site from ..core import (FixedTarget, Observer, list_FixedTarget_to_SkyCoord, - MAGIC_TIME) + MAGIC_TIME, NonFixedTarget) +from ..moon import get_moon from ..exceptions import TargetAlwaysUpWarning, TargetNeverUpWarning try: @@ -70,6 +71,31 @@ def test_FixedTarget_from_name(): # Make sure separation is small assert polaris_from_name.coord.separation(polaris_from_SIMBAD) < 1*u.arcsec +def test_NonFixedTarget_sun(): + time = Time("1995-08-31 02:03:04") + sun = NonFixedTarget(coord_function=get_sun, name="Sun") + sun_skycoord_nft = sun.at(time) + + sun_skycoord_getsun = get_sun(time) + tolerance = 0.1*u.arcsec + assert_quantity_allclose(sun_skycoord_nft.ra, sun_skycoord_getsun.ra, + atol=tolerance) + assert_quantity_allclose(sun_skycoord_nft.dec, sun_skycoord_getsun.dec, + atol=tolerance) + +def test_NonFixedTarget_moon(): + time = Time("1995-08-31 02:03:04") + location = get_site("Subaru") + pressure = 0*u.bar + moon = NonFixedTarget(coord_function=get_moon, name="Moon") + moon_skycoord_nft = moon.at(time, location, pressure) + + moon_skycoord_getmoon = get_moon(time, location, pressure) + tolerance = 0.1*u.arcsec + assert_quantity_allclose(moon_skycoord_nft.alt, moon_skycoord_getmoon.alt, + atol=tolerance) + assert_quantity_allclose(moon_skycoord_nft.az, moon_skycoord_getmoon.az, + atol=tolerance) def test_Observer_altaz(): """ From 5d88cdf64e5b72d399cf48846e397a9ee8397bb7 Mon Sep 17 00:00:00 2001 From: Brett Morris Date: Mon, 17 Aug 2015 17:03:09 -0700 Subject: [PATCH 3/4] Adding constant_kwargs dict --- astroplan/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/astroplan/core.py b/astroplan/core.py index 79124fd9..8f83117c 100644 --- a/astroplan/core.py +++ b/astroplan/core.py @@ -1811,12 +1811,13 @@ class NonFixedTarget(Target): Currently only some celestial objects are supported in this class. """ - def __init__(self, coord_function=None, name=None): + def __init__(self, coord_function=None, name=None, constant_kwargs=None): """ TODO: Docstring. """ self.name = name.lower() if name is not None else name self.coord_function = coord_function + self.constant_kwargs = constant_kwargs @classmethod def from_function(cls, coord_function, name=None): @@ -1839,6 +1840,9 @@ def at(self, *args, **kwargs): ------- A `~astropy.coordinates.SkyCoord` object as specified by the parameters. """ + if self.constant_kwargs is not None: + for key in self.constant_kwargs: + kwargs[key] = self.constant_kwargs[key] return self.coord_function(*args, **kwargs) class Constraint(object): From 1cb446827b06618de5aed0414e36bafd5020cb7f Mon Sep 17 00:00:00 2001 From: Brett Morris Date: Mon, 17 Aug 2015 23:44:33 -0700 Subject: [PATCH 4/4] Updating NonFixedTarget docstring --- astroplan/core.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/astroplan/core.py b/astroplan/core.py index 8f83117c..39a8d54c 100644 --- a/astroplan/core.py +++ b/astroplan/core.py @@ -1809,7 +1809,11 @@ class NonFixedTarget(Target): """ An object that is not "fixed" with respect to the celestial sphere. - Currently only some celestial objects are supported in this class. + A NonFixedTarget object holds a function that computes the coordinates + of the object at various points in time. + + NonFixedTarget objects can at present be initialized only by the + ``NonFixedTarget.from_name`` class method. """ def __init__(self, coord_function=None, name=None, constant_kwargs=None): """ @@ -1824,21 +1828,33 @@ def from_function(cls, coord_function, name=None): """ Initialize a `~astropy.NonFixedTarget` by passing in a function that computes a `~astropy.coordinates.SkyCoord` for the target object. + + Parameters + ---------- + coord_function : function + This function takes some input parameters and outputs a + `~astropy.coordinates.SkyCoord` for the object at one instance + in time + + name : str (optional) + Name of the object """ return cls(coord_function=coord_function, name=name) def at(self, *args, **kwargs): """ - Get `~astropy.coordinates.SkyCoord` for the `~astroplan.NonFixedTarget` - at a given time, location, etc. + Get a `~astropy.coordinates.SkyCoord` object for the + `~astroplan.NonFixedTarget` at a given time, and/or other parameters. Parameters ---------- - All parameters passed to ``coord_function``. + All arguments and keyword arguments passed to already-specified + ``self.coord_function``. Returns ------- - A `~astropy.coordinates.SkyCoord` object as specified by the parameters. + `~astropy.coordinates.SkyCoord` + Specified by passing the arguments to ``self.coord_function``. """ if self.constant_kwargs is not None: for key in self.constant_kwargs: