#!/usr/bin/perl use strict; use warnings; # # Make the response files and combine spectra, if requested. # # Requirements: # 0) must be in same directory as input event files. # 1) text file which lists the events files to process, one per line # # my $currDir; chomp ($currDir = `pwd`); my $unittype = 'N'; my $count = 0; my $currLine; my @numbers; my $num; my @files; my $file; my $r1_evt ; my $r2_evt ; my $inst; my $binninglist; my $spectrumbinning = "NULL"; my $process_r1 = 0; my $process_r2 = 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"; } # # I assume SAS has been initialized. Continue merrily on. # print "Enter the file listing the event files: "; chomp (my $event_files = <> ); $event_files = $currDir."/".$event_files; # # Check the headers to find out which instruments we need. # if (-e $event_files) { open(EVENTS, "<", $event_files); while ($currLine = ) { $currLine = $currDir."/".$currLine; chomp ($currLine); @files = split (/\s/, $currLine); foreach $file (@files) { $count = $count + 1; $inst = $currDir."/"."instru.list"; `fkeyprint $file instrume outfile=$inst`; open (INSTRU, "<", $inst); while ($currLine = ) { if ($currLine =~ m/INSTRUME= 'RGS1 '/ ) { $r1_evt = $file; $process_r1 = 1; } if ($currLine =~ m/INSTRUME= 'RGS2 '/ ) { $r2_evt = $file; $process_r2 = 1; } } close (INSTRU); `rm $inst`; } } close (EVENTS); } else { print "No events file found; stopping here. \n" ; exit; } my $combine_test = 'n'; if ($count gt 1) { print "Do you want to combine the same orders from the two RGSes? (y/n) \n"; chomp ($combine_test = <> ); if (($combine_test ne 'y') and ($combine_test ne 'Y') and ($combine_test ne 'n') and ($combine_test ne 'N')) { print "Not expecting that! Stopping. \n "; exit; } } # Make sure the files were binned in a way that is compatible with the desired output # in case rgscombine is modified to deal with different orders. if (-e $event_files) { open(EVENTS, "<", $event_files); while ($currLine = ) { $currLine = $currDir."/".$currLine; chomp ($currLine); @files = split (/\s/, $currLine); foreach $file (@files) { $binninglist = $currDir."/"."bins.list"; `fkeyprint $file continue outfile=$binninglist`; open (BINS, "<", $binninglist); while ($currLine = ) { if ($currLine =~ /lambda / ) { $spectrumbinning = "lambda"; } if ($currLine =~ /beta / ) { $spectrumbinning = "beta"; } } close (BINS); `rm $binninglist`; } } close (EVENTS); } if ($spectrumbinning eq "beta") { if (($combine_test eq 'Y') or ($combine_test eq 'y')) { print "The binning is not compatible with the requested combination. \n" ; print "Please run make_filteredevents_rgs with the correct binning. \n"; exit; } } # Make the response files. if (! -d $currDir.'/spectrum') { `mkdir spectrum`; } if ($process_r1 eq 1) { my $r1_srspec1 = glob ("*R1*SRSPEC1*"); my $r1_srspec2 = glob ("*R1*SRSPEC2*"); my $r1_srspec1_cp = "spectrum/r1_srspec1_cp" ; my $r1_srspec2_cp = "spectrum/r1_srspec2_cp" ; my $r1_evt_cp = "spectrum/r1_evt_cp"; my $r1_bgspec1 = glob ("*R1*BGSPEC1*"); my $r1_bgspec2 = glob ("*R1*BGSPEC2*"); my $r1_bgspec1_cp = "spectrum/r1_bgspec1_cp"; my $r1_bgspec2_cp = "spectrum/r1_bgspec2_cp"; my $r1_srcli = glob ("*R1*SRCLI*"); my $r1_srcli_cp = "spectrum/r1_srcli_cp"; my $r1_expmap = glob ("*R1*EXPMAP*"); my $r1_expmap_cp = "spectrum/r1_expmap_cp"; `cp $r1_evt $r1_evt_cp`; `cp $r1_srspec1 $r1_srspec1_cp`; `cp $r1_srspec2 $r1_srspec2_cp`; `cp $r1_bgspec1 $r1_bgspec1_cp`; `cp $r1_bgspec2 $r1_bgspec2_cp`; `cp $r1_srcli $r1_srcli_cp`; `cp $r1_expmap $r1_expmap_cp`; chdir $currDir.'/spectrum'; my $r1_o1_rmf = "r1_o1_rmf.fits"; my $r1_o2_rmf = "r1_o2_rmf.fits"; print "Making R1 response files. This will take some time... \n"; `rgsrmfgen spectrumset=r1_srspec1_cp rmfset=$r1_o1_rmf evlist=r1_evt_cp emin=0.4 emax=2.5 rows=5000 srclist=r1_srcli_cp spectrumbinning=$spectrumbinning`; `rgsrmfgen spectrumset=r1_srspec2_cp rmfset=$r1_o2_rmf evlist=r1_evt_cp emin=0.4 emax=2.5 rows=5000 order=2 srclist=r1_srcli_cp spectrumbinning=$spectrumbinning`; chdir $currDir; } if ($process_r2 eq 1) { my $r2_srspec1 = glob ("*R2*SRSPEC1*"); my $r2_srspec2 = glob ("*R2*SRSPEC2*"); my $r2_srspec1_cp = "spectrum/r2_srspec1_cp" ; my $r2_srspec2_cp = "spectrum/r2_srspec2_cp" ; my $r2_evt_cp = "spectrum/r2_evt_cp"; my $r2_bgspec1 = glob ("*R2*BGSPEC1*"); my $r2_bgspec2 = glob ("*R2*BGSPEC2*"); my $r2_bgspec1_cp = "spectrum/r2_bgspec1_cp"; my $r2_bgspec2_cp = "spectrum/r2_bgspec2_cp"; my $r2_srcli = glob ("*R2*SRCLI*"); my $r2_srcli_cp = "spectrum/r2_srcli_cp"; my $r2_expmap = glob ("*R2*EXPMAP*"); my $r2_expmap_cp = "spectrum/r2_expmap_cp"; `cp $r2_evt $r2_evt_cp`; `cp $r2_srspec1 $r2_srspec1_cp`; `cp $r2_srspec2 $r2_srspec2_cp`; `cp $r2_bgspec1 $r2_bgspec1_cp`; `cp $r2_bgspec2 $r2_bgspec2_cp`; `cp $r2_srcli $r2_srcli_cp`; `cp $r2_expmap $r2_expmap_cp`; chdir $currDir.'/spectrum'; my $r2_o1_rmf = "r2_o1_rmf.fits"; my $r2_o2_rmf = "r2_o2_rmf.fits"; print "Making R2 response files. This will take some time...\n"; `rgsrmfgen spectrumset=r2_srspec1_cp rmfset=$r2_o1_rmf evlist=r2_evt_cp emin=0.4 emax=2.5 rows=5000 srclist=r2_srcli_cp spectrumbinning=$spectrumbinning`; `rgsrmfgen spectrumset=r2_srspec2_cp rmfset=$r2_o2_rmf evlist=r2_evt_cp emin=0.4 emax=2.5 rows=5000 order=2 srclist=r2_srcli_cp spectrumbinning=$spectrumbinning`; } if (($combine_test eq 'y') or ($combine_test eq 'Y')) { # Combine the same orders from the two RGSes chdir $currDir.'/spectrum'; print "Combining first order spectra... \n "; `rgscombine pha='r1_srspec1_cp r2_srspec1_cp' bkg='r1_bgspec1_cp r2_bgspec1_cp' rmf='r1_o1_rmf.fits r2_o1_rmf.fits' filepha='r12_o1_srspec.fits' filermf='r12_o1_rmf.fits' filebkg='r12_o1_bgspec.fits' rmfgrid=5000`; print "Output files are r12_o1_srspec.fits, r12_o1_rmf.fits, and r12_o1_bgspec.fits. \n"; print "Combining second order spectra... \n "; `rgscombine pha='r1_srspec2_cp r2_srspec2_cp' bkg='r1_bgspec2_cp r2_bgspec2_cp' rmf='r1_o2_rmf.fits r2_o2_rmf.fits' filepha='r12_o2_srspec.fits' filermf='r12_o2_rmf.fits' filebkg='r12_o2_bgspec.fits' rmfgrid=5000`; print "Output files are r12_o2_srspec.fits, r12_o2_rmf.fits, and r12_o2_bgspec.fits. \n"; } # Clean up. chdir $currDir.'/spectrum'; if ($process_r1 eq 1) { `rm r1_srspec1_cp`; `rm r1_srspec2_cp`; `rm r1_bgspec1_cp`; `rm r1_bgspec2_cp`; `rm r1_evt_cp`; `rm r1_srcli_cp`; `rm r1_expmap_cp`; } if ($process_r2 eq 1) { `rm r2_srspec1_cp`; `rm r2_srspec2_cp`; `rm r2_bgspec1_cp`; `rm r2_bgspec2_cp`; `rm r2_evt_cp`; `rm r2_srcli_cp`; `rm r2_expmap_cp`; }