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

<td 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 {
% }
% if ($decider) {
<br><a target="_blank" href="<% sprintf qq{/model/$model/comet_decider.html?det_id=%s}, $det->detId %>">Decider</a>
% } # decider

% } elsif ($foo->{chip}) {

</td><td align="center" valign="center">
<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 {
% }
% if ($decider) {
<br><a target="_blank" href="<% sprintf qq{/model/$model/comet_decider.html?det_id=%s}, $det->detId %>">Decider</a>
% } # decider

% } 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>
% }
</td>
% }
</tr></table>

<%args>
$size => 75
$dets
$short => 0
$decider => undef
</%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 @stamps;
my $dfile;

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));

    $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";
        }
    }
    my $have_dfile = -f $dfile;

    my $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";
        }
    }
    my $have_cfile = -f $cfile;

    if ($have_dfile) {
        push @stamps, { diff => $dfile, det => $det };
    }
    elsif ($have_cfile) {
        push @stamps, { chip => $cfile, det => $det };
    }
    else {
        push @stamps, { none => undef, foo => $cfile, det => $det };
    }

}
</%init>
