#! /bin/csh -fe
# Last edited on 2003-11-20 22:59:14 by stolfi

set cmd = "$0"
set usage = "${cmd:t} RUN BAND OLDTAG NEWTAG MINLEN"

echo "=== begin ${cmd:t} ===================================="

set run = "$1"; shift;
set band = "$1"; shift;
set oldTag = "$1"; shift
set newTag = "$1"; shift
set minLength = "$1"; shift;

if ( $#argv > 0 ) then
  echo "usage: ${usage}"; exit 1
endif

echo "band = ${band} oldTag = ${oldTag} newTag = ${newTag}"

source RUN-${run}/f${band}.parms

@ maxCurveCands = 3 * ${band}
@ maxPairCands = ( ${band} + 1 ) / 2
set blurFactor = 3.0

set extraLength = `gawk -v bd=${band} -v bf=${blurFactor} 'BEGIN{printf "%.3f",(bf+1.0)*bd;}'`

set maxRefineShift = `gawk -v step=${step} 'BEGIN{printf "%.3f",3*step;}'`

# set maxInputCands = 50
set maxInputCands = 9999999

echo "step = ${step}"

echo "maxInputCands = ${maxInputCands}"
echo "minLength = ${minLength}"
echo "extraLength = ${extraLength}"
echo "blurFactor = ${blurFactor}"
echo "curvSigma = ${curvSigma}"
echo "critCurvDist = ${critCurvDist}"
echo "skipCurvDist = ${skipCurvDist}"
echo "maxCurvDist = ${maxCurvDist}"
echo "maxCurveCands = ${maxCurveCands}"
echo "maxRefineShift = ${maxRefineShift}"
echo "maxPairCands = ${maxPairCands}"

echo "maxDrawCands = ${maxDrawCands}"
echo "candDrawStep = ${candDrawStep}"

set input = "RUN-${run}/f${band}-${oldTag}"
set output = "RUN-${run}/f${band}-${newTag}"

echo "${input} -> ${output}"

nice -n 19 \
  PZRefineCands \
  -input ${input} \
  -chainDir . -chainPrefix f \
  -output ${output} \
  -band ${band} -step ${step} \
  -minLength ${minLength} \
  -blurFactor ${blurFactor} \
  -extraLength ${extraLength} \
  -curvSigma ${curvSigma} \
  -critDist ${critCurvDist} \
  -skipDist ${skipCurvDist} \
  -maxDist ${maxCurvDist} \
  -maxRefineShift ${maxRefineShift} \
  -maxPairCands ${maxPairCands} \
  -maxInputCands ${maxInputCands}
  
#nice -n 19 PZPrintCands \
#  -inName ${prefix}-f${band}-${newTag} \
#  -chainPrefix ${prefix} \
#  -band ${band} \
#  -maxCands 160
   
nice -n 19  PZDrawCands \
  -input ${output} \
  -chainPrefix f \
  -band ${band} \
  -output ${output}-can \
  -maxCands ${maxDrawCands} \
  -drawEvery ${candDrawStep}
 
echo "=== end ${cmd:t} ===================================="
