- Timestamp:
- May 3, 2010, 8:45:22 AM (16 years ago)
- Location:
- branches/simmosaic_branches
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippScripts/scripts/magic_destreak.pl (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/simmosaic_branches
- Property svn:mergeinfo changed
-
branches/simmosaic_branches/ippScripts/scripts/magic_destreak.pl
r24726 r27839 8 8 use Sys::Hostname; 9 9 my $host = hostname(); 10 my $date = `date`; 10 11 print "\n\n"; 11 print "Starting script $0 on $host \n\n";12 print "Starting script $0 on $host at $date\n\n"; 12 13 13 14 use vars qw( $VERSION ); … … 15 16 16 17 use IPC::Cmd 0.36 qw( can_run run ); 17 use File::Temp qw( tempfile );18 use File::Temp qw( tempfile tempdir ); 18 19 use File::Basename qw( basename dirname ); 19 20 use PS::IPP::Metadata::Config; … … 32 33 my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1); 33 34 my $censorObjects = can_run('censorObjects') or (warn "Can't find censorObjects" and $missing_tools = 1); 35 my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1); 36 my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1); 34 37 if ($missing_tools) { 35 38 warn("Can't find required tools."); … … 38 41 39 42 # Parse the command-line arguments 40 my ($magic_ds_id, $camera, $streaks, $ stage, $stage_id, $component, $uri, $path_base, $inverse, $cam_path_base);43 my ($magic_ds_id, $camera, $streaks, $inv_streaks, $exp_id, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base, $cam_reduction); 41 44 my ($outroot, $recoveryroot); 42 45 my ($replace, $release); … … 47 50 'camera=s' => \$camera, # camera for evaluating file rules 48 51 'streaks=s' => \$streaks, # file containing the list of streaks 52 'inv_streaks=s' => \$inv_streaks,# file containing the list of streaks from the inverse diff 53 'exp_id=s' => \$exp_id, # exp_id, chip_id, warp_id, or diff_id 49 54 'stage=s' => \$stage, # raw, chip, warp, or diff 50 55 'stage_id=s' => \$stage_id, # exp_id, chip_id, warp_id, or diff_id … … 52 57 'uri=s' => \$uri, # uri of the input image 53 58 'path_base=s' => \$path_base, # path_base of the input 54 'inverse' => \$inverse, # Inverse subtraction?55 59 'cam_path_base=s'=> \$cam_path_base, # path_base from camera stage (for chip and raw) 60 'cam_reduction=s'=> \$cam_reduction, # reduction class from camera stage (for chip and raw) 56 61 'outroot=s' => \$outroot, # "directory" for temporary images (may be nebulous) 57 62 'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels … … 74 79 defined $stage and 75 80 defined $stage_id and 81 defined $exp_id and 76 82 defined $component and 77 83 defined $uri and … … 81 87 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR ); # IPP configuration 82 88 $ipprc->redirect_output($logfile) or my_die( "Unable to redirect output", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR ) if $logfile; 89 90 $cam_reduction = 'DEFAULT' if !$cam_reduction or ($cam_reduction eq 'NULL'); 91 92 my $recipe_psastro = $ipprc->reduction($cam_reduction, 'PSASTRO'); # Recipe to use 93 &my_die("Unrecognised PSASTRO recipe", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro; 83 94 84 95 my ($skycell_args, $class_id, $skycell_id); … … 95 106 &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR); 96 107 } 108 $inv_streaks = undef if defined($inv_streaks) and ($inv_streaks eq "NULL"); 97 109 98 110 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files … … 110 122 if ($stage eq 'camera') { 111 123 my $nebulousServer = metadataLookupStr( $ipprc->{_siteConfig}, 'NEB_SERVER' ); 112 &my_die("cannot find NEB_SERVER in site configuration", 113 $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) 124 &my_die("cannot find NEB_SERVER in site configuration", 125 $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) 114 126 if !$ nebulousServer; 115 127 … … 144 156 } 145 157 } 158 159 my $statsFile = "$outroot/$exp_id.mds.$magic_ds_id.$stage_id.$component.stats"; 146 160 147 161 my $backup_path_base; … … 165 179 if (! -e $recoveryroot ) { 166 180 my $code = system "mkdir -p $recoveryroot"; 167 &my_die("cannot create output directory $ outroot", $magic_ds_id, $component,181 &my_die("cannot create output directory $recoveryroot", $magic_ds_id, $component, 168 182 $code >> 8) if $code; 169 183 } … … 172 186 $recovery_path_base = "$recoveryroot/$basename/"; 173 187 } 188 189 my $temp_dir; 174 190 175 191 if ($stage ne "camera") { … … 194 210 &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR); 195 211 212 $temp_dir = tempdir( CLEANUP => !$save_temps); 196 213 ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps); 197 214 198 215 foreach my $skycell (@$skycells) { 199 my $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base}); 216 my $skycell_uri; 217 if ($skycell->{data_state} eq "full") { 218 $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base}); 219 } else { 220 # diff run must have been cleaned up, need to create this skycell file on the fly 221 my $skycell_id = $skycell->{skycell_id}; 222 $skycell_uri = "$temp_dir/$skycell_id"; 223 $ipprc->skycell_file($skycell->{tess_id}, $skycell_id, $skycell_uri, $verbose) or 224 &my_die("failed to create skycell file for $skycell_id", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR); 225 } 200 226 print $sfh "$skycell_uri\n"; 201 227 } 202 close $sfh ;228 close $sfh 203 229 } 204 230 } 205 231 206 232 my ($image, $mask, $ch_mask, $weight, $astrom, $sources); 233 234 # if we're destreaking a bothways diff need to combine the 235 # two streaks files 236 my ($allstreaks_fh, $allstreaks_name); 207 237 208 238 if ($stage eq "raw") { … … 211 241 $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ; 212 242 } elsif ($stage eq "chip") { 243 244 # Check to see if we're using dynamic masks 245 my $dynamicMasks; # Use dynamic masks? 246 { 247 # Get the PSASTRO recipe 248 my $command = "$ppConfigDump -camera $camera -recipe PSASTRO $recipe_psastro -dump-recipe PSASTRO -"; 249 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 250 run(command => $command, verbose => $verbose); 251 unless ($success) { 252 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 253 &my_die("Unable to perform ppConfigDump: $error_code", $magic_ds_id, $component, 254 $PS_EXIT_CONFIG_ERROR); 255 } 256 my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or 257 &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, 258 $PS_EXIT_CONFIG_ERROR); 259 260 $dynamicMasks = metadataLookupBool($recipeData, 'REFSTAR_MASK'); 261 } 262 213 263 # we use the mask output from the camera stage for input and replace 214 264 # the output of the chip stage with that mask as well. 215 265 $image = $ipprc->filename("PPIMAGE.CHIP", $path_base, $class_id); 216 $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);217 $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);218 266 $weight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $path_base, $class_id); 219 267 $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base); 220 if (!$ipprc->file_exists($mask)) { 221 carp("camera mask file $mask for $component not found. Continuing using mask from chip stage."); 222 $mask = $ch_mask; 223 $ch_mask = undef; 224 } 268 269 if ($dynamicMasks) { 270 $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id); 271 $ch_mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id); 272 } else { 273 $mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id); 274 } 275 225 276 # XXX: should we censor the sources as well? For now we're leaving them out of the distribution bundles 226 277 # because they confuse people … … 234 285 $sources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $path_base); 235 286 } elsif ($stage eq "diff") { 236 my $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images 237 $image = $ipprc->filename($name, $path_base); 238 $mask = $ipprc->filename("$name.MASK", $path_base); 239 $weight = $ipprc->filename("$name.VARIANCE", $path_base); 287 $image = $ipprc->filename("PPSUB.OUTPUT", $path_base); 288 $mask = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base); 289 $weight = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $path_base); 240 290 $sources = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $path_base); 291 292 if ($inv_streaks) { 293 # create a temporary file containing the contents of the 294 # two streaks files 295 ($allstreaks_fh, $allstreaks_name) = tempfile ("/tmp/all.streaks.XXXX", 296 UNLINK => !$save_temps); 297 298 combine_streaks($allstreaks_fh, $streaks, $inv_streaks); 299 300 # apply the combined streaks to both the forward and inverse diffs 301 $streaks = $allstreaks_name; 302 } 241 303 } 242 304 … … 244 306 my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image"; 245 307 308 $command .= " -stats $statsFile"; 246 309 $command .= " -class_id $class_id" if defined $class_id; 247 310 $command .= " -recovery $recoveryroot" if defined $recoveryroot; … … 266 329 } 267 330 } 331 if (($stage eq "diff") and $inv_streaks) { 332 $image = $ipprc->filename("PPSUB.INVERSE", $path_base); 333 $mask = $ipprc->filename("PPSUB.INVERSE.MASK", $path_base); 334 $weight = $ipprc->filename("PPSUB.INVERSE.VARIANCE", $path_base); 335 $sources = $ipprc->filename("PPSUB.INVERSE.SOURCES", $path_base); 336 337 # Note: we create a stats file for the inverse procesing but we don't look at the results 338 my $invStatsFile = "$outroot/$exp_id.mds.$magic_ds_id.$stage_id.$component.inv.stats"; 339 340 my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image"; 341 $command .= " -stats $invStatsFile"; 342 343 $command .= " -recovery $recoveryroot" if defined $recoveryroot; 344 $command .= " -mask $mask" if defined $mask; 345 $command .= " -weight $weight" if defined $weight; 346 $command .= " -sources $sources" if defined $sources; 347 $command .= " -replace" if $replace; 348 $command .= " -release" if $release; 349 $command .= " -dbname $dbname" if defined $dbname; 350 unless (defined $no_op) { 351 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 352 run(command => $command, verbose => $verbose); 353 unless ($success) { 354 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 355 &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code); 356 } 357 } else { 358 print "skipping command $command\n"; 359 } 360 } 268 361 } else { 269 362 # camera stage. The only work to do is to censor the detections file … … 293 386 my $quality = $file->{quality}; 294 387 if (!$quality) { 295 my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 388 my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 296 389 if (! $ipprc->file_exists($mask)) { 297 390 # camera mask doesn't exist for this chip. Fall back to the chip mask … … 315 408 &my_die("Unable to perform censorObjects: $error_code", $magic_ds_id, $component, $error_code); 316 409 } 317 my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base); 410 my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base); 318 411 &my_die("expected output file $output not found ", $magic_ds_id, $component, $PS_EXIT_DATA_ERROR) 319 412 unless $ipprc->file_exists($output); … … 329 422 } 330 423 331 # if recovery and/or backup files were expected make sure they exist 424 my $statsFlags; 425 if ($stage ne "camera") { 426 file_check($statsFile); 427 { 428 my $resolvedStatsFile = $ipprc->file_resolve($statsFile); 429 my $command = "$ppStatsFromMetadata $resolvedStatsFile - STREAKSREMOVE"; 430 431 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 432 run(command => $command, verbose => $verbose); 433 unless ($success) { 434 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 435 &my_die("Unable to perform ppStatsFromMetadata: $error_code", $magic_ds_id, $component, $error_code); 436 } 437 foreach my $line (@$stdout_buf) { 438 $statsFlags .= " $line"; 439 } 440 chomp $statsFlags; 441 } 442 } 443 444 # XXX: if recovery and/or backup files were expected make sure they exist 332 445 333 446 # Input result into database … … 339 452 $command .= " -backup_path_base $backup_path_base" if $backup_path_base; 340 453 $command .= " -recovery_path_base $recovery_path_base" if $recovery_path_base; 454 $command .= " $statsFlags" if $statsFlags; 341 455 $command .= " -dbname $dbname" if defined $dbname; 342 456 … … 376 490 } 377 491 492 sub combine_streaks 493 { 494 my $fout = shift; 495 my $fn1 = shift; 496 my $fn2 = shift; 497 498 my ($n1, @streaks1) = read_streaks_file($fn1); 499 my_die("failed to read streaks from $fn1", $magic_ds_id, $component, 500 $PS_EXIT_UNKNOWN_ERROR) if $n1 < 0; 501 502 my ($n2, @streaks2) = read_streaks_file($fn2); 503 my_die("failed to read streaks from $fn2", $magic_ds_id, $component, 504 $PS_EXIT_UNKNOWN_ERROR) if $n2 < 0; 505 506 print $fout $n1 + $n2 . "\n"; 507 508 foreach my $line (@streaks1, @streaks2) { 509 print $fout $line; 510 } 511 512 close $fout 513 or my_die("failed to close combined streaks file", $magic_ds_id, 514 $component, $PS_EXIT_UNKNOWN_ERROR); 515 } 516 517 sub read_streaks_file 518 { 519 my $filename = shift; 520 my $fh; 521 open $fh, "<$filename" or my_die("failed to open $filename", 522 $magic_ds_id, $component, $PS_EXIT_UNKNOWN_ERROR); 523 524 # first line is the number of streaks 525 my $line = <$fh>; 526 chomp $line; 527 my $nstreaks = $line; 528 529 my @streaks; 530 531 foreach $line (<$fh>) { 532 push @streaks, $line; 533 } 534 535 close $fh; 536 537 return ($nstreaks, @streaks); 538 } 539 378 540 379 541 sub my_die
Note:
See TracChangeset
for help on using the changeset viewer.
