#! /bin/csh -fxv
# $Id: weeklyGlobk.csh,v 1.41 2004/11/29 19:09:17 pjamason Exp $
############################################################################
# weeklyGlobk.csh -t YYYY DDD Ndays or GPSWEEK -o option_file
#	[-i job_ID]

#	carry out multi-day globk combination solution

#	-t starting date or GPS week number
#	-o option file containing job options
#	-i job_ID, default = GPS week number

# by pfang@ucsd.edu
############################################################################


# set defaults
set optf = none
set jobid = none
#set sshid = ~/.ssh/hss.corp
set sshid = /usr/local/etc/proc/hss.corp
set scriptpath = /shared/scripts/bin/gpsproc

# pj, 07/09/2002: this is needed for igseop2gam.lin86 below
# this directory location should change; it is currently linked to
# /shared/gpsproc/src/bin
set tmpbinpath = /usr/local/processing/bin

if ($#argv < 1) then
	sed -e "1 d" -e "s/#//" -e "/pfang/q" $0
	exit
endif

source /home/gpsproc/.cshrc

# -------------------------------------------
echo "weeklyGlobk.csh: Sort user options"
# -------------------------------------------
foreach a ($argv)
switch($a)
	case -t:
		set ydoyn = (`echo $argv | cut -d- -f2`); shift ydoyn
		if ($#ydoyn < 3) set ydoyn = (`gwk2ydoy $ydoyn[1]` 7)
		set yr1 = `echo $ydoyn[1] | cut -c4`
		set gwk = (`gwk2ydoy $ydoyn`)
		echo "gweek: $gwk"
                breaksw
	case -o:
		set optf = ($argv[2])
		breaksw
	case -i:
		set jobid = ($argv[2])
		breaksw
endsw
shift
end
if (! -e $optf) then
	echo "$optf not found"
	exit 1
endif
if ($jobid[1] == 'none') set jobid = $gwk[1]

# -------------------------------------------
echo "weeklyGlobk.csh: read in options"
# -------------------------------------------
set soln = (`grep "SOLN:" $optf | cut -d: -f2`)
set hfil = (`grep "HFILES:" $optf | cut -d: -f2`)
set glbf = (`grep "GLBF:" $optf | cut -d: -f2`)
set hcom = (`grep "HCOMB:" $optf | cut -d: -f2`)
set tabl = (`grep "TABLES:" $optf | cut -d: -f2`)
set nets = (`grep "NETS:" $optf | cut -d: -f2`)
set styp = (`grep "STYPES:" $optf | cut -d: -f2`)
set tropsub = (`grep "TROPSUB:" $optf | cut -d: -f2`)
set geth = (`grep "GETH:" $optf | cut -d: -f2`)
set htoglb = (`grep "HTOGLB:" $optf | cut -d: -f2`)
set cleanh = (`grep "CLEANH:" $optf | cut -d: -f2`)
set combh = (`grep "COMBH:" $optf | cut -d: -f2`)
set comba = (`grep "COMBA:" $optf | cut -d: -f2`)
set eoporb = (`grep "EOPORB:" $optf | cut -d: -f2`)
set repeat = (`grep "REPEAT:" $optf | cut -d: -f2`)
set sinex = (`grep "^SINEX:" $optf | cut -d: -f2`)
set summary = (`grep "SUMM:" $optf | cut -d: -f2`)
set submit = (`grep "SUBMIT:" $optf | cut -d: -f2`)
set cleanb = (`grep "CLEANB:" $optf | cut -d: -f2`)
set dailysinex = (`grep "DAILYSINEX:" $optf | cut -d: -f2`)

# -------------------------------------------
echo "weeklyGlobk.csh: link tables"
# -------------------------------------------

cd $soln
$scriptpath/linkTables.csh $ydoyn[1] $ydoyn[2]


if ($geth[1] == 'yes') then
#--------------------------------------------
echo "weeklyGlobk.csh: removing old hfiles"
#--------------------------------------------
set now = `date`
echo $now
\rm $hfil/h* >& /dev/null
\rm $hcom/H* >& /dev/null

#--------------------------------------------
echo "weeklyGlobk.csh: linking hfiles"
#--------------------------------------------
set now = `date`
echo $now
set mjd = (`mjd2ydoy $ydoyn`)
@ mjdend = $mjd + $ydoyn[3]
while ($mjd < $mjdend)
        set ydoy = (`mjd2ydoy $mjd`)
	set yy = `echo $ydoy[1] | cut -c3-4`
                cd $hfil
                cp /archive/garner/hfiles/$ydoy[1]/$ydoy[2]/higs1*.Z .                
                cp /archive/garner/hfiles/$ydoy[1]/$ydoy[2]/higs2*.Z .                
                cp /archive/garner/hfiles/$ydoy[1]/$ydoy[2]/higs3*.Z .                
                cp /archive/garner/hfiles/$ydoy[1]/$ydoy[2]/higs4*.Z .                
                cp /archive/garner/hfiles/$ydoy[1]/$ydoy[2]/higs5*.Z .                
                cp /archive/projects/sugar/solutions/$ydoy[1]/$ydoy[2]/hsuma*.Z .
        @ mjd++
end
#--------------------------------------------
echo "weeklyGlobk.csh: uncompressing linked hfiles"
#--------------------------------------------

uncompress h*.Z

#--------------------------------------------
echo "weeklyGlobk.csh: check number of hfiles available"
#--------------------------------------------
set now = `date`
echo $now
set havailable = `ls $hfil/h?????.????? | wc | awk '{print $1}'`
@ hrequired = $#nets * $ydoyn[3] 
if($havailable != $hrequired) then
	echo "weeklyGlobk.csh: Number of available hfiles ($havailable) not equal to number of required hfiles ($hrequired)"
	echo "weeklyGlobk.csh: EXITING"
	exit
else
	echo "weeklyGlobk.csh: Got all required hfiles ($havailable), proceed..."
#	mailx -s "launching weekly for week $gwk" linette@gpsmail
endif

endif # end of linking and uncompressing hfiles


if ($htoglb[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: htoglb"
# -------------------------------------------
set now = `date`
echo $now
/bin/rm *.status *.warning *.fatal
/bin/rm $glbf/h* >& /dev/null
set mjd = (`mjd2ydoy $ydoyn`)
@ mjdlast = $mjd + $ydoyn[3]
while ($mjd < $mjdlast)
	set ydoy = (`mjd2ydoy $mjd`)
	set ymd = (`ymd2ydoy $ydoy`)
	set yy = `echo $ydoy[1] | cut -c3-4`
	set i = 1
	while ($i <= $#nets)
		set h = $hfil/h$nets[$i]a.$yy$ydoy[2]
		if (! -e $h) echo "$h not found"
		
		# pj 05/19/2003: can not use here or htoglb jobs will stack
		cd $soln; htoglb $glbf[1] $tabl[1]/svs_$nets[$i]_temp.apr $h

		echo "/bin/rm ls $glbf/h$yy$ymd[2]$ymd[3]????_$nets[$i].??? | grep -v $styp[$i]"
		/bin/rm `ls $glbf/h$yy$ymd[2]$ymd[3]????_$nets[$i].??? | grep -v $styp[$i]`

		echo "/bin/rm ls $glbf/h$yy$ymd[2]$ymd[3]????_$nets[$i].??? | grep -v $styp[$i]"
		if ($cleanh[1] == 'yes') /bin/rm $h		
		@ i++
	end
	@ mjd++
end # end of day loop
endif # end of htoglb job

if ($combh[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: combine and estimate weekly positions"
# -------------------------------------------
set now = `date`
echo $now
cd $soln[1]
/bin/rm *.status *.warning *.fatal
echo -n "" > gsuma$jobid.gdl
set mjd = (`mjd2ydoy $ydoyn`)
@ mjdlast = $mjd + $ydoyn[3]
while ($mjd < $mjdlast)
	set ydoy = (`mjd2ydoy $mjd`)
	set ymd = (`ymd2ydoy $ydoy`)
	set yy = `echo $ydoy[1] | cut -c3-4`
	set i = 1
	while ($i <= $#nets)
		set bf = (`ls $glbf/h$yy$ymd[2]$ymd[3]????_$nets[$i].$styp[$i]`)
		if ($i != $#nets) then
			echo "$bf +" >> gsuma$jobid[1]_pos.gdl
		else
			echo "$bf" >> gsuma$jobid[1]_pos.gdl
		endif
		@ i++
	end
	@ mjd++
end # end of day loop

/bin/rm gsuma$jobid[1]_comb.prt gsuma$jobid[1]_comb.log

cd $soln[1]; glred 6 gsuma$jobid[1]_pos.prt gsuma$jobid[1]_pos.log gsuma$jobid[1]_pos.gdl globk_pos.cmd

endif # end of combh job


if ($repeat[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: daily repeatability"
# -------------------------------------------
set now = `date`
echo $now

cd $soln[1]
/bin/rm *.status *.warning *.fatal
#echo -n "" > gk$jobid[1]_rpt.gdl
#echo -n "" > gk$jobid[1]_orb.svs
set mjd = (`mjd2ydoy $ydoyn`)
@ mjdlast = $mjd + $ydoyn[3]
#while ($mjd < $mjdlast)
#	set ydoy = (`mjd2ydoy $mjd`)
#	set ymd = (`ymd2ydoy $ydoy`)
#	set yy = `echo $ydoy[1] | cut -c3-4`
#	ls $hcom/HSUMA*_COMB.??? >> gsuma$jobid[1]_rpt.gdl
#	@ mjd++
#end # end of day loop
ls $hcom/H*_COMB.??? > gsuma$jobid[1]_rpt.gdl
#grep "^Eph. " gk$jobid[1]_orb.orbbak | cut -c6- >> gk$jobid[1]_orb.svs
#/bin/cp gk$jobid[1]_orb.svs repeat.svs
/bin/rm gsuma$jobid[1]_rpt.prt gsuma$jobid[1]_rpt.log gsuma$jobid[1]_rpt.org

ssh  -1 -i $sshid $repeat[2] "cd $soln[1]; glred 6 gsuma$jobid[1]_rpt.prt gsuma$jobid[1]_rpt.log gsuma$jobid[1]_pos.gdl globk_repeat.cmd"

ssh  -1 -i $sshid $repeat[2] "cd $soln[1]; ensum 2 su$jobid[1].ens va$jobid[1].ens gsuma$jobid[1]_rpt.org"

# pj, 12/27/2002: run a globk command to create weekly average positions for
# each site.  these will be used as input to db
#ssh -1 -i $sshid $repeat[2] "cd $soln[1]; globk 6 gk$jobid[1]_pos.prt gk$jobid[1]_pos.log gk$jobid[1]_rpt.gdl globk_pos.cmd"

ssh -1 -i $sshid $combh[2] "cpuJobTrack.pl -j suma_$jobid[1] -m del"
endif # end of repeatability job


if ($sinex[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: generating sinex file"
# -------------------------------------------
cd $soln[1]
/bin/rm *.status *.warning *.fatal
echo -n "" > gk$jobid[1]_snx.gdl
echo -n "" > gk$jobid[1]_orb.svs
set mjd = (`mjd2ydoy $ydoyn`)
@ mjdlast = $mjd + $ydoyn[3]
while ($mjd < $mjdlast)
	set ydoy = (`mjd2ydoy $mjd`)
	set ymd = (`ymd2ydoy $ydoy`)
	set yy = `echo $ydoy[1] | cut -c3-4`

	# pj, 08/25/2003: add igs4; 10/10/2003: added igs5
	#foreach n (igs1.glx igs2.glx igs3.glx eura.glx)
	foreach n (igs1.glx igs2.glx igs3.glx igs4.glx igs5.glx eura.glx)
		set plus = "+"
		if ($n[1] == "eura.glx") set plus = " "
		echo "../glbf/h$yy$ymd[2]$ymd[3]1200_$n[1] $plus" >> gk$jobid[1]_snx.gdl
	end
#	ls $hcom[1]/H$yy$ymd[2]$ymd[3]_COMB.??? >> gk$jobid[1]_snx.gdl
	@ mjd++
end # end of day loop
grep "^Eph. " gk$jobid[1]_orb.orbbak | cut -c6- >> gk$jobid[1]_orb.svs
/bin/cp gk$jobid[1]_orb.svs repeat.svs
/bin/rm gk$jobid[1]_snx.prt gk$jobid[1]_snx.log gk$jobid[1]_snx.org

ssh -1 -i $sshid $sinex[2] "cd $soln[1]; globk 6 gk$jobid[1]_snx.prt gk$jobid[1]_snx.log gk$jobid[1]_snx.gdl globk_sinex.cmd"

ssh  -1 -i $sshid $sinex[2] "cd $soln[1]; glsave sinex.com gk$jobid[1]_SNX.GLX 'Weekly combined Sinex file'"

cd $soln[1]; glbtosnx . head.snx gk$jobid[1]_SNX.GLX

cd $soln[1]; $scriptpath/fix_snx.pl -i sio$jobid[1]7.snx

/bin/mv sio$jobid[1]7.snx.new sio$jobid[1]7.snx

endif # end of sinex job

# pj, 06/16/2003: moved this to prior to makeWeekly below; that script
# needs the tro files created  here


if ($summary[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: making summary"
# -------------------------------------------
# pj, 10/07/2002
#ssh -1 -i $sshid $summary[2] "cd $soln[1]; $scriptpath/sio_summary.csh -j $jobid -w $soln[1]"
cd $soln
echo "/archive/zee06/projects/sugar/weekly/makeWeeklySugarSummaryFile.csh -j $jobid -w $soln[1]"
ssh  -1 -i $sshid $summary[2] "cd $soln[1]; /archive/zee06/projects/sugar/weekly/makeWeeklySugarSummaryFile.csh -j $jobid -w $soln[1]"

endif # end of summary job

if ($submit[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: submit reports and backup"
# -------------------------------------------
set now = `date`
echo $now

cd $soln[1]; $scriptpath/submitWeeklyAnalysis.csh -i $jobid -w $soln[1]

endif # end of submit job

if ($cleanb[1] == 'yes') then
# -------------------------------------------
echo "weeklyGlobk.csh: clean glbf and hcomb"
# -------------------------------------------
/bin/rm ../glbf/* ../hcomb/*

endif # end of cleanb job

