#!/bin/bash
#getwingrams
# relies on nondistributed program mmv for multiple move/rename of files based on patterns
SITEARG=$(echo $1 | tr '[a-z]' '[A-Z]')
sitearg=$(echo $1 | tr '[A-Z]' '[a-z]')

# require a region input to run at all
if [[  $SITEARG == "" ]]
then
echo "usage: getwindgramtj [DOMAIN[-WINDOW]|ALL]"
exit
fi


echo "Running $0" 1>>rasp.${sitearg}.printout
echo "Running $0"
# set these to your desired directories.
# WOUTDIR must match the output dir for (wks, ncgm)
BASEDIR=/home/tj/drjack/
STARTDIR=$(pwd)
WOUTDIR="/home/tj/drjack/RASP/UTIL/windgrams/"
PROGDIR="/home/tj/drjack/RASP/UTIL/"
echo "Running script $0  from $STARTDIR on $SITEARG basedir is /home/tj/drjack/ and WOUTDIR is $WOUTDIR "
#WOUTDIR=/home/tj/drjack"/WRF/NCL/windgrams/"
ARCHIVEDIR="/nt2/wx/archive/windgrams/"
SENDTOWEB="/home/tj/bin/uploadw"                 # could be a simple copy if your machine serves your web pages.
NCLDIR="/home/tj/drjack/WRF/NCL/"
doregions="notassigned"
workdone=0;
now=$(date +%k)

## start doing things

cd $PROGDIR

# decide what to do based on $1  
#if [ -z $1 ]
# then
#   doregions="all";
#     else
   doregions=$SITEARG
#   fi
echo $doregions



echo "It is now "$now" and we check to see whether to do pnwonly d2 run for all sites preview windgrams"
## pnw is an 8km domain without window that covers all remaining domains.  It runs very quickly and allows preliminary plots
if [[  ($doregions == "PNW"   &&  $now -gt 16  &&  $now -lt 21)  || $doregions == "PNWALL"  ]]
   then
   echo "Time to do a PNW only d2 run"
#exit
   $PROGDIR/getwindgramspnw
   exit
else
## BLANCHARD is run before some other regions and its d2 domain contains several sites of interest so a special run was created to make windgrams for those.
 if [[  ($doregions == "BLANCHARD"   &&  $now -gt 16  &&  $now -lt 21)  || $doregions == "BLANCHARDEARLYD2"  ]]
  then
  echo "Also time to do d2 windgrams for Blanchard from SKAG, EBEY, and WOOD"
  cd /home/tj/drjack/WRF/NCL
  ncl windgramtjblanchardearly.ncl DOMAIN=\"BLANCHARD\" type=\"ncgm\" ptop=30
 fi
fi

#start with empty windgram directory backups have been made in HTML and ARCHIVE

rm $WOUTDIR/*.png

echo "Not time to do pnw d2 run"
echo "getting windgramtj.ncl for  $region"
#exit

#if [[ ! -e $WOUTDIR ]]
#then
#mkdir $WOUTDIR
#fi

#if [[ ! -e $ARCHIVEDIR ]]
#then
#mkdir $ARCHIVEDIR
#fi

#  this could/should be done as a loop through the sites array with a conditional for the region...
#  that way the only customization needed would be the sites and domain arrays.
#4test echo $WOUTDIR
#4test echo $ARCHIVEDIR

cd /home/tj/drjack/WRF/NCL


#region=$(echo $1 | tr "a-z" "A-Z")
region=$SITEARG

# set this to the UTC current date which should end up the  same as date of the wrfout files for midday.  East of GMT YMMV
yyyy_mm_dd=$(date -u +%Y-%m-%d)

echo $yyyy_mm_dd
#4test 
echo "region ="$region

##                Actually run the windgramtj.ncl program through ncl.                                   
   if [[  $region == "PNW"   ||  $region == "SKAG"  || $region == "PNWALL" ]]
     then
       ncl ptop=30  DOMAIN=\"$region\" type=\"ncgm\" windgramtj.ncl 
     else
       ncl ptop=24  DOMAIN=\"$region\" type=\"ncgm\" windgramtj.ncl 
   fi
## This must match the OUTDIR used in windgramtj.ncl
cd $WOUTDIR

#test=$(ls $yyyy_mm_dd"*.ncgm")
# if [[ ! -z $test ]]  
#   then
#   workdone=1
#   fi
workdone=1               # always something wasn't working 

if [[ workdone -gt 0 ]]
then
echo "Moving to output directory $WOUTDIR and converting ncgm to png "
cd $WOUTDIR
touch ran_getwindgramtj_$region                ;create zero length temporary tracking file

######################################################################
# 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 800x800 -d sun -outfile tmp.windgram.sun
   /home/tj/drjack/UTIL/convert tmp.windgram.sun $file.png
   /usr/bin/mmv   $WOUTDIR"*.ncgm.png" $WOUTDIR"#1.png"
   rm $WOUTDIR$file
 done
###################################################################


#archive the files with the dates still part of filenames  
cp -uv *.png $ARCHIVEDIR
#remove the date part down to the site only name  using mmv  up to the first _ is #1 after the first _ is #2
/usr/bin/mmv "*_*windgram.png" "#2windgram.png"
cp -uv *.png $BASEDIR/RASP/HTML/

# create a list of files suitable to pass to curl

bfiles="{" ; efiles="}" ;files=$bfiles$(ls  $WOUTDIR*windgram.png   | sed  -e 's#^.*\/##' |tr -s "'\n" "," |sed -e 's/,$//g' )$efiles 
echo "Sending to WEB " $files

$SENDTOWEB $files  2>/dev/null
#/home/tj/bin/uploadw $files
cd $WOUTDIR
#files have been backed up so OK to delete all
echo " "
#4test 
echo "WOUTDIR before removing png files"
#pwd
echo "Files in $WOUTDIR are "$(ls $WOUTDIR*)
newfiles=$WOUTDIR"*.png"
rm $newfiles
newfiles=$WOUTDIR"*.sun"
rm $newfiles
echo ""
echo "files left in WOUTDIR after removing png files" $(ls $WOUTDIR *)
else
echo "No windgrams done for $region"
fi

echo " "
echo "moving back to $STARTDIR"
cd $STARTDIR
echo ""
#pwd
exit



ram.png		     Saddle_windgram.curr.allday.w2.png          
#  mv Red_windgram.png		     Red_windgram.curr.allday.w2.png             
#  mv Rampart_windgram.png		     Rampart_windgram.curr.allday.w2.png         
#  mv Penthouse_windgram.png		     Penthouse_windgram.curr.allday.w2.png       
#  mv N_Nooksack_Valley_windgram.png	     N_Nooksack_Valley_windgram.pn               
#  mv Mazama_windgram.png		     Mazama_windgram.curr.allday.w2.png          
#  mv Iron_windgram.png		     Iron_windgram.curr.allday.w2.png            
#  mv Ebey_windgram.png		     Ebey_windgram.curr.allday.w2.png            
#  mv Dog_windgram.png		     Dog_windgram.curr.allday.w2.png             
#  mv Chelan_windgram.png		     Chelan_windgram.curr.allday.w2.png          
#  mv Bridal_windgram.png		     Bridal_windgram.curr.allday.w2.png          
#  mv Blanchard_windgram.png		     Blanchard_windgram.curr.allday.w2.png       
#  mv BlanchardLZ_windgram.png	     BlanchardLZ_windgram.curr.allday.w2.png     
#  mv Black_windgram.png		     Black_windgram.curr.allday.w2.png           
#  mv BlackLZ_windgram.png		     BlackLZ_windgram.curr.allday.w2.png         
#  mv BJ_windgram.png			     BJ_windgram.curr.allday.w2.png              
#  mv Baldy_windgram.png                   Baldy_windgram.curr.allday.w2.png           


#exit
#/usr/bin/mmv "$yyyy_mm_dd_*windgram.png" "#1windgram.png"  
## this gets Black_windgram.png

bfiles="{" ; efiles="}" ;files=$bfiles$(ls  $WOUTDIR*windgram.png   | sed  -e 's#^.*\/##' |tr -s "'\n" "," |sed -e 's/,$//g' )$efiles 
echo "Sending to WEB " $files

$SENDTOWEB $files  2>/dev/null
#/home/tj/bin/uploadw $files
cd $WOUTDIR
#files have been backed up so OK to delete all
echo " "
#4test 
echo "WOUTDIR before removing png files"
#pwd
echo "Files in $WOUTDIR are "$(ls $WOUTDIR*)
newfiles=$WOUTDIR"*.png"
rm $newfiles
newfiles=$WOUTDIR"*.sun"
rm $newfiles
echo ""
echo "files left in WOUTDIR after removing png files" $(ls $WOUTDIR *)

touch $BASEDIR/RASP/RUN/done_getwindgramtj_$region_w2

else
echo "No windgrams done for $region"
fi

echo " "
echo "moving back to $STARTDIR"
cd $STARTDIR
echo ""
#pwd
exit



