## 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

$LOGSUBDIR = $LOGDIR/stack
exec mkdir -p $LOGSUBDIR

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

### Database lists
$stackSkycell_DB = 0

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

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

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

### Turn stacking tasks off
macro stack.off
  task stack.skycell.load
    active false
  end
  task stack.skycell.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 $LOGSUBDIR/stack.skycell.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command stacktool -tosum -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$stackSkycell_DB
      command stacktool -tosum -limit 20 -dbname $DB:$stackSkycell_DB
      $stackSkycell_DB ++
      if ($stackSkycell_DB >= $DB:n) set stackSkycell_DB = 0
    end
  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

  # 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
    
    # 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 camera -var CAMERA
    book getword stackSumSkyfile $pageName workdir -var WORKDIR_TEMPLATE
    book getword stackSumSkyfile $pageName dbname -var DBNAME

    ### XXX Instead of setting these by camera, need to set by skycell_id
#    set.host.for.camera $CAMERA $STACK_ID
#    set.workdir.by.camera $CAMERA $STACK_ID $WORKDIR_TEMPLATE $default_host WORKDIR
    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

    ## generate output log based on filerule (convert the URI to a PATH) (use STACK_ID?)
#    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id $STACK_ID --basename $outroot`
    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id NONE --basename $outroot`
    if ("$logfile" == "") 
      echo "WARNING: logfile not defined in stack.pro:135"
      break
    end

    stdout $logfile
    stderr $logfile
    dirname $logfile -var outpath
    mkdir $outpath

    $run = stack_skycell.pl --stack_id $STACK_ID --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
    command $run
  end

  # default exit status
  task.exit    default
    process_exit stackSumSkyfile $options:0 $JOB_STATUS
  end

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

