#! /bin/csh -f

if ($#argv == 0) then
	echo ""
	echo "do_bline_01.csh -t doy yy -o orbit -r sample -s sites"
        echo "sample rate is in seconds (use 30,60, or 120)"
	echo ""
	echo "	orbit = IGS/IGR/IGP/SIO/SIR/SIP ..."
	echo ""
	exit
endif

# set defaults
set solid = "offs"
set setup = /usr/local/processing/gamit_setup
set tables = /usr/local/processing/gamit_tables
set work = ~/pin1
set sittab = $work/tables/sittbl.
set sestab = $work/tables/sestbl.

foreach a ($argv)
switch($a)
	case -t:
		set doyy = ($argv[2] $argv[3])
		set gwk = (`gwk2ydoy 20$doyy[2] $doyy[1]`)
		@ yr1 = $doyy[2] % 10
		breaksw
	case -s:
		set sites = (`echo $argv | cut -d- -f2`); shift sites
		breaksw
	case -o:
		set orbf = $argv[2]
		breaksw
        case -r:
                set samp = $argv[2]
                breaksw
endsw
shift
end

if (! -e $work/$doyy[2]_$doyy[1]) mkdir $doyy[2]_$doyy[1]
cd $work/$doyy[2]_$doyy[1]

set orbsrc = ftp://lox/pub/products/$gwk[1]
set orbid = "$orbf[1]_"

set rnxsrc = ftp://lox/pub/rinex/2001/$doyy[1]
#set rnxsrc = /net/lox/ftp/pub/rinex/$doyy[2]data/$doyy[1]
#set rnxsrc2 = $work/$doyy[2]_$doyy[1]

# link eph, rinex, and orbits
echo $orbsrc/$orbf[1]$gwk[1]$gwk[2].sp3.Z
echo $samp[1] 
#ln -s $rnxsrc/auto$doyy[1]0.$doyy[2]n.Z .
wget $orbsrc/$orbf[1]$gwk[1]$gwk[2].sp3.Z .
wget $rnxsrc/auto$doyy[1]0.$doyy[2]n.Z

foreach s ($sites)
wget $rnxsrc/$s$doyy[1]0.$doyy[2]o.Z
#ln -s $rnxsrc2/$sites[2]$doyy[1]0.$doyy[2]o.Z .
#if ($rnxsrc1 = $rnxsrc2) (ln -s $rnxsrc2/$sites[2]$doyy[1]0.$doyy[2]o.Z .)
end

if (! -e auto$doyy[1]0.$doyy[2]n.Z) goto next
if (! -e $orbf[1]$gwk[1]$gwk[2].sp3.Z) goto next
foreach s ($sites)
echo $s$doyy[1]0.$doyy[2]o.Z
if (! -e $s$doyy[1]0.$doyy[2]o.Z) goto next
end

uncompress *.Z

#foreach s ($sites)
#crx2rnx $s$doyy[1]0.$doyy[2]d
#end



# link session and tables
$setup/link_tab_atst_y2k 20$doyy[2] $doyy[1] $samp[1] >& /dev/null
#\rm solve
#ln -s /home/dejonge/f/969/solve/nw2/solve solve
#if ($doyy[2] > 92) goto jump
#	cp $tables/ut1.vlbi ut1.
#	cp $tables/pole.vlbi pole.
#jump:
 
cp $sittab sittbl.
cp $sestab sestbl.
\rm autcln.cmd
cp $work/bin/autcln.cmd.prefit autcln.cmd

chmod 666 station.info
\rm station.info
cp $work/tables/station.info .
echo "$solid $orbid" >! station.info.new
sed -e "1,1 d" station.info >> station.info.new
mv -f station.info.new station.info
#mv pole.$gwk[1] pole.
#mv ut1.$gwk[1] ut1.

chmod 666 vg_in
\rm vg_in
cp $work/tables/vg_in .

gapr_to_l vg_in l$solid[1]$yr1.$doyy[1] long.in $doyy[2] $doyy[1] > /dev/null

echo $orbf[1]$gwk[1]$gwk[2].sp3 

sh_sp3fit -f $orbf[1]$gwk[1]$gwk[2].sp3
#cp /net/lox/ftp/pub/gfiles/$gwk[1]/g$orbid[1]$yr1.$doyy[1].Z .
#uncompress g*.Z
mv g$orbf[1]$gwk[1]$gwk[2].sp3 g$orbid[1]$yr1.$doyy[1]

echo g$orbid[1]$yr1.$doyy[1] 
echo $doyy[2] 
matchprn g$orbid[1]$yr1.$doyy[1]

mv auto* e$orbid[1]$yr1.$doyy[1]
 
makexp << EOF_makexp
20$doyy[2]
$doyy[1]
1
EOF_makexp
cp ../tables/lfile.plus l$solid[1]$yr1.$doyy[1]

makej e$orbid[1]$yr1.$doyy[1] j$orbid[1]$yr1.$doyy[1] > /dev/null
matchprn j$orbid[1]$yr1.$doyy[1]

sortj j$orbid[1]$yr1.$doyy[1]

makex $solid[1].makex.batch > /dev/null

fixdrv d$solid[1]$yr1.$doyy[1]

csh b$solid?.bat

bias_sum q*a.$doyy[1]

goto jump2

mv o$solid[1]a.$doyy[1] ../ofiles_$solid[1]/o$solid[1]a.1$yr1$doyy[1]
mv q$solid[1]a.$doyy[1] ../qfiles_$solid[1]/q$solid[1]a.1$yr1$doyy[1]
mv h*a.????? ../hfiles_$solid[1]
#mv g$orbid[1]$yr1.$doyy[1] ../gfiles
#mv ambi.dat ../plot/ambi.dat_9$yr1$doyy[1]

chmod 666 *.1$yr1[1]o
chmod 666 e*.$doyy[1]
chmod 666 long.in
chmod 666 vg_in
chmod 666 *.sp3

\rm *

jump2:
\rm p*.$doyy[1]
\rm ?$solid[1]p.$doyy[1]
\rm *.99d
\rm x?????.$doyy[1]
compress ????$doyy[1]?.99o
compress k?????.doyy[1]
next:
