sky2det (version 2.1)

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

##############################################################################
#
# SYNTAX: sky2det skyx skyy inst mode radius
#
# DESCRIPTION: This utility converts from sky coordinates to detector
# DESCRIPTION: coordinates. It does this by selecting all events with sky
# DESCRIPTION: coordinates within the given radius of the given point.
# DESCRIPTION: The detector coordinates are the mean of the detector coordinates
# DESCRIPTION: of the selected points.
# DESCRIPTION: detx and dety are echoed to stdout
# DESCRIPTION: NOTE: skyx, skyy, and radius MUST have a decimal point
# DESCRIPTION: in them.
#
# VERSION: 2.1
#
# HISTORY: 0.0 -> 1.0 5/21/97
# HISTORY: Now exits right away if there are no event files.
# HISTORY: Also, logs the event files used.
# HISTORY:
# HISTORY: 1.0 -> 1.1 6/3/97
# HISTORY: Shortened "no photons" message.
# HISTORY:
# HISTORY: 1.1 -> 1.2 6/10/97
# HISTORY: Fixed bug where no photons message wasn't shortened for the case
# HISTORY: where there were no filtered event files.
# HISTORY: 
# HISTORY: 1.2 -> 2.0 1999-05-07
# HISTORY: Converted from ksh to perl
# HISTORY: 
# HISTORY: 2.0 -> 2.1 1999-10-07
# HISTORY: Changed input file list in fmerge to a file reference since
# HISTORY: there were some sequences with too many files for the parameter
# HISTORY: interface.
#
# CALLS: $UTIL/log_entry
# CALLS: $UTIL/generate_filename
# CALLS: $UTIL/setup_parfile
# CALLS: $UTIL/ftool_test
# CALLS: $UTIL/read_fits_keyword
# CALLS: $UTIL/read_parfile
# CALLS: $FTOOL/fselect
# CALLS: $FTOOL/fmerge
# CALLS: $FTOOL/fstatistic
# 
##############################################################################

($skyx, $skyy, $inst, $mode, $radius) = @ARGV;

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

`$UTIL/log_entry "Converting ($skyx,$skyy,$radius) to $inst detector coordinates"`;

###############################
# Select rows from event files
###############################
@list=glob(`$UTIL/generate_filename event $inst any $mode any`);

if ( ! "@list" ) {
    print "No photons\n";
    exit 0;

} else {
    `$UTIL/log_entry "Using events in: @list"`;
}

open FILE_LIST, ">file_list.tmp";
@files=();
$i = 0;
foreach $event (@list) {

    $tmp="inreg${$}_$i.tmp";
    unlink $tmp;

    `$UTIL/setup_parfile $FTOOL/fselect.par infile="${event}+1" \\
                                            outfile=$tmp   \\
                        expr="CIRCLE($skyx,$skyy,$radius,X,Y)"   \\
                                            histkw=no            \\
                                            copyall=no           \\
                                            keycopy=no           \\
                                            clobber=yes`;

    `$FTOOL/fselect >stdout.log 2>stderr.log`;
    `$UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log`;

    @files=(@files,$tmp);
    print FILE_LIST "$tmp\n";

    $i++;
}

close FILE_LIST;


################
# Merge files
################
$merged="merged${$}.tmp";
unlink "$merged";
`$UTIL/setup_parfile $FTOOL/fmerge.par infiles="\@file_list.tmp" \\
                                       outfile="$merged"    \\
                                       columns="DETX DETY"  \\
                                       copyprime=no         \\
                                       history=yes          \\
                                       clobber=yes`;

`$FTOOL/fmerge >stdout.log 2>stderr.log`;
`$UTIL/ftool_test fmerge $? $0 2 stdout.log stderr.log`;

##################################################
# Clear out the individual selection result files
##################################################
foreach $file (@files) {
    unlink $file;
}

unlink "file_list.tmp";

##########################################################
# Check if there were any photons within the given radius
##########################################################
chomp($naxis2 = `$UTIL/read_fits_keyword ${merged}+1 NAXIS2`);
if ( ! "$naxis2" or "$naxis2" eq "0" ) {
    print "No photons\n";
    unlink "$merged";
    exit 0;
}

############
# Find DETX
############
`$UTIL/setup_parfile $FTOOL/fstatistic.par infile=$merged \\
                                           colname=DETX`;
`$FTOOL/fstatistic >stdout.log 2>stderr.log`;
$code = $?;
chomp($detx = `$UTIL/read_parfile fstatistic.par mean`);
`$UTIL/ftool_test fstatistic $code $0 2 stdout.log stderr.log`;

############
# Find DETY
############
`$UTIL/setup_parfile $FTOOL/fstatistic.par infile=$merged \\
                                           colname=DETY`;
`$FTOOL/fstatistic >stdout.log 2>stderr.log`;
$code = $?;
chomp($dety = `$UTIL/read_parfile fstatistic.par mean`);
`$UTIL/ftool_test fstatistic $code $0 2 stdout.log stderr.log`;

unlink "$merged";

#########################
# Echo results to stdout
#########################
print "$detx $dety\n";

exit 0;