SHARE
TWEET

steve54301

a guest Aug 13th, 2008 190 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #
  3. # Determines the current weather conditions and short forecast by reading
  4. # data from the weather service, and converts text to speech, to be read
  5. # over an Asterisk Telephone extension or similar. See also:
  6. # http://www.qsl.net/kb9mwr/projects/wxalert/plan.html
  7. #
  8. # Revision notes: This is version 4.2-061607 (June 16, 2007)
  9. # Comments were cleaned up. The file conditions.txt is used for screen
  10. # output in both applications (spelled correctly), with a specially
  11. # modified text "festread.txt" intended for Festival to use. It has
  12. # purposely misspelled words so that Festival will pronounce them
  13. # a certain way.
  14. #
  15. # This is still beta grade, provisions for data "not available" and
  16. # "variable" in the winds description should be worked in.
  17. #
  18. # Credits: Started by kb9mwr, minor tweaks by kb9aln
  19. #
  20. # Description:
  21. # A script that grabs that current decoded (METAR style) local weather
  22. # observations.  We do some magic to make it text to speech readable.
  23. # I'm using Cepstral to convert the text to speech, optionally you can use
  24. # festival.
  25. #
  26. #[custom-wxcond]
  27. #exten => s,1,Answer
  28. #exten => s,2,System(/usr/local/bin/get-conditions)
  29. #exten => s,3,Wait(2)
  30. #exten => s,4,MP3Play(/tmp/conditions.mp3)
  31. #exten => s,5,hangup
  32. #
  33. ##########
  34. # Clean-up
  35. ##########
  36. /bin/rm -f /tmp/metar.txt > /dev/null 2>&1
  37. /bin/rm -f /tmp/forecast.txt > /dev/null 2>&1
  38. /bin/rm -f /tmp/conditions.* > /dev/null 2>&1
  39. /bin/rm -f /var/lib/asterisk/sounds/conditions.gsm > /dev/null 2>&1
  40. #
  41. ###############
  42. # Get the METAR
  43. ###############
  44. #lynx -dump http://weather.noaa.gov/pub/data/observations/metar/decoded/KGRB.TXT > /tmp/metar.txt
  45. wget http://weather.noaa.gov/pub/data/observations/metar/decoded/KGRB.TXT -O /tmp/metar.txt > /dev/null 2>&1
  46. wget http://weather.noaa.gov/pub/data/forecasts/city/wi/green_bay.txt -O /tmp/forecast.txt > /dev/null 2>&1
  47. #
  48. ###################
  49. # Extract variables
  50. ###################
  51. TEMP1=`grep "Temperature" /tmp/metar.txt`
  52. TEMP=`echo $TEMP1 | cut -c13-15`
  53. SKY1=`grep "Sky conditions" /tmp/metar.txt`
  54. SKY=`echo $SKY1 | cut -c17-33`
  55. #
  56. # This determines if there is no wind at all ie. Calm. If it is, it sets a
  57. # flag. Cleaned up on 6/16/07
  58. #
  59. CALM1=`grep "Wind" /tmp/metar.txt`
  60. CALM2=`echo $CALM1 | cut -d " " -f2`
  61. #
  62.     if [ $CALM2 = "Calm:0" ];
  63.         then CALMFLAG=1;
  64.         else CALMFLAG=0;
  65.     fi    
  66. #
  67. #
  68.     if [ $CALMFLAG = 0 ];
  69.         then WIND1=`grep "Wind" /tmp/metar.txt`; WDIR1=`echo $WIND1 | cut -d " " -f4`;
  70.     fi
  71. #
  72. #
  73. # Had to set WDIR1 to something even if there is no wind, otherwise
  74. # bash complains that there is no unary operator.
  75. #
  76.     if [ $CALMFLAG = 1 ];
  77.         then WDIR1="0"
  78.     fi
  79. #
  80. # Replacement of directions with non-abbreviated words.
  81. # This was updated 6/13/07 to include ENE, WNW, WSW and ESE.
  82. #
  83.     if [ $CALMFLAG = 1 ];
  84.         then WDIR=" "
  85.     fi    
  86. #
  87. #
  88.     if [ $WDIR1 = "SSE" ];
  89.         then WDIR="South South-East"
  90.     fi
  91. #
  92. #
  93.     if [ $WDIR1 = "ESE" ];
  94.         then WDIR="East South-East"
  95.     fi
  96. #
  97. #
  98.     if [ $WDIR1 = "SSW" ];
  99.         then WDIR="South South-West"
  100.     fi
  101. #
  102. #
  103.     if [ $WDIR1 = "WSW" ];
  104.         then WDIR="West South-West"
  105.     fi
  106. #
  107. #
  108.     if [ $WDIR1 = "NNE" ];
  109.         then WDIR="North North-East"
  110.     fi
  111. #
  112. #
  113.     if [ $WDIR1 = "ENE" ];
  114.         then WDIR="East-North-East"
  115.     fi
  116. #
  117. #
  118.     if [ $WDIR1 = "NNW" ];
  119.         then WDIR="North North-West"
  120.     fi
  121. #
  122. #
  123.     if [ $WDIR1 = "WNW" ];
  124.         then WDIR="West North-West"
  125.     fi
  126. #
  127. #
  128.     if [ $WDIR1 = "NE" ];
  129.         then WDIR="North-East"
  130.     fi
  131. #
  132. #
  133.     if [ $WDIR1 = "NW" ];
  134.         then WDIR="North-West"
  135.     fi
  136. #
  137. #
  138.     if [ $WDIR1 = "SE" ];
  139.         then WDIR="South-East"
  140.     fi
  141. #
  142. #
  143.     if [ $WDIR1 = "SW" ];
  144.         then WDIR="South-West"
  145.     fi
  146. #
  147. #
  148.     if [ $WDIR1 = "N" ];
  149.         then WDIR="North"
  150.     fi
  151. #
  152. #
  153.     if [ $WDIR1 = "S" ];
  154.         then WDIR="South"
  155.     fi
  156. #
  157. #
  158.     if [ $WDIR1 = "E" ];
  159.         then WDIR="East"
  160.     fi
  161. #
  162. #
  163.     if [ $WDIR1 = "W" ];
  164.         then WDIR="West"
  165.     fi
  166. #
  167. #
  168. # Again we use the calm flag to test and make a decision. If the
  169. # flag is 1, it sets a null speed value. This is just to keep the
  170. # variable values filled with something in the script.
  171. #
  172.     if [ $CALMFLAG = 1 ];
  173.         then WSPEED=" ";
  174.         else WSPEED=`echo $WIND1 | cut -d " " -f8`;
  175.     fi
  176. ##
  177. #
  178. #################
  179. # Generate Output
  180. #################
  181. # This section processes the Screen output for the terminal.
  182. # originally put it in for debugging..
  183. #
  184. echo "The Current temperature is $TEMP degrees." >> /tmp/conditions.txt
  185. echo "Skies are $SKY." >> /tmp/conditions.txt
  186. #
  187. # Here is the conditional test and wind characteristics output to the
  188. # wxdisplay.txt file for the terminal output.
  189. #
  190.     if [ $CALMFLAG = 1 ];
  191.         then echo "The Wind is Calm." >> /tmp/conditions.txt;
  192.         else echo "The Wind is from the $WDIR at $WSPEED miles per hour." >> /tmp/conditions.txt;
  193.     fi
  194. echo "Forecast for Green Bay is as follows." >> /tmp/conditions.txt
  195. tail -5 /tmp/forecast.txt | while read line; do
  196. echo "$line." | cut -c2-80 >> /tmp/conditions.txt
  197. done
  198. #
  199. # Now for visual output. This just dumps the conditions.txt
  200. # file to the console or a terminal window. This file is also read by
  201. # Cepstra. Festival reads a different file.
  202. #
  203. cat /tmp/conditions.txt
  204. #
  205. # Here is the output that appears in the file that Festival reads:
  206. #
  207. echo "Here is your weather report, Sir." >> /tmp/festread.txt
  208. echo "The Current temperature is $TEMP degrees." >> /tmp/festread.txt
  209. echo "Skies are $SKY." >> /tmp/festread.txt
  210.     if [ $CALMFLAG = 1 ];
  211.         then echo "The Winde is Calm." >> /tmp/festread.txt;
  212.         else echo "The Winde is from the $WDIR at $WSPEED miles per 'hour." >> /tmp/festread.txt;
  213.     fi
  214. #
  215. #############################
  216. # Convert to wav & misc stuff
  217. #############################
  218. /opt/swift/bin/swift -f /tmp/conditions.txt -o /tmp/conditions.wav
  219. /usr/local/bin/lame --silent --scale 2.5 --abr 32 -m m -h /tmp/conditions.wav /tmp/conditions.mp3
  220. #text2wave conditions.txt -o conditions.wav
  221. #/usr/local/share/festival/bin/festival --tts /tmp/festread.txt
  222. #
  223. #sox /tmp/conditions.wav -c1 -r8000 -w /var/lib/asterisk/sounds/conditions.gsm
  224. #chown "asterisk":"asterisk" /var/lib/asterisk/sounds/conditions.gsm
  225. #chmod 755 /var/lib/asterisk/sounds/conditions.gsm
  226. #
  227. #
  228. #
  229. exit 0
  230. #
  231. # Exit with no errors
  232. #
RAW Paste Data
Top