astropy / astroplan

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

constraints function gives AttributeError if targets is in ndarray #513

Open ysBach opened 3 years ago

ysBach commented 3 years ago

For some reason, I have targets in np.ndarray. (I have to mask some targets frequently by comparing them (SkyCoord objects) with a catalog table)

The following code shows the problem:

import astroplan as ap
import numpy as np
from astropy.coordinates import EarthLocation, SkyCoord
from astropy.time import Time

consts = [ap.AtNightConstraint()]
observer = ap.Observer(EarthLocation(lat=0, lon=0, height=0))
targets = np.array([SkyCoord(ra=0, dec=0, unit='deg')])
times = Time("2020-01-01")

ap.is_observable(constraints=consts, observer=observer, targets=targets.tolist(), times=times)
# Works fine, giving [True]

ap.is_observable(constraints=consts, observer=observer, targets=targets, times=times)
# AttributeError: 'numpy.ndarray' object has no attribute 'isscalar'

A simple update to the source code may fix it

# original from https://github.com/astropy/astroplan/blob/master/astroplan/constraints.py#L265
            if targets.isscalar:
# Change to
            if hasattr(targets, "isscalar") and targets.isscalar:

How do you think?

bmorris3 commented 2 years ago

Hi @ysBach,

Thanks for this note. In general we recommend that you use target lists as pure Python lists to prevent confusion. Creating object arrays will work for masking, but you can achieve a similar effect with the following syntax without numpy:

masked_targets = [target for target, m in zip(targets, mask) if m]

Let me know if this helps, or if I'm not understanding the problem, Brett

bmorris3 commented 2 years ago

Hi @ysBach, any updates on this? If we should really consider supporting ndarrays of FixedTarget objects then I'm happy to investigate.