Changeset 18410 for trunk/ippScripts/scripts
- Timestamp:
- Jul 2, 2008, 5:53:22 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/ippScripts/scripts/magic_tree.pl (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/magic_tree.pl
r17671 r18410 30 30 use Pod::Usage qw( pod2usage ); 31 31 32 use constant MAX_FIELDS => 4; # Maximum number of fields to be in a node32 use constant MAX_FIELDS => 4; # Maximum number of fields to be in a node 33 33 34 34 # Parse the command-line arguments 35 35 my ($magic_id, $tess_id, $camera, $ra0, $dec0, $dbname, $outroot, $save_temps, $verbose, $no_update, $no_op); 36 36 GetOptions( 37 'magic_id=s' => \$magic_id, # Magic identifier38 'tess_id=s' => \$tess_id, # Tessellation identifier39 'camera=s' => \$camera,# Camera name40 'ra=f' => \$ra0, # Boresight right ascension, radians41 'dec=f' => \$dec0, # Boresight declination, radians42 'dbname=s' => \$dbname, # Database name43 'outroot=s' => \$outroot, # Output root name44 'save-temps' => \$save_temps, # Save temporary files?45 'verbose' => \$verbose, # Print stuff?46 'no-update' => \$no_update, # Don't update the database?47 'no-op' => \$no_op,# Don't do any operations?48 ) or pod2usage( 2 );37 'magic_id=s' => \$magic_id, # Magic identifier 38 'tess_id=s' => \$tess_id, # Tessellation identifier 39 'camera=s' => \$camera, # Camera name 40 'ra=f' => \$ra0, # Boresight right ascension, radians 41 'dec=f' => \$dec0, # Boresight declination, radians 42 'dbname=s' => \$dbname, # Database name 43 'outroot=s' => \$outroot, # Output root name 44 'save-temps' => \$save_temps, # Save temporary files? 45 'verbose' => \$verbose, # Print stuff? 46 'no-update' => \$no_update, # Don't update the database? 47 'no-op' => \$no_op, # Don't do any operations? 48 ) or pod2usage( 2 ); 49 49 50 50 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV; 51 51 pod2usage( -msg => "Required options: --magic_id --camera --outroot", 52 -exitval => 3) unless52 -exitval => 3) unless 53 53 defined $magic_id and 54 54 defined $tess_id and … … 65 65 my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1); 66 66 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1); 67 if ($missing_tools) { 67 if ($missing_tools) { 68 68 warn("Can't find required tools."); 69 exit($PS_EXIT_CONFIG_ERROR); 70 } 71 72 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files69 exit($PS_EXIT_CONFIG_ERROR); 70 } 71 72 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files 73 73 74 74 ### Get a list of skycells 75 my @skycells; # List of skycells75 my @skycells; # List of skycells 76 76 { 77 77 my $command = "$magictool -inputskyfile -magic_id $magic_id"; # Command to run 78 78 $command .= " -dbname $dbname" if defined $dbname; 79 79 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 80 run(command => $command, verbose => $verbose);80 run(command => $command, verbose => $verbose); 81 81 unless ($success) { 82 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);83 &my_die("Unable to perform magictool -inputfile: $error_code", $magic_id, $error_code);82 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 83 &my_die("Unable to perform magictool -inputfile: $error_code", $magic_id, $error_code); 84 84 } 85 85 86 86 my $metadata = $mdcParser->parse(join "", @$stdout_buf) or 87 &my_die("Unable to parse metadata config doc", $magic_id, $PS_EXIT_PROG_ERROR);87 &my_die("Unable to parse metadata config doc", $magic_id, $PS_EXIT_PROG_ERROR); 88 88 89 89 my $inputs = parse_md_list($metadata) or 90 &my_die("Unable to parse metadata list", $magic_id, $PS_EXIT_PROG_ERROR);90 &my_die("Unable to parse metadata list", $magic_id, $PS_EXIT_PROG_ERROR); 91 91 92 92 foreach my $input ( @$inputs ) { 93 push @skycells, $input->{skycell_id};93 push @skycells, $input->{skycell_id}; 94 94 } 95 95 } … … 99 99 foreach my $skycell_id ( @skycells ) { 100 100 my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $outroot, $skycell_id ); 101 $ipprc->skycell_file($tess_id, $skycell_id, $sky File, $verbose) or &my_die("Unable to generate skycells $skycell_id", $magic_id, $PS_EXIT_PROG_ERROR);101 $ipprc->skycell_file($tess_id, $skycell_id, $skyfile, $verbose) or &my_die("Unable to generate skycells $skycell_id", $magic_id, $PS_EXIT_PROG_ERROR); 102 102 my $skyfileResolved = $ipprc->file_resolve( $skyfile ); 103 103 my ($header, $status) = Astro::FITS::CFITSIO::fits_read_header( $skyfileResolved ); 104 104 &my_die("Unable to read skycell header: $status", $magic_id, $PS_EXIT_SYS_ERROR) if $status; 105 105 106 106 # Get the useful header keywords 107 107 my $naxis1 = $$header{'NAXIS1'} or &my_die("Can't find NAXIS1", $magic_id, $PS_EXIT_SYS_ERROR); … … 123 123 124 124 &my_die("Unexpected projection: $ctype1 and $ctype2.", $magic_id, $PS_EXIT_SYS_ERROR) unless 125 $ctype1 =~ /^\'RA---TAN\s*\'$/ and $ctype2 =~ /^\'DEC--TAN\s*\'$/;125 $ctype1 =~ /^\'RA---TAN\s*\'$/ and $ctype2 =~ /^\'DEC--TAN\s*\'$/; 126 126 &my_die("Can't determine size of skycell ($naxis1,$naxis2)", $magic_id, $PS_EXIT_SYS_ERROR) unless 127 $naxis1 > 0 and $naxis2 > 0;127 $naxis1 > 0 and $naxis2 > 0; 128 128 &my_die("Can't determine scale of skycell ($cdelt1,$cdelt2)", $magic_id, $PS_EXIT_SYS_ERROR) if 129 not defined $cdelt1 or $cdelt1 == 0 or not defined $cdelt2 or $cdelt2 == 0;129 not defined $cdelt1 or $cdelt1 == 0 or not defined $cdelt2 or $cdelt2 == 0; 130 130 &my_die("We don't know how to handle rotations ($crota1,$crota2)", $magic_id, $PS_EXIT_SYS_ERROR) 131 if defined $crota1 or defined $crota2;131 if defined $crota1 or defined $crota2; 132 132 133 133 # Relative coordinates of centre of the field … … 140 140 $xi *= $cdelt1; 141 141 $eta *= $cdelt2; 142 142 143 143 # Coordinates on rotated celestial sphere 144 144 my $phi = atan2($eta,$xi) + pi/2; 145 145 my $theta = atan(180 / pi / sqrt($xi**2 + $eta**2)); 146 146 147 147 # Coordinates on celestial sphere 148 148 $crval1 = deg2rad($crval1); 149 149 $crval2 = deg2rad($crval2); 150 150 my $ra = $crval1 + atan2(cos($theta) * sin($phi), 151 sin($theta) * cos($crval2) + cos($theta) * sin($crval2) * cos($phi));151 sin($theta) * cos($crval2) + cos($theta) * sin($crval2) * cos($phi)); 152 152 my $dec = asin(sin($theta) * sin($crval2) - cos($theta) * cos($crval2) * cos($phi)); 153 153 154 154 # Rotate to boresight 155 my $phi_new = atan2(cos($dec) * sin($ra - $ra0), 156 sin($dec) * cos($dec0) + cos($dec) * sin($dec0) * cos($ra - $ra0));155 my $phi_new = atan2(cos($dec) * sin($ra - $ra0), 156 sin($dec) * cos($dec0) + cos($dec) * sin($dec0) * cos($ra - $ra0)); 157 157 my $theta_new = asin(sin($dec) * sin($dec0) - cos($dec) * cos($dec0) * cos($ra - $ra0)); 158 158 … … 163 163 164 164 my $field = { id => $skycell_id, 165 xi => $xi_new,166 eta => $eta_new,167 };165 xi => $xi_new, 166 eta => $eta_new, 167 }; 168 168 169 169 push @fields, $field; … … 171 171 172 172 ### Subdivide list of positions into kd-tree 173 my $root = { # Root node of tree174 contents => \@fields, # Contents of node175 position => 'root', # Position in tree176 children => {}, # Children of node173 my $root = { # Root node of tree 174 contents => \@fields, # Contents of node 175 position => 'root', # Position in tree 176 children => {}, # Children of node 177 177 }; 178 178 my @tasks = ( $root ); … … 197 197 # Add the processed file to the database 198 198 unless ($no_update) { 199 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =200 run(command => $command, verbose => $verbose);201 unless ($success) {202 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);203 warn("Unable to perform magictool -inputtree: $error_code");204 exit($error_code);205 }199 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 200 run(command => $command, verbose => $verbose); 201 unless ($success) { 202 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 203 warn("Unable to perform magictool -inputtree: $error_code"); 204 exit($error_code); 205 } 206 206 } else { 207 print "Skipping command: $command\n";207 print "Skipping command: $command\n"; 208 208 } 209 209 } … … 213 213 sub my_die 214 214 { 215 my $msg = shift; # Warning message on die216 my $magic_id = shift; # Magic identifier217 my $exit_code = shift; # Exit code to add215 my $msg = shift; # Warning message on die 216 my $magic_id = shift; # Magic identifier 217 my $exit_code = shift; # Exit code to add 218 218 219 219 carp($msg); 220 220 if (defined $magic_id and not $no_update) { 221 my $command = "$magictool -inputtree";222 $command .= " -magic_id $magic_id";223 $command .= " -code $exit_code";224 $command .= " -dbname $dbname" if defined $dbname;225 system($command);221 my $command = "$magictool -inputtree"; 222 $command .= " -magic_id $magic_id"; 223 $command .= " -code $exit_code"; 224 $command .= " -dbname $dbname" if defined $dbname; 225 system($command); 226 226 } 227 227 exit $exit_code; … … 231 231 sub divide_list 232 232 { 233 my $list = shift; # List to divide234 my $index = shift; # Name of index for sorting233 my $list = shift; # List to divide 234 my $index = shift; # Name of index for sorting 235 235 236 236 my @sorted = sort { $$a{$index} <=> $$b{$index} } @$list; # Sorted list … … 244 244 sub new_node 245 245 { 246 my $parent = shift; # The parent node247 my $contents = shift; # Contents of the new node248 my $position = shift; # Position description249 my $tasks = shift; # Tasks to do246 my $parent = shift; # The parent node 247 my $contents = shift; # Contents of the new node 248 my $position = shift; # Position description 249 my $tasks = shift; # Tasks to do 250 250 251 251 my $node = { 252 contents => $contents,253 position => $parent->{position} . '_' . $position,254 children => {},252 contents => $contents, 253 position => $parent->{position} . '_' . $position, 254 children => {}, 255 255 }; 256 256 … … 265 265 sub divide_node 266 266 { 267 my $node = shift; # Node to divide268 my $tasks = shift; # Tasks to do267 my $node = shift; # Node to divide 268 my $tasks = shift; # Tasks to do 269 269 270 270 my $position = $node->{position}; … … 275 275 276 276 if (scalar @$lower > 4) { 277 my ($ll, $lr) = divide_list($lower, 'eta');278 new_node($node, $ll, 'll', $tasks);279 new_node($node, $lr, 'lr', $tasks);277 my ($ll, $lr) = divide_list($lower, 'eta'); 278 new_node($node, $ll, 'll', $tasks); 279 new_node($node, $lr, 'lr', $tasks); 280 280 } else { 281 new_node($node, $lower, 'L', $tasks);281 new_node($node, $lower, 'L', $tasks); 282 282 } 283 283 284 284 if (scalar @$upper > 4) { 285 my ($ul, $ur) = divide_list($upper, 'eta');286 new_node($node, $ul, 'ul', $tasks);287 new_node($node, $ur, 'ur', $tasks);285 my ($ul, $ur) = divide_list($upper, 'eta'); 286 new_node($node, $ul, 'ul', $tasks); 287 new_node($node, $ur, 'ur', $tasks); 288 288 } else { 289 new_node($node, $upper, 'U', $tasks);289 new_node($node, $upper, 'U', $tasks); 290 290 } 291 291 … … 298 298 sub print_node 299 299 { 300 my $node = shift; # Node to print300 my $node = shift; # Node to print 301 301 302 302 my $position = $node->{position}; # Position of node … … 305 305 306 306 if (defined $node->{contents}) { 307 foreach my $field ( @{$node->{contents}} ) {308 my $skycell_id = $field->{id};# Skycell name309 $output .= "$position\t\tSTR\t$skycell_id\n";310 $output .= "$skycell_id\t\tSTR\tNULL\t\# $field->{xi},$field->{eta}\n";311 }307 foreach my $field ( @{$node->{contents}} ) { 308 my $skycell_id = $field->{id}; # Skycell name 309 $output .= "$position\t\tSTR\t$skycell_id\n"; 310 $output .= "$skycell_id\t\tSTR\tNULL\t\# $field->{xi},$field->{eta}\n"; 311 } 312 312 } else { 313 foreach my $div ( keys %{$node->{children}} ) {314 $output .= "$position\t\tSTR\t${position}_$div\n";315 $output .= print_node($node->{children}->{$div});316 }313 foreach my $div ( keys %{$node->{children}} ) { 314 $output .= "$position\t\tSTR\t${position}_$div\n"; 315 $output .= print_node($node->{children}->{$div}); 316 } 317 317 } 318 318
Note:
See TracChangeset
for help on using the changeset viewer.
