#!/bin/bash
BSITEARG=$(echo $1 | tr '[a-z]' '[A-Z]')
bsitearg=$(echo $1 | tr '[A-Z]' '[a-z]')
echo "Running $0" 1>>rasp.${bsitearg}.printout
echo "Running $0  output to rasp.$bsitearg.printout"
 
cd /home/tj/drjack/RASP/UTIL
rm /home/tj/drjack/RASP/UTIL/blipspots/*.html

doregions="notassigned"
workdone=0;
if [ -z $1 ] 
 then
 doregions="all";
else
 doregions=$1;
fi

echo $doregions

if [[ $doregions == "PNW" && $(date +%k) -gt 16 &&  $(date +%k) -lt 21 ]]
 then
 echo "doing getblipspnw"
getblipspnw
exit
fi


if [[ $doregions == "Blanchard"  && $(date +%k) -gt 16 &&  $(date +%k) -lt 22 ]]
then 
getblipsblanchardearly
exit
fi


#Start with an empty directory by archiving.
#datetag=$(date +%Y%m%d%H)
#mmv "/home/tj/drjack/RASP/UTIL/blipspots*" "/nt2/wx/blipspots/$datetag#1"

#rm ./blipspots/*.html

echo "doregions = $doregions"
#exit

# run the extract blipspot program on predefined coordinates for different regions
# run this after the region in question has run
#
# variable inputs to this script  region 
# this script will handle the inputs hard wired to the 
# /home/tj/drjack/RASP/UTIL/extract.blipspot.PL PNW PNW d2 1 script
# that script requires that extract.blipspot.site.parameters be set to desired values
#
# \$1=region site  \$2=grid# \$3=day(-1/0/1/2)|yyyymmdd(archive) \$4,5=i,j(integer)_or_lat,lon(decimal) \$6=LINFO <\$*=time/param>\n;
#
# |sed -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<tr><td>#' -e 's#<td>$#</tr>#' 
# The above, when used with a 0 at the end of the command, gets an html table of the data without any elaboration.
# need to join it to labels for each row and column if used.  The following creates a table with numbered lines
# ./extract.blipspot.PL PNW PNW d2 0 48.22 -122.76 0 |sed -e '/^/=' -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<td>#' -e 's#<td>$#</tr>#g'|sed -e 
# using 0 option this gets a numbered file
#./extract.blipspot.PL PNW PNW d2 0 48.22 -122.76 0 |sed -e '/^/=' -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<td>#' -e 's#<td>$#</tr>#g'|sed -e 's#\(^[0-9][0-9]*\)#@\1#' |tr "\n" " " |tr "@" "\n"  > outtemp
#then join it with a column of names and strip leading line numbers
#join blipparams outtemp |sed -e 's#^[0-9]* ##' >blipspot.tmp
# the following line was working
# ./extract.blipspot.PL PNW PNW d2 0 48.22 -122.76 0 |sed -e '/^/=' -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<td>#' -e 's#<td>$#</tr>#g'|sed -e 's#\(^[0-9][0-9]*\)#@\1#' |tr "\n" " " |tr "@" "\n" 
#echo "<html><head><title>Blipspot for all Pacific NWet RASP sites</title><link rel=\"stylesheet\" type=\"text/css\" href=\"weather.css\"></head><body><table> "  >./blipspots/allblips.html


#blip map input and output parameters
#regions=( "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Tiger" "PNW"  "PNW" "PNW")  

if [[ "BLANCHARD" == "$doregions" ]]  
    then doregions="Blanchard"
fi

echo "doregions = $doregions   $(date)" >>blipspot.log
#Blanchard BlackLZ w2  48.9657 -122.07   #black lz
#Blanchard Red w2 0 48.932 -122.118 1
#blanchar LZ  48.57 -122.43 
#           1            2           3          4            5           6           7           8          9          10      11   12       13

#domains=( "Blanchard"  "SKAG"   "PNW"    "TIGER"   "EBEY"  "WOOD"  "CHELAN" )
#regions=( "Blanchard"  "Blanchard"  "Blanchard"  "Blanchard" "Blanchard"  "Blanchard"  "Blanchard"  "Blanchard"  "Blanchard"  \
#           "Blanchard"  "Blanchard" "Blanchard"  "Blanchard"  "Blanchard"  "Blanchard"  \
#           "TIGER"  "PNW"  "PNW"  "PNW"  \
#	   "EBEY"  \
#	   "WOOD"  "WOOD"  "WOOD"  \
#	   "SKAG"  "SKAG"  \
#	   "PNW" \
#	   "SKAG"  "SKAG" \
#           "PNW"  \
#	   "TIGER" \
#	   "CHELAN" )
#
#sites=( "BlanchardLZ"   "StewartLZ"  "N_Nooksack_Valley"  "BlackLZ"   "Black" "BlackNo"  "Blanchard"  "BJ" "BJLZS"  \
#        "Stewart"  "Red"  "Lippy1" "Lippy2"  "Sumas"  "SumasLZ"   \
#        "Tiger"  "Chelan"  "Baldy"  "Saddle"  \
#	"Ebey"  \
#	"Woodside"  "Bridal"  "RUCcompare"  \
#	"Iron"  "Sauk" \
#	"Dog" \
#	"Penthouse"  "Arlington"  \
#	"Mazama"  \
#	"Rampart" \
#	"Chelan" )
#
#
###;From google maps. black lz at road 48.965, -122.073  780ft asl
###;                  lippy 48.95688, -122.0499 at 2900ft     13,16    
###;                  top   48.970273, -122.029095 at 4600 ft
###;(0)     wlon = -122.03 which gives locX = 36
###;(0)     wlat = 48.7 which gives locY = 16
###;                 black just north of north ridge 48.9834,-122.024717
###; BJLZ south -- north fork road  48.8535,-122.1435
###; BJLZN north fork beer shrine 48.889446, -122.149462
#lats=( 48.57  48.77  48.88945  48.9657  48.98 48.9834  48.61  48.86  48.8535  \
#       48.77  48.932 48.95688 48.7 48.921146 48.92 \
#       47.48  47.81  46.86  46.7 \
#       48.22 \
#       49.247  49.171  49.14  \
#       48.48  48.52 \
#       46.489 \
#       48.333  48.57 \
#       48.59 \
#       47.42 \
#       47.81 )
###;           1       2        3          4        5         6         7         8          9         10        11        12       13         14       15        16         17       18        19  
#lons=( -122.43  -122.4  -122.149462  -122.07  -121.88 -122.0424717  -122.40  -122.13 -122.1435 \
#       -122.27  -122.118 -122.0499 -122.03  -122.228737  -122.29 \
#       -121.94  -120.03  -120.35  -119.7 \
#       -122.76  \
#       -121.89  -121.748  -121.99 \
#       -121.94  -121.6 \
#       -122.173 \
#       -121.713  -122.15 \
#       -120.40 \
#       -121.41 \
#       -120.03 )
#
#grids=( "w2"  "w2"  "w2"  "w2"  "w2"  "w2"  "w2" "w2" "w2" \
#          "w2"  "w2"  "w2" "w2"  "w2"  "w2" \
#          "w2"  "d2"  "d2"  "d2"  \
#	  "w2"  \
#	  "w2"  "w2"  "w2" \
#	  "w2"  "w2" \
#	  "d2"  \
#	  "w2"  "w2" \
#	  "d2"  \
#	  "d2" \
#	  "d2" )
#
#dgrids=( "d2"  "d2"  "d2"  "d2"  "d2"  "d2"  "d2" "d2" "d2" \
#          "d2"  "d2"  "d2" "d2"  "d2"  "d2" \
#          "d2"  "d2"  "d2"  "d2"  \
#	  "d2"  \
#	  "d2"  "d2"  "d2" \
#	  "d2"  "d2" \
#	  "d2"  \
#	  "d2"  "d2" \
#	  "d2"  \
#	  "d2" \
#	  "d2" )

source "/home/tj/drjack/RASP/UTIL/sites_sh"

length=${#grids[@]}
echo $length" sites"
i=0;

if [[ $2 == "d2" ]]                   # remember the spaces...
 then
 echo "Asked for $2"
while [[ "$i" -lt "$length"  ]]
 do
 grids[$i]=${dgrids[$i]}
 
echo  "$i -- ${grids[$i]}"
  (( i += 1 )) ;
done
else 
while [[ "$i" -lt "$length"  ]]
 do
echo  "$i -- ${grids[$i]}"
  (( i += 1 )) ;
done

fi






#regions=( "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Blanchard" "Blanchard" \
#           "TIGER" "PNW" "PNW" "PNW" \
#	   "EBEY" \
#	   "WOOD" "WOOD" "WOOD" \
#	   "SKAG" "SKAG" \
#	   "PNW"\
#	   "SKAG" "SKAG"\
#           "PNW" \
#	   "TIGER" )
#
#sites=( "BlanchardLZ"  "StewartLZ" "N_Nooksack_Valley" "BlackLZ"  "Black" "Blanchard" "BJ" "Stewart" "Red" \
#        "Tiger" "Chelan" "Baldy" "Saddle" \
#	"Ebey" \
#	"Woodside" "Bridal" "RUCcompare" \
#	"Iron" "Sauk"\
#	"Dog" \
#	"Penthouse" "Arlington" \
#	"Mazama" \
#	"Rampart" )
##         1      2        3        4        5       6       7      8        9       10       11      12     13     14      15       16
#lats=( "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" "49.14" \
#       "48.48"  "48.52"\
#       "46.489"\
#       "48.333" "48.57"\
#       "48.59" \
#       "47.42" )
##           1       2        3          4        5         6         7         8          9         10        11        12       13         14       15        16         17       18        19  
#lons=( "-122.43" "-122.4" "-122.14" "-122.07" "-121.88" "-122.40" "-122.13" "-122.27" "-122.118" \
#       "-121.94" "-120.03" "-120.35" "-119.7"\
#       "-122.76" \
#       "-121.89" "-121.748" "-121.99"\
#       "-121.94" "-121.6"\
#       "-122.173"\
#       "-121.713" "-122.15"\
#       "-120.40"\
#       "-121.41" )
#
#
##sites=( "BlanchardLZ"  "StewartLZ" "N_Nooksack_Valley" "BlackLZ"  "Black" "Blanchard" "BJ" "Stewart" "Red" "Tiger" "Chelan" "Baldy" "Saddle" "Ebey" "Woodside" "Bridal" "Iron" "Dog" "Penthouse" "Mazama" "Rampart" )
##         1      2        3        4        5       6       7      8        9       10       11      12     13     14      15       16
##lats=( "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  
#lons=( "-122.43" "-122.4" "-122.14" "-122.07" "-121.88" "-122.40" "-122.13" "-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" )
#grids=( "w2" "w2" "w2" "w2" "w2" "w2" "w2" "w2" "w2" "w2" "d2" "d2" "d2" "w2" "w2" "w2" "w2" "d2" "w2" "d2" "d2" )
## The ones that are central Blanchard are w2 but the others are d2 whether PNW or Blanchard
#grids=( "w2" "w2" "w2" "w2" "w2" "w2" "w2" "w2" "w2" \
#        "w2" "d2" "d2" "d2" \
#	"w2" \
#	"w2" "w2" "w2"\
#	"w2" "w2"\
#	"d2" \
#	"w2" "w2"\
#	"d2" \
#	"d2" )




#grids=("w2" "w2" "w2" "w2" "w2" "d2" "d2" "d2")
#49.171°N  	 121.748°W
#to find the contents of an array element, use curly bracket notation, that is, ${variable[xx]}.
# echo  ${arrayname[@]}           # List array
# echo "Length of first element in array = ${#array[0]}"
# Number of elements in array = ${#array[*]}"
#Blanchard N_Nooksack_Valley  w2 0 48.83 -122.14 1 
## in keeping with the idea that the elevation is more imprtant than the exact lat lon, these points could be used.
# 
# stewart         ./extract.blipspot.PL Blanchard Stewart w2 0 48.77 -122.27 1
# black           ./extract.blipspot.PL Blanchard Black w2 0 48.98 -121.88 1 an further east is off the chart
# 




length=${#sites[@]}
i=0;

while [[ "$i" -lt "$length"  ]]
 do
 
 if [[ "$doregions" == "all" ]] 
  then  region=${regions[$i]}
  echo $region" is in the region because all was indicated"
  workdone=1
 else
  if [[ "$doregions" == "${regions[$i]}" ]]
   then
   region=${regions[$i]}
   echo $region" is the region because it is passed to the program as $1"
   workdone=1
   else
   echo "skipping on ahead"; 
   (( i += 1 )) ;
   continue; 
 fi
fi

 echo "$region was assigned to region -- working "  

  site=${sites[$i]}
  lat=${lats[$i]}
  lon=${lons[$i]}                                                                                                                          
  grid=${grids[$i]}

  outfile="./blipspots/blipspot"$site".html"
# rm $outfile

echo $region $site $lat $lon $grid $outfile

#exit

echo " "
#echo "command is  /home/tj/drjack/RASP/UTIL/extract.blipspot.PL $region $site $grid 0 $lat $lon 0" 
##|sed -e '/^/=' -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<td>#' -e 's#<td>$#</tr>#g'|sed -e 's#\(^[0-9][0-9]*\)#@\1#' |tr \"\n\" \" \" |tr \"@\" \"\\n\"  > outtemp "

## this works better on the 1 option where there are labels and formatting

test="/home/tj/drjack/RASP/HTML/"$region"/FCST/sfcwinddir.curr.1200lst.*.data"

result=$(ls $test)
returncode=$?
echo $returncode
echo $result
##    /home/tj/drjack/RASP/HTML/Blanchard/FCST/sfcwinddir.curr.1700lst.w2.data
#exit
if [[ $returncode -eq 0 ]] || [[ ! -z $result ]]
#if [ -e "/home/tj/drjack/RASP/HTML/$region/FCST/sfcwinddir.curr.1700lst.*.data" ]
then
 echo "  /home/tj/drjack/RASP/UTIL/extract.blipspot.PL $region $site $grid 0 $lat $lon 1 "
 /home/tj/drjack/RASP/UTIL/extract.blipspot.PL $region $site $grid 0 $lat $lon 1 >tempout 
 grep BLIPSPOT tempout |sed -e 's#\(BLIPSPOT for \)\([0-9]*.*--\)#\1<b><big>\2<\/big><\/b>#g' >header
 cat tempout |grep -v '\-\-\-\-\-\-\-\-\-\-\-\-\-' \
 |tr -s " " " " |sed -e 's#\([a-zA-Z\.]\) #\1_#g'|sed  -e 's#lst_#lst #g' -e 's#IME_#IME #g' \
 -e 's#h_F#h F#' -e 's#_\([0-9]\)# \1#g'   -e 's#_-# -#g' -e 's#^ ##' -e 's#_$##g'  -e '# #</td><td class="bl">#g' \
 -e 's#$#</td></tr>#'  -e 's# #</td><td class="bl">#g' -e 's#^#<tr><td class="bl">#g' \
 -e '/^.*Sfc.*W/s#<tr>#<tr class="ehilite">#'  >outtemp

##  -e '/^.*Sfc.*W/s#<tr>#<tr class="ehilite">#'
# rm tempout
 workdone=1;
 echo ""
 echo "workdone= " $workdone 
 echo ""
#/home/tj/drjack/RASP/UTIL/extract.blipspot.PL $region $site $grid 0 $lat $lon 1  |sed -e '/^/=' -e 's#- ##g'  -e 's# #</td><td>#g' -e 's#^#<td>#' -e 's#<td>$#</tr>#g'|sed -e 's#\(^[0-9][0-9]*\)#@\1#' |tr "\n" " " |tr "@" "\n"   > outtemp
#echo "this is a cat of outtemp"
#cat outtemp


#then join it with a column of names and strip leading line numbers
#join blipparams outtemp |sed -e 's#^[0-9]* ##' >blipspot.tmp

 echo "<html><head><title>Blipspot for $site at $lat $lon</title><link rel=\"stylesheet\" type=\"text/css\" href=\"weather.css\"><link rel=\"SHORTCUT ICON\"  href=\"blipspot1.ico\"></head><body> "  >$outfile
 echo "<script type=\"text/javascript\" src=\"blipspots.js\"></script><hr class=\"thin\">" >>$outfile
# echo "<h2>Blipspot for $site at $lat $lon  </h2> from TJ's <a href=./>RASP forecasts</a>" >>$outfile
 echo "<h3><font color=#8800EE ><u>For $site</u></font>  Boundary Layer* Information Prediction for Soaring Potential Over Time Blipspot for $lat $lon  </h3> from TJ's <a href=./>RASP forecasts</a>" >>$outfile
 cat header   >>$outfile
 #echo "Generated on $(date +%Y__%m__%d)" >>$outfile
 echo "<table>" >>$outfile
#if [ $region != "TIGER" ]
#then
#echo "<tr><th>VALID.TIME</th><th> 0800lst</th><th>1100lst</th><th>1200lst</th><th>1300lst</th><th>1400lst</th><th>1500lst</th><th>1600lst</th><th>1700lst</th><th>1800lst</th><th> VALID.TIME</th></tr>" >>$outfile
#echo "<tr><th>ForecastPd</th><th>    9.0h</th><th>  12.0h</th><th>  13.0h</th><th>  14.0h</th><th>  15.0h</th><th>  16.0h</th><th>  17.0h</th><th>  18.0h</th><th>  19.0h</th><th> ForecastPd</th></tr>" >>$outfile
#else
#echo "<tr><th>VALID.TIME</th><th>1100lst</th><th>1200lst</th><th>1300lst</th><th>1400lst</th><th>1500lst</th><th>1600lst</th><th>1700lst</th><th>1800lst</th><th> VALID.TIME</th></tr>" >>$outfile
#echo "<tr><th>ForecastPd</th><th>  12.0h</th><th>  13.0h</th><th>  14.0h</th><th>  15.0h</th><th>  16.0h</th><th>  17.0h</th><th>  18.0h</th><th>  19.0h</th><th> ForecastPd</th></tr>" >>$outfile
#fi

#cat blipspot.tmp >>$outfile

cat outtemp |egrep -v 'BLIPSPOT' |sed -e 's#W\*#Up_Velocity fpm#g' -e 's#Hcrit#Max_soar_alt#g' -e 's#Depth#Depth_AGL#g' \
 -e 's#ForecastPd_#ForecastPd<br>\&nbsp; #'  |sed -e 's#lst#<small>PDT\&nbsp;</small>#g' \
 -e 's#_AGL_AGL#_AGL#g' -e 's#Max_soar_alt_Depth_AGL_#AGL_Lift_to#'   -e 's#Hour_#by#' -e 's#Sfc.Temp_=#When_sfc_temp= #g' -e 's#---#<small>unlikely</small>#g' \
  -e 's#td_cl#td cl#g'  -e '/^.*Up_Velocity/s#<tr>#<tr class="bhilite">#g'        >outtemp2
 
#  |sed -e 's#>TRIGGER_TIME</td><td class="bl">&</td><td class="bl">SURFACE_(2m)</td><td class="bl">TEMPERATURE# colspan=5>Thermal Trigger time & Surface (2m) Temperature that Time to get that  Depth of Lift#' >outtemp2

#collect the header lines to show what has been done.
cat header >>/home/tj/drjack/RASP/RUN/done_blips


#cat outtemp2 >>./blipspots/allblips.html
cat outtemp2 >>$outfile
#echo "<hr> " >>$outfile
if [[ $region == "EBEY" ]]
then 
echo "</table><a href=\"ebeyrasp.html\">See also blipspots from other domains for Ft. Ebey</A><hr class=\"thin\"> " >>$outfile
else
echo "</table><hr class=\"thin\">" >>$outfile
fi
echo "* The <i>Boundary Layer</i> is also known as the <i>Mixing layer</i> and its top is also called the <i>mixing height</i>.  It is the top of thermally induced lift and the level at which the <i>Thermal Index</i> goes to zero.   " >>$outfile
echo "<script type=\"text/javascript\" src=\"wxfooter.js\"> </script>" >>$outfile
echo "</body></html>" >>$outfile
#echo "<p><hr class=\"thin\"><p>" >>./blipspots/allblips.html

#grep  sed  -e 's#\(^.*   \)\([0-9][0-9]\)\([0-9][0-9]\)\( [0-9][0-9]\)\([0-9][0-9]\)\(.[0-9][0-9]\)\( [0-9][0-9]\)\([0-9][0-9]\)\(.[0-9][0-9]\)\(.*$\)#\<tr><td> \1</td><td><script type=text/javascript>document.write(vblue+direction[parseInt((\20+11.25)/22.5)])+endfont ;</script>\&deg;
rm outtemp
rm outtemp2
else

  echo "No data file for $region/FCST/sfcwinddir.curr.1800lst.*.data at  $(date +%Y_%m_%d_%H:%M)"
exit
fi

  (( i += 1 )) ;


done

#exit

#cho "</table></body></html>" >>./blipspots/allblips.html
#echo $outfile" is outfile"  
#echo $workdone" is workdone"
#if [[ $workdone >  0 ]]
#then echo "$workdone is greater than 0"
#fi   |sed -e 's#\(BLIPSPOT for \)\([0-9]*\)#\1<b><big>\2<\big><\b>#g'

if [[ $workdone -gt 0 ]]
 then
cd /home/tj/drjack/RASP/UTIL/blipspots
bfiles="{"
efiles="}"
files=$bfiles$(ls  ./*   | sed  -e 's#^.*\/##' |tr -s "'\n" "," )$efiles
/home/tj/bin/uploadw "$files"
else
echo "nothing to do"
exit
fi

mmv -a "blipspot*.html"    "$(date +%Y_%m_%d_%H)blipspots.html"
zip -m  /nt2/wx/archive/blipspots.zip  "$(date +%Y_%m_%d_%H)blipspots.html"


exit


 sed  -e 's#^.*\/##' |tr -s "'\n" "," )$efiles
/home/tj/bin/uploadw "$files"

getquickblips

else
echo "nothing to do"
exit
fi

/usr/bin/mmv -a "blipspot*.html"    "$(date +%Y_%m_%d_%H)blipspots.html"
/usr/bin/zip -m  /nt2/wx/archive/blipspots.zip  "$(date +%Y_%m_%d_%H)blipspots.html"

touch /home/tj/drjack/RASP/RUN/done_blipspots_$region
exit


