generate_filename (version 3.0)

You can also look at:
#! /usr/bin/perl

##############################################################################
#
# SYNTAX: generate_filename type inst index mode bitrate source [optarg]
#
# DESCRIPTION: Generates a file name from information about that file.
#
# NOTE: This script can be recursive under the following conditions:
#       type is "telemetry" -> called again with type of "unfiltered"
#       type is "telemetry" and generate_filename (unfiltered) is
#            not readable -> called again with type of "event"
#       type is "rmf" and inst is g[23] and source is "" or "any" ->
#            called again with type of "calsource"
#       type is "rmf" and inst is g[23] and source is neither "" nor "any" ->
#            called again with type of "spectrum"
#       type is "rmf" and inst is \[sg\].* or \[gs\].* ->
#            called again with type "rmf" for inst values of s0, s1, g2, and g3
#       type is "regionmap" -> called again with type of "extractregion"
#       type is "teldef" and inst is g[23] ->
#            called again with type of "unfiltered"
#       type is "teldef" and generate_filename (unfiltered) is
#            not readable -> called again with type of "event"
#       type is "extractregion" and inst is g[23] ->
#            called again with type of "unfiltered"
#       type is "extractregion" and generate_filename (unfiltered) is
#            not readable -> called again with type of "event"
#
# VERSION: 3.0
#
# HISTORY: 0.0 -> 1.0 8/19/96
# HISTORY: Quick analysis output files now
# HISTORY: have _sqa or _gqa, as they were renamed in rev1.
# HISTORY: 8/23/96 Added "scraps" file type for the tar-ed together
# HISTORY: small leftover raw files. Intended to be kept as a trend
# HISTORY: product.
# HISTORY:
# HISTORY: 1.0 -> 1.1 9/10/96
# HISTORY: Changed sky and totsky to accept an additional energy attribute
# HISTORY: in position 6.
# HISTORY: Deleted ".evt" from expo filename.
# HISTORY: Changed image type to corimg.
# HISTORY:
# HISTORY: 1.1 -> 1.2 9/13/96
# HISTORY: Changed ".log" to ".html".
# HISTORY:
# HISTORY: 1.2 -> 1.3 9/26/96
# HISTORY: Added "sourcepic" and "smooth" file types.
# HISTORY:
# HISTORY: 1.3 -> 1.4 10/8/96
# HISTORY: Added "sourcecat", "blankback", "sourcefree".
# HISTORY: Modified "spectrum" and "rmf".
# HISTORY:
# HISTORY: 1.4 -> 1.5 11/15/96
# HISTORY: Added some quotes to the "any instrument" case of the rmf
# HISTORY: file type. This preserved wildcards and fixed the problem where
# HISTORY: the sis rmfs weren't being listed in the trend cat.
# HISTORY:
# HISTORY: 1.5 -> 1.6 11/25/96
# HISTORY: Added "brightearth", "darkearth", "calsource", and "lightcurve".
# HISTORY: 12/4/96 Changed sourcepic to be a .ps file instead of a .ppm file.
# HISTORY:
# HISTORY: 1.6 -> 1.7 12/19/96
# HISTORY: Added sourceregion, specplot,lcplot, srcevent.
# HISTORY:
# HISTORY: 1.7 -> 1.8 2/4/97
# HISTORY: Added $indx to file names for "totexpo", "totsky", "corimg",
# HISTORY: "smooth".
# HISTORY: Changed format for "gif", "sourcepic", "sourceregion", and
# HISTORY: "backblank".
# HISTORY: Index is now forced to be a three-digit number, and padded with
# HISTORY: leading zeros if it isn't.
# HISTORY: 2/6/97 Modified "rmf" so that source is in the source slot,
# HISTORY: not the index slot.
# HISTORY: Changed the extension on "srcevents" from ".xtr" to ".ulc".
# HISTORY: 2/7/97 Added "sourceinfo".
# HISTORY: 2/18/97 Changed "rmf" to accomodate different numbers of
# HISTORY: PHA channels by reading NAXIS2 from the corresponding
# HISTORY: spectrum file.
# HISTORY:
# HISTORY: 1.8 -> 1.9 2/22/97
# HISTORY: Changed "calsource" to accomodate merged cal source spectra
# HISTORY: by taking bitrate out of name.
# HISTORY: Added "calsourceplot".
# HISTORY: 2/25/97 Modified "rmf" to accomodate calsource rmfs.
# HISTORY:
# HISTORY: 1.9 -> 1.10 3/24/97
# HISTORY: Added orbit file as read from job.par.
# HISTORY: Added discrim and changed sourceregion to accomodate
# HISTORY: files for each of the four instruments.
# HISTORY:
# HISTORY: 1.10 -> 1.11 5/17/97
# HISTORY: Removed repro version from "scraps" file name.
# HISTORY:
# HISTORY: 1.11 -> 1.12 9/25/97
# HISTORY: Supplied missing "UTIL" before a number of calls to "exception".
# HISTORY:
# HISTORY: 1.12 -> 2.0 12/29/97
# HISTORY: Supplied missing continuation character in exit test call.
# HISTORY: Added "corner".
# HISTORY:
# HISTORY: 2.0 -> 2.1 1/20/98
# HISTORY: Added "cleaned".
# HISTORY: 
# HISTORY: 2.1 -> 3.0 1999-03-24
# HISTORY: Converted to perl.
# HISTORY: Deleted ratefile, quickout, and quickps.
#
# CALLS: $UTIL/read_parfile
# CALLS: $UTIL/generate_filename
# CALLS: $UTIL/exception
# CALLS: $UTIL/read_fits_keyword
# CALLS: $UTIL/exit_test
# CALLS: $FTOOL/fkeypar
# CALLS: $FTOOL/pget
#
##############################################################################

use File::Basename;

#$DEBUG = 1;

($type, $inst, $indx, $mode, $bitrate) = @ARGV;

$UTIL = $ENV{"UTIL"};
$FTOOL = $ENV{"FTOOL"};
$JOBPAR = $ENV{"JOBPAR"};
$PARFILE = $ENV{"PARFILE"};

chomp($seq = `$UTIL/read_parfile $JOBPAR sequence`);
chomp($ver = `$UTIL/read_parfile $JOBPAR seqprocnum`);


#################################
# Parse info into neutral format
#################################
if ( "$seq" eq "any" ) {
    $seq = "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
}

if ( "$inst" eq "any" ) {
    $inst = "[sg][0123]";
}

for ( $indx ) {
    /^any$/ and do {
        $indx = "*";
        last;
    };

    /.*\*.*|.*\[.*\].*/ and do {
        last;
    };

    #
    # Force index to be a three-digit number
    #
    /.*/ and do {
        $indx = sprintf "%03d", $indx;
        last;
    };
}

if ( "$mode" eq "any" ) {
    for ( $inst ) {
        /^s\[01\]$/ and do {
            $mode ="[01][123]";
            last;
        };

        /^g\[23\]$/ and do {
            $mode = "7[01]";
            last;
        };

        /.*/ and do {
            $mode = "[017][0123]";
            last;
        };
    }
}

if ( "$bitrate" eq "any" ) {
    $bitrate = "[hml]";
}

########################
# General filename base
########################
$base = "ad${seq}${inst}${indx}${mode}${bitrate}";

###################################################
# Determine the appropriate file name for the type
###################################################
for ( $type ) {

    /^orbit$/ and do {
        #############
        # Orbit file
        #############
	chomp($orbit_name = `$UTIL/read_parfile $JOBPAR orbit`);
        $orbit_name = basename($orbit_name);
	print "$orbit_name\n";
        last;
    };

    /^scraps$/ and do {
        ###############################################
        # The tar-ed together small leftover raw files
        ###############################################
	print "ad${seq}_scraps.tar\n";
	last;
    };

    /^jobpar$/ and do {
        #############################
        # "saved" version of job.par
        #############################
	print "ad${seq}_${ver}_${JOBPAR}\n";
	last;
    };

    /^parfile$/ and do {
        #################################
        # "saved" version of process.par
        #################################
	print "ad${seq}_${ver}_${PARFILE}\n";
        last;
    };

    /^sciencetimes$/ and do {
        ###############################
        # Science times trend products
        ###############################
	print "ad${seq}_science_times.${inst}\n";
        last;
    };


    /^ascamode$/ and do {
        ######################
        # Ascamode trend file
        ######################
	print "ad${seq}_ascamode.txt\n";
        last;
    };

    /^ascalog$/ and do {
        ######################
        # Ascamode trend file
        ######################
        print "ad${seq}_ascalog.txt\n";
        last;
    };

    /^joblog$/ and do {
        ##########
        # Job log
        ##########
	print "ad${seq}_${ver}_joblog.html\n";
	last;
    };

    /^errlog$/ and do {
        ############
        # Error log
        ############
        print "ad${seq}_${ver}_errors.html\n";
	last;
    };

    /^milestones$/ and do {
        ################
        # Job log index
        ################
        print "ad${seq}_${ver}_index.html\n";
        last;
    };

    /^header$/ and do {
        ###################
        # HTML header page
        ###################
        print "ad${seq}_${ver}_hdr_page.html\n";
        last;
    };

    /^eventinfo$/ and do {
        ###################################
        # HTML event file information page
        ###################################
        print "ad${seq}_${ver}_event_info.html\n";
        last;
    };

    /^fileinfo$/ and do {
        #########################
        # HTML list of all files
        #########################
        print "ad${seq}_${ver}_file_info.html\n";
	last;
    };

    /^sourceinfo$/ and do {
        #########################
        # HTML list of all files
        #########################
        print "ad${seq}_${ver}_source_info.html\n";
        last;
    };

    /^procinfo$/ and do {
        #########################
        # HTML list of all files
        #########################
        print "ad${seq}_${ver}_proc_info.txt\n";
        last;
    };

    /^tapecat$/ and do {
        ###################################
        # FITS format distribution catalog
        ###################################
	print "ad${seq}_tape.cat\n";
        last;
    };

    /^trendcat$/ and do {
        ###################################
        # FITS format distribution catalog
        ###################################
        print "ad${seq}_trend.cat\n";
        last;
    };

    /^unfiltered$/ and do {
        ########################
        # unfiltered event file
        ########################
        print "${base}.unf\n";
        last;
    };

    /^event$/ and do {
        ######################
        # filtered event file
        ######################
        print "${base}.evt\n";
        last;
    };

    /^cleaned$/ and do {
        ##############################################
        # SIS unfiltered file with hot pixels removed
        ##############################################
        print "${base}.cln\n";
        last;
    };

    /^corner$/ and do {
        #########################
        # Corner pixel histogram
        #########################
        $chip = $ARGV[5];
	if ( "$chip" eq "any" ) {
	    $chip = "[0123]";
	};
        print "${base}_${chip}.cnr\n";
        last;
    };

    /^srcevent$/ and do {
        ##########################################
        # Event file extracted for a given source
        ##########################################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source="*";
	};
        print "${base}_${source}.ulc\n";
        last;
    };

    /^brightearth$/ and do {
        ##########################
        # Bright earth event file
        ##########################
        print "${base}.brt\n";
        last;
    };

    /^darkearth$/ and do {
        ########################
        # Dark earth event file
        ########################
        print "${base}.drk\n";
        last;
    };

    /^calsource$/ and do {
        ##################################
        # GIS calibration source spectrum
        ##################################
	print "ad${seq}${inst}${indx}${mode}.cal\n";
        last;
    };

    /^calsourceplot$/ and do {
        ##################################
        # GIS calibration source spectrum
        ##################################
	print "ad${seq}${inst}${indx}${mode}_cal.ps\n";
        last;
    };

    /^sourcefree$/ and do {
        #####################################
        # PI spectrum of source-free regions
        #####################################
        print "ad${seq}${inst}${indx}${mode}.free\n";
        last;
    };

    /^detimg$/ and do {
        #################
        # Detector image
        #################
	print "${base}.detimg\n";
        last;
    };

    /^expo$/ and do {
        ###############
        # Exposure map
        ###############
        print "${base}.expo\n";
        last;
    };

    /^sky$/ and do {
        ########################
        # Sky map from ascaexpo
        ########################
        $nrg = $ARGV[5];
        for ( $nrg ) {
            /^all$/ and do { print "${base}.img\n"; last; };
            /^any$/ and do { print "${base}*.img\n"; last; };
            /.*/  and do { print "${base}_${nrg}.img\n"; last; };
        };
        last;
    };

    /^dfe$/ and do {
        ########################
        # Dark frame error file
        ########################
        print "ad${seq}_${inst}.dfe\n";
        last;
    };

    /^telemetry$/ and do {
	################################################
        # Telemetry corresponding to a given event file
        ################################################
        chomp($event = `$UTIL/generate_filename unfiltered \\
                                          $inst $indx $mode $bitrate`);
	if ( ! -r "$event" ) {
            chomp($event = `$UTIL/generate_filename event \\
                                              $inst $indx $mode $bitrate`);
	    if ( ! -r "$event" ) {
		system("$UTIL/exception $0 1 \\
	  'Cannot read event file corresponding to requested telemetry file'");
		$event = "none";
	    }
	}

	if ( "$event" ne "none" ) {
            system("$UTIL/read_fits_keyword $event\[0\] TLM_FILE");
	}
	last;
    };

    /^totsky$/ and do {
        ########################################
        # Sky image summed over all event files
        ########################################
        $nrg = $ARGV[5];
	if ( ! "$nrg" or "$nrg" eq "any" ) {
            $nrg = "*";
	}

        for ( $inst ) {
            /^g.*$/ and do {
		print "ad${seq}gis${indx}${mode}_${nrg}.totsky\n";
		last;
	    };

            /^s.*$/ and do {
		print "ad${seq}sis${indx}${mode}_${nrg}.totsky\n";
		last;
	    };

            /^\[gs\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}_${nrg}.totsky\n";
		last;
	    };

            /^\[sg\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}_${nrg}.totsky\n";
		last;
	    };

            /.*/ and do {
		`$UTIL/exception $0 1 \\
                   "Unknown instrument type for totsky file: $inst"`;
		last;
	    };
	};
	last;
    };

    /^totexpo$/ and do {
        ########################################
        # Sky image summed over all event files
        ########################################
        for ( $inst ) {
            /^g.*$/ and do {
		print "ad${seq}gis${indx}${mode}.totexpo\n";
		last;
	    };

            /^s.*$/ and do {
		print "ad${seq}sis${indx}${mode}.totexpo\n";
		last;
	    };

            /^\[gs\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}.totexpo\n";
		last;
	    };

            /^\[sg\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}.totexpo\n";
		last;
	    };

            /.*/ and do {
		`$UTIL/exception $0 1 \\
                   "Unknown instrument type for totexpo file: $inst"`;
		last;
	    };
	};
        last;
    };

    /^corimg$/ and do {
        ###############################################################
        # Sky image summed over all event files and exposure corrected
        # Assumes for all energies
        ###############################################################
        for ( $inst ) {
            /^g.*$/ and do {
		print "ad${seq}gis${indx}${mode}.corimg\n";
		last;
	    };

            /^s.*$/ and do {
		print "ad${seq}sis${indx}${mode}.corimg\n";
		last;
	    };

            /^\[gs\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}.corimg\n";
		last;
	    };

            /^\[sg\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}.corimg\n";
		last;
	    };

            /.*/ and do {
		`$UTIL/exception $0 1 \\
                   "Unknown instrument type for corimg file: $inst"`;
		last;
	    };
        };
        last;
    };

    /^gif$/ and do {
        #################################
        # A combined image in GIF format
        #################################
        for ( $inst ) {
            /^\[sg\].*$/ and do { $inst = "[sg]is"; };
            /^\[gs\].*$/ and do { $inst = "[sg]is"; };
        };
        print "ad${seq}${inst}${indx}${mode}.gif\n";
	last;
    };

    /^smooth$/ and do {
        ############################################
        # Smoothed and exposure corrected sky image
        ############################################
        $nrg = $ARGV[5];
	if ( ! "$nrg" or "$nrg" eq "any" ) {
            $nrg = "*";
	}

        for ( $inst ) {
            /^g.*$/ and do {
		print "ad${seq}gis${indx}${mode}_${nrg}.smooth\n";
		last;
	    };

            /^s.*$/ and do {
		print "ad${seq}sis${indx}${mode}_${nrg}.smooth\n";
		last;
	    };

            /^\[gs\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}_${nrg}.smooth\n";
		last;
	    };

            /^\[sg\].*$/ and do {
		print "ad${seq}\[sg\]is${indx}${mode}_${nrg}.smooth\n";
		last;
	    };

            /.*/ and do {
		`$UTIL/exception $0 1 \
                   "Unknown instrument type for smooth file: $inst"`;
		last;
	    };
        };
        last;
    };

    /^mask$/ and do {
        ###############
        # Detector map
        ###############
        print "${base}.evt.imap\n";
        last;
    };

    /^sourcepic$/ and do {
        ###############################
        # An image showing the sources
        ###############################
        for ( $inst ) {
            /^\[sg\].*$/ and do { $inst = "[sg]is"; };
            /^\[gs\].*$/ and do { $inst = "[sg]is"; };
        };
	print "ad${seq}${inst}${indx}${mode}_source.ps\n";
        last;
    };

    /^sourcecat$/ and do {
        ###############################
        # An image showing the sources
        ###############################
        for ( $inst ) {
            /^\[sg\].*$/ and do { $inst = "[sg]is"; };
            /^\[gs\].*$/ and do { $inst = "[sg]is"; };
        };
        print "ad${seq}${inst}${indx}${mode}.src\n";
        last;
    };

    /^sourceregion$/ and do {
        ###############################
        # An image showing the sources
        ###############################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
            $source = "[0-9]*";
	};
        print "ad${seq}${inst}${indx}${mode}_${source}.reg\n";
        last;
    };

    /^spectrum$/ and do {
	###################
	# PI spectrum file
	###################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "[0-9]*";
	};
	print "ad${seq}${inst}${indx}${mode}_$source.pi\n";
        last;
    };

    /^specplot$/ and do {
	###################
	# PI spectrum file
	###################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "*";
	};
	print "ad${seq}${inst}${indx}${mode}_${source}_pi.ps\n";
        last;
    };

    /^discrim$/ and do {
	#####################################
	# SIS area discriminator mask region
	#####################################
	print "ad${seq}${inst}${indx}${mode}.discrim\n";
        last;
    };

    /^backblank$/ and do {
	##################################################
	# Background spectrum from a blanksky observation
	##################################################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "*";
	};
	print "ad${seq}${inst}${indx}${mode}_$source.blank\n";
        last;
    };

    /^lightcurve$/ and do {
	###################
	# PI spectrum file
	###################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "*";
	};
	print "ad${seq}${inst}${indx}${mode}_$source.lc\n";
        last;
    };

    /^lcplot$/ and do {
	###################
	# PI spectrum file
	###################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "*";
	};
	print "ad${seq}${inst}${indx}${mode}_${source}_lc.ps\n";
        last;
    };

    /^rate$/ and do {
	################
	# SIS rate file
	################
        if ( $inst =~ /^s0$/ ) { 
            ########
            # SIS0
            ########
            print "ad${seq}S0.rate\n"; 
        } elsif( $inst =~ /^s1$/ ) { 
            ########
            # SIS1
            ########
            print "ad${seq}S1.rate\n"; 
        } elsif( $inst =~ /^g[23]$/ ) { 
            ######################
            # GIS - no rate file
            #######################
            print "none\n" 
        }  else { 
            `$UTIL/exception $0 1 \\
		  "Unknown instrument $inst for rate file name generation"`; 
        }

        last;
    };

    /^rmf$/ and do {
	#######################
	# Response Matrix File
	#######################
        if( $inst =~ /^s.*/ ) {
            ######
            # SIS
            ######
            $source = $ARGV[5];
            if ( "$source" eq "any" ) {
                $source = "*";
            };
            print "ad${seq}${inst}${indx}${mode}_${source}.rmf\n";

        } elsif ( $inst =~ /^g.*/ ) {
            ######
            # GIS
            ######

            #########################################################
            # Get number of PHA bins from corresponding spectrum file
            #########################################################
            if ( "$ARGV[5]" eq ""  or "$ARGV[5]" eq "any" ) {
                ###################################################
                # No source mentioned, assume we are talking about
                # a calibration source spectrum
                ###################################################
                chomp($spec = `$UTIL/generate_filename calsource \\
		                                   "$inst" "$indx" "$mode"`);
            }
            else {
		###################################
		# Assume this is a source spectrum
		###################################
		chomp($spec =`$UTIL/generate_filename spectrum \\
		               "$inst" "$indx" "$mode" "$bitrate" "$ARGV[5]"`);
            };

            chomp($nchannels = `$UTIL/read_fits_keyword ${spec}+1 NAXIS2`);
            if ( ! "$nchannels" ) {
                `$UTIL/exception $0 1 \\
		        "Cannot determine number of PHA channels from $spec"`;
		exit 0;
            };

           system("$UTIL/read_parfile $PARFILE ${inst}_${nchannels}rmf");

        } elsif ( $inst =~ /^\[sg\].*$/ or $inst =~/^\[gs\].*$/ ) {
            ##################
            # any instrument 
            ##################
            foreach $dum (s0,s1,g2,g3) {
                `$UTIL/generate_filename rmf \\
                                $dum "$indx" "$mode" "$bitrate" "$ARGV[5]"`;
            };
	
        } else {
            #####################
            # Unknown instrument
            #####################
            `$UTIL/exception $0 1 "Unknown instrument for rmf file generation"`;
	};

	last;
    };
    
    /^arf$/ and do {
	###################
	# PI spectrum file
	###################
        $source = $ARGV[5];
	if ( "$source" eq "any" ) {
	    $source = "*";
	};
	print "ad${seq}${inst}${indx}${mode}_${source}.arf\n";
        last;
    };

    /^regionmap$/ and do {
	######################################################
	# A 1's and 0's FITS image of an saoimage region file
	# These are used as input detector maps by ascaexpo
	# (see extractregion/ and do {
	######################################################
	chomp($base = `$UTIL/generate_filename extractregion \\
                                         $inst $indx $mode $bitrate`);
	if ( "$base" eq "none" ) {
	    print "none\n";
	}
	else {
	    print "${base}.map\n";
	};
        last;
    };

    /^teldef$/ and do {
	############################
	# Telescope definition file
	############################
        if( $inst =~ /^s[01]$/ ) {
            ######
            # SIS
            ######
            print "${inst}_teldef_ascalin.fits\n";

	} elsif($inst =~ /^g[23]$/ ) {
            ######
            # GIS
            ######
            chomp($event = `$UTIL/generate_filename unfiltered \\
			                          $inst $indx $mode $bitrate`);

            if ( ! -r "$event" ) {
                chomp($event = `$UTIL/generate_filename event \\
						  $inst $indx $mode $bitrate`);

		if ( ! -r "$event" ) {
                    `$UTIL/exception $0 1 \\
	       "Cannot read event file corresponding to requested teldef file"`;
                    $event = "none";
		}
            }

            if ( "$event" ne "none" ) {
		#############################
		# Read POS_DET and ANO_TUNE
		#############################
                $pos_det= lc(`$UTIL/read_fits_keyword $event+0 POS_DET` );
                $ano_tune=lc(`$UTIL/read_fits_keyword $event+0 ANO_TUNE`);

                chomp($pos_det);
                chomp($ano_tune);

                ###################
		# Debugging output
		###################
		if ( $DEBUG ) {
		    print "generate_filename: pos_det=$pos_det\n";
		    print "generate_filename: ano_tune=$ano_tune\n";
		}


                system("$UTIL/read_parfile $PARFILE \\
                       ${inst}${ano_tune}${pos_det} " );


            }

        } else {
            #####################
            # Unknown instrument
            #####################
            `$UTIL/exception $0 1 \\
		     "Unknown instrument ${inst} for teldef file generation"`;
	}
        last;
    };

    /^extractregion$/ and do {
	###########################################################
	# Basic saoimage format region file for extracting events.
	# This is used to remove the calibration source and noisy
	# outer ring when filtering GIS data.
	###########################################################
	for ( $inst ) {
	    /^s\[01\]$/ and do {
		######
		# SIS
		######
		print "none\n";
		last;
	    };

	    /^g\[23\]$/ and do {
		######
		# GIS
		######
		chomp($unfiltered = `$UTIL/generate_filename unfiltered \\
			                         $inst $indx $mode $bitrate`);
		if ( ! -r $unfiltered ) {
		    chomp($unfiltered = `$UTIL/generate_filename event \
			                         $inst $indx $mode $bitrate`);
		};

		$ext = 1;
		if ( -r $unfiltered ) {
		    chomp($rawxbins = `$UTIL/read_fits_keyword \\
                                        ${unfiltered}\[${ext}\] \\
			                RAWXBINS`);

		    chomp($rawybins = `$UTIL/read_fits_keyword \\
                                        ${unfiltered}\[${ext}\] \\
			                RAWYBINS`);

		    if ( $rawxbins ne $rawybins ) {
			####################
			# Inconsistent bins
			####################
			`$UTIL/exception $0 1 "RAWXBINS=$rawxbins, " \\
				              "but RAWYBINS=$rawybins " \\
					      "in ${unfiltered}\[${ext}\]"`;
		    }
		    else {
			#########################################
			# Get appropriate filename from par file
			#########################################
			`$UTIL/read_parfile ./$PARFILE \\
                                            ${inst}region${rawxbins}`;
		    };
		}
		else {
		    ###################################
		    # Can't read unfiltered event file
		    ###################################
		    `$UTIL/exception $0 1 \\
                               "Cannot read RAW[X/Y]BINS from ${unfiltered}"`;
		};

		/.*/ and do {
		    #####################
		    # Unknown instrument
		    #####################
		    `$UTIL/exception $0 1 \\
                             "Unknown instrument: $inst for extractregion"`;
		    last;
		};
	    };

	    /.*/ and do {
		####################
		# Unknown file type
		####################
		`$UTIL/exception $0 1 \\
                           "Cannot generate file name for type: $type"`;
		last;
	    };
	    last;
	};
    };
    last;
}

exit 0;