## magic.pro : support for the streak removal : -*- sh -*-

# test for required global variables
check.globals

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

### Initialise the books containing the tasks to do
book init magicToTree
book init magicToProcess
book init magicToMask

### Database lists
$magicTree_DB = 0
$magicProcess_DB = 0
$magicMask_DB = 0

### Check status of tasks
macro magic.status
  book listbook magicToTree
  book listbook magicToProcess
  book listbook magicToMask
end

### Reset tasks
macro magic.reset
  book init magicTree
  book init magicProcess
  book init magicMask
end

### Turn tasks on
macro magic.on
  task magic.tree.load
    active true
  end
  task magic.tree.run
    active true
  end
  task magic.process.load
    active true
  end
  task magic.process.run
    active true
  end
  task magic.mask.load
    active true
  end
  task magic.mask.run
    active true
  end
end

### Turn tasks off
macro magic.on
  task magic.tree.load
    active false
  end
  task magic.tree.run
    active false
  end
  task magic.process.load
    active false
  end
  task magic.process.run
    active false
  end
  task magic.mask.load
    active false
  end
  task magic.mask.run
    active false
  end
end

task	       magic.tree.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/magic.tree.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup magicTree
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

task	       magic.tree.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword magicTree $pageName pantaskState RUN
    book getword magicTree $pageName magic_id -var MAGIC_ID
    book getword magicTree $pageName exp_id -var EXP_ID
    book getword magicTree $pageName camera -var CAMERA
    book getword magicTree $pageName workdir -var WORKDIR_TEMPLATE
    book getword magicTree $pageName dbname -var DBNAME
    book getword magicTree $pageName tess_id -var TESS_DIR
    book getword magicTree $pageName ra -var RA
    book getword magicTree $pageName decl -var DEC

#    set.host.for.camera $CAMERA $MAGIC_ID
#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
    host anyhost
    $WORKDIR = $WORKDIR_TEMPLATE

    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s.mgc.%s" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID

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

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

    $run = magic_tree.pl --magic_id $MAGIC_ID --camera $CAMERA --tess_id $TESS_DIR --ra $RA --dec $DEC --outroot $outroot --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 magicTree $options:0 $JOB_STATUS
  end

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

task	       magic.process.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/magic.process.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup magicProcess
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

task	       magic.process.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword magicTree $pageName pantaskState RUN
    book getword magicTree $pageName magic_id -var MAGIC_ID
    book getword magicTree $pageName exp_id -var EXP_ID
    book getword magicTree $pageName node -var NODE
    book getword magicTree $pageName camera -var CAMERA
    book getword magicTree $pageName workdir -var WORKDIR_TEMPLATE
    book getword magicTree $pageName dbname -var DBNAME

#    set.host.for.camera $CAMERA $MAGIC_ID
#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
    host anyhost
    $WORKDIR = $WORKDIR_TEMPLATE

    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s.mgc.%s.%s" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID $NODE

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

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

    $run = magic_process.pl --magic_id $MAGIC_ID --camera $CAMERA --node $NODE --outroot $outroot --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 magicProcess $options:0 $JOB_STATUS
  end

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

task	       magic.mask.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/magic.mask.log

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

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup magicMask
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

task	       magic.mask.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword magicMask $pageName pantaskState RUN
    book getword magicMask $pageName magic_id -var MAGIC_ID
    book getword magicMask $pageName exp_id -var EXP_ID
    book getword magicMask $pageName camera -var CAMERA
    book getword magicMask $pageName workdir -var WORKDIR_TEMPLATE
    book getword magicMask $pageName dbname -var DBNAME

#    set.host.for.camera $CAMERA $MAGIC_ID
#    set.workdir.by.camera $CAMERA $MAGIC_ID $WORKDIR_TEMPLATE $default_host WORKDIR
    host anyhost
    $WORKDIR = $WORKDIR_TEMPLATE

    basename $TESS_DIR -var TESS_ID
    sprintf outroot "%s/%s/%s.mgc.%s.mask" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID

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

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

    $run = magic_mask.pl --magic_id $MAGIC_ID --camera $CAMERA --outroot $outroot --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 magicMask $options:0 $JOB_STATUS
   end

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

