Index: branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl	(revision 24972)
+++ branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl	(revision 25050)
@@ -767,20 +767,7 @@
     }
 
-#     # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
-#     if (@$stdout_buf == 0)  {
-# 	my $command = "$dettool -det_id $stage_id -updateprocessedimfile -data_state new";
-# 	$command .= " -dbname $dbname" if defined $dbname;
-
-# 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 	    run(command => $command, verbose => $verbose);
-# 	unless ($success) {
-# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-# 	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
-# 	}
-# 	exit 0;
-#     }
-    
-    if (@$stdout_buf == 0) {
-	exit 0; #silently abort. I need to fix this for propers
+    # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
+    if (@$stdout_buf == 0)  {
+	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.
     }
 
@@ -831,17 +818,9 @@
             addFilename (\@files, "PPIMAGE.OUTPUT.MASK", $path_base, $class_id);
             addFilename (\@files, "PPIMAGE.OUTPUT.VARIANCE", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP.MASK", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
             if ($mode eq "goto_purged") {
                 # additional files to remove for 'purge' mode
-#                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base, $class_id);
-#                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.JPEG1", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.JPEG2", $path_base, $class_id);
+		addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
+		addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
                 addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
-#                addFilename (\@files, "PPIMAGE.CONFIG", $path_base, $class_id);
             }
 
@@ -960,5 +939,5 @@
 	# goto_scrubbed now requires the config file to not be present
 	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PSIMAGE.CONFIG", $path_base);
 	    
 	    if (!$config_file or ! -e $config_file) {
@@ -968,5 +947,5 @@
 	}
 	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PSIMAGE.CONFIG", $path_base);
 	    
 	    if ($config_file and -e $config_file) {
@@ -978,10 +957,9 @@
 	    my @files = ();
 	    # delete the temporary image datafiles
-	    addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
+	    # I can't find anything to put here
 	    if ($mode eq "goto_purged") {
 		# additional files to remove for 'purge' mode
 		addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
 		addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
-		addFilename (\@files, "PSASTRO.STATS", $path_base);
 	    }
 	    # actual command to delete the files
@@ -1092,4 +1070,5 @@
 
 	my $status = 1;
+
 # 	if ($mode eq "goto_cleaned") {
 # 	    my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
@@ -1112,7 +1091,18 @@
 # 	    }
 # 	}
+
 	if ($status) {
 	    # delete the temporary image datafiles
-#	    addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
+	    # There's no convenient way to get the detrend type, so I'm queueing all of them for deletion.
+	    # I understand that they all point to the same filename right now, but that may not be true in
+	    # the future.
+	    addFilename(\@files, "PPMERGE.OUTPUT.MASK", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.BIAS", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.DARK", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.SHUTTER", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.FLAT", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.FRINGE", $path_base, $stage_id);
+	    
+
 	    addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $stage_id);
 	    addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $stage_id);
@@ -1120,5 +1110,5 @@
 	    if ($mode eq "goto_purged") {
 		# additional files to remove for 'purge' mode
-		# Nothing?
+#		addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
 	    }
 
@@ -1241,5 +1231,4 @@
 	    }
 	} else {
-	    # since 'camera' has only a single imfile, we can just update the run
 	    my $command = "$dettool -updatenormalizedstat -det_id $stage_id -iteration $iteration -class_id $class_id -data_state $error_state";
 	    $command .= " -dbname $dbname" if defined $dbname;
@@ -1296,5 +1285,5 @@
 if ($stage eq "detrend.norm.imfile") {
     die "--stage_id required for stage $stage\n" if !$stage_id;
-    # this stage uses 'camtool'
+    # this stage uses 'dettool'
     my $dettool = can_run('dettool') or die "Can't find dettool";
 
@@ -1341,12 +1330,12 @@
 	if ($status) {
 	    my @files = ();
-	    # delete the temporary image datafiles
-	    addFilename (\@files, "PPIMAGE.OUTPUT", $path_base);
-	    addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base);
-	    addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base);
+
 	    if ($mode eq "goto_purged") {
 		# additional files to remove for 'purge' mode
-		addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
-		addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
+		addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base);
+		addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base);
+
+		addFilename (\@files, "PPIMAGE.OUTPUT", $path_base);
+		addFilename (\@files, "PPIMAGE.STATS", $path_base);
 	    }
 	    # actual command to delete the files
@@ -1426,5 +1415,5 @@
 if ($stage eq "detrend.norm.exp") {
     die "--stage_id required for stage $stage\n" if !$stage_id;
-    # this stage uses 'camtool'
+    # this stage uses 'dettool'
     my $dettool = can_run('dettool') or die "Can't find dettool";
 
@@ -1439,5 +1428,5 @@
         &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     }
-    # This is a hack to bomb out until I can diagnose why pantasks wants to keep running this
+
     if (@$stdout_buf == 0) {
 	exit 0;
@@ -1458,5 +1447,5 @@
 	# goto_scrubbed now requires the config file to not be present
 	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
 	    
 	    if (!$config_file or ! -e $config_file) {
@@ -1466,5 +1455,5 @@
 	}
 	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
 	    
 	    if ($config_file and -e $config_file) {
@@ -1476,10 +1465,8 @@
 	    my @files = ();
 	    # delete the temporary image datafiles
-#	    addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
 	    if ($mode eq "goto_purged") {
 		# additional files to remove for 'purge' mode
 		addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
 		addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
-#		addFilename (\@files, "PSASTRO.STATS", $path_base);
 	    }
 	    # actual command to delete the files
@@ -1576,20 +1563,6 @@
     }
 
-    # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
-#     if (@$stdout_buf == 0)  {
-# 	my $command = "$dettool -det_id $stage_id -updateresidimfile -data_state new";
-# 	$command .= " -dbname $dbname" if defined $dbname;
-
-# 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-# 	    run(command => $command, verbose => $verbose);
-# 	unless ($success) {
-# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-# 	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
-# 	}
-# 	exit 0;
-#     }
-    
     if (@$stdout_buf == 0) {
-	exit 0; #silently abort. I need to fix this for propers
+	exit 0;
     }
 
@@ -1639,17 +1612,9 @@
             # delete the temporary image datafiles
             addFilename (\@files, "PPIMAGE.OUTPUT", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP.MASK", $path_base, $class_id);
-#            addFilename (\@files, "PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
             if ($mode eq "goto_purged") {
                 # additional files to remove for 'purge' mode
-#                addFilename (\@files, "PPIMAGE.OUTPUT.FPA1", $path_base, $class_id);
-#                addFilename (\@files, "PPIMAGE.OUTPUT.FPA2", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.JPEG1", $path_base, $class_id);
-                addFilename (\@files, "PPIMAGE.JPEG2", $path_base, $class_id);
+		addFilename (\@files, "PPIMAGE.BIN1", $path_base, $class_id);
+		addFilename (\@files, "PPIMAGE.BIN2", $path_base, $class_id);
                 addFilename (\@files, "PPIMAGE.STATS", $path_base, $class_id);
-#                addFilename (\@files, "PPIMAGE.CONFIG", $path_base, $class_id);
             }
 
@@ -1758,10 +1723,10 @@
 	my $iteration = $exp->{iteration};
 	my $path_base = $exp->{path_base};
-
+#	my $class_id  = $exp->{class_id} 
 	my $status = 1;
 	# don't clean up unless the data needed to update is available
 	# goto_scrubbed now requires the config file to not be present
 	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
 	    
 	    if (!$config_file or ! -e $config_file) {
@@ -1771,5 +1736,5 @@
 	}
 	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base);
 	    
 	    if ($config_file and -e $config_file) {
@@ -1781,10 +1746,8 @@
 	    my @files = ();
 	    # delete the temporary image datafiles
-	    addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
 	    if ($mode eq "goto_purged") {
 		# additional files to remove for 'purge' mode
-		addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
-		addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
-		addFilename (\@files, "PSASTRO.STATS", $path_base);
+                addFilename (\@files, "PPIMAGE.JPEG1", $path_base);#, $class_id);
+                addFilename (\@files, "PPIMAGE.JPEG2", $path_base);#, $class_id);
 	    }
 	    # actual command to delete the files
@@ -1876,5 +1839,14 @@
     foreach my $file (@$files) {
         print STDERR "unlinking $file\n";
-	print TMPLOG "$file\n";
+	print TMPLOG "$stage $stage_id $file";
+	my $ff = $file;
+	$ff =~ s%^file://%%;
+	unless (-e $ff) {
+	    print TMPLOG "\t File not found\n";
+	}
+	else {
+	    print TMPLOG "\n";
+	}
+
         $ipprc->file_delete($file);
     }
Index: branches/czw_branch/cleanup/ippScripts/scripts/stack_skycell.pl
===================================================================
--- branches/czw_branch/cleanup/ippScripts/scripts/stack_skycell.pl	(revision 24972)
+++ branches/czw_branch/cleanup/ippScripts/scripts/stack_skycell.pl	(revision 25050)
@@ -65,4 +65,6 @@
 
 my $ipprc = PS::IPP::Config->new() or my_die( "Unable to set up", $stack_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
+$| = 1;
+print "I've set up: $stack_id\n";
 
 # XXX camera is not known here; cannot use filerules...
@@ -107,4 +109,6 @@
 &my_die("Stack list contains less than two elements", $stack_id, $PS_EXIT_SYS_ERROR) unless
     scalar @$files >= 2;
+
+print "I've loaded my inputs: $stack_id\n";
 
 # Parse the list of input files to get the tesselation, skycell identifiers and camera
@@ -146,4 +150,6 @@
 }
 
+print "I've configured everything: $stack_id\n";
+
 # Generate MDC file with the inputs
 my $tess_base = basename($tess_id);
@@ -180,4 +186,6 @@
     print $listFile "END\n\n";
 }
+
+print "I've checked everything: $stack_id\n";
 
 # Get the output filenames
@@ -200,4 +208,5 @@
 
 my $cmdflags;
+
 
 # Perform stacking
@@ -265,5 +274,6 @@
 #       &my_die("Couldn't find expected output file: $bin2Name",    $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin2Name);
     }
-
+    
+    print "I've stacked $listName: $stack_id\n";
 }
 
@@ -298,4 +308,6 @@
 }
 
+print "I've updated teh database: $stack_id\n";
+
 
 my $my_die_called = 0;
@@ -372,4 +384,5 @@
 }
 
+print "I've reached the end of processing with a code of $?: $stack_id\n";
 
 END {
Index: branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql
===================================================================
--- branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql	(revision 24972)
+++ branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql	(revision 25050)
@@ -1,3 +1,3 @@
-SELECT
+SELECT DISTINCT
     detNormalizedImfile.*,
     rawExp.camera
Index: branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql
===================================================================
--- branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql	(revision 24972)
+++ branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql	(revision 25050)
@@ -1,3 +1,3 @@
-SELECT
+SELECT DISTINCT
     detStackedImfile.*,
     rawExp.camera
Index: branches/czw_branch/cleanup/ippconfig/recipes/psastro.config
===================================================================
--- branches/czw_branch/cleanup/ippconfig/recipes/psastro.config	(revision 24972)
+++ branches/czw_branch/cleanup/ippconfig/recipes/psastro.config	(revision 25050)
@@ -3,4 +3,7 @@
 PSASTRO.ONLY.REFSTARS      BOOL FALSE  # skip all but refstar matches
 PSASTRO.SAVE.REFMATCH      BOOL FALSE  # save refstar matches as table in output smf file
+
+# select which WCS style to use on output images.
+PSASTRO.WCS.USECDKEYS	 BOOL FALSE
 
 # perform single-chip astrometry?
Index: branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.c
===================================================================
--- branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.c	(revision 24972)
+++ branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.c	(revision 25050)
@@ -289,5 +289,5 @@
     // test the CDELTi varient
     if (pcKeys) {
-        wcs->useCDkeys = 0;
+        wcs->wcsCDkeys = 0;
         wcs->cdelt1 = psMetadataLookupF64 (&status, header, "CDELT1");
         wcs->cdelt2 = psMetadataLookupF64 (&status, header, "CDELT2");
@@ -335,5 +335,5 @@
     // test the CDi_j varient
     if (cdKeys) {
-        wcs->useCDkeys = 1;
+        wcs->wcsCDkeys = 1;
         wcs->trans->x->coeff[1][0] = psMetadataLookupF64 (&status, header, "CD1_1"); // == PC1_1
         wcs->trans->x->coeff[0][1] = psMetadataLookupF64 (&status, header, "CD1_2"); // == PC1_2
@@ -377,5 +377,5 @@
     // XXX make it optional to write out CDi_j terms, or other versions
     // apply CDELT1,2 (degrees / pixel) to yield PCi,j terms of order unity
-    if (!(wcs->useCDkeys)) {
+    if (!(wcs->wcsCDkeys)) {
 
       double cdelt1 = wcs->cdelt1;
@@ -419,5 +419,5 @@
       }
     }
-    if (wcs->useCDkeys) {
+    if (wcs->wcsCDkeys) {
       
       psMetadataAddF64 (header, PS_LIST_TAIL, "CD1_1", PS_META_REPLACE, "", wcs->trans->x->coeff[1][0]);
@@ -555,4 +555,6 @@
         fpa->toSky->R -= 2.0*M_PI;
 
+    fpa->wcsCDkeys = wcs->wcsCDkeys;
+
     psTrace ("psastro", 5, "toFPA: %f %f  (%f,%f),(%f,%f)\n",
              chip->toFPA->x->coeff[0][0], chip->toFPA->y->coeff[0][0],
@@ -702,4 +704,5 @@
     wcs->cdelt2 = hypot (wcs->trans->y->coeff[1][0], wcs->trans->y->coeff[0][1]);
 
+    wcs->wcsCDkeys = fpa->wcsCDkeys;
     psFree (toTPA);
 
@@ -942,5 +945,5 @@
     wcs->trans = psPlaneTransformAlloc (nXorder, nYorder);
     wcs->toSky = NULL;
-    wcs->useCDkeys = 0;
+    wcs->wcsCDkeys = 0;
 
     memset (wcs->ctype1, 0, PM_ASTROM_WCS_TYPE_SIZE);
Index: branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.h
===================================================================
--- branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.h	(revision 24972)
+++ branches/czw_branch/cleanup/psModules/src/astrom/pmAstrometryWCS.h	(revision 25050)
@@ -23,5 +23,5 @@
     double crpix1, crpix2;
     double cdelt1, cdelt2;
-    bool useCDkeys;
+    bool wcsCDkeys;
     psProjection *toSky;
     psPlaneTransform *trans;
Index: branches/czw_branch/cleanup/psModules/src/camera/pmFPA.h
===================================================================
--- branches/czw_branch/cleanup/psModules/src/camera/pmFPA.h	(revision 24972)
+++ branches/czw_branch/cleanup/psModules/src/camera/pmFPA.h	(revision 25050)
@@ -48,4 +48,5 @@
     psPlaneTransform *toTPA;  ///< Transformation from focal plane to tangent plane, or NULL
     psProjection *toSky;         ///< Projection from tangent plane to sky, or NULL
+    bool wcsCDkeys;
     // Information
     psMetadata *concepts;               ///< FPA-level concepts
Index: branches/czw_branch/cleanup/psastro/src/psastroChipAstrom.c
===================================================================
--- branches/czw_branch/cleanup/psastro/src/psastroChipAstrom.c	(revision 24972)
+++ branches/czw_branch/cleanup/psastro/src/psastroChipAstrom.c	(revision 25050)
@@ -97,5 +97,6 @@
                 // write the elapsed time here; this will be updated in psastroMosaicAstrometry, if called
                 psMetadataAddF32 (updates, PS_LIST_TAIL, "DT_ASTR", PS_META_REPLACE, "elapsed psastro time", psTimerMark ("psastroAnalysis"));
-
+		
+		fpa->wcsCDkeys = psMetadataLookupBool(&status, recipe , "PSASTRO.WCS.USECDKEYS");
                 pmAstromWriteWCS (updates, fpa, chip, NONLIN_TOL);
 
