#!/bin/csh -f
#
# Simple script to plot the time series in PBO .pos file.
#
if( $#argv == 0 ) then
  echo "sh_plot_pbots:  Simple plotting script for PBO .pos file"
  echo "Usage: "
  echo "sh_plot_pbots <opts> <Names of .pos files>"
  echo "plots will be in same directory as .pos file with .ps extent"
  echo " "
  echo "When final and rapid position files are available, the rapid results"
  echo "will be appended to the finals and plotted in red"
  exit
endif

# Set defaults
set errline = N
gmtset PAPER_MEDIA letter

set opts = `echo $argv | grep '\-f ' | wc -w`
if( $opts > 0 ) then
#  Options have been passed

#  decipher command line
   while ($#argv > 0 )
      set input = ( $argv )
      switch($input[1])
	   case -err*:
              set errline = 'Y'
              breaksw
	   case -f:
           case -file:
               set files        =  (  $input[2-] )
               breaksw
      endsw
      shift argv
   end
else
   set files = ( $argv )
endif


# OK Loop over the list of files
foreach posf ( `echo $files`)
#
#    See if lock is present:
     while ( -e pbots.lock ) 
        sleep 1m
     end
     touch pbots.lock

#    Get ps file
     set psf = $posf:r.ps
     echo -n "Creating $psf "
#    See if there is a rapid file that goes with it
     set rapf = `echo $posf | awk '{sub("final","rapid")} {print $0}'`
     if( -e $rapf ) echo " Appending $rapf"
     if( ! -e $rapf ) echo " "
#
#    extract out the North displacement and convert to mm
     set verf = `head  -3 $posf | grep Vers | wc -l`
#     if( $verf == 0 ) then 
        grep '^ ' $posf | awk '{if( $19 < 0.1 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$16*1e3,$19*1e3)}}' >! t.nf
#     else
#        tail -n +10 $posf | awk '{if( $19 < 0.1 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$16*1e3,$19*1e3)}}' >! t.nf
#     endif 
     if( -e $rapf ) then
         set verr = `head  -3 $rapf | grep Vers | wc -l`
#         if( $verr == 0 ) then 
             grep '^ ' $rapf | awk '{if( $19 < 0.1 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$16*1e3,$19*1e3)}}' >! t.nr
#         else
#             tail -n +10 $rapf | awk '{if( $19 < 0.1 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$16*1e3,$19*1e3)}}' >! t.nr
#        endif
         cat t.nf t.nr >! t.n
     else
         cat t.nf >! t.n
     endif
#
     set scale = `minmax -I0.05/5 t.n` 
#    Bug in GMT does not seem to do y-limits correct in -C mode.  
     set limx = `minmax -C -I.05 t.n | awk '{print $1,$2}'`
     set xtick = `echo $limx | awk '{if( $2-$1 > 5 ) {print "a1f0.5"} else if ( $2-$1 > 2 ) {print "a0.5f0.1"} else if ( $2-$1 > 0.5 ) {print "a0.2f0.05"} else {print "a0.05f0.01"}}'`
     set limy = `minmax -C -I5 t.n | awk '{print $3,$4}'`
     set ytick = `echo $limy | awk '{if( $2-$1 > 100 ) {print "a20f5"} else if ( $2-$1 > 10 ) {print "a10f2"} else {print "a2f1"}}'`


     psxy t.nf $scale -JX6/2.5 -B${xtick}:"Year":/${ytick}:"North (mm)":/SWne -Sc0.04 -G0/0/0 -Y7.5 -Ey0/1/200/200/200 -K  -P >! $psf
     if( -e $rapf ) psxy t.nr $scale -JX  -Sc0.03 -W1/255/0/0 -G255/0/0 -Ey0/2/250/200/200 -K -O -P >> $psf
     if( $errline == Y ) then
         awk '{print $1,$3}' t.nf t.nr >! t.ne
         echo $limx | awk '{printf(" %s  0.00 \n %s 0.00\n",$1,$2)}' >! t.zl
         psxy t.ne $scale -JX  -W1/0/0/255 -K -O -P >> $psf
         psxy t.zl $scale -JX  -W1/0/0/255 -K -O -P >> $psf
     endif
     echo $limx $limy  $posf | awk '{print $1, $4+($4-$3)/20, "12 0 0 BL File: "$5}' >! t.t
     pstext t.t -R -JX -K -N -O >> $psf

#    Do the east component
     grep '^ ' $posf | awk '{if( $20 < 0.1 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$17*1e3,$20*1e3)}}' >! t.ef
     if( -e $rapf ) then 
         grep '^ '  $rapf | awk '{if( $20 < 0.1 ){printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$17*1e3,$20*1e3)}}' >! t.er
         cat t.ef t.er >! t.e
     else
         cat t.ef >! t.e
     endif
#
     set scale = `minmax -I0.05/5 t.e`   
     set limy = `minmax -C -I5 t.e | awk '{print $3,$4}'`
     set ytick = `echo $limy | awk '{if( $2-$1 > 100 ) {print "a20f5"} else if ( $2-$1 > 10 ) {print "a10f2"} else {print "a2f1"}}'`

     psxy t.ef $scale -JX6/2.5 -B${xtick}:"Year":/${ytick}:"East (mm)":/SWne -Sc0.04 -G0/0/0 -Y-3.2 -Ey0/1/200/200/200 -K -O -P >> $psf
     if( -e $rapf ) psxy t.er $scale -JX  -Sc0.03 -W1/255/0/0 -G255/0/0 -Ey0/2/250/200/200 -K -O -P >> $psf
     if( $errline == Y ) then
         awk '{print $1,$3}' t.ef t.er >! t.ee
         psxy t.ee $scale -JX  -W1/0/0/255 -K -O -P >> $psf
         psxy t.zl $scale -JX  -W1/0/0/255 -K -O -P >> $psf
     endif

#    Do the UP component
     grep '^ ' $posf | awk '{if( $21 < 0.2 ) {printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$18*1e3,$21*1e3)}}' >! t.uf
     if( -e $rapf ) then
         grep '^ ' $rapf | awk '{if( $21 < 0.2 ){printf(" %10.5f %10.5f %10.5f\n", ($3-51544.0)/365.25+2000,$18*1e3,$21*1e3)}}' >! t.ur
         cat t.uf t.ur >! t.u
     else
         cat t.uf >! t.u
     endif
#
     set scale = `minmax -I0.05/5 t.u`   
     set limy = `minmax -C -I5 t.u | awk '{print $3,$4}'`
     set ytick = `echo $limy | awk '{if( $2-$1 > 100 ) {print "a20f5"} else if ( $2-$1 > 10 ) {print "a10f2"} else {print "a2f1"}}'`

     if( -e $rapf ) then
        psxy t.uf $scale -JX6/2.5 -B${xtick}:"Year":/${ytick}:"Height (mm)":/SWne -Sc0.04  -G0/0/0 -Y-3.2 -Ey0/1/200/200/200 -K -O -P >> $psf
        if( $errline == Y ) then
            awk '{print $1,$3}' t.uf t.ur >! t.ue
            psxy t.ue $scale -JX  -W1/0/0/255 -K -O -P >> $psf
            psxy t.zl $scale -JX  -W1/0/0/255 -K -O -P >> $psf
        endif
        psxy t.ur $scale -JX  -Sc0.03 -W1/255/0/0 -G255/0/0 -Ey0/2/250/200/200  -O -P >> $psf
     else
        if( $errline == Y ) then
            awk '{print $1,$3}' t.uf t.ur >! t.ue
            psxy t.ue $scale -JX  -W1/0/0/255 -K -O -P -Y-3.2 >> $psf
            psxy t.zl $scale -JX  -W1/0/0/255 -K -O -P >> $psf
            psxy t.uf $scale -JX6/2.5 -Ba0.2f0.05:"Year":/f5:"Height (mm)":/SWne -Sc0.04  -G0/0/0        -Ey0/1/200/200/200 -O -P >> $psf
        else
            psxy t.uf $scale -JX6/2.5 -Ba0.2f0.05:"Year":/f5:"Height (mm)":/SWne -Sc0.04  -G0/0/0 -Y-3.2 -Ey0/1/200/200/200 -O -P >> $psf
        endif
     endif
     \rm pbots.lock
end
\rm pbots.lock t.n t.e t.u t.nf  t.ef t.uf t.nr t.er t.ur t.t t.ne t.ee t.eu t.zl >&! /dev/null

