#!/usr/bin/perl use strict; use warnings; # # Filters event files based on the light curves. If there is more than one # time period to excise, it will throw a DssBlockClash warning, but the output # will still be correct. # # Requirements: # 0) must be in same directory as input event files. # 1) text file which lists the events files to process, one per line # 2) text file which lists the BAD time intervals, one per line. # my $currDir; chomp ($currDir = `pwd`); my $m1_flt_evt2 = "m1_flt_evt2.fits"; my $m2_flt_evt2 = "m2_flt_evt2.fits"; my $pn_flt_evt2 = "pn_flt_evt2.fits"; my $unittype = 'N'; my $count = 0; my $currLine; my @numbers; my $num; my @files; my $file; my $m1_evt ; my $m2_evt ; my $pn_evt ; my $inst; my $process_pn = 0; my $process_m1 = 0; my $process_m2 = 0; # Set the environment variables. my @res1; my $baseDir; if ($currDir =~ m/proc/) { @res1 = split(/proc/, $currDir); $baseDir = $res1[0]; } if (-d $baseDir."/ODF") { $ENV{SAS_ODFPATH} = $baseDir."/ODF"; my $sumsas = glob ($baseDir."/ODF/*SUM.SAS"); $ENV{SAS_ODF} = $sumsas; $ENV{SAS_CCF} = $baseDir."/ODF/ccf.cif"; } if (-d $baseDir."/odf") { $ENV{SAS_ODFPATH} = $baseDir."/odf"; my $sumsas = glob ($baseDir."/odf/*SUM.SAS"); $ENV{SAS_ODF} = $sumsas; $ENV{SAS_CCF} = $baseDir."/odf/ccf.cif"; } # On to the main event. print "Enter the file listing the event files: "; chomp (my $event_files = <> ); $event_files = $currDir."/".$event_files; print "Enter the file with the times to be removed: "; chomp (my $times_file= <> ) ; $times_file = $currDir."/".$times_file; if (-e $event_files) { open(EVENTS, "<", $event_files); while ($currLine = ) { $currLine = $currDir."/".$currLine; chomp ($currLine); @files = split (/\s/, $currLine); foreach $file (@files) { # Check the headers to find out which instruments we need. $inst = $currDir."/"."instru.list"; `fkeyprint $file instrume outfile=$inst`; open (INSTRU, "<", $inst); while ($currLine = ) { if ($currLine =~ m/INSTRUME= 'EPN '/ ) { $pn_evt = $file; $process_pn = 1; } if ($currLine =~ m/INSTRUME= 'EMOS1 '/ ) { $m1_evt = $file; $process_m1 = 1; } if ($currLine =~ m/INSTRUME= 'EMOS2 '/ ) { $m2_evt = $file; $process_m2 = 1; } } close (INSTRU); `rm $inst`; } } close (EVENTS); } else { print "No events file found! \n" ; exit; } my $counttimes = 0; my $countlines = 0; my @starttime; my @endtime; my $j=0; my $filtset; if (-e $times_file) { open(TIMES, "<", $times_file); while ($currLine = ) { $countlines ++; @numbers = split (/\s/, $currLine); # count array elements to confirm the correct format and assign to variables if (@numbers != 2) { print "The input file does not have the correct format. \n"; exit; } push (@starttime, $numbers[0]); push (@endtime, $numbers[1]); } close(TIMES); } else { print "Can't find the times file! \n"; exit; } # # Remove the bad times iteratively. # if (! $countlines) { print "Times files is empty.\n"; exit; } my $m1_evt_hold = $m1_evt; my $m2_evt_hold = $m2_evt; my $pn_evt_hold = $pn_evt; print "Working... \n"; if ($process_m1) { for ($j=0; $j<$countlines; $j++) { $filtset = $m1_evt_hold."_".$j; `evselect table=$m1_evt_hold withfilteredset=yes expression='!(TIME in [$starttime[$j]:$endtime[$j]])' filteredset=$filtset filtertype=expression keepfilteroutput=yes updateexposure=yes filterexposure=yes`; $m1_evt_hold = $filtset; } `mv $filtset $m1_flt_evt2`; # Tidy up. $m1_evt_hold = $m1_evt; for ($j=0; $j<($countlines-1); $j++) { $filtset = $m1_evt_hold."_".$j; `rm $filtset`; $m1_evt_hold = $filtset; } } if ($process_m2) { for ($j=0; $j<$countlines; $j++) { $filtset = $m2_evt_hold."_".$j; `evselect table=$m2_evt_hold withfilteredset=yes expression='!(TIME in [$starttime[$j]:$endtime[$j]])' filteredset=$filtset filtertype=expression keepfilteroutput=yes updateexposure=yes filterexposure=yes`; $m2_evt_hold = $filtset; } `mv $filtset $m2_flt_evt2`; # Tidy up. $m2_evt_hold = $m2_evt; for ($j=0; $j<($countlines-1); $j++) { $filtset = $m2_evt_hold."_".$j; `rm $filtset`; $m2_evt_hold = $filtset; } } if ($process_pn) { for ($j=0; $j<$countlines; $j++) { $filtset = $pn_evt_hold."_".$j; `evselect table=$pn_evt_hold withfilteredset=yes expression='!(TIME in [$starttime[$j]:$endtime[$j]])' filteredset=$filtset filtertype=expression keepfilteroutput=yes updateexposure=yes filterexposure=yes`; $pn_evt_hold = $filtset; } `mv $filtset $pn_flt_evt2`; # Tidy up. $pn_evt_hold = $pn_evt; for ($j=0; $j<($countlines-1); $j++) { $filtset = $pn_evt_hold."_".$j; `rm $filtset`; $pn_evt_hold = $filtset; } } print "Done. \n";