#!/bin/csh -f
# Compute network solution for sites on one day
# base yyyy-mm-dd
# Modified by Andrea Donnellan from baseline written by Michael Heflin
# Jet Propulsion Laboratory
# California Institute of Technology
# further modified by Greg Lyzenga to run at HMC

# Offer some help when name is typed without arguments
if ((help == "$1") || ("$1" == "-H") || ("$1" == ""))  then
more << End_of_help
USAGE
        network YYYY-MM-DD

DESCRIPTION
        Expects rinex files to exist in directory where network is run
        Peforms point positioning for a specific station-day
        Gets precise orbits and clocks from sideshow.jpl.nasa.gov
        Creates YYYY-MM-DD.SITES.stacov - free-network position estimate
        Creates YYYY-MM-DD.SITES.ITRF00.stacov - position estimate in ITRF96
        Creates YYYY-MM-DD.SITES.tdp - troposphere, clock, and phase biases

EXAMPLE
        network 1997-08-01 >& 1997-08-01.log &

SEE ALSO
        stacov, stamrg, statistics, project, transform

AUTHOR
        Michael Heflin with modifications by Andrea Donnellan and Greg Lyzenga
End_of_help
exit 1
endif

# Set up variables required for file retrieval
echo $1 > temp.$$
echo $1
set yyyy = `awk '{print substr($1,1,4)}' temp.$$`
set yy = `awk '{print substr($1,3,2)}' temp.$$`
set mm = `awk '{print substr($1,6,2)}' temp.$$`
set mm2 = `awk '{printf("%d\n",substr($1,6,2))}' temp.$$`
set dd = `awk '{print substr($1,9,2)}' temp.$$`
set months = 'jan feb mar apr may jun jul aug sep oct nov dec'
set i = 1
foreach elem($months)
 if ($i == $mm2) then
  set mmm = $elem
  set MMM = `echo $mmm | tr "a-z" "A-Z"`
 endif
 @ i++
end
set doy = `cal2doy $yyyy $mm $dd | awk '{print $5}'`
echo $doy
if ($doy < 10) then
 echo 'blah1'
 set doy = 00$doy
else if ($doy < 100) then
 echo 'blah2'
 set doy = 0$doy
endif
set yymmmdd = $yy$mmm$dd
set yyMMMdd = $yy$MMM$dd
/bin/rm temp.$$

# Get orbits and clocks from sideshow.
set file1 = $1'.shad.Z'
set file2 = $1'_nf.eci.Z'
set file3 = $1'tpeo_nf.nml.Z'
set file4 = $1'_nf.tdpc.Z'
set file5 = $1'.JPL00_nf.qx.Z'
set file6 = $yymmmdd'.itrf05.x'
set file7 = $1'.IGS00_nf.qx.Z'
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/${yyyy}/orbits/${file1}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/${yyyy}/orbits/${file2}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/${yyyy}/orbits/${file3}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/${yyyy}/clocks/${file4}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/x-files/${file5}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/x-files/itrf05/${file6}
ncftpget sideshow.jpl.nasa.gov . /pub/gipsy_products/x-files/${file7}

# need to get new files in /pub/gipsy_products/x-files/itrf05/
# and they are of the form: $yymmmdd'.itrf05.x'

gunzip -f *.Z *.gz

# Make qmfile
foreach file ( *.rnx )
  if ( `grep "***" $file | wc -l` > 0 ) fix_star.rnx $file
  clockprep -i $file -o ${file}0 >>& clockprep.log
end
time qfront 300 qmfile0 *.rnx0 >& qfront.log

# Make site list for wash namelist
qm_sta qmfile0 > input.$$
grep -v GOLD input.$$ > inputw.$$
set string = `paste -s -d, input.$$ | sed "s/,/','/g"`
set stringw = `paste -s -d, inputw.$$ | sed "s/,/','/g"`

# Copy postbreak.nml, wash.nml, and wash_amb3.nml
/bin/cp /goa/local/postbreak.nml  .          ; chmod 644 postbreak.nml
sed -e "s/  DATREC(1,1) = /  DATREC(1,1) = '$stringw'/" \
    -e "s/  DATREC(1,2) = /  DATREC(1,2) = '$string'/" \
/goa/local/wash_net.nml > wash.nml
sed -e "s/  DATREC(1,1) = /  DATREC(1,1) = '$stringw'/" \
    -e "s/  DATREC(1,2) = /  DATREC(1,2) = '$string'/" \
/goa/local/wash_amb3.nml > wash_amb3.nml

# Generate qregres.nml...

set d = `echo $1:t | tr "-" " "`
set hvtime = 12
hvor $d $hvtime 00 00 input.$$ qregres.nml > hvor.log
if(! -z hvor.log) then
   echo ""
   echo "HVOR FAILED"
   /bin/cat hvor.log
   echo ""
   echo "STOPPING"
   exit -1
else
add_ocnld qregres.nml qregres.nml
grep -v 'Dd-Mmm-Yyyy Hh:Mm:Ss.ssss' /goa/local/qregres.nml >> qregres.nml
cat $1'tpeo_nf.nml' >> qregres.nml
sed '/station_model/r /goa/local/minp.insert' qregres.nml \
| tr "[:lower:]" "[:upper:]"   > Qregres.nml
/bin/mv -f Qregres.nml qregres.nml
replace '!    NMLWAHRK1' '     NMLWAHRK1' qregres.nml > & /dev/null
replace '!    NMLPOLTID' '     NMLPOLTID' qregres.nml > & /dev/null
set_qr_tropmap -i qregres.nml -m NIELL
/bin/rm -f *.qm >& /dev/null
gzip -q *.rnx >& /dev/null
endif

# Delete satellites not appearing on ecifile from qmfile:
sta qmfile0 /dev/null qm.sats
sort -o qm.sats qm.sats
del_qm -i qmfile0 -o qmfile1 \
       -s `awk '{print "GPS"$1}' $1'_nf.eci' | sort | comm -13 - qm.sats`
/bin/mv qmfile1 qmfile

# Standard estimation
time qregres -i qmfile -o rgfile -n qregres.nml -sc $1'_nf.eci' \
             -time_dep $1'_nf.tdpc' -shad $1'.shad'  \
             -pr qregres.log
time preprefilter wash.nml rgfile prefilter.txt >& misc.log
time prefilter prefilter.txt rgfile batch1.txt phase.txt >>& misc.log
time /goa/bin/filter batch1.txt wash.nml no_eci rgfile accume.nio smooth.nio \
       uinv.nio >>& misc.log
time smapper wash.nml no_eci accume.nio smooth.nio uinv.nio smsol.nio \
        smcov.nio smsig.nio >>& misc.log
time postfit wash.nml rgfile smsol.nio uinv.nio postfit.nio point.txt >>& misc.log

# Look for missed losses of lock and estimate new bias parameters.
postbreak -p postfit.nio -n postbreak.nml -b batch1.txt -o batch2.txt >>& misc.log
/bin/rm accume.nio smooth.nio uinv.nio smsol.nio smcov.nio
/bin/rm postfit.nio point.txt
time /goa/bin/filter batch2.txt wash.nml no_eci rgfile accume.nio smooth.nio \
     uinv.nio >>& misc.log
/bin/rm APVALSOUT TDPTABLE
time smapper wash.nml no_eci accume.nio smooth.nio uinv.nio smsol.nio \
     smcov.nio smsig.nio >>& misc.log
time postfit wash.nml rgfile smsol.nio uinv.nio postfit.nio point.txt >>& misc.log

# Delete outliers and perform final estimation.
if (-e point.txt) then
time edtpnt2 point.txt wash.nml rgfile accume.nio smooth.nio uinv.nio >>& misc.log
/bin/rm accume.nio smooth.nio smsol.nio smcov.nio postfit.nio point.txt
time /goa/bin/filter batch2.txt wash.nml no_eci rgfile accume.nio smooth.nio \
     uinv.nio >>& misc.log
/bin/rm APVALSOUT TDPTABLE
time smapper wash.nml no_eci accume.nio smooth.nio uinv.nio smsol.nio \
     smcov.nio smsig.nio >>& misc.log
time postfit wash.nml rgfile smsol.nio uinv.nio postfit.nio point.txt >>& misc.log
endif

/bin/rm APVALSOUT TDPTABLE smsol.nio smcov.nio smsig.nio
setenv     REAL              rgfile
setenv     REGRES            rgfile
setenv     AREGRES           aregres.nio
setenv     AMBIGONLOG        $1'ambigon.log'
time smapper wash_amb3.nml no_eci accume.nio smooth.nio \
     uinv.nio smsol.nio smcov.nio smsig.nio >& misc2.log
time edtpnt2 '' /goa/local/edtpnt2.nml aregres.nio accume.nio \
     smooth.nio uinv.nio >>& misc2.log
/bin/rm APVALSOUT TDPTABLE smsol.nio smcov.nio smsig.nio
time smapper wash.nml no_eci accume.nio smooth.nio uinv.nio smsol.nio \
     smcov.nio smsig.nio >>& misc2.log

# Make stacov file containing final position estimate
stacov -i smcov.nio -o $1'.stacov' -q qregres.nml

#if (-e $1'.IGS97_nf.qx') then
#apply -x $1'.IGS97_nf.qx' -i $1'.stacov' \
#      -o $1'.ITRF97.stacov' -r -t -s
#endif

if (-e $yymmmdd'.itrf05.x') then
/bin/rm $1'.ITRF05.stacov'
apply -x $yymmmdd'.itrf05.x' -i $1'.stacov' \
      -o $1'.ITRF05.stacov' -r -t -s
endif

if (-e $1'.JPL00_nf.qx') then
/bin/rm $1'.JPL00.stacov'
apply -x $1'.JPL00_nf.qx' -i $1'.stacov' \
      -o $1'.JPL00.stacov' -r -t -s
endif

# Clean up
/bin/rm *.txt
/bin/rm *.nio
#/bin/rm [a-z]*.nml
/bin/rm *qm*
/bin/rm site rgfile qregres.log clockprep.log ${1}ambigon.log postbreak*
/bin/rm sta_id.? sta_pos.? sta_svec.?
/bin/rm input*.$$
/bin/mv TDPTABLE $1'.tdp'
/bin/rm $1'.IGS97_nf.qx'
/bin/rm $1'.JPL00_nf.qx'
/bin/rm $yymmmdd'.itrf05.x' $yymmmdd'.itrf94.x'
/bin/rm $1'.shad'
/bin/rm $1'_nf.eci'
/bin/rm $1'_nf.tdpc'
/bin/rm $1'.tdp'
/bin/rm $1'tpeo_nf.nml'
/bin/rm *.rnx0
/bin/rm tmp*
#/bin/rm *.rnx
#/bin/rm ${1}.tdp

# statistics and summary...

echo "smapper results:" > SUMMARY
grep APPROX *.log >> SUMMARY
rm misc*

if (-e $1'.ITRF05.stacov') then
statistics -r $1'.ITRF05.stacov' -gc -nev -v
mv $1'.ITRF05.nev' $1'.ITRF05.rev'
statistics -r $1'.ITRF05.stacov' -gc -nev
echo "BIAS-FIXED SOLUTION in reference frame ITRF05:" >> SUMMARY
cat $1'.ITRF05.nev' >> SUMMARY
endif

if (-e $1'.JPL00.stacov') then
statistics -r $1'.JPL00.stacov' -gc -nev -v
mv $1'.JPL00.nev' $1'.JPL00.rev'
statistics -r $1'.JPL00.stacov' -gc -nev
echo "BIAS-FIXED SOLUTION in reference frame JPL00:" >> SUMMARY
cat $1'.JPL00.nev' >> SUMMARY
endif

