Skip to content

Commit

Permalink
Merge pull request #545 from astropy/revert-540-performance_plot_sche…
Browse files Browse the repository at this point in the history
…dule_airmass

Revert "Improve plot_schedule_airmass() performance by drawing night bands only once"
  • Loading branch information
bmorris3 authored Mar 17, 2023
2 parents 9a4aa54 + f884b7f commit 7649a29
Showing 1 changed file with 54 additions and 51 deletions.
105 changes: 54 additions & 51 deletions astroplan/plots/time_dependent.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def plot_airmass(targets, observer, time, ax=None, style_kwargs=None,
tzname = time.tzname()
tzinfo = time.tzinfo
else:
tzoffset = 0 * u.hour
tzoffset = 0
tzname = 'UTC'
tzinfo = None
# Populate time window if needed.
Expand Down Expand Up @@ -202,51 +202,40 @@ def plot_airmass(targets, observer, time, ax=None, style_kwargs=None,
# Shade background during night time
if brightness_shading:
start = time_ut[0]
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])

# 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])

# Invert y-axis and set limits.
y_lim = ax.get_ylim()
Expand Down Expand Up @@ -499,14 +488,28 @@ 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)),
brightness_shading=enable_brightness_shading)
plot_airmass(target, schedule.observer, ts, style_kwargs=dict(color=plt.cm.cool(ci)))
targ_to_color[target.name] = plt.cm.cool(ci)
enable_brightness_shading = False
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)
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, next_sunrise.plot_date,
facecolor='lightgrey', alpha=0.05)
plt.axvspan(previous_twilight.plot_date, next_twilight.plot_date,
facecolor='lightgrey', alpha=0.05)

for block in blocks:
if hasattr(block, 'target'):
Expand Down

0 comments on commit 7649a29

Please sign in to comment.