From 5b292804d1e84d5e070826c85933cd1aeea7b9a1 Mon Sep 17 00:00:00 2001 From: Michael Baisch Date: Mon, 6 Mar 2023 14:11:50 +0100 Subject: [PATCH 1/5] Improve plot_schedule_airmass() performance by drawing night bands only once --- astroplan/plots/time_dependent.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/astroplan/plots/time_dependent.py b/astroplan/plots/time_dependent.py index fc4ec4f4..2e80ba44 100644 --- a/astroplan/plots/time_dependent.py +++ b/astroplan/plots/time_dependent.py @@ -493,9 +493,14 @@ def plot_schedule_airmass(schedule, show_night=False): plot_airmass(target, schedule.observer, ts, style_kwargs=dict(color=plt.cm.cool(ci))) targ_to_color[target.name] = plt.cm.cool(ci) if show_night: - # I'm pretty sure this overlaps a lot, creating darker bands - for test_time in ts: - midnight = schedule.observer.midnight(test_time) + 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( @@ -506,10 +511,12 @@ def plot_schedule_airmass(schedule, show_night=False): next_twilight = schedule.observer.twilight_morning_astronomical( midnight, which='next') - plt.axvspan(previous_sunset.plot_date, next_sunrise.plot_date, - facecolor='lightgrey', alpha=0.05) + 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.05) + facecolor='lightgrey', alpha=0.7) + plt.axvspan(next_twilight.plot_date, next_sunrise.plot_date, + facecolor='lightgrey', alpha=0.5) for block in blocks: if hasattr(block, 'target'): From bd4f98a11c9c2b8ae2af400a7a40eb5a05d1bce4 Mon Sep 17 00:00:00 2001 From: Michael Baisch Date: Wed, 15 Mar 2023 12:43:58 +0100 Subject: [PATCH 2/5] When using show_night with plot_schedule_airmass() use brightness_shading in plot_airmass() Also improve brightness_shading in plot_airmass() to allow for plots > 24h --- astroplan/plots/time_dependent.py | 103 ++++++++++++++---------------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/astroplan/plots/time_dependent.py b/astroplan/plots/time_dependent.py index 2e80ba44..4b40a750 100644 --- a/astroplan/plots/time_dependent.py +++ b/astroplan/plots/time_dependent.py @@ -202,40 +202,51 @@ def plot_airmass(targets, observer, time, ax=None, style_kwargs=None, # Shade background during night time if brightness_shading: start = time_ut[0] - - # Calculate and order twilights and set plotting alpha for each - twilights = [ - (observer.sun_set_time(start, which='next'), 0.0), - (observer.twilight_evening_civil(start, which='next'), 0.1), - (observer.twilight_evening_nautical(start, which='next'), 0.2), - (observer.twilight_evening_astronomical(start, which='next'), 0.3), - (observer.twilight_morning_astronomical(start, which='next'), 0.4), - (observer.twilight_morning_nautical(start, which='next'), 0.3), - (observer.twilight_morning_civil(start, which='next'), 0.2), - (observer.sun_rise_time(start, which='next'), 0.1), - ] - - # add 'UTC' to each datetime object created above - twilights = [(t[0].datetime.replace(tzinfo=pytz.utc), t[1]) - for t in twilights] - - twilights.sort(key=operator.itemgetter(0)) - - # add in left & right edges, so that if the airmass plot is requested - # during the day, night is properly shaded - left_edges = [(xlo.datetime.replace(tzinfo=tzinfo), twilights[0][1])] + twilights - right_edges = twilights + [(xhi.datetime.replace(tzinfo=tzinfo), twilights[0][1])] - - for tw_left, tw_right in zip(left_edges, right_edges): - left = tw_left[0] - right = tw_right[0] - if tzinfo is not None: - # convert to local time zone (which is plotted), then hack away the tzinfo - # so that matplotlib doesn't try to double down on the conversion - left = left.astimezone(tzinfo).replace(tzinfo=None) - right = right.astimezone(tzinfo).replace(tzinfo=None) - ax.axvspan(left, right, - ymin=0, ymax=1, color='grey', alpha=tw_right[1]) + end = time_ut[-1] + nights = [] + + next_sun_rise = observer.sun_rise_time(start, which='next') + sun_set = observer.sun_set_time(next_sun_rise, which='previous') + while (sun_set < end): + + # Calculate and order twilights and set plotting alpha for each + twilights = [ + (sun_set, 0.0), + (observer.twilight_evening_civil(next_sun_rise, which='previous'), 0.1), + (observer.twilight_evening_nautical(next_sun_rise, which='previous'), 0.2), + (observer.twilight_evening_astronomical(next_sun_rise, which='previous'), 0.3), + (observer.twilight_morning_astronomical(next_sun_rise, which='previous'), 0.4), + (observer.twilight_morning_nautical(next_sun_rise, which='previous'), 0.3), + (observer.twilight_morning_civil(next_sun_rise, which='previous'), 0.2), + (next_sun_rise, 0.1), + ] + nights.append(twilights) + + next_sun_rise = observer.sun_rise_time(next_sun_rise, which='next') + sun_set = observer.sun_set_time(next_sun_rise, which='previous') + + for twilights in nights: + # add 'UTC' to each datetime object created above + twilights = [(t[0].datetime.replace(tzinfo=pytz.utc), t[1]) + for t in twilights] + + twilights.sort(key=operator.itemgetter(0)) + + # add in left & right edges, so that if the airmass plot is requested + # during the day, night is properly shaded + left_edges = [(xlo.datetime.replace(tzinfo=tzinfo), twilights[0][1])] + twilights + right_edges = twilights + [(xhi.datetime.replace(tzinfo=tzinfo), twilights[0][1])] + + for tw_left, tw_right in zip(left_edges, right_edges): + left = tw_left[0] + right = tw_right[0] + if tzinfo is not None: + # convert to local time zone (which is plotted), then hack away the tzinfo + # so that matplotlib doesn't try to double down on the conversion + left = left.astimezone(tzinfo).replace(tzinfo=None) + right = right.astimezone(tzinfo).replace(tzinfo=None) + ax.axvspan(left, right, + ymin=0, ymax=1, color='grey', alpha=tw_right[1]) # Invert y-axis and set limits. y_lim = ax.get_ylim() @@ -488,10 +499,13 @@ def plot_schedule_airmass(schedule, show_night=False): np.linspace(0, (schedule.end_time - schedule.start_time).value, 100) * u.day) targ_to_color = {} color_idx = np.linspace(0, 1, len(targets)) + enable_brightness_shading = show_night # lighter, bluer colors indicate higher priority for target, ci in zip(set(targets), color_idx): - plot_airmass(target, schedule.observer, ts, style_kwargs=dict(color=plt.cm.cool(ci))) + plot_airmass(target, schedule.observer, ts, style_kwargs=dict(color=plt.cm.cool(ci)), brightness_shading=enable_brightness_shading) targ_to_color[target.name] = plt.cm.cool(ci) + enable_brightness_shading = False + if show_night: midnights = [] test_time = schedule.start_time @@ -499,25 +513,6 @@ def plot_schedule_airmass(schedule, show_night=False): 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) - for block in blocks: if hasattr(block, 'target'): plt.axvspan(block.start_time.plot_date, block.end_time.plot_date, From fc0c71d9aae4e5b6a14a8a20615c11d89103abf2 Mon Sep 17 00:00:00 2001 From: Michael Baisch Date: Wed, 15 Mar 2023 12:53:02 +0100 Subject: [PATCH 3/5] Fix TimeDeltaMissingUnitWarning in plot_airmass() --- astroplan/plots/time_dependent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroplan/plots/time_dependent.py b/astroplan/plots/time_dependent.py index 4b40a750..7b42ec7e 100644 --- a/astroplan/plots/time_dependent.py +++ b/astroplan/plots/time_dependent.py @@ -160,7 +160,7 @@ def plot_airmass(targets, observer, time, ax=None, style_kwargs=None, tzname = time.tzname() tzinfo = time.tzinfo else: - tzoffset = 0 + tzoffset = 0 * u.hour tzname = 'UTC' tzinfo = None # Populate time window if needed. From 35cf8f7dad2e50debae2593b31ebc6656ae2ed9d Mon Sep 17 00:00:00 2001 From: Michael Baisch Date: Wed, 15 Mar 2023 16:21:37 +0100 Subject: [PATCH 4/5] Fix code style checks and remove not needed part --- astroplan/plots/time_dependent.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/astroplan/plots/time_dependent.py b/astroplan/plots/time_dependent.py index 7b42ec7e..f2683dc2 100644 --- a/astroplan/plots/time_dependent.py +++ b/astroplan/plots/time_dependent.py @@ -502,17 +502,12 @@ def plot_schedule_airmass(schedule, show_night=False): enable_brightness_shading = show_night # lighter, bluer colors indicate higher priority for target, ci in zip(set(targets), color_idx): - plot_airmass(target, schedule.observer, ts, style_kwargs=dict(color=plt.cm.cool(ci)), brightness_shading=enable_brightness_shading) + plot_airmass(target, schedule.observer, ts, + style_kwargs=dict(color=plt.cm.cool(ci)), + brightness_shading=enable_brightness_shading) targ_to_color[target.name] = plt.cm.cool(ci) enable_brightness_shading = False - 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) - for block in blocks: if hasattr(block, 'target'): plt.axvspan(block.start_time.plot_date, block.end_time.plot_date, From 8d979219b96abdcd1e01c04eec151920d48fc634 Mon Sep 17 00:00:00 2001 From: Michael Baisch Date: Mon, 19 Jun 2023 10:52:43 +0200 Subject: [PATCH 5/5] plot_airmass: fix bug causing exception when sunrises are more than 24h apart --- astroplan/plots/time_dependent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroplan/plots/time_dependent.py b/astroplan/plots/time_dependent.py index f2683dc2..959800f7 100644 --- a/astroplan/plots/time_dependent.py +++ b/astroplan/plots/time_dependent.py @@ -222,7 +222,7 @@ def plot_airmass(targets, observer, time, ax=None, style_kwargs=None, ] nights.append(twilights) - next_sun_rise = observer.sun_rise_time(next_sun_rise, which='next') + next_sun_rise = observer.sun_rise_time(next_sun_rise+1*u.hour, which='next') sun_set = observer.sun_set_time(next_sun_rise, which='previous') for twilights in nights: