## register.pro : tasks for image registration (insert into database) : -*- sh -*-
## this file contains the tasks for running the registration stage
## these tasks use the books regPendingImfile and regPendingExp

# test for required global variables
check.globals

book init regPendingImfile
book init regPendingExp

macro register.reset
  book init regPendingImfile
  book init regPendingExp
end

macro register.status
  book listbook regPendingImfile
  book listbook regPendingExp
end

macro register.on
  task register.imfile.load
    active true
  end
  task register.imfile.run
    active true
  end
  task register.exp.load
    active true
  end
  task register.exp.run
    active true
  end
end

macro register.off
  task register.imfile.load
    active false
  end
  task register.imfile.run
    active false
  end
  task register.exp.load
    active false
  end
  task register.exp.run
    active false
  end
end

# these variables will cycle through the known database names
$regPendingImfile_DB = 0
$regPendingExp_DB = 0

# select images ready for register analysis
# new entries are added to regPendingImfile
# compare the new list with the ones already selected
task	       register.imfile.load
  host         local

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

  # silently drop stdout
  stdout NULL
  stderr $LOGDIR/register.imfile.load.log

  # select entries from the current DB; cycle to the next DB, if it exists
  # iff the DB list is not set, use the value defined in .ipprc
  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command regtool -pendingimfile -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$regPendingImfile_DB
      command regtool -pendingimfile -limit 20 -dbname $DB:$regPendingImfile_DB
      $regPendingImfile_DB ++
      if ($regPendingImfile_DB >= $DB:n) set regPendingImfile_DB = 0
    end
  end

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup regPendingImfile
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

# run the register_imfile.pl script on pending images
task	       register.imfile.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 30

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

    book setword regPendingImfile $pageName pantaskState RUN

    book getword regPendingImfile $pageName exp_id       -var EXP_ID
    book getword regPendingImfile $pageName tmp_exp_name -var TMP_EXP_NAME
    book getword regPendingImfile $pageName tmp_camera   -var TMP_CAMERA
    book getword regPendingImfile $pageName tmp_class_id -var TMP_CLASS_ID
    book getword regPendingImfile $pageName uri          -var URI
    book getword regPendingImfile $pageName workdir      -var WORKDIR_TEMPLATE
    book getword regPendingImfile $pageName dbname       -var DBNAME

    # EXP_TAG is used to generate the unique, but human-readable, filenames
    sprintf EXP_TAG "%s.%s" $TMP_EXP_NAME $EXP_ID

    # specify choice of remote host
    set.host.for.camera $TMP_CAMERA $TMP_CLASS_ID 

    # set the WORKDIR variable
    set.workdir.by.camera $TMP_CAMERA $TMP_CLASS_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    # notes on how this works:
    # -- raw workdir examples:
    # file://data/@HOST@.0/gpc1/20080130
    # neb:///@HOST@-vol0/gpc1/20080130 (need to supply volname?, or are we re-defining this each time?)
    # -- out workdir examples:
    # file://data/ipp004.0/gpc1/20080130
    # neb:///ipp004-vol0/gpc1/20080130

    ## generate outroot specific to this exposure (& chip)
    sprintf logfile "%s/%s/%s.reg.%s.log" $WORKDIR $EXP_TAG $EXP_TAG $TMP_CLASS_ID

    stdout $LOGDIR/register.imfile.run.log
    stderr $LOGDIR/register.imfile.run.log

    # XXX register_imfile.pl differs from the standard script : it does not have an 'outroot' argument, and it does not take '--redirect'
    $run = register_imfile.pl --exp_id $EXP_ID --tmp_class_id $TMP_CLASS_ID --tmp_exp_name $TMP_EXP_NAME --uri $URI --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 regPendingImfile $options:0 $JOB_STATUS
  end

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

# select exposures ready for register_exp.pl
task	       register.exp.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/register.exp.load.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup regPendingExp
  end

  # default exit status
  task.exit    default
    showcommand failure
  end

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

# run the register_exp.pl script on pending exposures
task	       register.exp.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 30

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

    book setword regPendingExp $pageName pantaskState RUN
    book getword regPendingExp $pageName exp_id       -var EXP_ID
    book getword regPendingExp $pageName tmp_exp_name -var TMP_EXP_NAME
    book getword regPendingExp $pageName tmp_camera   -var TMP_CAMERA
    book getword regPendingExp $pageName workdir      -var WORKDIR_TEMPLATE
    book getword regPendingExp $pageName dbname       -var DBNAME

    # EXP_TAG is used to generate the unique, but human-readable, filenames
    sprintf EXP_TAG "%s.%s" $TMP_EXP_NAME $EXP_ID

    # specify choice of remote host
    set.host.for.camera $TMP_CAMERA $TMP_CLASS_ID 

    # set the WORKDIR variable
    set.workdir.by.camera $TMP_CAMERA $TMP_CLASS_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    # notes on how this works:
    # -- raw workdir examples:
    # file://data/@HOST@.0/gpc1/20080130
    # neb:///@HOST@-vol0/gpc1/20080130 (need to supply volname?, or are we re-defining this each time?)
    # -- out workdir examples:
    # file://data/ipp004.0/gpc1/20080130
    # neb:///ipp004-vol0/gpc1/20080130
    ## generate output log based on filerule
    sprintf logfile "%s/%s/%s.reg.log" $WORKDIR $EXP_TAG $EXP_TAG

    stdout $LOGDIR/register.exp.run.log
    stderr $LOGDIR/register.exp.run.log

    $run = register_exp.pl --exp_id $EXP_ID --exp_tag $EXP_TAG --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

  # success
  task.exit default
    process_exit regPendingExp $options:0 $JOB_STATUS
  end

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

## XXX add a global path to output files  
