## camera.pro : globals and support macros : -*- sh -*-
## this file contains the tasks for running the camera analysis stage
## these tasks use the book camPendingExp

# test for required global variables
check.globals

book init camPendingExp

macro camera.status
  book listbook camPendingExp
end

macro camera.reset
  book init camPendingExp
end

macro camera.on
  task camera.exp.load
    active true
  end
  task camera.exp.run
    active true
  end
end

macro camera.off
  task camera.exp.load
    active false
  end
  task camera.exp.run
    active false
  end
end

# this variable will cycle through the known database names
$camera_DB = 0

# select images ready for camera analysis
# new entries are added to camPendingExp
# skip already-present entries
task	       camera.exp.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/camera.exp.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup camPendingExp
  end

  # default exit status
  task.exit    default
    showcommand failure
  end

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

# run the cameraexposure script on pending images
task	       camera.exp.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 10

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

    book setword camPendingExp $pageName pantaskState RUN
    book getword camPendingExp $pageName camera -var CAMERA
    book getword camPendingExp $pageName exp_tag -var EXP_TAG
    book getword camPendingExp $pageName cam_id -var CAM_ID
    book getword camPendingExp $pageName workdir -var WORKDIR_TEMPLATE
    book getword camPendingExp $pageName dvodb  -var DVODB
    book getword camPendingExp $pageName dbname -var DBNAME
    book getword camPendingExp $pageName reduction -var REDUCTION

    # specify choice of remote host based on camera and chip (class_id)
    set.host.for.camera $CAMERA FPA

    # set the WORKDIR variable
    set.workdir.by.camera $CAMERA FPA $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 outroot "%s/%s/%s.cm.%s" $WORKDIR $EXP_TAG $EXP_TAG $CAM_ID

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

    $run = camera_exp.pl --exp_tag $EXP_TAG --cam_id $CAM_ID --camera $CAMERA --outroot $outroot --redirect-output
    if ("$REDUCTION" != "NULL")
      $run = $run --reduction $REDUCTION
    end
    if ("$DVODB" != "NULL")
      $run = $run --dvodb $DVODB
    end
    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 camPendingExp $options:0 $JOB_STATUS
  end

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

## XXX add a global path to output files  
