## warp.pro : image warping tasks : -*- sh -*-
## This file contains panTasks definitions for performing the image warping.

### This is done in two (main) steps.  After a warp (with associated warp_id) is defined,
### overlaps between the exposure being warped and skycells are calculated (tasks in warpInputExp).
### Then for each skycell, the warp is made (tasks in warpPendingSkycell).

### Setups
check.globals

### Initialise the books containing the tasks to do
book init warpInputExp
book init warpPendingSkyCell

### Database lists
$warpExp_DB = 0
$warpSkycell_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

### Load tasks for calculating the warp overlaps
### Tasks are loaded into warpInputExp.
task	       warp.exp.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/warp.exp.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command warptool -tooverlap -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$warpExp_DB
      command warptool -tooverlap -limit 20 -dbname $DB:$warpExp_DB
      $warpExp_DB ++
      if ($warpExp_DB >= $DB:n) set warpExp_DB = 0
    end
  end

  # success
  task.exit    0
    # convert 'stdout' to book format
    ipptool2book stdout warpInputExp -key warp_id:fake_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook warpInputExp
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup warpInputExp
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

### Run tasks for calculating the warp overlaps
### Tasks are taken from warpInputExp.
task	       warp.exp.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword warpInputExp $pageName pantaskState RUN
    book getword warpInputExp $pageName warp_id -var WARP_ID
    book getword warpInputExp $pageName camera -var CAMERA
    book getword warpInputExp $pageName workdir -var WORKDIR_TEMPLATE
    book getword warpInputExp $pageName dbname -var DBNAME
    # XXX change tess_id to tess_dir when schema is changed
    book getword warpInputExp $pageName tess_id -var TESS_DIR
    book getword warpInputExp $pageName exp_id -var EXP_ID

    ### XXX Instead of setting these by camera, need to set by skycell_id
    set.host.for.camera $CAMERA $WARP_ID
    set.workdir.by.camera $CAMERA $WARP_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    ## generate outroot specific to this exposure
    sprintf logfile "%s/%s/%s.wrp.%s.log" $WORKDIR $EXP_ID $EXP_ID $WARP_ID

    stdout $LOGDIR/warp.exp.log
    stderr $LOGDIR/warp.exp.log

    # XXX warp_overlap.pl differs from the standard script : it does not have an 'outroot' argument, and it does not take '--redirect'
    $run = warp_overlap.pl --warp_id $WARP_ID --camera $CAMERA --tess_dir $TESS_DIR --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 warpInputExp $options:0 $JOB_STATUS
  end

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


### Load tasks for doing the warps
### Tasks are loaded into warpPendingSkyCell.
task	       warp.skycell.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/warp.skycell.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command warptool -towarped -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$warpSkycell_DB
      command warptool -towarped -limit 20 -dbname $DB:$warpSkycell_DB
      $warpSkycell_DB ++
      if ($warpSkycell_DB >= $DB:n) set warpSkycell_DB = 0
    end
  end

  # success
  task.exit    0
    # convert 'stdout' to book format
    # XXX change tess_id to tess_dir when db is updated
    ipptool2book stdout warpPendingSkyCell -key warp_id:skycell_id:tess_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook warpPendingSkyCell
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup warpPendingSkyCell
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

### Run tasks for calculating the warp overlaps
### Tasks are taken from warpPendingSkyCell.
task	       warp.skycell.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword warpPendingSkyCell $pageName pantaskState RUN
    book getword warpPendingSkyCell $pageName warp_id -var WARP_ID
    book getword warpPendingSkyCell $pageName skycell_id -var SKYCELL_ID
    # XXX change tess_id to tess_dir when schema is changed
    book getword warpPendingSkyCell $pageName tess_id -var TESS_DIR
    book getword warpPendingSkyCell $pageName camera -var CAMERA
    book getword warpPendingSkyCell $pageName workdir -var WORKDIR_TEMPLATE
    book getword warpPendingSkyCell $pageName dbname -var DBNAME

    ### XXX Instead of setting these by camera, need to set by skycell_id
    set.host.for.camera $CAMERA $WARP_ID
    set.workdir.by.camera $CAMERA $WARP_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    # XXX we are supplied TESS_DIR, strip off path to generate TESS_ID
    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s/%s.%s.wrp.%s" $WORKDIR $TESS_ID $SKYCELL_ID $TESS_ID $SKYCELL_ID $WARP_ID

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

    $run = warp_skycell.pl --warp_id $WARP_ID --skycell_id $SKYCELL_ID --tess_dir $TESS_DIR --camera $CAMERA --outroot $outroot --redirect-output
    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 warpPendingSkyCell $options:0 $JOB_STATUS
  end

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

