Skip to content

Commit

Permalink
Merge pull request #448 from sot/refactor-html-report
Browse files Browse the repository at this point in the history
Refactor code and change format of HTML report
  • Loading branch information
taldcroft authored Feb 22, 2025
2 parents dd1994b + fb6377d commit 5c7f250
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 122 deletions.
10 changes: 1 addition & 9 deletions starcheck/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,11 @@ def make_plots_for_obsid(obsid, ra, dec, roll, starcat_time, catalog, outdir,

bad_stars = bad_acq_stars(stars)

f_plot = plot_stars(attitude=[ra, dec, roll], catalog=None, stars=stars,
title=None, starcat_time=starcat_time, duration=duration,
bad_stars=bad_stars, red_mag_lim=None)
f_plot.savefig(os.path.join(outdir, 'star_view_{}.png'.format(obsid)), dpi=80)
plt.close(f_plot)
cat_plot = plot_stars(attitude=[ra, dec, roll], catalog=catalog, stars=stars,
title="RA=%.6f Dec=%.6f Roll=%.6f" % (ra, dec, roll),
starcat_time=starcat_time, duration=duration,
bad_stars=bad_stars, red_mag_lim=red_mag_lim)
cat_plot.savefig(os.path.join(outdir, 'stars_{}.png'.format(obsid)), dpi=80)
cat_plot.savefig(os.path.join(outdir, 'stars_{}.png'.format(obsid)), dpi=150)
plt.close(cat_plot)
compass_plot = plot_compass(roll)
compass_plot.savefig(os.path.join(outdir, 'compass{}.png'.format(obsid)), dpi=80)
plt.close(compass_plot)


242 changes: 154 additions & 88 deletions starcheck/src/lib/Ska/Starcheck/Obsid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,97 @@ sub calc_fid_ang {
}

#############################################################################################
sub print_report {
sub get_report_html {
#############################################################################################
my $self = shift;
my $c;
my $o = ''; # Output

my $obsid = $self->{obsid};

# Internal reference link
$o .= "<!-- Start of HTML report content for obsid $obsid -->";

# Main table for per-obsid report
$o .= "<!-- Main table for per-obsid report for obsid $obsid -->";
$o .= "<TABLE CELLPADDING=0>\n";

# Left side of table with nav links and pre-formatted text
$o .= "<TD VALIGN=TOP WIDTH=810>\n";
my $nav_buttons = $self->get_report_prev_next_buttons_html();
$o .= qq{<A NAME="obsid$obsid">$nav_buttons</A>\n};
$o .= "<BR>\n";
$o .= "<!-- Star catalog preformatted information and table for obsid $obsid -->\n";
$o .= "<PRE>";
$o .= $self->get_report_header_html();
$o .= $self->get_report_starcat_table_html();
$o .= $self->get_report_footer_html();
$o .= "</PRE>\n";
$o .= "</TD>\n";

# Right side with images: starfield big, starfield small, compass
$o .= "<!-- Star field plot for obsid $obsid -->\n";
$o .= "<TD VALIGN=TOP>\n";
$o .= $self->get_report_images_html();
$o .= "</TD>\n";

$o .= "</TR>\n";
$o .= "</TABLE>\n";
$o .= "<!-- End of HTML report content for obsid $obsid -->\n";
return $o;
}

#############################################################################################
sub get_report_prev_next_buttons_html {
#############################################################################################
my $self = shift;
my $o = '';

if (defined $self->{prev}->{obsid} or defined $self->{next}->{obsid}) {
$o .= " <TABLE WIDTH=43><TR>";
if (defined $self->{prev}->{obsid}) {
$o .= sprintf(
"<TD><A HREF=\"#obsid%s\"><img align=\"top\" src=\"%s/up.gif\" ></A></TD>",
$self->{prev}->{obsid},
$self->{STARCHECK}
);
$o .= sprintf("<TD><A HREF=\"#obsid%s\">PREV</A> </TD>",
$self->{prev}->{obsid});
}
else {
$o .= sprintf("<TD><img align=\"top\" src=\"%s/up.gif\" ></TD>",
$self->{STARCHECK});
$o .= sprintf("<TD>PREV</TD>");
}
$o .= sprintf("<TD>&nbsp; &nbsp;</TD>");
if (defined $self->{next}->{obsid}) {
$o .= sprintf(
"<TD><A HREF=\"#obsid%s\"><img align=\"top\" src=\"%s/down.gif\" ></A></TD>",
$self->{next}->{obsid},
$self->{STARCHECK}
);
$o .= sprintf("<TD><A HREF=\"#obsid%s\">NEXT</A> </TD>",
$self->{next}->{obsid});
}
$o .= " </TR></TABLE>";
}
return $o;
}


#############################################################################################
sub get_report_header_html {
# Make the bit like this:
#
# OBSID: 30182 NGC5134 ACIS-S SIM Z offset:0 (0.00mm) Grating: NONE
# RA, Dec, Roll (deg): 201.336746 -21.137976 62.841900
# Dither: ON Y_amp=16.0 Z_amp=16.0 Y_period=1414.0 Z_period=2000.0
# BACKSTOP GUIDE_SUMM OR MANVR DOT TLR
#
# MP_TARGQUAT at 2025:041:02:29:09.263 (VCDU count = 9219318)
# Q1,Q2,Q3,Q4: 0.24868922 -0.47464310 -0.84208447 0.06132982
# MANVR: Angle= 152.43 deg Duration= 2692 sec End= 2025:041:03:14:07
#
#############################################################################################
my $self = shift;
my $c;
Expand All @@ -2024,9 +2114,6 @@ sub print_report {
my $target_name =
($self->{TARGET_NAME}) ? $self->{TARGET_NAME} : $self->{SS_OBJECT};

$o .= sprintf(
"<TABLE WIDTH=853 CELLPADDING=0><TD VALIGN=TOP WIDTH=810><PRE><A NAME=\"obsid%s\"></A>",
$self->{obsid});
$o .= sprintf("${blue_font_start}OBSID: %-5s ", $self->{obsid});
$o .= sprintf(
"%-22s %-6s SIM Z offset:%-5d (%-.2fmm) Grating: %-5s",
Expand Down Expand Up @@ -2118,44 +2205,32 @@ sub print_report {
$o .= "\n";
}
}
return $o;
}

#############################################################################################
sub get_report_starcat_table_html {
# Make this:
#
# MP_STARCAT at 2025:041:02:29:10.906 (VCDU count = 9219324)
# ---------------------------------------------------------------------------------------------
# IDX SLOT ID TYPE SZ P_ACQ MAG MAXMAG YANG ZANG DIM RES HALFW PASS NOTES
# ---------------------------------------------------------------------------------------------
# [ 1] 0 1 FID 8x8 --- 7.000 8.000 932 -1739 1 1 25
# ...
# [12] 7 803738368 ACQ 8x8 0.586 10.222 11.203 1020 1893 20 1 120
#############################################################################################
my $self = shift;
my $c;
my $table = '';
my $star_stat_lookup = "http://kadi.cfa.harvard.edu/star_hist/?agasc_id=";

my $table;
if ($c = find_command($self, "MP_STARCAT")) {

my @fid_fields =
my @fields =
qw (TYPE SIZE P_ACQ GS_MAG MAXMAG YANG ZANG DIMDTS RESTRK HALFW GS_PASS GS_NOTES);
my @fid_format = (
'%6s',
'%5s',
'%8.3f',
'%8s',
'%8.3f',
'%7d',
'%7d',
'%4d',
'%4d',
'%5d',
'%6s',
'%4s'
);
my @star_fields =
qw ( TYPE SIZE P_ACQ GS_MAG MAXMAG YANG ZANG DIMDTS RESTRK HALFW GS_PASS GS_NOTES);
my @star_format = (
'%6s',
'%5s',
'%8.3f',
'%8s',
'%8.3f',
'%7d',
'%7d',
'%4d',
'%4d',
'%5d',
'%6s',
'%4s'
);
my @format =
qw(%6s %5s %8.3f %8s %8.3f %7d %7d %4d %4d %5d %6s %4s);

$table .= sprintf "MP_STARCAT at $c->{date} (VCDU count = $c->{vcdu})\n";
$table .= sprintf
Expand All @@ -2168,13 +2243,7 @@ sub print_report {
"---------------------------------------------------------------------------------------------\n";

foreach my $i (1 .. 16) {
my @fields = @star_fields;
my @format = @star_format;
next if ($c->{"TYPE$i"} eq 'NUL');
if ($c->{"TYPE$i"} eq 'FID') {
@fields = @fid_fields;
@format = @fid_format;
}

# Define the color of output star catalog line based on NOTES:
# Yellow if NOTES is non-trivial.
Expand Down Expand Up @@ -2207,9 +2276,8 @@ sub print_report {
# Get a string for acquisition probability in the hover-over
my $acq_prob = "";
if ($c->{"TYPE$i"} =~ /BOT|ACQ/) {

# Fetch this slot's acq probability for the hover-over string,
# but if the probability is not defined (expected for weird cases such as
# but if the probability is not defined (expected for weird cases such as
# replan/reopen) just leave $acq_prob as the initialized empty string.
if (defined $self->{acq_probs}->{ $c->{"IMNUM${i}"} }) {
$acq_prob = sprintf("Prob Acq Success %5.3f",
Expand Down Expand Up @@ -2348,7 +2416,25 @@ sub print_report {
$table = sprintf(" " x 93 . "\n");
}

$o .= $table;
return $table;
}

#############################################################################################
sub get_report_footer_html {
# Make this:
#
# >> WARNING : [ 6] Imposter mag 11.0 centroid offset 2.9 row, col ( 422, 171) star ( 430, 179)
#
# Probability of acquiring 2 or fewer stars (10^-x): 3.4
# Acquisition Stars Expected : 6.24
# Guide star count: 5.0
# Predicted Max CCD temperature: -7.0 C (-7.007 C) N100 Warm Pix Frac 0.446
# Dynamic Mag Limits: Yellow 9.96 Red 10.37
#
#############################################################################################
my $self = shift;
my $c;
my $o = ''; # Output

$o .= "\n"
if ( @{ $self->{warn} }
Expand Down Expand Up @@ -2423,42 +2509,20 @@ sub print_report {
);
}

# cute little table for buttons for previous and next obsid
$o .= "</PRE></TD><TD VALIGN=TOP>\n";
if (defined $self->{prev}->{obsid} or defined $self->{next}->{obsid}) {
$o .= " <TABLE WIDTH=43><TR>";
if (defined $self->{prev}->{obsid}) {
$o .= sprintf(
"<TD><A HREF=\"#obsid%s\"><img align=\"top\" src=\"%s/up.gif\" ></A></TD>",
$self->{prev}->{obsid},
$self->{STARCHECK}
);
$o .= sprintf("<TD><A HREF=\"#obsid%s\">PREV</A> </TD>",
$self->{prev}->{obsid});
}
else {
$o .= sprintf("<TD><img align=\"top\" src=\"%s/up.gif\" ></TD>",
$self->{STARCHECK});
$o .= sprintf("<TD>PREV</TD>");
}
$o .= sprintf("<TD>&nbsp; &nbsp;</TD>");
if (defined $self->{next}->{obsid}) {
$o .= sprintf(
"<TD><A HREF=\"#obsid%s\"><img align=\"top\" src=\"%s/down.gif\" ></A></TD>",
$self->{next}->{obsid},
$self->{STARCHECK}
);
$o .= sprintf("<TD><A HREF=\"#obsid%s\">NEXT</A> </TD>",
$self->{next}->{obsid});
}
$o .= " </TR></TABLE>";

}
return $o;
}

# end of whole obsid table
$o .= " </TD></TABLE>";
sub get_report_images_html {
my $self = shift;
my $img_size = 600;

return $o;
my $out = "";
if ($self->{plot_file}) {
my $obs = $self->{obsid};
$out .= $self->star_image_map($img_size);
$out .= qq{<img src="$self->{plot_file}" usemap=\#starmap_${obs} width=$img_size height=$img_size border=0> };
}
return $out;
}

#############################################################################################
Expand Down Expand Up @@ -2686,6 +2750,7 @@ sub star_dbhist {
sub star_image_map {
#############################################################################################
my $self = shift;
my $img_size = shift;
my $c;
return unless ($c = find_command($self, 'MP_STARCAT'));
return
Expand Down Expand Up @@ -2717,12 +2782,13 @@ sub star_image_map {
$star_count++;
}

# notes for pixel scaling.
# these will need to change if we resize the images.
# top right +384+39
# top left +54+39
# 2900x2900
my $pix_scale = 330 / (2900. * 2);
# For a 798x798 image (native with dpi=150), -2900 to 2900 arcsec is 619 pixels.
# The top left corner is at x=100, y=63 pixels.
my $img_size_ref = 798; # pixels
my $img_size_scale = $img_size / $img_size_ref;
my $pix_scale = 619 / (2900. * 2) * $img_size_scale;
my $x_offset = 100 * $img_size_scale;
my $y_offset = 63 * $img_size_scale;

# Convert all the yag/zags to pixel rows/cols
my @yags = map { $self->{agasc_hash}->{$_}->{yag} } keys %plot_ids;
Expand All @@ -2740,8 +2806,8 @@ sub star_image_map {
my $sid = $cat_star->{id};
my $yag = $cat_star->{yag};
my $zag = $cat_star->{zag};
my $image_x = 54 + ((2900 - $yag) * $pix_scale);
my $image_y = 39 + ((2900 - $zag) * $pix_scale);
my $image_x = $x_offset + (2900 - $yag) * $pix_scale;
my $image_y = $y_offset + (2900 - $zag) * $pix_scale;
my $star =
'<area href="javascript:void(0);"' . "\n"
. 'ONMOUSEOVER="return overlib ('
Expand All @@ -2757,7 +2823,7 @@ sub star_image_map {
. 'ONMOUSEOUT="return nd();"' . "\n"
. 'SHAPE="circle"' . "\n"
. 'ALT=""' . "\n"
. "COORDS=\"$image_x,$image_y,2\">" . "\n";
. "COORDS=\"$image_x,$image_y,3\">" . "\n";
$map .= $star;
}
$map .= "</map> \n";
Expand Down
Loading

0 comments on commit 5c7f250

Please sign in to comment.