#!/bin/bash
## Copyright 2011 TJ Olney all rights reserved
## Premission granted to operators of freely publicly accessible RASP sites to use and modify this code as long as
## this notice remains intact.
## this script reades sitedata.ncl and uses it as the basis to generate menus and lists and even static html pages
## when readsitelist.ncl is run in debug=1 mode, it prints to a file that includes lots of stray "markers" to allow useful
## parsing of the output file.
sendtoweb="/home/tj/bin/upload"
sendtowebw="/home/tj/bin/uploadw"  # could be equal to $sendtoweb
blip_util_dir="/home/tj/drjack/RASP/UTIL/"

## look at tmpnewsites to see the output 
mv tmpnewsites tmpnewsites.prev
mv dgrids 	dgrids.prev
mv sites_sh	sites_sh.prev
mv regionwithsounding.js	regionwithsounding.js.prev
#mv param_info_soundings.js	param_info_soundings.js.prev
mv param_array.js		param_array.js.prev
mv newsoundingmenuns_n2s.html   newsoundingmenuns_n2s.htm.prev
mv newwindgrams_n2s.html	newwindgrams_n2s.html.prev
mv newblipmenu_n2s.html	     newblipmenu_n2s.html.prev
mv newsoundingsitenames.html    newsoundingsitenames.html.prev
mv blipspots.html		blipspots.html.prev
mv blipspots.js		     blipspots.js.prev
mv windgrams.js		     windgrams.js.prev
mv newsitemenu.js		newsitemenu.js.prev
mv sitemenuearly.js		sitemenuearly.js.prev
mv newsoundingsitenames.js	newsoundingsitenames.js.prev
mv ren_soundings		ren_soundings.prev
mv rasp.ncl.region.data	     rasp.ncl.region.data.prev
mv pushparamsnew		     pushparamsnew.prev
mv blipspotsi.js		blipspotsi.js.prev
mv windgramsi.js                windgramsi.js.prev
mv sitedata.js 			sitedata.js.prev

#strip ncl copyright etc
ncl readsitelist.ncl debug=1 |egrep -v 'opyright|niversity|anguage|software|details|using' >tmpnewsites

# sites_sh for use by getblipspots first strip lines that won't be used then turn it into sites_sh
cat tmpnewsites|egrep -vi 'remove|Type|ize| values|Dimensions|Coordinates|Number|Fill'\
 |sed -e 's#([0-9]*)# #g' -e 's/w2/"w2"/g' -e 's/d2/"d2"/g' -e 's/Variable:\(.*$\)/)@@@\1=( /g' \
 |tr "\n" " " |tr "@\t" "\n "   |sed -e 's/domain=/regions=/g'  \
 |sed -n -e '/domains=/,/wgrid/s/ / /pg' \
 |tail -6 |sed -e '/domains=/s/-WINDOW//g' \
 |sed -e 's#\([a-zA-Z]\)\( \)#\1"#g' -e 's#\( \)\([a-zA-Z]\)#"\2#g' \
  -e 's#^"##' -e 's/wgrids/grids/' |sed -e 's/\([wd]2".*\)$/\1)/'  -e 's#\([a-zA-Z][0-9]\) #\1" #g' |tr -s " " " "\
  |sed -e 's#"for.*$#)#g' >sites_sh

# no dgrids yet so 
# get grids and make a special line in sites_sh with the right number of "d2" entires for use with early runs
tail -1 sites_sh |tr "wg" "dd" |sed -e 's/drids/@dgrids/' -e 's#)#)@#' |tr "@" "\n"  > dgrids 
cat dgrids >>sites_sh


#create  javascript that has an associative array  regionwith[]
#  regionwith['sounding10']= new Array("FT_EBEY","w2")   // Smith_Is  at  -122.843    48.318
#  regionwith['sounding11']= new Array("PNW","d2")   // Arlington  at  -122.15    48.165
# may be redundant with the advent of myalphasitelist etc.

grep regionwith tmpnewsites |sed -e 's#(0).*remove##g' >regionwithsounding.js

#for the parameter information in js.
#grep param_info tmpnewsites  |sed -e 's#(0).*remove##g' > param_info_soundings.js
#echo 
grep ns_sitelist tmpnewsites |sort -r |sed -e 's/^.* my/my/g' >my_ns_sitelist.js
grep ew_sitelist tmpnewsites |sort -r |sed -e 's/^.* my/my/g' >my_ew_sitelist.js
grep mysitelist tmpnewsites  |sed -e 's/^.* my/my/g' >mysitelist.js
grep myalphasitelist tmpnewsites |sort |sed -e 's/^.* my/my/g' >myalphasitelist.js
# for soundings info and desc are the same
#cat param_info_soundings.js |sed -e 's/param_info/param_desc/g' >param_desc_soundings.js

#param_desc are either stored in param_array_base.js or are generated on the fly
#param_array.js.base is a manually created file of  param_info[bparam]  param_desc[bparam]
# it has the param_info['soundingn'] and param_info['windgram_site'] added to it by this script
# and spacers for the RASPtable style option menus.  Need to get any new sites into the bparam list.
cat param_array.js.base |egrep -v 'indgram|regionwith|sounding[0-9]' >param_array.js

# now build up the rest of the param_array.js from data extracted from sitedata.ncl
echo " ">>param_array.js
echo "var regionwith= new Array();" >>param_array.js
cat regionwithsounding.js >>param_array.js
#echo " ">>param_array.js
#cat param_info_soundings.js >>param_array.js
echo " ">>param_array.js
#cat param_desc_soundings.js >>param_array.js
echo " ">>param_array.js
echo "var my_ns_sitelist = new Array(); ">>param_array.js
cat my_ns_sitelist.js >>param_array.js
echo " ">>param_array.js
echo "var my_ew_sitelist = new Array(); ">>param_array.js
cat my_ew_sitelist.js >>param_array.js
echo " ">>param_array.js
echo "var myalphasitelist = new Array(); ">>param_array.js
cat myalphasitelist.js >>param_array.js
echo " ">>param_array.js
echo "var mysitelist = new Array(); ">>param_array.js
cat mysitelist.js >>param_array.js
echo " ">>param_array.js
cat param_generate.js >>param_array.js;
echo " ">>param_array.js
cat menu_functions.js >>param_array.js
echo " ">>param_array.js

##grep numsites tmpnewsites |sed -e 's#(0).*numsites#numsoundings#g' >>param_array.js

echo "numsites=mysitelist.length" >> param_array.js

echo "Stopping after generating only the param_array.js"
#exit


# create several menu like html pages
egrep 'href.*sounding' tmpnewsites |sed  -e 's/([0-9]*).*remove//g' |sort -r |sed -e 's#4[6-9]\.[0-9]* ##g' >newsoundingmenuns_n2s.html

grep windgram tmpnewsites|grep -v blipa |sed -e 's#(0).*remove##g' |sort -r |sed -e 's#4[6-9]\.[0-9]* ##g' >newwindgrams_n2s.html

grep blipa tmpnewsites|grep -v soundingsitename  |sed -e 's#(0).*remove##g' |sort -r |sed -e 's#4[6-9]\.[0-9]* ##g' >newblipmenu_n2s.html

grep soundingsitename tmpnewsites |sed -e 's#(0).*soundingsitename##g' |sort -r |sed -e 's#4[6-9]\.[0-9]* ##g'> newsoundingsitenames.html

echo "<b>Blipspots: Canada:</b>" >blipspots.html 

grep blipspot tmpnewsites |sed -e 's#(0).*remove##g' |sort -r |sed -e 's#4[6-9]\.[0-9]* ##g' -e 's#a href#a class=afake_button href#g' -e 's#|##g'  -e 's#</a>#</a> | #g' \
  -e 's# Black #<b> Black </b>#g'  \
  -e 's# BJ #<b> BJ </b>#g'  \
  -e 's# Blanchard #<b> Blanchard </b>#g'  \
  -e 's# Woodside #<b> Woodside </b>#g'  \
  -e 's# Bridal #<b> Bridal </b>#g'  \
  -e 's# Tiger #<b> Tiger </b>#g'  \
  -e 's#SumasCa </a>#SumasCa </a><br><b> USA: </b>#g'  \
  -e 's# Tiger #<b> Tiger </b>#g'  \
  >>blipspots.html
echo "<a class=afake_button href=http://wxtofly.net/quickblips.html >| Quick wind only blips all sites</a>" >>blipspots.html

# create a javascript that can have make a blipspot menu on any page.
echo "var blipspotmenu=\" \\" >blipspots.js
cat blipspots.html |sed -e 's#$#\\#g' >>blipspots.js
echo "<br>\"" >> blipspots.js
echo "if (window.top==window.self) {document.write(blipspotmenu)}">> blipspots.js

#use blipspots.js as a template for a similar javascript for windgrams.

#cat blipspots.js |sed -e 's/blipspot/@/g' -e 's/.html/_windgram.png/'       -e 's#@##g' -e 's#Blipspots#Windgrams#g'       -e 's#menu#windgram_menu#g' -e 's#quickblips.html#windgrams.html#g' -e 's# Quick wind only blips all sites#| All Windgrams#g' >windgrams.js
#fixed quickblip issue
# use blipspots menu to make windgram menu
cat blipspots.js |sed -e 's#blipspotmenu#windgram_menu#g' -e 's/blipspot/@/g' -e 's/.html/_iwindgram.html/'    \
  -e 's#@##g' -e 's#Blipspots#Windgrams#g'        \
  -e 's#quickblips#windgrams#g' -e 's# Quick wind only blips all sites#| All Windgrams#g'\
  -e 's#windgrams_windgram.png#windgrams.html#g' >windgrams.js

#### create the javascript that becomes the list of soundings on the univiewer page. 
echo "var sitemenu=\" <br> \\" >newsitemenu.js
cat newblipmenu_n2s.html | tr "\n" "@" | sed -e 's#@#\\@#g' -e 's#"#\\"#g' |tr "@" "\n" >>newsitemenu.js
echo "<br>\"" >>newsitemenu.js
echo "document.write(sitemenu)" >>newsitemenu.js

echo "var sitemenunames=\" <br> \\" >newsoundingsitenames.js
cat newsoundingsitenames.html | tr "\n" "@" | sed -e 's#@#\\@#g' -e 's#"#\\"#g' |tr "@" "\n" >>newsoundingsitenames.js
echo "<br>\"" >>newsoundingsitenames.js
echo "document.write(sitemenunames)">>newsoundingsitenames.js

## create a new script to create a north south sorted list of soundings
echo "#\!/bin/bash" >ren_soundings
grep mmv tmpnewsites |sed -e 's#(0)##g' |sed -e 's#4[6-9]\.[0-9]* ##g' |sed -e 's/remove//g'|tac >>ren_soundings
chmod +x ren_soundings 

#(0)     remove FRASER-WINDOW TIGER-WINDOW ncldata sounding57@Tiger@47.48@-121.94
#(0)     remove PNW TIGER-WINDOW ncldata sounding57@Tiger@47.48@-121.94
#(0)     remove PNW-WINDOW TIGER-WINDOW ncldata sounding57@Tiger@47.48@-121.94


### create a new rasp.ncl.region.data file   this is a very kludgy 
### to adapt it for your site, you will need to carefully examine the results of
### grep ncldata tmpnewsites |uniq 
### if you have only one region, it should be simple  for more than that it becomes difficult
### sounding numbers in the rasp.ncl.region.data file must be continuous so if you want to use the same sounding number
### for a site no matter where it appears, you must have all prior numbered sites listed for that region.

## wasd hard wire alert for last sounding  some kind ov kludge should work...
## already have line numsites =  nn in tmpnewsites
#grep numsites tmpnewsites |sed -e 's/\)^.*\)\([0-9]*\)/\2/' >numsites
numsites=$(grep "numsites" tmpnewsites |sed -e 's#\(^.*\)\([0-9][0-9]\)#\2#')

echo "numsites = $numsites"

grep ncldata tmpnewsites   |uniq    | sed  -e '/FRASER-WINDOW PNW ncldata sounding10/,/FRASER-WINDOW TIGER-WINDOW/d' \
  -e "/remove PNW-WINDOW FRASER-WINDOW ncldata sounding1/,/PNW-WINDOW TIGER-WINDOW ncldata sounding$numsites/d" \
  |grep -v "ncldata ===PNW-WINDOW" |sed   -e  '/===/s/$/@american/g'  -e 's/-WINDOW//g'    -e 's#(0).*ncldata ##g'  \
  -e 's/sounding[0-9]*@/&~F37~m~F21~/g' |tr "@" "\n" | sed  -e 's/^.*LZ/~F37~m~F21~LZ/g' \
  -e 's/\(^~F37~m~F21~[A-Z].....\).*$/\1/g'  >rasp.ncl.region.data 

##fortunately, it is very easy once this is all written out to simply go in and delete the extra lines that will be obvious.
# the @ serves as a place holder to be later replaced with a line feed.


# create a new @pushparams line for rasp.run.parameters.xxx for the soundings. not fully generalized yet...
# see new method below that gets data from already created regionwithsoundings.js
#grep ncldata tmpnewsites |sed -e 's/(.*ncldata//g' -e 's/@.*$//g' \
#    -e "s#===#push@{\$PARAMETER_DOLIST\{\'#g" \
#    -e "s/-WINDOW/\'}},(\'/g" |tr " \n" "''"\
#    |sed -e "s#''p#' ); \%p#g" -e "s#''#','#g" \
#    -e "s#(',''#( '#g"      \
#    -e "s#PNW','s#PNW'}},( s#g"  \
#    -e "s#'p#p#g" |tr "%" "\n" | egrep -v "PNW[^s]*sounding25" \
#    | sed -e "/TIGER/s#49'#49' );#g" > pushparams  


#this gets fraser right.  Then another to get 
#forfraser=$(grep FRASER regionwithsounding.js |tail -1  |sed -e 's#\(^.*\)\(sounding[0-9]*\)\(.*$\)#\2#g'); 
#echo "lastfraser = "$forfraser
#forebey=$(grep EBEY regionwithsounding.js |tail -1  |sed -e 's#\(^.*\)\(sounding[0-9]*\)\(.*$\)#\2#g'); 
#echo "lastebey ="$forebey

#grep ncldata tmpnewsites |sed -e 's/(.*ncldata//g' -e 's/@.*$//g'     -e "s#===#push@{\$PARAMETER_DOLIST\{\'#g"     -e "s#-WINDOW#\'}},(\'#g" |tr " \n" "''"    |sed -e "s#''p#' ); \%p#g" -e "s#''#','#g"     -e "s#(',''#( '#g"          -e "s#PNW','s#PNW'}},( s#g"      -e "s#'p#p#g" |tr "%" "\n" 
#|sed -e "s#\(push.*FRASER[^p]*$forfraser.\)\([^;]*;\)#\1);#g" >tmppush
#cat tmppush  |sed -e  "/EBEY/s#\(push.*EBEY[^p]*$forebey.\)\([^;]*;\)#\1);#g" |grep -v ",( sou"  >pushparams
#grep ncldata tmpnewsites |sed -e 's/(.*ncldata//g' -e 's/@.*$//g'     -e "s#===#push@{\$PARAMETER_DOLIST\{\'#g" |tr " \n" "''" | sed -e "s#'push#%push#g" |tr "%" "\n" |grep -v "'PNW'" |sed -e "s/-WINDOW/\'}},(\'/g" -e "s#'''#'#g" -e "s#''#','#g" -e "s/$/ );/g"
#grep ncldata tmpnewsites |sed -e 's/(.*ncldata//g' -e 's/@.*$//g'     -e "s#===#push@{\$PARAMETER_DOLIST\{\'#g" |tr " \n" "''" | sed -e "s#'push#%push#g" |tr "%" "\n" |grep -v "'PNW'" |sed -e "s/-WINDOW/\'}},(\'/g" -e "s#'''#'#g" -e "s#''#','#g" -e "s/$/ );/g" -e "s#\(push.*FRASER[^p]*$forfraser.\)\([^;]*;\)#\1);#g" -e  "/EBEY/s#\(push.*EBEY[^p]*$forebey.\)\([^;]*;\)#\1);#g"  >pushparams

## added to makenewsitesnew from fixpushparams 

grep FRASER regionwithsounding.js | sed -e 's#^.*sound#sound#g' -e 's#]= new.*$##g' -e "s#\(s.*[0-9]\)*.#'\1'#g" |tr "\n" "," |sed -e "s/',$/' )/" -e "s#^'#push@\{\$PARAMETER_DOLIST\{'FRASER'}},('# " >pushparamsnew
echo " " >>pushparamsnew
grep FT_EBEY regionwithsounding.js | sed -e 's#^.*sound#sound#g' -e 's#]= new.*$##g' -e "s#\(s.*[0-9]\)*.#'\1'#g" |tr "\n" "," |sed -e "s/',$/' )/" -e "s#^'#push@\{\$PARAMETER_DOLIST\{'FT_EBEY'}},('# " >>pushparamsnew
echo " " >>pushparamsnew
grep PNW regionwithsounding.js | sed -e 's#^.*sound#sound#g' -e 's#]= new.*$##g' -e "s#\(s.*[0-9]\)*.#'\1'#g" |tr "\n" "," |sed -e "s/',$/' )/" -e "s#^'#push@\{\$PARAMETER_DOLIST\{'PNW'}},('# " >>pushparamsnew
echo " " >>pushparamsnew
grep TIGER regionwithsounding.js | sed -e 's#^.*sound#sound#g' -e 's#]= new.*$##g' -e "s#\(s.*[0-9]\)*.#'\1'#g" |tr "\n" "," |sed -e "s/',$/' )/" -e "s#^'#push@\{\$PARAMETER_DOLIST\{'TIGER'}},('# " >>pushparamsnew
echo " " >>pushparamsnew


#see paramnewtest for work on replacing the sounding lines in rasp.run.parameters.*
#for file in "/home/tj/drjack/RASP/RUN/rasp.run.parameters.FT_EBEY*"
#do
#old=$(egrep 'push@.*FT_EBEY.*sounding.*' $file)
#new=$(grep FT_EBEY pushparamsnew)
#echo "replace \"$old\" \"new\" -- $file "
#done

# finally  still need to insert the lines by hand into rasp.run.parameters.REGIONXYZ

cat blipspots.js |sed -e 's/html/html target=\\"blipmap\\"/' >blipspotsi.js
cat windgrams.js |sed -e 's/png/png target=\\"blipmap\\"/' >windgramsi.js

cat newsitemenu.js |sed -e 's#w2#d2#g' >sitemenuearly.js


echo "<html><head><script type=text/javascript src=param_array.js></script>\
<body>\
newsitemenu<br><script type=text/javascript src=newsitemenu.js></script>\
sitemenuearly<br><script type=text/javascript src=sitemenuearly.js></script>\
blipspots<br><script type=text/javascript src=blipspots.js></script>\
windgrams<br><script type=text/javascript src=windgrams.js></script>\
newsoundingsitenames<br><script type=text/javascript src=newsoundingsitenames.js></script>\
blipspots target iframe<br><script type=text/javascript src=blipspotsi.js></script>\
windgrams target iframe<br><script type=text/javascript src=windgramsi.js></script>\
</body></html>" >testit.html


echo "Exiting after building everything but not moving it to its future homes."
echo " Comment out the exit after this echo to move things where they will be used."

exit 0   # comment this out if you want to actually move everything to their appropriate places.

#put things where they belong

cp -u sites_sh   $blip_util_dir 

sendtoweb param_array.js
sendtowebw param_array.js
sendtowebw newsitemenu.js
sendtowebw sitemenuearly.js

sendtowebw blipspots.html

sendtoweb blipspots.js
sendtowebw blipspots.js
sendtoweb windgrams.js
sendtowebw windgrams.js
newsoundingsitenames.js
#ren_soundings

cp -u rasp.ncl.region.data  /home/tj/drjack/WRF/NCL/
cp -u pushparams /home/tj/drjack/RASP/RUN
cp -u pushparams /home/tj/drjack/RASP/RUN/runparams/


#remove the intermediate files that build param_array.js
rm my*.js
rm regionwithsounding.js

#blipspotsi.js
#windgramsi.js

# now rebuild the static sounding pages
cd soundings
#mv makenewstaticsoundsings makenewstaticsoundsings.prev
#cat newstatsoundhead >makenewstaticsoundsings 
#cat ../sites_sh >>makenewstaticsoundsings 
#cat newstatsoundfoot >> makenewstaticsoundsings 
#chmod 755 makenewstaticsoundsings 
# just source the new ../sites_sh file 
makenewstaticsoundings

## this still doesn't deal with the static RASP pages that have soundings at the bottom.
## perhaps just link them to the static sounding pages...
## created a file soundings_index.html for this purpose.
## this has been dealt with by creating the new nearly static index pages under RASP/regionxyx on the web

