#!/bin/ksh -l
################################################################################
# wrapup_wsr -- wrapup script to be run after wsr_main job has been run
################################################################################
# UPDATED 05/21/13 for WCOSS with test mode by Richard Wobus

# for production, name this script wrapup_wsr
# for testing, name this script wrapup_wsr_test
#              and set up test destinations for your LOGNAME below

###################################
# set up test mode if required
###################################
NET=${NET:-wsr}
RUN=${RUN:-wsr}
envir=${envir:-prod}

#if [[ $useexpid = no ]]; then
#	. ${PACKAGEROOT}/wsr.${wsr_ver}/versions/run.ver
#else
#	. ../../../versions/run.ver
#fi
#ver=${ver:-$(echo ${wsr_ver:-v3.3.0}|cut -c1-4)}

dirname=`dirname $0`
. $dirname/../versions/run.ver

#. ../../../versions/run.ver

#shellname=ksh
. /usr/share/lmod/lmod/init/profile
module purge

module load envvar/$envvar_ver
module load intel/$intel_ver
module load PrgEnv-intel/$PrgEnv_intel_ver

module load prod_util/$prod_util_ver

module list

scriptname=`basename $0`
case $scriptname in
	(wrapup_wsr)
		testmode=no
		;;
	(wrapup_wsr_test)
		testmode=yes
		;;
	(*)
		echo no test setting for scriptname=$scriptname
		exit
		;;
esac

if [[ $testmode = yes ]]; then

	# these test settings are only used when testmode=yes
	case $LOGNAME in
#		(SDM)
		(nco.sdm)
			# these are the production locations, edit them to use test locations
			testemail=sdm@noaa.gov
			testuser=wx12sd
			testrzdm=ncorzdm
			testdirectory=/home/people/nco/www/htdocs/pmb/sdm_wsr
			useexpid=no
			expid=""
			envir=${envir:-para}
			#PDY=20210215 #20201120 #20210215 #For Canned Data Test
			;;
		(ops.prod)
			# these are the production locations, edit them to use test locations
			testemail=nco.spa@noaa.gov
			#testuser=ops.prod
			testuser=nwprod
			testrzdm=ncorzdm
			testdirectory=/home/people/nco/www/htdocs/pmb/nwprod_wsr
			useexpid=no
			expid=""
			envir=${envir:-para}
			#PDY=20210215 #20201120 #20210215 #For Canned Data Test
			;;
		(ops.para)
			# these are the production locations, edit them to use test locations
			testemail=nco.spa@noaa.gov
			#testuser=ops.prod
			testuser=nwprod
			testrzdm=ncorzdm
			testdirectory=/home/people/nco/www/htdocs/pmb/nwprod_wsr
			useexpid=no
			expid=""
			envir=${envir:-para}
			#PDY=20210215 #20201120 #20210215 #For Canned Data Test
			;;
		(Xianwu.Xue)
			testemail=Xianwu.Xue@noaa.gov
			testuser=xianwu.xue
			testrzdm=emcrzdm
			testdirectory=/home/people/emc/www/htdocs/gmb/xianwu.xue/wsr_sdm
			useexpid=yes
			envir=dev
			expid=$(basename $(readlink -f `pwd`/../../../))
			testtmpdir=/lfs/h2/emc/ptmp/$LOGNAME/o
			PDY=20210215
			export COMPATH=$testtmpdir/$expid/$envir/com/${NET}
			export envir=prod #To make compath.py get the right path
			;;
		(*)
			echo Please add test settings to $0 for LOGNAME=$LOGNAME
			;;
	esac

	echo LOGNAME=$LOGNAME
	echo testemail=$testemail
	echo testuser=$testuser
	echo testrzdm=$testrzdm
	echo testdirectory=$testdirectory
	echo useexpid=$useexpid

	if [[ $useexpid = yes ]]; then
		dfile=$0
		dfb=`basename $dfile`
		dfd=`dirname $dfile`
		if [[ "$dfd" = '.' ]]; then
			dfd=`pwd`
		fi
		#dfd=/nwprod/ush
		expid=${expid:-null}
		dfdtestdone=no
		dfdt=$dfd
		while [[ $dfdtestdone = no ]]
		do
			dfdtd=`dirname $dfdt`
			if [[ "$dfdtd" = '.' ]]; then
				dfdtd=`pwd`
			fi
			dfdtb=`basename $dfdt`
			if [[ $dfdtb = '/' ]]; then
				dfdtestdone=yes
				expid=${expid:-test}
			else
				dfdfound=no
				case $dfdtb in
					(nwdev)  dfdfound=yes ;;
					(nwpara) dfdfound=yes ;;
					(nwtest) dfdfound=yes ;;
				esac
				if [[ $dfdfound = yes ]]; then
					expid=`basename $dfdtd`
					dfdtestdone=yes
				else
					dfdt=$dfdtd
				fi
			fi
		done

		# specify expid here if the procedure above does not choose it appropriately

		echo expid=$expid

		#COMDIR=${COMDIR:-$PTMP/$expid/${envir}/com/${NET}/${ver}}
		#COMIN=${COMIN:-${COMDIR}/$RUN.$PDY/setup}
		#COMOUT=${COMOUT:-${COMDIR}/$RUN.$PDY/wrapup}
		#COMIN=$PTMP/$LOGNAME/o/$expid/com/wsr/dev/wsr.$PDY/setup
		#COMOUT=$PTMP/$LOGNAME/o/$expid/com/wsr/dev/wsr.$PDY/wrapup

	fi

	echo testmode=$testmode
fi

###################################
# end set up test mode if required
###################################

# set -x

###################################################################
# READ IN DATA FROM targdata.d
###################################################################
export ver=${ver:-$(echo ${wsr_ver:-v3.3.0}|cut -c1-4)}

curyy=`date -u +%Y`
curmm=`date -u +%m`
curdd=`date -u +%d`
curhh=`date -u +%H`
PDY=${PDY:-${curyy}${curmm}${curdd}}

export COMIN=${COMIN_setup:-$(compath.py ${envir}/com/${NET}/${ver})/setup}
export COMOUT=${COMIN_main:-$(compath.py ${envir}/com/${NET}/${ver})/wrapup/${PDY}}
#export COMIN=${COMIN_setup:-$(compath.py ${envir}/com/${NET}/${ver})/${RUN}.${PDY}/setup}
#export COMOUT=${COMIN_main:-$(compath.py ${envir}/com/${NET}/${ver})/${RUN}.${PDY}/wrapup}
if [ ! -s $COMOUT ]; then mkdir -p $COMOUT; fi
echo COMOUT=$COMOUT

#module purge
#module load envvar/1.0

#module load intel/19.1.3.304
#module load PrgEnv-intel/8.1.0

#module load prod_util/2.0.5

#module list

#### Read in targdata.d header (first 9 lines) ####
PDY=`head -1 $COMIN/targdata.d`
cases=`head -2 $COMIN/targdata.d | tail -1`
resolution=`head -3 $COMIN/targdata.d | tail -1`
vnorm=`head -4 $COMIN/targdata.d | tail -1`
radvr=`head -5 $COMIN/targdata.d | tail -1`
lon1=`head -6 $COMIN/targdata.d | tail -1`
lon2=`head -7 $COMIN/targdata.d | tail -1`
lat1=`head -8 $COMIN/targdata.d | tail -1`
lat2=`head -9 $COMIN/targdata.d | tail -1`


#### Read in targdata.d body (remaining lines) ####
#### Assumes 6 lines per case ####

i=1
while [ $i -le $cases ]
do
	line=`expr ${i} \* 6 + 4`
	obsdate[i]=`head -n $line $COMIN/targdata.d | tail -1`
	line=`expr $line + 1`
	veridate[i]=`head -n $line $COMIN/targdata.d | tail -1`
	line=`expr $line + 1`
	vrlon[i]=`head -n $line $COMIN/targdata.d | tail -1`
	line=`expr $line + 1`
	vrlat[i]=`head -n $line $COMIN/targdata.d | tail -1`
	line=`expr $line + 1`
	priority[i]=`head -n $line $COMIN/targdata.d | tail -1`
	line=`expr $line + 1`
	comments[i]=`head -n $line $COMIN/targdata.d | tail -1`
	i=`expr ${i} + 1`
done

i=1
while [ $i -le $cases ]
do
	case ${priority[$i]} in
		1) prioritytxt[$i]=HIGH;;
		2) prioritytxt[$i]=MEDIUM;;
		3) prioritytxt[$i]=LOW;;
	esac
	i=`expr ${i} + 1`
done

#curyy=`date -u +%Y`
#curmm=`date -u +%m`
#curdd=`date -u +%d`
#curhh=`date -u +%H`
#PDY=${PDY:-${curyy}${curmm}${curdd}}

text=${COMOUT}/${PDY}decision.txt
carcahtext=${COMOUT}/${PDY}carcah_decision.txt
verinfo=${COMOUT}/verifcases${curyy}.d
trackinfo=${COMOUT}/veriftracks${curyy}.d
rm -rf $text
rm -rf $carcahtext

# Two phases of the program, with separate leading time
# 
flydate1=`$NDATE +48 ${PDY}00`
flydate2=`$NDATE +72 ${PDY}00`
flydate[1]=`echo ${flydate1}|cut -c1-8`
flydate[2]=`echo ${flydate2}|cut -c1-8`
flytime[1]=`$NDATE +48 ${PDY}00`
flytime[2]=`$NDATE +72 ${PDY}00`


echo "Decision issued from NCEP at ${curhh}Z on ${curmm}/${curdd}/${curyy}" >> $text
echo >> $text
echo "Decision issued from NCEP at ${curhh}Z on ${curmm}/${curdd}/${curyy}" >> $carcahtext
echo >> $carcahtext

if [[ $testmode = no ]]; then
	clear
else
	echo "##############################################################################"
	echo clear page here when not in test mode
	echo "##############################################################################"
fi

echo "----------------------------------------------------------------"
echo "|     WINTER STORM RECONNAISSANCE PROGRAM DECISION SCRIPT      |"
echo "----------------------------------------------------------------"
echo
echo

echo "DO YOU WANT TO MAKE A FLIGHT REQUEST FOR ${flydate[1]}? (y/n)"
read dowefly
echo

if test "$dowefly" = 'y' || test "$dowefly" = 'Y'
then
	newcasenum=`expr ${cases} + 1`

	echo "FLIGHT REQUEST FOR ${flydate[1]}" >> $text
	echo >> $text
	echo "FLIGHT REQUEST FOR ${flydate[1]}" >> $carcahtext
	echo >> $carcahtext


	echo "HOW MANY FLIGHTS ARE DESIRED?"
	read numflights
	echo
	if [ ${numflights} -gt 3 ]
	then
		echo "You must choose 1 to 3 flights"
		echo "Please try again"
		echo
		echo "HOW MANY FLIGHTS ARE DESIRED?"
		read numflights
		echo
	fi

	ct=1
	while test ${ct} -le ${numflights}
	do
		mk0=0
		while test ${mk0} -ne 1
		do
			mk0=1
			if [ ${numflights} -gt 1 ]
			then
				if [ ${ct} -eq 1 ]
				then
					echo "ENTER THE NUMBER OF THE FIRST DESIRED FLIGHT TRACK"
				else
					echo "ENTER THE NUMBER OF THE NEXT DESIRED FLIGHT TRACK"
				fi
			else
				echo "ENTER THE NUMBER OF THE DESIRED FLIGHT TRACK"
			fi
			read tracknum[$ct]
			echo

			if [[ ${tracknum[$ct]} -gt 100 || ${tracknum[$ct]} -lt 1 ]]
			then
				echo "Invalid track number; valid tracks are 1 to 26, 31 to 49 and 51 to 100"
				echo
				mk0=0
			fi


			if [[ ${tracknum[$ct]} -eq 50 || ${tracknum[$ct]} -eq 30 ]]
			then
				echo "Invalid track number; valid tracks are 1 to 26, 31 to 49 and 51 to 100"
				echo
				mk0=0
			fi

		done

		mk20=1
		controldate[$ct]=`echo ${flytime[1]}|cut -c7-8`
		controlhour[$ct]=`echo ${flytime[1]}|cut -c9-10`
		while test ${mk20} -eq 1
		do
			echo "<ENTER> TO ACCEPT WSR DEFAULT CONTROL TIME (${controldate[$ct]}/${controlhour[$ct]}Z),"
			echo "        OR PRESS c TO CHANGE THE CONTROL TIME"
			read ans
			echo
			if test "$ans" = ""
			then
				controltime[$ct]=${flytime[1]}
				mk20=2
			else
				echo
				echo "ENTER THE TWO DIGIT HOUR OF THE CONTROL TIME"
				read controlhour[$ct]
				controltime[$ct]=${flydate[1]}${controlhour[$ct]}

				echo
				echo "THE CONTROL TIME FOR TRACK ${tracknum[$ct]} IS ${controldate[$ct]}/${controlhour[$ct]}Z"
				echo "DO YOU WISH TO MAKE ANY CHANGES TO THIS DATA? (y/n)"
				read controlans
				echo
				if test "$controlans" = 'n' || test "$controlans" = 'N'
				then
					mk20=2
				else
					mk20=1
				fi
			fi
		done

		answer=y
		mark=1
		while test "$answer" = 'y' || test "$answer" = 'Y'
		do
			mk1=0
			while test ${mk1} -ne 1
			do
				mk1=1
				newcasemark=0
				index=`expr ${ct} \* 10 + ${mark}`
				#caseview2.2.sh
				echo "----------------------------------------------------------"
				i=1
				while test ${i} -le ${cases}
				do
					echo "PLOT# ${i}"
					echo "  Observation time:  ${obsdate[$i]}"
					echo "  Verification time: ${veridate[$i]}"
					echo "  Latitude:  ${vrlat[$i]}"
					echo "  Longitude: ${vrlon[$i]}"
					echo "  Priority:  ${prioritytxt[$i]}"
					echo "  Comments:  ${comments[$i]}"
					i=`expr ${i} + 1`
					if [ ${i} -le ${cases} ]
					then
						echo
					fi
				done
				echo "----------------------------------------------------------"
				echo
				echo "  WHAT IS THE PLOT NUMBER OF THE SITUATION FLIGHT ${tracknum[$ct]} WILL TARGET?"
				echo "  (enter ${newcasenum} to input data for a new case)"
				read casenum[$index]
				echo

				if [ ${casenum[$index]} -eq ${newcasenum} ]
				then
					echo "-----------------------------------------------------------------"
					echo "FOR CASE ${newcasenum}:"
					echo
					echo "ENTER THE OBSERVATION TIME (yyyymmddhh)"
					read obsdate[$newcasenum]
					echo
					echo "ENTER THE VERIFICATION TIME (yyyymmddhh)"
					read veridate[$newcasenum]
					echo
					echo "ENTER THE LATITUDE OF THE CENTER OF THE VERIFICATION REGION"
					read vrlat[$newcasenum]
					echo
					echo "ENTER THE LONGITUDE OF THE CENTER OF THE VERIFICATION REGION"
					read vrlon[$newcasenum]
					echo
					echo "CHOOSE A PRIORITY"
					echo "1) HIGH PRIORITY"
					echo "2) MEDIUM PRIORITY"
					echo "3) LOW PRIORITY"
					read priority[$newcasenum]
					case ${priority[$newcasenum]} in
						1) prioritytxt[$newcasenum]=HIGH;;
						2) prioritytxt[$newcasenum]=MEDIUM;;
						3) prioritytxt[$newcasenum]=LOW;;
					esac
					echo
					echo "PROVIDE COMMENTS OR PRESS ENTER"
					read comments[$newcasenum]
					echo "-----------------------------------------------------------------"
					echo
					newcasemark=1

				else
					if [[ ${casenum[$index]} -lt 1 || ${casenum[$index]} -gt ${cases} ]]
					then
						if [ ${cases} -eq 1 ]
						then
							echo "  Invalid entry; the only valid entry is 1."
						else
							echo "  Invalid entry; valid entries are 1 through ${cases}"
						fi
						echo
						mk1=0
					fi
				fi

				casenumber=${casenum[$index]}
				if [[ ${obsdate[$casenumber]} -ne ${controltime[$ct]} && ${mk1} -eq 1 ]]
				then
					echo "  You must select a case with ${controltime[$ct]} as the observation time"
					echo "  Please try again"
					echo
					mk1=0
				fi
			done

			if [ ${newcasemark} -eq 1 ]
			then
				newcasenum=`expr ${newcasenum} + 1`
			fi

			echo "  ARE THERE ANY OTHER SITUATIONS FLIGHT ${tracknum[$ct]} WILL TARGET? (y/n)"
			read answer
			echo

			if test "$answer" = 'n' || test "$answer" = 'N'
			then
				casecounter[$ct]=${mark}
			else
				mark=`expr ${mark} + 1`
			fi

		done


		ct=`expr ${ct} + 1`
	done

	echo "PROVIDE COMMENTS ABOUT THE FLIGHT(S) FOR CARCAH OR PRESS ENTER"
	read carcahcomments
	echo

	echo "IF DESIRED, PROVIDE ADDITIONAL COMMENTS ABOUT THE FLIGHT(S) OR PRESS ENTER"
	read morecomments
	echo

	mk2=0
	while test ${mk2} -ne 1
	do
		if [[ $testmode = no ]]; then
			clear
		else
			echo "##############################################################################"
			echo clear page here when not in test mode
			echo "##############################################################################"
		fi
		echo "---------------------------------------------------------------"
		echo "PLEASE VERIFY THE FOLLOWING INFORMATION"
		echo "---------------------------------------------------------------"
		echo "OBSERVING AT TIME: ${controltime[$ct]}"

		ct=1
		option=1
		while test ${ct} -le ${numflights}
		do
			echo "${option}) FLIGHT TRACK: ${tracknum[$ct]}"
			number=`expr 10 + ${ct}`
			optracker[$option]=${number}
			option=`expr ${option} + 1`
			mark=1
			while test ${mark} -le ${casecounter[$ct]}
			do
				index=`expr ${ct} \* 10 + ${mark}`
				casenumber=${casenum[$index]}
				echo "${option})   CASE: ${casenumber}"
				echo "       Verification time: ${veridate[$casenumber]}"
				echo "       Latitude:  ${vrlat[$casenumber]}N"
				echo "       Longitude: ${vrlon[$casenumber]}W"
				echo "       Priority:  ${prioritytxt[$casenumber]}"
				echo "       Comments:  ${comments[$casenumber]}"
				number=`expr 10 + 10 \* ${ct} + ${mark}`
				optracker[$option]=$number
				option=`expr ${option} + 1`
				mark=`expr ${mark} + 1`
			done
			ct=`expr ${ct} + 1`
		done

		echo "${option}) CARCAH comments:     ${carcahcomments}"
		optracker[$option]=2
		option=`expr ${option} + 1`
		echo "${option}) Additional Comments: ${morecomments}"
		echo "---------------------------------------------------------------"
		echo
		optracker[$option]=1

		echo "DO YOU WISH TO MAKE ANY CHANGES TO THIS DATA (y/n)?"
		read response
		echo
		if test "$response" = 'n' || test "$response" = 'N'
		then
			ct=1
			echo ${controltime[$ct]} >> $trackinfo
			while test ${ct} -le ${numflights}
			do
				if [[ ${tracknum[$ct]} -ge 1 && ${tracknum[$ct]} -le 29 ]]
				then
					city=Anchorage
				fi
				if [[ ${tracknum[$ct]} -ge 31 && ${tracknum[$ct]} -le 56 ]]
				then
					city=Honolulu
				fi
				if [[ ${tracknum[$ct]} -ge 61  && ${tracknum[$ct]} -le 66 ]]
				then
					city=Gulf
				fi
				if [[ ${tracknum[$ct]} -ge 68  && ${tracknum[$ct]} -le 90 ]]
				then
					city=Yokota
				fi
				echo "  A flight is requested from ${city} along track ${tracknum[$ct]}" >> $text
				echo "      with a control time of ${controldate[$ct]}/${controlhour[$ct]}Z" >> $text
				echo >> $text
				echo "  A flight is requested from ${city} along track ${tracknum[$ct]}" >> $carcahtext
				echo "      with a control time of ${controldate[$ct]}/${controlhour[$ct]}Z" >> $carcahtext
				echo >> $carcahtext
				echo "  Verification information is as follows:" >> $text
				echo >> $text
				echo ${tracknum[$ct]} >> $trackinfo
				mark=1
				while test ${mark} -le ${casecounter[$ct]}
				do
					index=`expr ${ct} \* 10 + ${mark}`
					casenumber=${casenum[$index]}
					echo "      Verification time: ${veridate[$casenumber]}" >> $text
					echo "      Latitude:  ${vrlat[$casenumber]}" >> $text
					echo "      Longitude: ${vrlon[$casenumber]}" >> $text
					echo "      Priority:  ${prioritytxt[$casenumber]}" >> $text
					echo "      Comments:  ${comments[$casenumber]}" >> $text
					echo >> $text
					echo ${veridate[$casenumber]} >> $verinfo
					echo ${controltime[$ct]} >> $verinfo
					echo ${vrlon[$casenumber]} >> $verinfo
					echo ${vrlat[$casenumber]} >> $verinfo
					echo ${radvr} >> $verinfo
					mark=`expr ${mark} + 1`
				done
				ct=`expr ${ct} + 1`
			done

			echo "  CARCAH Comments:      ${carcahcomments}" >> $text
			echo >> $text
			echo "  Additional Comments:  ${morecomments}" >> $text
			echo >> $text
			echo "  Comments:  ${carcahcomments}" >> $carcahtext
			echo >> $carcahtext
			mk2=1
		else
			echo "ENTER THE NUBMER OF THE ITEM YOU WISH TO CHANGE"
			read changenum
			echo
			if [[ ${optracker[$changenum]} -le 19 && ${optracker[$changenum]} -gt 2 ]]
			then
				mk0=0
				index=`expr ${optracker[$changenum]} - 10`
				while test ${mk0} -ne 1
				do
					mk0=1
					echo "ENTER THE NUBMER OF THE DESIRED FLIGHT TRACK"
					read tracknum[$index]
					echo

					if [[ ${tracknum[$index]} -gt 100  || ${tracknum[$index]} -lt 1 ]]
					then
						echo "Invalid track number; valid tracks are 1 to 29, 31 to 49 and 51 to 100 "
						echo
						mk0=0
					fi

					if [[ ${tracknum[$index]} -eq 50 || ${tracknum[$index]} -eq 30 ]]
					then
						echo "Invalid track number; valid tracks are 1 to 29, 31 to 49 and 51 to 100"
						echo
						mk0=0
					fi
				done
			fi

			if [ ${optracker[$changenum]} -gt 20 ]
			then
				mk1=0
				index=`expr ${optracker[$changenum]} - 10`
				while test ${mk1} -ne 1
				do
					mk1=1
					#caseview2.2.sh
					echo "----------------------------------------------------------"
					i=1
					while test ${i} -le ${cases}
					do
						echo "CASE ${i}"
						echo "  Observation time:  ${obsdate[$i]}"
						echo "  Verification time: ${veridate[$i]}"
						echo "  Latitude:  ${vrlat[$i]}"
						echo "  Longitude: ${vrlon[$i]}"
						echo "  Priority:  ${prioritytxt[$i]}"
						echo "  Comments:  ${comments[$i]}"
						i=`expr ${i} + 1`
						if [ ${i} -le ${cases} ]
						then
							echo
						fi
					done
					echo "----------------------------------------------------------"

					echo
					echo "WHAT IS THE CASE NUMBER OF THE SITUATION THE FLIGHT WILL TARGET?"
					echo "(enter ${newcasenum} to input data for a new case)"
					read casenum[$index]
					echo

					newcasemarker=0
					if [ ${casenum[index]} -eq ${newcasenum} ]
					then
						echo "-----------------------------------------------------------------"
						echo "FOR CASE ${newcasenum}:"
						echo
						echo "ENTER THE OBSERVATION TIME (yyyymmddhh)"
						read obsdate[$newcasenum]
						echo
						echo "ENTER THE VERIFICATION TIME (yyyymmddhh)"
						read veridate[$newcasenum]
						echo
						echo "ENTER THE LATITUDE OF THE CENTER OF THE VERIFICATION REGION"
						read vrlat[$newcasenum]
						echo
						echo "ENTER THE LONGITUDE OF THE CENTER OF THE VERIFICATION REGION"
						read vrlon[$newcasenum]
						echo
						echo "CHOOSE A PRIORITY"
						echo "1) HIGH PRIORITY"
						echo "2) MEDIUM PRIORITY"
						echo "3) LOW PRIORITY"
						read priority[$newcasenum]
						case ${priority[$newcasenum]} in
							1) prioritytxt[$newcasenum]=HIGH;;
							2) prioritytxt[$newcasenum]=MEDIUM;;
							3) prioritytxt[$newcasenum]=LOW;;
						esac
						echo
						echo "PROVIDE COMMENTS OR PRESS ENTER"
						read comments[$newcasenum]
						echo "-----------------------------------------------------------------"
						echo
						newcasemarker=1

					else
						if [[ ${casenum[$index]} -lt 1 || ${casenum[$index]} -gt ${cases} ]]
						then
							if [ ${cases} -eq 1 ]
							then
								echo "Invalid entry; the only valid entry is 1."
							else
								echo "Invalid entry; valid entries are 1 through ${cases}"
							fi
							echo
							mk1=0
						fi
					fi

					casenumber=${casenum[$index]}
					if [[ ${obsdate[$casenumber]} -ne ${controltime[$ct]} && ${mk1} -eq 1 ]]
					then
						echo "You must select a case with ${controltime[$ct]} as the observation time"
						echo "Please try again"
						echo
						mk1=0
					fi

					if [[ ${mk1} -eq 1 && ${newcasemarker} -eq 1 ]]
					then
						newcasenum=expr` ${newcasenum} + 1 `
					fi
				done
			fi

			if [ ${optracker[$changenum]} -eq 1 ]
			then
				echo "ENTER ADDITIONAL COMMENTS ABOUT THE FLIGHT(S)"
				read morecomments
				echo
			fi

			if [ ${optracker[$changenum]} -eq 2 ]
			then
				echo "ENTER COMMENTS ABOUT THE FLIGHT(S) FOR CARCAH"
				read carcahcomments
				echo
			fi
		fi
	done
fi

if test "$dowefly" = 'n' || test "$dowefly" = 'N'
then
	echo "PROVIDE COMMENTS FOR CARCAH OR PRESS ENTER"
	read carcahcomments
	echo
	echo "PROVIDE ADDITIONAL COMMENTS OR PRESS ENTER"
	read additcomments
	echo
	echo "FLIGHT REQUEST FOR ${flydate[1]}" >> $text
	echo >> $text
	echo "  No flight is requested" >> $text
	echo >> $text
	echo "  CARCAH comments:      ${carcahcomments}" >> $text
	echo >> $text
	echo "  Additional comments:  ${additcomments}" >> $text
	echo >> $text
	echo "FLIGHT REQUEST FOR ${controltime[$ct]}" >> $carcahtext
	echo >> $carcahtext
	echo "  No flight is requested" >> $carcahtext
	echo >> $carcahtext
	echo "  Comments:  ${carcahcomments}" >> $carcahtext
	echo >> $carcahtext
fi

echo "OUTLOOK FOR ${flydate[2]}" >> $text
echo >> $text
echo "OUTLOOK FOR ${flydate[2]}" >> $carcahtext
echo >> $carcahtext

if [[ $testmode = no ]]; then
	clear
else
	echo "##############################################################################"
	echo clear page here when not in test mode
	echo "##############################################################################"
fi

echo "OUTLOOK:"
echo
echo "DO YOU ANTICIPATE FLYING AROUND ${flydate[2]}? (y/n)"
read response
echo
if test "$response" = 'y' || test "$response" = 'Y'
then
	mk29=0
	while test ${mk29} -ne 1
	do
		mk29=1
		echo "HOW MANY TRACKS ARE ANTICIPATED?"
		read outlooktks
		if [[ ${outlooktks} -gt 3 || ${outlooktks} -lt 1 ]]
		then
			echo "Only 1-3 tracks are possible.  Please try again."
			echo
			mk29=0
		else
			echo
		fi
	done

	ct=1
	while test ${ct} -le ${outlooktks}
	do

		mk3=0
		while test ${mk3} -ne 1
		do
			mk3=1
			if [ ${outlooktks} -eq 1 ]
			then
				echo "WHICH TRACK IS ANTICIPATED FOR THIS FLIGHT?"
			else
				echo "WHICH TRACK IS ANTICIPATED FOR FLIGHT ${ct}?"
			fi
			read tracknum[$ct]
			echo

			if [[ ${tracknum[$ct]} -gt 100 || ${tracknum[$ct]} -lt 1 ]]
			then
				echo "Invalid track number; valid tracks are 1 to 29, 31 to 49 and 51 to 100"
				echo
				mk3=0
			fi

			if [[ ${tracknum[$ct]} -eq 50 || ${tracknum[$ct]} -eq 30 ]]
			then
				echo "Invalid track number; valid tracks are 1 to 29, 31 to 49 and 51 to 100"
				echo
				mk3=0
			fi

			if [ ${mk3} -eq 1 ]
			then
				ct=`expr ${ct} + 1`
			fi
		done
	done

	echo "ENTER ANY COMMENTS FOR CARCAH"
	read outlookcarcah
	echo

	echo "ENTER ANY OTHER ADDITIONAL COMMENTS"
	read addcomments
	echo

	ct=1
	while test ${ct} -le ${outlooktks}
	do
		if [[ ${tracknum[$ct]} -ge 1 && ${tracknum[$ct]} -le 29 ]]
		then
			city=Anchorage
		fi

		if [[ ${tracknum[$ct]} -ge 31 && ${tracknum[$ct]} -le 56 ]]
		then
			city=Honolulu
		fi

		if [[ ${tracknum[$ct]} -ge 61 && ${tracknum[$ct]} -le 66 ]]
		then
			city=Gulf
		fi
		if [[ ${tracknum[$ct]} -ge 68 && ${tracknum[$ct]} -le 90 ]]
		then
			city=Yokota
		fi

		echo "  A flight from ${city} along track ${tracknum[$ct]} is possible" >> $text
		echo >> $text
		echo "  A flight from ${city} along track ${tracknum[$ct]} is possible" >> $carcahtext
		echo >> $carcahtext
		ct=`expr ${ct} + 1`
	done

	echo "  CARCAH comments:      ${outlookcarcah}" >> $text
	echo >> $text
	echo "  Additional comments:  ${addcomments}" >> $text

	echo "  Comments:  ${outlookcarcah}" >> $carcahtext

fi

if test "$response" = 'n' || test "$response" = 'N'
then
	echo "PROVIDE COMMENTS FOR CARCAH OR PRESS ENTER"
	read carcahcomments
	echo
	echo "PROVIDE ADDITIONAL COMMENTS OR PRESS ENTER"
	read additcomments
	echo
	echo  "  No flights are anticipated" >> $text
	echo >> $text
	echo "  CARCAH comments:      ${carcahcomments}" >> $text
	echo >> $text
	echo "  Additional comments:  ${additcomments}" >> $text
	echo "  No flights are anticipated" >> $carcahtext
	echo >> $carcahtext
	echo "  Comments:  ${carcahcomments}" >> $carcahtext
fi

if [[ $testmode = no ]]; then
	clear
else
	echo "##############################################################################"
	echo clear page here when not in test mode
	echo "##############################################################################"
fi


echo "HERE IS THE FINAL DECISION FILE:"

echo "*******************************************************************"

cat $text

echo "*******************************************************************"
echo "Please press enter when done"

read dummyresponse

if [[ $testmode = no ]]; then
	clear
else
	echo "##############################################################################"
	echo clear page here when not in test mode
	echo "##############################################################################"
fi

echo "-------------------------------------------------------------------"
echo "| The Winter Storm Reconnaissance decision file has been created. |"
echo "-------------------------------------------------------------------"

###ping -c1 emc1 > /dev/null 2>&1
###pingerr=$?
###if test $pingerr -eq 0
###then
#   ftp emc1 << ftpEOF > /dev/null 2>&1
#   cd /disk21/data/web/ensgem/wsr/text
#   mkdir $PDY
#   cd $PDY
#   lcd $GESdir
#   put ${PDY}carcah_decision.txt carcah_decision.txt
#   put ${PDY}decision.txt decision.txt
#   bye
#ftpEOF
#  ftp rzdm << ftpEOF > /dev/null 2>&1
#  cd /home/people/nco/www/htdocs/pmb/sdm_wsr
#  mkdir $PDY
#  cd $PDY
#  lcd $GESdir
#  put ${PDY}carcah_decision.txt carcah_decision.txt
#  put ${PDY}decision.txt decision.txt
#  bye
#ftpEOF

if [[ $testmode = no ]]; then

	ssh -l wx12sd ncorzdm "mkdir -p /home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY"
	scp ${COMOUT}/${PDY}carcah_decision.txt wx12sd@ncorzdm:/home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY/carcah_decision.txt
	scp ${COMOUT}/${PDY}decision.txt wx12sd@ncorzdm:/home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY/decision.txt

else

	echo "For testing send output text to $testuser on $testrzdm in $testdirectory/test$expid/$PDY"
	ssh -l $testuser $testrzdm "mkdir -p $testdirectory/test$expid/$PDY"
	scp ${COMOUT}/${PDY}carcah_decision.txt ${testuser}@$testrzdm:$testdirectory/test$expid/$PDY/carcah_decision.txt
	scp ${COMOUT}/${PDY}decision.txt ${testuser}@${testrzdm}:${testdirectory}/test$expid/$PDY/decision.txt

fi

###fi

echo ""
echo "Do you want to send an email out to CARCAH? (y/n)"
echo ""
read emailans

if test "$emailans" = 'y' || test "$emailans" = 'Y'
then
	if [[ $testmode = no ]]; then

		cat $COMOUT/${PDY}carcah_decision.txt | mail -s WINTER_STORM_RECON_REQUEST ncep.nhc.carcah@noaa.gov
		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST sdm@noaa.gov
		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST wr-roc@noaa.gov
		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST Jack.R.Parrish@noaa.gov
		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST Paul.Flaherty@noaa.gov
		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST Warren.Blier@noaa.gov
		#  cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST stephen.Renwick@keesler.af.mil
		#  cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST steverenwick@cableone.net
		#  cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST Donald.Brinson@keesler.af.mil

	else

		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST $testemail
		echo "Email Sent to:  $testemail only for testing"
		echo "Email not really sent to the following:"

	fi
	echo "Email Sent to:"
	echo " "
	echo "ncep.nhc.carcah@noaa.gov"
	echo "sdm@noaa.gov"
	echo "wr-roc@noaa.gov"
	#  echo "stephen.Renwick@keesler.af.mil"
	#  echo "steverenwick@cableone.net"
	#  echo "Donald.Brinson@keesler.af.mil"
	echo "Jack.R.Parrish@noaa.gov"
	echo "Paul.Flaherty@noaa.gov"
	echo "Warren.Blier@noaa.gov"
	echo " "
	echo "Contact Info: John Pavone (305)229-4474"
else
	if [[ $testmode = no ]]; then

		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST sdm@noaa.gov
		echo "Email Sent to sdm@noaa.gov"

	else

		cat $COMOUT/${PDY}decision.txt | mail -s WINTER_STORM_RECON_REQUEST $testemail
		echo "Email Sent to:  $testemail only for testing"

	fi
fi
exit