#! /bin/csh -f
# Last edited on 2003-11-20 22:58:04 by stolfi

echo "=== begin $0 =========" 

set usopt = "[ -show ]"
set usage = "$0 ${usopt} RUN BAND TAG"

set show = 0
while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-show" ) ) then
    set show = 1; shift;
  else
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 3 ) then
  echo "usage: ${usage}"; exit 1
endif
set run = "$1"; shift;
set band = "$1"; shift;
set tag = "$1"; shift;

set maxPtDist =  5.0
set maxCvDist = `gawk -v h=${band} 'BEGIN{printf "%.6f\n", 3*0.18/exp(1.2*log(h));}'`
set maxSyDist = `gawk -v h=${band} 'BEGIN{printf "%.6f\n", 3*0.18/exp(1.2*log(h));}'`

echo "maxPtDist = ${maxPtDist}"
echo "maxCvDist = ${maxCvDist}"
echo "maxSyDist = ${maxSyDist}"

set input = "RUN-${run}/f${band}-${tag}"
set output = "RUN-${run}/f${band}-${tag}-d2h"

echo "${input} -> ${output}-{Pt,Cv,Sy}.plt"

@ maxCands = 5 * ${band} 

nice -n 19 PZDistSqrHistogram \
      -input ${input} \
      -maxCands ${maxCands} \
      -chainDir . -chainPrefix f \
      -band ${band} \
      -maxPtDist ${maxPtDist} \
      -maxCvDist ${maxCvDist} \
      -maxSyDist ${maxSyDist} \
      -output ${output}

# Convert to Postscript and show it:

foreach type ( "Pt" "Cv" "Sy" ) 

  if ( "/${type}" == "/Pt" ) then
    set title = "point distance"
    set xunits = "pixel^2"
    set maxdist = "${maxPtDist}"
    set nbins = 51
  else if ( "/${type}" == "/Cv" ) then
    set title = "curvature difference"
    set xunits = "pixel^-2"
    set maxdist = "${maxCvDist}"
    set nbins = 51
  else if ( "/${type}" == "/Sy" ) then
    set title = "quantized curvature difference"
    set xunits = "pixel^-2"
    set maxdist = "${maxSyDist}"
    set nbins = 53
  else 
    echo "bad chain type ${type}"; exit 1
  endif

  set pltFile = "${output}-${type}.plt"
  set epsFile = "${output}-${type}.eps"
  if ( -r ${pltFile} ) then 
    draw-dist-sqr-histogram \
      ${maxdist} ${nbins} \
      "${pltFile}" \
      "${epsFile}" \
      "${band}" \
      "${title}" \
      "${xunits}"
    if ( $show ) then
      ghostview ${epsFile}
    endif
  else
    echo "${pltFile} not found"
  endif
end

echo "=== end $0 =========" 

