Index: /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.getchip.pt
===================================================================
--- /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.getchip.pt	(revision 42911)
+++ /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.getchip.pt	(revision 42911)
@@ -0,0 +1,184 @@
+
+# master book is btChipNight listing all CHIPNIGHT books
+# CHIPNIGHT books are named after the nights and chips, e.g. 20250704.XY01
+
+# get the next available chip (one chip from an exposure for a single night)
+task bt.getchip.run
+  periods      -poll $BT_POLL
+  periods      -exec $BT_EXEC
+  periods      -timeout 1800
+
+  task.exec
+    # if we are unable to run the 'exec', use a long retry time
+    periods -exec $BT_EXEC
+
+    if ($BT_PAUSE) break
+    
+    # how many hosts are defined?
+    book npages btChipNight -var Nnights
+
+    # if we have reached the end, restart the loop over chip hosts
+    if ($BT_CHIPNIGHT_NSEQ >= $Nnights)
+      $BT_CHIPNIGHT_NSEQ = 0
+      echo "restart loop"
+      break
+    end
+    
+    # XXX CHIPNIGHT could be the same as the page name
+    book getpage btChipNight $BT_CHIPNIGHT_NSEQ           -var BT_CHIPNIGHT_PAGE
+    book getword btChipNight $BT_CHIPNIGHT_PAGE CHIPNIGHT -var BT_CHIPNIGHT_BOOK
+    book getword btChipNight $BT_CHIPNIGHT_PAGE STATE     -var BT_CHIPNIGHT_STATE
+
+    # ENTRY tracks the sequence number in the CHIPNIGHT
+    book getword btChipNight $BT_CHIPNIGHT_PAGE ENTRY     -var BT_CHIPNIGHT_ENTRY
+
+    # PREVTABLE (previousTable) will be NULL at the start (populated below)
+    book getword btChipNight $BT_CHIPNIGHT_PAGE PREVTABLE -var previousTable
+
+    # is this chip/night ready to go?
+    if (("$BT_CHIPNIGHT_STATE" == "RUN") || ("$BT_CHIPNIGHT_STATE" == "FAIL"))
+      $BT_CHIPNIGHT_NSEQ ++
+      periods -exec 0.05
+      break
+    end
+
+    # BT_CHIPNIGHT_ENTRY tracks the currently active or most recently completed chip.
+    # The starting value should be -1.
+    $BT_CHIPNIGHT_ENTRY ++
+
+    # are there still chips to do for this chip/night?
+    book npages $BT_CHIPNIGHT_BOOK -var BT_CHIPNIGHT_NCHIP
+    if ($BT_CHIPNIGHT_ENTRY >= $BT_CHIPNIGHT_NCHIP) 
+      echo "done with chip/night $BT_CHIPNIGHT_BOOK"
+      $BT_CHIPNIGHT_NSEQ ++
+      periods -exec 0.05
+      break
+    end
+
+    # XXX do anything special at the start?
+    if ("$BT_CHIPNIGHT_STATE" == "DONE")
+      # NOP?
+    end
+
+    # select next ready page in book
+    book getpage $BT_CHIPNIGHT_BOOK $BT_CHIPNIGHT_ENTRY -var pageName
+    if ("$pageName" == "NULL")
+      # this is a programming error
+      stop
+      echo "PROGRAMMING ERROR"
+      break
+    end
+
+    # get the data from this page and copy to the correct chiphost book
+    bt.add.chip $BT_CHIPNIGHT_BOOK
+
+    # save info about currently active entry
+    book setword btChipNight $BT_CHIPNIGHT_PAGE STATE       RUN
+    book setword btChipNight $BT_CHIPNIGHT_PAGE ENTRY       $BT_CHIPNIGHT_ENTRY
+    book setword btChipNight $BT_CHIPNIGHT_PAGE PREVTABLE   $outTableReal
+
+    # XXX choose something sensible:
+    # stdout $mypath/$YEAR/$DATEOBS/log.burntool.run.$CHIP_ID.v$ITER
+    # stderr $mypath/$YEAR/$DATEOBS/log.burntool.run.$CHIP_ID.v$ITER
+
+    periods -exec 0.05
+    command echo selected $exposure, $class_id for processing
+  end
+
+  task.exit 0
+    bt.getchip.exit OK
+  end
+
+  # exit values other than 0:
+  task.exit    default
+    bt.getchip.exit FAIL
+    showcommand failure
+  end
+
+  task.exit    crash
+    bt.getchip.exit CRASH
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    bt.getchip TIMEOUT
+    showcommand timeout
+  end
+end
+
+macro bt.getchip.exit
+  if ($0 != 2)
+    echo "USAGE: bt.getchip.exit (quality)"
+    break
+  end
+
+  local myBook myPage chPage
+  $myBook = $options:0
+  $myPage = $options:1
+  $chPage = $options:2
+
+  book setword $myBook $myPage pantaskState DONE
+
+  # what else needs to happen after the job is done?
+
+  # decrement NRUN for this host
+  book getword btChipNight $chPage NRUN -var BT_CHIPNIGHT_NRUN
+  $BT_CHIPNIGHT_NRUN --
+  book setword btChipNight $chPage NRUN      $BT_CHIPNIGHT_NRUN
+
+  # update the night/exp/chip list as well
+  # this chip state set to DONE or FAIL
+  # book delpage burnhost.book $options:0
+end
+
+
+######### supporting methods #############
+
+# add a new chip to be processed
+macro bt.add.chip
+
+  if ($0 != 2)
+    echo "USAGE: bt.add.chip (srcbook)"
+    break
+  end
+
+  # need to prevent other tasks from modifying the relevant books
+  $BT_PAUSE = 1
+  
+  local rawImfileReal outTableReal previousTable expname chipID myHost
+
+  ### XXX need to track entry in btChipNight so bt.onechip.exit can update
+
+  book getword $BT_CHIPNIGHT_BOOK $pageName rawImfileReal -var rawImfileReal
+  book getword $BT_CHIPNIGHT_BOOK $pageName outTableReal  -var outTableReal 
+  book getword $BT_CHIPNIGHT_BOOK $pageName previousTable -var previousTable
+  book getword $BT_CHIPNIGHT_BOOK $pageName exposure      -var expname
+  book getword $BT_CHIPNIGHT_BOOK $pageName class_id      -var chipID
+  book getword $BT_CHIPNIGHT_BOOK $pageName datahost      -var myHost
+
+  book create $myHost; # this is a NOP if it exists
+
+  # is this a new book?  
+  book getword btChipHost $myHost NRUN -var NRUN
+  if ("$NRUN" == "NULL")
+     book newpage btChipHost $myHost
+     book setword btChipHost $myHost NRUN     0
+     book setword btChipHost $myHost HOSTNAME $myHost
+  end
+
+  book newpage $myHost $expname.$chipID
+  book setword $myHost $expname.$chipID rawImfileReal $rawImfileReal 
+  book setword $myHost $expname.$chipID outTableReal  $outTableReal  
+  book setword $myHost $expname.$chipID previousTable $previousTable 
+  book setword $myHost $expname.$chipID exposure      $expname
+  book setword $myHost $expname.$chipID class_id      $chipID
+  book setword $myHost $expname.$chipID fullname      $expname.$chipID; # XXX needed?
+  book setword $myHost $expname.$chipID datahost      $myHost
+  book setword $myHost $expname.$chipID pantaskState  INIT
+
+  $BT_PAUSE = 0
+end
+
+### XXXX when the processing for this chip is done (bt.onechip.pt), then the
+### corresponding entry in the CHIPNIGHT book needs to be updated (to DONE)
Index: /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.onechip.pt
===================================================================
--- /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.onechip.pt	(revision 42911)
+++ /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.onechip.pt	(revision 42911)
@@ -0,0 +1,161 @@
+
+# QQQ relevant variables:
+
+# btChipHost : book with list of chiphost books.  each of those books
+# has info on chips ready to be run. note that the chip host only
+# specified where the chip is stored.  It does NOT mean the job must
+# run on that host.  I am not using the pantasks / pcontrol methods to
+# limit the rate because I do not want to require jobs to be run on
+# their hosts, but I still want to limit the number of outstanding
+# jobs wanting data from a host.
+
+# $BT_CHIPHOST_NSEQ : current (or last) chiphost examined.  this task
+# loops over the full list of chiphost books
+
+# $BT_CHIPHOST_NMAX : max number of jobs allowed per chiphost
+
+if (not($?BT_SETUP))
+  $BT_SETUP = 1
+  $BT_CHIPHOST_NSEQ = 0
+  $BT_CHIPHOST_NMAX = 2
+  book create btChipHost
+
+  $BT_POLL = 10
+  $BT_EXEC = 2
+  $BT_PAUSE = 0
+end
+
+# run a single job (one chip from an exposure for a single night)
+task bt.onechip.run
+  periods      -poll $BT_POLL
+  periods      -exec $BT_EXEC
+  periods      -timeout 1800
+
+  task.exec
+    # if we are unable to run the 'exec', use a long retry time
+    periods -exec $BT_EXEC
+
+    if ($BT_PAUSE) break
+    
+    # how many hosts are defined?
+    book npages btChipHost -var Nhosts
+
+    # if we have reached the end, restart the loop over chip hosts
+    if ($BT_CHIPHOST_NSEQ >= $Nhosts)
+      $BT_CHIPHOST_NSEQ = 0
+      echo "restart loop"
+      break
+    end
+    
+    book getpage btChipHost $BT_CHIPHOST_NSEQ          -var BT_CHIPHOST_PAGE
+    book getword btChipHost $BT_CHIPHOST_PAGE HOSTNAME -var BT_CHIPHOST_BOOK
+    book getword btChipHost $BT_CHIPHOST_PAGE NRUN     -var BT_CHIPHOST_NRUN
+
+    # only run NMAX jobs per chiphost
+    if ($BT_CHIPHOST_NRUN >= $BT_CHIPHOST_NMAX) 
+      $BT_CHIPHOST_NSEQ ++
+      echo "try next ($BT_CHIPHOST_NSEQ) -- done loop"
+      periods -exec 0.05
+      break
+    end
+
+    # if nothing to do on this host, try the next host
+    book npages $BT_CHIPHOST_BOOK -var BT_CHIPHOST_NCHP
+    if ($BT_CHIPHOST_NCHP == 0) 
+      $BT_CHIPHOST_NSEQ ++
+      echo "try next ($BT_CHIPHOST_NSEQ) -- no chips"
+      periods -exec 0.05
+      break
+    end
+    
+    # select next ready page in book
+    book getpage $BT_CHIPHOST_BOOK 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL")
+      $BT_CHIPHOST_NSEQ ++
+      echo "try next ($BT_CHIPHOST_NSEQ) -- nothing ready"
+      periods -exec 0.05
+      break
+    end
+
+    book setword $BT_CHIPHOST_BOOK $pageName pantaskState       RUN
+    book getword $BT_CHIPHOST_BOOK $pageName rawImfileReal -var rawImfileReal
+    book getword $BT_CHIPHOST_BOOK $pageName outTableReal  -var outTableReal 
+    book getword $BT_CHIPHOST_BOOK $pageName previousTable -var previousTable
+    book getword $BT_CHIPHOST_BOOK $pageName exposure      -var exposure     
+    book getword $BT_CHIPHOST_BOOK $pageName class_id      -var class_id     
+    book getword $BT_CHIPHOST_BOOK $pageName fullname      -var fullname     
+    book getword $BT_CHIPHOST_BOOK $pageName datahost      -var BURNHOST
+
+    # XXX choose something sensible:
+    # stdout $mypath/$YEAR/$DATEOBS/log.burntool.run.$CHIP_ID.v$ITER
+    # stderr $mypath/$YEAR/$DATEOBS/log.burntool.run.$CHIP_ID.v$ITER
+
+    # XXX choose a reasonable host
+    host any
+    
+    # sprintf psvidOutput "%s/%s/%s" $mypath $YEAR $DATEOBS
+    sprintf psvidOutput "%s.vid" $outTableReal
+    if ("$previousTable" == "NULL")
+      $addtable =
+    else
+      $addtable = --prevtable=$previousTable
+    end
+
+    # if we are able to run the 'exec', use a short retry time to launch all outstanding jobs
+    # save the chiphost (book & page), and chiphost page for exit tasks
+    option $BT_CHIPHOST_BOOK $pageName $BT_CHIPHOST_PAGE
+
+    $BT_CHIPHOST_NRUN ++
+    book setword btChipHost $BT_CHIPHOST_PAGE NRUN $BT_CHIPHOST_NRUN
+
+    periods -exec 0.05
+    command echo burntool_one_chip.pl --rerun --imfile $rawImfileReal --outtable=$outTableReal $addtable 
+  end
+
+  task.exit 0
+    bt.onechip.exit OK
+  end
+
+  # exit values other than 0:
+  task.exit    default
+    bt.onechip.exit FAIL
+    showcommand failure
+  end
+
+  task.exit    crash
+    bt.onechip.exit CRASH
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    bt.onechip TIMEOUT
+    showcommand timeout
+  end
+end
+
+macro bt.onechip.exit
+  if ($0 != 2)
+    echo "USAGE: bt.onechip.exit (quality)"
+    break
+  end
+
+  local myBook myPage chPage
+  $myBook = $options:0
+  $myPage = $options:1
+  $chPage = $options:2
+
+  book setword $myBook $myPage pantaskState DONE
+
+  # what else needs to happen after the job is done?
+
+  # decrement NRUN for this host
+  book getword btChipHost $chPage NRUN -var BT_CHIPHOST_NRUN
+  $BT_CHIPHOST_NRUN --
+  book setword btChipHost $chPage NRUN      $BT_CHIPHOST_NRUN
+
+  # update the night/exp/chip list as well
+  # this chip state set to DONE or FAIL
+  # book delpage burnhost.book $options:0
+end
+
Index: /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.test.pt
===================================================================
--- /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.test.pt	(revision 42911)
+++ /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/bt.test.pt	(revision 42911)
@@ -0,0 +1,60 @@
+
+# add a fake entry
+macro bt.add.chip
+
+  if ($0 != 4)
+    echo "USAGE: bt.add.chip (chiphost) (expname) (chipID)"
+    break
+  end
+
+  $BT_PAUSE = 1
+  
+  local myHost expname chipID
+  $myHost = $1
+  $expname = $2
+  $chipID = $3
+
+  book create $myHost; # this is a NOP if it exists
+
+  book getword btChipHost $myHost NRUN -var NRUN
+  if ("$NRUN" == "NULL")
+     book newpage btChipHost $myHost
+     book setword btChipHost $myHost NRUN     0
+     book setword btChipHost $myHost HOSTNAME $myHost
+  end
+
+  book newpage $myHost $expname.$chipID
+  book setword $myHost $expname.$chipID rawImfileReal /neb/$expname/$expname.$chipID.ch.fit
+  book setword $myHost $expname.$chipID outTableReal  /neb/$expname/$expname.$chipID.bt.fit
+  book setword $myHost $expname.$chipID previousTable NULL
+  book setword $myHost $expname.$chipID exposure      $expname
+  book setword $myHost $expname.$chipID class_id      $chipID
+  book setword $myHost $expname.$chipID fullname      $expname.$chipID
+  book setword $myHost $expname.$chipID datahost      $myHost
+  book setword $myHost $expname.$chipID pantaskState  INIT
+
+  $BT_PAUSE = 0
+end
+
+macro add.chips
+  for i 0 5
+    bt.add.chip ipp100 o500g00$i XY00
+    bt.add.chip ipp101 o500g00$i XY01
+    bt.add.chip ipp102 o500g00$i XY02
+  end
+end
+
+macro show.host
+  if ($0 != 3)
+    echo "USAGE: show.host (hostname) (chipname)"
+    break
+  end
+
+  local myHost myChip
+  $myHost = $1
+  $myChip = $2
+  
+  for i 0 50
+    book listpage $myHost o500g00$i.$myChip
+  end
+end
Index: /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/burntool.pt
===================================================================
--- /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/burntool.pt	(revision 42910)
+++ /branches/eam_branches/ipp-20250626/tools/eam/burntool.20250520/src/burntool.pt	(revision 42911)
@@ -13,10 +13,10 @@
 
 # we use the following lists 
-# BURNTOOL_BOOK_NAME  : list of the names of the night/chip books
-# BURNTOOL_BOOK_PAGE  : current active item in this book
-# BURNTOOL_BOOK_STATE : status of current active item (
+# BT_CHIPHOST_NAME  : list of the names of the night/chip books
+# BT_CHIPHOST_PAGE  : current active item in this book
+# BT_CHIPHOST_STATE : status of current active item (
 
 # run a single night/exposure/chip job
-task burntool.onechip.run
+task bt.onechip.run
   periods      -poll $RPOLL
   periods      -exec $REXEC
