Index: trunk/ippTasks/phase2.pro
===================================================================
--- trunk/ippTasks/phase2.pro	(revision 10758)
+++ trunk/ippTasks/phase2.pro	(revision 11210)
@@ -1,10 +1,5 @@
 ## this file contains the tasks for running the phase 0 stage
+## these tasks use the book p2PendingImfile
 
-## these tasks use the queue Phase2Imfiles
-## the Phase2Imfiles queue contains:
-## STATE EXP_TAG CLASS CLASS_ID URI-FITS
-## queue keys are counted from 0
-
-queueinit Phase2Imfiles
 if ($?network == 0)
   $network = 1
@@ -13,7 +8,4 @@
   $parallel = 0
 end
-
-$Phase2ImfileFail = 0
-
 if ($?VERBOSE == 0)
   echo "VERBOSE not defined: load pantasks.pro first"
@@ -24,15 +16,17 @@
   break
 end
+
 $LOGSUBDIR = $LOGDIR/phase2
 exec mkdir -p $LOGSUBDIR
 
+book init p2PendingImfile
+
 macro phase2.status
-  queueprint Phase2Imfiles
-  echo "Phase2ImfileFail: $Phase2ImfileFail"
+  book listbook p2PendingImfile
 end
 
 # select images ready for phase2 analysis
-# new entries are added to queue Phase2Imfiles
-# compare the new list with the ones already selected
+# new entries are added to p2PendingImfile
+# skip already-present entries
 task	       phase2.imfile.load
   host         local
@@ -42,35 +36,21 @@
   periods      -timeout 30
 
-  stdout $LOGSUBDIR/phase2.imfile.log
+  stdout NULL
   stderr $LOGSUBDIR/phase2.imfile.log
 
   task.exec
-    # command p2tool -pendingimfile -simple -limit {$Phase2ImfileFail + 20}
-    command p2tool -pendingimfile -simple
+    command p2tool -pendingimfile -limit 20
   end
 
   # success
   task.exit    0
-    local i Nqueue
-
-    # save stdout and stderr
-
-    # compare output with Phase2Imfiles queue
-    # only add entries which don't exist in queue
-    queuesize stdout -var Nqueue
-    for i 0 $Nqueue
-      queuepop stdout -var line
-      if ($VERBOSE > 2)
-        echo $line
-      end
-      list word -split $line
-      $EXP_TAG  = $word:0
-      $CLASS_ID = $word:1
-      $URI      = $word:2
-      queuepush Phase2Imfiles -uniq -key 1:2 "NEW $EXP_TAG $CLASS_ID $URI"
+    # convert 'stdout' to book format
+    ipptool2book stdout p2PendingImfile -key exp_tag:class_id -uniq
+    if ($VERBOSE > 2)
+      book listbook p2PendingImfile
     end
 
     # delete existing entries which are DONE
-    queuedrop Phase2Imfiles -key 0 DONE
+    book delpage p2PendingImfile -key state DONE
   end
 
@@ -86,5 +66,5 @@
 end
 
-# run the phase2imfile script on pending images
+# run the phase2.pl script on pending images
 task	       phase2.imfile.run
   periods      -poll $runpoll
@@ -93,25 +73,21 @@
 
   task.exec
-    queuesize Phase2Imfiles -var N
+    book npages p2PendingImfile -var N
     if ($N == 0) break
     if ($network == 0) break
     
-    # look for new images on the internal DetrendImfilesToProcess queue
-    # caution with these 'if' statements: syntax errors 
-    # will make the task fail without given a good status
-    queuepop Phase2Imfiles -var line -key 0 NEW
-    if ("$line" == "NULL") break
+    # look for new images in p2PendingImfile (state == NULL)
+    book getpage p2PendingImfile 0 -var pageName -key state NULL
+    if ($pageName == NULL) break
 
-    strpop line state
-    queuepush Phase2Imfiles -replace -key 1:2 "RUN $line"
+    book setword p2PendingImfile $pageName state RUN
+    book getword p2PendingImfile $pageName camera -var CAMERA
+    book getword p2PendingImfile $pageName exp_tag -var EXP_TAG
+    book getword p2PendingImfile $pageName outpath -var OUTPATH
+    book getword p2PendingImfile $pageName class_id -var CLASS_ID
+    book getword p2PendingImfile $pageName uri -var URI
 
-    ## the Phase2Imfiles queue contains (line has popped STATE)
-    ## STATE EXP_TAG CLASS CLASS_ID URI-FITS
-    list word -split $line
-    $EXP_TAG  = $word:0
-    $CLASS_ID = $word:1
-    $URI      = $word:2
-
-    # specify choice of remote host:(need to choose based on chips)
+    # specify choice of remote host
+    # XXX need to choose based on chips
     if ($parallel)
       host anyhost
@@ -120,40 +96,30 @@
     end
 
-    ## add 'p2' to the log files?
-    # XXX this is still a lame rule
-    $word = `basename $EXP_TAG | tr '.' ' '`
-    list word -split $word
-    $base = $word:0
-    stdout $LOGSUBDIR/$base/$EXP_TAG.$CLASS_ID.log
-    stderr $LOGSUBDIR/$base/$EXP_TAG.$CLASS_ID.log
-    exec mkdir -p $LOGSUBDIR/$base
+    ## generate output log based on filerule
+    $outroot = $OUTPATH/$EXP_TAG
+    $logfile = `ipp_filename.pl PHASE2.LOG $outroot $CLASS_ID`
+    stdout $logfile
+    stderr $logfile
+    exec mkdir -p $OUTPATH
+
+    # save the pageName for future reference below
+    options $pageName
 
     # create the command line
-    options "$line"
     if ($VERBOSE > 1)
-      echo command phase2.pl --exp_tag $EXP_TAG --class_id $CLASS_ID --uri $URI
+      echo command phase2.pl --exp_tag $EXP_TAG --class_id $CLASS_ID --uri $URI --camera $CAMERA
     end
-    ## XXX this is annoying: phase2.pl now requires camera :: it should not need this, it has the file.
-    command phase2.pl --exp_tag $EXP_TAG --class_id $CLASS_ID --uri $URI --camera CTIO_MOSAIC2
-  end
-
-  # success
-  task.exit 0
-    # phase2imfile.pl updates DB tables, here we just update the queue
-    queuepush Phase2Imfiles -replace -key 1:2 "DONE $options:0"
+    command phase2.pl --exp_tag $EXP_TAG --class_id $CLASS_ID --uri $URI --camera $CAMERA --outpath $OUTPATH
   end
 
   # default exit status
   task.exit    default
-    showcommand failure
-    queuepush Phase2Imfiles -replace -key 1:2 "FAIL $options:0"
-    $Phase2ImfileFail ++
+    process_exit p2PendingImfile $options:0 $JOB_STATUS
   end
 
-  # operation times out?
+  # operation timed out?
   task.exit    timeout
     showcommand timeout
-    queuepush Phase2Imfiles -replace -key 1:2 "TIMEOUT $options:0"
-    $Phase2ImfileFail ++
+    book setword p2PendingImfile $options:0 state TIMEOUT
   end
 end
