Index: trunk/ippScripts/scripts/staticsky.pl
===================================================================
--- trunk/ippScripts/scripts/staticsky.pl	(revision 28153)
+++ trunk/ippScripts/scripts/staticsky.pl	(revision 28154)
@@ -38,5 +38,9 @@
 }
 
-my ($sky_id, $camera, $dbname, $threads, $outroot, $reduction, $inverse, $run_state, $verbose, $no_update, $no_op, $redirect, $save_temps);
+# XXX test:
+print "run staticsky.pl @ARGV\n";
+# exit 0;
+
+my ($sky_id, $camera, $dbname, $threads, $outroot, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps);
 GetOptions(
     'sky_id=s'          => \$sky_id, # Diff identifier
@@ -44,5 +48,4 @@
     'dbname|d=s'        => \$dbname, # Database name
     'threads=s'         => \$threads,   # Number of threads to use
-    'run-state=s'       => \$run_state,   # state for run: 'new' or 'update'
     'outroot=s'         => \$outroot, # Output root name
     'reduction=s'       => \$reduction, # Reduction class
@@ -56,5 +59,5 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --sky_id --outroot --camera (--run_state)",
+    -msg => "Required options: --sky_id --outroot --camera",
     -exitval => 3,
           ) unless 
@@ -62,10 +65,8 @@
     defined $outroot and
     defined $camera;
-#   and defined $run_state;
 
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $sky_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 
 my $logDest = $ipprc->filename("LOG.EXP", $outroot);
-# $logDest .= ".update" if $run_state eq "update";
 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $sky_id, $PS_EXIT_SYS_ERROR ) if $redirect;
 
@@ -85,5 +86,5 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform difftool -inputskyfile: $error_code", $sky_id, $error_code);
+        &my_die("Unable to perform staticskytool -inputs: $error_code", $sky_id, $error_code);
     }
 
@@ -152,7 +153,7 @@
 print "recipe_psphot: $recipe_psphot\n";
 
-my $cmdflags;
-
-# Perform subtraction
+# my $cmdflags;
+
+# Perform stack photometry analysis
 {
     my $command = "$psphotStack $outroot";
@@ -167,10 +168,9 @@
 
     unless ($no_op) {
-        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 ppSub: $error_code", $sky_id, $error_code);
-        }
+	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 psphotStack: $error_code", $sky_id, $error_code);
+	}
 
         # my $outputStatsReal = $ipprc->file_resolve($outputStats);
@@ -190,6 +190,7 @@
         # chomp $cmdflags;
 
-        my ($quality) = $cmdflags =~ /-quality (\d+)/; # Quality flag
-
+        # my ($quality) = $cmdflags =~ /-quality (\d+)/; # Quality flag
+
+	my $quality = 0;
         if (!$quality) {
 
@@ -197,8 +198,8 @@
 	    # we have one set of output files per input file set
 	    for (my $i = 0; $i < @$files; $i++) {
-		my $outputName     = $ipprc->filename("PSPHOT.STACK.OUTPUT.IMAGE", $outroot);
-		my $outputMask     = $ipprc->filename("PSPHOT.STACK.OUTPUT.MASK", $outroot);
-		my $outputVariance = $ipprc->filename("PSPHOT.STACK.OUTPUT.VARIANCE", $outroot);
-		my $outputSources  = $ipprc->filename("PSPHOT.STACK.OUTPUT", $outroot);
+		my $outputName     = $ipprc->filename("PSPHOT.STACK.OUTPUT.IMAGE", $outroot, $i);
+		my $outputMask     = $ipprc->filename("PSPHOT.STACK.OUTPUT.MASK", $outroot, $i);
+		my $outputVariance = $ipprc->filename("PSPHOT.STACK.OUTPUT.VARIANCE", $outroot, $i);
+		my $outputSources  = $ipprc->filename("PSPHOT.STACK.OUTPUT", $outroot, $i);
 
 		&my_die("Couldn't find expected output file: $outputName", $sky_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
@@ -211,9 +212,4 @@
 	    #my $outputStats    = $ipprc->filename("SKYCELL.STATS", $outroot);
 	    #my $traceDest      = $ipprc->filename("TRACE.EXP", $outroot);
-
-	    # if ($run_state eq 'update') {
-	    # 	$traceDest .= '.update';
-	    # 	$outputStats .= '.update';
-	    # }
 
 	    my $chisqName     = $ipprc->filename("PSPHOT.CHISQ.IMAGE", $outroot);
@@ -235,13 +231,9 @@
     {
         my $command = "$staticskytool -sky_id $sky_id";
-        # if ($run_state eq 'new') {
 	$command .= " -addresult -path_base $outroot";
 	$command .= " -num_inputs $nInputs";
-	$command .= " $cmdflags";
+	# $command .= " $cmdflags";
 	$command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
 	$command .= " -hostname $host" if defined $host;
-        # } else {
-        #     $command .= " -tofullskyfile";
-        # }
         $command .= " -dbname $dbname" if defined $dbname;
 
@@ -250,8 +242,7 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            my $err_message = $run_state eq "update" ?
-                "Unable to perform difftool -adddiffskyfile" :
-                "Unable to perform difftool -tofullskyfile";
-            &my_die("$err_message: $error_code", $sky_id, $error_code);
+            my $err_message = "Unable to perform staticskytool -addresult";
+	    warn($err_message);
+	    exit $error_code;
         }
     }
@@ -270,13 +261,9 @@
     if (defined $sky_id and not $no_update) {
         my $command = "$staticskytool -sky_id $sky_id -fault $exit_code";
-        if ($run_state eq 'new') {
-            $command .= " -adddiffskyfile";
-            $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
-            $command .= " -hostname $host" if defined $host;
-            $command .= " -path_base $outroot" if defined $outroot;
-            $command .= " -dbname $dbname" if defined $dbname;
-        } else {
-            $command .= " -updatediffskyfile";
-        }
+	$command .= " -addresult";
+	$command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
+	$command .= " -hostname $host" if defined $host;
+	$command .= " -path_base $outroot" if defined $outroot;
+	$command .= " -dbname $dbname" if defined $dbname;
         run(command => $command, verbose => $verbose);
     }
Index: trunk/ippTasks/Makefile.am
===================================================================
--- trunk/ippTasks/Makefile.am	(revision 28153)
+++ trunk/ippTasks/Makefile.am	(revision 28154)
@@ -20,4 +20,5 @@
 	diff.pro \
 	stack.pro \
+	staticsky.pro \
 	summit.copy.pro \
 	survey.pro \
Index: trunk/ippTasks/staticsky.pro
===================================================================
--- trunk/ippTasks/staticsky.pro	(revision 28154)
+++ trunk/ippTasks/staticsky.pro	(revision 28154)
@@ -0,0 +1,253 @@
+## staticsky.pro : tasks for staticsky photometry analysis : -*- sh -*-
+
+## This file contains panTasks definitions for performing the
+## staticsky photometry analysis.  After a staticsky entry (with
+## associated sky_id) is defined, psphotStack is performed (via script
+## staticsky.pl) (tasks in staticskyResult).
+
+# test for required global variables
+check.globals
+
+### Initialise the books containing the tasks to do
+book init staticskyResult
+
+### Database lists
+$staticsky_DB = 0
+$staticsky_revert_DB = 0
+
+### Check status of staticsky tasks
+macro staticsky.status
+  book listbook staticskyResult
+end
+
+### Reset staticsky tasks
+macro staticsky.reset
+  book init staticskyResult
+end
+
+### Turn staticsky tasks on
+macro staticsky.on
+  task staticsky.load
+    active true
+  end
+  task staticsky.run
+    active true
+  end
+  task staticsky.revert
+    active false
+  end
+end
+
+### Turn staticsky tasks off
+macro staticsky.off
+  task staticsky.load
+    active false
+  end
+  task staticsky.run
+    active false
+  end
+  task staticsky.revert
+    active false
+  end
+end
+
+macro staticsky.revert.on
+  task staticsky.revert
+    active true
+  end
+end
+
+macro staticsky.revert.off
+  task staticsky.revert
+    active false
+  end
+end
+
+### Load tasks for staticsky
+### Tasks are loaded into staticskyResult.
+task	       staticsky.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 30
+  npending     1
+
+  stdout NULL
+  stderr $LOGDIR/staticsky.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = staticskytool -todo
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$staticsky_DB
+      $run = $run -dbname $DB:$staticsky_DB -limit 40
+      $staticsky_DB ++
+      if ($staticsky_DB >= $DB:n) set staticsky_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 staticskyResult -key sky_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook staticskyResult
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup staticskyResult
+  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
+
+### Run tasks for the staticsky analysis
+### Tasks are taken from staticskyResult.
+task	       staticsky.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 10800
+
+  task.exec
+    book npages staticskyResult -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    if ($BURNTOOLING == 1) break
+
+    # look for new entries in staticskyResult (pantaskState == INIT)
+    book getpage staticskyResult 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword staticskyResult $pageName pantaskState RUN
+    book getword staticskyResult $pageName sky_id -var SKY_ID
+    book getword staticskyResult $pageName tess_id -var TESS_DIR
+    book getword staticskyResult $pageName skycell_id -var SKYCELL_ID
+    book getword staticskyResult $pageName workdir -var WORKDIR_TEMPLATE
+    book getword staticskyResult $pageName path_base -var PATH_BASE
+    book getword staticskyResult $pageName reduction -var REDUCTION
+    book getword staticskyResult $pageName dbname -var DBNAME
+    book getword staticskyResult $pageName state -var RUN_STATE
+
+    # move this above when we get camera into the db correctly:
+    # book getword staticskyResult $pageName camera -var CAMERA
+    $CAMERA = GPC1
+
+    # set the host and workdir based on the skycell hash
+    set.host.for.skycell $SKYCELL_ID
+    set.workdir.by.skycell $SKYCELL_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+
+    basename $TESS_DIR -var TESS_ID
+    if ("$PATH_BASE" == "NULL")
+        sprintf outroot "%s/%s/%s/%s.%s.stk.%s" $WORKDIR $TESS_ID $SKYCELL_ID $TESS_ID $SKYCELL_ID $SKY_ID
+    else
+        $outroot = $PATH_BASE
+    end
+
+    stdout $LOGDIR/staticsky.log
+    stderr $LOGDIR/staticsky.log
+
+    $run = staticsky.pl --threads @MAX_THREADS@ --sky_id $SKY_ID --outroot $outroot --redirect-output --camera $CAMERA
+    if ("$REDUCTION" != "NULL")
+      $run = $run --reduction $REDUCTION
+    end
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    command $run
+  end
+
+  # default exit status
+  task.exit    default
+    process_exit staticskyResult $options:0 $JOB_STATUS
+  end
+
+  # locked list
+  task.exit    crash
+    showcommand crash
+    echo "hostname: $JOB_HOSTNAME"
+    book setword staticskyResult $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword staticskyResult $options:0 pantaskState TIMEOUT
+  end
+end
+
+task staticsky.revert
+  host         local
+
+  periods      -poll 60.0
+  periods      -exec 1800.0
+  periods      -timeout 120.0
+  npending     1
+  active false
+  
+  stdout NULL
+  stderr $LOGDIR/revert.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    # Only revert failures with fault=2 (SYS_ERROR), which tend to be
+    # temporary filesystem problems.  Every other fault type is
+    # interesting and should be kept for debugging (and so it doesn't
+    # continue to occur).
+    $run = staticskytool -revertsumskyfile -fault 2
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$staticsky_revert_DB
+      $run = $run -dbname $DB:$staticsky_revert_DB
+      $staticsky_revert_DB ++
+      if ($staticsky_revert_DB >= $DB:n) set staticsky_revert_DB = 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/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 28153)
+++ trunk/ippTools/share/Makefile.am	(revision 28154)
@@ -294,4 +294,5 @@
      staticskytool_todo.sql \
      staticskytool_result.sql \
+     staticskytool_revert.sql \
      warptool_change_skyfile_data_state.sql \
      warptool_change_run_state.sql \
Index: trunk/ippTools/share/staticskytool_revert.sql
===================================================================
--- trunk/ippTools/share/staticskytool_revert.sql	(revision 28154)
+++ trunk/ippTools/share/staticskytool_revert.sql	(revision 28154)
@@ -0,0 +1,5 @@
+DELETE FROM staticskyResult
+USING staticskyResult, staticskyRun
+WHERE staticskyRun.sky_id = staticskyResult.sky_id
+    AND staticskyRun.state = 'new'
+    AND staticskyResult.fault != 0
Index: trunk/ippTools/share/staticskytool_todo.sql
===================================================================
--- trunk/ippTools/share/staticskytool_todo.sql	(revision 28153)
+++ trunk/ippTools/share/staticskytool_todo.sql	(revision 28154)
@@ -4,5 +4,7 @@
     staticskyRun.reduction,
     staticskyRun.label,
-    staticskyRun.state
+    staticskyRun.state,
+    stackRun.tess_id,
+    stackRun.skycell_id
 FROM staticskyRun
 JOIN staticskyInput USING (sky_id)
Index: trunk/ippTools/src/staticskytool.c
===================================================================
--- trunk/ippTools/src/staticskytool.c	(revision 28153)
+++ trunk/ippTools/src/staticskytool.c	(revision 28154)
@@ -42,4 +42,6 @@
 static bool importrunMode(pxConfig *config);
 
+static bool setstaticskyRunState(pxConfig *config, psS64 sky_id, const char *state);
+
 # define MODECASE(caseName, func) \
     case caseName: \
@@ -129,4 +131,5 @@
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
+    PXOPT_LOOKUP_BOOL(check_inputs, config->args, "-check_inputs", false);
 
     psString select = pxDataGet("staticskytool_definebyquery_select.sql");
@@ -255,4 +258,16 @@
 	    psWarning("staticskytool ERROR: no rows found for known tess_id, skycell_id?");
 	    continue;
+	}
+
+	if (check_inputs) {
+	  // negative simple so the default is true
+	  if (!ippdbPrintMetadatas(stdout, inputs, "staticskyInput", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            psFree(where);
+            return false;
+	  }
+	  psFree(inputs);
+	  continue;
 	}
 
@@ -550,4 +565,14 @@
     }
 
+    if (!fault) {
+        if (!setstaticskyRunState(config, sky_id, "full")) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "failed to change staticskyRun state");
+            return false;
+        }
+    }
+
     // point of no return
     if (!psDBCommit(config->dbh)) {
@@ -643,9 +668,8 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-# if (0)
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_S64(config->args, where, "-stack_id", "stackSumSkyfile.stack_id", "==");
-    pxAddLabelSearchArgs(config, where, "-label", "stackRun.label", "==");
-    PXOPT_COPY_S16(config->args, where, "-fault", "stackSumSkyfile.fault", "==");
+    PXOPT_COPY_S64(config->args, where, "-sky_id", "staticskyResult.sky_id", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "staticskyRun.label", "==");
+    PXOPT_COPY_S16(config->args, where, "-fault", "staticskyResult.fault", "==");
 
     if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
@@ -656,5 +680,5 @@
 
     // Delete product
-    psString delete = pxDataGet("staticskytool_revertsumskyfile_delete.sql");
+    psString delete = pxDataGet("staticskytool_revert.sql");
     if (!delete) {
         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
@@ -680,39 +704,17 @@
 
     psFree(where);
-# endif
-
     return true;
 }
 
-# if (0)
-static bool updateresultState(pxConfig *config)
-{
-    // check that state is a valid string value
-    if (!pxIsValidState(state)) {
-        psError(PS_ERR_UNKNOWN, false, "invalid stackRun state: %s", state);
-        return false;
-    }
-
-    char *query = "UPDATE stackRun SET state = '%s' WHERE stack_id = %"PRId64;
-    if (!p_psDBRunQueryF(config->dbh, query, state, stack_id)) {
-        psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for stack_id %"PRId64, stack_id);
-        return false;
-    }
-    return true;
-}
-# endif
-
 static bool updateresult(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-# if (0)
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_S64(config->args, where, "-stack_id",   "stack_id",   "==");
-
-    if (!pxSetFaultCode(config->dbh, "stackSumSkyfile", where, fault)) {
+    PXOPT_COPY_S64(config->args, where, "-sky_id",   "sky_id",   "==");
+
+    if (!pxSetFaultCode(config->dbh, "staticskyResult", where, fault)) {
         psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
         psFree (where);
@@ -720,6 +722,4 @@
     }
     psFree (where);
-
-# endif
     return true;
 }
@@ -898,4 +898,23 @@
 # endif
   return true;
+}
+
+static bool setstaticskyRunState(pxConfig *config, psS64 sky_id, const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(state, false);
+
+    // check that state is a valid string value
+    if (!pxIsValidState(state)) {
+        psError(PS_ERR_UNKNOWN, false, "invalid staticskyRun state: %s", state);
+        return false;
+    }
+
+    char *query = "UPDATE staticskyRun SET state = '%s' WHERE sky_id = %"PRId64;
+    if (!p_psDBRunQueryF(config->dbh, query, state, sky_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to change state for sky_id %"PRId64, sky_id);
+        return false;
+    }
+    return true;
+
 }
 
Index: trunk/ippTools/src/staticskytoolConfig.c
===================================================================
--- trunk/ippTools/src/staticskytoolConfig.c	(revision 28153)
+++ trunk/ippTools/src/staticskytoolConfig.c	(revision 28154)
@@ -61,4 +61,5 @@
     psMetadataAddTime(definebyqueryArgs, PS_LIST_TAIL, "-set_registered", 0, "time detrend run was registered", now);
     psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-pretend",  0, "do not actually modify the database", false);
+    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-check_inputs",  0, "list inputs, do not modify database", false);
     psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-simple", 0, "use the simple output format", false);
 
