#!/bin/bash
# what d2 res will appear in the .data files?
cd /home/tj/drjack/RASP/RUN/

d2_res="4.0"

if [[ -z $1 ]]
then
SITEARG="PNW"
else
SITEARG=$1
fi

sitearg=$(echo "$SITEARG" | tr '[A-Z]' '[a-z]')

echo "Running $0" 1>>/home/tj/drjack/RASP/RUN/rasp.$sitearg.printout 2>&1

# get rid of old one if it is there.  (left in case I check between domains
if [[ -e /home/tj/drjack/RASP/RUN/plotthesegrids ]] ;then rm /home/tj/drjack/RASP/RUN/plotthesegrids ; fi

cd /home/tj/drjack/RASP/HTML/$SITEARG/FCST
#echo "Moved to $(pwd)"

# find .w2.data files that have  d2 resolution, parse out the times and build into plotme REGION time commands.
grep -B1 "Reskm= $d2_res" bltopvariab.curr*w2.data |sed -e 's#^.*PDT##g' -e 's#~Z~.*~#@#g' -e 's#Proj.*$##g' -e 's#^z.*$##g' |tr "\n" " " |tr "@" "\n" |sed -e 's/^.*(//g' -e 's#\(^[0-2][0-9]\)\(.*$\)#plotme $SITEARG-WINDOW \1#g' |grep -v curr  |uniq >/home/tj/drjack/RASP/RUN/plotthesegrids
#plotme pl routine creates the previous which would then be uploaded so delete them.


# if there is output from above then do the rest  otherwise just exit

if [[  -s /home/tj/drjack/RASP/RUN/plotthesegrids ]]
then
echo "rm /home/tj/drjack/RASP/HTML/$SITEARG/FCST/previous*.png" >>/home/tj/drjack/RASP/RUN/plotthesegrids

# create upload_generic commands for the replotted w2.png files.
grep  "Reskm= $d2_res" bltopvariab.*.w2.data |sed -e 's#data:.*$#png#g' -e "s#^bltopvariab.curr#upload_generic $SITEARG \"*#g" -e 's/$/"/' >>/home/tj/drjack/RASP/RUN/plotthesegrids

cd /home/tj/drjack/RASP/RUN/

#if [[  -s /home/tj/drjack/RASP/RUN/plotthesegrids ]] ;then  echo "full"; else echo "empty"; fi
#if [[  -s /home/tj/drjack/RASP/RUN/plotthesegrids ]]
#then
#cat plotthesegrids

cat /home/tj/drjack/RASP/RUN/plotthesegrids 1>>/home/tj/drjack/RASP/RUN/rasp.wronggrids.printout 2>&1
cat /home/tj/drjack/RASP/RUN/plotthesegrids 1>>/home/tj/drjack/RASP/RUN/rasp.$sitearg.printout 2>&1

source /home/tj/drjack/RASP/RUN/plotthesegrids
touch done_wronggrids_replotted_d2_w2_mixups_$SITEARG

else
cd /home/tj/drjack/RASP/RUN/
#echo "Seems OK No w2 d2 grid mixups"
touch done_wronggrids_FOUND_NO_MIXUPS_$SITEARG
exit

fi

exit
