Index: trunk/ippScripts/scripts/addstar_run.pl
===================================================================
--- trunk/ippScripts/scripts/addstar_run.pl	(revision 34264)
+++ trunk/ippScripts/scripts/addstar_run.pl	(revision 34265)
@@ -94,4 +94,7 @@
 # Recipes to use based on reduction class
 $reduction = 'DEFAULT' unless defined $reduction;
+if ($stage =~/skycal/) {
+    $reduction = 'DEFAULT';
+}
 my $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR');
 # XXX This is now not used: do we still need it?
@@ -105,4 +108,10 @@
 #  $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR_STATICSKY'); # Recipe to use
 #}
+if ($stage =~/skycal/) {
+    $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use                                                              
+}
+
+
+
 &my_die("Unrecognised ADDSTAR recipe", $add_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_addstar;
 
@@ -118,4 +127,9 @@
 my $fpaObjects = $ipprc->filename("PSASTRO.OUTPUT", $stageroot) or &my_die("Missing entry from camera config", $add_id, $PS_EXIT_CONFIG_ERROR);
 my $fpaObjectsAlt = $fpaObjects;
+
+if ($stage =~ /skycal/) {
+    #should be ok for skycal?
+    print "using $fpaObjects for $stage\n";
+}
 
 
@@ -162,9 +176,11 @@
     }
 }
-if (($stage =~/stack/)) {
+
+if (($stage =~/stack/ || $stage =~ /skycal/)) {
     $fpaObjects =~ s/smf$/cmf/;
     $fpaObjectsAlt =~ s/smf$/cmf/;
 
 }
+
 my $fpaObjects1;
 my $fpaObjects2;
Index: trunk/ippTasks/addstar.pro
===================================================================
--- trunk/ippTasks/addstar.pro	(revision 34264)
+++ trunk/ippTasks/addstar.pro	(revision 34265)
@@ -24,4 +24,7 @@
   end
   task addstar.exp.load.staticsky
+    active true  
+  end
+  task addstar.exp.load.skycal
     active true
   end
@@ -41,4 +44,7 @@
     active false
   end
+  task addstar.exp.load.skycal
+    active false
+  end
   task addstar.exp.run
     active false
@@ -50,11 +56,15 @@
     active false
   end
- task addstar.revert.stack
-    active false
-  end
- task addstar.revert.staticsky
-    active false
-  end
-end
+  task addstar.revert.stack
+    active false
+  end
+  task addstar.revert.staticsky
+    active false
+  end
+  task addstar.revert.skycal
+    active false
+  end
+end
+
 macro addstar.revert.on
   task addstar.revert.cam
@@ -65,4 +75,7 @@
   end
   task addstar.revert.staticsky
+    active true
+  end
+  task addstar.revert.skycal
     active true
   end
@@ -77,4 +90,5 @@
 $addstar_revert_DB_SS = 0
 $addstar_revert_DB_SSM = 0
+$addstar_revert_DB_SC = 0 
 
 $LOADEXEX_ADD = 300
@@ -244,4 +258,58 @@
 
   # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task           addstar.exp.load.skycal
+  host         local
+  periods      -poll $LOADPOLL
+  periods      -exec 300
+  periods      -timeout 300
+  npending     1
+
+  stdout NULL
+  stderr $LOGDIR/addstar.exp.log
+
+  task.exec
+   # if ($LABEL:n == 0) break                                                                                 
+    $run = addtool -pendingexp -stage skycal
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks                                                                   
+      option $DB:$addstar_DB
+      $run = $run -dbname $DB:$addstar_DB
+      $addstar_DB ++
+      if ($addstar_DB >= $DB:n) set addstar_DB = 0
+    end
+    add_poll_args run
+    add_poll_labels run
+    command $run
+  end
+
+  # success                                                                                                   
+  task.exit    0
+    # convert 'stdout' to book format                                                                         
+    ipptool2book stdout addPendingExp -key add_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook addPendingExp
+    end
+
+    # delete existing entries in the appropriate pantaskStates                                                
+    process_cleanup addPendingExp
+  end
+
+  # default exit status                                                                                       
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?                                                                                      
   task.exit    timeout
     showcommand timeout
@@ -311,4 +379,8 @@
 	sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
     end
+    if ("$STAGE" == "skycal")
+        sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
+    end
+
   
 
@@ -326,4 +398,10 @@
       $run = $run --stage_id $STAGE_ID
     end
+   if ("$STAGE" == "skycal")
+      $run = $run --stage_id $STAGE_ID
+    end
+
+
+
     if ("$IMAGE_ONLY" == "T")
       $run = $run --image-only
@@ -508,2 +586,49 @@
   end
 end
+
+task addstar.revert.skycal
+  host         local
+
+  periods      -poll 5.0
+  periods      -exec 60.0
+  periods      -timeout 1200
+  npending     1
+  active        false
+
+  stdout NULL
+  stderr $LOGDIR/revert.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = addtool -revertprocessedexp -stage skycal
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks                                                                   
+      option $DB:$addstar_revert_DB_SC
+      $run = $run -dbname $DB:$addstar_revert_DB_SC
+      $addstar_revert_DB_SC ++
+      if ($addstar_revert_DB_SC >= $DB:n) set addstar_revert_DB_SC = 0
+    end
+     add_poll_labels run
+    command $run
+  end
+
+  # success                                                                                                   
+  task.exit    0
+  end
+
+  # locked list                                                                                               
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?                                                                                      
+  task.exit    timeout
+    showcommand timeout
+  end
+end
Index: trunk/ippTasks/survey.pro
===================================================================
--- trunk/ippTasks/survey.pro	(revision 34264)
+++ trunk/ippTasks/survey.pro	(revision 34265)
@@ -862,4 +862,8 @@
         #only queue uncensored stacks
         $run = $run -uncensored
+    end
+    if ("$stage" == "skycal")
+        #skycal doesn't have magic, however, we still need to tell addtool 
+	$run = $run -uncensored
     end
 
Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 34264)
+++ trunk/ippTools/share/Makefile.am	(revision 34265)
@@ -8,4 +8,6 @@
 	addtool_find_cam_id_dvo.sql \
 	addtool_find_cam_id.sql \
+	addtool_find_skycal_id_dvo.sql \
+	addtool_find_skycal_id.sql \
 	addtool_find_sky_id_multi_dvo.sql \
 	addtool_find_sky_id_multi.sql \
@@ -14,8 +16,10 @@
 	addtool_find_pendingexp.sql \
 	addtool_find_pendingexp_cam.sql \
+	addtool_find_pendingexp_skycal.sql \
 	addtool_find_pendingexp_stack.sql \
 	addtool_find_pendingexp_staticsky_multi.sql \
 	addtool_find_pendingmergeprocess.sql \
 	addtool_find_processedexp_cam.sql \
+	addtool_find_processedexp_skycal.sql \
 	addtool_find_processedexp_stack.sql \
 	addtool_find_processedexp_staticsky_multi.sql \
@@ -27,7 +31,9 @@
 	addtool_queue_stack_id.sql \
 	addtool_queue_sky_id_multi.sql \
+	addtool_queue_skycal_id.sql \
 	addtool_queue_minidvodbrun.sql \
 	addtool_revertminidvodbprocessed.sql \
 	addtool_revertprocessedexp_cam.sql \
+	addtool_revertprocessedexp_skycal.sql \
 	addtool_revertprocessedexp_stack.sql \
 	addtool_revertprocessedexp_staticsky.sql \
Index: trunk/ippTools/share/addtool_find_pendingexp_skycal.sql
===================================================================
--- trunk/ippTools/share/addtool_find_pendingexp_skycal.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_find_pendingexp_skycal.sql	(revision 34265)
@@ -0,0 +1,30 @@
+SELECT
+    addRun.*,
+    skycalResult.path_base as stageroot,
+    rawExp.camera,
+    rawExp.telescope
+FROM addRun
+JOIN skycalRun
+    ON skycal_id = stage_id
+JOIN skycalResult
+    USING (skycal_id)
+JOIN stackRun
+    using(stack_id)
+JOIN stackInputSkyfile 
+     USING(stack_id)
+JOIN warpRun using(warp_id)
+JOIN fakeRun using(fake_id)
+JOIN camRun using(cam_id)
+JOIN chipRun using(chip_id)
+JOIN rawExp using (exp_id)
+LEFT JOIN addProcessedExp using (add_id)
+LEFT JOIN addMask
+    ON addRun.label = addMask.label
+WHERE
+    skycalRun.state = 'full'
+    AND stage = 'skycal'
+    AND ((addRun.state = 'new' AND addProcessedExp.add_id IS NULL) OR addRun.state = 'update')
+    AND addRun.dvodb IS NOT NULL
+    AND addRun.workdir IS NOT NULL
+    AND addMask.label IS NULL
+
Index: trunk/ippTools/share/addtool_find_processedexp_skycal.sql
===================================================================
--- trunk/ippTools/share/addtool_find_processedexp_skycal.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_find_processedexp_skycal.sql	(revision 34265)
@@ -0,0 +1,6 @@
+SELECT
+    addProcessedExp.*,
+    addRun.workdir
+FROM addProcessedExp
+JOIN addRun
+    USING(add_id)
Index: trunk/ippTools/share/addtool_find_skycal_id.sql
===================================================================
--- trunk/ippTools/share/addtool_find_skycal_id.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_find_skycal_id.sql	(revision 34265)
@@ -0,0 +1,22 @@
+SELECT
+     skycalRun.*
+FROM 
+     skycalRun
+JOIN skycalResult using (skycal_id)
+JOIN staticskyInput using (sky_id, stack_id)
+JOIN staticskyRun using (sky_id)
+
+LEFT JOIN (SELECT skycal_id       AS added_skycal_id,
+                  addRun.dvodb AS previous_dvodb
+           FROM addRun
+JOIN skycalRun on skycal_id = stage_id
+          ) as foo
+     ON skycal_id = added_skycal_id 
+     AND stage = 'skycal'
+     -- hook for qualifying the join on the previous_dvodb
+     AND %s
+WHERE
+    skycalRun.state = 'full'
+    AND skycalResult.quality = 0
+    AND added_skycal_id IS NULL
+    -- addtool adds checks on exposure being added to the dvodb previously
Index: trunk/ippTools/share/addtool_find_skycal_id_dvo.sql
===================================================================
--- trunk/ippTools/share/addtool_find_skycal_id_dvo.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_find_skycal_id_dvo.sql	(revision 34265)
@@ -0,0 +1,12 @@
+SELECT skycalRun.* FROM skycalRun
+JOIN skycalResult USING (skycal_id)
+JOIN staticskyInput USING (sky_id,stack_id)
+JOIN stackRun USING(stack_id)
+
+WHERE skycalRun.state = 'full' and skycalResult.quality = 0
+    AND stack_id NOT IN (SELECT stack_id
+       FROM addRun
+       JOIN skycalResult on skycalResult.skycal_id = addRun.stage_id
+       JOIN skycalRun USING(skycal_id)
+       WHERE addRun.stage = 'skycal' AND %s
+      )
Index: trunk/ippTools/share/addtool_queue_skycal_id.sql
===================================================================
--- trunk/ippTools/share/addtool_queue_skycal_id.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_queue_skycal_id.sql	(revision 34265)
@@ -0,0 +1,22 @@
+INSERT INTO addRun
+    SELECT
+        0,              -- add_id
+        'skycal',		-- stage
+        skycal_id,         -- stage_id
+	%d,		-- stage_extra1
+        '%s',           -- state
+        '%s',           -- workdir
+	'%s',           -- workdir_state
+        '%s',           -- reduction
+        '%s',           -- label
+        '%s',           -- data_group
+        '%s',           -- dvodb 
+        '%s',           -- note
+	%d,		-- image_only
+	%d,		-- minidvodb
+	'%s',           -- minidvodb_group 
+ 	'%s'	        -- minidvodb_name
+    FROM skycalRun
+    WHERE
+        skycalRun.state = 'full'
+        AND skycalRun.skycal_id = %lld
Index: trunk/ippTools/share/addtool_revertprocessedexp_skycal.sql
===================================================================
--- trunk/ippTools/share/addtool_revertprocessedexp_skycal.sql	(revision 34265)
+++ trunk/ippTools/share/addtool_revertprocessedexp_skycal.sql	(revision 34265)
@@ -0,0 +1,7 @@
+DELETE FROM addProcessedExp
+USING addProcessedExp, addRun
+WHERE
+    addRun.add_id = addProcessedExp.add_id
+    AND addProcessedExp.fault != 0
+    AND addRun.state = 'new'
+    AND addRun.stage = 'skycal'
Index: trunk/ippTools/src/addtool.c
===================================================================
--- trunk/ippTools/src/addtool.c	(revision 34264)
+++ trunk/ippTools/src/addtool.c	(revision 34265)
@@ -120,27 +120,31 @@
     PXOPT_COPY_S64(config->args, where,  "-cam_id",    "camRun.cam_id", "==");
     PXOPT_COPY_S64(config->args, where,  "-stack_id",    "stackRun.stack_id", "==");
- PXOPT_COPY_S64(config->args, where,  "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_COPY_S64(config->args, where,  "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_COPY_S64(config->args, where,  "-skycal_id",  "skycalRun.skycal_id", "==");
     PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     if (strcmp(stage, "cam")== 0) {
       pxcamGetSearchArgs (config, where);
-    pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "=="); // define using camRun label
-    pxAddLabelSearchArgs (config, where, "-data_group","camRun.data_group", "=="); // define using camRun label
-
-    PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
+      pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "=="); // define using camRun label
+      pxAddLabelSearchArgs (config, where, "-data_group","camRun.data_group", "=="); // define using camRun label
+      PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
     }
     if (strcmp(stage, "stack")== 0) {
-
-    pxAddLabelSearchArgs (config, where, "-label",     "stackRun.label", "=="); // define using camRun label
-    pxAddLabelSearchArgs (config, where, "-data_group","stackRun.data_group", "=="); // define using camRun label
-    PXOPT_COPY_STR(config->args, where,  "-reduction", "stackRun.reduction", "==");
-    pxAddLabelSearchArgs (config, where, "-filter",     "stackRun.filter", "=="); // define using camRun label
+      pxAddLabelSearchArgs (config, where, "-label",     "stackRun.label", "=="); // define using camRun label
+      pxAddLabelSearchArgs (config, where, "-data_group","stackRun.data_group", "=="); // define using camRun label
+      PXOPT_COPY_STR(config->args, where,  "-reduction", "stackRun.reduction", "==");
+      pxAddLabelSearchArgs (config, where, "-filter",     "stackRun.filter", "=="); // define using camRun label
     }
     if (strcmp(stage, "staticsky")== 0) {
-
-    pxAddLabelSearchArgs (config, where, "-label",     "staticskyRun.label", "=="); // define using camRun label
-    pxAddLabelSearchArgs (config, where, "-data_group","staticskyRun.data_group", "=="); // define using camRun label
-    PXOPT_COPY_STR(config->args, where,  "-reduction", "staticskyyRun.reduction", "==");
+      pxAddLabelSearchArgs (config, where, "-label",     "staticskyRun.label", "=="); // define using camRun label
+      pxAddLabelSearchArgs (config, where, "-data_group","staticskyRun.data_group", "=="); // define using camRun label
+      PXOPT_COPY_STR(config->args, where,  "-reduction", "staticskyRun.reduction", "==");
     //no filter here
     }
+    if (strcmp(stage, "skycal")==0) {
+      pxAddLabelSearchArgs (config, where, "-label",     "skycalRun.label", "=="); //define using skycalRun label
+      pxAddLabelSearchArgs (config, where, "-data_group","skycalRun.data_group", "==");
+      PXOPT_COPY_STR(config->args, where,  "-reduction", "skycalRun.reduction",  "==");
+    }
+
     if (!psListLength(where->list)) {
         psFree(where);
@@ -228,12 +232,35 @@
 	// user supplied dvodb
 	psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
-    } else {
-      psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_sky_id_multi.sql\n%s\n",stage);
+      } else {
+	psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_sky_id_multi.sql\n%s\n",stage);
         // find the cam_id of all the exposures that we want to queue up.
         bare_query = pxDataGet("addtool_find_sky_id_multi.sql");
         // inherit dvodb from camRun, avoid matching NULL
         psStringAppend(&dvodb_string, "(staticskyRun.dvodb IS NOT NULL AND previous_dvodb = staticskyRun.dvodb)");
-    }
-    }
+      }
+    }
+    
+    if (strcmp(stage,"skycal") == 0) {
+      if (dvodb ) {
+        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_skycal_id_dvo.sql\n%s\n", dvodb,stage);
+        // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
+        bare_query = pxDataGet("addtool_find_skycal_id_dvo.sql");
+        // user supplied dvodb                                                                                                                                              
+        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
+      } else {
+        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_skycal_id.sql\n%s\n",stage);
+        // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
+        bare_query = pxDataGet("addtool_find_skycal_id.sql");
+        // inherit dvodb from skycalRun, avoid matching NULL                                                                                                                   
+        psStringAppend(&dvodb_string, "(skycalRun.dvodb IS NOT NULL AND previous_dvodb = skycalRun.dvodb)");
+	// this is silly, there is no dvodb in skycalRun...?
+      }
+    }
+
+
+
+
+
+
 
     if (!bare_query) {
@@ -267,5 +294,5 @@
 	psStringAppend(&query, " AND (stackRun.magicked > 0)");
       }
-      // staticSky has no magicked column.
+      // staticSky/skycal have no magicked column.
     } else {
       //This picks only the unmagicked/uncensored ones
@@ -291,8 +318,10 @@
       //   psStringAppend(&query, " GROUP BY stack_id");
       // }
-    if (strcmp(stage,"staticsky") == 0) {
-      psStringAppend(&query, " GROUP BY sky_id, stack_id");  //some reason it needs this
-      }
-
+      if (strcmp(stage,"staticsky") == 0) {
+	psStringAppend(&query, " GROUP BY sky_id, stack_id");  //some reason it needs this
+      }
+      if (strcmp(stage,"skycal") == 0) {
+	psStringAppend(&query, " GROUP BY skycal_id, sky_id, stack_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
+      }
 
     psTrace("addtool.c", PS_LOG_INFO,"query: \n\n%s\n\n",query);
@@ -381,4 +410,6 @@
     }
    
+
+
     if (strcmp(stage,"staticsky") == 0) {
     for (long i = 0; i < psArrayLength(output); i++) {
@@ -418,4 +449,30 @@
     }
 
+    if (strcmp(stage,"skycal") == 0) {
+      for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+        skycalRunRow *row = skycalRunObjectFromMetadata(md);
+
+        if (!row) {
+	  psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
+	  psFree(output);
+	  return false;
+        }
+
+	if (!dvodb) {  // there's no skycalRun.dvodb
+ 	  psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, skycal_id %" PRId64, row->label, row->skycal_id);
+	  psFree(output);
+	  return false;
+        }
+        if (!workdir && !row->workdir) {
+	  psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, skycal_id %" PRId64, row->label, row->skycal_id);
+	  psFree(output);
+	  return false;
+        }
+
+        psFree(row);
+      }
+    }
 
 
@@ -572,5 +629,47 @@
       }
 
-
+    if (strcmp(stage,"skycal") == 0) {
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+	psS64 stage_id =0; 
+	
+		  skycalRunRow *row = skycalRunObjectFromMetadata(md);
+	  stage_id = row->skycal_id;
+	
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp
+        if (!pxaddQueueByCamID(config,
+			       stage,
+                               stage_id,
+			       0,
+                               workdir     ? workdir   : row->workdir,
+                               reduction   ? reduction : row->reduction,
+                               label       ? label     : row->label,
+                               data_group  ? data_group : (row->data_group ? row->data_group :  (label ? label : row->label)),
+                               dvodb       ? dvodb     : NULL,
+                               note        ? note      : NULL,
+                               image_only,
+                               minidvodb,
+                               minidvodb_group,
+                               minidvodb_name
+        )) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error sfg");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to trying to queue stage %s %" PRId64,stage, stage_id);
+            psFree(row);
+            psFree(output);
+            return false;
+        }
+        psFree(row);
+    }
+      }
+   
 
 
@@ -594,6 +693,6 @@
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
     PXOPT_COPY_S64(config->args, where, "-stack_id",    "stackRun.stack_id", "==");
-   PXOPT_COPY_S64(config->args, where, "-sky_id",    "staticskyRun.sky_id", "==");
-    
+    PXOPT_COPY_S64(config->args, where, "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-skycal_id", "skycalRun.skycal_id", "==");
     PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where); // most search arguments based on camera
@@ -610,13 +709,16 @@
     psString query = NULL;
     if (strcmp(stage, "cam")==0) {
-    query = psStringCopy("UPDATE addRun JOIN camRun on cam_id = stage_id JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
+      query = psStringCopy("UPDATE addRun JOIN camRun on cam_id = stage_id JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
     }
     if (strcmp(stage, "stack")==0) {
-    query = psStringCopy("UPDATE addRun JOIN stackRun on stack_id = stage_id");
+      query = psStringCopy("UPDATE addRun JOIN stackRun on stack_id = stage_id");
     }
     if (strcmp(stage, "staticsky")==0) {
-    query = psStringCopy("UPDATE addRun JOIN staticskyRun on sky_id = stage_id");
-    }
-    
+      query = psStringCopy("UPDATE addRun JOIN staticskyRun on sky_id = stage_id");
+    }
+    if (strcmp(stage, "skycal")==0) {
+      query = psStringCopy("UPDATE addRun JOIN skycalRun on skycal_id = stage_id");
+    }
+
 
     // pxUpdateRun gets parameters from config->args and runs the update query
@@ -640,4 +742,5 @@
     PXOPT_COPY_S64(config->args, where, "-stack_id",    "stackRun.stack_id", "==");
     PXOPT_COPY_S64(config->args, where, "-sky_id",    "staticskyRun.sky_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-skycal_id", "skycalRun.skycal_id", "==");
     PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     pxcamGetSearchArgs (config, where);
@@ -650,13 +753,16 @@
     
     if (strcmp(stage, "cam")==0) { 
-    query = pxDataGet("addtool_find_pendingexp_cam.sql");
+      query = pxDataGet("addtool_find_pendingexp_cam.sql");
     }
     if (strcmp(stage, "stack")==0) { 
-    query = pxDataGet("addtool_find_pendingexp_stack.sql");
+      query = pxDataGet("addtool_find_pendingexp_stack.sql");
     }
     if (strcmp(stage, "staticsky")==0) { 
-    query = pxDataGet("addtool_find_pendingexp_staticsky_multi.sql");
-    }
-    
+      query = pxDataGet("addtool_find_pendingexp_staticsky_multi.sql");
+    }
+    if (strcmp(stage, "skycal")==0) {
+      query = pxDataGet("addtool_find_pendingexp_skycal.sql");
+    }
+
 
     if (!query) {
@@ -676,8 +782,8 @@
       psStringAppend(&query, " GROUP BY %s", "stack_id");
     }
-    //if (strcmp(stage, "staticsky") == 0) {
-    //  //this group by is needed to join against all the warps (to get camera)
-    //  psStringAppend(&query, " GROUP BY %s", "sky_id");
-    //}
+    if (strcmp(stage, "skycal") == 0) {
+      //this group by is needed to join against all the warps (to get camera)
+      psStringAppend(&query, " GROUP BY %s", "skycal_id");
+    }
     if (strcmp(stage, "staticsky") == 0) {
       //this group by is needed to join against all the warps (to get camera)
@@ -881,11 +987,14 @@
 
     if (strcmp (stage,"cam") == 0) {
-    query = pxDataGet("addtool_find_processedexp_cam.sql");
+      query = pxDataGet("addtool_find_processedexp_cam.sql");
     }
     if (strcmp (stage,"stack") == 0) {
-    query = pxDataGet("addtool_find_processedexp_stack.sql");
+      query = pxDataGet("addtool_find_processedexp_stack.sql");
     }
     if (strcmp (stage,"staticsky") == 0) {
-    query = pxDataGet("addtool_find_processedexp_staticsky_multi.sql");
+      query = pxDataGet("addtool_find_processedexp_staticsky_multi.sql");
+    }
+    if (strcmp (stage,"skycal") == 0) {
+      query = pxDataGet("addtool_find_processedexp_skycal.sql");
     }
 
@@ -1000,4 +1109,7 @@
 	 query = pxDataGet("addtool_revertprocessedexp_staticsky_multi.sql");
       }
+    if (strcmp(stage, "skycal") == 0) {
+      query = pxDataGet("addtool_revertprocessedexp_skycal.sql");
+    }
 
 
Index: trunk/ippTools/src/addtoolConfig.c
===================================================================
--- trunk/ippTools/src/addtoolConfig.c	(revision 34264)
+++ trunk/ippTools/src/addtoolConfig.c	(revision 34265)
@@ -53,4 +53,5 @@
     psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-stack_id",             0, "search by stack_id", 0);
     psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-sky_id",             0, "search by sky_id", 0);
+    psMetadataAddS64(definebyqueryArgs, PS_LIST_TAIL, "-skycal_id",          0, "search by skycal_id", 0);
     pxcamSetSearchArgs(definebyqueryArgs);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by camRun label", NULL);
@@ -81,4 +82,5 @@
     psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-stack_id",                 0, "search by stack_id", 0);
     psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-sky_id",                 0, "search by sky_id", 0);
+    psMetadataAddS64(updaterunArgs, PS_LIST_TAIL, "-skycal_id",                 0, "search by skycal_id", 0);
     pxcamSetSearchArgs(updaterunArgs);
     psMetadataAddStr(updaterunArgs, PS_LIST_TAIL, "-label",                  0, "search by addRun label", NULL);
@@ -101,4 +103,5 @@
     psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-stack_id",                 0, "search by stack_id", 0);
     psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-sky_id",                 0, "search by sky_id", 0);
+    psMetadataAddS64(pendingexpArgs, PS_LIST_TAIL, "-skycal_id",                 0, "search by skycal_id", 0);
     pxcamSetSearchArgs(pendingexpArgs);
     psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by addRun label", NULL);
Index: trunk/ippTools/src/pxadd.c
===================================================================
--- trunk/ippTools/src/pxadd.c	(revision 34264)
+++ trunk/ippTools/src/pxadd.c	(revision 34265)
@@ -151,17 +151,27 @@
         query = pxDataGet("addtool_queue_cam_id.sql");
         psMemSetPersistent(query, true);
+      } 
+      else if (strcmp(stage,"stack") == 0) {
+	query = pxDataGet("addtool_queue_stack_id.sql");
+	psMemSetPersistent(query, true);
       }
-      if (strcmp(stage,"stack") == 0) {
-	query = pxDataGet("addtool_queue_stack_id.sql");
+      else if (strcmp(stage,"staticsky") == 0) {
+	query = pxDataGet("addtool_queue_sky_id_multi.sql");
+        psMemSetPersistent(query, true);
+      } 
+      else if (strcmp(stage,"skycal") == 0) {
+	query = pxDataGet("addtool_queue_skycal_id.sql");
         psMemSetPersistent(query, true);
       }
-      if (strcmp(stage,"staticsky") == 0) {
-	query = pxDataGet("addtool_queue_sky_id_multi.sql");
-        psMemSetPersistent(query, true);
+      else 
+	{
+	  psError(PXTOOLS_ERR_SYS, false, "uknown stage %s for pxadd.c", stage);
+	  return false;
+	}
+
+      if (!query) {
+	psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+	return false;
       }
-        if (!query) {
-            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
-            return false;
-        }
     }
 
