## stack.pro : tasks for image stacking : -*- sh -*-

## This file contains panTasks definitions for performing the image stacking.
## After a stack (with associated stack_id) is defined, the stack is performed
## (tasks in stackSumSkyfile).

# test for required global variables
check.globals

### Initialise the books containing the tasks to do
book init stackSumSkyfile
#book init stackCleanup

### Database lists
$stackSkycell_DB = 0
$stack_revert_DB = 0
#$stackCleanup_DB = 0

### Check status of stacking tasks
macro stack.status
  book listbook stackSumSkyfile
#  book listbook stackCleanup
end

### Reset stacking tasks
macro stack.reset
  book init stackSumSkyfile
#  book init stackCleanup
end

### Turn stacking tasks on
macro stack.on
  task stack.skycell.load
    active true
  end
  task stack.skycell.run
    active true
  end
  task stack.revert
    active false
  end
end

### Turn stacking tasks off
macro stack.off
  task stack.skycell.load
    active false
  end
  task stack.skycell.run
    active false
  end
  task stack.revert
    active false
  end
end

macro stack.revert.on
  task stack.revert
    active true
  end
end

macro stack.revert.off
  task stack.revert
    active false
  end
end





# macro stack.cleanup.on
#   task stack.cleanup.load
#     active true
#   end
#   task stack.cleanup.run
#     active true
#   end
# end

# macro stack.cleanup.off
#   task stack.cleanup.load
#     active false
#   end
#   task stack.cleanup.run
#     active false
#   end
# end


### Load tasks for doing the stack
### Tasks are loaded into stackSumSkyfile.
task	       stack.skycell.load
  host         local

  periods      -poll $LOADPOLL
  periods      -exec $LOADEXEC
  periods      -timeout 30
  npending     1

  stdout NULL
  stderr $LOGDIR/stack.skycell.log

  task.exec
    if ($LABEL:n == 0) break
    $run = stacktool -tosum
    if ($DB:n == 0)
      option DEFAULT
    else
      # save the DB name for the exit tasks
      option $DB:$stackSkycell_DB
      $run = $run -dbname $DB:$stackSkycell_DB -limit 40
      $stackSkycell_DB ++
      if ($stackSkycell_DB >= $DB:n) set stackSkycell_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 stackSumSkyfile -key stack_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook stackSumSkyfile
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup stackSumSkyfile
  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 stack overlaps
### Tasks are taken from stackSumSkyfile.
task	       stack.skycell.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 10800

  task.exec
    book npages stackSumSkyfile -var N
    if ($N == 0) break
    if ($NETWORK == 0) break
    if ($BURNTOOLING == 1) break

    # look for new images in stackSumSkyfile (pantaskState == INIT)
    book getpage stackSumSkyfile 0 -var pageName -key pantaskState INIT
    if ("$pageName" == "NULL") break

    book setword stackSumSkyfile $pageName pantaskState RUN
    book getword stackSumSkyfile $pageName stack_id -var STACK_ID
    book getword stackSumSkyfile $pageName tess_id -var TESS_DIR
    book getword stackSumSkyfile $pageName skycell_id -var SKYCELL_ID
    book getword stackSumSkyfile $pageName workdir -var WORKDIR_TEMPLATE
    book getword stackSumSkyfile $pageName reduction -var REDUCTION
    book getword stackSumSkyfile $pageName dbname -var DBNAME
    book getword stackSumSkyfile $pageName state -var RUN_STATE

    # 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

    # XXX old code:
    # host anyhost
    # $WORKDIR = $WORKDIR_TEMPLATE

    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s/%s.%s.stk.%s" $WORKDIR $TESS_ID $SKYCELL_ID $TESS_ID $SKYCELL_ID $STACK_ID

    stdout $LOGDIR/stack.skycell.log
    stderr $LOGDIR/stack.skycell.log

    $run = stack_skycell.pl --threads @MAX_THREADS@ --stack_id $STACK_ID --outroot $outroot --redirect-output --run-state $RUN_STATE
    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 stackSumSkyfile $options:0 $JOB_STATUS
  end

  # locked list
  task.exit    crash
    showcommand crash
    echo "hostname: $JOB_HOSTNAME"
    book setword stackSumSkyfile $options:0 pantaskState CRASH
  end

  # operation timed out?
  task.exit    timeout
    showcommand timeout
    book setword stackSumSkyfile $options:0 pantaskState TIMEOUT
  end
end


# # select images ready for stack analysis
# # new entries are added to stackCleanup
# # skip already-present entries
# task	       stack.cleanup.load
#   host         local

#   periods      -poll $LOADPOLL
#   periods      -exec $LOADEXEC
#   periods      -timeout 30
#   npending     1
#   active       false

#   stdout NULL
#   stderr $LOGDIR/stack.cleanup.log

#   task.exec
#     if ($LABEL:n == 0) break
#     $run = stacktool -pendingcleanuprun
#     if ($DB:n == 0)
#       option DEFAULT
#     else
#       # save the DB name for the exit tasks
#       option $DB:$stackCleanup_DB
#       $run = $run -dbname $DB:$stackCleanup_DB
#       $stackCleanup_DB ++
#       if ($stackCleanup_DB >= $DB:n) set stackCleanup_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 stackCleanup -key stack_id -uniq -setword dbname $options:0 -setword pantaskState INIT
#     if ($VERBOSE > 2)
#       book listbook stackCleanup
#     end

#     # delete existing entries in the appropriate pantaskStates
#     process_cleanup stackCleanup
#   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 the ipp_cleanup.pl script on pending images
# task	       stack.cleanup.run
#   periods      -poll $RUNPOLL
#   periods      -exec $RUNEXEC
#   periods      -timeout 60
#   active       false

#   task.exec
#     book npages stackCleanup -var N
#     if ($N == 0) break
#     if ($NETWORK == 0) break
    
#     # look for new images in stackCleanup (pantaskState == INIT)
#     book getpage stackCleanup 0 -var pageName -key pantaskState INIT
#     if ("$pageName" == "NULL") break

#     book setword stackCleanup $pageName pantaskState RUN
#     book getword stackCleanup $pageName camera -var CAMERA
#     book getword stackCleanup $pageName state -var CLEANUP_MODE
#     book getword stackCleanup $pageName stack_id -var STACK_ID
#     book getword stackCleanup $pageName dbname -var DBNAME

#     # specify choice of local or remote host based on camera and stack (class_id)
#     set.host.for.camera $CAMERA FPA

#     stdout $LOGDIR/stack.cleanup.log
#     stderr $LOGDIR/stack.cleanup.log

#     # XXX is everything listed here needed?
#     $run = ipp_cleanup.pl --stage stack --stage_id $STACK_ID --camera $CAMERA --mode $CLEANUP_MODE
#     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 stackCleanup $options:0 $JOB_STATUS
#   end

#   task.exit    crash
#     showcommand crash
#     book setword stackCleanup $options:0 pantaskState CRASH
#   end

#   # operation timed out?
#   task.exit    timeout
#     showcommand timeout
#     book setword stackCleanup $options:0 pantaskState TIMEOUT
#   end
# end

task stack.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 = stacktool -revertsumskyfile -fault 2
    if ($DB:n == 0)
      option DEFAULT
    else
      # save the DB name for the exit tasks
      option $DB:$stack_revert_DB
      $run = $run -dbname $DB:$stack_revert_DB
      $stack_revert_DB ++
      if ($stack_revert_DB >= $DB:n) set stack_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

