<%args>
$imgtype => 'png'
$epoch_mjd => undef
$nn => undef
$category => 'A'
$xrange => 10.0
$binsize => 0.10
</%args>

<%init>
use File::Slurp;
use File::Temp qw/tempfile/;
use PS::MOPS::Lib qw/mopslib_nn2mjd/;

my $model = $m->notes('MODEL');
my $inst = $m->notes('INST');
my $config = $inst->getConfig();
my $gmt_offset_hours = $config->{site}->{gmt_offset_hours};
if ($nn) {
    $epoch_mjd = mopslib_nn2mjd($nn, $gmt_offset_hours);
}

my %typemap = (
    epsc => 'application/postscript',
    eps => 'application/postscript',
    png => 'image/png',
);

die "bogus image type: $imgtype" unless exists($typemap{$imgtype});
$r->content_type($typemap{$imgtype});

my $stuff = join '', `attrhistplot --title '$model' --category $category --instance $model --binsize $binsize --terminal $imgtype --epoch_mjd $epoch_mjd --xrange $xrange | gnuplot | pngtopnm | pnmscale --xysize 800 600 | pnmtopng`;
$m->clear_buffer;
$m->print($stuff);
$m->abort;
</%init>
