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

observer.target_rise_time(): Optional argument to throw TargetAlwaysUpWarning as an exception? #520

Closed void4 closed 2 years ago

void4 commented 2 years ago

When calling target_rise_time() with a target that is always up from the observers' location, it sensibly returns no value and logs this warning:

WARNING: TargetAlwaysUpWarning: Target with index 0 does not cross horizon=0.0 deg within 24 hours [astroplan.observer]

Would it be possible to add a keyword argument to target_rise_time, e.g. throw=True that would throw TargetAlwaysUpWarning as an exception so this case can be caught and handled separately?

I didn't find a separate is_target_always_up() function on the observer class...

wtgee commented 2 years ago

Hi @void4, thanks for the Issue.

You can (almost) just as easily capture warnings as you can exceptions so I don't see a strong need for this.

In [1]: from astroplan.observer import Observer
In [2]: from astroplan.exceptions import TargetAlwaysUpWarning
In [3]: from astropy.coordinates import SkyCoord, EarthLocation
In [4]: from astropy.time import Time
In [5]: import warnings

In [6]: warnings.filterwarnings('error', category=TargetAlwaysUpWarning)

In [7]: try:
   ...:     Observer.at_site('Subaru').target_rise_time(Time.now(), SkyCoord.from_name('Polaris'))
   ...: except TargetAlwaysUpWarning as e:
   ...:     print('always up!')
   ...: 
always up!

I'm going to close the Issue but if you encounter problems feel free to open back up. Cheers!

void4 commented 2 years ago

It's not optimal (because it's a global setting), but it will suffice for now I think. It would be nice to have a separate is_target_always_up() function still

wtgee commented 2 years ago

You can use a context manager to make a non global suppression:

https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings

void4 commented 2 years ago

Thank you!