#!/bin/bash
# This actually works to re plot an hour including the .data files
# it writes the ncgm output to ENV_NCL_OUTDIR
# 
if [[ $1 == "" || $2 == "" || $3 == "" ]]                                        
then
echo "USAGE: plot_nowrf REGION[-WINDOW] UTCDATEnn  UTCTIMEnn"
echo "The entire set of parameters for the given region and grid will be plotted, converted to png, and moved to the html directory."
echo "Intermediate files will be in the normal $BASEDIR/RASP/RUN/OUT directories."
echo "The default is to name the plots as if from the w2 grid."
exit
fi

# whole length minus (whole lenght - ndx)
echo $1 
inputname=$1
inlen=${#inputname}
ndx=$(expr index "$inputname" "-")
echo "inlen="$inlen
echo "where - is ndx ="$ndx
if [[ $ndx -eq 0 ]]
then 
baselen=$inlen
else
taillen=$(( (($inlen-$ndx))+1 ))
echo "taillen="$taillen
baselen=$(( $inlen-$taillen ))  #  7 so even if it is just PNW-W it should be a window
#baselen=$(( $baselen-1 ))
fi

echo $baselen" = baselen"
basename=${inputname:0:baselen}
echo $basename" = basename"
 
if [[ $baselen -eq $inlen ]]
then
 echo $1" is a not Window"
 grid="d2"
 dirname=$basename
 echo "dirname = "$dirname
else
 echo $1" is window"
 dirname=$basename"-WINDOW"
 grid="w2"
 echo "dirname = "$dirname
fi

#echo $dirname

#echo "end of new stuff"
#startwin=${basename%%"-"}
#echo "startwin= "$startwin
#echo $ndx

#if [[ $basename == $1 ]]
#then  
#echo $basename" equals "$1" its a non WINDOW" 
#else 
#echo $1" is a Window";  
#fi

#if [[ $4 == "" ]]                                        
#then
#grid="w2"
#else
#grid=$4
#fi



cd /home/tj/drjack/WRF/NCL/
pstpdt=$(date +%Z)
localtime=$(echo $3 |/home/tj/bin/utc2lsthr)"00lst"
month=$(date -u +%m)
yyyy=$(date -u +%Y)
echo $localtime
wrf_file="/home/tj/drjack/WRF/WRFV2/RASP/"$dirname"/wrfout_d02_"$yyyy"-"$month"-"$2"_"$3":00:00"
echo $wrf_file

export ENV_NCL_ID="Valid $localtime $pstpdt from  $3UTC rerun "
export ENV_NCL_REGIONNAME=$dirname
##export  ENV_NCL_FILENAME="/home/tj/drjack/WRF/WRFV2/RASP/$1/wrfout_d02_$yyyy-$month-$2_$3:00:00"
export  ENV_NCL_FILENAME=$wrf_file
##"/home/tj/drjack/WRF/WRFV2/RASP/$1/wrfout_d02_$yyyy-$month-$2_$3:00:00"

echo $ENV_NCL_FILENAME

export ENV_NCL_OUTDIR="/home/tj/drjack/RASP/RUN/OUT/$dirname"

echo $ENV_NCL_OUTDIR
#remove soundings
export  ENV_NCL_PARAMS="hbl:boxwmax:dbl:hwcrit:dwcrit:wstar:bsratio:sfcsunpct:sfcshf:experimental2:hglider:blwind:sfcwind:sfcwind0:sfcwind2:tenmwind:zsfclcl:zsfclcldif:zsfclclmask:zblcl:zblcldif:zblclmask:blcloudpct:blcwbase:press990:press980:press970:press960:press950:press900:press940:press930:press920:press910:press860:press870:press880:press890:press850:press700:press1000:bltopvariab:bltopwind:wblmaxmin:blwindshear:zwblmaxmin:sfcdewpt:rhblmax:cape:wstar_bsratio:mslpress:rain1:sfctemp:wrf=CFRACH:wrf=CFRACM:wrf=CFRACL:wrf=RAINNC:xbl_-122.43_48.61_45:xbl_-122.43_48.61_windk_8:xbl_-122.07_48.91_windk_3"
export ENV_NCL_DATIME="Day= $2  ValidZ= $3 Local=$localtime$pstpdt  Init= 0"
export  ENV_NCL_CONTOURS="FIXED"
echo $ENV_NCL_DATIME 

# ENV_NCL_INFOFILENAME= missing

#exit

/home/tj/drjack/UTIL/ncl rasp.ncl >/home/tj/drjack/RASP/RUN/plot_nowrf.rasp.printout
#ENV_NCL_REGIONNAME=$1 ENV_NCL_FILENAME= "/home/tj/drjack/WRF/WRFV2/RASP/PNW-WINDOW/wrfout_d02_2011-11-$2_$3:00:00"

cd $ENV_NCL_OUTDIR
# use ctrans and convert to get the ncgm files turned into png images.
#for file in $yyyy_mm_dd*.ncgm
for file in *.ncgm
 do
 /home/tj/drjack/UTIL/ctrans $file  -resolution 1200x1200 -d sun -outfile tmp.$file.sun
 /usr/bin/convert tmp.$file.sun $file.png
 /usr/bin/mmv   "*.ncgm.png" "#1.curr.$localtime.$grid.png"
 rm $file
 rm tmp.$file.sun
 done

if [[ $grid == "w2" ]]
then
 /usr/bin/mmv   "*.data" "#1.curr.$localtime.$grid.data"
fi

v0=$1
pubdir=${v0/%-WINDOW/}
echo $pubdir
echo "copy to  /home/tj/drjack/RASP/HTML/$pubdir/FCST/"
ls -lart *.curr.$localtime.$grid.png
cp -u *.curr.$localtime.$grid.data /home/tj/drjack/RASP/HTML/$pubdir/FCST/
cp -u *.curr.$localtime.$grid.png  /home/tj/drjack/RASP/HTML/$pubdir/FCST/


