## detrend.correct.pro : globals and support macros : -*- sh -*-
## this file contains the tasks for running the detrend correction stage
## these tasks use the books detPendingCorrectedImfile and detPendingCorrectedExp

# test for required global variables
check.globals

book init detPendingCorrectedImfile
book init detPendingCorrectedExp

macro detcorr.reset
  book init detPendingCorrectedImfile
  book init detPendingCorrectedExp
end

macro detcorr.status
  echo detPendingCorrectedImfile
  book listbook detPendingCorrectedImfile
  echo detPendingCorrectedExp
  book listbook detPendingCorrectedExp
end

macro detcorr.on
  task detrend.correct.load
    active true
  end
  task detrend.correct.run
    active true
  end
  task detrend.correctexp.load
    active true
  end
  task detrend.correctexp.run
    active true
  end
end

macro detcorr.off
  task detrend.correct.load
    active false
  end
  task detrend.correct.run
    active false
  end
  task detrend.correctexp.load
    active false
  end
  task detrend.correctexp.run
    active false
  end
end


# these variables will cycle through the known database names
$detPendingCorrectedImfile_DB = 0
$detPendingCorrectedExp_DB = 0

# select images ready for copy 
# new entries are added to detPendingCorrectedImfile
# compare the new list with the ones already selected
task	       detrend.correct.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/detcorr.load.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup detPendingCorrectedImfile
  end

  # error
  task.exit    default
    showcommand failure
  end

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

# run detrend_correct_imfile.pl on pending images
task	       detrend.correct.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 30

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

    book setword detPendingCorrectedImfile $pageName pantaskState RUN
    book getword detPendingCorrectedImfile $pageName det_id    -var DET_ID   
    book getword detPendingCorrectedImfile $pageName exp_id    -var EXP_ID  
    book getword detPendingCorrectedImfile $pageName class_id  -var CLASS_ID 
    book getword detPendingCorrectedImfile $pageName exp_tag   -var EXP_TAG  XXX where does this come from
    book getword detPendingCorrectedImfile $pageName det_type  -var DET_TYPE  XXX where does this come from
    book getword detPendingCorrectedImfile $pageName camera    -var CAMERA  XXX where does this come from
    book getword detPendingCorrectedImfile $pageName uri       -var URI      
    book getword detPendingCorrectedImfile $pageName corr_id   -var CORR_ID
    book getword detPendingCorrectedImfile $pageName corr_type -var CORR_TYPE
    book getword detPendingCorrectedImfile $pageName workdir   -var WORKDIR_TEMPLATE
    book getword detPendingCorrectedImfile $pageName dbname    -var DBNAME

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

    # set the WORKDIR variable
    set.workdir.by.camera $CAMERA $CLASS_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    ## generate outroot specific to this exposure (& chip)
    sprintf outroot "%s/%s.%s.%s/%s/%s.detcorr.%s" $WORKDIR $CAMERA $DET_TYPE $DET_ID $EXP_TAG $EXP_TAG $DET_ID

    stdout $LOGDIR/detcorr.run.log
    stderr $LOGDIR/detcorr.run.log

    $run = detrend_correct_imfile.pl --det_id $DET_ID --exp_id $EXP_ID --class_id $CLASS_ID --exp_tag $EXP_TAG --input_uri $URI --corr_id $CORR_ID --corr_type $CORR_TYPE --camera $CAMERA --outroot $outroot --redirect-output --verbose
    if ("$WORKDIR" != "NULL")
      $run = $run --workdir $WORKDIR
    end
    if ("$REDUCTION" != "NULL")
      $run = $run --reduction $REDUCTION
    end
    add_standard_args run

    # save the pageName for future reference below
    options $pageName

    # create example job options as a demonstration
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

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

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

# select images ready for copy 
# new entries are added to detPendingCorrectedExp
# compare the new list with the ones already selected
task	       detrend.correctexp.load
  host         local

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

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

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup detPendingCorrectedExp
  end

  # error
  task.exit    default
    showcommand failure
  end

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

# copy new images, sending job to desired host
task	       detrend.correctexp.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 30

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

    book setword detPendingCorrectedExp $pageName pantaskState RUN
    book getword detPendingCorrectedExp $pageName det_id    -var DET_ID
    book getword detPendingCorrectedExp $pageName exp_id    -var EXP_ID
    book getword detPendingCorrectedExp $pageName det_type  -var DET_TYPE
    book getword detPendingCorrectedExp $pageName camera    -var CAMERA  
    book getword detPendingCorrectedExp $pageName exp_tag   -var EXP_TAG
    book getword detPendingCorrectedExp $pageName corr_id   -var CORR_ID
    book getword detPendingCorrectedExp $pageName corr_type -var CORR_TYPE
    book getword detPendingCorrectedExp $pageName workdir   -var WORKDIR_TEMPLATE
    book getword detPendingCorrectedExp $pageName dbname    -var DBNAME

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

    # set workdir (interpolate host; see camera.pro for examples)
    set.workdir.by.camera $CAMERA FPA $WORKDIR_TEMPLATE $default_host WORKDIR

    ## generate outroot specific to this exposure (& chip)
    sprintf outroot "%s/%s.%s.%s/%s/%s.detcorr.%s" $WORKDIR $CAMERA $DET_TYPE $DET_ID $EXP_TAG $EXP_TAG $DET_ID

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

    $run = detrend_correct_exp.pl --det_id $DET_ID --exp_id $EXP_ID --exp_tag $EXP_TAG --det_type $DET_TYPE --camera $CAMERA --corr_id $CORR_ID --corr_type $CORR_TYPE --camera $CAMERA --redirect-output --verbose

    if ("$WORKDIR" != "NULL")
      $run = $run --workdir $WORKDIR
    end
    add_standard_args run

    # save the pageName for future reference below
    options $pageName

    # create example job options as a demonstration
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

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

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