astropy / astroplan

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

`plot_schedule_airmass` with `show_night=True` is immensely slow #539

Closed michaelbaisch closed 1 year ago

michaelbaisch commented 1 year ago

Greetings,

I've noticed that running plot_schedule_airmass() is very slow when I activate the argument show_night=True, I'm talking like 8 seconds for a very simple plot. As this comment realized there is a lot of overlap here. My suggestion is calculating all midnights first and coloring in the nights once:

if show_night:
        midnights = []
        test_time = schedule.start_time
        while (midnight := schedule.observer.midnight(test_time, which='next')) < schedule.end_time:
            test_time = midnight + 6 * u.hour
            midnights.append(midnight)

        # Ceating darker bands
        for midnight in midnights:
            previous_sunset = schedule.observer.sun_set_time(
                midnight, which='previous')
            next_sunrise = schedule.observer.sun_rise_time(
                midnight, which='next')

            previous_twilight = schedule.observer.twilight_evening_astronomical(
                midnight, which='previous')
            next_twilight = schedule.observer.twilight_morning_astronomical(
                midnight, which='next')

            plt.axvspan(previous_sunset.plot_date, previous_twilight.plot_date,
                        facecolor='lightgrey', alpha=0.5)
            plt.axvspan(previous_twilight.plot_date, next_twilight.plot_date,
                        facecolor='lightgrey', alpha=0.7)
            plt.axvspan(next_twilight.plot_date, next_sunrise.plot_date,
                        facecolor='lightgrey', alpha=0.5)

This makes it a lot faster.