<table border=0 cellpadding=2 cellspacing=0>
<tr>
% foreach my $foo (@stampdata) {
% my $det = $foo->{det};

<td align="center" valign="center">

<table style="display: inline" border=0 cellpadding=0 cellspacing=0>
% if ($show eq 'chip' or $show eq 'both') {
<tr><td style="height: 80px" align="center" valign="center">
% if ($foo->{chip}) {
    <img width="<% $size %>" height"<% $size %>" src="/model/<% $dbname %>/stamp.jpg?file=<% $foo->{chip} %>">
%   if ($short < 1) {
    <br><% sprintf "%s = %.1f<br>%s = %.1f<br>S/N = %.1f", $det->filter, $det->mag, 'V', mopslib_filt2V($det->mag, $det->filter, $v2filt), $det->s2n %>
    <br><small>Chip | <a href="/model/<% $dbname %>/stamp.fits?file=<% $foo->{chip} %>">FITS</a>
    </small>
%   } else {
%   }
% } else {
    <div style="width: 75px; height: 75px; border: 1px solid grey;"><b>No<br>chip<br>stamp</b>
% }
</tr></td>
% }

% if ($show eq 'diff' or $show eq 'both') {
<tr><td style="height: 80px" align="center" valign="center">
% if ($foo->{diff}) {
    <img width="<% $size %>" height"<% $size %>" src="/model/<% $dbname %>/stamp.jpg?file=<% $foo->{diff} %>">
%   if ($short < 1) {
    <br><% sprintf "%s = %.1f<br>%s = %.1f<br>S/N = %.1f", $det->filter, $det->mag, 'V', mopslib_filt2V($det->mag, $det->filter, $v2filt), $det->s2n %>
    <br><small>Diff | <a href="/model/<% $dbname %>/stamp.fits?file=<% $foo->{diff} %>">FITS</a>
    </small>
%   } else {
%   }
% } else {
    <div style="width: 75px; height: 75px; border: 1px solid grey;"><b>No<br>diff<br>stamp</b>
% }
</tr></td>
% }

<tr><td align="center" valign="top">
% my $ffz_str = $ffz{$det->detId} ? ('<br>[' . $ffz{$det->detId} . ']') : '<br>N/A';
% if ($decider) {
    <a target="_blank" href="<% sprintf qq{/model/$model/comet_decider.html?det_id=%s}, $det->detId %>">Decider</a><% $ffz_str %>
% } else {
    <b>N/A</b>
    <br><% sprintf "%s = %.1f<br>%s = %.1f<br>S/N = %.1f", $det->filter, $det->mag, 'V', mopslib_filt2V($det->mag, $det->filter, $v2filt), $det->s2n %>
    <br><a target="_blank" href="<% sprintf qq{/model/$model/comet_decider.html?det_id=%s}, $det->detId %>">Decider</a><% $ffz_str %>
% }

</td></tr></table>

</td>
% }
</tr></table>

<%args>
$size => 75
$dets
$short => 0
$decider => undef
$ffuzzmap => undef
$show => 'both'
</%args>

<%init>
use Digest::MD5;
use Data::Dumper;
use PS::MOPS::Lib qw(mopslib_filt2V);

my $inst = $m->notes('INST');
my $model = $m->notes('MODEL');
my $mc = $inst->getConfig();
my $v2filt = $mc->{site}->{v2filt};

my $dbname = $inst->dbname;
my @stampdata;
my $dfile;
my $cfile;
my %ffz = {};   # map of det ID to ffuzz value

# convert DET_ID=FFUZZ pairs in ffuzzmap into a dict.
if ($ffuzzmap) {
    my @thangs = split /,/, $ffuzzmap;
    for (@thangs) {
        my ($id, $f) = split /=/, $_;
        $ffz{$id} = ($f || 'N/A');
    }
}

foreach my $det (@{$dets}) {
    my $det_id = $det->detId;
    my $id = sprintf "%010d", $det_id;
    my $nn = int($det->epoch) - 1;  # TJD to MOPS nn
    my $dreldir = join('/', 'MD', split '', substr(uc(Digest::MD5::md5_hex($id)), 0, 5));
    my $creldir = join('/', 'MC', split '', substr(uc(Digest::MD5::md5_hex($id)), 0, 5));

    my $have_dfile;
    my $have_cfile;
    if ($show eq 'diff' or $show eq 'both') {
        $dfile = $inst->getEnvironment('VARDIR') . "/stamps/$dreldir/MD-$id.fits";
        if (!-f $dfile) {
            my ($dir) = $det_id =~ /(\d)$/;
            $dfile = "/data/mops13.0/stamp-store/stamps/special-stamps/$dbname-auto/$nn/$dir/D$det_id.fits";
            if (!-f $dfile) {
                $dfile = "/data/mops13.0/stamp-store/stamps/special-stamps/$dbname-auto/$nn/D$det_id.fits";
            }
        }
        $have_dfile = -f $dfile;
    }

    if ($show eq 'chip' or $show eq 'both') {
        $cfile = $inst->getEnvironment('VARDIR') . "/stamps/$nn/$det_id.fits";
        if (!-f $cfile) {
            my ($dir) = $det_id =~ /(\d)$/;
            $cfile = "/data/mops13.0/stamp-store/stamps/special-stamps/$dbname-auto/$nn/$dir/C$det_id.fits";
            if (!-f $cfile) {
                $cfile = "/data/mops13.0/stamp-store/stamps/special-stamps/$dbname-auto/$nn/C$det_id.fits";
            }
        }
        $have_cfile = -f $cfile;
    }

    push @stampdata, {
        diff => ($have_dfile ? $dfile : undef),
        chip => ($have_cfile ? $cfile : undef),
        det => $det,
    };
}
</%init>
