#!/bin/csh

## script will decode the already-decoded and retrieved files by WPC stored in $LATESTDATA/sfcanalysis 
## There are 2 files fronts_latest.txt and isobars_latest.txt to decode.
## The final product is a self describing text file that gets shipped to the fewx web page in a specific format
## Each line in the file defines the object to be plotted, its color, color of its attributes, and either a lat lon point or a set of lat lon points defining the verticies of a line
## Two files get decoded and have two separate blocks of code since the two files contain different formats as provided by NCEP


## initial settings
set DIR=$SCRIPTS/sfcanalysis/webloop; cd $DIR
set OUTFILE=decoded_sfclatest.txt
set DATADIR=$LATESTDATA/sfcanalysis
set FRONTS=fronts_latest.txt
set ISOBARS=isobars_latest.txt
set YEAR=`date +%Y`; set TROPICALS=$SCRIPTS/tropical_bulletins/master_bulletins_${YEAR}.csv

## copy files here to work on - bail out if they cant be found
touch $ISOBARS $FRONTS dum_latest.txt; /bin/rm *latest.txt
if (! -e $DATADIR/$FRONTS ) echo "$DATADIR/$FRONTS wasnt found";  if (-e $DATADIR/$FRONTS)  cp $DATADIR/$FRONTS  .
if (! -e $DATADIR/$ISOBARS) echo "$DATADIR/$ISOBARS wasnt found"; if (-e $DATADIR/$ISOBARS) cp $DATADIR/$ISOBARS .
if (! -e $DATADIR/$FRONTS && ! -e $DATADIR/$ISOBARS) goto EXITING


### set object colors and associated pressure value colors -- format  0xaarrggbb where 0x is literal, aarrggbb are alpha, red, green, blue vals in hex (00-ff)
set col_TROF="0xffff8247"
set col_COLD="0xff0000ff"
set col_WARM="0xffff0000"
set col_STNRY="0xffff0000"
set col_OCFNT="0xff912cee"
set col_ISOBARS="0xffcd8200"; set pcol_ISOBARS=col_ISOBARS
set col_TS="0xffff0000"; set pcol=col_TS
set TYPES="TROF COLD WARM STNRY OCFNT ISOBARS HIGH LOW TS"


#### decode $FRONTS  
echo "`date` - decoding $FRONTS"
set CYMDT=`head -n 7 $FRONTS | egrep "EDT|EST"`; echo " " > $OUTFILE; echo " " >> $OUTFILE; echo " " >> $OUTFILE; echo "$CYMDT" >> $OUTFILE
set startline=`cat -n $FRONTS | grep "VALID" | awk '{print $1+1'}`; set maxline=`cat $FRONTS | wc -l`
sed -n $startline,${maxline}p $FRONTS | awk '{ 														\
	# this block of sed| awk will put all coords from an object on one line										\
	if ($1 == "HIGHS" || $1 == "LOWS" || $1 == "TROF" || $1 == "COLD" || $1 == "WARM" || $1 == "STNRY" || $1 == "OCFNT") { print line;line=$0}	\
	else {																		\
		if (substr($1,1,1) != "" && substr($1,1,1) != " " && substr($1,1,1) != "$") {								\
			if (line != "") {line=line" "$0} else {line=$0} 										\
		}																	\
	}																		\
}' | awk '{																		\
	# this awk block will take that one line of coords and print out the right format for the web page						\
	if ($1 == "HIGHS" || $1 == "LOWS") {														\
		type="HIGH"; color="0xff0000ff"; if ($1 == "LOWS") {type="LOW"; color="0xffff0000"}							\
		for (field=2;field<=NF;field=field+2) {													\
			pval=$field;lat=substr($(field+1),1,4)/100; latoffset=lat-.5									\
                        lon=substr($(field+1),5,3); if(substr(lon,1,1) <7) lon="1"lon; lon=lon/-10							\
			print("type:"type";color:"color";pairs:1;coords:"lat","lon";pval:"pval";pcolor:0xffffffff;ppairs:1;pcoords:"latoffset","lon)	\
		}																	\
	} else {																	\
		type=$1																	\
		if (type == "TROF")  color="0xffff8247" 												\
		if (type == "COLD")  color="0xff0000ff"													\
		if (type == "WARM")  color="0xffff0000"													\
		if (type == "STNRY") color="0xffff0000"													\
		if (type == "OCFNT") color="0xffff00ff"													\
		npairs=NF-1; coords="coords:"														\
		for (field=2; field<=NF;field++) {													\
			lat=substr($(field),1,3)/10;lon=substr($(field),4,4)/-10 #for fronts lat lon is different format than pvals			\
			if (coords != "coords:") {coords=coords","lat","lon} else {coords=coords""lat","lon}						\
		}																	\
		if (type != "") print("type:"type";color:"color";pairs:"npairs";"coords)								\
	}																		\
}' | sort | uniq >> $OUTFILE


### decode $ISOBARS
echo "`date` - decoding $ISOBARS"
awk '{																						\
	## handle by order of appearance in $ISOBARS file (per isobar object - obtain closed state, val of isobar, num pts, coord list)						\
																						\
																						\
	# <VG TYPE> line in isobar data block holding closed state AND signals start of a new isobar data block -- which means printout the previous (if not the first one)	\
	if (substr($0,1,3) == "<VG") {																		\
		split($0,texta,"CLOSED>"); split (texta[2],textb,"<"); closed=textb[1]												\
		if (started == "no") {started = "yes"} else {															\
			print "type:ISOBARS;color:0xffcd8200;pairs:"numpts";"coords";closed:"closed";pval:"pval";pcolor:0xffcd8200;ppairs:"plottedpvals";pcoords:"pcoords	\
		}																				\
		pval=""; numpts="";coords="";ppairs="";pcoords=""														\
	}																					\
																						\
																						\
	# <GROUPED TEXT> line holding isobar val																\
	if (substr($0,1,3) == "<GR") {split($0,text,">"); pval=text[2]}					 									\
																						\
																						\
	# <NUMPTS> line holding numpts																		\
	if (substr($0,1,3) == "<NU") {split($0,texta,">"); split(texta[2],textb,"<"); numpts=textb[1]}										\
																						\
																						\
	# after <NUMPTS> come lines of coord pairs -- keep appending until next <VG TYPE> or EOF encountered.  Also create list of  -- also every 10th pair 			\
	if (substr($0,1,1) != "<" && closed != "" && pval != "" && numpts != "") {												\
		if (coords == "") {																		\
			# initialize the coords string 																\
			coords="coords:"$1","$2;ncoords=1															\
			# also track number of coords and initialize the list of plotted pressure values to 1 and the first coord 						\
			ncoords=1;plottedpvals=1;pcoords=$1","$2														\
		} else {																			\
			# keep appending to a comma separated list of coords string, every 25 coords plot the pvalue on the isobar 						\
			coords=coords","$1","$2;ncoords+=1;															\
			if (ncoords == 25) {ncoords=0;plottedpvals +=1; pcoords=pcoords","$1","$2}										\
		} 																				\
	}																					\
}' started="no" $ISOBARS >> $OUTFILE 


### decode tropical systems time synced 
echo "`date` - obtaining tropical system info"

# put fronts date time in a unix date readable format
set rawhhmm=`echo "$CYMDT" | awk '{print $1}'`; set ampm=`echo "$CYMDT" | awk '{print $2}'`
set newhhmm=`echo $rawhhmm | awk '{endhrchar=1; if (length($0) == 4) endhrchar=2; print(substr($0,1,endhrchar)":"substr($0,endhrchar+1,2))}'`
set NEWCYMDT=`echo "$CYMDT" | sed "s|${rawhhmm} ${ampm}|${newhhmm} ${ampm}|g"`
set NEWSECS=`date -d "$NEWCYMDT" +%s`

# now use the time in seconds to compare to the tropical message time in seconds -- anything 4hrs old or newer print out into tropicals.txt
awk -F , '{										\
	if ($1 != "SPECIAL TIME") {							\
		cmd="date -d \""$7"\" +%s" ; cmd|getline tropicaltime;close(cmd)	\
		if ( (frontstime-tropicaltime) <=14000 ) print $0			\
	} 										\
}' frontstime=$NEWSECS $TROPICALS | sort | uniq > tropicals.txt
touch tropicals.txt

# decode tropicals.txt for the outputfile
awk -F , '{											\
	type=""											\
	if ($4 == "Hurricane") 			type="HUR"					\
	if ($4 == "Tropical Storm") 		type="TS"					\
	if ($4 == "Tropical Depression") 	type="TD"					\
	if ($4 == "Extra Tropical") 		type="ET"					\
	name=$5;mb=$(NF);maxmph=$(NF-1);lon=$(NF-2);lat=$(NF-3);VT=$(NF-4); 			\
	cmd="date -d \""VT"\" +\"%m/%d/%y%l%p %Z\""; cmd|getline VALID;close(cmd)		\
	print "type:"type";color:0xffff0000;pairs:1;coords:"lat","lon";pval:"mb" mb "maxmph" mph;pcolor:0xffff0000;ppairs:1;pcoords:"lat-.1","lon";name:"name";ncolor:0xffff0000;npairs:1;ncoords:"lat+.1","lon";validtime:NHC "VALID";vcolor:0xffff0000;vpairs:1;vcoords:"lat+.1","lon					\
}' tropicals.txt >> $OUTFILE


## send to web via ftp
echo "`date` - sending $OUTFILE to web"
set base=`echo $0 | awk -F / '{print $NF}'`
echo "	cd $FTPHOMEDIR_FEWX_INTERNAL/classes" 	 > ${base}_ftpinstructions_internal.txt
echo "  lcd $DIR"				>> ${base}_ftpinstructions_internal.txt
echo "	bi" 					>> ${base}_ftpinstructions_internal.txt
echo "	put $OUTFILE"			 	>> ${base}_ftpinstructions_internal.txt
oftp ${base} $FTPSITE_FEWX_INTERNAL `pwd`/${base}_ftpinstructions_internal.txt $FTPUSER_FEWX_INTERNAL $FTPPASS_FEWX_INTERNAL 3 300 >& ftp.log


### exit routine ##################
EXITING:
echo "`date` - exiting script"
exit(0)
