#!/bin/csh -f
#
if( $#argv == 0 ) then
    echo " "
    echo "Script to generate PBO velocity file"
    echo "Usage:"
    echo "% sh_pbor_vel <globk velocity .org file> <glist file>"
    echo " "
    echo "The eq_files used in the globk velocity should be used in generation of glist file"
    echo "   with the same data as used in the globk solution.  When combined GLX files are used, "
    echo "   a gdl file with the original daily list of glx files will generate more accurate time"
    echo "   ranges for the velocity estimates"
    echo "PBO velocity file with be original .org file root with .pbovel extent"
    echo "NOTE: PBOP output option must be set (default in globk 5.22 and beyond"
    echo " "
    exit
endif

set if = $1
set of = $if:r.pbovel
set gl = $2

#
# Get all the site lines
set ts =  `date +"%Y%m%d%H%M%S"`

# MOD TAH 080724: Get the reference frame information
set refframe = `grep '^ Reference Frame        :' $if | awk '{print substr($0,26,16)}'`
echo "PBO Velocity file from $if Reference Frame : $refframe" >! $of
echo "Format Version: 1.1.0" >> $of
echo "Release Date  : $ts" >> $of
echo "Start Field Description" >> $of
echo "Dot#        4-character identifier for a given station" >> $of
echo "Name        16-character station name" >> $of
echo "Ref_epoch   Date and time at which the station position is as given in ref_XYZ and ref_NEU.  Also the date and time for which the given velocity is first valid.Format is YYYYMMDDhhmmss." >> $of  
echo "Ref_jday    Reference epoch, represented as Modified Julian day" >> $of
echo "Ref_X       Reference X coordinate in SNARF reference frame at Ref_epoch, meters" >> $of
echo "Ref_Y       Reference Y coordinate in SNARF reference frame at Ref_epoch, meters" >> $of
echo "Ref_Z       Reference Z coordinate in SNARF reference frame at Ref_epoch, meters" >> $of
echo "Ref_Nlat    Reference North latitude in SNARF reference frame WGS-84 ellipsoid, decimal degrees" >> $of
echo "Ref_Elong   Reference East Longitude in SNARF reference frame WGS-84 ellipsoid, decimal degrees" >> $of
echo "Ref_Up      Referemce Height  in SNARF reference frame WGS-84 ellipsoid, decimal degrees" >> $of
echo "dX/dt       X component of station velocity, meters/yr" >> $of
echo "dY/dt       Y component of station velocity, meters/yr" >> $of
echo "dZ/dt       Z component of station velocity, meters/yr" >> $of
echo "SXd         Standard deviation of X velocity,  meters/yr" >> $of
echo "SYd         Standard deviation of Y velocity,  meters/yr" >> $of
echo "SZd         Standard deviation of Z velocity,  meters/yr" >> $of
echo "Rxy         Correlation of X and Y velocity, meters/yr" >> $of
echo "Rxz         Correlation of X and Z velocity, meters/yr" >> $of
echo "Ryz         Correlation of Y and Z velocity, meters/yr" >> $of
echo "dN/dt       North component of station velocity, meters/yr" >> $of
echo "dE/dt       East component of station velocity, meters/yr" >> $of
echo "dU/dt       Vertical component of station velocity, meters/yr" >> $of
echo "SNd         Standard deviation of North velocity, meters/yr" >> $of
echo "SEd         Standard deviation of East velocity, meters/yr" >> $of
echo "SUd         Standard deviation of vertical velocity, meters/yr" >> $of
echo "Rne         Correlation of North and East velocity, meters/yr" >> $of
echo "Rnu         Correlation of North and vertical velocity, meters/yr" >> $of
echo "Reu         Correlation of East and vertical velocity, meters/yr" >> $of
echo "first_epoch Epoch of first data used to derive the station velocity, in the same format as ref_epoch." >> $of
echo "last_epoch  Epoch of last data used to derive the station velocity, in the same format as ref_epoch." >> $of
echo "End Field Description " >> $of
echo "*Dot#     Name           Ref_epoch      Ref_jday      Ref_X          Ref_Y           Ref_Z         Ref_Nlat        Ref_Elong     Ref_Up...     dX/dt    dY/dt   dZ/dt    SXd     SYd     SZd    Rxy     Rxz    Rzy      dN/dt     dE/dt    dU/dt   SNd     SEd     SUd     Rne    Rnu    Reu   first_epoch     last_epoch " >> $of

set siteord = `grep '^pbr.' $if | awk '{print substr($2,1,4)}' | sort -u`
set sites = ''
foreach s ( $siteord )
    set newsites = `grep "^pbr. $s" $if | awk '{print $2}'`
    set sites = `echo $sites $newsites`
end
\rm vel.tmp ; touch vel.tmp
foreach s ( `echo $sites` )
    set times = `grep P$ $gl | grep $s | awk '{print $5,$6}'`
#   Convert times
    set date = `echo $times[1] | awk '{print int($1)," 1 ", int(365*($1-int($1)))+1}'`
    set sd = `doy $date | head -1 | awk '{print substr($2,1,4)substr($2,6,2)substr($2,9,2)"000000"}'`
    set date = `echo $times[2] | awk '{print int($1)," 1 ", int(365*($1-int($1)))+2}'`
    set ed = `doy $date | head -1 | awk '{print substr($2,1,4)substr($2,6,2)substr($2,9,2)"000000"}'`
    echo $s $sd $ed
    grep '^pbr.' $if | grep $s | awk -v sd=$sd -v ed=$ed '{print substr($0,5,5),substr($0,15,279),sd,ed}' >> vel.tmp
end
cat vel.tmp >> $of

echo "Created $of"

# ALGO_GPS Algonquin        20050827120000 53609.5000    918129.37559 -4346071.28056  4561977.85384   45.9558002587  281.9286318860  200.91944 0.00155-0.00459 0.00272 0.00014 0.00035 0.00035 -0.496  0.476 -0.840  -0.00155 0.00057 0.00531 0.00014 0.00012 0.00048  0.001 -0.114  0.040  18581117000000 18581117000000
