astroplan icon indicating copy to clipboard operation
astroplan copied to clipboard

constraints function gives AttributeError if targets is in ndarray

Open ysBach opened this issue 3 years ago • 2 comments

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?

ysBach avatar Oct 31 '21 07:10 ysBach

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 avatar Nov 03 '21 08:11 bmorris3

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

bmorris3 avatar Oct 11 '22 23:10 bmorris3