#! /bin/csh -fxv

# perform distributed global processing

# by pfang@ucsd.edu Oct. 96

# ---------------------------------------------------
echo set defaults
# ---------------------------------------------------
set ocode = pgga
set elink = ""
set clncmd = ""
set extra = ""
# ---------------------------------------------------
echo read in user options
# ---------------------------------------------------
foreach a ($argv)
switch($a)
	case -e:
		set extra = (`echo $argv | cut -d- -f2`); shift extra
		set extra = (`echo $extra | tr "%" "-"`)
		breaksw
	case -o:
		set optf = $argv[2]
		set optf = /usr/local/processing/gamit_setup/$optf
		breaksw
	case -p:
		set pcode = $argv[2]
		set sfx = `echo $pcode | cut -c1`
		switch($pcode)
		case ngs1:
                        set sfx = h1
                        breaksw
		case ngs2:
                        set sfx = h2
                        breaksw
		case ngs3:
                        set sfx = h3
                        breaksw
		case caln:
                        set sfx = cn
                        breaksw
		case calm:
                        set sfx = cm
                        breaksw
		case cals:
                        set sfx = cs
                        breaksw
		case net7:
                        set sfx = s7
                        breaksw
		case net8:
                        set sfx = s8
                        breaksw
		case noam:
                        set sfx = a
                        breaksw
		case cors:
			set sfx = m
			set elink = ("-l links.oct")
			breaksw
		case cor2:
			set sfx = n
			set elink = ("-l links.oct")
			breaksw
		endsw
		breaksw
	case -l:
		set elink = ($argv[1] $argv[2])
		breaksw
	case -s:
		set sess = (`echo $argv | cut -d- -f2`); shift sess
		breaksw
endsw
shift
end

echo check if solution is already done on raid

if (-e /archive/raid/DHR/matthijs/hpgn/98data/$sess[2]h/q$pcode[1]a.$sess[2].Z) then
	echo SOLUTION $pcode $sess IS ALREADY DONE, EXIT
	echo ""
	echo ""
	exit
else
	echo SOLUTION $pcode $sess /archive/raid/DHR/matthijs/hpgn/98data/$sess[2]h/q$pcode[1]a.$sess[2].Z NOT DONE YET, STARTING...
endif

# ---------------------------------------------------
echo read in job options
# ---------------------------------------------------
set soltype = (`grep "#SOLTYPE" $optf`); shift soltype
set strategy = (`grep "#STRATEGY" $optf`); shift strategy
set sessrng = (`grep "#SESSRNG" $optf`); shift sessrng
set orbneop = (`grep "#ORBNEOP" $optf`); shift orbneop
set regfile = (`grep "#REGFILE" $optf`); shift regfile
set setup = (`grep "#SETUP" $optf`); shift setup
set work = (`grep "#WORK" $optf`); shift work
set ephsrc = (`grep "#EPHSRC" $optf`); shift ephsrc
set orbsrc = (`grep "#ORBSRC" $optf`); shift orbsrc
set eopsrc = (`grep "#EOPSRC" $optf`); shift eopsrc
set rnxsrc = (`grep "#RNXSRC" $optf`); shift rnxsrc
set backup = (`grep "#BACKUP" $optf`); shift backup
set nzen = (`grep "#NZEN" $optf`); shift nzen
set clncmd = (`grep "#AUTCLN" $optf`); shift clncmd

# convert time parameters
set gwk = (`gwk2ydoy $sess`)
@ yy = $sess[1] % 100
@ yr1 = $yy % 10

# replace time parameters in variables
set ephsrc = (`echo $ephsrc | sed -e "s/YY/$yy/g" -e "s/DDD/$sess[2]/g" -e "s/YR1/$yr1/g" -e "s/WWWW/$gwk[1]$gwk[2]/g"`)
set orbsrc = (`echo $orbsrc | sed -e "s/YY/$yy/g" -e "s/DDD/$sess[2]/g" -e "s/YR1/$yr1/g" -e "s/WWWW/$gwk[1]$gwk[2]/g"`)
set rnxsrc = (`echo $rnxsrc | sed -e "s/YY/$yy/g" -e "s/DDD/$sess[2]/g" -e "s/YR1/$yr1/g" -e "s/WWWW/$gwk[1]$gwk[2]/g"`)

# set up orbneop directory
set orbneop = $orbneop/$soltype$sess[2]
mkdir $orbneop >& /dev/null
if (! -e $orbneop) then
	echo "Can not make $orbneop directory. STOP"
	exit
endif
cd $orbneop

if (`ls | wc -l` > 30) goto skip

# prepare eop batch
echo "#\! /bin/csh -f" >! $orbneop/eop.bat
switch($eopsrc[1])
	case usno:
		echo "cat gt/usno/ut1.usno.$gwk[1] >! ut1." >> $orbneop/eop.bat
		echo "cat gt/usno/pole.usno.$gwk[1] >! pole." >> $orbneop/eop.bat
		breaksw
	case daily:
		shift eopsrc
		echo "/bin/rm ut1. pole." >> $orbneop/eop.bat
		set eopsrc = (`echo $eopsrc | sed -e "s/YY/$yy/g"`)
		set eopsrc = (`echo $eopsrc | sed -e "s/DDD/$sess[2]/g"`)
		foreach e ($eopsrc)
			echo "cp $e ." >> $orbneop/eop.bat
		end
		echo "uncompress ut1.* pole.*" >> $orbneop/eop.bat
		breaksw
endsw

# prepare orb batch
echo "#\! /bin/csh -f" >! $orbneop/orb.bat
echo "pp_efile -c comm -t $sess[2] $yy -s sio cdd jpl own" >> $orbneop/orb.bat

switch($orbsrc[1])
	case daily:
		shift orbsrc
		foreach o ($orbsrc)

		set lastf = (`echo $o | tr '/' ' '`)
		set lastc = (`echo $o | tr '.' ' '`)
		set newf = ("`echo $lastf[$#lastf] | cut -c1`$ocode[1]$yr1.$sess[2]")
		if ($lastc[$#lastc] == Z) then
			echo "zcat $o > $newf" >> $orbneop/orb.bat
		else
			echo "cp $o $newf" >> $orbneop/orb.bat
		endif
		echo "chmod a+w $newf" >> $orbneop/orb.bat

		end
		breaksw
	case sp3:
		shift orbsrc
		set orbsrc = (`echo $orbsrc | sed -e "s/GWK/$gwk[1]/g"`)
		set orbsrc = (`echo $orbsrc | sed -e "s/GWD/$gwk[2]/g"`)
		echo "ls -l $orbsrc[1]" >> $orbneop/orb.bat
		echo "sleep 1" >> $orbneop/orb.bat
		echo "ls -l $orbsrc[1]" >> $orbneop/orb.bat
		echo "zcat $orbsrc[1] > ext$gwk[1]$gwk[2].sp3" >> $orbneop/orb.bat
		echo "sh_sp3fit -f ext*.sp3 -o pgga $yy $sess[2]" >> $orbneop/orb.bat
		breaksw
endsw

set post = ("-post matchprn g$ocode[1]$yr1.$sess[2]")

/net/domino/matthijs/hpgn/bin/pp_sess -w $orbneop -s $sess $sessrng -r $regfile $post &

skip:

set work = $work/$sess[2]$sfx
echo $work
mkdir $work >& /dev/null
cd $work; chmod +w *; /bin/rm *
echo "`hostname` $$ long $work `date`" >! myreg
/bin/rm sess_mk.done
chmod +w *; /bin/rm *

set myname = (`echo $0 | tr '/' ' '`)
while (-e $regfile.lock)
	sleep 1
end
echo "`hostname` $$ $myname[$#myname] $work `date`" >> $regfile

# ---------------------------------------------------
echo prepare $pcode 
# ---------------------------------------------------
#set pre = ("pick_net %t $sess[2] $yy %l $setup/reg_$pcode[1].list %m 42 %p $rnxsrc")
set pre = ("/net/domino/matthijs/hpgn/bin/pick_rnx %t $sess[2] $yy %l $setup/reg_$pcode[1].list %r 4 42 %i 600")
/net/domino/matthijs/hpgn/bin/preproc -w $work -m $strategy -r $regfile -s $orbneop -o $ocode -p $pcode -pre $pre -z $nzen $elink -a $clncmd

# ---------------------------------------------------
echo adding extra
# ---------------------------------------------------
cp $backup .
if ($#extra > 1) echo "$extra" >> b$pcode$yr1.bat
#echo "ctox_auto $yy" >> b$pcode$yr1.bat
set junk = `echo $pcode | cut -c1-3`
if($junk == "ngs") then
	set sfx = "h"
	echo "mv -f autcln.sum autcln.sum.$pcode[1]" >> b$pcode$yr1.bat
endif
if($junk == "cal") then
	set sfx = "c"
	echo "mv -f autcln.sum autcln.sum.$pcode[1]" >> b$pcode$yr1.bat
endif
echo "uniback PCODE $pcode YY $yy DDD $sess[2] SUFX $sfx" >> b$pcode$yr1.bat
echo "cd .. ; \rm -rf $work" >> b$pcode$yr1.bat
#if ($#extra <= 1) echo "QCGamit $sess[2] $yy $pcode &" >> b$pcode$yr1.bat
chmod +x b$pcode$yr1.bat
csh ./b$pcode$yr1.bat > /dev/null 

jobs -l >! /tmp/tmp.tmp
set aline = (`cat /tmp/tmp.tmp | cut -c4-`)
echo "`hostname` $aline[2] $aline[4] $work `date`" >> $regfile

# deregister
while (-e $regfile.lock)
	sleep 1
end
grep -v "`hostname` $$" $regfile >! $regfile.lock
\mv $regfile.lock $regfile
