#!/bin/csh  
# script pull latest coded fronts and pmsl and put them into $LATESTDATA/sfcanalysis

set DIR="$SCRIPTS/sfcanalysis"; cd $DIR
mkdir -p $LATESTDATA/sfcanalysis
set EDATE=`date -u +%Y%m%d_%H%M`z # in case we cant get an actual valid time for the products we will use this

# house cleaning
touch dum_latest.txt dum.dir wget_isobars.log wget_fronts.log; /bin/rm *_latest.txt *.dir wget_isobars*.log wget_fronts*.log 

##### isobars
wget -o wget_isobarsdir.log -v -T 30 -t 1 -O isobars.dir https://ftp.wpc.ncep.noaa.gov/sfcanl_isobars/  # need this to get a likely time stamp
wget -o wget_isobars.log -v -T 30 -t 1 -O isobars_latest.txt https://ftp.wpc.ncep.noaa.gov/sfcanl_isobars/isobars_latest.txt
if (! -e isobars_latest.txt) then
	echo "isobars_latest.txt not retrieved - see wget_isobars.log for details"
else
	# see if we can get a date/time for the file
	touch isobars.dir
	set DTE=`cat isobars.dir | grep isobars_latest.txt | awk -F : '{print substr($1,length($1)-13,14)":"substr($2,1,2)}'`
	if ("$DTE" == "") then
		echo "A time stamp in the directory listing was not available for the isobars_latest.txt file -- this is unusual -- see wget_isobarsdir.log"
		echo "so its valid time will be estimated"
		cp isobars_latest.txt $ARCHIVE/sfcanalysis/${EDATE}_estimatedtimestamp.codedisobars
	else
		set ZVALIDTIME=`date -ud "$DTE" +%Y%m%d_%H%Mz`
                cp isobars_latest.txt $ARCHIVE/sfcanalysis/${ZVALIDTIME}.codedisobars
                echo "isobars_latest.txt copied to $ARCHIVE/sfcanalysis/${ZVALIDTIME}.codedisobars"
		echo "it is likely the actual valid time is the $ZVALIDTIME -3 hours rounded up to the nearest hour evenly divisible by 3"
		echo "(example if ZHR is 14, subtract 3 to get 11.  The next higher hour divisible by 3 <issuance frequency> is 12 so its likely a 12z analysis)"
	endif

	mv isobars_latest.txt $LATESTDATA/sfcanalysis
	echo "isobars_latest.txt moved to $LATESTDATA/sfcanalysis"
endif


### fronts
set YMD=`echo $EDATE | cut -f 1 -d _`
wget -o wget_frontsdir.log -v -T 30 -t 1 -O fronts.dir https://ftp.wpc.ncep.noaa.gov/coded_sfc/$YMD/
touch fronts.dir
set ZHR=`cat fronts.dir | grep codsus | tail -n 1 | awk -F _hr '{print substr($1,length($1)-1,2)}'`

if ("$ZHR" == "") then
	echo "The latest available ZHR from https://ftp.wpc.ncep.noaa.gov/coded_sfc/$YMD/ was not discernable in fronts.dir -- see wget_frontsdir.log for details"
else
	wget -o wget_fronts.log -v -T 30 -t 1 -O fronts_latest.txt https://ftp.wpc.ncep.noaa.gov/coded_sfc/$YMD/codsus${ZHR}_hr
	if (! -e fronts_latest.txt) then
		echo "fronts_latest.txt not retrieved - see wget_fronts.log for details"
	else
		# determine valid date/time in UTC and copy to archive with new name that shows valid z time
		set refline=`cat -n fronts_latest.txt | grep "CODED SURFACE FRONTAL POSITIONS" | head -n 1 | awk '{print $1}'| bc -l`
		if ($refline == "") then 
			echo "Could not find a header line in fronts_latest.txt containing the string 'CODED SURFACE FRONTAL POSITIONS'"
			echo "and therefore could not obtain its valid time - will use an estimated z time"
			exit
        	        cp fronts_latest.txt $ARCHIVE/sfcanalysis/${EDATE}_estimatedtimestamp.codedfronts
                	echo "fronts_latest.txt copied to $ARCHIVE/sfcanalysis/${EDATE}_estimatedtimestamp.codedfronts"
		else
			@ refline = $refline + 2
			set ISSUETIME=`sed -n ${refline}p fronts_latest.txt` # should get something like 636 AM EDT FRI SEP 09 2022

			@ refline = $refline + 2
			set VALIDTIME=`sed -n ${refline}p fronts_latest.txt | awk '{print $2}'` # should get something like VALID 090909Z  MMDDZZ

 			# because the valid time does not hold a year need to guard against when we cross a calendar year
 			# meaning we cant just tack on todays year 
			# Therefore we need to first convert the ISSUETIME to ZTIME 

			set MYD=`echo "$ISSUETIME" | awk '{print $5,$6,$7}'`
			set HHMM=`echo "$ISSUETIME" | awk '{HHMM=$1; if ($2 == "PM" && substr($1,2)*1 <12) HHMM=HHMM+1200; print HHMM}'`
			
			set CIVILDATE=`date -d "$MYD $HHMM"`
			set ZDATE_ISSUETIME=`date -ud "$CIVILDATE" +%Y%m%d_%H%M`# this is the zdate of the issuance time
		
			# Now we can compare the ISSUETIME in UTC (ZDATE_ISSUETIME) to the VT (also in UTC) to deduce the most likely full valid date/time of this product
			# by using simple logic -- always use the VALIDTIME HHMM the "as is" and the VALIDTIME DD "as is"
			# USe the ZDATE_ISSUETIME YMD if the day matches the VALIDTIME MMDD.  But if the VALIDTIME MMDD is different than the Zdateissuetime MMDD its tricky...
			# enter a loop to keep stepping the ISSUETIME in UTC backward 1 day until you get a MMDD to match the Valid Time MMDD 
			# (we assume the issue time is never earlier than the valid time)
		
			set  VTMMDD=`echo "$VALIDTIME" | cut -c 1-4`       # valid time MMDD in utc

			set VALIDDATE=`echo $ZDATE_ISSUETIME | cut -c 1-8`; set OLDVALIDDATE=$VALIDDATE
			while (`echo $VALIDDATE | cut -c 5-8` != "$VTMMDD") 
				set VALIDDATE=`date -d "$VALIDDATE -24 Hours" +%Y%m%d`
				if ($VALIDDATE == $OLDVALIDDATE) set VALIDDATE=`date -d $VALIDDATE -25 Hours" +%Y%m%d` # sometimes subtracting 24 hours doesnt give you the previous date 
				set OLDVALIDDATE=$VALIDDATE
			end
	 		set FINALVALIDDATE="${VALIDDATE}_"`echo $VALIDTIME | cut -c 5-6`00z

			cp fronts_latest.txt $ARCHIVE/sfcanalysis/${FINALVALIDDATE}.codedfronts
			echo "fronts_latest.txt copied to $ARCHIVE/sfcanalysis/${FINALVALIDDATE}.codedfronts"
		endif

		# move raw file to $LATESTDATA/sfcanalysis dir
	        mv fronts_latest.txt $LATESTDATA/sfcanalysis
        	echo "fronts_latest.txt moved to $LATESTDATA/sfcanalysis"
	
	endif
endif


# convert the retrieved coded fronts and isobars files to something useable by the fewx web page
echo "`date` - converting latest coded fronts and isbars file to web useable format"
#decode_sfcanalysis.sh >& decode_sfcanalysis_launch.log
webloop/sfctoweb >& cron_sfctoweb.log

EXITING:
exit(0)
