#!/bin/csh 
# this script creates the colorcoded radar image for the fewx web page by completing these steps in this order
# 0. preliminary variable setting, error checking (expected files exist?), house cleaning (removal of old log and unneeded temp files)
# 1. create a national composite via the $MAKEGRIBPROG script (uses pyart and remaps to a 1.5km grid)
# 2. decodes metar data to ptype by site lat/lon via the $PYTPEDECODER script (uses awk decoder on the last-fully retrieved metar set in $LATESTDATA)
# 3. performs a barnes analysis per ptype (rain, snow, mix) to create grib2 masks per ptype  using the pybarnes script
# 4. remaps the masks to same shape and res as natlcomposite grb2 file using wgrib2 
# 5. creates the colorized gif (resized to match what web page expects) using the gp plotting utility
# 6. restacks exising precip_??.gif files and ensures the lastest gif is precip_12.gif
# 7. sends the images to the web via oftp utility

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


### set script and output file names ##############################################################################################################################
set MAKEGRIBPROG="make_composite_grib"					# script that makes the composite radar and writes it out to a grb2 file
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
set IMAGEFILE=natrad_color.png						# the name of the image file that will be created of the nat rad image colorized by ptype
set PTYPEDECODER=radweb_ptype						# script that decodes the metars and creates pybarnes-ready ptype file
set PTYPERAWFILE=ptype.web						# file containing the pybarnes-read ptype file - name is hardcoded in the $PTYPDECODER script


### initial error checking and house cleaning ##########################################################################################################################
set numerrors=0
if (! -e $MAKEGRIBPROG) then
	echo "The script $MAKEGRIBPROG that makes the national radar composite grb2 file was not found in the present working directory `pwd`"; @ numerrors += 1
endif
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 (! -e $PTYPEDECODER) then
	echo "The script $PTYPEDECODER that makes the pybarnes-ready ptype file was not found in the present working directory `pwd`"; @ numerrors += 1
endif
if (`which pybarnes` == "") then
	echo "The barnes analysis tool  pybarnes  was not found on the system"; @ numerrors += 1
endif
if (`which wgrib2` == "") then
	echo "The grib2 tool wgrib2 was not found on the system"; @ numerrors += 1
endif
if ($numerrors > 0) goto EXITING

touch dum.grb2 dum.web dum.mask dum.ctl dum.png dum.log dum.data dum.idx; /bin/rm *.grb2 *.web* *.mask *.ctl *.png *.log *.data natrad*.gif *.idx


### main program #####################################################################################################################################################

# 1. make national composite gribfile -- warn the user and exit if it failed -- otherwise get the dims and shape of the grid for later use
echo "`date` - running composite script $MAKEGRIBPROG (3-4 minutes)"

$MAKEGRIBPROG >& composite.log

if (! -e $RAWCOMPOSITE) then
	echo "$MAKEGRIB script did not create the file $RAWCIMPOSITE"; goto EXITING
endif
set grib2dims=`wgrib2 -grid $RAWCOMPOSITE | grep "lat-lon grid:" | awk -F : '{print $2}' | awk -F ")" '{print $1}' | sed 's/(//g' | sed s/x//g`
set latbounds=`wgrib2 -grid $RAWCOMPOSITE | grep "lat " | awk -F "lat" '{print $2}' | sed s/to//g | sed s/by//g`
set lonbounds=`wgrib2 -grid $RAWCOMPOSITE | grep "lon " | grep -v "lat" | awk -F "lon" '{print $2}' | sed s/to//g | sed s/by//g`
set        nx=`echo "$grib2dims" | awk '{print $1}'`
set        ny=`echo "$grib2dims" | awk '{print $2}'`
set    minlat=`echo "$latbounds" | awk '{print $1}'`
set    maxlat=`echo "$latbounds" | awk '{print $2}'`
set        dx=`echo "$latbounds" | awk '{print $3}'`
set    minlon=`echo "$lonbounds" | awk '{print $1}'`
set    maxlon=`echo "$lonbounds" | awk '{print $2}'`
set        dy=`echo "$latbounds" | awk '{print $3}'`
set   npoints=`echo "$lonbounds" | awk -F "=" '{print $2}'`
if ($nx == "" || $ny == "" || $minlat == "" || $maxlat == "" || $minlon == "" || $maxlon == "" || $dx == "" || $dy == "" || $npoints == "") then
	echo "could not get the nx, ny, minlat, minlon, dx, dy, npoints of $RAWCOMPOSITE using wgrib2 -grid"; goto EXITING
endif
set minwestlon=`echo "$minlon" | awk '{print $minlon-360}'`  # min lon in west lon format
set maxwestlon=`echo "$maxlon" | awk '{print $maxlon-360}'`  # max lon in west lon format


# 2. run the ptype metar decoder to create three pybarnes-ready point-files per ptype rain, snow, mix (warn user and exit if failed)
echo "`date` - running metar decoder $PTYPEDECODER to make ptype information (<=1 minute)"

$PTYPEDECODER >& decoder.log

if (! -e $PTYPERAWFILE) then
	echo "$PTYPERAWFILE file was not created "; goto EXITING
endif


# 3. run the pybarnes routine per ptype file to create gridded masks by ptype (warn user and exit if failed) - type pybarnes help for more info on pybarnes
echo "`date` - running barnes analysis to create and remap ptype masks (<=1 minute)" 
awk -F , '{ptype=0; if ($NF == 1)             ptype=1; print ($1","$2","ptype)}' $PTYPERAWFILE > ptype_r.mask
awk -F , '{ptype=0; if ($NF >= 2 && $NF <= 4) ptype=1; print ($1","$2","ptype)}' $PTYPERAWFILE > ptype_s.mask
awk -F , '{ptype=0; if ($NF == 5)             ptype=1; print ($1","$2","ptype)}' $PTYPERAWFILE > ptype_mix.mask
pybarnes datafile=ptype_r.mask   outfile=ptype_r_mask.grb2   datadomain=${minwestlon},${maxwestlon},${minlat},${maxlat} backinterp=daniels >& barnes_rain.log
pybarnes datafile=ptype_s.mask   outfile=ptype_s_mask.grb2   datadomain=${minwestlon},${maxwestlon},${minlat},${maxlat} backinterp=daniels >& barnes_snow.log
pybarnes datafile=ptype_mix.mask outfile=ptype_mix_mask.grb2 datadomain=${minwestlon},${maxwestlon},${minlat},${maxlat} backinterp=daniels >& barnes_mix.log
if (! -e ptype_r_mask.grb2)   echo "pybarnes did not create ptype_r_mask.grb2   - see barnes_rain.log for more info"
if (! -e ptype_s_mask.grb2)   echo "pybarnes did not create ptype_s_mask.grb2   - see barnes_snow.log for more info"
if (! -e ptype_mix_mask.grb2) echo "pybarnes did not create ptype_mix_mask.grb2 - see barnes_mix.log  for more info"
if (! -e ptype_r_mask.grb2  || ! -e ptype_s_mask.grb2 || ! -e ptype_mix_mask.grb2) goto EXITING


# 4. remap the masks to the same domain/res (shape) as the natrad grib2 file (warn user and exit if failed)
echo "`date` - remapping ptype grib files to same resolution as natl radar composite (<= 15 sec)"
wgrib2 ptype_r_mask.grb2   -new_grid latlon ${minlon}:${nx}:${dx} ${minlat}:${ny}:${dy} ptype_r_mask_remapped.grb2   >& remap_r.log
wgrib2 ptype_s_mask.grb2   -new_grid latlon ${minlon}:${nx}:${dx} ${minlat}:${ny}:${dy} ptype_s_mask_remapped.grb2   >& remap_s.log
wgrib2 ptype_mix_mask.grb2 -new_grid latlon ${minlon}:${nx}:${dx} ${minlat}:${ny}:${dy} ptype_mix_mask_remapped.grb2 >& remap_mix.log
# 2nd pass needed because although shape of remapped gribs perfectly match $RAWCOMPOSITE, max lat and lons are not precisely aligned (off at 3rd/4th decimal place) so this assures more 1:1 matching  
set remaplatbounds=`wgrib2 -grid ptype_r_mask_remapped.grb2 | grep "lat " | awk -F "lat" '{print $2}' | sed s/to//g | sed s/by//g`
set remaplonbounds=`wgrib2 -grid ptype_r_mask_remapped.grb2 | grep "lon " | grep -v "lat" | awk -F "lon" '{print $2}' | sed s/to//g | sed s/by//g`
set    remapmaxlat=`echo "$remaplatbounds" | awk '{print $2}'`
set    remapmaxlon=`echo "$remaplonbounds" | awk '{print $2}'`    
set dyadjust=`echo "$dy $ny $maxlat $remapmaxlat" | awk '{printf "%.10f\n",($1 + ($3 - $4)/$2)}'`
set dxadjust=`echo "$dx $nx $maxlon $remapmaxlon" | awk '{printf "%.10f\n",($1 + ($3 - $4)/$2)}'`
wgrib2 ptype_r_mask.grb2   -new_grid latlon ${minlon}:${nx}:${dxadjust} ${minlat}:${ny}:${dyadjust} ptype_r_mask_remapped_adj.grb2   >& remap_adj_r.log
wgrib2 ptype_s_mask.grb2   -new_grid latlon ${minlon}:${nx}:${dxadjust} ${minlat}:${ny}:${dyadjust} ptype_s_mask_remapped_adj.grb2   >& remap_adj_s.log
wgrib2 ptype_mix_mask.grb2 -new_grid latlon ${minlon}:${nx}:${dxadjust} ${minlat}:${ny}:${dyadjust} ptype_mix_mask_remapped_adj.grb2 >& remap_adj_mix.log
if (! -e ptype_r_mask_remapped_adj.grb2)   echo "wgrib2 remapping did not create ptype_r_mask_remapped_adj.grb2   - see remap_adj_r.log   for more info"
if (! -e ptype_s_mask_remapped_adj.grb2)   echo "wgrib2 remapping did not create ptype_s_mask_remapped_adj.grb2   - see remap_adj_s.log   for more info"
if (! -e ptype_mix_mask_remapped_adj.grb2) echo "wgrib2 remapping did not create ptype_mix_mask_remapped_adj.grb2 - see remap_adj_mix.log for more info"
# 3rd pass needed to change all nans created by wgrib2 remapping to 0 in mask file (wgrib2 doent have an option to allow user to assign nan a val). goofy results otherwise occur via gp plotter where precip overlaps nan areas 
FEWXPYTHON << ENDPY
import xarray as xr; import numpy as np; from cfgrib.xarray_to_grib import to_grib
ds=xr.load_dataset("natradrawcomposite.grb2",          engine="cfgrib"); ds=ds.fillna(0); to_grib(ds,'natradrawcomposite_nonans.grb2'         ,grib_keys={'edition':2}, no_warn=True)
ds=xr.load_dataset("ptype_r_mask_remapped_adj.grb2",   engine="cfgrib"); ds=ds.fillna(0); to_grib(ds,'ptype_r_mask_remapped_adj_nonans.grb2'  ,grib_keys={'edition':2}, no_warn=True)
ds=xr.load_dataset("ptype_s_mask_remapped_adj.grb2",   engine="cfgrib"); ds=ds.fillna(0); to_grib(ds,'ptype_s_mask_remapped_adj_nonans.grb2'  ,grib_keys={'edition':2}, no_warn=True)
ds=xr.load_dataset("ptype_mix_mask_remapped_adj.grb2", engine="cfgrib"); ds=ds.fillna(0); to_grib(ds,'ptype_mix_mask_remapped_adj_nonans.grb2',grib_keys={'edition':2}, no_warn=True)
exit()
ENDPY

# 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 BASENAME=`echo $IMAGEFILE | cut -f 1 -d \.`; convert $IMAGEFILE -resize 3899x2310! ${BASENAME}.gif
set numframes=12; set currframe=1; set webfilebasename="precip_"
while ($currframe < $numframes)
	@ cframeplus1 = $currframe + 1
	set newframe=`echo $cframeplus1 | awk '{printf "%02d\n",$0}'`
	set oldframe=`echo $currframe   | awk '{printf "%02d\n",$0}'`
	if (-e ${webfilebasename}${newframe}.gif) mv ${webfilebasename}${newframe}.gif ${webfilebasename}${oldframe}.gif
	@ currframe += 1
end
cp ${BASENAME}.gif ${webfilebasename}${numframes}.gif

# 7. ftp to internal weather web page
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 &

# optional step - run this script to create a 4pnl showing the raw rad and the three ptypes for debugging -- takes <=1.5 min to complete
echo "`date` - running quicktest to create a 4pnl image of ptype components for debugging (< 2 min)"; quicktest >& test.log

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

