IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 2, 2009, 5:10:19 PM (17 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/20090820
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090820

  • branches/eam_branches/20090820/ippScripts/scripts/ipp_cleanup.pl

    r24642 r25766  
    6060
    6161
    62 my %stages = ( chip => 1, camera => 1, fake => 1, warp => 1, stack => 1, diff  => 1,
    63                detrend.process.imfile => 1, detrend.process.exp => 1, detrend.stack.imfile => 1,
    64                detrend.normstat.imfile => 1, detrend.norm.imfile => 1, detrend.norm.exp => 1,
    65                detrend.resid.imfile => 1, detrend.resid.exp => 1 );
     62my %stages = ( "chip" => 1, "camera" => 1, "fake" => 1, "warp" => 1, "stack" => 1, "diff"  => 1,
     63               "detrend.process.imfile" => 1, "detrend.process.exp" => 1, "detrend.stack.imfile" => 1,
     64               "detrend.normstat.imfile" => 1, "detrend.norm.imfile" => 1, "detrend.norm.exp" => 1,
     65               "detrend.resid.imfile" => 1, "detrend.resid.exp" => 1 );
    6666unless ($stages{$stage}) {
    6767    die "unknown stage $stage for ipp_cleanup.pl\n";
     
    119119        # don't clean up unless the data needed to update is available
    120120        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
     121        # goto_scrubbed now requires the config file to not exist.
    121122        if ($mode eq "goto_cleaned") {
    122123            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
    123             print STDERR "CHIP: CONFIG_FILE : $config_file\n";
    124             if (!$config_file or ! -e $config_file) {
     124
     125#            if (!$config_file or ! -e $config_file) {
     126            unless ($ipprc->file_exists($config_file)) {
    125127                print STDERR "skipping cleanup for chipRun $stage_id $class_id "
    126                     . " because config file is missing\n";
     128                    . " because config file ($config_file) is missing\n";
    127129                $status = 0;
    128130            }
    129131        }
     132        elsif ($mode eq "goto_scrubbed") {
     133            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     134
     135            if ($ipprc->file_exists($config_file)) {
     136                print STDERR "skipping scrubbed for chipRun $stage_id $class_id "
     137                    . " because config file ($config_file) is present\n";
     138                $status = 0;
     139            }
     140        }
    130141
    131142        if ($status) {
     
    134145
    135146            # delete the temporary image datafiles
    136             addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
    137             addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
    138             addFilename (\@files, "PPIMAGE.OUTPUT.VARIANCE", $path_base, $class_id);
     147#            addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
     148#            addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
     149#            addFilename (\@files, "PPIMAGE.OUTPUT.VARIANCE", $path_base, $class_id);
    139150            addFilename (\@files, "PPIMAGE.CHIP", $path_base, $class_id);
    140151            addFilename (\@files, "PPIMAGE.CHIP.MASK", $path_base, $class_id);
     
    144155                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base, $class_id);
    145156                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base, $class_id);
    146                 addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
    147                 addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
     157                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);          # clean?
     158                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);          # clean?
    148159                addFilename (\@files, "PPIMAGE.JPEG1", $path_base, $class_id);
    149160                addFilename (\@files, "PPIMAGE.JPEG2", $path_base, $class_id);
    150                 addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
     161                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);         #clean?
    151162                addFilename (\@files, "PPIMAGE.CONFIG", $path_base, $class_id);
    152163            }
     
    160171            if ($mode eq "goto_purged") {
    161172                $command .= " -topurgedimfile";
    162             } else {
     173            }
     174            elsif ($mode eq "goto_cleaned") {
    163175                $command .= " -tocleanedimfile";
    164176            }
     177            elsif ($mode eq "goto_scrubbed") {
     178                $command .= " -toscrubbedimfile";
     179            }
     180
    165181            $command .= " -dbname $dbname" if defined $dbname;
    166182
     
    218234    my $status = 1;
    219235    # don't clean up unless the data needed to update is available
     236    # goto_scrubbed now requires the config file to not be present
    220237    if ($mode eq "goto_cleaned") {
    221238        my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
    222 
    223         if (!$config_file or ! -e $config_file) {
     239       
     240        unless ($ipprc->file_exists($config_file)) {
    224241            print STDERR "skipping cleanup for camRun $stage_id because config file is missing\n";
    225242            $status = 0;
    226243        }
     244    }
     245    elsif ($mode eq "goto_scrubbed") {
     246        my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
     247
     248        if ($ipprc->file_exists($config_file)) {
     249            print STDERR "skipping cleanup for camRun $stage_id because config file ($config_file) is present\n";
     250            $status = 0;
     251        }
    227252    }
    228253    if ($status) {
    229254        my @files = ();
    230255        # delete the temporary image datafiles
    231         addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
     256#        addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
    232257        if ($mode eq "goto_purged") {
    233258            # additional files to remove for 'purge' mode
     
    246271        }
    247272        if ($mode eq "goto_scrubbed") {
    248             $command = "$camtool -updaterun -cam_id $stage_id -set_state cleaned";
     273            $command = "$camtool -updaterun -cam_id $stage_id -set_state scrubbed";
    249274        }
    250275        if ($mode eq "goto_purged") {
     
    305330            my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
    306331
    307             if (!$config_file or ! -e $config_file) {
     332            unless ($ipprc->file_exists($config_file)) {
    308333                print STDERR "skipping cleanup for warpRun $stage_id $skycell_id" .
    309334                    " because config file is missing\n";
     
    311336            }
    312337        }
    313         if ($status) {
    314             # delete the temporary image datafiles
    315             addFilename(\@files, "PSWARP.OUTPUT", $path_base, $skycell_id );
    316             addFilename(\@files, "PSWARP.OUTPUT.MASK", $path_base, $skycell_id);
    317             addFilename(\@files, "PSWARP.OUTPUT.VARIANCE", $path_base, $skycell_id);
    318             addFilename(\@files, "PSWARP.OUTPUT.SOURCES", $path_base, $skycell_id);
    319 
    320             if ($mode eq "goto_purged") {
    321                 # additional files to remove for 'purge' mode
    322                 addFilename(\@files, "PSWARP.BIN1", $path_base, $skycell_id );
    323                 addFilename(\@files, "PSWARP.BIN2", $path_base, $skycell_id );
    324                 addFilename(\@files, "SKYCELL.STATS", $path_base, $skycell_id );
    325                 # addFilename(\@files, "PSPHOT.PSF.SKY.SAVE", $path_base);
    326 
    327                 # XXX: do we want to delete these?
    328                 # addFilename(\@files, "TRACE.EXP", $path_base, $skycell_id);
    329                 # addFilename(\@files, "PSWARP.CONFIG", $path_base, $skycell_id);
    330             }
     338        elsif ($mode eq "goto_scrubbed") {
     339            my $config_file = $ipprc->filename("PSWARP.CONFIG", $path_base, $skycell_id);
     340
     341            if ($ipprc->file_exists($config_file)) {
     342                print STDERR "skipping scrubbed for warpRun $stage_id $skycell_id" .
     343                    " because config file is present\n";
     344                $status = 0;
     345            }
     346        }
     347        if ($status) {
     348            if ($skyfile->{quality} != 8007) {
     349                my @files = ();
     350               
     351                # delete the temporary image datafiles
     352                addFilename(\@files, "PSWARP.OUTPUT", $path_base, $skycell_id );
     353                addFilename(\@files, "PSWARP.OUTPUT.MASK", $path_base, $skycell_id);
     354                addFilename(\@files, "PSWARP.OUTPUT.VARIANCE", $path_base, $skycell_id);
     355#            addFilename(\@files, "PSWARP.OUTPUT.SOURCES", $path_base, $skycell_id);
     356                if ($mode eq "goto_purged") {
     357                    # additional files to remove for 'purge' mode
     358                    addFilename(\@files, "PSWARP.BIN1", $path_base, $skycell_id );
     359                    addFilename(\@files, "PSWARP.BIN2", $path_base, $skycell_id );
     360                    addFilename(\@files, "SKYCELL.STATS", $path_base, $skycell_id );
     361                    # addFilename(\@files, "PSPHOT.PSF.SKY.SAVE", $path_base);
     362                   
     363                    # XXX: do we want to delete these?
     364                    # addFilename(\@files, "TRACE.EXP", $path_base, $skycell_id);
     365                    # addFilename(\@files, "PSWARP.CONFIG", $path_base, $skycell_id);
     366                }
    331367            # actual command to delete the files
    332             $status = &delete_files (\@files);
     368                $status = &delete_files (\@files);
     369            }
    333370        }
    334371
     
    337374            if ($mode eq "goto_purged") {
    338375                $command .= " -topurgedskyfile";
    339             } else {
     376            }
     377            elsif ($mode eq "goto_cleaned") {
    340378                $command .= " -tocleanedskyfile";
    341379            }
     380            elsif ($mode eq "goto_scrubbed") {
     381                $command .= " -toscrubbedskyfile";
     382            }
    342383            $command .= " -dbname $dbname" if defined $dbname;
    343384
     
    395436        if ($mode eq "goto_cleaned") {
    396437            my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
    397             print STDERR "MY CONFIG FILE = $config_file\n";
    398             printf(STDERR "BOOLS: %d %d %d %s\n",!$config_file, ! -e $config_file, -e $config_file,$config_file);
    399             $config_file =~ s%^file://%%;
    400             if (!$config_file or ! -e $config_file) {
     438
     439            unless ($ipprc->file_exists($config_file)) {
    401440                print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
    402441                    " because config file is missing\n";
    403442                $status = 0;
    404443            }
    405             $config_file = 'file://' . $config_file;
     444        }
     445        elsif ($mode eq "goto_scrubbed") {
     446            my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
     447
     448            if ($ipprc->file_exists($config_file)) {
     449                print STDERR "skipping scrubbed for stackRun $stage_id $skycell_id" .
     450                    " because config file is present\n";
     451                $status = 0;
     452            }
    406453        }
    407454        if ($status) {
     455            my @files = ();
    408456            # delete the temporary image datafiles
    409457            addFilename(\@files, "PPSTACK.OUTPUT", $path_base, $skycell_id);
     
    428476            if ($mode eq "goto_purged") {
    429477                $command .= " -updaterun -state purged";
    430             } else {
     478            }
     479            elsif ($mode eq "goto_cleaned") {
    431480                $command .= " -updaterun -state cleaned";
     481            }
     482            elsif ($mode eq "goto_scrubbed") {
     483                $command .= " -updaterun -state scrubbed";
    432484            }
    433485            $command .= " -dbname $dbname" if defined $dbname;
     
    485537        if ($mode eq "goto_cleaned") {
    486538            my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
    487             print STDERR "MY CONFIG FILE = $config_file\n";
    488             printf(STDERR "BOOLS: %d %d %d %s\n",!$config_file, ! -e $config_file, -e $config_file,$config_file);
    489             $config_file =~ s%^file://%%;
    490             if (!$config_file or ! -e $config_file) {
     539
     540            unless ($ipprc->file_exists($config_file)) {
    491541                print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
    492542                    " because config file ($config_file) is missing\n";
    493543                $status = 0;
    494544            }
    495             $config_file = 'file://' . $config_file;
     545        }
     546        elsif ($mode eq "goto_scrubbed") {
     547            my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
     548
     549            if ($ipprc->file_exists($config_file)) {
     550                print STDERR "skipping scrubbed for diffRun $stage_id $skycell_id" .
     551                    " because config file ($config_file) is present\n";
     552                $status = 0;
     553            }
    496554        }
    497555        if ($status) {
     556            my @files = ();
    498557            # delete the temporary image datafiles
    499558            addFilename(\@files, "PPSUB.OUTPUT", $path_base, $skycell_id);
     
    504563            addFilename(\@files, "PPSUB.INVERSE.MASK", $path_base, $skycell_id);
    505564            addFilename(\@files, "PPSUB.INVERSE.VARIANCE", $path_base, $skycell_id);
     565
    506566            addFilename(\@files, "PPSUB.INPUT.CONV", $path_base, $skycell_id);
    507567            addFilename(\@files, "PPSUB.INPUT.CONV.MASK", $path_base, $skycell_id);
     
    523583               
    524584            }
    525             print STDERR "MY FILES: @files\n";
     585#           print STDERR "MY FILES: @files\n";
    526586            $status = &delete_files(\@files);
    527587        }
    528         print STDERR "MY STATUS: $status\n";
     588#       print STDERR "MY STATUS: $status\n";
    529589        if ($status) {
    530590            my $command = "$difftool -diff_id $stage_id";
    531 #           my $command = "$difftool -diff_id $stage_id -skycell_id $skycell_id";
     591
    532592            if ($mode eq "goto_purged") {
    533593                $command .= " -updaterun -state purged";
    534             } else {
     594            }
     595            elsif ($mode eq "goto_cleaned") {
    535596                $command .= " -updaterun -state cleaned";
    536597            }
     598            elsif ($mode eq "goto_scrubbed") {
     599                $command .= " -updaterun -state scrubbed";
     600            }
     601
    537602            $command .= " -dbname $dbname" if defined $dbname;
    538603           
     
    545610        } else {
    546611            my $command = "$difftool -updaterun -diff_id $stage_id -state $error_state";
    547 #           my $command = "$difftool -updaterun -diff_id $stage_id -skycell_id $skycell_id -state $error_state";
     612
    548613            $command .= " -dbname $dbname" if defined $dbname;
    549614           
     
    560625}
    561626if ($stage eq 'fake') {
    562     die "ipp_cleanup.pl -stage fake not yet implemented. Probably will just mark database cleaned.\n";
     627    print STDERR "This does not seem to work at present, as no files exist. Terminating quietly.\n";
     628    exit(0);
     629    die "--stage_id required for stage fake\n" if !$stage_id;
     630    ### select the imfiles for this entry
     631
     632    # this stage uses 'chiptool'
     633    my $faketool = can_run('faketool') or die "Can't find faketool";
     634
     635    # Get list of component imfiles
     636    # XXX may need a different my_die for each stage
     637    my $imfiles;                      # Array of component files
     638    my $command = "$faketool -pendingcleanupimfile -fake_id $stage_id"; # Command to run
     639    $command .= " -dbname $dbname" if defined $dbname;
     640    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     641    unless ($success) {
     642        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     643        &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
     644    }
     645
     646    # if there are no fakeProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
     647    if (@$stdout_buf == 0)  {
     648        my $command = "$faketool -fake_id $stage_id -updaterun -set_state new";
     649        $command .= " -dbname $dbname" if defined $dbname;
     650
     651        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     652            run(command => $command, verbose => $verbose);
     653        unless ($success) {
     654            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     655            &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
     656        }
     657        exit 0;
     658    }
     659
     660    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     661        &my_die("Unable to parse metadata config doc", "fake", $stage_id, $PS_EXIT_PROG_ERROR);
     662
     663    # extract the metadata for the files into a hash list
     664    $imfiles = parse_md_list($metadata) or
     665        &my_die("Unable to parse metadata list", "fake", $stage_id, $PS_EXIT_PROG_ERROR);
     666
     667    # loop over all of the imfiles, determine the path_base and class_id for each
     668    foreach my $imfile (@$imfiles) {
     669        my $class_id = $imfile->{class_id};
     670        my $path_base = $imfile->{path_base};
     671        my $status = 1;
     672
     673        # don't clean up unless the data needed to update is available
     674        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
     675        # goto_scrubbed now requires the config file to not exist.
     676        if ($mode eq "goto_cleaned") {
     677            my $config_file = $ipprc->filename("PPSIM.CONFIG", $path_base, $class_id);
     678
     679            unless ($ipprc->file_exists($config_file)) {
     680                print STDERR "skipping cleanup for fakeRun $stage_id $class_id "
     681                    . " because config file is missing\n";
     682                $status = 0;
     683            }
     684        }
     685        elsif ($mode eq "goto_scrubbed") {
     686            my $config_file = $ipprc->filename("PPSIM.CONFIG", $path_base, $class_id);
     687
     688            if ($ipprc->file_exists($config_file)) {
     689                print STDERR "skipping scrubbed for fakeRun $stage_id $class_id "
     690                    . " because config file is present\n";
     691                $status = 0;
     692            }
     693        }
     694
     695        if ($status) {
     696            # array of actual filenames to delete
     697            my @files = ();
     698
     699            # delete the temporary image datafiles
     700            addFilename (\@files, "PPSIM.OUTPUT.MEF", $path_base, $class_id);
     701            addFilename (\@files, "PPSIM.OUTPUT.SPL", $path_base, $class_id);
     702            addFilename (\@files, "PPSIM.FAKE.CHIP", $path_base, $class_id);
     703            addFilename (\@files, "PPSIM.FORCE.CHIP", $path_base, $class_id);
     704            if ($mode eq "goto_purged") {
     705                # additional files to remove for 'purge' mode
     706                addFilename (\@files, "PPSIM.SOURCES", $path_base, $class_id);
     707                addFilename (\@files, "PPSIM.FAKE.SOURCES", $path_base, $class_id);
     708                addFilename (\@files, "PPSIM.FORCE.SOURCES", $path_base, $class_id);
     709            }
     710
     711            # actual command to delete the files
     712            $status = &delete_files (\@files);
     713        }
     714
     715        if ($status)  {
     716            my $command = "$faketool -fake_id $stage_id -class_id $class_id";
     717            if ($mode eq "goto_purged") {
     718                $command .= " -topurgedimfile";
     719            }
     720            elsif ($mode eq "goto_cleaned") {
     721                $command .= " -tocleanedimfile";
     722            }
     723            elsif ($mode eq "goto_scrubbed") {
     724                $command .= " -toscrubbedimfile";
     725            }
     726
     727            $command .= " -dbname $dbname" if defined $dbname;
     728
     729            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     730                    run(command => $command, verbose => $verbose);
     731            unless ($success) {
     732                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     733                &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
     734            }
     735        } else {
     736
     737            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
     738            my $command = "$faketool -updateprocessedimfile -fake_id $stage_id -class_id $class_id -set_state $error_state";
     739            $command .= " -dbname $dbname" if defined $dbname;
     740
     741            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     742                    run(command => $command, verbose => $verbose);
     743            unless ($success) {
     744                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     745                &my_die("Unable to perform faketool: $error_code", "fake", $stage_id, $error_code);
     746            }
     747        }
     748    }
     749    exit 0;
     750
    563751}
    564 # fake : faketool : -pendingcleanupimfile (loop over imfiles)
     752# Detrend stages
     753if ($stage eq "detrend.process.imfile") {
     754
     755    die "--stage_id required for stage detrend.process.imfile\n" if !$stage_id;
     756    ### select the imfiles for this entry
     757
     758    # this stage uses 'dettool'
     759    my $dettool = can_run('dettool') or die "Can't find chiptool";
     760
     761    # Get list of component imfiles
     762    # XXX may need a different my_die for each stage
     763    my $imfiles;                      # Array of component files
     764    my $command = "$dettool -pendingcleanup_processedimfile -det_id $stage_id"; # Command to run
     765    $command .= " -dbname $dbname" if defined $dbname;
     766    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     767    unless ($success) {
     768        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     769        &my_die("Unable to perform dettool: $error_code", "detrend.process.imfile", $stage_id, $error_code);
     770    }
     771
     772    # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
     773    if (@$stdout_buf == 0)  {
     774        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.
     775    }
     776
     777    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     778        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     779
     780    # extract the metadata for the files into a hash list
     781    $imfiles = parse_md_list($metadata) or
     782        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     783
     784    # loop over all of the imfiles, determine the path_base and class_id for each
     785    foreach my $imfile (@$imfiles) {
     786        my $exp_id   = $imfile->{exp_id};
     787        my $class_id = $imfile->{class_id};
     788        my $path_base = $imfile->{path_base};
     789        my $status = 1;
     790
     791        # don't clean up unless the data needed to update is available
     792        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
     793        # goto_scrubbed now requires the config file to not exist.
     794       
     795        # Possibly not the correct config file, but simtest doesn't leave any around to check.
     796        if ($mode eq "goto_cleaned") {
     797            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     798
     799            unless ($ipprc->file_exists($config_file)) {
     800                print STDERR "skipping cleanup for detrend.process.imfile $stage_id $class_id "
     801                    . " because config file is missing\n";
     802                $status = 0;
     803            }
     804        }
     805        elsif ($mode eq "goto_scrubbed") {
     806            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     807
     808            if ($ipprc->file_exists($config_file)) {
     809                print STDERR "skipping scrubbed for detrend.process.imfile $stage_id $class_id "
     810                    . " because config file is present\n";
     811                $status = 0;
     812            }
     813        }
     814
     815        if ($status) {
     816            # array of actual filenames to delete
     817            my @files = ();
     818
     819            # delete the temporary image datafiles
     820            addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
     821            addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
     822            addFilename (\@files, "PPIMAGE.OUTPUT.VARIANCE", $path_base, $class_id);
     823            if ($mode eq "goto_purged") {
     824                # additional files to remove for 'purge' mode
     825                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
     826                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
     827                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
     828            }
     829
     830            # actual command to delete the files
     831            $status = &delete_files (\@files);
     832        }
     833
     834        if ($status)  {
     835            my $command = "$dettool -det_id $stage_id -exp_id $exp_id -class_id $class_id -updateprocessedimfile";
     836            if ($mode eq "goto_purged") {
     837                $command .= " -data_state purged";
     838            }
     839            elsif ($mode eq "goto_cleaned") {
     840                $command .= " -data_state cleaned";
     841            }
     842            elsif ($mode eq "goto_scrubbed") {
     843                $command .= " -data_state scrubbed";
     844            }
     845
     846            $command .= " -dbname $dbname" if defined $dbname;
     847
     848            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     849                    run(command => $command, verbose => $verbose);
     850            unless ($success) {
     851                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     852                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     853            }
     854
     855        } else {
     856
     857            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
     858            my $command = "$dettool -updateprocessedimfile -det_id $stage_id -exp_id $exp_id -class_id $class_id -data_state $error_state";
     859            $command .= " -dbname $dbname" if defined $dbname;
     860
     861            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     862                    run(command => $command, verbose => $verbose);
     863            unless ($success) {
     864                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     865                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     866            }
     867        }
     868    }
     869
     870    # Check to see if we can mark the whole detRunSummary object as cleaned.
     871
     872    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     873    $command .= " -dbname $dbname" if defined $dbname;
     874    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     875    unless ($success) {
     876        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     877        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     878    }
     879    if (@$stdout_buf != 0) {
     880        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     881            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     882        my $exps = parse_md_list($metadata) or
     883            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     884       
     885        foreach my $exp (@$exps) {
     886            my $iteration = $exp->{iteration};
     887            my $command;
     888            if ($mode eq "goto_cleaned") {
     889                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     890            }
     891            if ($mode eq "goto_scrubbed") {
     892                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     893            }
     894            if ($mode eq "goto_purged") {
     895                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     896            }
     897            $command .= " -dbname $dbname" if defined $dbname;
     898            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     899                run(command => $command, verbose => $verbose);
     900            unless ($success) {
     901                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     902                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     903            }
     904        }
     905    }
     906   
     907    exit 0;
     908}
     909if ($stage eq "detrend.process.exp") {
     910    die "--stage_id required for stage $stage\n" if !$stage_id;
     911    # this stage uses 'dettool'
     912    my $dettool = can_run('dettool') or die "Can't find dettool";
     913
     914    # Get list of component imfiles
     915    # XXX may need a different my_die for each stage
     916    my $exps;                      # Array of component files
     917    my $command = "$dettool -pendingcleanup_processedexp -det_id $stage_id"; # Command to run
     918    $command .= " -dbname $dbname" if defined $dbname;
     919    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     920    unless ($success) {
     921        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     922        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     923    }
     924   
     925    if (@$stdout_buf == 0) {
     926        exit 0; #silently abort. I need to fix this for propers
     927    }
     928
     929    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     930        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     931
     932    $exps = parse_md_list($metadata) or
     933        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     934
     935
     936    foreach my $exp (@$exps) {
     937        my $path_base = $exp->{path_base};
     938        my $exp_id    = $exp->{exp_id};
     939
     940        my $status = 1;
     941        # don't clean up unless the data needed to update is available
     942        # goto_scrubbed now requires the config file to not be present
     943        if ($mode eq "goto_cleaned") {
     944            my $config_file = $ipprc->filename("PSIMAGE.CONFIG", $path_base);
     945           
     946            unless ($ipprc->file_exists($config_file)) {
     947                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     948                $status = 0;
     949            }
     950        }
     951        elsif ($mode eq "goto_scrubbed") {
     952            my $config_file = $ipprc->filename("PSIMAGE.CONFIG", $path_base);
     953           
     954            if ($ipprc->file_exists($config_file)) {
     955                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     956                $status = 0;
     957            }
     958        }
     959        if ($status) {
     960            my @files = ();
     961            # delete the temporary image datafiles
     962            # I can't find anything to put here
     963            if ($mode eq "goto_purged") {
     964                # additional files to remove for 'purge' mode
     965                addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
     966                addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
     967            }
     968            # actual command to delete the files
     969            $status = &delete_files (\@files);
     970        }
     971       
     972        if ($status)  {
     973            my $command;
     974            if ($mode eq "goto_cleaned") {
     975                $command = "$dettool -updateprocessedexp -det_id $stage_id -exp_id $exp_id -data_state cleaned";
     976            }
     977            if ($mode eq "goto_scrubbed") {
     978                $command = "$dettool -updateprocessedexp -det_id $stage_id -exp_id $exp_id -data_state scrubbed";
     979            }
     980            if ($mode eq "goto_purged") {
     981                $command = "$dettool -updateprocessedexp -det_id $stage_id -exp_id $exp_id -data_state purged";
     982            }
     983            $command .= " -dbname $dbname" if defined $dbname;
     984            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     985                run(command => $command, verbose => $verbose);
     986            unless ($success) {
     987                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     988                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     989            }
     990        } else {
     991            # since 'camera' has only a single imfile, we can just update the run
     992            my $command = "$dettool -updateprocessedexp -det_id $stage_id -exp_id $exp_id -data_state $error_state";
     993            $command .= " -dbname $dbname" if defined $dbname;
     994           
     995            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     996                run(command => $command, verbose => $verbose);
     997            unless ($success) {
     998                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     999                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1000            }
     1001            exit $PS_EXIT_UNKNOWN_ERROR;
     1002        }
     1003    }
     1004    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1005
     1006    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1007    $command .= " -dbname $dbname" if defined $dbname;
     1008    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1009    unless ($success) {
     1010        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1011        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1012    }
     1013    if (@$stdout_buf != 0) {
     1014        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1015            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1016        $exps = parse_md_list($metadata) or
     1017            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1018       
     1019        foreach my $exp (@$exps) {
     1020            my $iteration = $exp->{iteration};
     1021            my $command;
     1022            if ($mode eq "goto_cleaned") {
     1023                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1024            }
     1025            if ($mode eq "goto_scrubbed") {
     1026                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1027            }
     1028            if ($mode eq "goto_purged") {
     1029                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1030            }
     1031            $command .= " -dbname $dbname" if defined $dbname;
     1032            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1033                run(command => $command, verbose => $verbose);
     1034            unless ($success) {
     1035                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1036                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1037            }
     1038        }
     1039    }
     1040
     1041    exit 0;
     1042}
     1043if ($stage eq "detrend.stack.imfile") {
     1044   
     1045    die "--stage_id required for stage $stage\n" if !$stage_id;
     1046
     1047    # this stage uses 'dettool'
     1048    my $dettool = can_run('dettool') or die "Can't find dettool";
     1049
     1050    # Get list of component imfiles
     1051    my $stacks;                  # Array reference of component files
     1052    my $command = "$dettool -pendingcleanup_stacked -det_id $stage_id"; # Command to run
     1053    $command .= " -dbname $dbname" if defined $dbname;
     1054    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1055    unless ($success) {
     1056        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1057        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1058    }
     1059    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or
     1060        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1061
     1062    $stacks = parse_md_list($metadata) or
     1063        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1064   
     1065    my @files = ();
     1066    foreach my $stack (@{ $stacks }) {
     1067        # detStackedImfile does not have a path_base column.  This is inconvenient, as it means we need to calculate it.
     1068        my $path_base = $stack->{uri};
     1069        my $iteration = $stack->{iteration};
     1070        my $class_id  = $stack->{class_id};
     1071
     1072        $path_base =~ s/\.fits$//; # That should do it?
     1073
     1074        my $status = 1;
     1075
     1076#       if ($mode eq "goto_cleaned") {
     1077#           my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
     1078
     1079#           $config_file =~ s%^file://%%;
     1080#           if (!$config_file or ! -e $config_file) {
     1081#               print STDERR "skipping cleanup for $stage $stage_id $path_base" .
     1082#                   " because config file is missing\n";
     1083#               $status = 0;
     1084#           }
     1085#           $config_file = 'file://' . $config_file;
     1086#       }
     1087#       elsif ($mode eq "goto_scrubbed") {
     1088#           my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
     1089#           $config_file =~ s%^file://%%;
     1090#           if ($config_file and -e $config_file) {
     1091#               print STDERR "skipping scrubbed for $stage $stage_id $path_base" .
     1092#                   " because config file is present\n";
     1093#               $status = 0;
     1094#           }
     1095#       }
     1096
     1097        if ($status) {
     1098            my @files = ();
     1099            # delete the temporary image datafiles
     1100            # There's no convenient way to get the detrend type, so I'm queueing all of them for deletion.
     1101            # I understand that they all point to the same filename right now, but that may not be true in
     1102            # the future.
     1103            addFilename(\@files, "PPMERGE.OUTPUT.MASK", $path_base, $stage_id);
     1104            addFilename(\@files, "PPMERGE.OUTPUT.BIAS", $path_base, $stage_id);
     1105            addFilename(\@files, "PPMERGE.OUTPUT.DARK", $path_base, $stage_id);
     1106            addFilename(\@files, "PPMERGE.OUTPUT.SHUTTER", $path_base, $stage_id);
     1107            addFilename(\@files, "PPMERGE.OUTPUT.FLAT", $path_base, $stage_id);
     1108            addFilename(\@files, "PPMERGE.OUTPUT.FRINGE", $path_base, $stage_id);
     1109           
     1110
     1111            addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $stage_id);
     1112            addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $stage_id);
     1113
     1114            if ($mode eq "goto_purged") {
     1115                # additional files to remove for 'purge' mode
     1116#               addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
     1117            }
     1118
     1119            $status = &delete_files(\@files);
     1120        }
     1121
     1122        if ($status) {
     1123            my $command = "$dettool -det_id $stage_id -iteration $iteration -class_id $class_id";
     1124            if ($mode eq "goto_purged") {
     1125                $command .= " -updatestacked -data_state purged";
     1126            }
     1127            elsif ($mode eq "goto_cleaned") {
     1128                $command .= " -updatestacked -data_state cleaned";
     1129            }
     1130            elsif ($mode eq "goto_scrubbed") {
     1131                $command .= " -updatestacked -data_state scrubbed";
     1132            }
     1133            $command .= " -dbname $dbname" if defined $dbname;
     1134           
     1135            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1136                run(command => $command, verbose => $verbose);
     1137            unless ($success) {
     1138                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1139                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1140            }
     1141        } else {
     1142            my $command = "$dettool -updatestacked  -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1143            $command .= " -dbname $dbname" if defined $dbname;
     1144           
     1145            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1146                run(command => $command, verbose => $verbose);
     1147            unless ($success) {
     1148                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1149                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1150            }
     1151            exit $PS_EXIT_UNKNOWN_ERROR;
     1152        }
     1153    }
     1154    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1155
     1156    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1157    $command .= " -dbname $dbname" if defined $dbname;
     1158    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1159    unless ($success) {
     1160        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1161        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1162    }
     1163    if (@$stdout_buf != 0) {
     1164        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1165            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1166        my $exps = parse_md_list($metadata) or
     1167            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1168       
     1169        foreach my $exp (@$exps) {
     1170            my $iteration = $exp->{iteration};
     1171            my $command;
     1172            if ($mode eq "goto_cleaned") {
     1173                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1174            }
     1175            if ($mode eq "goto_scrubbed") {
     1176                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1177            }
     1178            if ($mode eq "goto_purged") {
     1179                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1180            }
     1181            $command .= " -dbname $dbname" if defined $dbname;
     1182            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1183                run(command => $command, verbose => $verbose);
     1184            unless ($success) {
     1185                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1186                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1187            }
     1188        }
     1189    }
     1190    exit 0;
     1191}
     1192if ($stage eq "detrend.normstat.imfile") {
     1193    print STDERR "I'm not convinced there's anything to clean up from stage $stage\n";
     1194    die "--stage_id required for stage $stage\n" if !$stage_id;
     1195    # this stage uses 'camtool'
     1196    my $dettool = can_run('dettool') or die "Can't find dettool";
     1197
     1198    my $command = "$dettool -pendingcleanup_normalizedstat -det_id $stage_id"; # Command to run
     1199    $command .= " -dbname $dbname" if defined $dbname;
     1200    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1201    unless ($success) {
     1202        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1203        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1204    }
     1205    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1206        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1207
     1208    my $exps = parse_md_list($metadata) or
     1209        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1210
     1211    foreach my $exp (@$exps) {
     1212#       my $path_base = $exp->{path_base};
     1213        my $iteration = $exp->{iteration};
     1214        my $class_id  = $exp->{class_id};
     1215
     1216        my $status = 1;
     1217        if ($status)  {
     1218            my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id";
     1219            if ($mode eq "goto_cleaned") {
     1220                $command .= " -data_state cleaned";
     1221            }
     1222            if ($mode eq "goto_scrubbed") {
     1223                $command .= " -data_state scrubbed";
     1224            }
     1225            if ($mode eq "goto_purged") {
     1226                $command .= " -data_state purged";
     1227            }
     1228            $command .= " -dbname $dbname" if defined $dbname;
     1229            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1230                run(command => $command, verbose => $verbose);
     1231            unless ($success) {
     1232                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1233                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1234            }
     1235        } else {
     1236            my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1237            $command .= " -dbname $dbname" if defined $dbname;
     1238           
     1239            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1240                run(command => $command, verbose => $verbose);
     1241            unless ($success) {
     1242                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1243                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1244            }
     1245            exit $PS_EXIT_UNKNOWN_ERROR;
     1246        }
     1247    }
     1248    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1249
     1250    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1251    $command .= " -dbname $dbname" if defined $dbname;
     1252    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 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 (detRunSummary)", $stage_id, $error_code);
     1256    }
     1257    if (@$stdout_buf != 0) {
     1258        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1259            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1260        $exps = parse_md_list($metadata) or
     1261            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1262       
     1263        foreach my $exp (@$exps) {
     1264            my $iteration = $exp->{iteration};
     1265            my $command;
     1266            if ($mode eq "goto_cleaned") {
     1267                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1268            }
     1269            if ($mode eq "goto_scrubbed") {
     1270                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1271            }
     1272            if ($mode eq "goto_purged") {
     1273                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1274            }
     1275            $command .= " -dbname $dbname" if defined $dbname;
     1276            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1277                run(command => $command, verbose => $verbose);
     1278            unless ($success) {
     1279                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1280                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1281            }
     1282        }
     1283    }
     1284
     1285    exit 0;
     1286
     1287}
     1288if ($stage eq "detrend.norm.imfile") {
     1289    die "--stage_id required for stage $stage\n" if !$stage_id;
     1290    # this stage uses 'dettool'
     1291    my $dettool = can_run('dettool') or die "Can't find dettool";
     1292
     1293    # Get list of component imfiles
     1294    # XXX may need a different my_die for each stage
     1295    my $exps;                      # Array of component files
     1296    my $command = "$dettool -pendingcleanup_normalizedimfile -det_id $stage_id"; # Command to run
     1297    $command .= " -dbname $dbname" if defined $dbname;
     1298    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1299    unless ($success) {
     1300        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1301        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1302    }
     1303    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1304        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1305
     1306    $exps = parse_md_list($metadata) or
     1307        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1308
     1309    foreach my $exp (@$exps) {
     1310        my $path_base = $exp->{path_base};
     1311        my $iteration = $exp->{iteration};
     1312        my $class_id  = $exp->{class_id};
     1313
     1314        my $status = 1;
     1315        # don't clean up unless the data needed to update is available
     1316        # goto_scrubbed now requires the config file to not be present
     1317        if ($mode eq "goto_cleaned") {
     1318            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1319           
     1320            unless ($ipprc->file_exists($config_file)) {
     1321                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     1322                $status = 0;
     1323            }
     1324        }
     1325        elsif ($mode eq "goto_scrubbed") {
     1326            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1327           
     1328            if ($ipprc->file_exists($config_file)) {
     1329                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     1330                $status = 0;
     1331            }
     1332        }
     1333        if ($status) {
     1334            my @files = ();
     1335
     1336            if ($mode eq "goto_purged") {
     1337                # additional files to remove for 'purge' mode
     1338                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base);
     1339                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base);
     1340
     1341                addFilename (\@files, "PPIMAGE.OUTPUT", $path_base);
     1342                addFilename (\@files, "PPIMAGE.STATS", $path_base);
     1343            }
     1344            # actual command to delete the files
     1345            $status = &delete_files (\@files);
     1346        }
     1347       
     1348        if ($status)  {
     1349            my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id";
     1350            if ($mode eq "goto_cleaned") {
     1351                $command .= " -data_state cleaned";
     1352            }
     1353            if ($mode eq "goto_scrubbed") {
     1354                $command .= " -data_state scrubbed";
     1355            }
     1356            if ($mode eq "goto_purged") {
     1357                $command .= " -data_state purged";
     1358            }
     1359            $command .= " -dbname $dbname" if defined $dbname;
     1360            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1361                run(command => $command, verbose => $verbose);
     1362            unless ($success) {
     1363                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1364                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1365            }
     1366        } else {
     1367            my $command = "$dettool -updatenormalizedimfile -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1368            $command .= " -dbname $dbname" if defined $dbname;
     1369           
     1370            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1371                run(command => $command, verbose => $verbose);
     1372            unless ($success) {
     1373                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1374                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1375            }
     1376            exit $PS_EXIT_UNKNOWN_ERROR;
     1377        }
     1378    }
     1379    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1380
     1381    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1382    $command .= " -dbname $dbname" if defined $dbname;
     1383    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1384    unless ($success) {
     1385        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1386        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1387    }
     1388    if (@$stdout_buf != 0) {
     1389        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1390            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1391        $exps = parse_md_list($metadata) or
     1392            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1393       
     1394        foreach my $exp (@$exps) {
     1395            my $iteration = $exp->{iteration};
     1396            my $command;
     1397            if ($mode eq "goto_cleaned") {
     1398                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1399            }
     1400            if ($mode eq "goto_scrubbed") {
     1401                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1402            }
     1403            if ($mode eq "goto_purged") {
     1404                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1405            }
     1406            $command .= " -dbname $dbname" if defined $dbname;
     1407            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1408                run(command => $command, verbose => $verbose);
     1409            unless ($success) {
     1410                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1411                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1412            }
     1413        }
     1414    }
     1415
     1416    exit 0;
     1417}
     1418if ($stage eq "detrend.norm.exp") {
     1419    die "--stage_id required for stage $stage\n" if !$stage_id;
     1420    # this stage uses 'dettool'
     1421    my $dettool = can_run('dettool') or die "Can't find dettool";
     1422
     1423    # Get list of component imfiles
     1424    # XXX may need a different my_die for each stage
     1425    my $exps;                      # Array of component files
     1426    my $command = "$dettool -pendingcleanup_normalizedexp -det_id $stage_id"; # Command to run
     1427    $command .= " -dbname $dbname" if defined $dbname;
     1428    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1429    unless ($success) {
     1430        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1431        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1432    }
     1433
     1434    if (@$stdout_buf == 0) {
     1435        exit 0;
     1436    }
     1437    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1438        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1439
     1440    $exps = parse_md_list($metadata) or
     1441        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1442
     1443    foreach my $exp (@$exps) {
     1444        my $exp_id = $exp->{exp_id};
     1445        my $iteration = $exp->{iteration};
     1446        my $path_base = $exp->{path_base};
     1447
     1448        my $status = 1;
     1449        # don't clean up unless the data needed to update is available
     1450        # goto_scrubbed now requires the config file to not be present
     1451        if ($mode eq "goto_cleaned") {
     1452            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1453           
     1454            unless ($ipprc->file_exists($config_file)) {
     1455                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     1456                $status = 0;
     1457            }
     1458        }
     1459        elsif ($mode eq "goto_scrubbed") {
     1460            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1461           
     1462            if ($ipprc->file_exists($config_file)) {
     1463                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     1464                $status = 0;
     1465            }
     1466        }
     1467        if ($status) {
     1468            my @files = ();
     1469            # delete the temporary image datafiles
     1470            if ($mode eq "goto_purged") {
     1471                # additional files to remove for 'purge' mode
     1472                addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
     1473                addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
     1474            }
     1475            # actual command to delete the files
     1476            $status = &delete_files (\@files);
     1477        }
     1478       
     1479        if ($status)  {
     1480            my $command = "$dettool -updatenormalizedexp -det_id $stage_id -iteration $iteration";
     1481            if ($mode eq "goto_cleaned") {
     1482                $command .= " -data_state cleaned";
     1483            }
     1484            if ($mode eq "goto_scrubbed") {
     1485                $command .= " -data_state scrubbed";
     1486            }
     1487            if ($mode eq "goto_purged") {
     1488                $command .= " -data_state purged";
     1489            }
     1490            $command .= " -dbname $dbname" if defined $dbname;
     1491            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1492                run(command => $command, verbose => $verbose);
     1493            unless ($success) {
     1494                print STDERR " residexp had an issue setting the state:? $success $error_code\n";
     1495                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1496                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1497            }
     1498        } else {
     1499            my $command = "$dettool -updatenormalizedexp -det_id $stage_id -exp_id $exp_id -iteration $iteration -data_state $error_state";
     1500            $command .= " -dbname $dbname" if defined $dbname;
     1501           
     1502            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1503                run(command => $command, verbose => $verbose);
     1504            unless ($success) {
     1505                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1506                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1507            }
     1508            exit $PS_EXIT_UNKNOWN_ERROR;
     1509        }
     1510    }
     1511    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1512
     1513    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1514    $command .= " -dbname $dbname" if defined $dbname;
     1515    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1516    unless ($success) {
     1517        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1518        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1519    }
     1520    if (@$stdout_buf != 0) {
     1521        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1522            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1523        $exps = parse_md_list($metadata) or
     1524            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1525       
     1526        foreach my $exp (@$exps) {
     1527            my $iteration = $exp->{iteration};
     1528            my $command;
     1529            if ($mode eq "goto_cleaned") {
     1530                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1531            }
     1532            if ($mode eq "goto_scrubbed") {
     1533                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1534            }
     1535            if ($mode eq "goto_purged") {
     1536                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1537            }
     1538            $command .= " -dbname $dbname" if defined $dbname;
     1539            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1540                run(command => $command, verbose => $verbose);
     1541            unless ($success) {
     1542                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1543                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1544            }
     1545        }
     1546    }
     1547    exit 0;
     1548}
     1549if ($stage eq "detrend.resid.imfile") {
     1550
     1551    die "--stage_id required for stage $stage\n" if !$stage_id;
     1552    ### select the imfiles for this entry
     1553
     1554    # this stage uses 'dettool'
     1555    my $dettool = can_run('dettool') or die "Can't find dettool";
     1556
     1557    # Get list of component imfiles
     1558    # XXX may need a different my_die for each stage
     1559    my $imfiles;                      # Array of component files
     1560    my $command = "$dettool -pendingcleanup_residimfile -det_id $stage_id"; # Command to run
     1561    $command .= " -dbname $dbname" if defined $dbname;
     1562    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1563    unless ($success) {
     1564        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1565        &my_die("Unable to perform dettool: $error_code", "detrend.process.imfile", $stage_id, $error_code);
     1566    }
     1567
     1568    if (@$stdout_buf == 0) {
     1569        exit 0;
     1570    }
     1571
     1572    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1573        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1574
     1575    # extract the metadata for the files into a hash list
     1576    $imfiles = parse_md_list($metadata) or
     1577        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1578
     1579    # loop over all of the imfiles, determine the path_base and class_id for each
     1580    foreach my $imfile (@$imfiles) {
     1581        my $class_id = $imfile->{class_id};
     1582        my $iteration = $imfile->{iteration};
     1583        my $exp_id = $imfile->{exp_id};
     1584        my $path_base = $imfile->{path_base};
     1585        my $status = 1;
     1586
     1587        # don't clean up unless the data needed to update is available
     1588        # modes goto_purged and goto_scrubbed will remove files even if the config is non-existent
     1589        # goto_scrubbed now requires the config file to not exist.
     1590       
     1591        # Possibly not the correct config file, but simtest doesn't leave any around to check.
     1592        if ($mode eq "goto_cleaned") {
     1593            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     1594
     1595            unless ($ipprc->file_exists($config_file)) {
     1596                print STDERR "skipping cleanup for $stage $stage_id $class_id "
     1597                    . " because config file is missing\n";
     1598                $status = 0;
     1599            }
     1600        }
     1601        elsif ($mode eq "goto_scrubbed") {
     1602            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
     1603
     1604            if ($ipprc->file_exists($config_file)) {
     1605                print STDERR "skipping scrubbed for $stage $stage_id $class_id "
     1606                    . " because config file is present\n";
     1607                $status = 0;
     1608            }
     1609        }
     1610
     1611        if ($status) {
     1612            # array of actual filenames to delete
     1613            my @files = ();
     1614
     1615            # delete the temporary image datafiles
     1616            addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
     1617            if ($mode eq "goto_purged") {
     1618                # additional files to remove for 'purge' mode
     1619                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
     1620                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
     1621                addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
     1622            }
     1623
     1624            # actual command to delete the files
     1625            $status = &delete_files (\@files);
     1626        }
     1627
     1628        if ($status)  {
     1629            my $command = "$dettool -updateresidimfile -det_id $stage_id -exp_id $exp_id -iteration $iteration -class_id $class_id";
     1630            if ($mode eq "goto_purged") {
     1631                $command .= " -data_state purged";
     1632            }
     1633            elsif ($mode eq "goto_cleaned") {
     1634                $command .= " -data_state cleaned";
     1635            }
     1636            elsif ($mode eq "goto_scrubbed") {
     1637                $command .= " -data_state scrubbed";
     1638            }
     1639
     1640            $command .= " -dbname $dbname" if defined $dbname;
     1641
     1642            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1643                    run(command => $command, verbose => $verbose);
     1644            unless ($success) {
     1645                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1646                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1647            }
     1648        } else {
     1649            my $command = "$dettool -updateresidimfile -det_id $stage_id -exp_id $exp_id -iteration $iteration -class_id $class_id -data_state $error_state";
     1650            $command .= " -dbname $dbname" if defined $dbname;
     1651
     1652            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1653                    run(command => $command, verbose => $verbose);
     1654            unless ($success) {
     1655                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1656                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1657            }
     1658        }
     1659    }
     1660    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1661
     1662    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1663    $command .= " -dbname $dbname" if defined $dbname;
     1664    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 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    if (@$stdout_buf != 0) {
     1670        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1671            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1672        my $exps = parse_md_list($metadata) or
     1673            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1674       
     1675        foreach my $exp (@$exps) {
     1676            my $iteration = $exp->{iteration};
     1677            my $command;
     1678            if ($mode eq "goto_cleaned") {
     1679                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1680            }
     1681            if ($mode eq "goto_scrubbed") {
     1682                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1683            }
     1684            if ($mode eq "goto_purged") {
     1685                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1686            }
     1687            $command .= " -dbname $dbname" if defined $dbname;
     1688            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1689                run(command => $command, verbose => $verbose);
     1690            unless ($success) {
     1691                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1692                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1693            }
     1694        }
     1695    }
     1696
     1697    exit 0;
     1698}
     1699if ($stage eq "detrend.resid.exp") {
     1700    die "--stage_id required for stage $stage\n" if !$stage_id;
     1701    # this stage uses 'camtool'
     1702    my $dettool = can_run('dettool') or die "Can't find dettool";
     1703
     1704    # Get list of component imfiles
     1705    # XXX may need a different my_die for each stage
     1706    my $exps;                      # Array of component files
     1707    my $command = "$dettool -pendingcleanup_residexp -det_id $stage_id"; # Command to run
     1708    $command .= " -dbname $dbname" if defined $dbname;
     1709    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1710    unless ($success) {
     1711        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1712        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1713    }
     1714    # This is a hack to bomb out until I can diagnose why pantasks wants to keep running this
     1715    if (@$stdout_buf == 0) {
     1716        exit 0;
     1717    }
     1718    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1719        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1720
     1721    $exps = parse_md_list($metadata) or
     1722        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     1723
     1724    foreach my $exp (@$exps) {
     1725        my $exp_id = $exp->{exp_id};
     1726        my $iteration = $exp->{iteration};
     1727        my $path_base = $exp->{path_base};
     1728#       my $class_id  = $exp->{class_id}
     1729        my $status = 1;
     1730        # don't clean up unless the data needed to update is available
     1731        # goto_scrubbed now requires the config file to not be present
     1732        if ($mode eq "goto_cleaned") {
     1733            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1734           
     1735            unless ($ipprc->file_exists($config_file)) {
     1736                print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
     1737                $status = 0;
     1738            }
     1739        }
     1740        elsif ($mode eq "goto_scrubbed") {
     1741            my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
     1742           
     1743            if ($ipprc->file_exists($config_file)) {
     1744                print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
     1745                $status = 0;
     1746            }
     1747        }
     1748        if ($status) {
     1749            my @files = ();
     1750            # delete the temporary image datafiles
     1751            if ($mode eq "goto_purged") {
     1752                # additional files to remove for 'purge' mode
     1753                addFilename (\@files, "PPIMAGE.JPEG1", $path_base);#, $class_id);
     1754                addFilename (\@files, "PPIMAGE.JPEG2", $path_base);#, $class_id);
     1755            }
     1756            # actual command to delete the files
     1757            $status = &delete_files (\@files);
     1758        }
     1759       
     1760        if ($status)  {
     1761            my $command = "$dettool -updateresidexp -det_id $stage_id -exp_id $exp_id -iteration $iteration";
     1762            if ($mode eq "goto_cleaned") {
     1763                $command .= " -data_state cleaned";
     1764            }
     1765            if ($mode eq "goto_scrubbed") {
     1766                $command .= " -data_state scrubbed";
     1767            }
     1768            if ($mode eq "goto_purged") {
     1769                $command .= " -data_state purged";
     1770            }
     1771            $command .= " -dbname $dbname" if defined $dbname;
     1772            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1773                run(command => $command, verbose => $verbose);
     1774            unless ($success) {
     1775                print STDERR " residexp had an issue setting the state:? $success $error_code\n";
     1776                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1777                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1778            }
     1779        } else {
     1780            my $command = "$dettool -updateresidexp -det_id $stage_id -exp_id $exp_id -iteration $iteration -data_state $error_state";
     1781            $command .= " -dbname $dbname" if defined $dbname;
     1782           
     1783            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1784                run(command => $command, verbose => $verbose);
     1785            unless ($success) {
     1786                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1787                &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     1788            }
     1789            exit $PS_EXIT_UNKNOWN_ERROR;
     1790        }
     1791    }
     1792    # Check to see if we can mark the whole detRunSummary object as cleaned.
     1793
     1794    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id";
     1795    $command .= " -dbname $dbname" if defined $dbname;
     1796    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
     1797    unless ($success) {
     1798        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1799        &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1800    }
     1801    if (@$stdout_buf != 0) {
     1802        $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     1803            &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1804        $exps = parse_md_list($metadata) or
     1805            &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
     1806       
     1807        foreach my $exp (@$exps) {
     1808            my $iteration = $exp->{iteration};
     1809            my $command;
     1810            if ($mode eq "goto_cleaned") {
     1811                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
     1812            }
     1813            if ($mode eq "goto_scrubbed") {
     1814                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
     1815            }
     1816            if ($mode eq "goto_purged") {
     1817                $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
     1818            }
     1819            $command .= " -dbname $dbname" if defined $dbname;
     1820            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1821                run(command => $command, verbose => $verbose);
     1822            unless ($success) {
     1823                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1824                &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     1825            }
     1826        }
     1827    }
     1828    exit 0;
     1829}
     1830
    5651831
    5661832die "ipp_cleanup.pl -stage $stage not yet implemented\n";
     
    5691835{
    5701836    my $files = shift; # reference to a list of files to unlink
     1837#     my $test_verbose = 1;
     1838   
     1839#     if ($test_verbose == 1) {
     1840#       open(TMPLOG,">>/tmp/czw.cleanup.log");
     1841#       flock(TMPLOG,2);
     1842#     }
    5711843
    5721844    # this script is, of course, very dangerous.
    5731845    foreach my $file (@$files) {
    574         print STDERR "unlinking $file\n";
     1846        print STDERR "unlinking $stage $stage_id $file";
     1847        unless ($ipprc->file_exists($file)) {
     1848            print STDERR "\t File not found\n";
     1849        }
     1850        else {
     1851            print STDERR "\n";
     1852        }
     1853
     1854#       if ($test_verbose == 1) {
     1855#           print TMPLOG "$stage $stage_id $file";
     1856           
     1857#           else {
     1858#               print TMPLOG "\n";
     1859#           }
     1860#       }
     1861
    5751862        $ipprc->file_delete($file);
    5761863    }
     1864
     1865#     if ($test_verbose == 1) {
     1866#       flock(TMPLOG,8);
     1867#       close(TMPLOG);
     1868#     }
     1869
    5771870    return 1;
    5781871}
Note: See TracChangeset for help on using the changeset viewer.