3131import matplotlib .pyplot as plt
3232from astropy .time import Time
3333import smtplib , email
34+ from urllib .parse import urlencode , quote_plus
3435
3536# logging
3637import coloredlogs , logging
@@ -91,17 +92,20 @@ def gather_context(args):
9192 .filter (astrosource = source , band = "R" , epoch__night__lt = args .date )
9293 .order_by ('-epoch__night' )
9394 .values_list ('epoch__night' , flat = True )
94- .last ())
95+ .first ())
9596
96- # get the results for this source from the previous night, or for this night if there is no previous night
97+ # Get the results for the source in the given night, but if there is only
98+ # one result, get the results from the previous night for comparison
9799
98- if prev_night is not None :
99- qs0 = PhotoPolResult .objects .filter (astrosource = source , band = "R" ).filter (epoch__night__gte = args .date ).order_by ('-juliandate' )
100+ qs_today = PhotoPolResult .objects .filter (astrosource = source , band = "R" ).filter (epoch__night = args .date )
101+
102+ if prev_night is not None and qs_today .count () == 1 :
103+ qs0 = PhotoPolResult .objects .filter (astrosource = source , band = "R" ).filter (epoch__night__gte = prev_night , epoch__night__lte = args .date ).order_by ('-juliandate' )
100104 else :
101- qs0 = PhotoPolResult . objects . filter ( astrosource = source , band = "R" ). filter ( epoch__night = args . date ) .order_by ('-juliandate' )
105+ qs0 = qs_today .order_by ('-juliandate' )
102106
103107
104- fig = mplt .figure .Figure (figsize = (800 / 100 , 600 / 100 ), dpi = 100 )
108+ fig = mplt .figure .Figure (figsize = (1000 / 100 , 600 / 100 ), dpi = 100 )
105109 axs = fig .subplots (nrows = 3 , ncols = 1 , sharex = True , gridspec_kw = {'hspace' : 0.05 })
106110
107111 for instrument , color in zip (instruments , colors ):
@@ -118,6 +122,10 @@ def gather_context(args):
118122 axs [1 ].errorbar (x = vals ['datetime' ], y = vals ['p' ], yerr = vals ['p_err' ], marker = "." , color = color , linestyle = "none" )
119123 axs [2 ].errorbar (x = vals ['datetime' ], y = vals ['chi' ], yerr = vals ['chi_err' ], marker = "." , color = color , linestyle = "none" )
120124
125+ # x-axis date locator and formatter
126+ from matplotlib .dates import AutoDateLocator
127+ axs [- 1 ].xaxis .set_major_locator (AutoDateLocator (interval_multiples = False , maxticks = 7 ))
128+
121129 # invert magnitude axis
122130 axs [0 ].invert_yaxis ()
123131
@@ -156,7 +164,24 @@ def gather_context(args):
156164 imgbytes = buf .read ()
157165 imgb64 = base64 .b64encode (imgbytes ).decode ("utf-8" )
158166
159- results_summary_images [source .name ] = imgb64
167+ # build the link to the interactive source plot
168+
169+ url_args = dict ()
170+
171+ url_args ['srcname' ] = source .name
172+
173+ if prev_night is not None :
174+ url_args ['from' ] = str (prev_night )
175+
176+ # we need 12:00 of the next day
177+ next_day_noon = (datetime .datetime .combine (args .date , datetime .time (12 , 0 )) + datetime .timedelta (days = 1 ))
178+ url_args ['to' ] = str (next_day_noon )
179+
180+ source_plot_url = args .site_url + "/iop4/explore/plot/?" + urlencode (url_args , quote_via = quote_plus )
181+
182+ # save the results to the context
183+
184+ results_summary_images [source .name ] = dict (imgb64 = imgb64 , source_plot_url = source_plot_url )
160185
161186 # save vars to context and return it
162187
0 commit comments