#!/bin/bash
# usage plotme region utchour params
# added | uniq to prevent multiple plots of same missed times appearing in printout files  2010-04-23 
# looks through the printout file to find missed plot times
# then creates a command file to plot those times
# run from runons   
# runons runs from the end of run.rasp 
# after the REGIONXYZ is finished.


# your basedir may vary
BASEDIR="/home/tj/drjack"
cd $BASEDIR/RASP/RUN

quo='"'
crlf=$':\x0a\x0d'
crvt=$'\x0b\x0d'
lf=$'\x0a'
cr=$'\x0d'
vt=$'\x0b'
lf2=$'\x16\x0a'


usage0="plotmissing $vt plots the times for a region that failed to create plots $vt$cr  because the plotting started before the data was really available."
usage="USAGE:  plotmissing region "
usage2="Where region is of the form \"PANOCHE\" or \"PANOCHE-WINDOW\"  "




if [[ !  $1  ]]
then
echo ""
echo  $usage0
echo ""
echo $usage
echo $usage2

exit
fi
# get capitalized and uncapitalized versions of the region name given in the command line
thefiles=$(echo $1 |tr '[A-Z]' '[a-z]'|sed -e "s#-window##g")
theFILES=$(echo $1 |sed -e "s#-WINDOW##g")


SITEARG=$(echo $1 | tr '[a-z]' '[A-Z]')
sitearg=$(echo $1 | tr '[A-Z]' '[a-z]')
mnth=$(date +%m)
day=$(date +%d) 
hr=$(date +%H)
yr=$(date +%Y) 

echo "  " 1>>rasp.${thefiles}.printout
echo "Running $0 at $hr $yr-$mnth-$day" 1>>rasp.${thefiles}.printout
echo "  " 1>>rasp.${thefiles}.printout

rm plotmissing$1
rm missed$1



echo "thefiles= "$thefiles
echo "theFILES= "$theFILES

#if [[ "BLANCHARD" == "$theFILES" ]]
#    then theFILES="Blanchard"
#    fi
#echo $theFILES
    

# figure out what to look for in the rasp.$thefiles.stderr file
#if [[ "$1" ==  *WINDOW ]]
#then
#res="w2"
#totest=$theFILES"-WINDOW/"  
#echo "plot missing for window run"
#else
#res="d2"
#totest=$theFILES"/"
#fi
#echo $res
#teststring="'exist.*"$totest"|FILENAMES\(0\).*"$totest\'
#teststring="'FILENAMES(0).*$totest'"
#echo $teststring
#echo "Will egrep for $teststring"

#grep "NCL TIMEOUT" rasp*err 

# to plot all the missed times 
# check error printout for race conditions where wrfout file did not exist when ncl went to look for it.
# remember double quotes for variables in sed
#missed=$(cat rasp.$thefiles.stderr |grep  "NCL TIMEOUT" |grep "$theFILES")           #"$teststring")
missed=$(grep "NCL TIMEOUT" rasp.$thefiles.stderr )           #"$teststring")
if [[ -z $missed ]] 
then 
 echo "no ncl plot files missing for the $1 domain" 
 echo "no ncl plot files missing for the $1 domain" >>rasp.$thefiles.printout
 exit
fi


echo "missed = "
echo "$missed"
echo "theFILES = $theFILES"
echo -n $missed >missedncl           #still the whole lines

#echo -n  "sed commandline = "
#echo  "s#^.*$yr-$mnth-[0-9][0-9]_#\/home\/tj\/drjack\/RASP\/RUN\/plotme $1 #g" -e 's#:.*$##g' "

if [[ "$missed" ==  *WINDOW* ]]
then
res="w2"
#totest=$theFILES"-WINDOW/"  
echo "plot missing for window run"
else
res="d2"
#totest=$theFILES"/"
fi
echo $res




#*** ERROR: /home/tj/drjack/WRF/WRFV2/RASP/PNW-WINDOW/wrfout_d02_2010-11-12_23:00:00 NCL TIMEOUT: 1 
#*** ERROR: /home/tj/drjack/WRF/WRFV2/RASP/PNW/wrfout_d02_2010-11-12_23:00:00 NCL TIMEOUT: 1 
#echo "$missed" |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#\/home\/tj\/drjack\/RASP\/RUN\/plotme $1 #g" -e 's#:.*$##g' >missed$1
#cat rasp.$thefiles.stderr |grep  FILENAMES |grep "$theFILES"|uniq |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#\/home\/tj\/drjack\/RASP\/RUN\/plotme $1 #g" -e 's#:.*$##g' >missed$1

#cat missedncl |sed -e 's#^.*wrfout_d02##g' -e 's#:00:00 NCL.*$##' -e 's#^.*\([0-2][0-9]\)#\1#g' |awk 'ORS=" "{print "missed["FNR"=" $1 "\n";}'
#cat missedncl |sed -e 's#^.*wrfout_d02##g' -e 's#:00:00 NCL.*$##' -e 's#^.*\([0-2][0-9]\)#\1#g' |awk 'ORS=" " {print "plotme blah "$1" \n";}' |sed -e "s#blah# $1 #g"
#cat missedncl |sed -e 's#^.*wrfout_d02##g' -e 's#:00:00 NCL.*$##' -e 's#^.*\([0-2][0-9]\)#\1#g' |awk 'ORS=" " {print "plotme blah "$1" \n";}' |sed -e "s#blah# $1 #g" >missed$1
cat missedncl |sed -e 's#^.*RASP/# #g' -e 's#/wrfout_d02# #g'  -e 's#:00:00 NCL.*$##' -e 's#\(^[ A-Z\_\-]*\) _.*\([0-2][0-9]\)#\1 \2#g' |awk 'ORS=" " {print "plotme blah "$1" "$2" \n";}' |sed -e "s#blah#  #g" >missed$1

echo "missed1"
cat missed$1
echo " "

#cat rasp.$thefiles.stderr |grep "exist.*$totest" |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#/home/tj/bin/upload_generic $theFILES  $quo\*#g" \
#cat rasp.$thefiles.stderr |grep  FILENAMES |grep "$theFILES" \
#  |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#\/home\/tj\/bin/upload_generic $theFILES #g"\
#  -e 's#:.*$##g' \
#  -e "s/:.*$/%%lst.$res.png$quo/g" |/home/tj/bin/utc2lsthr |sed -e 's#%%#00#g'  >>missed$1

#for modified rasp.ncl with check for filesnmes  change back to to exist if revert to rasp.ncl without it.
#cat rasp.$thefiles.stderr |grep  FILENAMES |grep "$theFiles" |uniq\
#   |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#\/home\/tj\/bin/upload_generic  $theFILES #g"\
#   -e 's#:.*$##'\
#   |/home/tj/bin/utc2lsthr \
#   |sed  -e 's#[0-9][0-9]# "\*&00lst.w2.png#' >>missed$1



#echo missed$1
#cat missed$1
echo "cmd line parameter = "$1
echo "res = "$res
echo "totest = "$totest
echo "theFILES = "$theFILES
echo "thefiles = "$thefiles

#4test exit

#cat rasp.$thefiles.stderr |grep exist |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#plotme $1 #g" -e 's#:.*$##g' >missed$1
#cat missed$1
## need to change utc to lst before upload
#cat rasp.$thefiles.stderr |grep exist |sed -e "s#^.*$yr-$mnth-[0-9][0-9]_#upload_generic $theFILES  $quo\*#g" -e "s/:.*$/%%lst.$res.png$quo/g" |/home/tj/bin/utc2lsthr |sed -e 's#%%#00#g'  >>missed$1
#cat missed$1
echo " " >> rasp.$thefiles.printout

filelist=$(cat missed$1)
#echo $filelist




if [ -z "$filelist" ] 
then 
 echo "no plot files missing for the $1 domain" 
 echo "no plot files missing for the $1 domain" >>rasp.$thefiles.printout
 exit
else
cat missed$1 >plotmissing$1
cat missed$1 | /home/tj/bin/utc2lsthr |sed -e 's/plotme/upload_generic/g' -e 's#upload_generic *\([A-Z]*\)\(-WINDOW\) \([0-9]*\)#upload_generic \1 \"\*\300lst.\2#g' -e 's#-WINDOW#w2.png\"#g' -e 's#\(^.*upload_generic *\)\([A-Z ]*\)\([0-2][0-9]\) #\1\2 \"\*\300lst.d2.png\"#g' >>plotmissing$1
#cat missed$1 | /home/tj/bin/utc2lsthr |sed -e 's/plotme/upload_generic/g' -e "s#[0-2][0-9]#\"\*&00lst.$res.png\"#g" >>plotmissing$1
cat plotmissing$1

 params=$(grep ^[^#]*@\{\$PARAMETER_DOLIST  rasp.run.parameters.$theFILES |sed -e "s/^.*[(]//g"  -e "s/[)]//g" -e 's/;//g' |tr -s "\n'," "   "  )
 echo "# "$params >>missed$1
 echo "sourcing  missed$1 to plot missing times for  $1 domain at $(date)" 
 echo "sourcing  missed$1 to plot missing times for  $1 domain at $(date)" >>rasp.$thefiles.printout
 source plotmissing$1
fi

exit

# to plot all the missed times cat rasp.bow.stderr |grep exist |sed -e 's#^.*2009-01-[0-9][0-9]_#/home/tj/drjack/RASP/RUN/plotme BOW-WINDOW #g' -e 's#:.*$##g' 
