IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27638


Ignore:
Timestamp:
Apr 8, 2010, 10:14:37 AM (16 years ago)
Author:
Paul Price
Message:

Additional checking that path_base is defined (it may not be for some old data).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippScripts/scripts/ipp_cleanup.pl

    r27144 r27638  
    6161
    6262my %stages = ( "chip" => 1, "camera" => 1, "fake" => 1, "warp" => 1, "stack" => 1, "diff"  => 1,
    63                "detrend.processed" => 1, "detrend.resid" => 1, "detrend.process.exp" => 0, "detrend.stack.imfile" => 0,
    64                "detrend.normstat.imfile" => 0, "detrend.norm.imfile" => 0, "detrend.norm.exp" => 0 );
     63               "detrend.processed" => 1, "detrend.resid" => 1, "detrend.process.exp" => 0, "detrend.stack.imfile" => 0,
     64               "detrend.normstat.imfile" => 0, "detrend.norm.imfile" => 0, "detrend.norm.exp" => 0 );
    6565unless (exists($stages{$stage})) {
    6666    die "unknown stage $stage for ipp_cleanup.pl\n";
     
    9696    # XXX Why? This could just mean there's nothing to cleanup, or that we're trying to rerun an errored run.
    9797    if (@$stdout_buf == 0)  {
    98         my $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
    99         $command .= " -dbname $dbname" if defined $dbname;
    100 
    101         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    102             run(command => $command, verbose => $verbose);
    103         unless ($success) {
    104             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    105             &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
    106         }
    107         exit 0;
     98        my $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
     99        $command .= " -dbname $dbname" if defined $dbname;
     100
     101        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     102            run(command => $command, verbose => $verbose);
     103        unless ($success) {
     104            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     105            &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
     106        }
     107        exit 0;
    108108    }
    109109
     
    120120        my $path_base = $imfile->{path_base};
    121121        my $status = 1;
     122        $status = 0 unless defined $path_base and $path_base ne "NULL";
    122123
    123124        # don't clean up unless the data needed to update is available
    124125        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
    125         # goto_scrubbed now requires the config file to not exist.
    126         if ($mode eq "goto_cleaned") {
    127             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
    128 
    129             unless ($ipprc->file_exists($config_file)) {
    130                 print STDERR "skipping cleanup for chipRun $stage_id $class_id "
    131                     . " because config file ($config_file) is missing\n";
    132                 $status = 0;
    133             }
    134         }
    135         elsif ($mode eq "goto_scrubbed") {
    136             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
    137 
    138             if ($ipprc->file_exists($config_file)) {
    139                 print STDERR "skipping scrubbed for chipRun $stage_id $class_id "
    140                     . " because config file ($config_file) is present\n";
    141                 $status = 0;
    142             }
    143         }
     126        # goto_scrubbed now requires the config file to not exist.
     127        if ($status) {
     128            if ($mode eq "goto_cleaned") {
     129                my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     130
     131                unless ($ipprc->file_exists($config_file)) {
     132                    print STDERR "skipping cleanup for chipRun $stage_id $class_id "
     133                        . " because config file ($config_file) is missing\n";
     134                    $status = 0;
     135                }
     136            }
     137            elsif ($mode eq "goto_scrubbed") {
     138                my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     139
     140                if ($ipprc->file_exists($config_file)) {
     141                    print STDERR "skipping scrubbed for chipRun $stage_id $class_id "
     142                        . " because config file ($config_file) is present\n";
     143                    $status = 0;
     144                }
     145            }
     146        }
    144147
    145148        if ($status) {
     
    175178                $command .= " -topurgedimfile";
    176179            }
    177             elsif ($mode eq "goto_cleaned") {
     180            elsif ($mode eq "goto_cleaned") {
    178181                $command .= " -tocleanedimfile";
    179182            }
    180             elsif ($mode eq "goto_scrubbed") {
    181                 $command .= " -toscrubbedimfile";
    182             }
     183            elsif ($mode eq "goto_scrubbed") {
     184                $command .= " -toscrubbedimfile";
     185            }
    183186
    184187            $command .= " -dbname $dbname" if defined $dbname;
     
    191194            }
    192195
    193             # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
    194             my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
    195             $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
    196             $command .= " -dbname $dbname" if defined $dbname;
    197             ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    198                 run(command => $command, verbose => $verbose);
    199             unless ($success) {
    200                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    201                 &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
    202             }
     196            # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
     197            my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
     198            $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
     199            $command .= " -dbname $dbname" if defined $dbname;
     200            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     201                run(command => $command, verbose => $verbose);
     202            unless ($success) {
     203                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     204                &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
     205            }
    203206
    204207        } else {
    205208
    206             # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
    207             my $command = "$chiptool -updateprocessedimfile -chip_id $stage_id -class_id $class_id -set_state $error_state";
    208             $command .= " -dbname $dbname" if defined $dbname;
     209            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
     210            my $command = "$chiptool -updateprocessedimfile -chip_id $stage_id -class_id $class_id -set_state $error_state";
     211            $command .= " -dbname $dbname" if defined $dbname;
    209212
    210213            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    214217                &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
    215218            }
    216            
    217             # We want to flag the run as well, to avoid attempting to reprocess the same data over and over again.
    218             $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
    219             $command .= " -dbname $dbname" if defined $dbname;
    220 
    221             ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    222                 run(command => $command, verbose => $verbose);
    223             unless ($success) {
    224                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    225                 &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
    226             }
     219
     220            # We want to flag the run as well, to avoid attempting to reprocess the same data over and over again.
     221            $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
     222            $command .= " -dbname $dbname" if defined $dbname;
     223
     224            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     225                run(command => $command, verbose => $verbose);
     226            unless ($success) {
     227                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     228                &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
     229            }
    227230
    228231        }
     
    264267    if ($mode eq "goto_cleaned") {
    265268        my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
    266        
    267         unless ($ipprc->file_exists($config_file)) {
     269
     270        unless ($ipprc->file_exists($config_file)) {
    268271            print STDERR "skipping cleanup for camRun $stage_id because config file is missing\n";
    269272            $status = 0;
     
    271274    }
    272275    elsif ($mode eq "goto_scrubbed") {
    273         my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
    274 
    275         if ($ipprc->file_exists($config_file)) {
    276             print STDERR "skipping cleanup for camRun $stage_id because config file ($config_file) is present\n";
    277             $status = 0;
    278         }
     276        my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
     277
     278        if ($ipprc->file_exists($config_file)) {
     279            print STDERR "skipping cleanup for camRun $stage_id because config file ($config_file) is present\n";
     280            $status = 0;
     281        }
    279282    }
    280283    if ($status) {
     
    296299        if ($mode eq "goto_cleaned") {
    297300            $command = "$camtool -updaterun -cam_id $stage_id -set_state cleaned";
    298         }
     301        }
    299302        if ($mode eq "goto_scrubbed") {
    300303            $command = "$camtool -updaterun -cam_id $stage_id -set_state scrubbed";
    301         }
     304        }
    302305        if ($mode eq "goto_purged") {
    303306            $command = "$camtool -updaterun -cam_id $stage_id -set_state purged";
     
    311314        }
    312315
    313         # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
    314         my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
    315         $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
     316        # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
     317        my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
     318        $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
    316319        $command .= " -dbname $dbname" if defined $dbname;
    317         ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    318             run(command => $command, verbose => $verbose);
    319         unless ($success) {
    320             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    321             &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
    322         }
     320        ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     321            run(command => $command, verbose => $verbose);
     322        unless ($success) {
     323            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     324            &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
     325        }
    323326
    324327    } else {
    325         # since 'camera' has only a single imfile, we can just update the run
     328        # since 'camera' has only a single imfile, we can just update the run
    326329        my $command = "$camtool -updaterun -cam_id $stage_id -set_state $error_state";
    327330        $command .= " -dbname $dbname" if defined $dbname;
     
    356359        &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
    357360    }
    358    
     361
    359362    if (@$stdout_buf == 0) {
    360         # No skycells were found for some reason.
    361         # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
    362         my $command = "$warptool -updaterun -warp_id $stage_id -set_state $error_state";
    363         $command .= " -dbname $dbname" if defined $dbname;
    364        
    365         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    366             run(command => $command, verbose => $verbose);
    367         unless ($success) {
    368             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    369             &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
    370         }
    371        
    372         exit(0);
     363        # No skycells were found for some reason.
     364        # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
     365        my $command = "$warptool -updaterun -warp_id $stage_id -set_state $error_state";
     366        $command .= " -dbname $dbname" if defined $dbname;
     367
     368        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     369            run(command => $command, verbose => $verbose);
     370        unless ($success) {
     371            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     372            &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
     373        }
     374
     375        exit(0);
    373376    }
    374377    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     
    384387
    385388        my $status = 1;
    386         if ($mode eq "goto_cleaned") {
    387             my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
    388 
    389             unless ($ipprc->file_exists($config_file)) {
    390                 print STDERR "skipping cleanup for warpRun $stage_id $skycell_id" .
    391                     " because config file is missing\n";
    392                 $status = 0;
    393             }
    394         }
    395         elsif ($mode eq "goto_scrubbed") {
    396             my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
    397 
    398             if ($ipprc->file_exists($config_file)) {
    399                 print STDERR "skipping scrubbed for warpRun $stage_id $skycell_id" .
    400                     " because config file is present\n";
    401                 $status = 0;
    402             }
    403         }
    404         if ($status) {
    405             if ($skyfile->{quality} != 8007) {
    406                 my @files = ();
    407                
    408                 # delete the temporary image datafiles
    409                 addFilename(\@files, "PSWARP.OUTPUT", $path_base, $skycell_id );
    410                 addFilename(\@files, "PSWARP.OUTPUT.MASK", $path_base, $skycell_id);
    411                 addFilename(\@files, "PSWARP.OUTPUT.VARIANCE", $path_base, $skycell_id);
     389        $status = 0 unless defined $path_base and $path_base ne "NULL";
     390
     391        if ($status) {
     392            if ($mode eq "goto_cleaned") {
     393                my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
     394
     395                unless ($ipprc->file_exists($config_file)) {
     396                    print STDERR "skipping cleanup for warpRun $stage_id $skycell_id" .
     397                        " because config file is missing\n";
     398                    $status = 0;
     399                }
     400            }
     401            elsif ($mode eq "goto_scrubbed") {
     402                my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
     403
     404                if ($ipprc->file_exists($config_file)) {
     405                    print STDERR "skipping scrubbed for warpRun $stage_id $skycell_id" .
     406                        " because config file is present\n";
     407                    $status = 0;
     408                }
     409            }
     410        }
     411
     412        if ($status) {
     413            if ($skyfile->{quality} != 8007) {
     414                my @files = ();
     415
     416                # delete the temporary image datafiles
     417                addFilename(\@files, "PSWARP.OUTPUT", $path_base, $skycell_id );
     418                addFilename(\@files, "PSWARP.OUTPUT.MASK", $path_base, $skycell_id);
     419                addFilename(\@files, "PSWARP.OUTPUT.VARIANCE", $path_base, $skycell_id);
    412420#            addFilename(\@files, "PSWARP.OUTPUT.SOURCES", $path_base, $skycell_id);
    413                 if ($mode eq "goto_purged") {
    414                     # additional files to remove for 'purge' mode
    415                     addFilename(\@files, "PSWARP.BIN1", $path_base, $skycell_id );
    416                     addFilename(\@files, "PSWARP.BIN2", $path_base, $skycell_id );
    417                     addFilename(\@files, "SKYCELL.STATS", $path_base, $skycell_id );
    418                     # addFilename(\@files, "PSPHOT.PSF.SKY.SAVE", $path_base);
    419                    
    420                     # XXX: do we want to delete these?
    421                     # addFilename(\@files, "TRACE.EXP", $path_base, $skycell_id);
    422                     # addFilename(\@files, "PSWARP.CONFIG", $path_base, $skycell_id);
    423                 }
     421                if ($mode eq "goto_purged") {
     422                    # additional files to remove for 'purge' mode
     423                    addFilename(\@files, "PSWARP.BIN1", $path_base, $skycell_id );
     424                    addFilename(\@files, "PSWARP.BIN2", $path_base, $skycell_id );
     425                    addFilename(\@files, "SKYCELL.STATS", $path_base, $skycell_id );
     426                    # addFilename(\@files, "PSPHOT.PSF.SKY.SAVE", $path_base);
     427
     428                    # XXX: do we want to delete these?
     429                    # addFilename(\@files, "TRACE.EXP", $path_base, $skycell_id);
     430                    # addFilename(\@files, "PSWARP.CONFIG", $path_base, $skycell_id);
     431                }
    424432            # actual command to delete the files
    425                 $status = &delete_files (\@files);
    426             }
     433                $status = &delete_files (\@files);
     434            }
    427435        }
    428436
     
    431439            if ($mode eq "goto_purged") {
    432440                $command .= " -topurgedskyfile";
    433             } 
    434             elsif ($mode eq "goto_cleaned") {
     441            }
     442            elsif ($mode eq "goto_cleaned") {
    435443                $command .= " -tocleanedskyfile";
    436444            }
    437             elsif ($mode eq "goto_scrubbed") {
    438                 $command .= " -toscrubbedskyfile";
    439             }
     445            elsif ($mode eq "goto_scrubbed") {
     446                $command .= " -toscrubbedskyfile";
     447            }
    440448            $command .= " -dbname $dbname" if defined $dbname;
    441449
     
    447455            }
    448456
    449             # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
    450             my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
    451             $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
    452             $command .= " -dbname $dbname" if defined $dbname;
    453             ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    454                 run(command => $command, verbose => $verbose);
    455             unless ($success) {
    456                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    457                 &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
    458             }
     457            # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
     458            my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
     459            $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
     460            $command .= " -dbname $dbname" if defined $dbname;
     461            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     462                run(command => $command, verbose => $verbose);
     463            unless ($success) {
     464                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     465                &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
     466            }
    459467
    460468         } else {
    461             my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -set_state $error_state";
    462             $command .= " -dbname $dbname" if defined $dbname;
     469            my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -set_state $error_state";
     470            $command .= " -dbname $dbname" if defined $dbname;
    463471
    464472            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    469477            }
    470478
    471             #            exit $PS_EXIT_UNKNOWN_ERROR;
     479            #            exit $PS_EXIT_UNKNOWN_ERROR;
    472480        }
    473481    }
     
    487495    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    488496    unless ($success) {
    489         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    490         &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
     497        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     498        &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
    491499    }
    492500
    493501    if (@$stdout_buf == 0) {
    494         # No skycells were found for some reason.
    495         # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
    496         my $command = "$stacktool -updaterun -stack_id $stage_id -set_state $error_state";
    497         $command .= " -dbname $dbname" if defined $dbname;
    498        
    499         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    500             run(command => $command, verbose => $verbose);
    501         unless ($success) {
    502             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    503             &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
    504         }
    505        
    506         exit(0);
    507     }
    508 
    509     my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
    510         &my_die("Unable to parse metadata config doc", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
    511 
    512     $skyfiles = parse_md_list($metadata) or 
    513         &my_die("Unable to parse metadata list", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
    514    
     502        # No skycells were found for some reason.
     503        # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
     504        my $command = "$stacktool -updaterun -stack_id $stage_id -set_state $error_state";
     505        $command .= " -dbname $dbname" if defined $dbname;
     506
     507        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     508            run(command => $command, verbose => $verbose);
     509        unless ($success) {
     510            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     511            &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
     512        }
     513
     514        exit(0);
     515    }
     516
     517    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or
     518        &my_die("Unable to parse metadata config doc", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
     519
     520    $skyfiles = parse_md_list($metadata) or
     521        &my_die("Unable to parse metadata list", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
     522
    515523    my @files = ();
    516524    foreach my $skyfile (@{ $skyfiles }) {
    517         my $path_base = $skyfile->{path_base};
    518         my $skycell_id = $skyfile->{skycell_id};
    519 
    520         my $status = 1;
    521         if ((!exists($skyfile->{path_base}))||
    522             (!defined($path_base))) {
    523             $status = 0;
    524         }
    525         if ($status) {
    526             if ($mode eq "goto_cleaned") {
    527                 my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
    528                
    529                 unless ($ipprc->file_exists($config_file)) {
    530                     print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
    531                         " because config file is missing\n";
    532                     $status = 0;
    533                 }
    534             }
    535             elsif ($mode eq "goto_scrubbed") {
    536                 my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
    537                
    538                 if ($ipprc->file_exists($config_file)) {
    539                     print STDERR "skipping scrubbed for stackRun $stage_id $skycell_id" .
    540                         " because config file is present\n";
    541                     $status = 0;
    542                 }
    543             }
    544         }
    545 
    546         if ($status) {
    547             my @files = ();
    548             # delete the temporary image datafiles
    549             addFilename(\@files, "PPSTACK.OUTPUT", $path_base, $skycell_id);
    550             addFilename(\@files, "PPSTACK.OUTPUT.MASK", $path_base, $skycell_id);
    551             addFilename(\@files, "PPSTACK.OUTPUT.VARIANCE", $path_base, $skycell_id);
    552 
    553             if ($mode eq "goto_purged") {
    554                 # additional files to remove for 'purge' mode
    555                 addFilename(\@files, "PPSTACK.CONV.KERNEL", $path_base, $skycell_id);
    556                 addFilename(\@files, "PPSTACK.OUTPUT.JPEG1", $path_base, $skycell_id);
    557                 addFilename(\@files, "PPSTACK.OUTPUT.JPEG2", $path_base, $skycell_id);
    558                 # Commented out to match warp files.
    559                 #addFilename(\@files, "PPSTACK.TARGET.PSF", $path_base, $skycell_id);
    560                 #addFilename(\@files, "PPSTACK.CONFIG", $path_base, $skycell_id);
    561             }
    562 
    563             $status = &delete_files(\@files);
    564         }
    565 
    566         if ($status) {
    567             my $command = "$stacktool -stack_id $stage_id";
    568             if ($mode eq "goto_purged") {
    569                 $command .= " -updaterun -set_state purged";
    570             }
    571             elsif ($mode eq "goto_cleaned") {
    572                 $command .= " -updaterun -set_state cleaned";
    573             }
    574             elsif ($mode eq "goto_scrubbed") {
    575                 $command .= " -updaterun -set_state scrubbed";
    576             }
    577             $command .= " -dbname $dbname" if defined $dbname;
    578            
    579             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    580                 run(command => $command, verbose => $verbose);
    581             unless ($success) {
    582                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    583                 &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
    584             }
    585 
    586             # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
    587             my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
    588             $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
    589             $command .= " -dbname $dbname" if defined $dbname;
    590             ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    591                 run(command => $command, verbose => $verbose);
    592             unless ($success) {
    593                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    594                 &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
    595             }
    596 
    597         } else {
    598             my $command = "$stacktool -updaterun  -stack_id $stage_id -set_state $error_state";
    599             $command .= " -dbname $dbname" if defined $dbname;
    600            
    601             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    602                 run(command => $command, verbose => $verbose);
    603             unless ($success) {
    604                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    605                 &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
    606             }
    607 #           exit $PS_EXIT_UNKNOWN_ERROR;
    608         }
     525        my $path_base = $skyfile->{path_base};
     526        my $skycell_id = $skyfile->{skycell_id};
     527
     528        my $status = 1;
     529        if ((!exists($skyfile->{path_base}))||
     530            (!defined($path_base))) {
     531            $status = 0;
     532        }
     533        if ($status) {
     534            if ($mode eq "goto_cleaned") {
     535                my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
     536
     537                unless ($ipprc->file_exists($config_file)) {
     538                    print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
     539                        " because config file is missing\n";
     540                    $status = 0;
     541                }
     542            }
     543            elsif ($mode eq "goto_scrubbed") {
     544                my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
     545
     546                if ($ipprc->file_exists($config_file)) {
     547                    print STDERR "skipping scrubbed for stackRun $stage_id $skycell_id" .
     548                        " because config file is present\n";
     549                    $status = 0;
     550                }
     551            }
     552        }
     553
     554        if ($status) {
     555            my @files = ();
     556            # delete the temporary image datafiles
     557            addFilename(\@files, "PPSTACK.OUTPUT", $path_base, $skycell_id);
     558            addFilename(\@files, "PPSTACK.OUTPUT.MASK", $path_base, $skycell_id);
     559            addFilename(\@files, "PPSTACK.OUTPUT.VARIANCE", $path_base, $skycell_id);
     560
     561            if ($mode eq "goto_purged") {
     562                # additional files to remove for 'purge' mode
     563                addFilename(\@files, "PPSTACK.CONV.KERNEL", $path_base, $skycell_id);
     564                addFilename(\@files, "PPSTACK.OUTPUT.JPEG1", $path_base, $skycell_id);
     565                addFilename(\@files, "PPSTACK.OUTPUT.JPEG2", $path_base, $skycell_id);
     566                # Commented out to match warp files.
     567                #addFilename(\@files, "PPSTACK.TARGET.PSF", $path_base, $skycell_id);
     568                #addFilename(\@files, "PPSTACK.CONFIG", $path_base, $skycell_id);
     569            }
     570
     571            $status = &delete_files(\@files);
     572        }
     573
     574        if ($status) {
     575            my $command = "$stacktool -stack_id $stage_id";
     576            if ($mode eq "goto_purged") {
     577                $command .= " -updaterun -set_state purged";
     578            }
     579            elsif ($mode eq "goto_cleaned") {
     580                $command .= " -updaterun -set_state cleaned";
     581            }
     582            elsif ($mode eq "goto_scrubbed") {
     583                $command .= " -updaterun -set_state scrubbed";
     584            }
     585            $command .= " -dbname $dbname" if defined $dbname;
     586
     587            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     588                run(command => $command, verbose => $verbose);
     589            unless ($success) {
     590                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     591                &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
     592            }
     593
     594            # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
     595            my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
     596            $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
     597            $command .= " -dbname $dbname" if defined $dbname;
     598            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     599                run(command => $command, verbose => $verbose);
     600            unless ($success) {
     601                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     602                &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
     603            }
     604
     605        } else {
     606            my $command = "$stacktool -updaterun  -stack_id $stage_id -set_state $error_state";
     607            $command .= " -dbname $dbname" if defined $dbname;
     608
     609            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     610                run(command => $command, verbose => $verbose);
     611            unless ($success) {
     612                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     613                &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
     614            }
     615#           exit $PS_EXIT_UNKNOWN_ERROR;
     616        }
    609617    }
    610618    exit 0;
     
    612620
    613621if ($stage eq 'diff') {
    614    
     622
    615623    die "--stage_id required for stage diff\n" if !$stage_id;
    616624
     
    624632    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    625633    unless ($success) {
    626         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    627         &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
     634        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     635        &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
    628636    }
    629637
    630638    if (@$stdout_buf == 0) {
    631         # No skycells were found for some reason.
    632         # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
    633         my $command = "$difftool -updaterun -diff_id $stage_id -set_state $error_state";
    634         $command .= " -dbname $dbname" if defined $dbname;
    635        
    636         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    637             run(command => $command, verbose => $verbose);
    638         unless ($success) {
    639             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    640             &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
    641         }
    642        
    643         exit(0);
    644     }
    645 
    646     my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
    647         &my_die("Unable to parse metadata config doc", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
    648 
    649     $skyfiles = parse_md_list($metadata) or 
    650         &my_die("Unable to parse metadata list", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
    651    
     639        # No skycells were found for some reason.
     640        # Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
     641        my $command = "$difftool -updaterun -diff_id $stage_id -set_state $error_state";
     642        $command .= " -dbname $dbname" if defined $dbname;
     643
     644        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     645            run(command => $command, verbose => $verbose);
     646        unless ($success) {
     647            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     648            &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
     649        }
     650
     651        exit(0);
     652    }
     653
     654    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or
     655        &my_die("Unable to parse metadata config doc", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
     656
     657    $skyfiles = parse_md_list($metadata) or
     658        &my_die("Unable to parse metadata list", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
     659
    652660    my @files = ();
    653661    foreach my $skyfile (@{ $skyfiles }) {
    654         my $path_base = $skyfile->{path_base};
    655         my $skycell_id = $skyfile->{skycell_id};
    656 
    657         my $status = 1;
    658         if ((!exists($skyfile->{path_base}))||
    659             (!defined($path_base))) {
    660             $status = 0;
    661         }
    662         if ($status) {
    663            
    664             if ($mode eq "goto_cleaned") {
    665                 my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
    666                
    667                 unless ($ipprc->file_exists($config_file)) {
    668                     print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
    669                         " because config file ($config_file) is missing\n";
    670                     $status = 0;
    671                 }
    672             }
    673             elsif ($mode eq "goto_scrubbed") {
    674                 my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
    675                
    676                 if ($ipprc->file_exists($config_file)) {
    677                     print STDERR "skipping scrubbed for diffRun $stage_id $skycell_id" .
    678                         " because config file ($config_file) is present\n";
    679                     $status = 0;
    680                 }
    681             }
    682         }
    683         if ($status) {
    684             my @files = ();
    685             # delete the temporary image datafiles
    686             addFilename(\@files, "PPSUB.OUTPUT", $path_base, $skycell_id);
    687             addFilename(\@files, "PPSUB.OUTPUT.MASK", $path_base, $skycell_id);
    688             addFilename(\@files, "PPSUB.OUTPUT.VARIANCE", $path_base, $skycell_id);
    689 
    690             addFilename(\@files, "PPSUB.INVERSE", $path_base, $skycell_id);
    691             addFilename(\@files, "PPSUB.INVERSE.MASK", $path_base, $skycell_id);
    692             addFilename(\@files, "PPSUB.INVERSE.VARIANCE", $path_base, $skycell_id);
    693 
    694             addFilename(\@files, "PPSUB.INPUT.CONV", $path_base, $skycell_id);
    695             addFilename(\@files, "PPSUB.INPUT.CONV.MASK", $path_base, $skycell_id);
    696             addFilename(\@files, "PPSUB.INPUT.CONV.VARIANCE", $path_base, $skycell_id);
    697            
    698             addFilename(\@files, "PPSUB.REF.CONV", $path_base, $skycell_id);
    699             addFilename(\@files, "PPSUB.REF.CONV.MASK", $path_base, $skycell_id);
    700             addFilename(\@files, "PPSUB.REF.CONV.VARIANCE", $path_base, $skycell_id);
    701            
    702             if ($mode eq "goto_purged") {
    703                 # additional files to remove for 'purge' mode
    704                 addFilename(\@files, "PPSUB.OUTPUT.KERNELS", $path_base, $skycell_id);
    705                 addFilename(\@files, "PPSUB.OUTPUT.JPEG1", $path_base, $skycell_id);
    706                 addFilename(\@files, "PPSUB.OUTPUT.JPEG2", $path_base, $skycell_id);
    707                 # Commented out to match warp files.
    708                 #addFilename(\@files, "PPSUB.CONFIG", $path_base, $skycell_id);
    709                 addFilename(\@files, "PPSUB.OUTPUT.SOURCES", $path_base, $skycell_id);
    710                 addFilename(\@files, "PPSUB.INVERSE.SOURCES", $path_base, $skycell_id);
    711                
    712             }
    713 #           print STDERR "MY FILES: @files\n";
    714             $status = &delete_files(\@files);
    715         }
    716 #       print STDERR "MY STATUS: $status\n";
    717         if ($status) {
    718             my $command = "$difftool -diff_id $stage_id -skycell_id $skycell_id";
    719 
    720             if ($mode eq "goto_purged") {
    721                 $command .= " -topurgedskyfile";
    722             }
    723             elsif ($mode eq "goto_cleaned") {
    724                 $command .= " -tocleanedskyfile";
    725             }
    726             elsif ($mode eq "goto_scrubbed") {
    727                 $command .= " -toscrubbedskyfile";
    728             }
    729 
    730             $command .= " -dbname $dbname" if defined $dbname;
    731            
    732             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    733                 run(command => $command, verbose => $verbose);
    734             unless ($success) {
    735                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    736                 &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
    737             }
    738 
    739             # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
    740             my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
    741             $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
    742             $command .= " -dbname $dbname" if defined $dbname;
    743             ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    744                 run(command => $command, verbose => $verbose);
    745             unless ($success) {
    746                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    747                 &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
    748             }
    749 
    750         } else {
    751             my $command = "$difftool -updaterun -diff_id $stage_id -set_state $error_state";
    752 
    753             $command .= " -dbname $dbname" if defined $dbname;
    754            
    755             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    756                 run(command => $command, verbose => $verbose);
    757             unless ($success) {
    758                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    759                 &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
    760             }
    761 #           exit $PS_EXIT_UNKNOWN_ERROR;
    762         }
     662        my $path_base = $skyfile->{path_base};
     663        my $skycell_id = $skyfile->{skycell_id};
     664
     665        my $status = 1;
     666        if ((!exists($skyfile->{path_base}))||
     667            (!defined($path_base))) {
     668            $status = 0;
     669        }
     670        if ($status) {
     671
     672            if ($mode eq "goto_cleaned") {
     673                my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
     674
     675                unless ($ipprc->file_exists($config_file)) {
     676                    print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
     677                        " because config file ($config_file) is missing\n";
     678                    $status = 0;
     679                }
     680            }
     681            elsif ($mode eq "goto_scrubbed") {
     682                my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
     683
     684                if ($ipprc->file_exists($config_file)) {
     685                    print STDERR "skipping scrubbed for diffRun $stage_id $skycell_id" .
     686                        " because config file ($config_file) is present\n";
     687                    $status = 0;
     688                }
     689            }
     690        }
     691        if ($status) {
     692            my @files = ();
     693            # delete the temporary image datafiles
     694            addFilename(\@files, "PPSUB.OUTPUT", $path_base, $skycell_id);
     695            addFilename(\@files, "PPSUB.OUTPUT.MASK", $path_base, $skycell_id);
     696            addFilename(\@files, "PPSUB.OUTPUT.VARIANCE", $path_base, $skycell_id);
     697
     698            addFilename(\@files, "PPSUB.INVERSE", $path_base, $skycell_id);
     699            addFilename(\@files, "PPSUB.INVERSE.MASK", $path_base, $skycell_id);
     700            addFilename(\@files, "PPSUB.INVERSE.VARIANCE", $path_base, $skycell_id);
     701
     702            addFilename(\@files, "PPSUB.INPUT.CONV", $path_base, $skycell_id);
     703            addFilename(\@files, "PPSUB.INPUT.CONV.MASK", $path_base, $skycell_id);
     704            addFilename(\@files, "PPSUB.INPUT.CONV.VARIANCE", $path_base, $skycell_id);
     705
     706            addFilename(\@files, "PPSUB.REF.CONV", $path_base, $skycell_id);
     707            addFilename(\@files, "PPSUB.REF.CONV.MASK", $path_base, $skycell_id);
     708            addFilename(\@files, "PPSUB.REF.CONV.VARIANCE", $path_base, $skycell_id);
     709
     710            if ($mode eq "goto_purged") {
     711                # additional files to remove for 'purge' mode
     712                addFilename(\@files, "PPSUB.OUTPUT.KERNELS", $path_base, $skycell_id);
     713                addFilename(\@files, "PPSUB.OUTPUT.JPEG1", $path_base, $skycell_id);
     714                addFilename(\@files, "PPSUB.OUTPUT.JPEG2", $path_base, $skycell_id);
     715                # Commented out to match warp files.
     716                #addFilename(\@files, "PPSUB.CONFIG", $path_base, $skycell_id);
     717                addFilename(\@files, "PPSUB.OUTPUT.SOURCES", $path_base, $skycell_id);
     718                addFilename(\@files, "PPSUB.INVERSE.SOURCES", $path_base, $skycell_id);
     719
     720            }
     721#           print STDERR "MY FILES: @files\n";
     722            $status = &delete_files(\@files);
     723        }
     724#       print STDERR "MY STATUS: $status\n";
     725        if ($status) {
     726            my $command = "$difftool -diff_id $stage_id -skycell_id $skycell_id";
     727
     728            if ($mode eq "goto_purged") {
     729                $command .= " -topurgedskyfile";
     730            }
     731            elsif ($mode eq "goto_cleaned") {
     732                $command .= " -tocleanedskyfile";
     733            }
     734            elsif ($mode eq "goto_scrubbed") {
     735                $command .= " -toscrubbedskyfile";
     736            }
     737
     738            $command .= " -dbname $dbname" if defined $dbname;
     739
     740            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     741                run(command => $command, verbose => $verbose);
     742            unless ($success) {
     743                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     744                &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
     745            }
     746
     747            # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
     748            my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
     749            $command = "$magicdstool -stage $stage -stage_id $stage_id -updaterun -set_state goto_cleaned -state full";
     750            $command .= " -dbname $dbname" if defined $dbname;
     751            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     752                run(command => $command, verbose => $verbose);
     753            unless ($success) {
     754                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     755                &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
     756            }
     757
     758        } else {
     759            my $command = "$difftool -updaterun -diff_id $stage_id -set_state $error_state";
     760
     761            $command .= " -dbname $dbname" if defined $dbname;
     762
     763            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     764                run(command => $command, verbose => $verbose);
     765            unless ($success) {
     766                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     767                &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
     768            }
     769#           exit $PS_EXIT_UNKNOWN_ERROR;
     770        }
    763771    }
    764772    exit 0;
     
    786794    # if there are no fakeProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
    787795    if (@$stdout_buf == 0)  {
    788         my $command = "$faketool -fake_id $stage_id -updaterun -set_state $error_state";
    789         $command .= " -dbname $dbname" if defined $dbname;
    790 
    791         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    792             run(command => $command, verbose => $verbose);
    793         unless ($success) {
    794             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    795             &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
    796         }
    797         exit 0;
     796        my $command = "$faketool -fake_id $stage_id -updaterun -set_state $error_state";
     797        $command .= " -dbname $dbname" if defined $dbname;
     798
     799        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     800            run(command => $command, verbose => $verbose);
     801        unless ($success) {
     802            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     803            &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
     804        }
     805        exit 0;
    798806    }
    799807
     
    813821        # don't clean up unless the data needed to update is available
    814822        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
    815         # goto_scrubbed now requires the config file to not exist.
     823        # goto_scrubbed now requires the config file to not exist.
    816824        if ($mode eq "goto_cleaned") {
    817825            my $config_file = $ipprc->filename("PPSIM.CONFIG", $path_base, $class_id);
    818826
    819             unless ($ipprc->file_exists($config_file)) {
     827            unless ($ipprc->file_exists($config_file)) {
    820828                print STDERR "skipping cleanup for fakeRun $stage_id $class_id "
    821829                    . " because config file is missing\n";
     
    823831            }
    824832        }
    825         elsif ($mode eq "goto_scrubbed") {
    826             my $config_file = $ipprc->filename("PPSIM.CONFIG", $path_base, $class_id);
    827 
    828             if ($ipprc->file_exists($config_file)) {
    829                 print STDERR "skipping scrubbed for fakeRun $stage_id $class_id "
    830                     . " because config file is present\n";
    831                 $status = 0;
    832             }
    833         }
     833        elsif ($mode eq "goto_scrubbed") {
     834            my $config_file = $ipprc->filename("PPSIM.CONFIG", $path_base, $class_id);
     835
     836            if ($ipprc->file_exists($config_file)) {
     837                print STDERR "skipping scrubbed for fakeRun $stage_id $class_id "
     838                    . " because config file is present\n";
     839                $status = 0;
     840            }
     841        }
    834842
    835843        if ($status) {
     
    844852            if ($mode eq "goto_purged") {
    845853                # additional files to remove for 'purge' mode
    846                 addFilename (\@files, "PPSIM.SOURCES", $path_base, $class_id);
    847                 addFilename (\@files, "PPSIM.FAKE.SOURCES", $path_base, $class_id);
    848                 addFilename (\@files, "PPSIM.FORCE.SOURCES", $path_base, $class_id);
     854                addFilename (\@files, "PPSIM.SOURCES", $path_base, $class_id);
     855                addFilename (\@files, "PPSIM.FAKE.SOURCES", $path_base, $class_id);
     856                addFilename (\@files, "PPSIM.FORCE.SOURCES", $path_base, $class_id);
    849857            }
    850858
     
    858866                $command .= " -topurgedimfile";
    859867            }
    860             elsif ($mode eq "goto_cleaned") {
     868            elsif ($mode eq "goto_cleaned") {
    861869                $command .= " -tocleanedimfile";
    862870            }
    863             elsif ($mode eq "goto_scrubbed") {
    864                 $command .= " -toscrubbedimfile";
    865             }
     871            elsif ($mode eq "goto_scrubbed") {
     872                $command .= " -toscrubbedimfile";
     873            }
    866874
    867875            $command .= " -dbname $dbname" if defined $dbname;
     
    875883        } else {
    876884
    877             # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
    878             my $command = "$faketool -updateprocessedimfile -fake_id $stage_id -class_id $class_id -set_state $error_state";
    879             $command .= " -dbname $dbname" if defined $dbname;
     885            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
     886            my $command = "$faketool -updateprocessedimfile -fake_id $stage_id -class_id $class_id -set_state $error_state";
     887            $command .= " -dbname $dbname" if defined $dbname;
    880888
    881889            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    889897    exit 0;
    890898
    891 } 
     899}
    892900# Detrend stages
    893901if ($stage eq "detrend.processed") {
     
    916924    # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
    917925    if (@$stdout_buf != 0)  {
    918 #       exit 0; # Silently exit if there's nothing to do.  I don't know how we'd ever get here, but let's be safe.
    919 
    920 
    921         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     926#       exit 0; # Silently exit if there's nothing to do.  I don't know how we'd ever get here, but let's be safe.
     927
     928
     929        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    922930        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    923        
    924         # extract the metadata for the files into a hash list
    925         $imfiles = parse_md_list($metadata) or
    926             &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     931
     932        # extract the metadata for the files into a hash list
     933        $imfiles = parse_md_list($metadata) or
     934            &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    927935    }
    928936    # loop over all of the imfiles, determine the path_base and class_id for each
    929937    foreach my $imfile (@$imfiles) {
    930         my $iexp_id   = $imfile->{exp_id};
     938        my $iexp_id   = $imfile->{exp_id};
    931939        my $class_id = $imfile->{class_id};
    932940        my $path_base = $imfile->{path_base};
    933941        my $status = 1;
    934942
    935         unless (defined($path_base)) {
    936             print STDERR "PATH BASE: >>$path_base<< didn't get defined for $iexp_id $exp_id $det_id $class_id\n";
    937             $status = 0;
    938         }
    939         unless (defined($class_id)) {
    940             print STDERR "CLASS_ID: >>$class_id<< didn't get defined for $iexp_id $exp_id $det_id $path_base\n";
    941             $status = 0;
    942         }
    943         # Detrends cannot be updated, so goto_cleaned and goto_scrubbed are treated as equivalent,
    944         # and so there is no check for config files.
     943        unless (defined($path_base)) {
     944            print STDERR "PATH BASE: >>$path_base<< didn't get defined for $iexp_id $exp_id $det_id $class_id\n";
     945            $status = 0;
     946        }
     947        unless (defined($class_id)) {
     948            print STDERR "CLASS_ID: >>$class_id<< didn't get defined for $iexp_id $exp_id $det_id $path_base\n";
     949            $status = 0;
     950        }
     951        # Detrends cannot be updated, so goto_cleaned and goto_scrubbed are treated as equivalent,
     952        # and so there is no check for config files.
    945953        if ($status) {
    946954            # array of actual filenames to delete
     
    952960            if ($mode eq "goto_purged") {
    953961                # additional files to remove for 'purge' mode
    954                 addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
    955                 addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
     962                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
     963                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
    956964                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
    957965            }
     
    965973                $command .= " -data_state purged";
    966974            }
    967             elsif ($mode eq "goto_cleaned") {
     975            elsif ($mode eq "goto_cleaned") {
    968976                $command .= " -data_state cleaned";
    969977            }
    970             elsif ($mode eq "goto_scrubbed") {
    971                 $command .= " -data_state scrubbed";
    972             }
     978            elsif ($mode eq "goto_scrubbed") {
     979                $command .= " -data_state scrubbed";
     980            }
    973981            $command .= " -dbname $dbname" if defined $dbname;
    974982
     
    981989
    982990        } else {
    983             # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_*
    984             my $command = "$dettool -det_id $det_id -exp_id $iexp_id -class_id $class_id -updateprocessedimfile ";
    985             $command .= " -data_state $error_state";
    986             $command .= " -dbname $dbname" if defined $dbname;
     991            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_*
     992            my $command = "$dettool -det_id $det_id -exp_id $iexp_id -class_id $class_id -updateprocessedimfile ";
     993            $command .= " -data_state $error_state";
     994            $command .= " -dbname $dbname" if defined $dbname;
    987995
    988996            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    9921000                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    9931001            }
    994         }
     1002        }
    9951003    }
    9961004
     
    10011009    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    10021010    unless ($success) {
    1003         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1004         &my_die("Unable to perform dettool: $error_code", "$stage (detProcessedExp)", $stage_id, $error_code);
     1011        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1012        &my_die("Unable to perform dettool: $error_code", "$stage (detProcessedExp)", $stage_id, $error_code);
    10051013    }
    10061014    if (@$stdout_buf != 0) {
    1007         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1008             &my_die("Unable to parse metadata config doc", "$stage (detProcessedExp)", $stage_id, $PS_EXIT_PROG_ERROR);
    1009         my $exps = parse_md_list($metadata) or
    1010             &my_die("Unable to parse metadata list", "$stage (detProcessedExp)", $stage_id, $PS_EXIT_PROG_ERROR);
    1011        
    1012         foreach my $exp (@$exps) {
    1013             my $exp_id = $exp->{exp_id};
    1014             my $command = "$dettool -updateprocessedexp -det_id $det_id -exp_id $exp_id ";
    1015             if ($mode eq "goto_cleaned") {
    1016                 $command .= " -data_state cleaned ";
    1017             }
    1018             if ($mode eq "goto_scrubbed") {
    1019                 $command .= " -data_state scrubbed ";
    1020             }
    1021             if ($mode eq "goto_purged") {
    1022                 $command .= " -data_state purged ";
    1023             }
    1024             $command .= " -dbname $dbname" if defined $dbname;
    1025 #           print "$command\n";
    1026             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1027                 run(command => $command, verbose => $verbose);
    1028             unless ($success) {
    1029                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1030                 &my_die("Unable to perform dettool: $error_code", "$stage (detProcessedExp)", $stage_id, $error_code);
    1031             }
    1032         }
    1033     }
    1034      
     1015        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1016            &my_die("Unable to parse metadata config doc", "$stage (detProcessedExp)", $stage_id, $PS_EXIT_PROG_ERROR);
     1017        my $exps = parse_md_list($metadata) or
     1018            &my_die("Unable to parse metadata list", "$stage (detProcessedExp)", $stage_id, $PS_EXIT_PROG_ERROR);
     1019
     1020        foreach my $exp (@$exps) {
     1021            my $exp_id = $exp->{exp_id};
     1022            my $command = "$dettool -updateprocessedexp -det_id $det_id -exp_id $exp_id ";
     1023            if ($mode eq "goto_cleaned") {
     1024                $command .= " -data_state cleaned ";
     1025            }
     1026            if ($mode eq "goto_scrubbed") {
     1027                $command .= " -data_state scrubbed ";
     1028            }
     1029            if ($mode eq "goto_purged") {
     1030                $command .= " -data_state purged ";
     1031            }
     1032            $command .= " -dbname $dbname" if defined $dbname;
     1033#           print "$command\n";
     1034            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1035                run(command => $command, verbose => $verbose);
     1036            unless ($success) {
     1037                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1038                &my_die("Unable to perform dettool: $error_code", "$stage (detProcessedExp)", $stage_id, $error_code);
     1039            }
     1040        }
     1041    }
     1042
    10351043    exit 0;
    10361044}
     
    10571065        &my_die("Unable to perform dettool: $error_code", "detrend.process.imfile", $stage_id, $error_code);
    10581066    }
    1059    
     1067
    10601068    # if there are no detResidImfiles (@$stdout_buf == 0), then silently exit.
    10611069    if (@$stdout_buf != 0) {
    1062         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1063             &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    1064        
    1065         # extract the metadata for the files into a hash list
    1066         $imfiles = parse_md_list($metadata) or
    1067             &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1070        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1071            &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1072
     1073        # extract the metadata for the files into a hash list
     1074        $imfiles = parse_md_list($metadata) or
     1075            &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    10681076    }
    10691077    # loop over all of the imfiles, determine the path_base and class_id for each
    10701078    foreach my $imfile (@$imfiles) {
    1071         my $iexp_id = $imfile->{exp_id};
     1079        my $iexp_id = $imfile->{exp_id};
    10721080        my $class_id = $imfile->{class_id};
    10731081        my $path_base = $imfile->{path_base};
    1074         my $iteration = $imfile->{iteration};
     1082        my $iteration = $imfile->{iteration};
    10751083
    10761084        my $status = 1;
    10771085
    1078         # Detrends cannot be updated, so goto_cleaned and goto_scrubbed are treated as equivalent,
    1079         # and so there is no check for config files.
    1080         unless (defined($path_base)) {
    1081             print STDERR "PATH BASE: >>$path_base<< didn't get defined for $iexp_id $exp_id $det_id $class_id\n";
    1082             $status = 0;
    1083         }
    1084         unless (defined($class_id)) {
    1085             print STDERR "CLASS_ID: >>$class_id<< didn't get defined for $iexp_id $exp_id $det_id $path_base\n";
    1086             $status = 0;
    1087         }
     1086        # Detrends cannot be updated, so goto_cleaned and goto_scrubbed are treated as equivalent,
     1087        # and so there is no check for config files.
     1088        unless (defined($path_base)) {
     1089            print STDERR "PATH BASE: >>$path_base<< didn't get defined for $iexp_id $exp_id $det_id $class_id\n";
     1090            $status = 0;
     1091        }
     1092        unless (defined($class_id)) {
     1093            print STDERR "CLASS_ID: >>$class_id<< didn't get defined for $iexp_id $exp_id $det_id $path_base\n";
     1094            $status = 0;
     1095        }
    10881096        if ($status) {
    10891097            # array of actual filenames to delete
     
    10941102            if ($mode eq "goto_purged") {
    10951103                # additional files to remove for 'purge' mode
    1096                 addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
    1097                 addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
     1104                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
     1105                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
    10981106                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
    10991107            }
    1100 #           foreach my $f (@files) {
    1101 #               print "RESID: $f\n";
    1102 #           }
     1108#           foreach my $f (@files) {
     1109#               print "RESID: $f\n";
     1110#           }
    11031111            # actual command to delete the files
    11041112            $status = &delete_files (\@files);
     
    11101118                $command .= " -data_state purged";
    11111119            }
    1112             elsif ($mode eq "goto_cleaned") {
     1120            elsif ($mode eq "goto_cleaned") {
    11131121                $command .= " -data_state cleaned";
    11141122            }
    1115             elsif ($mode eq "goto_scrubbed") {
    1116                 $command .= " -data_state scrubbed";
    1117             }
     1123            elsif ($mode eq "goto_scrubbed") {
     1124                $command .= " -data_state scrubbed";
     1125            }
    11181126
    11191127            $command .= " -dbname $dbname" if defined $dbname;
     
    11261134            }
    11271135        } else {
    1128             my $command = "$dettool -det_id $det_id -exp_id $iexp_id -iteration $iteration -class_id $class_id -updateresidimfile ";
    1129             $command .= " -data_state $error_state ";
    1130             $command .= " -dbname $dbname" if defined $dbname;
     1136            my $command = "$dettool -det_id $det_id -exp_id $iexp_id -iteration $iteration -class_id $class_id -updateresidimfile ";
     1137            $command .= " -data_state $error_state ";
     1138            $command .= " -dbname $dbname" if defined $dbname;
    11311139
    11321140            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    11401148    # Flag the detResidExp as clean now as well, if it is marked tobe cleaned (this is still clunky).
    11411149
    1142     $command = "$dettool -pendingcleanup_residexp -det_id $det_id -exp_id $exp_id"; 
     1150    $command = "$dettool -pendingcleanup_residexp -det_id $det_id -exp_id $exp_id";
    11431151    $command .= " -dbname $dbname" if defined $dbname;
    11441152    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    11451153    unless ($success) {
    1146         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1147         &my_die("Unable to perform dettool: $error_code", "$stage (detResidExp)", $stage_id, $error_code);
     1154        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1155        &my_die("Unable to perform dettool: $error_code", "$stage (detResidExp)", $stage_id, $error_code);
    11481156    }
    11491157    if (@$stdout_buf != 0) {
    1150         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1151             &my_die("Unable to parse metadata config doc", "$stage (detResidExp)", $stage_id, $PS_EXIT_PROG_ERROR);
    1152         my $exps = parse_md_list($metadata) or
    1153             &my_die("Unable to parse metadata list", "$stage (detResidExp)", $stage_id, $PS_EXIT_PROG_ERROR);
    1154        
    1155         foreach my $exp (@$exps) {
    1156             my $iteration = $exp->{iteration};
    1157             my $command = "$dettool -updateresidexp -det_id $det_id -exp_id $exp_id ";
    1158             if ($mode eq "goto_cleaned") {
    1159                 $command .= " -data_state cleaned";
    1160             }
    1161             if ($mode eq "goto_scrubbed") {
    1162                 $command .= " -data_state scrubbed";
    1163             }
    1164             if ($mode eq "goto_purged") {
    1165                 $command .= " -data_state purged";
    1166             }
    1167             $command .= " -dbname $dbname" if defined $dbname;
    1168             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1169                 run(command => $command, verbose => $verbose);
    1170             unless ($success) {
    1171                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1172                 &my_die("Unable to perform dettool: $error_code", "$stage (detResidExp)", $stage_id, $error_code);
    1173             }
    1174         }
     1158        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1159            &my_die("Unable to parse metadata config doc", "$stage (detResidExp)", $stage_id, $PS_EXIT_PROG_ERROR);
     1160        my $exps = parse_md_list($metadata) or
     1161            &my_die("Unable to parse metadata list", "$stage (detResidExp)", $stage_id, $PS_EXIT_PROG_ERROR);
     1162
     1163        foreach my $exp (@$exps) {
     1164            my $iteration = $exp->{iteration};
     1165            my $command = "$dettool -updateresidexp -det_id $det_id -exp_id $exp_id ";
     1166            if ($mode eq "goto_cleaned") {
     1167                $command .= " -data_state cleaned";
     1168            }
     1169            if ($mode eq "goto_scrubbed") {
     1170                $command .= " -data_state scrubbed";
     1171            }
     1172            if ($mode eq "goto_purged") {
     1173                $command .= " -data_state purged";
     1174            }
     1175            $command .= " -dbname $dbname" if defined $dbname;
     1176            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1177                run(command => $command, verbose => $verbose);
     1178            unless ($success) {
     1179                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1180                &my_die("Unable to perform dettool: $error_code", "$stage (detResidExp)", $stage_id, $error_code);
     1181            }
     1182        }
    11751183    }
    11761184
     
    11791187
    11801188if ($stage eq "detrend.stack.imfile") {
    1181    
     1189
    11821190    die "--stage_id required for stage $stage\n" if !$stage_id;
    11831191
     
    11911199    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    11921200    unless ($success) {
    1193         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1194         &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1195     }
    1196     my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
    1197         &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    1198 
    1199     $stacks = parse_md_list($metadata) or 
    1200         &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
    1201    
     1201        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1202        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1203    }
     1204    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or
     1205        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1206
     1207    $stacks = parse_md_list($metadata) or
     1208        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1209
    12021210    my @files = ();
    12031211    foreach my $stack (@{ $stacks }) {
    1204         # detStackedImfile does not have a path_base column.  This is inconvenient, as it means we need to calculate it.
    1205         my $path_base = $stack->{uri};
    1206         my $iteration = $stack->{iteration};
    1207         my $class_id  = $stack->{class_id};
    1208 
    1209         $path_base =~ s/\.fits$//; # That should do it?
    1210 
    1211         my $status = 1;
    1212 
    1213         if ($status) {
    1214             my @files = ();
    1215             # delete the temporary image datafiles
    1216             # There's no convenient way to get the detrend type, so I'm queueing all of them for deletion.
    1217             # I understand that they all point to the same filename right now, but that may not be true in
    1218             # the future.
    1219             addFilename(\@files, "PPMERGE.OUTPUT.MASK", $path_base, $stage_id);
    1220             addFilename(\@files, "PPMERGE.OUTPUT.BIAS", $path_base, $stage_id);
    1221             addFilename(\@files, "PPMERGE.OUTPUT.DARK", $path_base, $stage_id);
    1222             addFilename(\@files, "PPMERGE.OUTPUT.SHUTTER", $path_base, $stage_id);
    1223             addFilename(\@files, "PPMERGE.OUTPUT.FLAT", $path_base, $stage_id);
    1224             addFilename(\@files, "PPMERGE.OUTPUT.FRINGE", $path_base, $stage_id);
    1225            
    1226 
    1227             addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $stage_id);
    1228             addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $stage_id);
    1229 
    1230             if ($mode eq "goto_purged") {
    1231                 # additional files to remove for 'purge' mode
    1232 #               addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
    1233             }
    1234 
    1235             $status = &delete_files(\@files);
    1236         }
    1237 
    1238         if ($status) {
    1239             my $command = "$dettool -det_id $stage_id -iteration $iteration -class_id $class_id";
    1240             if ($mode eq "goto_purged") {
    1241                 $command .= " -updatestacked -data_state purged";
    1242             }
    1243             elsif ($mode eq "goto_cleaned") {
    1244                 $command .= " -updatestacked -data_state cleaned";
    1245             }
    1246             elsif ($mode eq "goto_scrubbed") {
    1247                 $command .= " -updatestacked -data_state scrubbed";
    1248             }
    1249             $command .= " -dbname $dbname" if defined $dbname;
    1250            
    1251             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1252                 run(command => $command, verbose => $verbose);
    1253             unless ($success) {
    1254                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1255                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1256             }
    1257         } else {
    1258             my $command = "$dettool -updatestacked  -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
    1259             $command .= " -dbname $dbname" if defined $dbname;
    1260            
    1261             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1262                 run(command => $command, verbose => $verbose);
    1263             unless ($success) {
    1264                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1265                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1266             }
    1267             exit $PS_EXIT_UNKNOWN_ERROR;
    1268         }
     1212        # detStackedImfile does not have a path_base column.  This is inconvenient, as it means we need to calculate it.
     1213        my $path_base = $stack->{uri};
     1214        my $iteration = $stack->{iteration};
     1215        my $class_id  = $stack->{class_id};
     1216
     1217        $path_base =~ s/\.fits$//; # That should do it?
     1218
     1219        my $status = 1;
     1220
     1221        if ($status) {
     1222            my @files = ();
     1223            # delete the temporary image datafiles
     1224            # There's no convenient way to get the detrend type, so I'm queueing all of them for deletion.
     1225            # I understand that they all point to the same filename right now, but that may not be true in
     1226            # the future.
     1227            addFilename(\@files, "PPMERGE.OUTPUT.MASK", $path_base, $stage_id);
     1228            addFilename(\@files, "PPMERGE.OUTPUT.BIAS", $path_base, $stage_id);
     1229            addFilename(\@files, "PPMERGE.OUTPUT.DARK", $path_base, $stage_id);
     1230            addFilename(\@files, "PPMERGE.OUTPUT.SHUTTER", $path_base, $stage_id);
     1231            addFilename(\@files, "PPMERGE.OUTPUT.FLAT", $path_base, $stage_id);
     1232            addFilename(\@files, "PPMERGE.OUTPUT.FRINGE", $path_base, $stage_id);
     1233
     1234
     1235            addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $stage_id);
     1236            addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $stage_id);
     1237
     1238            if ($mode eq "goto_purged") {
     1239                # additional files to remove for 'purge' mode
     1240#               addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
     1241            }
     1242
     1243            $status = &delete_files(\@files);
     1244        }
     1245
     1246        if ($status) {
     1247            my $command = "$dettool -det_id $stage_id -iteration $iteration -class_id $class_id";
     1248            if ($mode eq "goto_purged") {
     1249                $command .= " -updatestacked -data_state purged";
     1250            }
     1251            elsif ($mode eq "goto_cleaned") {
     1252                $command .= " -updatestacked -data_state cleaned";
     1253            }
     1254            elsif ($mode eq "goto_scrubbed") {
     1255                $command .= " -updatestacked -data_state scrubbed";
     1256            }
     1257            $command .= " -dbname $dbname" if defined $dbname;
     1258
     1259            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1260                run(command => $command, verbose => $verbose);
     1261            unless ($success) {
     1262                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1263                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1264            }
     1265        } else {
     1266            my $command = "$dettool -updatestacked  -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1267            $command .= " -dbname $dbname" if defined $dbname;
     1268
     1269            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1270                run(command => $command, verbose => $verbose);
     1271            unless ($success) {
     1272                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1273                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1274            }
     1275            exit $PS_EXIT_UNKNOWN_ERROR;
     1276        }
    12691277    }
    12701278    # Check to see if we can mark the whole detRunSummary object as cleaned.
    12711279
    1272     $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id"; 
     1280    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
    12731281    $command .= " -dbname $dbname" if defined $dbname;
    12741282    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    12751283    unless ($success) {
    1276         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1277         &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1284        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1285        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    12781286    }
    12791287    if (@$stdout_buf != 0) {
    1280         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1281             &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1282         my $exps = parse_md_list($metadata) or
    1283             &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1284        
    1285         foreach my $exp (@$exps) {
    1286             my $iteration = $exp->{iteration};
    1287             my $command;
    1288             if ($mode eq "goto_cleaned") {
    1289                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
    1290             }
    1291             if ($mode eq "goto_scrubbed") {
    1292                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
    1293             }
    1294             if ($mode eq "goto_purged") {
    1295                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
    1296             }
    1297             $command .= " -dbname $dbname" if defined $dbname;
    1298             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1299                 run(command => $command, verbose => $verbose);
    1300             unless ($success) {
    1301                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1302                 &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    1303             }
    1304         }
     1288        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1289            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1290        my $exps = parse_md_list($metadata) or
     1291            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1292
     1293        foreach my $exp (@$exps) {
     1294            my $iteration = $exp->{iteration};
     1295            my $command;
     1296            if ($mode eq "goto_cleaned") {
     1297                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1298            }
     1299            if ($mode eq "goto_scrubbed") {
     1300                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1301            }
     1302            if ($mode eq "goto_purged") {
     1303                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1304            }
     1305            $command .= " -dbname $dbname" if defined $dbname;
     1306            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1307                run(command => $command, verbose => $verbose);
     1308            unless ($success) {
     1309                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1310                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1311            }
     1312        }
    13051313    }
    13061314    exit 0;
     
    13261334
    13271335    foreach my $exp (@$exps) {
    1328 #       my $path_base = $exp->{path_base};
    1329         my $iteration = $exp->{iteration};
    1330         my $class_id  = $exp->{class_id};
    1331 
    1332         my $status = 1;
    1333         if ($status)  {
    1334             my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id";
    1335             if ($mode eq "goto_cleaned") {
    1336                 $command .= " -data_state cleaned";
    1337             }
    1338             if ($mode eq "goto_scrubbed") {
    1339                 $command .= " -data_state scrubbed";
    1340             }
    1341             if ($mode eq "goto_purged") {
    1342                 $command .= " -data_state purged";
    1343             }
    1344             $command .= " -dbname $dbname" if defined $dbname;
    1345             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1346                 run(command => $command, verbose => $verbose);
    1347             unless ($success) {
    1348                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1349                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1350             }
    1351         } else {
    1352             my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
    1353             $command .= " -dbname $dbname" if defined $dbname;
    1354            
    1355             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1356                 run(command => $command, verbose => $verbose);
    1357             unless ($success) {
    1358                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1359                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1360             }
    1361             exit $PS_EXIT_UNKNOWN_ERROR;
    1362         }
     1336#       my $path_base = $exp->{path_base};
     1337        my $iteration = $exp->{iteration};
     1338        my $class_id  = $exp->{class_id};
     1339
     1340        my $status = 1;
     1341        if ($status)  {
     1342            my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id";
     1343            if ($mode eq "goto_cleaned") {
     1344                $command .= " -data_state cleaned";
     1345            }
     1346            if ($mode eq "goto_scrubbed") {
     1347                $command .= " -data_state scrubbed";
     1348            }
     1349            if ($mode eq "goto_purged") {
     1350                $command .= " -data_state purged";
     1351            }
     1352            $command .= " -dbname $dbname" if defined $dbname;
     1353            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1354                run(command => $command, verbose => $verbose);
     1355            unless ($success) {
     1356                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1357                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1358            }
     1359        } else {
     1360            my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1361            $command .= " -dbname $dbname" if defined $dbname;
     1362
     1363            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1364                run(command => $command, verbose => $verbose);
     1365            unless ($success) {
     1366                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1367                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1368            }
     1369            exit $PS_EXIT_UNKNOWN_ERROR;
     1370        }
    13631371    }
    13641372    # Check to see if we can mark the whole detRunSummary object as cleaned.
    13651373
    1366     $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id"; 
     1374    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
    13671375    $command .= " -dbname $dbname" if defined $dbname;
    13681376    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    13691377    unless ($success) {
    1370         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1371         &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1378        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1379        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    13721380    }
    13731381    if (@$stdout_buf != 0) {
    1374         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1375             &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1376         $exps = parse_md_list($metadata) or
    1377             &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1378        
    1379         foreach my $exp (@$exps) {
    1380             my $iteration = $exp->{iteration};
    1381             my $command;
    1382             if ($mode eq "goto_cleaned") {
    1383                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
    1384             }
    1385             if ($mode eq "goto_scrubbed") {
    1386                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
    1387             }
    1388             if ($mode eq "goto_purged") {
    1389                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
    1390             }
    1391             $command .= " -dbname $dbname" if defined $dbname;
    1392             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1393                 run(command => $command, verbose => $verbose);
    1394             unless ($success) {
    1395                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1396                 &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    1397             }
    1398         }
     1382        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1383            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1384        $exps = parse_md_list($metadata) or
     1385            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1386
     1387        foreach my $exp (@$exps) {
     1388            my $iteration = $exp->{iteration};
     1389            my $command;
     1390            if ($mode eq "goto_cleaned") {
     1391                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1392            }
     1393            if ($mode eq "goto_scrubbed") {
     1394                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1395            }
     1396            if ($mode eq "goto_purged") {
     1397                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1398            }
     1399            $command .= " -dbname $dbname" if defined $dbname;
     1400            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1401                run(command => $command, verbose => $verbose);
     1402            unless ($success) {
     1403                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1404                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1405            }
     1406        }
    13991407    }
    14001408
     
    14241432
    14251433    foreach my $exp (@$exps) {
    1426         my $path_base = $exp->{path_base};
    1427         my $iteration = $exp->{iteration};
    1428         my $class_id  = $exp->{class_id};
    1429 
    1430         my $status = 1;
    1431         # don't clean up unless the data needed to update is available
    1432         # goto_scrubbed now requires the config file to not be present
    1433         if ($mode eq "goto_cleaned") {
    1434             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
    1435            
    1436             unless ($ipprc->file_exists($config_file)) {
    1437                 print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
    1438                 $status = 0;
    1439             }
    1440         }
    1441         elsif ($mode eq "goto_scrubbed") {
    1442             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
    1443            
    1444             if ($ipprc->file_exists($config_file)) {
    1445                 print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
    1446                 $status = 0;
    1447             }
    1448         }
    1449         if ($status) {
    1450             my @files = ();
    1451 
    1452             if ($mode eq "goto_purged") {
    1453                 # additional files to remove for 'purge' mode
    1454                 addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base);
    1455                 addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base);
    1456 
    1457                 addFilename (\@files, "PPIMAGE.OUTPUT", $path_base);
    1458                 addFilename (\@files, "PPIMAGE.STATS", $path_base);
    1459             }
    1460             # actual command to delete the files
    1461             $status = &delete_files (\@files);
    1462         }
    1463        
    1464         if ($status)  {
    1465             my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id";
    1466             if ($mode eq "goto_cleaned") {
    1467                 $command .= " -data_state cleaned";
    1468             }
    1469             if ($mode eq "goto_scrubbed") {
    1470                 $command .= " -data_state scrubbed";
    1471             }
    1472             if ($mode eq "goto_purged") {
    1473                 $command .= " -data_state purged";
    1474             }
    1475             $command .= " -dbname $dbname" if defined $dbname;
    1476             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1477                 run(command => $command, verbose => $verbose);
    1478             unless ($success) {
    1479                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1480                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1481             }
    1482         } else {
    1483             my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
    1484             $command .= " -dbname $dbname" if defined $dbname;
    1485            
    1486             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1487                 run(command => $command, verbose => $verbose);
    1488             unless ($success) {
    1489                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1490                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1491             }
    1492             exit $PS_EXIT_UNKNOWN_ERROR;
    1493         }
     1434        my $path_base = $exp->{path_base};
     1435        my $iteration = $exp->{iteration};
     1436        my $class_id  = $exp->{class_id};
     1437
     1438        my $status = 1;
     1439        # don't clean up unless the data needed to update is available
     1440        # goto_scrubbed now requires the config file to not be present
     1441        if ($mode eq "goto_cleaned") {
     1442            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1443
     1444            unless ($ipprc->file_exists($config_file)) {
     1445                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     1446                $status = 0;
     1447            }
     1448        }
     1449        elsif ($mode eq "goto_scrubbed") {
     1450            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1451
     1452            if ($ipprc->file_exists($config_file)) {
     1453                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     1454                $status = 0;
     1455            }
     1456        }
     1457        if ($status) {
     1458            my @files = ();
     1459
     1460            if ($mode eq "goto_purged") {
     1461                # additional files to remove for 'purge' mode
     1462                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base);
     1463                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base);
     1464
     1465                addFilename (\@files, "PPIMAGE.OUTPUT", $path_base);
     1466                addFilename (\@files, "PPIMAGE.STATS", $path_base);
     1467            }
     1468            # actual command to delete the files
     1469            $status = &delete_files (\@files);
     1470        }
     1471
     1472        if ($status)  {
     1473            my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id";
     1474            if ($mode eq "goto_cleaned") {
     1475                $command .= " -data_state cleaned";
     1476            }
     1477            if ($mode eq "goto_scrubbed") {
     1478                $command .= " -data_state scrubbed";
     1479            }
     1480            if ($mode eq "goto_purged") {
     1481                $command .= " -data_state purged";
     1482            }
     1483            $command .= " -dbname $dbname" if defined $dbname;
     1484            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1485                run(command => $command, verbose => $verbose);
     1486            unless ($success) {
     1487                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1488                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1489            }
     1490        } else {
     1491            my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1492            $command .= " -dbname $dbname" if defined $dbname;
     1493
     1494            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1495                run(command => $command, verbose => $verbose);
     1496            unless ($success) {
     1497                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1498                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1499            }
     1500            exit $PS_EXIT_UNKNOWN_ERROR;
     1501        }
    14941502    }
    14951503    # Check to see if we can mark the whole detRunSummary object as cleaned.
    14961504
    1497     $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id"; 
     1505    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
    14981506    $command .= " -dbname $dbname" if defined $dbname;
    14991507    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    15001508    unless ($success) {
    1501         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1502         &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1509        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1510        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    15031511    }
    15041512    if (@$stdout_buf != 0) {
    1505         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1506             &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1507         $exps = parse_md_list($metadata) or
    1508             &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1509        
    1510         foreach my $exp (@$exps) {
    1511             my $iteration = $exp->{iteration};
    1512             my $command;
    1513             if ($mode eq "goto_cleaned") {
    1514                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
    1515             }
    1516             if ($mode eq "goto_scrubbed") {
    1517                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
    1518             }
    1519             if ($mode eq "goto_purged") {
    1520                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
    1521             }
    1522             $command .= " -dbname $dbname" if defined $dbname;
    1523             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1524                 run(command => $command, verbose => $verbose);
    1525             unless ($success) {
    1526                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1527                 &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    1528             }
    1529         }
     1513        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1514            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1515        $exps = parse_md_list($metadata) or
     1516            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1517
     1518        foreach my $exp (@$exps) {
     1519            my $iteration = $exp->{iteration};
     1520            my $command;
     1521            if ($mode eq "goto_cleaned") {
     1522                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1523            }
     1524            if ($mode eq "goto_scrubbed") {
     1525                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1526            }
     1527            if ($mode eq "goto_purged") {
     1528                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1529            }
     1530            $command .= " -dbname $dbname" if defined $dbname;
     1531            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1532                run(command => $command, verbose => $verbose);
     1533            unless ($success) {
     1534                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1535                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1536            }
     1537        }
    15301538    }
    15311539
     
    15491557
    15501558    if (@$stdout_buf == 0) {
    1551         exit 0;
     1559        exit 0;
    15521560    }
    15531561    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     
    15581566
    15591567    foreach my $exp (@$exps) {
    1560         my $exp_id = $exp->{exp_id};
    1561         my $iteration = $exp->{iteration};
    1562         my $path_base = $exp->{path_base};
    1563 
    1564         my $status = 1;
    1565         # don't clean up unless the data needed to update is available
    1566         # goto_scrubbed now requires the config file to not be present
    1567         if ($mode eq "goto_cleaned") {
    1568             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
    1569            
    1570             unless ($ipprc->file_exists($config_file)) {
    1571                 print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
    1572                 $status = 0;
    1573             }
    1574         }
    1575         elsif ($mode eq "goto_scrubbed") {
    1576             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
    1577            
    1578             if ($ipprc->file_exists($config_file)) {
    1579                 print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
    1580                 $status = 0;
    1581             }
    1582         }
    1583         if ($status) {
    1584             my @files = ();
    1585             # delete the temporary image datafiles
    1586             if ($mode eq "goto_purged") {
    1587                 # additional files to remove for 'purge' mode
    1588                 addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
    1589                 addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
    1590             }
    1591             # actual command to delete the files
    1592             $status = &delete_files (\@files);
    1593         }
    1594        
    1595         if ($status)  {
    1596             my $command = "$dettool -updatenormalizedexp -det_id $stage_id -iteration $iteration";
    1597             if ($mode eq "goto_cleaned") {
    1598                 $command .= " -data_state cleaned";
    1599             }
    1600             if ($mode eq "goto_scrubbed") {
    1601                 $command .= " -data_state scrubbed";
    1602             }
    1603             if ($mode eq "goto_purged") {
    1604                 $command .= " -data_state purged";
    1605             }
    1606             $command .= " -dbname $dbname" if defined $dbname;
    1607             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1608                 run(command => $command, verbose => $verbose);
    1609             unless ($success) {
    1610                 print STDERR " residexp had an issue setting the state:? $success $error_code\n";
    1611                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1612                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1613             }
    1614         } else {
    1615             my $command = "$dettool -updatenormalizedexp -det_id $stage_id -exp_id $exp_id -iteration $iteration -data_state $error_state";
    1616             $command .= " -dbname $dbname" if defined $dbname;
    1617            
    1618             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1619                 run(command => $command, verbose => $verbose);
    1620             unless ($success) {
    1621                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1622                 &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
    1623             }
    1624             exit $PS_EXIT_UNKNOWN_ERROR;
    1625         }
     1568        my $exp_id = $exp->{exp_id};
     1569        my $iteration = $exp->{iteration};
     1570        my $path_base = $exp->{path_base};
     1571
     1572        my $status = 1;
     1573        # don't clean up unless the data needed to update is available
     1574        # goto_scrubbed now requires the config file to not be present
     1575        if ($mode eq "goto_cleaned") {
     1576            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1577
     1578            unless ($ipprc->file_exists($config_file)) {
     1579                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     1580                $status = 0;
     1581            }
     1582        }
     1583        elsif ($mode eq "goto_scrubbed") {
     1584            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1585
     1586            if ($ipprc->file_exists($config_file)) {
     1587                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     1588                $status = 0;
     1589            }
     1590        }
     1591        if ($status) {
     1592            my @files = ();
     1593            # delete the temporary image datafiles
     1594            if ($mode eq "goto_purged") {
     1595                # additional files to remove for 'purge' mode
     1596                addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
     1597                addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
     1598            }
     1599            # actual command to delete the files
     1600            $status = &delete_files (\@files);
     1601        }
     1602
     1603        if ($status)  {
     1604            my $command = "$dettool -updatenormalizedexp -det_id $stage_id -iteration $iteration";
     1605            if ($mode eq "goto_cleaned") {
     1606                $command .= " -data_state cleaned";
     1607            }
     1608            if ($mode eq "goto_scrubbed") {
     1609                $command .= " -data_state scrubbed";
     1610            }
     1611            if ($mode eq "goto_purged") {
     1612                $command .= " -data_state purged";
     1613            }
     1614            $command .= " -dbname $dbname" if defined $dbname;
     1615            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1616                run(command => $command, verbose => $verbose);
     1617            unless ($success) {
     1618                print STDERR " residexp had an issue setting the state:? $success $error_code\n";
     1619                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1620                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1621            }
     1622        } else {
     1623            my $command = "$dettool -updatenormalizedexp -det_id $stage_id -exp_id $exp_id -iteration $iteration -data_state $error_state";
     1624            $command .= " -dbname $dbname" if defined $dbname;
     1625
     1626            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1627                run(command => $command, verbose => $verbose);
     1628            unless ($success) {
     1629                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1630                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1631            }
     1632            exit $PS_EXIT_UNKNOWN_ERROR;
     1633        }
    16261634    }
    16271635    # Check to see if we can mark the whole detRunSummary object as cleaned.
    16281636
    1629     $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id"; 
     1637    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
    16301638    $command .= " -dbname $dbname" if defined $dbname;
    16311639    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
    16321640    unless ($success) {
    1633         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1634         &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1641        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1642        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    16351643    }
    16361644    if (@$stdout_buf != 0) {
    1637         $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    1638             &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1639         $exps = parse_md_list($metadata) or
    1640             &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
    1641        
    1642         foreach my $exp (@$exps) {
    1643             my $iteration = $exp->{iteration};
    1644             my $command;
    1645             if ($mode eq "goto_cleaned") {
    1646                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
    1647             }
    1648             if ($mode eq "goto_scrubbed") {
    1649                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
    1650             }
    1651             if ($mode eq "goto_purged") {
    1652                 $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
    1653             }
    1654             $command .= " -dbname $dbname" if defined $dbname;
    1655             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1656                 run(command => $command, verbose => $verbose);
    1657             unless ($success) {
    1658                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1659                 &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
    1660             }
    1661         }
     1645        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1646            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1647        $exps = parse_md_list($metadata) or
     1648            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1649
     1650        foreach my $exp (@$exps) {
     1651            my $iteration = $exp->{iteration};
     1652            my $command;
     1653            if ($mode eq "goto_cleaned") {
     1654                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1655            }
     1656            if ($mode eq "goto_scrubbed") {
     1657                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1658            }
     1659            if ($mode eq "goto_purged") {
     1660                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1661            }
     1662            $command .= " -dbname $dbname" if defined $dbname;
     1663            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1664                run(command => $command, verbose => $verbose);
     1665            unless ($success) {
     1666                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1667                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1668            }
     1669        }
    16621670    }
    16631671    exit 0;
     
    16711679    my $files = shift; # reference to a list of files to unlink
    16721680#     my $test_verbose = 1;
    1673    
     1681
    16741682#     if ($test_verbose == 1) {
    1675 #       open(TMPLOG,">>/tmp/czw.cleanup.log");
    1676 #       flock(TMPLOG,2);
     1683#       open(TMPLOG,">>/tmp/czw.cleanup.log");
     1684#       flock(TMPLOG,2);
    16771685#     }
    16781686
     
    16801688    foreach my $file (@$files) {
    16811689        print STDERR "unlinking $stage $stage_id $file";
    1682         unless ($ipprc->file_exists($file)) {
    1683             print STDERR "\t File not found\n";
    1684         }
    1685         else {
    1686             print STDERR "\n";
    1687         }
    1688 
    1689 #       if ($test_verbose == 1) {
    1690 #           print TMPLOG "$stage $stage_id $file";
    1691            
    1692 #           else {
    1693 #               print TMPLOG "\n";
    1694 #           }
    1695 #       }
     1690        unless ($ipprc->file_exists($file)) {
     1691            print STDERR "\t File not found\n";
     1692        }
     1693        else {
     1694            print STDERR "\n";
     1695        }
     1696
     1697#       if ($test_verbose == 1) {
     1698#           print TMPLOG "$stage $stage_id $file";
     1699
     1700#           else {
     1701#               print TMPLOG "\n";
     1702#           }
     1703#       }
    16961704
    16971705        $ipprc->file_delete($file);
     
    16991707
    17001708#     if ($test_verbose == 1) {
    1701 #       flock(TMPLOG,8);
    1702 #       close(TMPLOG);
     1709#       flock(TMPLOG,8);
     1710#       close(TMPLOG);
    17031711#     }
    17041712
Note: See TracChangeset for help on using the changeset viewer.