#!/bin/csh
### Plot_tserie 1.0 12/01/06 

if ($#argv < 1) then
  echo ""
  echo 'USAGE: plot_tserie -s sta [-i input_dir] [-n noise] [-o cl/ww] [-off] [-lin] [-res]'
  echo '                   [-up] [-noplot] -b ' 
  echo ""
  echo "       -s      = site ID"
  echo "       -i      = input directory [raw/clean]" 
  echo "       -n      = [w/wf/wr/wk] white only/white+flicker/white+random/white+free index noise model [default = w]"
  echo "       -o      = outliers detection criteria [default cl=1.75; ww=1 year]"
  echo "       -off    = plotta serie senza offset [default offset in plot]"
  echo "       -lin    = sets inversion without periodic signals [default with]"
  echo "       -w      = write clean/residuals in output"
  echo "       -res    = plot residuals (without model)"
  echo "       -noplot = no plot "
  echo "       -b      = uses already stored cats output files"
  echo ""
  echo "EXAMPLE"
  echo "        plot_tserie -s tori -i raw -res"
  echo "        plot_tserie -s tori -i raw -n wf -o 1.75/1 -lin "
  echo ""
  echo "AUTHORS"
  echo "        Daniele Cheloni & Nicola D'Agostino (January 2006)"
  echo ""
  exit 1
endif

# -----------------------------------------------------------
#  cl = (res-mediana)/IQR (outlier se > cl)
#  ww = intervallo temporale per finestra mobile

set odc = "1.75/1"
set k = 1
set ioff = 1
set ires = 0
set res_flg = ""
set lin = 0
set maon = 0
set vert = 1
set iplot = 1
set noise = w
set dir = raw
set clean_dir = clean
set iw = 0
set ib = 0

while ($k <= $#argv)
 switch ($argv[$k])
  case -s:
   @ k++
   set sta = `echo $argv[$k] | tr "[a-z]" "[A-Z]"`
   @ k++
  breaksw

  case -n:
   @ k++
   set noise = $argv[$k]
   @ k++
  breaksw

  case -o:
   @ k++
   set odc = `echo $argv[$k]`   
   @ k++
  breaksw

  case -off:
   set ioff = 0
   @ k++
  breaksw

  case -lin:
   set lin = 1
   @ k++
  breaksw

  case -i:
   @ k++
   set dir = $argv[$k]
   @ k++
  breaksw

  case -res:
   set ires = 1
   set res_flg = "residuals"
   @ k++
  breaksw

  case -up:
   set vert = 1
   @ k++
  breaksw

  case -w:
   set iw = 1
   @ k++
  breaksw

  case -b:
   set ib = 1
   @ k++
  breaksw

  case -noplot
   set iplot = 0
   @ k++
  breaksw

 endsw
end

#
# --- Verifica che siano presenti le directories di output ---
#
if (! -d ./plots) then
 echo mkdir ./plots
 mkdir ./plots
endif
if (! -d ./clean) then
 echo mkdir ./clean
 mkdir ./clean
endif
if (! -d ./model) then
 echo mkdir ./model
 mkdir ./model
endif
if (! -d ./out) then
 echo mkdir ./out
 mkdir ./out
endif
if (! -d ./logs) then
 echo mkdir ./logs
 mkdir ./logs
endif
if (! -d ./res) then
 echo mkdir ./res
 mkdir ./res
endif
if (! -d ./cats_serie_in) then
 echo mkdir ./cats_serie_in
 mkdir ./cats_serie_in
endif
if (! -d ./vel) then
 echo mkdir ./vel
 mkdir ./vel
endif
if (! -d ./mle) then
 echo mkdir ./mle
 mkdir ./mle
endif
if (! -d ./no_off) then
 echo mkdir ./no_off
 mkdir ./no_off
endif

# -------------
 set NETWORK = "UNKNOWN"
if (-e /datagps/station_info/CGPS/sinex/${sta}.snx) then
 set xc = `grep POSITION /datagps/station_info/CGPS/sinex/${sta}.snx | cl 2`
 set yc = `grep POSITION /datagps/station_info/CGPS/sinex/${sta}.snx | cl 3`
 set zc = `grep POSITION /datagps/station_info/CGPS/sinex/${sta}.snx | cl 4`
 set NETWORK = `grep NETWORK /datagps/station_info/CGPS/sinex/${sta}.snx | awk '{print $2}'`
else if (! -e /datagps/station_info/CGPS/sinex/${sta}.snx) then
 set xc = `grep " X(m)" ./raw/${sta}.neu | cl 3`
 set yc = `grep " Y(m)" ./raw/${sta}.neu | cl 3`
 set zc = `grep " Z(m)" ./raw/${sta}.neu | cl 3`
else
 echo approx coordinates not found
 set xc = 0
 set yc = 0
 set zc = 0
endif
set clon = `xyz2gd $xc $yc $zc | cl 2`
set clat = `xyz2gd $xc $yc $zc | cl 1`

#
# --- CATS
#
# --- per stimare i parametri del modello con solo white noise 
#
echo ----------------------------------------------
echo $sta
echo " "
echo "I CATS run"
echo " "

if ($lin == 0) set A = "-A1y1"
if ($lin == 1) set A = ""

if ($noise == w)  then
 echo "Modello errore: white noise"
 set err = "-Mwh:"
 set noise_type = "white"
endif

if ($noise == f)  then
 echo "Modello errore: flicker noise"
 set err = "-Mpl:k-1"
 set noise_type = "flicker"
endif

if ($noise == wf) then
 echo "Modello errore: white + flicker noise"
 set err = "-Mpl:k-1 -Mwh:"
 set noise_type = "white+flicker"
endif

if ($noise == wr) then
 echo "Modello errore: white + random walk"
 set err = "-Mpl:k-2 -Mwh:"
 set noise_type = "white+random_walk"
endif

if ($noise == wk) then
 echo "Modello errore: white + free index"
 set err = "-Mpl: -Mwh:"
 set noise_type = "white+free_index"
endif

# -------------------------------
set here = `pwd`
set wdir = ${sta}_tmp.$$
mkdir $wdir
cd $wdir

set mle = ../mle/${sta}.${dir}.${noise}.mle
echo mle $mle

echo $odc | awk -F/ '{print $1}'  >  .cats_serie.par
echo $odc | awk -F/ '{print $2}'  >> .cats_serie.par
echo $ioff    >> .cats_serie.par
echo $lin     >> .cats_serie.par

#
# ================================= CATS =============================
#

if($ib == 0) then
echo "run cats"

rm -f $mle
echo cats ../${dir}/${sta}.neu $err $A -C7 -V -O${mle}
cats ../${dir}/${sta}.neu $err $A -C7 -V -O${mle}

#
# --- Legge il file di output di CATS ---
#

read_mle ../${dir}/${sta}.neu ${mle} 0 > cats_serie.in
cp cats_serie.in  ../cats_serie_in/${sta}.${dir}.${noise}.cats_serie.in

#
# --- CATS_SERIE: calcola il modello, i residui e rimuove gli outliers
#

cats_serie.e > ../logs/${sta}.${dir}.${noise}.log 
mv model.neu   ../model/${sta}.${dir}.${noise}.neu
mv serie_no_off.neu ../no_off/${sta}.${dir}.${noise}.neu
#rm -f cats_serie.in

cp residuals.neu  ../res/${sta}.${dir}.${noise}.neu

if ($iw == 1) then
cp clean.neu      ../clean/${sta}.neu
cp outliers.neu   ../out/${sta}.neu
endif

endif

# =====================================================================

if ($lin == 0) set trend = nlin
if ($lin == 1) set trend = lin

set model =   ../model/${sta}.${dir}.${noise}.neu
set res =     ../res/${sta}.${dir}.${noise}.neu
set log =     ../logs/${sta}.${dir}.${noise}.log
set outfile = ../out/${sta}.neu
set clean =   ../clean/${sta}.neu
if ($ires == 1) then
 set out =     ../plots/${sta}.${dir}.${noise}.${trend}.res.ps
 set Ytick = a10f5g200
else if ($ires == 0) then
 set out =     ../plots/${sta}.${dir}.${noise}.${trend}.ps
 set Ytick = a10f5
endif 

#
#
 
gmtdefaults -D > .gmtdefaults4
gmtset MEASURE_UNIT cm
gmtset ANOT_FONT_SIZE 9
gmtset LABEL_FONT_SIZE 12
gmtset HEADER_FONT_SIZE 20

set J = -JX14/6
set Xtick = a1f0.5
set otl = `wc -l ../out/${sta}.neu | awk '{print $1}'`
set tot = `wc -l ../raw/${sta}.neu | awk '{print $1}'` 
set rej = `echo $otl $tot | awk '{printf"%4.1f",$1/$2*100}'`

# --- Vel_Lon ------------------------------------------------
#
echo ''
echo 'Lon_Vel'
 
set tmin = `minmax -C $model | awk '{print $1-0.15}'`
set tmax = `minmax -C $model | awk '{print $2+0.15}'`
if($ires == 1) then 
 set ymin = `minmax -C $res | awk '{print ($5*1000)-10}'`
 set ymax = `minmax -C $res | awk '{print ($6*1000)+10}'`
 set tint = `minmax -C $res | awk '{printf"%5.2f\n", $2-$1}'`
else if ($ires == 0) then
 set ymin = `minmax -C $model | awk '{print ($5*1000)-10}'`
 set ymax = `minmax -C $model | awk '{print ($6*1000)+10}'`
 set tint = `minmax -C $model | awk '{printf"%5.2f\n", $2-$1}'`
endif
set ntot = `cat $log | grep "np" | awk '{print $4}'`
#set rej = `cat $log | grep "outliers" | awk '{printf"%4.2f\n", $6}'`
set lonvel = `cat $mle | grep "EAST  SLOPE" | awk '{printf"%4.2f\n", $4}'`
set rms = `cat $log | grep "EAST" | awk '{print $2}'`
set wrms = `cat $log | grep "EAST" | awk '{print $5}'`
set ek = `grep "+EAST  INDEX" ${mle} | grep -v FREE | head -1 | awk '{printf"%5.2f\n",$4}'`

set wnE = `cat $mle | grep "EAST" | grep "WH" | awk '{print $4}'`
set wnEAST = `echo $wnE | awk '{printf"%4.2f\n", $2}'`

if ($noise == w) set cnEAST = ne
if ($noise == wf || $noise == wr || $noise == wk || $noise == f) then
 set cnEAST = `cat $mle | grep "+EAST  PL " | grep -v FREE | awk '{printf"%4.2f\n",$4}'`
endif

set sigE = `cat $mle | grep "EAST  SLOPE" | awk '{printf"%4.2f\n", $6}'`
 
echo Time_interval = $tint
echo ntot = $ntot
echo rejected = $rej
echo lonvel = $lonvel "+-"  $sigE
echo rms = $rms
echo wrms = $wrms
echo white noise = $wnEAST
echo coloured noise = $cnEAST
echo index = $ek

set Rlon = -R$tmin/$tmax/$ymin/$ymax
 
psbasemap $J $Rlon -B${Xtick}::/${Ytick}:"Lon (mm)":WeSn -K -P -X4 -Y18.5 > $out
echo -1 9.7 10 0 2 LM $here | pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out
echo 12 9.7 12 0 2 LM  $NETWORK | pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out
echo "7 9.7 18 0 0 CM ${sta} " | pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out
echo "7 9.2 12 0 2 CM ("err_model: ${noise_type}")" | pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out
echo 7 8.2 14 0 0 CM "Time Span = $tint yrs;  N= $ntot ; $dir/$res_flg" \
| pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out

echo 1 7.2 12 0 0 LM "XYZ (m) = " $xc $yc $zc | pstext -Jx1 -R0/10/0/10 -W -N -K -O >> $out

foreach off (`cat ../${dir}/${sta}.neu | grep offset | awk '{print $3}'`)
echo $off $ymin >  off.tmp
echo $off $ymax >> off.tmp
psxy off.tmp $J $Rlon -W15/255/255/0  -K -O >> $out
psxy off.tmp $J $Rlon -W3/0t14_10:0 -B${Xtick}::/${Ytick}::WeSn -K -O >> $out
end

# ------- discontinuita' orbite flinn/flinnR -----------
#psxy $J $Rlon -W3/100/100/100ta -K -O << END >> $out
#2009.624 $ymin
#2009.624 $ymax
#END

if($ires == 1) then
 awk '{print $1,$3*1000,$6*1000}' $res | psxy $J $Rlon -Sc0.01 -W0.1/0 -Ey0.0/1/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$3*1000,$6*1000}' $res | psxy $J $Rlon -Sc0.1 -W1/0 -O -K -G255/0/0 >> $out
else if($ires == 0) then
 awk '{print $1,$3*1000,$6*1000}' ../${dir}/${sta}.neu | psxy $J $Rlon -W0.1/0 -Sc0.01 -Ey0.0/1/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$3*1000,$6*1000}' ../${dir}/${sta}.neu | psxy $J $Rlon -W1/0 -Sc0.1 -O -K -G255/0/0 >> $out
if ($iw == 1) then
grep "E" $outfile | awk '{print $1,$3*1000,$6*1000}' | psxy $J $Rlon -Sc0.2 -W1/0/0/255 -O -K >> $out 
endif
awk '{print $1,$3*1000}' $model | psxy $J $Rlon -W12/0/255/0 -K -O >> $out
awk '{print $1,$3*1000}' $model | psxy $J $Rlon -W2/0/0/0 -O -K >> $out
endif

echo "0.25 5.5 8 0 0 5 Rate (mm/yr) = $lonvel +- $sigE   WRMS = $wrms mm RMS = $rms mm" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i  -N -K -O >> $out 
echo "0.25 0.5 8 0 0 5 Spectral Index = $ek  White Noise = $wnEAST mm      Coloured  Noise = $cnEAST" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i  -N -K -O >> $out
echo "0.25 5.0 8 0 0 5 outliers = $otl   ($rej%)" |  pstext -Jx1 -R0/10/0/10 -G0/0/255 -W255o1/255 -C0.02i/0.02i -N -K -O >> $out

# --- Vel_Lat ----------------------------------------------------------------------------
#
echo ''
echo 'Lat_Vel'

if($ires == 0) then
 set ymin = `minmax -C $model | awk '{print ($3*1000)-10}'`
 set ymax = `minmax -C $model | awk '{print ($4*1000)+10}'`
 set tint = `minmax -C $model | awk '{printf"%5.2f\n", $2-$1}'`
 set tstart_end = `minmax -C $model | awk '{printf"%7.2f-%7.2f\n", $1,$2}'`
else if($ires == 1) then
 set ymin = `minmax -C $res | awk '{print ($3*1000)-10}'`
 set ymax = `minmax -C $res | awk '{print ($4*1000)+10}'`
 set tint = `minmax -C $res | awk '{printf"%5.2f\n", $2-$1}'`
 set tstart_end = `minmax -C $res | awk '{printf"%7.2f-%7.2f\n", $1,$2}'`
endif

set ntot = `cat $log | grep "np" | awk '{print $4}'`
set rej = `cat $log | grep "outliers" | awk '{printf"%4.2f\n", $5}'`
set latvel = `cat $mle | grep "NORT  SLOPE" | awk '{printf"%4.2f\n", $4}'`
set rms = `cat $log | grep "NORT" | awk '{print $1}'`
set wrms = `cat $log | grep "NORT" | awk '{print $4}'`
set nk = `grep "+NORT  INDEX" ${mle} | grep -v FREE | head -1 | awk '{printf"%5.2f\n",$4}'`

set wnN = `cat $mle | grep "NORT" | grep "WH" | awk '{print $4}'`
set wnNORT = `echo $wnN | awk '{printf"%4.2f\n", $2}'`

if ($noise == w) set cnNORT = ne
if ($noise == wf || $noise == wr || $noise == wk || $noise == f) then
  set cnNORT = `cat $mle | grep "+NORT  PL " | grep -v FREE | awk '{printf"%4.2f\n",$4}'`
endif

set sigN = `cat $mle | grep "NORT" | grep "SLOPE" | awk '{printf"%4.2f\n", $6}'`

echo Time_interval = $tint
echo Tstart-Tend = $tstart_end
echo ntot = $ntot
echo rejected = $rej
echo latvel = $latvel "+-" $sigN
echo rms = $rms
echo wrms = $wrms
echo white noise = $wnNORT
echo coloured noise = $cnNORT
echo index = $nk

set Rlat = -R$tmin/$tmax/$ymin/$ymax
 
psbasemap $J $Rlat -B${Xtick}::/${Ytick}:"Lat (mm)":WeSn -K -O -Y-7.5 >> $out
foreach off (`cat ../${dir}/${sta}.neu | grep offset | awk '{print $3}'`)
echo $off $ymin >  off.tmp
echo $off $ymax >> off.tmp
psxy off.tmp $J $Rlat -W15/255/255/0 -K -O >> $out
psxy off.tmp $J $Rlat -W3/0t14_10:0 -B${Xtick}::/${Ytick}::WeSn -K -O >> $out
end

# ------- discontinuita' orbite flinn/flinnR -----------
#psxy $J $Rlon -W3/100/100/100ta -K -O << END >> $out
#2009.624 $ymin
#2009.624 $ymax
#END

if($ires == 1) then
 awk '{print $1,$2*1000,$5*1000}' $res | psxy $J $Rlat -Sc0.01 -W0.1/0 -Ey0.0/1/150/150/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$2*1000,$5*1000}' $res | psxy $J $Rlat -Sc0.1 -W0.1/0 -O -K -G255/0/0 >> $out
else if($ires == 0) then
 awk '{print $1,$2*1000,$5*1000}' ../${dir}/${sta}.neu | psxy $J $Rlat -W0.1/0 -Sc0.01 -Ey0.0/1/150/150/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$2*1000,$5*1000}' ../${dir}/${sta}.neu | psxy $J $Rlat -W0.1/0 -Sc0.1 -O -K -G255/0/0 >> $out
if ($iw == 1) then
grep "N" $outfile | awk '{print $1,$2*1000,$5*1000}' | psxy $J $Rlat -Sc0.2 -W1/0/0/255 -O -K >> $out
endif
awk '{print $1,$2*1000}' $model | psxy $J $Rlat -W12/0/255/0 -K -O >> $out
awk '{print $1,$2*1000}' $model | psxy $J $Rlat -W2/0/0/0 -O -K >> $out
endif

echo "0.25 5.5 8 0 0 5 Rate (mm/yr) = $latvel +- $sigN  WRMS = $wrms mm RMS = $rms mm" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i  -N -K -O >> $out 
echo "0.25 0.5 8 0 0 5 Spectral Index = $nk    White Noise = $wnNORT mm      Coloured  Noise = $cnNORT" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i  -N -K -O >> $out
#echo "0.25 5.0 8 0 0 5 outliers = $otl   ($rej%)" |  pstext -Jx1 -R0/10/0/10 -G0/0/255 -W255o1/255 -C0.02i/0.02i -N -K -O >> $out

#
### --- Calcola la correlazione tra le coomponenti orizzontali di velocit -----
#

echo 'calcolo correlazione componenti orizzontali'

set tmp1 = tmp1.$$
set tmp2 = tmp2.$$

cat $res | awk '{print $1,$2*1000}' > $tmp1
cat $res | awk '{print $1,$3*1000}' > $tmp2

res_corr.e << END > ../res/${sta}_res_corr.log
$tmp1
$tmp2
END

set corr = `cat ../res/${sta}_res_corr.log | grep corr | awk '{print $3}'` 
#set corr = 0.0
rm -f ../res/${sta}_res_corr.log $tmp1 $tmp2

# --- Vel_Rad --------------------------------------------------------
#
echo ''
echo 'Rad_Vel'

if($ires == 0) then 
 set ymin = `minmax -C $model | awk '{print ($7*1000)-30}'`
 set ymax = `minmax -C $model | awk '{print ($8*1000)+30}'`
else if($ires == 1) then
 set ymin = `minmax -C $res | awk '{print ($7*1000)-30}'`
 set ymax = `minmax -C $res | awk '{print ($8*1000)+30}'`
endif

set ntot = `cat $log | grep "np" | awk '{print $4}'`
set rej = `cat $log | grep "outliers" | awk '{printf"%4.2f\n", $7}'`
set radvel = `cat $mle | grep "VERT  SLOPE" | awk '{printf"%4.2f\n", $4}'`
set rms = `cat $log | grep "VERT" | awk '{print $3}'`
set wrms = `cat $log | grep "VERT" | awk '{print $6}'`
set vk = `grep "+VERT  INDEX" ${mle} | grep -v FREE | head -1 | awk '{printf"%5.2f\n",$4}'` 

set wnV = `cat $mle | grep "VERT" | grep "WH" | awk '{print $4}'`
set wnVERT = `echo $wnV | awk '{printf"%4.2f\n", $2}'`

if ($noise == w) set cnVERT = ne
if ($noise == wf || $noise == wr || $noise == wk || $noise == f) then 
  set cnVERT = `cat $mle | grep "+VERT  PL " | grep -v FREE | awk '{printf"%4.2f\n",$4}'`
endif
  
set sigV = `cat $mle | grep "VERT  SLOPE" | awk '{printf"%4.2f\n", $6}'`

echo Time_interval = $tint
echo ntot = $ntot
echo rejected = $rej
echo radvel = $radvel "+-" $sigV
echo rms = $rms
echo wrms = $wrms
echo white noise = $wnVERT
echo coloured noise = $cnVERT
echo index = $vk

set Rrad = -R$tmin/$tmax/$ymin/$ymax

psbasemap $J $Rrad -B${Xtick}:Years:/${Ytick}:"Up (mm)":WeSn -K -O -Y-7.5 -U >> $out

foreach off (`cat ../${dir}/${sta}.neu | grep offset | awk '{print $3}'`)
echo $off $ymin >  off.tmp
echo $off $ymax >> off.tmp
psxy off.tmp $J $Rrad -W15/255/255/0 -K -O >> $out
psxy off.tmp $J $Rrad -W3/0t14_10:0 -B${Xtick}::/${Ytick}::WeSn -K -O >> $out
end

# ------- discontinuita' orbite flinn/flinnR -----------
#psxy $J $Rlon -W3/100/100/100ta -K -O << END >> $out
#2009.624 $ymin
#2009.624 $ymax
#END

if($ires == 1) then
 awk '{print $1,$4*1000,$7*1000}' $res | psxy $J $Rrad -Sc0.01 -W0.1/0 -Ey0.0/1/150/150/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$4*1000,$7*1000}' $res | psxy $J $Rrad -Sc0.1 -W0.1/0 -O -K -G255/0/0 >> $out
# awk '{print $1,$4*1000,$7*1000}' $res | psxy $J $Rrad -Sc0.1 -O -K -G255/0/0 >> $out
else if($ires == 0) then
 awk '{print $1,$4*1000,$7*1000}' ../${dir}/${sta}.neu | psxy $J $Rrad -W0.1/0 -Sc0.01 -Ey0.0/1/150/150/150 -O -K -G255/0/0 >> $out
 awk '{print $1,$4*1000,$7*1000}' ../${dir}/${sta}.neu | psxy $J $Rrad -W0.1/0 -Sc0.1 -O -K -G255/0/0 >> $out
# awk '{print $1,$4*1000,$7*1000}' ../${dir}/${sta}.neu | psxy $J $Rrad -Sc0.1 -O -K -G255/0/0 >> $out

if ($iw == 1) then
grep "V" $outfile | awk '{print $1,$4*1000,$7*1000}' | psxy $J $Rrad -Sc0.2 -W1/0/0/255 -O -K >> $out
endif
awk '{print $1,$4*1000}' $model | psxy $J $Rrad -W12/0/255/0 -K -O >> $out
awk '{print $1,$4*1000}' $model | psxy $J $Rrad -W2/0/0/0 -O -K >> $out
endif


echo "0.25 5.5 8 0 0 5 Rate (mm/yr) = $radvel +- $sigV  WRMS = $wrms mm RMS = $rms mm" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i -N -O -K >> $out
echo "0.25 0.5 8 0 0 5 Spectral Index = $vk    White Noise = $wnVERT mm      Coloured  Noise = $cnVERT" \
| pstext -Jx1 -R0/10/0/10 -W255o1/255 -C0.02i/0.02i -N -O -K >> $out

#echo "0.25 5.0 8 0 0 5 (Outliers = $rej %)" |  pstext -Jx1 -R0/10/0/10 -G0/0/255 -W255o1/255 -C0.02i/0.02i -N -O -K >> $out
#

#
### --- Scrive i files di velocit --------- 
#

#cat /raid/sta_info/*sta_pos | grep $sta | head -1 > tmp.sta_pos
#if (-z tmp.sta_pos) then
#cat sta_pos | grep $sta | head -1 > tmp.sta_pos
#echo find sta_pos in local dir
#endif
# set clon = `sta_pos2gd $sta tmp.sta_pos | awk '{print $1}'`
# set clat = `sta_pos2gd $sta tmp.sta_pos | awk '{print $2}'`
#rm -f tmp.sta_pos

echo 'clon clat ' $clon $clat $lonvel $latvel $sigE $sigN $corr $sta
echo  $clon $clat $lonvel $latvel $sigE $sigN $corr $sta $tint $ntot $tstart_end \
| awk '{printf "%12.3f%12.3f%8.2f%8.2f%8.2f%8.2f%8.2f%7s%8.2f%7s%18s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' > ../vel/${sta}.${dir}.${noise}.vel

if ($vert == 1) then
 echo 'clon clat upvel ' $clon $clat $radvel $sigV $sta
 echo  $clon $clat $radvel $sigV $sta $tint $ntot \
 | awk '{printf "%12.3f%12.3f%8.2f%8.2f%7s%8.2f%7s\n",$1,$2,$3,$4,$5,$6,$7}'  > ../vel/${sta}.${dir}.${noise}.up.vel
endif

echo "0.5 -0.5 12 0 0 5 $clon  $clat  $lonvel   $latvel   $sigE   $sigN   $corr  $sta  $tint  $ntot" \
| pstext -Jx1 -R0/10/0/10 -G0/0/0 -N -Y-2 -O >>  $out

if ($iplot == 1) gv $out &

rm -f model.neu residuals.neu outliers.neu clean.neu cats_serie.in 
cd $here
rm -r $wdir
