#!/bin/csh 
# this script creates the archive colorcoded radar image for the fewx web page by completing these steps in this order
# and is launched at the end of the makeweb script

cd $SCRIPTS/rad/webloop2
echo "`date` - starting script in `pwd`"


### set script and output file names ##############################################################################################################################
set RAWCOMPOSITE="natradrawcomposite.grb2"				# the name of the composite radar grb2 file that is written -- hardcoded in $MAKEGRIBPROG script
set TEMPLATEPLOTFILE=$SCRIPTS/pycommon/ctl/natlrad_color_template.ctl	# the gp plotter input file - template needs to have some generic settings finalized
set PLOTFILE=plot_rad.ctl						# the finalized gp plotter input file
set RAINFILL=$SCRIPTS/pycommon/fills/natrad_rain.pycol			# fill scheme for ptype rain
set SNOWFILL=$SCRIPTS/pycommon/fills/natrad_snow.pycol			# fill scheme for ptype snow
set  MIXFILL=$SCRIPTS/pycommon/fills/natrad_mix.pycol			# fill scheme for ptype mix


### initial error checking and house cleaning ##########################################################################################################################
set numerrors=0
if (`which gp` == "") then
	echo "The grib plotter   gp  was not found on the system"; @ numerrors += 1
endif
if (! -e $TEMPLATEPLOTFILE) then
	echo "The template plot file $TEMPLATEPLOTFILE needed to create the national radar image was not found"; @ numerrors += 1
endif
if (! -e $RAINFILL) then
	echo "The fill scheme file $RAINFILL needed to colorize ptype rain on the radar image was not found"; @ numerrors += 1
endif
if (! -e $SNOWFILL) then
        echo "The fill scheme file $SNOWFILL needed to colorize ptype snow on the radar image was not found"; @ numerrors += 1
endif
if (! -e $MIXFILL) then
        echo "The fill scheme file $MIXFILL needed to colorize ptype mix on the radar image was not found"; @ numerrors += 1
endif
if ($numerrors > 0) goto EXITING

#touch /bin/rm


### main program #####################################################################################################################################################
# Create output based on reported weather (per https://www.weather.gov/media/okx/Aviation/TAF_Card.pdf) and error check using wetb temp.  Use temperature if no weather is reported.
set OUTFILE=winds.csv
echo "stn,lat,lon,SUSMPH,PKMPH" > $OUTFILE
cat latest.data | grep -v "STN,DD" | awk -F , '{                                                                \
        # for each ob get stnID, sus (filed ), peak (field )							\
        STN=$1;SUS=$8;PK=$9		                                                                        \
                                                                                                                \
        # get lat lon of station                                                                                \
        cmd="cat -v $SCRIPTS/stationdata/stations.csv | grep "STN",| cut -f 7-8 -d, ";                          \
        latlon=""; cmd|getline latlon;close(cmd);                                                               \
                                                                                                                \
        # if lat lon not missing print out final string to be captured in $OUTFILE                              \
        if (latlon != "-99.99,-99.99") print STN","latlon","T","TD","poormanswetb","WX","wxflag","ptype}        \
}' >> $OUTFILE


# 5. run the plotter to create the image (warn user and exit if it failed) -- first cat out the template file to replace instances of IMAGEFILE FILENAME AND GRIBFILE 
echo "`date` - plotting color coded rad image (<= 3 min)"
set RAINMASK=ptype_r_mask_remapped_adj_nonans.grb2; set SNOWMASK=ptype_s_mask_remapped_adj_nonans.grb2; set MIXMASK=ptype_mix_mask_remapped_adj_nonans.grb2;set NONANRAWCOMPOSITE=natradrawcomposite_nonans.grb2
cat $TEMPLATEPLOTFILE | sed s/IMAGEFILE/${IMAGEFILE}/g | sed s/NONANRAWCOMPOSITE/${RAWCOMPOSITE}/g | sed s/RAINMASK/${RAINMASK}/g | sed s/SNOWMASK/${SNOWMASK}/g | sed s/MIXMASK/${MIXMASK}/g | sed "s|RAINFILL|${RAINFILL}|g" | sed "s|SNOWFILL|${SNOWFILL}|g" | sed "s|MIXFILL|${MIXFILL}|g" > $PLOTFILE
gp $PLOTFILE >& gp.log
if (! -e $IMAGEFILE) then
	echo "The natl radar image $IMAGEFILE was not successfully created - see gp.log for more info"; goto EXITING
endif


### send to web #############################################################################################################################################################
# 6. resize the image for the web site then rename and restack for 12 frames by makeing the latest image 12 and reshuffling the rest (old 2 becomes current 1, old 3 becomes current 2, etc)
SEND:
echo "`date` - sending to the web (< 10 sec)"
set base=`echo $0 | awk -F / '{print $NF}'`
echo "     cd $FTPHOMEDIR_FEWX_INTERNAL/classes" > ${base}_ftpinstructions_internal.txt
echo "     mput precip_*.gif" >> ${base}_ftpinstructions_internal.txt # radar
#oftp ${base} $FTPSITE_FEWX_INTERNAL `pwd`/${base}_ftpinstructions_internal.txt $FTPUSER_FEWX_INTERNAL $FTPPASS_FEWX_INTERNAL 3 300 &


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

