#!/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]'
echo "Running $0" 1>>rasp.${sitearg}.printout

# 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/"
echo "Running script $0  from $STARTDIR 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.
workdone=0

#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

# require a region input to run at all
if [[  $1 == "" ]]
then
echo "usage: getwindgrams [region]"
exit
fi

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

# 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

if [[ $region == "Blanchard" || $region == "BLANCHARD" || $region == "ALL" ]]
then


ncl SITE=\"BlanchardLZ\" windgram.ncl
ncl SITE=\"StewartLZ\" windgram.ncl
ncl SITE=\"N_Nooksack_Valley\" windgram.ncl
ncl SITE=\"BlackLZ\" windgram.ncl 
ncl SITE=\"Black\" windgram.ncl
ncl SITE=\"Blanchard\" windgram.ncl
ncl SITE=\"BJ\" windgram.ncl
ncl SITE=\"Stewart\" windgram.ncl
ncl SITE=\"Red\" windgram.ncl
workdone=1
fi

if [[ $region == "Ebey" || $region == "EBEY"  || $region == "ALL" ]]
then
ncl SITE=\"Ebey\" windgram.ncl
workdone=1
fi

if [[ $region == "Wood" || $region == "WOOD" || $region == "ALL" ]]
then
ncl SITE=\"Woodside\" windgram.ncl
ncl SITE=\"Bridal\" windgram.ncl
workdone=1
fi

if [[ $region == "Skag" || $region == "SKAG" || $region == "ALL" ]]
then 
ncl SITE=\"Iron\" windgram.ncl
ncl SITE=\"Penthouse\" windgram.ncl
workdone=1
fi

if [[ $region == "pnw" || $region == "PNW" || $region == "ALL" ]]
then
ncl SITE=\"Baldy\" windgram.ncl 
ncl SITE=\"Saddle\" windgram.ncl
ncl SITE=\"Chelan\" windgram.ncl
ncl SITE=\"Dog\" windgram.ncl
ncl SITE=\"Mazama\" windgram.ncl
workdone=1
fi


if [[ $region == "chelan" || $region == "CHELAN" || $region == "ALL" ]]
then
ncl SITE=\"Baldy\" windgram.ncl 
ncl SITE=\"Saddle\" windgram.ncl
ncl SITE=\"Chelan\" windgram.ncl
ncl SITE=\"Mazama\" windgram.ncl
workdone=1
fi


if [[ $region == "Tiger" || $region == "TIGER" || $region == "ALL" ]]
then
ncl SITE=\"Tiger\" windgram.ncl
ncl SITE=\"Rampart\" windgram.ncl
workdone=1
fi

if [[ workdone -gt 0 ]]
then
echo "Moving to output directory $WOUTDIR and converting ncgm to png "
cd $WOUTDIR
touch ran_getwindgrams_$region
# 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 700x700 -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"

#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 "Uploading "$files

$SENDTOWEB $files  2>/dev/null
#/home/tj/bin/uploadw $files
cd $WOUTDIR
#files have been backed up so OK to delete all
echo " "
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

#           1            2           3          4            5           6           7           8          9          10      11   12       13
#regions=(/ "Blanchard", "Blanchard", "Blanchard", "Blanchard", "Blanchard", "Blanchard", "Blanchard", "Blanchard", "Blanchard", "TIGER", "PNW", "PNW", "PNW", "EBEY", "WOOD", "WOOD", "SKAG", "PNW", "SKAG", "PNW", "TIGER" /)
##           1            2                3                 4       5         6         7     8       9      10      11       12       13  
#         1      2        3        4        5       6       7      8        9       10       11      12     13     14      15       16
## wlats=(/ 48.57, 48.77, 48.83, 48.9657, 48.98, 48.61, 48.86, 48.77, 48.932, 47.48, 47.81, 46.86, 46.7, 48.22, 49.247, 49.171, 48.48, 46.489, 48.333, 48.59, 47.42 /)
##           1       2        3          4        5         6         7         8          9         10        11        12       13         14       15        16         17       18        19  
## wlons=(/ -122.43, -122.4, -122.14, -122.07, -121.88, -122.40, -122.43, -122.27, -122.118, -121.94, -120.03, -120.35, -119.7, -122.76, -121.89, -121.748, -121.94, -122.173, -121.713, -120.40, -121.41 /)
## wgrids=(/ "w2", "w2", "w2", "w2", "w2", "w2", "w2", "w2", "w2", "w2", "d2", "d2", "d2", "w2", "w2", "w2", "w2", "d2", "w2", "d2", "d2" /)




