Correlate/Method/FINDOFF.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 276 CC 29 SCAR 64.2
CC
29
Cov
0%
correlate
870.0
BEGIN
2.0
BEGIN
2.0
CRAP 616.0
1 package Astro::Correlate::Method::FINDOFF;
2  
3 =head1 NAME
4  
5 Astro::Correlate::Method::FINDOFF - Correlation using Starlink FINDOFF.
6  
7 =head1 SYNOPSIS
8  
9 ( $corrcat1, $corrcat2 ) = Astro::Correlate::Method::FINDOFF->correlate( catalog1 => $cat1, catalog2 => $cat2 );
10  
11 =head1 DESCRIPTION
12  
13 This class implements catalogue cross-correlation using Starlink's FINDOFF
14 application.
15  
16 =cut
17  
18 use 5.006;
19 use strict;
20 use warnings;
21 use warnings::register;
22 use Carp;
23 use File::Temp qw/ tempfile /;
24 use Storable qw/ dclone /;
25  
26 use Starlink::ADAM;
27 use Starlink::AMS::Init;
28 use Starlink::AMS::Task;
29 use Starlink::Config qw/ :override /;
30 use Starlink::EMS qw/ :sai get_facility_error /;
31  
32 our $VERSION = '0.01';
33 our $DEBUG = 0;
34  
35 # Cache the task.
36 our $TASK;
37  
38 =head1 METHODS
39  
40 =head2 General Methods
41  
42 =over 4
43  
44 =item B<correlate>
45  
46 Cross-correlates two catalogues.
47  
48 ( $corrcat1, $corrcat2 ) = Astro::Correlate::Method::FINDOFF->correlate( catalog1 => $cat1,
49 catalog2 => $cat2 );
50  
51 This method takes two mandatory arguments, both C<Astro::Catalog> objects.
52 It returns two C<Astro::Catalog> objects containing C<Astro::Catalog::Star>
53 objects that matched spatially between the two input catalogues. The
54 first returned catalogue contains matched objects from the first input
55 catalogue, and ditto for the second. The C<Astro::Catalog::Star> objects
56 in the returned catalogues are not in the original order, nor do they have
57 the same IDs as in the input catalogues. A matched object has the same ID
58 in the two returned catalogues, allowing for further comparisons between
59 matched objects.
60  
61 This method takes the following optional named arguments:
62  
63 =item keeptemps - If this argument is set to true (1), then this
64 method will keep temporary files used in processing. Defaults to
65 false.
66  
67 =item messages - If set to true (1), then this method will print
68 messages from the FINDOFF task during processing. Defaults to false.
69  
70 =item temp - Set the directory to hold temporary files. If not set,
71 then a new temporary directory will be created using File::Temp.
72  
73 =item timeout - Set the time in seconds to wait for the CCDPACK
74 monolith to time out. Defaults to 60 seconds.
75  
76 =item verbose - If this argument is set to true (1), then this method will
77 print progress statements. Defaults to false.
78  
79 This method uses the Starlink FINDOFF task, which is part of
80 CCDPACK. In order for this method to work it must be able to find
81 FINDOFF. It first looks in the directory pointed to by the CCDPACK_DIR
82 environment variable, then it looks in the Starlink binary files
83 directory pointed to by the Starlink::Config module, with C</ccdpack>
84 appended. If either of these fail, then this method will croak. See
85 the C<Starlink::Config> module for information on overriding the base
86 Starlink directory for non-standard installations.
87  
88 =cut
89  
90 sub correlate {
91 my $class = shift;
92  
93 # Grab the arguments, and make sure they're defined and are
94 # Astro::Catalog objects (the catalogues, at least).
95 my %args = @_;
96 my $inputcat1 = dclone( $args{'catalog1'} );
97 my $inputcat2 = dclone( $args{'catalog2'} );
98  
99 if( ! defined( $inputcat1 ) ||
100 ! UNIVERSAL::isa( $inputcat1, "Astro::Catalog" ) ) {
101 croak "catalog1 parameter to correlate method must be defined and must be an Astro::Catalog object";
102 }
103 if( ! defined( $inputcat2 ) ||
104 ! UNIVERSAL::isa( $inputcat2, "Astro::Catalog" ) ) {
105 croak "catalog2 parameter to correlate method must be defined and must be an Astro::Catalog object";
106 }
107  
108 # Make deep clones of the two input catalogues so we can modify IDs
109 # and not trample those input catalogues.
110 my $cat1 = dclone( $inputcat1 );
111 my $cat2 = dclone( $inputcat2 );
112  
113 my $keeptemps = defined( $args{'keeptemps'} ) ? $args{'keeptemps'} : 0;
114 my $temp;
115 if( exists( $args{'temp'} ) && defined( $args{'temp'} ) ) {
116 $temp = $args{'temp'};
117 } else {
118 $temp = tempdir( UNLINK => ! $keeptemps );
119 }
120 my $verbose = defined( $args{'verbose'} ) ? $args{'verbose'} : 0;
121 my $messages = defined( $args{'messages'} ) ? $args{'messages'} : 0;
122 my $timeout = defined( $args{'timeout'} ) ? $args{'timeout'} : 60;
123  
124 # Try to find the FINDOFF binary. First, try the CCDPACK_DIR
125 # environment variable. If that doesn't find it, use
126 # Starlink::Config. If that doesn't work, croak.
127 my $findoff_bin;
128 if( defined( $ENV{'CCDPACK_DIR'} ) &&
129 -d $ENV{'CCDPACK_DIR'} &&
130 -e File::Spec->catfile( $ENV{'CCDPACK_DIR'}, "findoff" ) ) {
131 $findoff_bin = File::Spec->catfile( $ENV{'CCDPACK_DIR'}, "findoff" );
132 } elsif( -d File::Spec->catfile( $StarConfig{Star_Bin}, "ccdpack" ) &&
133 -e File::Spec->catfile( $StarConfig{Star_Bin}, "ccdpack", "findoff" ) ) {
134 $findoff_bin = File::Spec->catfile( $StarConfig{Star_Bin}, "ccdpack", "findoff" );
135 } else {
136 croak "Could not find FINDOFF binary.\n";
137 }
138 print "FINDOFF binary is in $findoff_bin\n" if $DEBUG;
139  
140 # Get two temporary file names for catalog files.
141 ( undef, my $catfile1 ) = tempfile( DIR => $temp );
142 ( undef, my $catfile2 ) = tempfile( DIR => $temp );
143  
144 # Create two hash lookup tables. Key will be an integer incrementing
145 # from 1, value will be the original ID. We have to renumber because
146 # some modern catalogues have star IDs where the integer part exceeds
147 # a 32-bit integer (as used by FINDOFF)
148 my %lookup_cat1;
149 my %lookup_cat2;
150  
151 my $cat1stars = $cat1->stars;
152 my $newid = 1;
153 foreach my $cat1star ( @$cat1stars ) {
154 $lookup_cat1{$newid} = $cat1star->id;
155 print "Catalogue 1 star with original ID of " . $cat1star->id . " has FINDOFF-ed ID of $newid\n" if $DEBUG;
156 $cat1star->id( $newid );
157 $newid++;
158 }
159  
160 my $cat2stars = $cat2->stars;
161 $newid = 1;
162 foreach my $cat2star ( @$cat2stars ) {
163 $lookup_cat2{$newid} = $cat2star->id;
164 print "Catalogue 2 star with original ID of " . $cat2star->id . " has FINDOFF-ed ID of $newid\n" if $DEBUG;
165 $cat2star->id( $newid );
166 $newid++;
167 }
168  
169 # We need to write two input files for FINDOFF, one for each catalogue.
170 # Do so using Astro::Catalog.
171 $cat1->write_catalog( Format => 'FINDOFF', File => $catfile1 );
172 $cat2->write_catalog( Format => 'FINDOFF', File => $catfile2 );
173 print "Input catalog 1 written to $catfile1.\n" if $DEBUG;
174 print "Input catalog 2 written to $catfile2.\n" if $DEBUG;
175  
176 # We need to write an input file for FINDOFF that lists the above two
177 # input files.
178 ( my $findoff_fh, my $findoff_input ) = tempfile( DIR => $temp, UNLINK => 1 );
179 print $findoff_fh "$catfile1\n$catfile2\n";
180 close $findoff_fh;
181  
182 # Set up the parameter list for FINDOFF.
183 my $param = "ndfnames=false error=5 maxdisp=! minsep=5 fast=yes failsafe=yes";
184 $param .= " logto=terminal namelist=! complete=0.15";
185 $param .= " inlist=^$findoff_input outlist='*.off'";
186  
187 # Do the correlation.
188 local $ENV{'ADAM_DIR'} = ( defined( $ENV{'ADAM_DIR'} ) ?
189 $ENV{'ADAM_DIR'} . "/corr" :
190 $ENV{'HOME'} . "/adam/corr" );
191  
192 my @findoffargs = ( "ndfnames=false",
193 "error=2",
194 "maxdisp=!",
195 "minsep=5",
196 "fast=yes",
197 "failsafe=yes",
198 "logto=neither",
199 "namelist=!",
200 "complete=0.05",
201 "inlist=^$findoff_input",
202 "outlist='*.off'" );
203  
204 my $ams = new Starlink::AMS::Init(1);
205 $ams->timeout( $timeout );
206 my $set_messages = $ams->messages;
207 if( ! defined( $set_messages ) ) {
208 $ams->messages( $messages );
209 }
210 if( ! defined( $TASK ) ) {
211 $TASK = new Starlink::AMS::Task( "findoff", "$findoff_bin" );
212 }
213 my $STATUS = $TASK->contactw;
214 if( ! $STATUS ) {
215 croak "Could not contact FINDOFF monolith";
216 }
217 $STATUS = $TASK->obeyw("findoff", join( " ", @findoffargs ) );
218 if( $STATUS != SAI__OK && $STATUS != &Starlink::ADAM::DTASK__ACTCOMPLETE ) {
219 ( my $facility, my $ident, my $text ) = get_facility_error( $STATUS );
220 croak "Error in running FINDOFF: $STATUS - $text";
221 }
222  
223 # Read in the first output catalog. If it doesn't exist, croak because
224 # FINDOFF has failed to find a correlation.
225 my $outfile1 = $catfile1 . ".off";
226 if( ! -e $outfile1 ) {
227 croak "FINDOFF failed to find a correlation between the two input catalogues";
228 }
229 my $tempcat = new Astro::Catalog( Format => 'FINDOFF',
230 File => $outfile1 );
231 # Loop through the stars, making a new catalogue with new stars using
232 # a combination of the new ID and the old information.
233 my $corrcat1 = new Astro::Catalog();
234 my @stars = $tempcat->stars;
235 foreach my $star ( @stars ) {
236  
237 # The old ID is found in the first column of the star's comment.
238 # However, this old ID has been "FINDOFF-ed", i.e. all non-numeric
239 # characters have been stripped from it. Use the lookup table we
240 # generated earlier to find the proper old ID.
241 $star->comment =~ /^(\w+)/;
242 my $oldfindoffid = $1;
243 my $oldid = $lookup_cat1{$oldfindoffid};
244  
245 # Get the star's information.
246 my $oldstar = $inputcat1->popstarbyid( $oldid );
247 $oldstar = $oldstar->[0];
248 next if ! defined( $oldstar );
249  
250 # Set the ID to the new star's ID.
251 $oldstar->id( $star->id );
252  
253 # Set the comment denoting the old ID.
254 $oldstar->comment( "Old ID: " . $oldid );
255  
256 # And push this star onto the output catalogue.
257 $corrcat1->pushstar( $oldstar );
258 }
259  
260 # Do the same for the second catalogue.
261 my $outfile2 = $catfile2 . ".off";
262 if( ! -e $outfile2 ) {
263 croak "FINDOFF failed to find a correlation between the two input catalogues";
264 }
265 $tempcat = new Astro::Catalog( Format => 'FINDOFF',
266 File => $outfile2 );
267  
268 # Loop through the stars, making a new catalogue with new stars using
269 # a combination of the new ID and the old information.
270 my $corrcat2 = new Astro::Catalog();
271 @stars = $tempcat->stars;
272  
273 foreach my $star ( @stars ) {
274  
275 # The old ID is found in the first column of the star's comment.
276 # However, this old ID has been "FINDOFF-ed", i.e. all non-numeric
277 # characters have been stripped from it. Use the lookup table we
278 # generated earlier to find the proper old ID.
279 $star->comment =~ /^(\w+)/;
280 my $oldfindoffid = $1;
281 my $oldid = $lookup_cat2{$oldfindoffid};
282  
283 # Get the star's information.
284 my $oldstar = $inputcat2->popstarbyid( $oldid );
285 $oldstar = $oldstar->[0];
286 next if ! defined( $oldstar );
287  
288 # Set the ID to the new star's ID.
289 $oldstar->id( $star->id );
290  
291 # Set the comment denoting the old ID.
292 $oldstar->comment( "Old ID: " . $oldid );
293  
294 # And push this star onto the output catalogue.
295 $corrcat2->pushstar( $oldstar );
296 }
297  
298 # Delete the temporary catalogues.
299 unlink $catfile1 unless ( $DEBUG || $keeptemps );
300 unlink $catfile2 unless ( $DEBUG || $keeptemps );
301 unlink $outfile1 unless ( $DEBUG || $keeptemps );
302 unlink $outfile2 unless ( $DEBUG || $keeptemps );
303  
304 unlink $findoff_input unless ( $DEBUG || $keeptemps );
305  
306 return ( $corrcat1, $corrcat2 );
307  
308 }
309  
310 =back
311  
312 =head1 SEE ALSO
313  
314 C<Astro::Correlate>, C<Starlink::Config>
315  
316 Starlink User Note 139.
317  
318 =head1 REVISION
319  
320 $Id$
321  
322 =head1 AUTHORS
323  
324 Brad Cavanagh <brad.cavanagh@gmail.com>
325  
326 =head1 COPYRIGHT
327  
328 Copyright (C) 2005 Particle Physics and Astronomy Research Council.
329 All Rights Reserved.
330  
331 This program is free software; you can redistribute it and/or modify it under
332 the terms of the GNU General Public License as published by the Free Software
333 Foundation; either version 2 of the License, or (at your option) any later
334 version.
335  
336 This program is distributed in the hope that it will be useful,but WITHOUT ANY
337 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
338 PARTICULAR PURPOSE. See the GNU General Public License for more details.
339  
340 You should have received a copy of the GNU General Public License along with
341 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
342 Place,Suite 330, Boston, MA 02111-1307, USA
343  
344 =cut
345  
346 1;