#!/bin/csh 
# Script creates the precip type color coded natl radar image at 1km resolution for web page 
# using existing sfc temp grid and natl radar grids -- the latter two are routinely created 
# by other programs. 
#
# It is designed to run every 10 min since it completes within 3-4 min, 8 min when bogged down 
#
# This is launched then every 10 offset by one minute to let the composite script run 
#
# This program also ensures hourly sfc obs will be time matched to the radar overlay
#
# First created 2010 by BK
#
# Re done for speed 2012 09 11 by PCM
#
# 2014 12 10 pcm for new realtime ptype grid (5=s, 4=ip, 3=mix, 2=zr, 1=r)
#

echo " ";echo $0 started at `date`; echo " "

###### regulate how many instances of this script can run concurrently 
set instances=`ps -ef | grep "${0}" | grep "/bin/csh" | wc -l`
echo "For logging purposes the number of instances of this script $0 presently running is $instances"; echo " "
if ($instances > 1) then
        echo "Including this, there are presently ${instances} instances of '$0' running, only 1 allowed so exiting the script."
        goto EXITING
endif

### main body
setenv DISPLAY ":0.0"
set DIR=$SCRIPTS/rad/web
mkdir -p $DIR
cd $DIR

# delete any .nts files and ensure we have a non corrupt color scheme for precip fill
touch dum.nts dum.out coltbl.xwp radimg.gif; /bin/rm -rf *.nts *.out coltbl.xwp radimg.gif*
if ( (! -e precipfill.tbl.bak || -z precipfill.tbl.bak)  && (! -e precipfill.tbl || -z precipfill.tbl)) then
        echo "Warning - local table and its backup containing the fill scheme for precip types was not found (precipfill.tbl and precipfill.tbl.bak)"
        echo "The default fill table is being used which won't provide meaningful colors for a radar image"
else
	if (! -e precipfill.tbl || -z precipfill.tbl) cp precipfill.tbl.bak precipfill.tbl
	cp precipfill.tbl coltbl.xwp
endif


# use the latest gridded radar file in $OBS/grid/rad/natl 
set GFUNC="bref1"
set numpasses=1
set passtext="pass"
set min=missing
set DTE=""
set RADGRID=`ls -1t $OBS/grid/rad/natl/natl*.grd | head -n 1`
if ($RADGRID == "") then
        echo "Warning - No radar file found in $OBS/grid/rad/natl dir"
        set GFUNC=-5
else
	echo "$RADGRID  exists"
	set ZDATE=`echo $RADGRID | head -n 1 |cut -f 2-3 -d _ | cut -f 1 -d \. | sed 's/_/ /g'`
	set a=`date -ud "$ZDATE"`
	set DTE=`date -d "$a" +%l:%M" "%p" "%a" "%b-%d-%Y | tr '[a-z]' '[A-Z]'`
endif

set PTYPEGRID=`ls -1t $OBS/grid/sfc/us_ptype_*hr_old.grd | head -n 1`
if ($PTYPEGRID == "") then
	echo "Warning - no surface temperature grid found in $OBS/grid/sfc"
	echo "Images will not be color coded by temperature - default rain scheme will be used"
else 
	# lets check the PTYPEGRID to see if there are any temps within the ranges that need to be color coded
	# if not then lets not iwaste time doing the extra passes in gdplot3
	echo "$PTYPEGRID exists"
	gdlist_gn << ENDGDLIST > gdlist.out
		GDATTIM  = last
		GLEVEL   = 0
		GVCORD   = none
		GFUNC    = wpty 
		GDFILE   = $PTYPEGRID
		GAREA    = dset
		PROJ     =  
		SCALE    = 0
		OUTPUT   = T
		r
		exit
ENDGDLIST
	set max=`cat gdlist.out | grep "MINIMUM AND MAXIMUM" | awk '{print $6}'`
	if ($max == "") then
		set max=na
	else
		if (`echo "scale=5; $max >= 2 && $max < 5" | bc -l` == 1) then
			set numpasses=2
			set passtext=passes
			#set GFUNC="bref1\!MUL(bref1,mul(ge(TMPF+2^last,30),lt(TMPF+2^last,35)))"
			set GFUNC="bref1\!MUL(bref1,ge(wpty+2^last,1.99))"
		endif
		if (`echo "scale=5; $max >= 5 " | bc -l` == 1) then
			set numpasses=3
			set passtext=passes
			#set GFUNC="bref1\!MUL(bref1,mul(ge(TMPF+2^last,30),lt(TMPF+2^last,35)))\!MUL(bref1,lt(TMPF+2^last,30))"
			set GFUNC="bref1\!MUL(bref1,ge(wpty+2^last,1.99))\!MUL(bref1,ge(wpty+2^last,4.99))"
		endif
	endif
endif

#############
# At this point we will ensure we have time matched data for lightning and hourlies that get posted to the web site -- 
# we do this now to get it out of the way since the majority of the script spends time on gdplot3 below
# 
# To ensure time matching of the overlays on the google radar web iterface, corresponing lightning and hourly data are generated at this point
# This removes the overhead (internal code) in the web interface that would otherwise have to check for time matching
# The google interface however does perform time matching on the advisories watches and warnings
#
# lightning data prep for google (takes less than 1 seconds to run) -- the parser will exit if $DTE is empty so it wont crash
# hrly data prep for google (takes 30 seconds to run)
#echo prepping hourly data at `date`
#$SCRIPTS/metar/webhrly > $SCRIPTS/metar/web/log.webhrly

############
echo gdplot3 `date` on radar grid $RADGRID with $numpasses $passtext since greatest ptype cat in sfc grid is $max
gdplot3_gf << ENDGD > gdplot3.out
	\$MAPFIL=mehsuo.ncp
        GDFILE   = $RADGRID + $PTYPEGRID
        GDATTIM  = last 
        GLEVEL   = 0
        GVCORD   = none
        PANEL    = 0
        SKIP     = 0
        SCALE    = 0
        GDPFUN   = $GFUNC
        TYPE     = F
        CONTUR   = 0
        CINT     = 0
        LINE     = 0
        FINT     = 5/5
	FLINE    = 0-14!0;1;2;15;16;17;18;19;20;21;22;23;24;13;14!0;1;2;25;26;27;28;29;30;31;9;10;11;13;14
        HILO     =  
        HLSYM    =  
        CLRBAR   = 
        WIND     =
        REFVEC   =
        TITLE    = 0
        TEXT     = 0
        CLEAR    = yes!no!no
        GAREA    = 21.64;-127.62;50.40;-66.50
        PROJ     = mer//nm
        IJSKIP   = 0
        MAP      = 0
        MSCALE   = 0
        LATLON   = 0
        DEVICE   = gf|radimg.gif|2048;1220
        STNPLT   =
        SATFIL   =
        RADFIL   =
        IMCBAR   =
        LUTFIL   =
        STREAM   =
        POSN     = 0
        COLORS   = 0
        MARKER   = 0
        GRDLBL   = 0
        FILTER   = NO
        BOXLIN   = 0
        REGION   = VIEW
        TXTCOL   =
        TXTYPE   =
        TXTFIL   =
        TXTLOC   =
        COLUMN   = 1
        SHAPE    =
        INFO     =
        LOCI     =
        ANOTLN   =
        ANOTYP   =
        l

	r

        exit
ENDGD

if ( ! -e radimg.gif) then
	echo "Warning - radimg.gif (gif image of radar) not found - nothing new posted to the web"
	goto EXITING
endif

echo "Converting..... at `date`"
if (-e precip_02.gif) convert -transparent black precip_02.gif precip_01.gif
if (-e precip_03.gif) convert -transparent black precip_03.gif precip_02.gif
if (-e precip_04.gif) convert -transparent black precip_04.gif precip_03.gif
if (-e precip_05.gif) convert -transparent black precip_05.gif precip_04.gif
if (-e precip_06.gif) convert -transparent black precip_06.gif precip_05.gif
if (-e precip_07.gif) convert -transparent black precip_07.gif precip_06.gif
if (-e precip_08.gif) convert -transparent black precip_08.gif precip_07.gif
if (-e precip_09.gif) convert -transparent black precip_09.gif precip_08.gif
if (-e precip_10.gif) convert -transparent black precip_10.gif precip_09.gif
if (-e precip_11.gif) convert -transparent black precip_11.gif precip_10.gif
if (-e precip_12.gif) convert -transparent black precip_12.gif precip_11.gif
if (-e    radimg.gif) convert -transparent black radimg.gif    precip_12.gif

# fnames file creation/modification 
touch tempfnames.txt; /bin/rm -rf tempfnames.txt; touch fnames.txt 
set maxnum=12 #max number of lines that should be in the fnames.txt file
@ oneless = $maxnum - 1
tail -n $oneless fnames.txt > tempfnames.txt

# in case fnames.txt was empty need to still have it full so here is klugy way of filling it
set c=1
while ($c <= $maxnum)
	echo "${DTE}" >> tempfnames.txt # in caxe fnbe sure fnames has a full number of lines in the file to avoid error message on wxvviewer clock
	@ c ++
end
head -n $maxnum tempfnames.txt > fnames.txt

#FTP
echo "xmitting radar image files and hrly text files and wawa and lightning at `date`"
set base=`echo $0 | awk -F / '{print $NF}'`
echo "     cd $FTPHOMEDIR_FEWX_INTERNAL/classes" > ${base}_ftpinstructions_internal.txt
#echo "     put $LATESTDATA/sfcanalysis/decoded_sfclatest.txt decoded_sfclatest.txt" >> ${base}_ftpinstructions_internal.txt # non gempak fronts/isobars
echo "     lcd $SCRIPTS/rad/ntlcomposite" >> ${base}_ftpinstructions_internal.txt 
echo "     mput precip_*.gif" >> ${base}_ftpinstructions_internal.txt # radar
echo "     put fnames.txt" >> ${base}_ftpinstructions_internal.txt # time labels
#echo "     lcd $SCRIPTS/metar/metar_hrly_webserver" >> ${base}_ftpinstructions_internal.txt # obs
echo "     lcd $SCRIPTS/metar/web" >> ${base}_ftpinstructions_internal.txt # obs
echo "     mput hrly_web_*.txt" >> ${base}_ftpinstructions_internal.txt
#echo "     lcd $SCRIPTS/wwa/decoded_files/active" >> ${base}_ftpinstructions_internal.txt # wwa
#echo "     put ACTIVE_WARNINGS_WEBSERVER.txt warn.txt" >> ${base}_ftpinstructions_internal.txt # convective warnings
#echo "     put ACTIVE_WATCHES_WEBSERVER.txt watch.txt" >> ${base}_ftpinstructions_internal.txt # convective watches
#echo "     put ACTIVE_WWA_WEBSERVER.txt wstm.txt" >> ${base}_ftpinstructions_internal.txt # winter storm wwa
#echo "     put wstm.txt warn.txt" >> ${base}_ftpinstructions_internal.txt # winter storm wwa



## lightning data
#pwd
#echo "before ltg move"
#ll ltg_*
set SNAPSHOT=$SCRIPTS/nldn/nldn_snapshot.csv
set filenummax=11; set filenum=1
while ($filenum <= $filenummax)
	@ filenumnext = $filenum + 1
        touch ltg_${filenumnext}.txt; mv ltg_${filenumnext}.txt ltg_${filenum}.txt; echo moved ltg_${filenumnext}.txt to ltg_${filenum}.txt
        @ filenum ++
end
#echo "after ltg move"
#ll ltg_*
# filter live stream for message ids of "8" (field 1) and cgs vs incloud (field 5 = 1) and also make sure its all data since last time period processed ($PROCESSMAX)
# btw the $6/1 in the print a sneaky way to get rid of a trailing Carriage Return that didn tget filtered out by dos2unix.  Dividing by 1 casts it into a number vs a string
set PROCESSMAX=600
touch ltg_12.txt; /bin/rm ltg_12.txt
awk -F , '{if ($1 == 8 && systime()-$2/1000 <= PROCESSMAX && $5 == 1) print strftime("%m/%d/%y %H:%M:%S",$2/1000),$3,$4,$6/1,$5}' PROCESSMAX=$PROCESSMAX $SNAPSHOT > ltg_12.txt

pwd 
echo "dump ltg12" $SNAPSHOT $PROCESSMAX 
awk -F , '{if ($1 == 8 && systime()-$2/1000 <= PROCESSMAX && $5 == 1) print strftime("%m/%d/%y %H:%M:%S",$2/1000),$3,$4,$6/1,$5}' PROCESSMAX=$PROCESSMAX $SNAPSHOT 

echo "finished dump"
ll ltg_12.txt
echo "     lcd $DIR" >> ${base}_ftpinstructions_internal.txt
echo "     lcd $SCRIPTS/nldn" >> ${base}_ftpinstructions_internal.txt
echo "     mput ltg_*.txt" >> ${base}_ftpinstructions_internal.txt


##### old NAWIPS generated
#echo "     lcd $DIR" >> ${base}_ftpinstructions_internal.txt
#echo "     mput precip_*.gif" >> ${base}_ftpinstructions_internal.txt
#echo "     put $SCRIPTS/sfcanalysis/currentsfcanl.txt currentsfcanl.txt" >> ${base}_ftpinstructions_internal.txt # sfc vgf
#set LATESTFILE=`ls $GEMDATA/misc/warning|grep -v .firsthalf|sort -nr | head -n 1`; if ("$LATESTFILE" != "") echo "     put $GEMDATA/misc/warning/${LATESTFILE} warn.txt" >> ${base}_ftpinstructions_internal.txt #warnings
#echo "     lcd $DIR" >> ${base}_ftpinstructions_internal.txt
#echo "     put fnames.txt" >> ${base}_ftpinstructions_internal.txt  
#echo "     put $SCRIPTS/wwa/fewxwebwatchlist.txt watch.txt" >> ${base}_ftpinstructions_internal.txt #watches
#echo "     lcd $SCRIPTS/metar/web" >> ${base}_ftpinstructions_internal.txt # obs
#echo "     mput hrly_web_*.txt" >> ${base}_ftpinstructions_internal.txt
#set LATESTFILE=`ls $GEMDATA/misc/wstm   |grep -v .firsthalf|sort -nr | head -n 1`; if ("$LATESTFILE" != "") echo "     put $GEMDATA/misc/wstm/${LATESTFILE}    wstm.txt" >> ${base}_ftpinstructions_internal.txt #wstm 

oftp ${base}_ddo $FTPSITE_FEWX_INTERNAL `pwd`/${base}_ftpinstructions_internal.txt $FTPUSER_FEWX_INTERNAL $FTPPASS_FEWX_INTERNAL 3 300 &

###############EXIT ROUTINE
EXITING:
echo " ";echo script $0 finished at `date`; echo " "
exit(0)
