## flatcorr.pro : tasks to run flat-field correction : -*- sh -*-
## this file contains the tasks for running the flatcorr stage
## these tasks use the book 'flatcorrBook'

# test for required global variables
check.globals

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

book init flatcoorBook

macro flatcorr.reset
  book init flatcorrBook
end

macro flatcorr.status
  book listbook flatcorrBook
end

macro flatcorr.on
  task flatcorr.init
    active true
  end
end

macro flatcorr.off
  task flatcorr.init
    active false
  end
end

# these variables will cycle through the known ippdb database names
$calInit_DB = 0

# we have three steps here:
# 1) get the list of dvo databases:  caltool -dbs
# 2) define a new flatcorr run for each of the dvo dbs
#    caltool 

# create new flatcorr entries for the currently known DVO databases
# run this multiple times once an hour - on pass for each db
task	       flatcorr.load
  host         local

  # check the list of pending flatcorr runs
  periods      -poll 10
  periods      -exec 900
  periods      -timeout 60
  npending 1

  # define the command (does not depend on previous queries)
  if ($DB:n == 0)
    command flatcorr -pending
  else
    # save the DB name for the exit tasks
    # note that this DB name refers to the ippdb, not the dvodb
    option $DB:$calInit_DB
    command flatcorr -pending -dbname $DB:$calInit_DB
    $calInit_DB ++
    if ($calInit_DB >= $DB:n) set calInit_DB = 0
  end

  # silently drop stdout
  stdout NULL
  stderr $LOGSUBDIR/flatcorr.log

  # success
  task.exit $EXIT_SUCCESS
    # convert 'stdout' to book format
    # XXX have ippTools report the dbname?
    ipptool2book stdout flatcorrBook -key dbname:corr_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook flatcorrBook
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup flatcorrBook
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

# create new flatcorr entries for the currently known DVO databases
# run this multiple times once an hour - one pass for each db
# can we include information that the db has been updated without a recent cal analysis?
task	       flatcorr.run
  host         local

  periods      -poll $LOADPOLL
  periods      -exec $LOADEXEC
  periods      -timeout 60
  # trange     Hourly@00:00 Hourly@00:10
  # every hour on the hour
  npending     1

  # silently drop stdout
  stdout NULL
  stderr $LOGSUBDIR/flatcorr.log

  task.exec
    book npages flatcorrBook -var N
    if ($N == 0) break
    if ($NETWORK == 0) break
    
    # look for new images in flatcorrBook
    # the sequencing in this task set is by the pantasksState (see notes.txt)
    book getpage flatcorrBook 0 -var pageName -key pantaskState INIT
    if ("$pageName" == "NULL") break

    book setword flatcorrBook $pageName pantaskState     RUN
    book setword flatcorrBook $pageName region           $REGION

    # XXX probably need to set the output / log based on WORKDIR...
    book getword flatcorrBook $pageName cal_id      -var ID
    book getword flatcorrBook $pageName dvodb       -var DVODB
    book getword flatcorrBook $pageName region      -var REGION
    book getword flatcorrBook $pageName filter      -var FILTER
    book getword flatcorrBook $pageName dbname      -var DBNAME

    # specify choice of remote host
    # set a specific DVO host here 
    if ($PARALLEL)
      host anyhost
    else
      host local
    end

    # save the pageName for future reference below
    options $pageName

    $run = flatcorr_proc.pl --corr_id $ID --dvodb $DVODB --region $REGION --filter $FILTER
    add_standard_args run

    # create the command line
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

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

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