<%args>
$det_id
</%args>

<%init>
# Given a detection ID, return the diff FITS postage stamp.
use File::Slurp;
use Digest::MD5;
use PS::MOPS::DC::Detection;

my $inst = $m->notes('INST');
my $dbname = $inst->dbname;
my $det = modcd_retrieve($inst, detId => $det_id);

my $id = sprintf "%010d", $det_id;
my $nn = int($det->epoch) - 1;  # TJD to MOPS nn
my ($dir) = $det_id =~ /(\d)$/;
my $dfile = "/data/mops13.0/stamp-store/stamps/special-stamps/$dbname-auto/$nn/$dir/D$det_id.fits";
if (!-f $dfile) {
    die "can't find stamp for det ID $det ID";
}

my $fits = read_file($dfile);
my $dlfile = "$dbname-D$det_id.fits";
$r->content_type('image/fits');
$r->headers_out->add('Content-Disposition' => "filename=$dlfile");
$m->clear_buffer;
$m->print($fits);
$m->abort;
</%init>
