#!/bin/ksh -l
################################################################################
# setup_wsr -- setup script to be run before wsr_main job is submitted
################################################################################
# Right now outputs three files, targdata.d is created in GESdir to be read by 
# main targeting script.   (current date)targdata.d is a copy of targdata.d, but
# is stored in a different permanent directory, COMOUT.  Finally, another file 
# called (current date)targdata.txt is saved in COMOUT as a human-readable 
# version of targdata.d As of 6/5/00 these two saved files will be overwritten
# by files created by a second run of this script on the same day
# UPDATED FOR WSR02 BY J.R.MOSKAITIS 11/14/01 # 
# UPDATED 2/13/02 TO FIX INSTRUCTIONS REGARDING VERIFICATION LONGITUDE #
# UPDATED 05/09/13 for WCOSS with test mode by Richard Wobus
# UPDATED 07/12/2021 for WCOSS2 with test mode by Xianwu Xue

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

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

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

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

. /usr/share/lmod/lmod/init/profile
module purge

module load envvar/$envvar_ver
module load prod_envir/$prod_envir_ver
module load prod_util/$prod_util_ver

module list

NET=${NET:-wsr}
RUN=${RUN:-wsr}
envir=${envir:-prod}
ver=${ver:-$(echo ${wsr_ver:-v3.3.0}|cut -c1-4)}

if [[ $testmode = yes ]]; then

	# these testmode 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=para
			#PDY=20210215 # For the Canned case
			;;
		(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/sdm_wsr
			useexpid=no
			expid=""
			envir=prod
			#PDY=20210215 # For the Canned case
			;;
		(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/sdm_wsr
			useexpid=no
			expid=""
			envir=para
			#PDY=20210215 # For the Canned case
			;;
		(Xianwu.Xue)
			testemail=xianwu.xue@noaa.gov
			testuser=xianwu.xue
			testrzdm=emcrzdm
			testdirectory=/home/www/emc/htdocs/gmb/xianwu.xue/wsr_sdm
			useexpid=yes
			envir=dev
			PTMP=/lfs/h2/emc/ptmp
			expid=$(basename $(readlink -f `pwd`/../))
			PDY=20210215 #20210215 20201120
			export COMPATH=$PTMP/$LOGNAME/o/$expid/$envir/com/${NET}
			envir=prod #To make compath.py get the right path
			;;
		(*)
			echo Please add test settings to $0 for LOGNAME=$LOGNAME
			exit
			;;
	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

	fi

	echo testmode=$testmode
fi

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

####END USER SECTION #######

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

####################################
# Set Working Directories
####################################
export COMIN=${COMIN:-$(compath.py ${envir}/com/${NET}/${ver})}
export COMOUT=${COMOUT:-$(compath.py ${envir}/com/${NET}/${ver})/setup}
#export COMOUT=${COMOUT:-$(compath.py ${envir}/com/${NET}/${ver})/${RUN}.${PDY}/setup}

if [ ! -s $COMOUT ]; then mkdir -p $COMOUT; fi
echo COMOUT=$COMOUT

if [[ $testmode = yes ]]; then
	ls -al $COMOUT
fi

text=${COMOUT}/targdata.txt.$curhh
rm -rf $text 2> /dev/null

cd $COMOUT
if [[ $testmode = no ]]; then
	rm targdata.d
	rm -rf targdata.d 2> /dev/null
else
	if [[ -f targdata.d ]]; then
		dtg=`date -r targdata.d +%Y%m%d%H%M%S`
		echo dtg=$dtg
		ls -l targdata.d
		for file in targdata.d targdata.txt targdata.txt? targdata.txt?? targdata.txt??? targdata.txt????
		do
			if [[ -f $file ]]; then
				ls -l $file
				mv $file $file.$dtg
				ls -l $file.$dtg
			fi
		done
	fi
	if [[ -d $COMIN ]]; then
		cd $COMIN
		for dir in wsr.$PDY/main
		do
			if [[ -d $dir ]]; then
				ls -ld $dir
				mv $dir $dir.$dtg
				ls -ld $dir.$dtg
			fi
		done
		cd $COMOUT
	fi
fi

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

echo $PDY >> $COMOUT/targdata.d

echo "----------------------------------------------------------------"
echo "|       WINTER STORM RECONNAISSANCE PROGRAM INPUT SCRIPT       |"
echo "----------------------------------------------------------------"
echo
echo "Please enter one case for each verification date and region."
echo "Additional cases between the observation date entered and the" 
echo "verification date will be added for the computations."
echo
echo
echo "ARE THERE FLIGHT REQUESTS FOR TONIGHT? Y/N" 
read tonight
if test "$tonight" = 'y' || test "$tonight" = 'Y'
then
	echo "DO YOU WANT TO ADD THEM TODAY? Y/N"
	read resp
	if test "$resp"='y' || test "$resp"='Y'
	then
		echo "PLEASE REMEMBER TO ADD THE CASE(S)! "

		readfile=$COMOUT/verifcases$curyy.d

		if [[ -s $readfile ]]; then

			rawlines=`wc -l $readfile`
			lines=`echo $rawlines|cut -d" " -f1 `
			cases=`expr ${lines} / 5`

			echo $cases
			i=1
			line=1
			while [[ ${i} -le ${cases} ]]
			do
				veridate[i]=`head -n $line $readfile | tail -1`
				line=`expr $line + 1`
				obsdate[i]=`head -n $line $readfile | tail -1`
				line=`expr $line + 1`
				vrlon[i]=`head -n $line $readfile | tail -1`
				line=`expr $line + 1`
				vrlat[i]=`head -n $line $readfile | tail -1`
				line=`expr $line + 1`
				radvr[i]=`head -n $line $readfile | tail -1`
				line=`expr $line + 1`
				echo ${obsdate[$i]}
				i=`expr ${i} + 1`
			done

		fi

	fi
   
fi

echo 
echo "HOW MANY TARGETING CASES DO YOU WANT TO RUN?"
read cases
echo
echo

echo $cases >> $COMOUT/targdata.d
if [ ${cases} -ne 1 ]; then
	echo "On ${curmm}/${curdd}/${curyy}, ${cases} cases were run" >> $text
else
	echo "On ${curmm}/${curdd}/${curyy}, 1 case was run" >> $text
fi

resolution=1
echo $resolution >> $COMOUT/targdata.d

if [ ${resolution} -eq 1 ]; then
	echo "The targeting program was run in synoptic mode" >> $text
else
	echo "The targeting program was run in mesoscale mode" >> $text
fi 

mk50=0
while test ${mk50} -eq 0
do
	mk50=1
	echo "                                    SEARCH           VERIFICATION       VERIFICATION"
	echo "                                     AREA                NORM              RADIUS"
	echo "<ENTER> OR 1) FOR TPARC/WSR DEFAULT:80E,300E;00N,80N 3-level u,v,T        1000 km"
	echo "           2) PACJET DEFAULT : 200E,240E;30N,55N     3-level u,v,T         500 km"
	echo "           3) EAST COAST     : 260E,300E;20N,50N     3-level u,v,T         500 km"
	echo "           4) MANUALLY ENTER"
	echo "              NEW PARAMETERS"
	read defchoice
	echo

	case $defchoice in
		"") vnorm=2
			radvr=1000
			lon1=80
			lon2=300
			lat1=00
			lat2=80;;
		1) vnorm=2
			radvr=1000
			lon1=80
			lon2=300
			lat1=00
			lat2=80;;
		2) vnorm=2
			radvr=500
			lon1=200
			lon2=240
			lat1=30
			lat2=55;;
		3) vnorm=2
			radvr=500
			lon1=260
			lon2=300
			lat1=20
			lat2=50;;
		4) mk51=0
			while test ${mk51} -eq 0
			do
				mk51=1
				echo "CHOOSE ONE OF THE FOLLOWING VERIFICATION NORMS"
				echo "1) 200mb, 500mb, AND 850mb WINDS"
				echo "2) 200mb, 500mb, AND 850mb WINDS AND TEMPERATURE"
				echo "3) 850mb WINDS, PRECIPITATION, AND MSLP"
				read vnorm
				if [[ ${vnorm} -ne 1 && ${vnorm} -ne 2 ]]; then
					if [ ${vnorm} -ne 3 ]; then
						mk51=0
						echo
						echo "Invalid entry, please try again"
					fi
				fi
				echo
			done

			mk52=1
			echo "ENTER THE RADIUS OF THE VERIFICATION REGION (km)"
			read radvr
			echo

			echo "** for longitudes in the western hemisphere, degrees east = 360 - degrees west **"
			echo "** please enter integer values **"
			echo
			echo "ENTER THE WESTERN BOUNDARY OF THE SEARCH AREA (degrees east of 0)"
			read lon1
			echo
			echo "ENTER THE EASTERN BOUNDARY OF THE SEARCH AREA (degrees east of 0)"
			read lon2
			echo
			echo "ENTER THE SOUTHERN BOUNDARY OF THE SEARCH AREA (degrees north)"
			read lat1
			echo
			echo "ENTER THE NORTHERN BOUNDARY OF THE SEARCH AREA (degrees north)"
			read lat2
			echo

			mk12=0
			while test ${mk12} -eq 0
			do
				echo "---------------------------------------------------------------"
				echo "PLEASE VERIFY THIS SEARCH AREA INFORMATION"
				echo "---------------------------------------------------------------"
				echo "1) WESTERN BOUNDARY: ${lon1}"
				echo "2) EASTERN BOUNDARY: ${lon2}"
				echo "3) SOUTHERN BOUNDARY: ${lat1}"
				echo "4) NORTHERN BOUNDARY: ${lat2}"
				echo "---------------------------------------------------------------"
				echo "<ENTER> IF THIS IS CORRECT OR PRESS c TO MAKE CHANGES"

				read response

				if test "$response" = ''
				then
					mk12=1
				else
					echo
					echo "ENTER THE NUMBER OF THE ITEM YOU WISH TO CHANGE"
					read changenum
					echo
					case $changenum in
						1) echo "ENTER THE WESTERN BOUNDARY OF THE SEARCH AREA"
							read lon1;;
						2) echo "ENTER THE EASTERN BOUNDARY OF THE SEARCH AREA"
							read lon2;;
						3) echo "ENTER THE SOUTHERN BOUNDARY OF THE SEARCH AREA"
							read lat1;;
						4) echo "ENTER THE NORTHERN BOUNDARY OF THE SEARCH AREA"
							read lat2;;
						*) echo "please enter one of the numbers on the menu";;
					esac
					echo
				fi
			done;;

		*) echo "Invalid entry, please try again"
			mk50=0
			echo;;

	esac
done

echo ${vnorm} >> $COMOUT/targdata.d
echo ${radvr} >> $COMOUT/targdata.d
echo ${lon1} >> $COMOUT/targdata.d
echo ${lon2} >> $COMOUT/targdata.d
echo ${lat1} >> $COMOUT/targdata.d
echo ${lat2} >> $COMOUT/targdata.d

case $vnorm in
	1) echo "200mb, 500mb and 850mb winds were used as the verifying norm" >> $text;;
	2) echo "200mb, 500mb and 850mb winds and temperature were used as the verifying norm" >> $text;;
	3) echo "850mb winds, precipiation, and mslp were used as the verifying norm" >> $text;;
esac

echo "The radius of the verification region(s) is ${radvr}km" >> $text
echo
echo >> $text
echo "Search Area Boundaries" >> $text
echo "  Western: ${lon1}E" >> $text
echo "  Eastern: ${lon2}E" >> $text
echo "  Southern: ${lat1}N" >> $text
echo "  Northern: ${lat2}N" >> $text

ct1=0
while test ${ct1} -ne ${cases}
do
	ct1=`expr ${ct1} + 1`
	mk2=0
	while test ${mk2} -ne 1
	do
		mk3=0
		while test ${mk3} -ne 1
		do
			mk3=1
			verifsame='n'
			if [ ${ct1} -ne 1 ]; then
				echo "IS THE FLIGHT OBSERVATION TIME FOR CASE ${ct1} ALSO ${obsdate}? (y/n)"
				read choice
				echo
				if test "$choice" = 'n' || test "$choice" =  'N'
				then
					echo "FLIGHT OBSERVATION TIME FOR CASE ${ct1} (yyyymmddhh)"
					echo "Enter the earliest observation time."
					read obsdate
					echo
					echo "IS THIS VERIFICATION CASE IDENTICAL TO THE PREVIOUS CASE? (y/n)"
					read verifsame
					echo
				fi
			else
				echo "FLIGHT OBSERVATION TIME FOR CASE ${ct1} (yyyymmddhh)"
				echo "Enter the earliest observation time."
				read obsdate
			fi

			if [ ${#obsdate} -ne 10 ]; then
				echo "Invalid number of digits"
				echo "There must be ten digits"
				mk3=0
			fi

			hh=`echo $obsdate|cut -c9-10`
			if [[ ${hh} -ne 00 && ${hh} -ne 12 ]]; then
				echo "Invalid time; valid times are 00 and 12"
				mk3=0
			fi

			#latestobstime=`/nwprod/util/exec/ndate +204 ${PDY}00`
			latestobstime=`$NDATE +204 ${PDY}00`
			if [ ${obsdate} -gt ${latestobstime} ]; then
				echo "Observation time must be within 204 hours of ${PDY}00"
				mk3=0
			fi


			if [ ${mk3} -eq 0 ]; then
				echo "Please try again"
				echo
			else
				echo
			fi

		done

		if test "$verifsame" = 'y' || test "$verifsame" = 'Y'
		then
			nothing=0
		else

			mk4=0
			while test ${mk4} -ne 1
			do
				mk4=1
				echo "VERIFICATION TIME FOR CASE ${ct1} (yyyymmddhh)"
				read veridate

				if [ ${#veridate} -ne 10 ]; then
					echo "Invalid number of digits"
					echo "There must be ten digits"
					mk4=0
				fi

				hh=`echo $veridate|cut -c9-10`
				if [[ ${hh} -ne 00 && ${hh} -ne 12 ]]; then
					echo "Invalid time; valid times are 00 and 12"
					mk4=0
				fi

				latestveritime=`$NDATE +204 ${PDY}00`
				if [ ${veridate} -gt ${latestveritime} ]; then
					echo "Verification time must be within 204 hours of ${PDY}00"
					mk4=0
				fi

				if [ ${mk4} -eq 0 ]; then
					echo "Please try again"
					echo
				else
					echo
				fi

			done
		fi

		if [ ${obsdate} -ge ${veridate} ]; then
			echo "The verification time must be larger than the observing time."
			echo "The selected observation and verification times are:"
			echo "$obsdate and $veridate"
			echo "Please try again"
			echo
		else
			mk2=1
		fi
	done

	if test "$verifsame" = 'y' || test "$verifsame" = 'Y'
	then
		nothing=0
	else

		echo "LATITUDE OF THE CENTER OF THE VERIFICATION REGION FOR CASE ${ct1}"
		echo "(integer or one decimal place)"
		read lat
		echo

		echo "LONGITUDE OF THE CENTER OF THE VERIFICATION REGION FOR CASE ${ct1}"
		echo "(Note! Use negative to represent degrees west, e.g. -70.5)"
		read long
		echo

		mk7=0
		while test ${mk7} -ne 1
		do
			echo "CHOOSE A PRIORITY FOR CASE ${ct1}"
			echo "1) HIGH PRIORITY"
			echo "2) MEDIUM PRIORITY"
			echo "3) LOW PRIORITY"
			read priority
			case $priority in
				1) mk7=1
					prioritytxt=HIGH;;
				2) mk7=1
					prioritytxt=MEDIUM;;
				3) mk7=1
					prioritytxt=LOW;;
				*) echo "Invalid entry, please try again";;
			esac
			echo
		done

		echo "ENTER COMMENTS ABOUT CASE ${ct1}, OR PRESS ENTER"
		read comments
		echo

	fi

	mk8=0
	while test ${mk8} -ne 1
	do

		echo "----------------------------------------------------------"
		echo "PLEASE VERIFY THIS INFORMATION YOU HAVE ENTERED FOR CASE ${ct1}"
		echo "----------------------------------------------------------"
		echo "1) OBSERVATION TIME:  $obsdate"
		echo "2) VERIFICATION TIME: $veridate"
		echo "3) LATITUDE: $lat"
		echo "4) LONGITUDE:  $long"
		echo "5) PRIORITY:  $prioritytxt"
		echo "6) COMMMENTS: $comments"
		echo "----------------------------------------------------------"
		echo "<ENTER> IF THIS IS CORRECT, OR PRESS c TO MAKE CHANGES"
		read response

		if test "$response" = ''
		then
			echo $obsdate >> $COMOUT/targdata.d
			echo $veridate >> $COMOUT/targdata.d
			echo $long >> $COMOUT/targdata.d
			echo $lat >> $COMOUT/targdata.d
			echo $priority >> $COMOUT/targdata.d
			echo $comments >> $COMOUT/targdata.d
			chmod u=rwx $COMOUT/targdata.d
			mk8=1

			echo >> $text
			echo "FOR CASE ${ct1}:" >> $text
			echo "Observation time:  ${obsdate}" >> $text
			echo "Verification time: ${veridate}" >> $text
			echo "Latitude:  ${lat}N" >> $text
			check_vrlon=`echo "${long} <=0"|bc -l`
			#if [[ $long -le 0 ]]; then
			if [[ ${check_vrlon} -eq 1 ]]; then
				longwest=`echo "0 - ${long}"|bc`
				echo "Longitude: ${longwest}W" >> $text
			else
				echo "Longitude: ${long}E" >> $text
			fi
			echo "Priority:  ${prioritytxt}" >> $text
			echo "Comments:  ${comments}" >> $text
		else
			echo
			echo "ENTER THE NUMBER OF THE ITEM YOU WISH TO CHANGE"
			read changenum
			echo
			case $changenum in
				1) mk3=0
					while test ${mk3} -ne 1
					do
						mk3=1
						echo "FLIGHT OBSERVATION TIME FOR CASE ${ct1} (yyyymmddhh)"
						read obsdate
						echo

						if [ ${#obsdate} -ne 10 ]; then
							echo "Invalid number of digits"
							echo "There must be ten digits"
							mk3=0
						fi

						hh=`echo $obsdate|cut -c9-10`
						if [[ ${hh} -ne 00 && ${hh} -ne 12 ]]; then
							echo "Invalid time; valid times are 00 and 12"
							mk3=0
						fi

						#latestobstime=`/nwprod/util/exec/ndate +204 ${PDY}00`
						latestobstime=`$NDATE +204 ${PDY}00`
						if [ ${obsdate} -gt ${latestobstime} ]; then
							echo "Observation time must be within 204 hours of ${PDY}00"
							mk3=0
						fi

						if [ ${mk3} -eq 0 ]; then
							echo "Please try again"
							echo
						else
							echo
						fi
					done
					if [ ${obsdate} -ge ${veridate} ]; then
						echo "*******************************************"
						echo "WARNING: THE OBSERVATION TIME IS CURRENTLY"
						echo "         GREATER THAN THE VERIFICATION TIME"
						echo "         PLEASE MAKE APPROPRIATE CHANGES"
						echo "*******************************************"
						echo
					fi;;

				2) mk4=0
					while test ${mk4} -ne 1
					do
						mk4=1
						echo "VERIFICATION TIME FOR CASE ${ct1} (yyyymmddhh)"
						read veridate

						if [ ${#veridate} -ne 10 ]; then
							echo "Invalid number of digits"
							echo "There must be ten digits"
							mk4=0
						fi

						hh=`echo $veridate|cut -c9-10`
						if [[ ${hh} -ne 00 && ${hh} -ne 12 ]]; then
							echo "Invalid time; valid times are 00 and 12"
							mk4=0
						fi

						latestveritime=`$NDATE +204 ${PDY}00`
						if [ ${veridate} -gt ${latestveritime} ]; then
							echo "Verification time must be within 204 hours of ${PDY}00"
							mk4=0
						fi

						if [ ${mk4} -eq 0 ]; then
							echo "Please try again"
							echo
						else
							echo
						fi
					done
					if [ ${obsdate} -ge ${veridate} ]; then
						echo "*******************************************"
						echo "WARNING: THE OBSERVATION TIME IS CURRENTLY"
						echo "         GREATER THAN THE VERIFICATION TIME"
						echo "         PLEASE MAKE APPROPRIATE CHANGES"
						echo "*******************************************"
						echo
					fi;;

				4) mk5=0
					echo "LONGITUDE OF THE CENTER OF THE VERIFICATION REGION FOR CASE ${ct1}"
					echo "(Note! Use negative to represent degrees west, e.g. -70.5)"
					read long
					echo;;

				3) mk6=0
					echo "LATITUDE OF THE CENTER OF THE VERIFICATION REGION FOR CASE ${ct1}"
					echo "(integer or one decimal place)"
					read lat
					echo;;

				5) mk7=0
					while test ${mk7} -ne 1
					do
						echo "CHOOSE A PRIORITY FOR CASE ${ct1}"
						echo "1) HIGH PRIORITY"
						echo "2) MEDIUM PRIORITY"
						echo "3) LOW PRIORITY"
						read priority
						case $priority in
							1) mk7=1
								prioritytxt=high;;
							2) mk7=1
								prioritytxt=medium;;
							3) mk7=1
								prioritytxt=low;;
							*) echo "Invalid entry, please try again";;
						esac
						echo
					done;;

				6) echo "ENTER COMMENTS ABOUT CASE ${ct1}, OR PRESS ENTER"
					read comments
					echo;;

				*) echo "Invalid response";;
			esac
		fi
		echo
	done
	if [[ $testmode = no ]]; then
		clear
	else
		echo "##############################################################################"
		echo clear page here when not in test mode
		echo "##############################################################################"
	fi
done



#
#  Later edit - march from obsdatas to verifdates 
#

TARGFILE=$COMOUT/targdata.d

head -1 $TARGFILE | tail -1 > $COMOUT/targdata_tmp.d
CASES=`head -2 $TARGFILE | tail -1` #| read CASES
RESOLUTION=`head -3 $TARGFILE | tail -1` # | read RESOLUTION 
IVNORM=`head -4 $TARGFILE | tail -1` # | read IVNORM
RADVR=`head -5 $TARGFILE | tail -1` # | read RADVR
LON1=`head -6 $TARGFILE | tail -1` # | read LON1
LON2=`head -7 $TARGFILE | tail -1` # | read LON2
LAT1=`head -8 $TARGFILE | tail -1` # | read LAT1
LAT2=`head -9 $TARGFILE | tail -1` # | read LAT2

i=1
while [ $i -le $CASES ]
do
	LINE=`expr ${i} \* 6 + 4`
	OBSDATE[i]=`head -n $LINE $TARGFILE | tail -1` # | read OBSDATE[i]
	LINE=`expr $LINE + 1`
	VERIDATE[i]=`head -n $LINE $TARGFILE | tail -1` # | read VERIDATE[i]
	LINE=`expr $LINE + 1`
	VRLON[i]=`head -n $LINE $TARGFILE | tail -1` # | read VRLON[i]
	LINE=`expr $LINE + 1`
	VRLAT[i]=`head -n $LINE $TARGFILE | tail -1` # | read VRLAT[i]
	LINE=`expr $LINE + 1`
	PRIORITY[i]=`head -n $LINE $TARGFILE | tail -1` # | read PRIORITY[i]
	LINE=`expr $LINE + 1`
	COMMENTS[i]=`head -n $LINE $TARGFILE | tail -1` # | read COMMENTS[i]
	i=`expr ${i} + 1`
done

#curyy=`date -u +%Y`
#curmm=`date -u +%m`
#curdd=`date -u +%d`
#curhh=`date -u +%H`
#PDY=${curyy}${curmm}${curdd}
#latestobstime=`/nwprod/util/exec/ndate +204 ${PDY}00`
latestobstime=`$NDATE +204 ${PDY}00`

i=1
CURCASES=0
while [ $i -le $CASES ]
do
	#  LOOP TO ADD TIME STEPS BETWEEN OBSDATE AND VERIDATE
	OBSDATE_TMP=${OBSDATE[i]}
	while [[ $OBSDATE_TMP -le ${VERIDATE[i]} && $OBSDATE_TMP -le $latestobstime ]]
	do
		#OBSDATE_TMP=`/nwprod/util/exec/ndate +12 $OBSDATE_TMP`
		OBSDATE_TMP=`$NDATE +12 $OBSDATE_TMP`
		CURCASES=`expr $CURCASES + 1`
	done
	i=`expr ${i} + 1`
done

echo $CURCASES >> $COMOUT/targdata_tmp.d
echo $RESOLUTION >> $COMOUT/targdata_tmp.d
echo $IVNORM >> $COMOUT/targdata_tmp.d
echo $RADVR >> $COMOUT/targdata_tmp.d
echo $LON1 >> $COMOUT/targdata_tmp.d
echo $LON2 >> $COMOUT/targdata_tmp.d
echo $LAT1 >> $COMOUT/targdata_tmp.d
echo $LAT2 >> $COMOUT/targdata_tmp.d

i=1
while [[ $i -le $CASES ]]
do
	OBSDATE_TMP=${OBSDATE[i]}
	while [[ $OBSDATE_TMP -le ${VERIDATE[i]} && $OBSDATE_TMP -le $latestobstime ]]
	do
		echo ${OBSDATE_TMP} >> $COMOUT/targdata_tmp.d
		echo ${VERIDATE[i]} >> $COMOUT/targdata_tmp.d
		echo ${VRLON[i]} >> $COMOUT/targdata_tmp.d
		echo ${VRLAT[i]} >> $COMOUT/targdata_tmp.d
		echo ${PRIORITY[i]} >> $COMOUT/targdata_tmp.d
		echo ${COMMENTS[i]} >> $COMOUT/targdata_tmp.d
		#OBSDATE_TMP=`/nwprod/util/exec/ndate +12 $OBSDATE_TMP`
		OBSDATE_TMP=`$NDATE +12 $OBSDATE_TMP`
	done
	i=`expr ${i} + 1`
done


chmod 777 ${COMOUT}/targdata_tmp.d
mv ${COMOUT}/targdata_tmp.d ${COMOUT}/targdata.d

if [[ $testmode = no ]]; then

	ssh -l wx12sd ncorzdm "mkdir -p /home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY"
	scp ${COMOUT}/targdata.d wx12sd@ncorzdm:/home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY
	scp ${COMOUT}/targdata.txt.$curhh wx12sd@ncorzdm:/home/people/nco/www/htdocs/pmb/sdm_wsr/$PDY/targdata.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}/targdata.d $testuser@$testrzdm:$testdirectory/test$expid/$PDY
	scp ${COMOUT}/targdata.txt.$curhh $testuser@$testrzdm:$testdirectory/test$expid/$PDY/targdata.txt

fi

if [[ $testmode = no ]]; then
	cat  $COMOUT/targdata.d | mail -s WSR_SETUP.$PDY sdm@noaa.gov
	echo "Email Sent to:  sdm@noaa.gov"
else
	cat  $COMOUT/targdata.d | mail -s WSR_SETUP.$PDY $testemail
	echo "Email Sent to:  $testemail only for testing"
fi

echo "------------------------------------------------------------------"
echo "|  The Winter Storm Reconnaissance input file has been created.  |"
echo "|         Please ask the operator to run jwsr_main_00            |"
echo "------------------------------------------------------------------"

#ftp rzdm << ftpEOF > /dev/null 2>&1
#cd /home/people/nco/www/htdocs/pmb/sdm_wsr
#mkdir $PDY
#cd $PDY
#lcd $COMOUT
#put targdata.txt.$curhh targdata.txt
#put targdata.d targdata.d
#bye
#ftpEOF

exit