astropy / astroplan

Observation planning package for astronomers – maintainer @bmorris3
https://astroplan.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
200 stars 109 forks source link

"Tried to do is_equivalent_frame on something that isn't frame-like" error from MoonSeparationConstraint().compute_constraint() #567

Closed pmaxted closed 1 year ago

pmaxted commented 1 year ago

astropy-5.3.3 astroplan-0.9

import astroplan as ap
from astropy.time import Time
import astropy.units as u
from astropy.coordinates import SkyCoord

observer = ap.Observer.at_site('lasilla')
targets = [ap.FixedTarget(SkyCoord.from_name('Sirius'), name='Sirius')]
noon = Time("2023-06-06 12:00:00")
time_grid = ap.time_grid_from_range([noon,noon+1*u.day])
moon_constraint = ap.MoonSeparationConstraint(30*u.degree)
moon_ok_grid = moon_constraint.compute_constraint(time_grid, observer, [targets])

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/dl/tc85r2_j2v94kqhby3hvz4n00000gn/T/ipykernel_86529/2961427994.py in <module>
      9 time_grid = ap.time_grid_from_range([noon,noon+1*u.day])
     10 moon_constraint = ap.MoonSeparationConstraint(30*u.degree)
---> 11 moon_ok_grid = moon_constraint.compute_constraint(time_grid, observer, target)

~/miniconda3/lib/python3.9/site-packages/astroplan/constraints.py in compute_constraint(self, times, observer, targets)
    596         # the former calculates the separation in the frame of the moon coord
    597         # which is GCRS, and that is what we want.
--> 598         moon_separation = moon.separation(targets)
    599 
    600         if self.min is None and self.max is not None:

~/miniconda3/lib/python3.9/site-packages/astropy/coordinates/sky_coordinate.py in separation(self, other)
   1170         from .angle_utilities import angular_separation
   1171 
-> 1172         if not self.is_equivalent_frame(other):
   1173             try:
   1174                 kwargs = (

~/miniconda3/lib/python3.9/site-packages/astropy/coordinates/sky_coordinate.py in is_equivalent_frame(self, other)
   1129         else:
   1130             # not a BaseCoordinateFrame nor a SkyCoord object
-> 1131             raise TypeError(
   1132                 "Tried to do is_equivalent_frame on something that isn't frame-like"
   1133             )

TypeError: Tried to do is_equivalent_frame on something that isn't frame-like

I can get this working for a single target if I replace line 598 of constraints.py with ...

moon_separation = moon.separation(SkyCoord([target.coord for target in targets]))

... but I'm not sure why, and this does not work if targets is a list of more than one target.

-Pierre

bmorris3 commented 1 year ago

Hi @pmaxted!

Thanks for letting me know. This is appears to be a bug introduced by #558.

One slight workaround that works without modification to astroplan is to pass a SkyCoord rather than a FixedTarget:

import astroplan as ap
from astropy.time import Time
import astropy.units as u
from astropy.coordinates import SkyCoord

observer = ap.Observer.at_site('lasilla')
targets = SkyCoord.from_name('Sirius')
noon = Time("2023-06-06 12:00:00")
time_grid = ap.time_grid_from_range([noon,noon+1*u.day])
moon_constraint = ap.MoonSeparationConstraint(30*u.degree)
moon_ok_grid = moon_constraint.compute_constraint(time_grid, observer, targets)

I'll get a fix into main ASAP.

bmorris3 commented 1 year ago

New astroplan v0.9.1 released with this bugfix. Thanks again @pmaxted!