#! /bin/csh -fe
if ($#argv < 6) then
  echo "UNWRAP_PAR: unwrap phase with using parameters from the ISP interferogram parameter file 8-May-2000 clw"
  echo ""
  echo "usage: UNWRAP_PAR <interf_par> <interf> <cc> <pwr> <unwrap> <flag> [corr_thr] [pwr_thr] [r_init] [az_init] [r1] [r2] [l1] [l2]"
  echo "       interf_par       interferogram parameter file *.off"
  echo "       interf           interferogram filename  (*.int, *.flt)"
  echo "       cc               correlation filename (*.cc)"
  echo "       pwr              intensity image (*.pwr, *.mli)"
  echo "       unwrap           unwrap output file (*.unw)"
  echo "       flag             unwapping flag file (*.flag)"
  echo "       corr_thr         threshold for correlation in the unwrapping mask (default=.3)"
  echo "       pwr_thr	        intensity threshold for phase unwrapping neutrons, multiples of average (default = 6.)"  
  echo "       r_init           range seed location in the interferogram"
  echo "       az_init          azimuth seed location in the interferogram"
  echo "       r1               starting range sample offset to unwrap"
  echo "       r2               ending range sample offset to unwrap"
  echo "       l1		starting line offset to unwrap"
  echo "       l2               ending line offset to unwrap\n"
  exit
endif


set TREE = tree_cc
set RESIDUE = residue

set CORR_THR = .7
set MAX_BRANCH = 32
set NTHR = 6.0

echo ""
echo "interferogram/offset parameter file: $1"
echo `awk '$1 == "title:"' $1`

set w1 =   `awk '$1 == "interferogram_width:" {print $2}' $1`
set nls =  `awk '$1 == "interferogram_azimuth_lines:" {print $2}' $1`
set nrb =  `awk '$1 == "first_nonzero_range_pixel:" {print $2}' $1`
set nrps = `awk '$1 == "number_of_nonzero_range_pixels:" {print $2}' $1`

echo interferogram width, '#'lines: $w1 $nls
echo offset to first interferogram pixel, '#'interferogram pixels: $nrb $nrps

set r1 = $nrb
set r2 = `echo $nrb $nrps 1|awk '{print $1+$2-$3}'`
set l1 = 0
set l2 = `echo $nls 1 0|awk '{print $1-$2-$3}'`

if ($#argv >= 11) then
  set r1 = $11
  echo "starting pixel range offset: $r1"
endif

if ($#argv >= 12) then
  set r2 = $12
  echo "ending pixel range offset:   $r2"
endif

if ($#argv >= 13) then
  set l1 = $13
  echo "starting line offset: $l1"
endif

if ($#argv >= 14) then
  set l2 = $14
  echo "ending line offset:   $l2"
endif

set INTF = $2
set CCF = $3
set PWR1F = $4
set UNWF = $5
set FLAGF = $6

if ($#argv >= 7) set CORR_THR = $7
if ($#argv >= 8) set NTHR = $8

echo ""
echo "low correlation threshold:    $CORR_THR"
echo "neutron intensity threshold:  $NTHR"
echo "maximum tree branch length:   $MAX_BRANCH"
echo ""

if ($#argv >= 9) then
  set R_SEED = $9
  echo "range seed location:   $R_SEED"
endif

if ($#argv >= 10) then
  set AZ_SEED = $10
  echo "azimuth seed location: $AZ_SEED"
endif

if (!(-r $CCF)) then
  echo ""
  echo "ERROR: correlation file does not exist: $CCF"
  echo ""
  exit(-1)
endif

if (!(-r $PWR1F)) then
  echo ""
  echo "ERROR: intensity image file file does not exist: $PWR1F"
  echo ""
  exit(-1)
endif

rm -f $FLAGF $UNWF

echo ""
echo "corr_flag $CCF $FLAGF $w1 $CORR_THR" 
corr_flag $CCF $FLAGF $w1 $CORR_THR 

echo ""
echo "$RESIDUE $INTF $FLAGF $w1"
$RESIDUE $INTF $FLAGF $w1 

echo ""
echo "neutron $PWR1F $FLAGF $w1 $NTHR"
neutron $PWR1F $FLAGF $w1 $NTHR

echo "$TREE $FLAGF $w1 $MAX_BRANCH $r1 $r2 $l1 $l2" 
$TREE $FLAGF $w1 $MAX_BRANCH $r1 $r2 $l1 $l2 

echo ""
if ($#argv >= 9)then
  echo "grasses $INTF $FLAGF $UNWF $w1 $r1 $r2 $l1 $l2 $R_SEED $AZ_SEED"
  grasses $INTF $FLAGF $UNWF $w1 $r1 $r2 $l1 $l2 $R_SEED $AZ_SEED
else
  echo "grasses $INTF $FLAGF $UNWF $w1 $r1 $r2 $l1 $l2"
  grasses $INTF $FLAGF $UNWF $w1 $r1 $r2 $l1 $l2
endif

echo ""
echo "rasrmg $UNWF $PWR1F $w1 1 1 0 1 1 .333"
rasrmg $UNWF $PWR1F $w1 1 1 0 1 1 .333

echo ""
echo "rastree $FLAGF $UNWF $INTF $w1 1 0 1.0 $FLAGF.ras"
rastree $FLAGF $UNWF $INTF $w1 1 0 1.0 $FLAGF.ras

echo ""
echo "rastree $FLAGF $UNWF $INTF $w1 1 0 .3333  $FLAGF"2.ras""
rastree $FLAGF $UNWF $INTF $w1 1 0 .3333 $FLAGF'2.ras'

echo ""
echo "interferogram file:        $INTF"
echo "intensity file from SLC-1: $PWR1F"
echo "correlation file:          $CCF"
echo "unwrapping flag file:      $FLAGF"
echo "unwrapped interferogram:   $UNWF"
echo "SUN raster image of flag file (phase scale 1.00): $FLAGF.ras"
echo "SUN raster image of flag file (phase scale .333): $FLAGF"2.ras""
echo ""

exit(0)

