#! /bin/sh # This is the LHEA perl script: grating2xspec # The purpose of this special block is to make this script work with # the user's local perl, regardless of where that perl is installed. # The variable LHEAPERL is set by the initialization script to # point to the local perl installation. #------------------------------------------------------------------------------- eval ' if [ "x$LHEAPERL" = x ]; then echo "Please run standard LHEA initialization before attempting to run acisscr een." exit 3 elif [ "$LHEAPERL" = noperl ]; then echo "During LHEA initialization, no acceptable version of Perl was found." echo "Cannot execute script acisscreen." exit 3 elif [ `$LHEAPERL -v < /dev/null 2> /dev/null | grep -ic "perl"` -eq 0 ]; then echo "LHEAPERL variable does not point to a usable perl." exit 3 else exec $LHEAPERL -x $0 ${1+"$@"} fi ' if(0); # Do not delete anything above this comment from an installed LHEA script! #------------------------------------------------------------------------------- #! /usr1/local/bin/perl5 $version ="1.00"; $date ="2001-04-28"; $author = "kaa"; # This script takes the standard Chandra grating pipeline PHA2 spectrum and # creates the background file from the BACKGROUND_DOWN and BACKGROUND_UP files. # Get and parse the command line option if ($ARGV[0]=~/-(\S+)/){ $option=$1; if($option=~/h/){$help =1;} shift(@ARGV); } if($help){ print "\n grating2xspec version $version $date $author\n\n"; print "usage: grating2xspec [-h] filename option\n\n"; print "This script uses the input pipeline PHA2 spectrum to make another\n"; print "type II spectrum file containing the background. The input file\n"; print "is also copied and the copy modified so that the BACKFILE column\n"; print "points to the correct background spectrum. The option argument is\n"; print "either up, down, or both. If up then BACKGROUND_UP is used, if\n"; print "down then BACKGROUN_DOWN and if both then the _UP and _DOWN spectra\n"; print "are summed and the BACKSCUP and BACKSCDN keywords are summed.\n"; print "\n"; print "e.g. grating2xspec foo_pha2.fits both\n"; print "creates a background file foo_bkg2.fits from the sum of the\n"; print "BACKGROUND_UP and BACKGROUND_DOWN columns in foo_pha2.fits. It\n"; print "also creates a copy of the input file as foo_npha2.fits which\n"; print "includes pointers to the background file. To read say the 3rd\n"; print "spectrum into XSPEC simply give the command\n"; print "XSPEC> data foo_npha2.fits{3}\n"; exit(0); } # we need HEASOFT... if($ENV{'LHEASOFT'} !~/\S/) { print "\n You need to set up HEASOFT to use this script.\n\n"; exit(0); } # Check that the user gave the correct number of arguments. if(@ARGV != 2) { print "\n usage : grating2xspec [-h] filename option\n"; print " type grating2xspec -h to get more information\n\n"; exit(0); } $infile = $ARGV[0]; ($filename = $infile) =~ s/pha2/npha2/; ($backfile = $infile) =~ s/pha2/bkg2/; $option = $ARGV[1]; if ( $option != "up" && $option != "down" && $option != "both" ) { print "option must be one of up, down or both\n"; exit(1); } # make a copy of the input file $command = "cp $infile $filename"; print "\n",$command,"\n"; system($command); # copy the input file into the background file using on-the-fly calculation # to reset the COUNTS column if ( $option eq "up" ) { $expres = "BACKGROUND_UP" } elsif ( $option eq "down" ) { $expres = "BACKGROUND_DOWN" } elsif ( $option eq "both" ) { $expres = "BACKGROUND_UP + BACKGROUND_DOWN" } $command = "fcopy '$filename\[SPECTRUM\]\[col COUNTS = $expres\]' $backfile"; print "\n",$command,"\n"; system($command); # Set the BACKSCAL keyword. $command = "fkeypar $filename\[SPECTRUM\] BACKSCUP\n"; system($command); ($backscup = `pget fkeypar value`) =~ s/\n//; $command = "fkeypar $filename\[SPECTRUM\] BACKSCDN\n"; system($command); ($backscdn = `pget fkeypar value`) =~ s/\n//; # catch the problem with the BACKSCUP/DN being the inverse of their true # values if ( $backscup < 1 ) { $backscup = 1/$backscup } if ( $backscdn < 1 ) { $backscdn = 1/$backscdn } if ( $option eq "up" ) { $backscal = $backscup } elsif ( $option eq "down" ) { $backscal = $backscdn } else { $backscal = $backscup + $backscdn } $command = "fparkey value=$backscal fitsfile=$backfile\[SPECTRUM\] keyword=BACKSCAL"; print "\n",$command,"\n"; system($command); # Delete the superfluous columns $command = "fdelcol infile=$backfile\[SPECTRUM\] colname=BACKGROUND_UP confirm=no proceed=yes"; print $command,"\n"; system($command); $command = "fdelcol infile=$backfile\[SPECTRUM\] colname=BACKGROUND_DOWN confirm=no proceed=yes"; print $command,"\n"; system($command); # Also clobber the STAT_ERR column and set the POISSERR keyword to T $command = "fdelcol infile=$backfile\[SPECTRUM\] colname=STAT_ERR confirm=no proceed=yes"; print "\n",$command,"\n"; system($command); $command = "fparkey value=T fitsfile=$backfile\[SPECTRUM\] keyword=POISSERR"; print "\n",$command,"\n"; system($command); # Finally edit the input filename to set the BACKFILE column appropriately. # First make an ascii file with the names to place in the column open(TFILE, "> backlist.tmp"); # get the number of rows $command = "fkeypar $filename\[SPECTRUM\] NAXIS2\n"; system($command); ($nrows = `pget fkeypar value`) =~ s/\n//; # and set the BACKFILE values foreach $i (1 .. $nrows) { $backname = $backfile . "{" . $i . "}"; print TFILE $backname,"\n"; } close(TFILE); # Make the file that describes the format for the BACKFILE column open(TFILE, "> backlist.fmt"); print TFILE "BACKFILE 64A","\n"; close(TFILE); # Use fcreate to make a FITS file containing this data $command = "fcreate cdfile=backlist.fmt datafile=backlist.tmp outfile=backlist.fits"; print "\n",$command,"\n"; system($command); # Finally add this new column back into the input file $command = "faddcol infile=$filename\[SPECTRUM\] colfile=backlist.fits colname=BACKFILE delkey=yes history=yes casesen=no"; print "\n",$command,"\n"; system($command); # Tidy up the temporary files $command = "rm -f backlist.tmp backlist.fmt backlist.fits"; print "\n",$command,"\n"; system($command); # delete the superfluous BACKFILE keyword if it exists $command = "fparkey dummy $filename\[SPECTRUM\] BACKFILE add=yes"; print "\n",$command,"\n"; system($command); $command = "fparkey dummy $filename\[SPECTRUM\] -BACKFILE"; print $command,"\n"; system($command);