Index: /branches/czw_branch/20100427/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- /branches/czw_branch/20100427/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 28030)
+++ /branches/czw_branch/20100427/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 28031)
@@ -632,21 +632,23 @@
     if (($img_type eq "raw") or ($img_type eq "chip")) {
 
-        my $runs = lookup_runs_by_cam_id_and_coords($ipprc, $imagedb, $img_type,
+        my $chips = lookup_by_cam_id_and_coords($ipprc, $imagedb, $img_type,
             $ra, $dec, $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose);
 
-        # lookup is going to filter these we don't need to do it here
-#        $runs = filterRuns($img_type, $need_magic, $runs, $verbose);
-        foreach my $run (@$runs) {
-            next if $component and ($run->{component} ne $component);
-            my $these_results = lookup($ipprc, $rowList, $imagedb, "byid", $img_type, $run->{id},
-                $tess_id, $run->{component}, $need_magic, 
-                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
-
-            next if !$these_results;
-            push @$results, @$these_results;
+        if (!$chips or scalar @$chips == 0) {
+            setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
+        } else {
+            foreach my $chip (@$chips) {
+                next if $component and ($chip->{component} ne $component);
+                my $these_results = lookup($ipprc, $rowList, $imagedb, "byid", $img_type, $chip->{id},
+                    $tess_id, $chip->{component}, $need_magic, 
+                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
+
+                next if !$these_results;
+                push @$results, @$these_results;
+            }
         }
         
     } else {
-        # this should be checked elsewhere
+        # this should have been checked elsewhere
         die "unexpected image type $img_type" if ($img_type ne "warp") 
                                               and ($img_type ne "stack") and ($img_type ne "diff");
@@ -654,11 +656,15 @@
         my $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
 
-        foreach my $skycell (@$skycells) {
-            my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $img_type, undef,
-                $skycell->{tess_id}, $skycell->{component}, $need_magic, 
-                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
-
-            next if !$these_results;
-            push @$results, @$these_results;
+        if (!$skycells or scalar @$skycells == 0) {
+            setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
+        } else {
+            foreach my $skycell (@$skycells) {
+                my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $img_type, undef,
+                    $skycell->{tess_id}, $skycell->{component}, $need_magic, 
+                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
+
+                next if !$these_results;
+                push @$results, @$these_results;
+            }
         }
     }
@@ -667,8 +673,9 @@
 }
 
-# lookup_runs_by_cam_id_and_coords
-# given an ra, dec, and optionally other paramters, find camera runs for exposures
+# lookup__by_cam_id_and_coords
+# given an ra, dec, and optionally other parameters, find camera runs for exposures
 # that are within some distance of the coordinates
-sub lookup_runs_by_cam_id_and_coords {
+
+sub lookup_by_cam_id_and_coords {
     my $ipprc      = shift;
     my $imagedb   = shift;
@@ -686,6 +693,8 @@
 
     {
+        my $search_radius = 1.6; # XXX: this should be camera specific
+
         my $command = "$camtool -dbname $imagedb -processedexp -pstamp_order";
-           $command .= " -ra $ra -decl $dec -radius 1.6";
+           $command .= " -ra $ra -decl $dec -radius $search_radius";
            $command .= " -dateobs_begin $dateobs_begin" if $dateobs_begin;
            $command .= " -dateobs_end   $dateobs_end"   if $dateobs_end  ;
@@ -693,5 +702,5 @@
            # NOTE: we are applying the data_group to the camera run.
            # If we're looking for chip stage images there is no guarentee that 
-           # chipRun.data_group = camRun.data_group. In practice this is almost
+           # chipRun.data_group eq camRun.data_group. In practice this is almost
            # always the case. If this turns out to be a problem we can defer
            # the data_group test to when we look up the chipProcessedImfiles
@@ -705,5 +714,5 @@
         return undef;
     }
-    my $runs;
+    my $components;
     my $last_exp_id = 0;
     foreach my $camRun (@$camruns) {
@@ -774,26 +783,29 @@
 
         # build the hash to return
-        my $run = {
+        # XXX: very few of the entries in this hash are currently used.
+        # Used are: id, component
+        # I had plans to use it for the camRun lookup optimization, but the cache solves the
+        # problem
+        my $comp = {
             exp_id    => $camRun->{exp_id},
             exp_name  => $camRun->{exp_name},
             chip_id   => $camRun->{chip_id},
             cam_id    => $camRun->{cam_id},
-            astrom    => $astrom_resolved,  # XXX: is astrom used?
             class_id  => $class_id,
             component => $class_id
         };
         if ($img_type eq "chip") {
-            $run->{id} = $camRun->{chip_id};
-            $run->{state} = $camRun->{chip_state};
-            $run->{magicked} = $camRun->{chip_magicked};
-        } else {
-            $run->{id} = $camRun->{exp_id};
-            $run->{state} = 'full';
-            $run->{magicked} = $camRun->{raw_magicked};
-        }
-        push @$runs, $run;
-    }
-
-    return $runs;
+            $comp->{id} = $camRun->{chip_id};
+            $comp->{state} = $camRun->{chip_state};
+            $comp->{magicked} = $camRun->{chip_magicked};
+        } else {
+            $comp->{id} = $camRun->{exp_id};
+            $comp->{state} = 'full';
+            $comp->{magicked} = $camRun->{raw_magicked};
+        }
+        push @$components, $comp;
+    }
+
+    return $components;
 }
 
Index: /branches/czw_branch/20100427/ippScripts/scripts/chip_imfile.pl
===================================================================
--- /branches/czw_branch/20100427/ippScripts/scripts/chip_imfile.pl	(revision 28030)
+++ /branches/czw_branch/20100427/ippScripts/scripts/chip_imfile.pl	(revision 28031)
@@ -76,4 +76,6 @@
     defined $run_state;
 
+my_die ("$run_state is an invalid value for run-state", $exp_id, $chip_id, $class_id, $PS_EXIT_PROG_ERROR) unless ($run_state eq 'new' or $run_state eq 'update');
+
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $exp_id, $chip_id, $class_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 
@@ -201,11 +203,22 @@
 
         my $burntoolStateGood;
+        my $burntoolStateGoodUpdate;
         foreach my $camEntry (@$camData) {
             if ($camEntry->{name} eq "BURNTOOL.STATE.GOOD") {
                 $burntoolStateGood = $camEntry->{value};
             }
-        }
-
-        print "burntool state vs burntoolStateGood : $burntoolState vs $burntoolStateGood\n";
+            if ($camEntry->{name} eq "BURNTOOL.STATE.GOOD.UPDATE") {
+                $burntoolStateGoodUpdate = $camEntry->{value};
+            }
+        }
+
+        if ($run_state eq 'new') {
+            print "burntool state vs burntoolStateGood : $burntoolState vs $burntoolStateGood\n";
+        } else {
+            # if doing update go ahead if burntoolState is at least at BURNTOOL.STATE.GOOD.UPDATE
+            print "burntool state vs burntoolStateGoodUpdate : $burntoolState vs $burntoolStateGoodUpdate\n";
+            $useBestBurntool = 0 if (abs($burntoolState) >= $burntoolStateGoodUpdate);
+        }
+
         if (abs($burntoolState) != $burntoolStateGood) {
             if ($useBestBurntool) {
Index: /branches/czw_branch/20100427/ippToPsps/config/detection/tables.xml
===================================================================
--- /branches/czw_branch/20100427/ippToPsps/config/detection/tables.xml	(revision 28030)
+++ /branches/czw_branch/20100427/ippToPsps/config/detection/tables.xml	(revision 28031)
@@ -163,4 +163,5 @@
     <column name="projectionCellID" type="TLONG" default="-999" comment="projection cell identifier at discovery time"></column>
     <column name="dataRelease" type="TBYTE" default="0" comment="Data release to propagate to the object"></column>
+    <column name="surveyID" type="TBYTE" default="255" comment="surveyID &lt;/coluumn&gt;"></column>
   </table>
   <table name="ObjectCalColor">
Index: /branches/czw_branch/20100427/ippToPsps/scripts/convertPhotCodesToXml.pl
===================================================================
--- /branches/czw_branch/20100427/ippToPsps/scripts/convertPhotCodesToXml.pl	(revision 28030)
+++ /branches/czw_branch/20100427/ippToPsps/scripts/convertPhotCodesToXml.pl	(revision 28031)
@@ -52,4 +52,5 @@
         my $photCode = $columns[1];
         my $zeroPoint = $columns[4];
+        my $extinction = $columns[5];
 
         $writer->startTag('row',
@@ -59,5 +60,5 @@
                 "AB" => "0.0", # TODO
                 "zeropoint" => $zeroPoint,
-                "extinction" => "0.0", # TODO
+                "extinction" => $extinction,
                 "colorterm" => "0.0", # TODO
                 "colorExtn" => "0.0", # TODO
Index: /branches/czw_branch/20100427/ippToPsps/scripts/ippToPsps_run.pl
===================================================================
--- /branches/czw_branch/20100427/ippToPsps/scripts/ippToPsps_run.pl	(revision 28030)
+++ /branches/czw_branch/20100427/ippToPsps/scripts/ippToPsps_run.pl	(revision 28031)
@@ -16,4 +16,5 @@
 # TODO temporary until we use database to store current jobid
 my $jobIdFilePath = "./config/currentJobId.txt";
+my $logFile = "./config/logfile.txt";
 
 # default values for certain globals
@@ -27,4 +28,5 @@
 $singleExpId = undef;
 $datastoreProduct = "PSPS_test";
+
 
 # get user args
@@ -166,4 +168,6 @@
     $query->execute;
 
+    open (LOGFILE, ">> $logFile");
+
     # loop round exposures
     while (my @row = $query->fetchrow_array()) {
@@ -215,4 +219,7 @@
                 }
 
+                # write to the log
+                print LOGFILE "$jobId $expId\n";
+
                 $jobId++;
                 $batchId = 0;
@@ -232,4 +239,5 @@
 
     $db->disconnect();
+    close (LOGFILE);
 
     return 1;
Index: /branches/czw_branch/20100427/ippToPsps/src/ippToPspsBatchDetection.c
===================================================================
--- /branches/czw_branch/20100427/ippToPsps/src/ippToPspsBatchDetection.c	(revision 28030)
+++ /branches/czw_branch/20100427/ippToPsps/src/ippToPspsBatchDetection.c	(revision 28031)
@@ -336,4 +336,5 @@
             fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_OBJID, 1, 1, nDet, objID, &status);
             fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_IPPOBJID, 1, 1, nDet, ippObjID, &status);
+            fits_write_col(this->fitsOut, TBYTE, SKINNYOBJECT_SURVEYID, 1, 1, nDet, surveyIDs, &status);
             if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
 
Index: /branches/czw_branch/20100427/ippToPsps/src/ippToPspsDetEnums.h
===================================================================
--- /branches/czw_branch/20100427/ippToPsps/src/ippToPspsDetEnums.h	(revision 28030)
+++ /branches/czw_branch/20100427/ippToPsps/src/ippToPspsDetEnums.h	(revision 28031)
@@ -167,4 +167,5 @@
   SKINNYOBJECT_PROJECTIONCELLID = 3,
   SKINNYOBJECT_DATARELEASE = 4,
+  SKINNYOBJECT_SURVEYID = 5,
 } SkinnyObject;
 
Index: /branches/czw_branch/20100427/ippTools/share/chiptool_setimfiletoupdate.sql
===================================================================
--- /branches/czw_branch/20100427/ippTools/share/chiptool_setimfiletoupdate.sql	(revision 28030)
+++ /branches/czw_branch/20100427/ippTools/share/chiptool_setimfiletoupdate.sql	(revision 28031)
@@ -1,4 +1,7 @@
 UPDATE chipRun
     JOIN chipProcessedImfile USING(chip_id)
+    LEFT JOIN magicDSRun ON (stage_id = chip_id AND stage = 'chip')
+    LEFT JOIN magicDSFile ON (magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
+                              AND component = class_id)
 SET chipRun.state = 'update', 
     chipProcessedImfile.data_state = 'update'
@@ -7,2 +10,6 @@
     AND (chipRun.state = 'cleaned' OR chipRun.state = 'update')
     AND (chipProcessedImfile.data_state = 'cleaned')
+    -- don't queue update if the associated magicDSFile exists
+    AND (chipRun.magicked = 0 
+      OR (magicDSRun.state = 'cleaned' OR magicDSRun.state = 'new')
+            AND magicDSFile.component IS NULL)
Index: /branches/czw_branch/20100427/ippTools/share/difftool_setskyfiletoupdate.sql
===================================================================
--- /branches/czw_branch/20100427/ippTools/share/difftool_setskyfiletoupdate.sql	(revision 28030)
+++ /branches/czw_branch/20100427/ippTools/share/difftool_setskyfiletoupdate.sql	(revision 28031)
@@ -1,4 +1,7 @@
 UPDATE diffRun
     JOIN diffSkyfile USING(diff_id)
+    LEFT JOIN magicDSRun ON (stage_id = diff_id AND stage = 'diff')
+    LEFT JOIN magicDSFile ON (magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
+                              AND component = skycell_id)
 SET diffRun.state = 'update', 
     diffSkyfile.data_state = 'update'
@@ -7,2 +10,6 @@
     AND (diffRun.state = 'cleaned' OR diffRun.state = 'update')
     AND (diffSkyfile.data_state = 'cleaned')
+    -- don't queue update if the associated magicDSFile still exists
+    AND (diffRun.magicked = 0 
+      OR (magicDSRun.state = 'cleaned' OR magicDSRun.state = 'new')
+            AND magicDSFile.component IS NULL)
Index: /branches/czw_branch/20100427/ippTools/share/warptool_setskyfiletoupdate.sql
===================================================================
--- /branches/czw_branch/20100427/ippTools/share/warptool_setskyfiletoupdate.sql	(revision 28030)
+++ /branches/czw_branch/20100427/ippTools/share/warptool_setskyfiletoupdate.sql	(revision 28031)
@@ -1,4 +1,7 @@
 UPDATE warpRun
     JOIN warpSkyfile USING(warp_id)
+    LEFT JOIN magicDSRun ON (stage_id = warp_id AND stage = 'warp')
+    LEFT JOIN magicDSFile ON (magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
+                              AND component = skycell_id)
 SET warpRun.state = 'update', 
     warpSkyfile.data_state = 'update'
@@ -7,2 +10,5 @@
     AND (warpRun.state = 'cleaned' OR warpRun.state = 'update')
     AND (warpSkyfile.data_state = 'cleaned')
+    AND (warpRun.magicked = 0
+      OR (magicDSRun.state = 'cleaned' OR magicDSRun.state = 'new')
+      AND magicDSFile.component IS NULL)
Index: /branches/czw_branch/20100427/ippconfig/gpc1/camera.config
===================================================================
--- /branches/czw_branch/20100427/ippconfig/gpc1/camera.config	(revision 28030)
+++ /branches/czw_branch/20100427/ippconfig/gpc1/camera.config	(revision 28031)
@@ -150,5 +150,7 @@
 PHOTCODE.RULE           STR     {DETECTOR}.{FILTER.ID}.{CHIP.NAME}	# Rule for generating photcode
 
-BURNTOOL.STATE.GOOD     S16     14  # Value for burntool_state with the most recent bt version.
+BURNTOOL.STATE.GOOD        S16  14  # Value for burntool_state with the most recent bt version.
+BURNTOOL.STATE.GOOD.UPDATE S16  13  # for upddate processing accept earlier version
+
 FOV                     F32     15000
 NPIX_INTERCHIP          S32     0
Index: /branches/czw_branch/20100427/ippconfig/recipes/filerules-split.mdc
===================================================================
--- /branches/czw_branch/20100427/ippconfig/recipes/filerules-split.mdc	(revision 28030)
+++ /branches/czw_branch/20100427/ippconfig/recipes/filerules-split.mdc	(revision 28031)
@@ -155,5 +155,6 @@
 PPIMAGE.CHIP.VARIANCE        OUTPUT {OUTPUT}.{CHIP.NAME}.ch.wt.fits   VARIANCE        COMP_WT    CHIP       TRUE      NONE
 PPIMAGE.CHIP.RESID           OUTPUT {OUTPUT}.{CHIP.NAME}.ch.fits      IMAGE           NONE       CHIP       TRUE      NONE
-                                                                                                              
+PPIMAGE.CHIP.UNCOMPRESSED    OUTPUT {OUTPUT}.{CHIP.NAME}.ch.fits      IMAGE	      NONE	 CHIP	    TRUE      NONE
+
 PPIMAGE.OUTPUT.FPA1          OUTPUT {OUTPUT}.b1.fits                  IMAGE           NONE       FPA        TRUE      NONE
 PPIMAGE.OUTPUT.FPA2          OUTPUT {OUTPUT}.b2.fits                  IMAGE           NONE       FPA        TRUE      NONE
@@ -229,5 +230,6 @@
 PSWARP.BIN2                  OUTPUT {OUTPUT}.b2.fits                  IMAGE           NONE       FPA        TRUE      NONE
 PSWARP.CONFIG                OUTPUT {OUTPUT}.pswarp.mdc               TEXT            NONE       FPA        TRUE      NONE
-                                                                                                              
+PSWARP.OUTPUT.UNCOMPRESSED   OUTPUT {OUTPUT}.fits		      IMAGE	      NONE	 FPA	    TRUE      NONE
+                                                                                                   
 SKYCELL.STATS                OUTPUT {OUTPUT}.stats                    STATS           NONE       FPA        TRUE      NONE
 SKYCELL.TEMPLATE             OUTPUT {OUTPUT}.skycell                  SKYCELL         NONE       FPA        TRUE      NONE
Index: /branches/czw_branch/20100427/ippconfig/recipes/psphot.config
===================================================================
--- /branches/czw_branch/20100427/ippconfig/recipes/psphot.config	(revision 28030)
+++ /branches/czw_branch/20100427/ippconfig/recipes/psphot.config	(revision 28031)
@@ -342,4 +342,5 @@
 # Recipe overrides for DIFF
 DIFF	METADATA
+  OUTPUT.FORMAT		STR   PS1_DV1
 END
 
Index: /branches/czw_branch/20100427/ppViz/src/ppVizPattern/ppVizPatternLoop.c
===================================================================
--- /branches/czw_branch/20100427/ppViz/src/ppVizPattern/ppVizPatternLoop.c	(revision 28030)
+++ /branches/czw_branch/20100427/ppViz/src/ppVizPattern/ppVizPatternLoop.c	(revision 28031)
@@ -80,4 +80,11 @@
                 }
 
+                // Remove bias sections to avoid warnings
+                psMetadataItem *biassec = psMetadataLookup(readout->parent->concepts, "CELL.BIASSEC");
+                if (psListLength(biassec->data.V)) {
+                    psFree(biassec->data.V);
+                    biassec->data.V = psListAlloc(NULL);
+                }
+
                 readout->data_exists = true;
                 readout->parent->data_exists = true;
Index: /branches/czw_branch/20100427/pstamp/scripts/pstampparse.pl
===================================================================
--- /branches/czw_branch/20100427/pstamp/scripts/pstampparse.pl	(revision 28030)
+++ /branches/czw_branch/20100427/pstamp/scripts/pstampparse.pl	(revision 28031)
@@ -521,48 +521,5 @@
     my $dep_id;
 
-    # XXX: this code is repeated in queueGetImageJobs we should encapsulate it in a subroutine and share it
-    if ($stage ne 'raw') {
-        # updates for stack stage not supported yet
-        my $allow_wait_for_update = ($stage ne 'stack');
-        my $run_state = $image->{state};
-        my $data_state = $image->{data_state};
-        $data_state = $run_state if $stage eq 'stack';
-        if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
-            ($run_state eq 'drop') or 
-            ($run_state eq 'error_cleaned') or ($data_state eq 'error_cleaned') or
-            ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
-            # image is gone and it's not coming back
-            $newState = 'stop';
-            $fault = $PSTAMP_GONE;
-        } elsif (($data_state ne 'full') or ($need_magic and ($image->{magicked} < 0))) {
-            if ($stage eq 'chip') {
-                my $burntool_state = $image->{burntool_state};
-                if ($burntool_state and (abs($burntool_state) < 14)) {
-                    $newState = 'stop';
-                    $fault = $PSTAMP_NOT_AVAILABLE;
-                }
-            } 
-            if (!$allow_wait_for_update) {
-                print STDERR "wait for update not supported for stage $stage yet\n";
-                $newState = 'stop';
-                $fault = $PSTAMP_NOT_AVAILABLE;
-            }
-            if (!$fault) {
-                # wait for update unless the customer asks us not to
-                if (($option_mask & $PSTAMP_NO_WAIT_FOR_UPDATE)) {
-                    $newState = 'stop';
-                    $fault = $PSTAMP_NOT_AVAILABLE;
-                } elsif (!$image->{magicked}) {
-                    $newState = 'stop';
-                    $fault = $PSTAMP_NOT_DESTREAKED;
-                } else {
-                    # cause the image to be re-made
-                    # set up to queue an update run
-                    queue_update_run(\$newState, \$fault, \$dep_id, $image->{imagedb}, 
-                        $run_state, $stage, $image->{stage_id}, $image->{component}, $need_magic);
-                }
-            }
-        }
-    }
+    queueUpdatesIfNeeded($stage, $image, $option_mask, \$newState, \$fault, \$dep_id);
 
     my $command = "$pstamptool -addjob  -req_id $req_id -job_type $row->{JOB_TYPE}"
@@ -704,26 +661,5 @@
         my $dep_id;
 
-        if ($stage ne 'raw') {
-            my $run_state = $image->{state};
-            my $data_state = $image->{data_state};
-            $data_state = $run_state if $stage eq "stack";
-            if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
-                ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
-                # image is gone and it's not coming back
-                $newState = 'stop';
-                $fault = $PSTAMP_GONE;
-            } elsif (($data_state ne 'full') or ($need_magic and ($image->{magicked} < 0))) {
-                # wait for update unless the customer asks us to not to
-                if ($option_mask & $PSTAMP_NO_WAIT_FOR_UPDATE) {
-                    $newState = 'stop';
-                    $fault = $PSTAMP_NOT_AVAILABLE;
-                } else {
-                    # cause the image to be re-made
-                    # set up to queue an update run
-                    queue_update_run(\$newState, \$fault, \$dep_id, $image->{imagedb}, 
-                        $run_state, $stage, $image->{stage_id}, $image->{component}, $need_magic);
-                }
-            }
-        }
+        queueUpdatesIfNeeded($stage, $image, $option_mask, \$newState, \$fault, \$dep_id);
 
         $num_jobs++;
@@ -864,15 +800,16 @@
 }
 
-sub queue_update_run 
+sub get_dependent 
 {
     my ($r_jobState, $r_fault, $r_dep_id, $imagedb, $state, $stage, $stage_id, $component, $need_magic) = @_;
 
-    # XXX: The update process for warp and subsequent stages requires 
-    # destreaking to run because the -pending queries require it when magicked > 0
-    # queries. 
-    # The case of stack-stack diffs is taken care of in pstamp_checkdependent
+    # XXX: The update process for warp and subsequent stages requires # destreaking to be performed
+    # because the -pending queries require the inputs to have magicked >= 0
+    # The case of stack-stack diffs not needing to be destreaked is taken care of in pstamp_checkdependent
     $need_magic = 1 if $imagedb eq 'gpc1';
 
-    if (($state ne 'cleaned') and ($state ne 'update') and ($state ne 'goto_cleaned')) {
+    # chipRun's can be in full state if destreaking is necessary
+    if (($state ne 'cleaned') and ($state ne 'update') and ($state ne 'goto_cleaned') and 
+        ($stage eq 'chip' and $state eq 'full')) {
         my_die("$stage $stage_id is in unexpected state $state", $PS_EXIT_PROG_ERROR);
     }
@@ -910,4 +847,50 @@
 }
 
+sub queueUpdatesIfNeeded {
+    my $stage = shift;
+    my $image = shift;
+    my $option_mask = shift;
+    my $r_newState = shift;
+    my $r_fault = shift;
+    my $r_dep_id = shift;
+
+    if ($stage ne 'raw') {
+        my $run_state = $image->{state};
+        my $data_state = $image->{data_state};
+        $data_state = $run_state if $stage eq 'stack';
+        if (($run_state =~ /purged/) or ($run_state =~ /scrubbed/) or ($run_state eq 'drop')) {
+            # image is gone and it's not coming back
+            $$r_newState = 'stop';
+            $$r_fault = $PSTAMP_GONE;
+        } elsif (($run_state eq "error_cleaned") or ($data_state eq 'error_cleaned')) {
+            # if cleanup had an error don't get the user's hopes up set fault to gone
+            $$r_newState = 'stop';
+            $$r_fault = $PSTAMP_GONE;
+        } elsif (($data_state ne 'full') or ($need_magic and ($image->{magicked} < 0))) {
+   
+            if ($stage eq 'stack') {
+                print STDERR "Stamps cannot be made from cleaned stacks.\n";
+                $$r_newState = 'stop';
+                $$r_fault = $PSTAMP_GONE;
+            }
+            if (!$$r_fault) {
+                # wait for update unless the customer asks us not to
+                if (($option_mask & $PSTAMP_NO_WAIT_FOR_UPDATE)) {
+                    $$r_newState = 'stop';
+                    $$r_fault = $PSTAMP_NOT_AVAILABLE;
+                } elsif (!$image->{magicked}) {
+                    $$r_newState = 'stop';
+                    $$r_fault = $PSTAMP_NOT_DESTREAKED;
+                } else {
+                    # cause the image to be re-made
+                    # set up to queue an update run
+                    get_dependent(\$$r_newState, \$$r_fault, $r_dep_id, $image->{imagedb}, 
+                        $run_state, $stage, $image->{stage_id}, $image->{component}, $need_magic);
+                }
+            }
+        }
+    }
+}
+
 sub write_params {
     my $output_base = shift;
Index: /branches/czw_branch/20100427/pswarp/src/pswarpLoop.c
===================================================================
--- /branches/czw_branch/20100427/pswarp/src/pswarpLoop.c	(revision 28030)
+++ /branches/czw_branch/20100427/pswarp/src/pswarpLoop.c	(revision 28031)
@@ -292,5 +292,7 @@
         psFree(covar);
 
-        psImageCovarianceTransfer(output->variance, output->covariance);
+        if (output->variance) {
+            psImageCovarianceTransfer(output->variance, output->covariance);
+        }
     }
 
Index: /branches/czw_branch/20100427/tools/czartool.pl
===================================================================
--- /branches/czw_branch/20100427/tools/czartool.pl	(revision 28030)
+++ /branches/czw_branch/20100427/tools/czartool.pl	(revision 28031)
@@ -234,4 +234,5 @@
         foreach $distLabel (@distributionLabels) {
             chomp($stdsLabel);
+            chomp($distLabel);
             if ($stdsLabel eq $distLabel) { $distributing = 1; last;}
 
