Index: trunk/ippTasks/diff.pro
===================================================================
--- trunk/ippTasks/diff.pro	(revision 27920)
+++ trunk/ippTasks/diff.pro	(revision 28375)
@@ -11,4 +11,5 @@
 book init diffSkyfile
 #book init diffCleanup
+book init diffPendingSummary
 
 ### Database lists
@@ -16,4 +17,5 @@
 $diffAdvance_DB = 0
 $diff_revert_DB = 0
+$diffSummary_DB = 0
 #$diffCleanup_DB = 0
 
@@ -27,4 +29,5 @@
 macro diff.reset
   book init diffSkyfile
+  book init diffPendingSummary
 #  book init diffCleanup
 end
@@ -44,4 +47,10 @@
     active false
   end
+  task diff.summary.load
+    active true
+  end
+  task diff.summary.run
+    active true
+  end
 end
 
@@ -58,4 +67,10 @@
   end
   task diff.revert
+    active false
+  end
+  task diff.summary.load
+    active false
+  end
+  task diff.summary.run
     active false
   end
@@ -193,5 +208,4 @@
     # host anyhost
     # $WORKDIR = $WORKDIR_TEMPLATE
-
     if (($DIFF_MODE == 1)||("$DIFF_MODE" == "NULL")) 
 	$DIFF_TAG = ""
@@ -477,2 +491,150 @@
   end
 end
+
+### Load tasks for doing the diffs
+### Tasks are loaded into diffPendingSkyCell.
+task	       diff.summary.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 1200
+  npending     1
+
+  stdout NULL
+  stderr $LOGDIR/diff.summary.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = difftool -tosummary
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$diffSummary_DB
+      $run = $run -dbname $DB:$diffSummary_DB
+      $diffSummary_DB ++
+      if ($diffSummary_DB >= $DB:n) set diffSummary_DB = 0
+    end
+    add_poll_args run
+    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+    # convert 'stdout' to book format
+    # XXX change tess_id to tess_dir when db is updated
+    ipptool2book stdout diffPendingSummary -key diff_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook diffPendingSummary
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup diffPendingSummary
+  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 calculating the diff overlaps
+### Tasks are taken from diffPendingSkyCell.
+task	       diff.summary.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 60
+
+  task.exec
+    # if we are unable to run the 'exec', use a long retry time
+    periods -exec $RUNEXEC
+
+    book npages diffPendingSummary -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    
+    # look for new images in diffPendingSkyCell (pantaskState == INIT)
+    book getpage diffPendingSummary 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword diffPendingSummary $pageName pantaskState RUN
+    book getword diffPendingSummary $pageName diff_id -var DIFF_ID
+    book getword diffPendingSummary $pageName camera -var CAMERA
+    book getword diffPendingSummary $pageName workdir -var WORKDIR_TEMPLATE
+    book getword diffPendingSummary $pageName dbname -var DBNAME
+    book getword diffPendingSummary $pageName tess_id -var TESS_DIR
+    book getword diffPendingSummary $pageName diff_mode -var DIFF_MODE
+    book getword diffPendingSummary $pageName state -var RUN_STATE
+
+    # set the host and workdir based on the skycell hash
+    host anyhost
+    strsub $WORKDIR_TEMPLATE @HOST@.0 $default_host -var WORKDIR
+#    set.workdir.by.skycell $SKYCELL_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+
+#    if ("$PATH_BASE" == "NULL") 
+
+    if (($DIFF_MODE == 1)||("$DIFF_MODE" == "NULL")) 
+	$DIFF_TAG = ""
+    end
+    if ($DIFF_MODE == 2)
+	$DIFF_TAG = "WS."
+    end 
+    if ($DIFF_MODE == 3)
+	$DIFF_TAG = "SW."
+    end
+    if ($DIFF_MODE == 4)
+	$DIFF_TAG = "SS."
+    end
+
+    basename $TESS_DIR -var TESS_ID
+
+    ## generate outroot specific to this exposure
+    sprintf outroot "%s/%s/%s.%sdif.%s.summary" $WORKDIR $TESS_ID $TESS_ID $DIFF_TAG $DIFF_ID
+
+
+    stdout $LOGDIR/diff.summary.log
+    stderr $LOGDIR/diff.summary.log
+
+    $run = skycell_jpeg.pl --stage diff --stage_id $DIFF_ID --camera $CAMERA --outroot $outroot
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    periods -exec 0.05
+    command $run
+  end
+
+  # default exit status
+  task.exit    default
+    process_exit diffPendingSummary $options:0 $JOB_STATUS
+  end
+
+  # locked list
+  task.exit    crash
+    showcommand crash
+    echo "hostname: $JOB_HOSTNAME"
+    book setword diffPendingSummary $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword diffPendingSummary $options:0 pantaskState TIMEOUT
+  end
+end
