package EPICImageSize;
use strict;
use English;
use Carp;
use vars
qw(@ISA $VERSION $name $author $date $version @instList $numberOfStreams);
@ISA = qw(Module);
use ModuleResources;
use ModuleUtil;
use POSIX;
$name = __PACKAGE__;
$VERSION = '0.04';
$version = $VERSION;
$author = 'Ed Chapin, Jose Vicente Perea';
$date = '2018-07-25';
@instList = qw(epic);
sub numberOfStreams
{
1;
}
sub evaluateRules
{
my $inst = thisInstrument;
return ignore()
if allIgnored( module => 'pnEvents' ) and allIgnored( module => 'MOSEvents' );
start()
if (allComplete( module => 'pnEvents' ) and allComplete( module => 'MOSEvents' ));
}
sub performAction
{
info("Module version number: $version");
my @evFiles= ( findFile(class => 'product', content => 'EPIC PN imaging mode event list'),
findFile(class => 'product', content => 'EPIC MOS imaging mode event list') );
my $attGTIFile = findFile(
class => 'intermediate'
, instrument => 'all'
, content => 'attitude GTI'
, required => 'true'
);
info("global Attitude GTI File: $attGTIFile");
if ($#evFiles<0)
{
return success();
}
my $gti = readFITSTable( file => $attGTIFile,
extension => "STDGTI",
colname => [qw( START STOP )] );
my ($tmin6, $tmax6, $tmin7, $tmax7);
foreach my $evFile (@evFiles)
{
my $ev = readFITSTable( file => $evFile,
extension => "EVENTS",
colname => [qw( TIME X Y )] );
my $xnull = readFITSKeyword( file => $evFile
,extension => 'EVENTS'
,keyword => 'TNULL6');
my $ynull = readFITSKeyword( file => $evFile
,extension => 'EVENTS'
,keyword => 'TNULL7');
info("X,Y NULL values: $xnull , $ynull\n");
my $xmin;
my $xmax;
my $ymin;
my $ymax;
my $init=1;
for my $j ( 0 .. $#{$ev->{TIME}} ) {
my $time = ${$ev->{TIME}}[$j];
my $x = ${$ev->{X}}[$j];
my $y = ${$ev->{Y}}[$j];
for my $i ( 0 .. $#{$gti->{START}} ) {
if ( $time >= ${$gti->{START}}[$i] &&
$time <= ${$gti->{STOP}}[$i] &&
$x != $xnull &&
$y != $ynull ) {
if ( $init ) {
$xmin = $x;
$xmax = $x;
$ymin = $y;
$ymax = $y;
undef $init;
info("Initial: $xmin $xmax $ymin $ymax");
} else {
if ($x < $xmin) { $xmin = $x; }
if ($x > $xmax) { $xmax = $x; }
if ($y < $ymin) { $ymin = $y; }
if ($y > $ymax) { $ymax = $y; }
unless( defined($xmin) && defined($xmax) && defined($ymin) && defined($ymax) ) {
info("Huh? Undefined bounds: xmin=$xmin xmax=$xmax ymin=$ymin ymax=$ymax");
}
}
}
}
}
info("First pass $evFile: xmin=$xmin xmax=$xmax ymin=$ymin ymax=$ymax");
my $thistmin6;
my $thistmax6;
my $thistmin7;
my $thistmax7;
if( defined($xmin) && defined($xmax) && defined($ymin) && defined($ymax) ) {
my $xminscaled = ceil(-1.0*$xmin/3600.0);
my $xmaxscaled = ceil(($xmax-51840)/3600.0);
my $yminscaled = ceil(-1.0*$ymin/3600.0);
my $ymaxscaled = ceil(($ymax-51840)/3600.0);
$thistmin6= 1-3600*( ($xminscaled > 0) ? $xminscaled : 0 );
$thistmax6 = 51840+3600*( ($xmaxscaled > 0) ? $xmaxscaled : 0);
$thistmin7= 1-3600*( ($yminscaled > 0) ? $yminscaled : 0 );
$thistmax7 = 51840+3600*( ($ymaxscaled > 0) ? $ymaxscaled : 0);
} else {
info("No valid events during GTIs found in $evFile.");
}
info("First pass $evFile: tmin6=$thistmin6 tmax6=$thistmax6 tmin7=$thistmin7 tmax7=$thistmax7");
if ($evFile eq $evFiles[0])
{
$tmin6 = $thistmin6;
$tmax6 = $thistmax6;
$tmin7 = $thistmin7;
$tmax7 = $thistmax7;
}
else
{
$tmin6 = $thistmin6 if $thistmin6 < $tmin6;
$tmax6 = $thistmax6 if $thistmax6 > $tmax6;
$tmin7 = $thistmin7 if $thistmin7 < $tmin7;
$tmax7 = $thistmax7 if $thistmax7 > $tmax7;
}
info("Current max ranges: $tmin6 $tmax6 $tmin7 $tmax7");
}
unless( defined($tmin6) && defined($tmax6) && defined($tmin7) && defined($tmax7) ) {
info("No valid events found, so TLMIN/MAX will not be updated");
return success();
}
info("Full set of EPIC events encompassed by TLMIN6=$tmin6 TLMAX6=$tmax6 TLMIN7=$tmin7 TLMAX7=$tmax7");
foreach my $evFile (@evFiles)
{
info("Writing TLMIN/MAX{6,7} to $evFile. TLMIN6=$tmin6 TLMAX6=$tmax6 TLMIN7=$tmin7 TLMAX7=$tmax7");
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMIN6" => {value => $tmin6});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMAX6" => {value => $tmax6});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMIN7" => {value => $tmin7});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMAX7" => {value => $tmax7});
}
my @evFilesFWCandOOT= ( findFile(class => 'intermediate', content => 'EPIC PN imaging mode FWC event list'),
findFile(class => 'intermediate', content => 'EPIC PN imaging mode OOT event list'),
findFile(class => 'intermediate', content => 'EPIC MOS imaging mode FWC event list') );
foreach my $evFile (@evFilesFWCandOOT)
{
info("Writing TLMIN/MAX{6,7} to $evFile. TLMIN6=$tmin6 TLMAX6=$tmax6 TLMIN7=$tmin7 TLMAX7=$tmax7");
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMIN6" => {value => $tmin6});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMAX6" => {value => $tmax6});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMIN7" => {value => $tmin7});
FITSUtils::writeKeyword($evFile, "EVENTS", "TLMAX7" => {value => $tmax7});
}
return success();
}
1;