Index: /trunk/ippTasks/magic.pro
===================================================================
--- /trunk/ippTasks/magic.pro	(revision 18782)
+++ /trunk/ippTasks/magic.pro	(revision 18783)
@@ -8,57 +8,72 @@
 
 ### Initialise the books containing the tasks to do
+book init magicToTree
 book init magicToProcess
 book init magicToMask
-book init magicToSkyfileMask
 
 ### Database lists
-$magic_DB = 0
-
-### Check status of warping tasks
-#macro warp.status
-#  book listbook warpInputExp
-#  book listbook warpPendingSkyCell
-#end
-
-### Reset warping tasks
-#macro warp.reset
-#  book init warpInputExp
-#  book init warpPendingSkyCell
-#end
-
-### Turn warping tasks on
-# macro warp.on
-#   task warp.exp.load
-#     active true
-#   end
-#   task warp.exp.run
-#     active true
-#   end
-#   task warp.skycell.load
-#     active true
-#   end
-#   task warp.skycell.run
-#     active true
-#   end
-# end
-# 
-# ### Turn warping tasks off
-# macro warp.off
-#   task warp.exp.load
-#     active false
-#   end
-#   task warp.exp.run
-#     active false
-#   end
-#   task warp.skycell.load
-#     active false
-#   end
-#   task warp.skycell.run
-#     active false
-#   end
-# end
-
-# populate magicToProcess
-task	       magic.toprocess
+$magicTree_DB = 0
+$magicProcess_DB = 0
+$magicMask_DB = 0
+
+### Check status of tasks
+macro warp.status
+  book listbook magicToTree
+  book listbook magicToProcess
+  book listbook magicToMask
+end
+
+### Reset tasks
+macro warp.reset
+  book init magicTree
+  book init magicProcess
+  book init magicMask
+end
+
+### Turn tasks on
+macro warp.on
+  task magic.tree.load
+    active true
+  end
+  task magic.tree.run
+    active true
+  end
+  task magic.process.load
+    active true
+  end
+  task magic.process.run
+    active true
+  end
+  task magic.mask.load
+    active true
+  end
+  task magic.mask.run
+    active true
+  end
+end
+
+### Turn tasks off
+macro warp.on
+  task magic.tree.load
+    active false
+  end
+  task magic.tree.run
+    active false
+  end
+  task magic.process.load
+    active false
+  end
+  task magic.process.run
+    active false
+  end
+  task magic.mask.load
+    active false
+  end
+  task magic.mask.run
+    active false
+  end
+end
+
+task	       magic.tree.load
   host         local
 
@@ -69,20 +84,16 @@
 
   stdout NULL
-  stderr $LOGSUBDIR/magic.toprocess.log
+  stderr $LOGDIR/magic.tree.log
 
   task.exec
     if ($DB:n == 0)
       option DEFAULT
-      run = command magictool -toprocess -limit 20
-      if ($VERBOSE > 1)
-        echo command $run
-      end
-      command $run
+      command magictool -totree -limit 20
     else
       # save the DB name for the exit tasks
-      option $DB:$magic_DB
-      command magictool -toprocess -limit 20 -dbname $DB:$magic_DB
-      $magic_DB ++
-      if ($magic_DB >= $DB:n) set magic_DB = 0
+      option $DB:$magicTree_DB
+      command magictool -totree -limit 20 -dbname $DB:$magicTree_DB
+      $magicTree_DB ++
+      if ($magicTree_DB >= $DB:n) set magicTree_DB = 0
     end
   end
@@ -91,11 +102,11 @@
   task.exit    0
     # convert 'stdout' to book format
-    ipptool2book stdout magicToProcess -key magic_id:node -uniq -setword dbname $options:0 -setword pantaskState INIT
+    ipptool2book stdout magicTree -key magic_id -uniq -setword dbname $options:0 -setword pantaskState INIT
     if ($VERBOSE > 2)
-      book listbook magicToProcess
+      book listbook magicTree
     end
 
     # delete existing entries in the appropriate pantaskStates
-    process_cleanup magicToProcess
+    process_cleanup magicTree
   end
 
@@ -111,26 +122,74 @@
 end
 
-# task	 magic.process
-#   periods      -poll $loadpoll
-#   periods      -exec $loadexec
-#   periods      -timeout 30
-# 
-#   task.exec
-#   end
-# 
-#   # default exit status
-#   task.exit    default
-#     process_exit magicToProcess $options:0 $JOB_STATUS
-#   end
-# 
-#   # operation timed out?
-#   task.exit    timeout
-#     showcommand timeout
-#     book setword magicToProcess $options:0 pantaskState TIMEOUT
-#   end
-# end
-
-# populate magicToMask
-task	       magic.tomask
+task	       magic.tree.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 60
+
+  task.exec
+    book npages magicTree -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    
+    # look for new images (pantaskState == INIT)
+    book getpage magicTree 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword magicTree $pageName pantaskState RUN
+    book getword magicTree $pageName magic_id -var MAGIC_ID
+    book getword magicTree $pageName exp_id -var EXP_ID
+    book getword magicTree $pageName camera -var CAMERA
+    book getword magicTree $pageName workdir -var WORKDIR_TEMPLATE
+    book getword magicTree $pageName dbname -var DBNAME
+    book getword magicTree $pageName tess_id -var TESS_DIR
+    book getword magicTree $pageName ra -var RA
+    book getword magicTree $pageName decl -var DEC
+
+#    set.host.for.camera $CAMERA $MAGIC_ID
+#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+    host anyhost
+    $WORKDIR = $WORKDIR_TEMPLATE
+
+    basename $TESS_DIR -var TESS_ID
+    sprintf outroot "%s/%s/%s.mgc.%s" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID
+
+    ## generate output log based on filerule (convert the URI to a PATH)
+    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id $MAGIC_ID --basename $outroot`
+    if ("$logfile" == "") 
+      echo "WARNING: logfile not defined in magic.tree.run"
+      break
+    end
+
+    stdout $logfile
+    stderr $logfile
+    dirname $logfile -var outpath
+    mkdir $outpath
+
+    $run = magic_tree.pl --magic_id $MAGIC_ID --camera $CAMERA --tess_id $TESS_DIR --ra $RA --dec $DEC --outroot $outroot --logfile $logfile
+    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 magicTree $options:0 $JOB_STATUS
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword magicTree $options:0 pantaskState TIMEOUT
+  end
+end
+
+task	       magic.process.load
   host         local
 
@@ -141,20 +200,16 @@
 
   stdout NULL
-  stderr $LOGSUBDIR/magic.tomask.log
+  stderr $LOGDIR/magic.process.log
 
   task.exec
     if ($DB:n == 0)
       option DEFAULT
-      run = command magictool -tomask -limit 20
-      if ($VERBOSE > 1)
-        echo command $run
-      end
-      command $run
+      command magictool -toprocess -limit 20
     else
       # save the DB name for the exit tasks
-      option $DB:$magic_DB
-      command magictool -tomask -limit 20 -dbname $DB:$magic_DB
-      $magic_DB ++
-      if ($magic_DB >= $DB:n) set magic_DB = 0
+      option $DB:$magicTree_DB
+      command magictool -toprocess -limit 20 -dbname $DB:$magicProcess_DB
+      $magicProcess_DB ++
+      if ($magicProcess_DB >= $DB:n) set magicProcess_DB = 0
     end
   end
@@ -163,11 +218,11 @@
   task.exit    0
     # convert 'stdout' to book format
-    ipptool2book stdout magicToMask -key magic_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    ipptool2book stdout magicProcess -key magic_id:node -uniq -setword dbname $options:0 -setword pantaskState INIT
     if ($VERBOSE > 2)
-      book listbook magicToProcess
+      book listbook magicProcess
     end
 
     # delete existing entries in the appropriate pantaskStates
-    process_cleanup magicToMask
+    process_cleanup magicProcess
   end
 
@@ -183,7 +238,72 @@
 end
 
-
-# populate magicToMask
-task	       magic.toskyfilemask
+task	       magic.process.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 60
+
+  task.exec
+    book npages magicProcess -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    
+    # look for new images (pantaskState == INIT)
+    book getpage magicProcess 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword magicTree $pageName pantaskState RUN
+    book getword magicTree $pageName magic_id -var MAGIC_ID
+    book getword magicTree $pageName exp_id -var EXP_ID
+    book getword magicTree $pageName node -var NODE
+    book getword magicTree $pageName camera -var CAMERA
+    book getword magicTree $pageName workdir -var WORKDIR_TEMPLATE
+    book getword magicTree $pageName dbname -var DBNAME
+
+#    set.host.for.camera $CAMERA $MAGIC_ID
+#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+    host anyhost
+    $WORKDIR = $WORKDIR_TEMPLATE
+
+    basename $TESS_DIR -var TESS_ID
+    sprintf outroot "%s/%s/%s.mgc.%s.%s" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID $NODE
+
+    ## generate output log based on filerule (convert the URI to a PATH)
+    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id $MAGIC_ID --basename $outroot`
+    if ("$logfile" == "") 
+      echo "WARNING: logfile not defined in magic.process.run"
+      break
+    end
+
+    stdout $logfile
+    stderr $logfile
+    dirname $logfile -var outpath
+    mkdir $outpath
+
+    $run = magic_process.pl --magic_id $MAGIC_ID --camera $CAMERA --node $NODE --outroot $outroot --logfile $logfile
+    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 magicProcess $options:0 $JOB_STATUS
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword magicProcess $options:0 pantaskState TIMEOUT
+  end
+end
+
+task	       magic.mask.load
   host         local
 
@@ -194,20 +314,16 @@
 
   stdout NULL
-  stderr $LOGSUBDIR/magic.toskyfilemask.log
+  stderr $LOGDIR/magic.mask.log
 
   task.exec
     if ($DB:n == 0)
       option DEFAULT
-      run = command magictool -toskyfilemask -limit 20
-      if ($VERBOSE > 1)
-        echo command $run
-      end
-      command $run
+      command magictool -tomask -limit 20
     else
       # save the DB name for the exit tasks
-      option $DB:$magic_DB
-      command magictool -toskyfilemask-limit 20 -dbname $DB:$magic_DB
-      $magic_DB ++
-      if ($magic_DB >= $DB:n) set magic_DB = 0
+      option $DB:$magicMask_DB
+      command magictool -tomask -limit 20 -dbname $DB:$magicMask_DB
+      $magicMask_DB ++
+      if ($magicMask_DB >= $DB:n) set magicMask_DB = 0
     end
   end
@@ -216,11 +332,11 @@
   task.exit    0
     # convert 'stdout' to book format
-    ipptool2book stdout magicToSkyfileMask -key magic_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    ipptool2book stdout magicMask -key magic_id -uniq -setword dbname $options:0 -setword pantaskState INIT
     if ($VERBOSE > 2)
-      book listbook magicToProcess
+      book listbook magicMask
     end
 
     # delete existing entries in the appropriate pantaskStates
-    process_cleanup magicToSkyfileMask
+    process_cleanup magicMask
   end
 
@@ -235,2 +351,69 @@
   end
 end
+
+task	       magic.mask.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 60
+
+  task.exec
+    book npages magicMask -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    
+    # look for new images (pantaskState == INIT)
+    book getpage magicMask 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword magicMask $pageName pantaskState RUN
+    book getword magicMask $pageName magic_id -var MAGIC_ID
+    book getword magicMask $pageName exp_id -var EXP_ID
+    book getword magicMask $pageName camera -var CAMERA
+    book getword magicMask $pageName workdir -var WORKDIR_TEMPLATE
+    book getword magicMask $pageName dbname -var DBNAME
+
+#    set.host.for.camera $CAMERA $MAGIC_ID
+#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+    host anyhost
+    $WORKDIR = $WORKDIR_TEMPLATE
+
+    basename $TESS_DIR -var TESS_ID
+    sprintf outroot "%s/%s/%s.mgc.%s.mask" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID
+
+    ## generate output log based on filerule (convert the URI to a PATH)
+    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id $MAGIC_ID --basename $outroot`
+    if ("$logfile" == "") 
+      echo "WARNING: logfile not defined in magic.tree.run"
+      break
+    end
+
+    stdout $logfile
+    stderr $logfile
+    dirname $logfile -var outpath
+    mkdir $outpath
+
+    $run = magic_mask.pl --magic_id $MAGIC_ID --camera $CAMERA --outroot $outroot --logfile $logfile
+    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 magicMask $options:0 $JOB_STATUS
+   end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword magicMask $options:0 pantaskState TIMEOUT
+  end
+end
+
