#!/bin/bash
# usage plotme region utchour params
## the plotting program handles the UTC to lst time conversion
## this should all be UTC to PST or local standard time  based 
## if no parameterd are passed param list is read from rasp.run.parameters.$theFILES
BASEDIR="/home/tj/drjack"
imageoutdir="$BASEDIR/tmp"
regiontimeadj="PDT -7"

#useful constants and initilizations
plothour=""
us="_"
lf="$'\n'"
quo='"'

usage="USAGE:  plotme region utchour [params]"
usage2="Where region is of the form \"PANOCHE\" or \"PANOCHE-WINDOW\", \"utchour\" is 2 digit and \"params\" are a space separated list \
or use \"allday\" to plot all available hours for the domain.  \
Currently limited to one time or all times. \
"

if [[ -z  $1  ]]
then 
echo $usage
echo $usage2

fi
region=$1

echo "Running plotme for $1 $2"

##  Get date info for when this is being run.
#mnth=$(date +%m)
#day=$(date +%d) 
hr=$(date +%H)
utc_hr=$(date -u +%H)
uct_hr=$(echo "$(( 10#$utc_hr ))")  # base conversion so leading zeros don't get mis-interpreted
yr=$(date +%Y) 
pstpdt=$(date +%Z)
offset=$(date +%z)
offset="-"${offset:2:1}   #substring of returned value start at pos 2 for one char  index starts at 0 

# if it has run, the day and month  of plots will be the utc day and month, not the local day 
day=$(date -u +%d)
mnth=$(date -u +%m)
## must deal with end of month days.. and it happily also deals with the leading zero issue for nday..
## late afternoon evening plots are for next utc day and month.
nuday=$(date -u --date=tomorrow +%d)
nday=$(date -u --date=tomorrow +%d)

numonth=$(date -u --date=tomorrow +%m) 
nmonth=$(date --date=tomorrow +%m) 


#date --date=tomorrow +%b%d
#day=$(( $day-1 ))               ## for after the fact runs when wrfout files still around
#echo $day

#pstpdt="PST"    # whole pstpdt thing is only needed to tell when to switch the day to nday.
echo "offset= "$offset
if [[ "$1" ==  *WINDOW ]]
then
res="w2"
else
res="d2"
fi
echo $res

## kludges to shift time back and forth
if [[ $pstpdt == "PST" ]]
then
index=(16 17 18 19 20 21 22 23  0  1  2  3 4  5  6  7  8   9 10 11 12 13 14 15)  
plothours=(08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07)   # lst  PST
plotprogfullpath=$BASEDIR/RASP/UTIL/plot.wrfoutfilePST.PL
plotdates=( $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $nday $nday $nday $nday $nday $nday $nday $nday ) 
else
index=(17 18 19 20 21 22 23  0  1  2  3 4  5  6  7  8   9 10 11 12 13 14 15 16)  ## for PDT?
plothours=(07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 )   # lst  PDT
plotprogfullpath=$BASEDIR/RASP/UTIL/plot.wrfoutfilePDT.PL
plotdates=( $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $nday $nday $nday $nday $nday $nday $nday $nday ) 
fi

# to plot all the missed times 

theFILES=$(echo $1 |sed -e "s#-WINDOW##g")
thefiles=$(echo $1 |tr '[A-Z]' '[a-z]'|sed -e "s#-window##g")
echo $thefiles
echo $theFILES
sleep 1

#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
#exit

#####  should not need to edit below here


# index is for controlling the loop when needed
# map the index to the hours so that index[0]corresponds to 00Z in lst 
#index=0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23       PST 
#  the array plothours is indexed so that inputs can be mapped and looped throuh based on an index rather than times that might cross
#  the UTC / local date boundary.    All plotting is done for current day another mechanism could be added to indicate the default date
#           0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23



#echo "starting day is $day and length of day array is ${#day} "
#lday=${#day}

#uct_hr=$(echo "$(( 10#$utc_hr ))")  # base conversion so leading zeros don't get mis-interpreted

#if [[ "$hr" -gt "15" ]]
#then 
#echo "Because hr > 15 going to the next day"
#day=$(echo "$(( 10#$day ))")
#day=$(($day+1))
#lday=${#day}
#echo " $day and length of it is ${#day} "
#fi

#if [[ "$lday" -lt 2 ]]
#then
#day="0"$day
#echo "day number is length  $lday which is less than 2 add a leading zero"
#fi


#nday=$(($day+1));
#lnday=${#nday}
#if [[ "$lnday" -lt 2 ]]
#then
#echo "nday (next day) number is  length $lnday which is less than 2 add a leading zero"
#nday="0"$nday
#fi




#if [[ $pstpdt == "PST" ]]
#then
#echo "Nextday nday = $nday"
# this is a mapping for the UTC date boundary crossing.
#            0   1     2    3    4    5    6    7    8    9    10   11   12  13   14    15   16    17    18    19    20    21   22     23
#echo "Using standard time"
#plotdates=( $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $nday $nday $nday $nday $nday $nday $nday $nday ) 
#else
#echo "Using daylight time"
# nday in PDT is at 17UTC
#plotdates=( $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $day $nday $nday $nday $nday $nday $nday $nday $nday ) 
#fi
#  together these three insure that dates will be correct and match the wrfout conventions.



if [[  -z $2 ]]
then
echo $usage
echo $usage2
exit
else
if [[ $2 == "allday" ]]
then
start=8
numhours=12
i=$start;
end=$((1+$numhours+$i));
else



numhours=1
echo "Today is $day and nextday = nday = $nday"
n=$(echo "$(( 10#$2 ))")     # use base conversion to get rid of leading zero and not be interpreted as octal


echo ${index[$n]} "is index[ $n ] which will be assigned to start"
echo $n "is n"
start=${index[$n]}


 
echo $start "is start  input was $2"
plothour=$2 
test=( ${plothours[$start]} )
echo "plothours [ start] = plothours[$start] =  $test and the index pointer is $start  "
numhours=1
end=$(( $start + 1 ))
i=$start
 echo "Using passed hour $2 = plothour = $plothour indexed from $i to $end "
fi
fi

echo "Will start at  ${plothours[$start]} UTC  for  $numhours hours while hour is less than  ${plothours[$end]}  UTC."
datest="${plotdates[$start]}$us${plothours[$start]}"
echo " The date string is now: $datest"
echo "command line will be:"
command="$BASEDIR/RASP/UTIL/plot.wrfoutfile.PL -s -g 1200x1200 -f -d $imageoutdir $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params"
echo $command
#4test ls -la $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00
##exit
#4test  exit

# have parameters been passed?
if [[ -z $3  ]]
then
#params=$(grep ^[^#]*@\{\$PARAMETER_DOLIST  rasp.run.parameters.BLANCHARD |sed -e "s/^.*[(]//g"  -e "s/[)]//g" -e 's/;//g' |tr -s "\n" " " |tr -s " " "," |sed -e s/,$// )
params=$(grep ^[^#]*@\{\$PARAMETER_DOLIST  rasp.run.parameters.$theFILES |sed -e "s/^.*[(]//g"  -e "s/[)]//g" -e 's/;//g' |tr -s "\n'," "   "  )
echo $params
#grep ^[^#]*@\{\$PARAMETER_DOLIST  rasp.run.parameters.Blanchard |sed -e "s/^.*[(]//g"  -e "s/[)];$/,/g" |tr -s "\n" " "
#params="hbl dbl hwcrit dwcrit wstar bsratio sfcsunpct sfcshf experimental2 hglider blwind sfcwind zsfclcl zsfclcldif zsfclclmask zblcl zblcldif zblclmask blcloudpct blcwbase press990 press980 press970 press960 press950 press900 press940 press930 press920 press910 press850 press700 press1000 bltopvariab bltopwind wblmaxmin zwblmaxmin  blwindshear  sfctemp sfcdewpt  cape  wstar_bsratio mslpress  wrf=RAINC wrf=ALBEDO wrf=SNOWC wrf=SNOWNC  sounding1 sounding2 sounding3 sounding4 sounding5 sounding6 sounding7 sounding8 sounding12 sounding19 sounding20 sounding21 ";
##params=" dbl hwcrit dwcrit wstar bsratio sfcsunpct sfcshf experimental2 hglider blwind sfcwind zsfclcl zsfclcldif zsfclclmask zblcl zblc ";
else
params=$3
fi

## was $numhours  was -lt the -le then -lt ...
while [[ $i -lt $end   ]]
do
 plothr=${plothours[$i]};
 dt=${plotdates[$i]}



#echo "for date = $dt hour =  $plothr   sub[$i]" ;
##imageoutdir="$BASEDIR/RASP/RUN/OUT/$region"
datest="$dt$us$plothr"
#echo ""
#echo " The date string is now: $datest"
#echo ""
rm $imageoutdir/*.ncgm  
##rm $BASEDIR/tmp/*.png

### using utc should make this un-necessary and it is broken for first of the month
#if [[ $mnth -ne $nmonth  ]]    ## if it still breaks add back and include && $day -ne $utcday
#then
#mnth=$nmonth
#fi

echo "Plotting for  $1 $2 $3 Starting at $start $pstpdt :"
echo " Plotting for $1 $2 $3 Starting at $start $pstpdt  :" >>rasp.$thefiles.printout

echo "$plotprogfullpath -s -g 1200x1200 -f -d $imageoutdir $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params"
#echo "$BASEDIR/RASP/UTIL/plot.wrfoutfile.PL -s -g 1200x1200 -f -d $imageoutdir $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params"
echo -e "\n$plotprogfullpath -s -g 1200x1200 -f -d $imageoutdir $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params\n" >>rasp.$thefiles.printout
#echo "$BASEDIR/RASP/UTIL/plot.wrfoutfile.PL -s -g 1200x1200 -f -d $imageoutdir $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params" >>rasp.$thefiles.printout
echo ""
echo "" >>rasp.$thefiles.printout



#$BASEDIR/RASP/UTIL/plot.wrfoutfile.PL -s   -g 1200x1200 -f -d $imageoutdir  $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params
$plotprogfullpath -s   -g 1200x1200 -f -d $imageoutdir  $BASEDIR/WRF/WRFV2/RASP/$region/wrfout_d02_$yr-$mnth-$datest:00:00 $params

(( i += 1 ))
#sleep 2s
done
touch "done_plotme_`basename $0`_$*"
# to plot all the missed times cat rasp.bow.stderr |grep exist |sed -e 's#^.*2009-01-[0-9][0-9]_#plotme BOW-WINDOW #g' -e 's#:.*$##g' 
