## diff.pro : image difference analysis : -*- sh -*-

## This file contains panTasks definitions for performing the image differencing.
## After a difference (with associated diff_id) is defined, the difference is performed
## (tasks in diffSkyfile).

# test for required global variables
check.globals

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

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

### Database lists
$diffSkycell_DB = 0

### Check status of diffing tasks
macro diff.status
  book listbook diffSkyfile
end

### Reset diffing tasks
macro diff.reset
  book init diffSkyfile
end

### Turn diffing tasks on
macro diff.on
  task diff.skycell.load
    active true
  end
  task diff.skycell.run
    active true
  end
end

### Turn diffing tasks off
macro diff.off
  task diff.skycell.load
    active false
  end
  task diff.skycell.run
    active false
  end
end


### Load tasks for doing the differences
### Tasks are loaded into diffSkyfile.
task	       diff.skycell.load
  host         local

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

  stdout NULL
  stderr $LOGSUBDIR/diff.skycell.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup diffSkyfile
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

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

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

    book setword diffSkyfile $pageName pantaskState RUN
    book getword diffSkyfile $pageName diff_id -var DIFF_ID
    book getword diffSkyfile $pageName tess_id -var TESS_DIR
    book getword diffSkyfile $pageName skycell_id -var SKYCELL_ID
    book getword diffSkyfile $pageName camera -var CAMERA
    book getword diffSkyfile $pageName workdir -var WORKDIR_TEMPLATE
    book getword diffSkyfile $pageName dbname -var DBNAME

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

    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s/%s.%s.dif.%s" $WORKDIR $TESS_ID $SKYCELL_ID $TESS_ID $SKYCELL_ID $DIFF_ID

    ## generate output log based on filerule (convert the URI to a PATH)
    $logfile = `ipp_filename.pl --filerule LOG.EXP --camera $CAMERA --class_id $DIFF_ID --basename $outroot`
    if ("$logfile" == "") 
      echo "WARNING: logfile not defined in diff.skycell.run"
      break
    end

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

    $run = diff_skycell.pl --diff_id $DIFF_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 diffSkyfile $options:0 $JOB_STATUS
  end

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

