Skip to content

Commit

Permalink
Link to plot webgui in summary, fix plot and style
Browse files Browse the repository at this point in the history
  • Loading branch information
juanep97 committed Feb 10, 2025
1 parent 12d974b commit 2bc8d47
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
31 changes: 27 additions & 4 deletions iop4lib/iop4_night_summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
<title>iop4 summary {{night|date:"Y/m/d"}}</title>
</head>
<style>

* {
box-sizing: border-box;
}

body {
font-size: 14px;
}

table {
border-collapse: collapse;
width: 100%;
Expand All @@ -27,6 +36,13 @@
margin: 1em 0;
}

img.source-plot {
min-width: 45em;
width: 49%;
max-width: 49%;
height: auto;
}

</style>
<body>
<h1>IOP4 summary {{night|date:"Y/m/d"}}</h1>
Expand Down Expand Up @@ -102,10 +118,17 @@ <h2>Summary of results (band R only)</h2>
</p>
{% endif %}

<p>Some of these plots might include the previous existing night for the source for comparison.</p>

{% for srcname, imgb64 in results_summary_images.items %}
<img src="data:image/png;base64,{{ imgb64 }}" alt="{{ srcname }}"/>
<p>
<small>
Some of these plots might include the previous existing night for the source for comparison.
Click on the plots to go to the interactive plot in the web interface.
</small>
</p>

{% for srcname, val in results_summary_images.items %}
<a href="{{ val.source_plot_url}}">
<img class="source-plot" src="data:image/png;base64,{{ val.imgb64 }}" alt="{{ srcname }}"/>
</a>
{% endfor %}

{% else %}
Expand Down
39 changes: 32 additions & 7 deletions iop4lib/iop4_night_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import matplotlib.pyplot as plt
from astropy.time import Time
import smtplib, email
from urllib.parse import urlencode, quote_plus

# logging
import coloredlogs, logging
Expand Down Expand Up @@ -91,17 +92,20 @@ def gather_context(args):
.filter(astrosource=source, band="R", epoch__night__lt=args.date)
.order_by('-epoch__night')
.values_list('epoch__night', flat=True)
.last())
.first())

# get the results for this source from the previous night, or for this night if there is no previous night
# Get the results for the source in the given night, but if there is only
# one result, get the results from the previous night for comparison

if prev_night is not None:
qs0 = PhotoPolResult.objects.filter(astrosource=source, band="R").filter(epoch__night__gte=args.date).order_by('-juliandate')
qs_today = PhotoPolResult.objects.filter(astrosource=source, band="R").filter(epoch__night=args.date)

if prev_night is not None and qs_today.count() == 1:
qs0 = PhotoPolResult.objects.filter(astrosource=source, band="R").filter(epoch__night__gte=prev_night, epoch__night__lte=args.date).order_by('-juliandate')
else:
qs0 = PhotoPolResult.objects.filter(astrosource=source, band="R").filter(epoch__night=args.date).order_by('-juliandate')
qs0 = qs_today.order_by('-juliandate')


fig = mplt.figure.Figure(figsize=(800/100, 600/100), dpi=100)
fig = mplt.figure.Figure(figsize=(1000/100, 600/100), dpi=100)
axs = fig.subplots(nrows=3, ncols=1, sharex=True, gridspec_kw={'hspace': 0.05})

for instrument, color in zip(instruments, colors):
Expand All @@ -118,6 +122,10 @@ def gather_context(args):
axs[1].errorbar(x=vals['datetime'], y=vals['p'], yerr=vals['p_err'], marker=".", color=color, linestyle="none")
axs[2].errorbar(x=vals['datetime'], y=vals['chi'], yerr=vals['chi_err'], marker=".", color=color, linestyle="none")

# x-axis date locator and formatter
from matplotlib.dates import AutoDateLocator
axs[-1].xaxis.set_major_locator(AutoDateLocator(interval_multiples=False, maxticks=7))

# invert magnitude axis
axs[0].invert_yaxis()

Expand Down Expand Up @@ -156,7 +164,24 @@ def gather_context(args):
imgbytes = buf.read()
imgb64 = base64.b64encode(imgbytes).decode("utf-8")

results_summary_images[source.name] = imgb64
# build the link to the interactive source plot

url_args = dict()

url_args['srcname'] = source.name

if prev_night is not None:
url_args['from'] = str(prev_night)

# we need 12:00 of the next day
next_day_noon = (datetime.datetime.combine(args.date, datetime.time(12, 0)) + datetime.timedelta(days=1))
url_args['to'] = str(next_day_noon)

source_plot_url = args.site_url + "/iop4/explore/plot/?" + urlencode(url_args, quote_via=quote_plus)

# save the results to the context

results_summary_images[source.name] = dict(imgb64=imgb64, source_plot_url=source_plot_url)

# save vars to context and return it

Expand Down

0 comments on commit 2bc8d47

Please sign in to comment.