Guest User

NOAA terminal scripts

a guest
Feb 24th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | Software | 0 0
  1. Note that there are two scripts here, one sed (GNU sed), one awk (gawk).
  2.  
  3. Both are invoked through a bash function which finds your local/preferred forecast page using DuckDuckGo's
  4. bang search, such as:
  5.  
  6. noaa ()
  7. {
  8. w3m -B -dump "https://duckduckgo.com/lite?q=\!noaa ${1:-20755}" | ~/bin/weather.sed | cat -s | ~/bin/weather.awk | less
  9. }
  10.  
  11. --------------------------------------------------------------------------------
  12.  
  13. #!/bin/sed -nEf
  14.  
  15. # NB: This relies on GNU sed, BSD sed (MacOS lacks features used, so use gsed from Homebrew on your Mac.
  16.  
  17. # General cleanup...
  18. 1,/Your local forecast office is/d
  19. # 1,/\[news-impor\]/d # Commented Tue 28 May 2019 11:36:32 PM CDT
  20. 1,/^./ {
  21. /^$/d
  22. /^./ b print
  23. }
  24.  
  25.  
  26. # Delete blocks
  27. /^Zone Area Forecast/,/^Point Forecast/ {
  28. /^Point Forecast/ { n; n; b end }
  29. /./ d
  30. }
  31.  
  32. # Delete to end.
  33. # /^Additional Forecasts and Information/,$d
  34. /^Forecast Discussion/,$d
  35.  
  36.  
  37. # Linefeed before date in extended forecast
  38. /^Detailed Forecast/,/^Additional Forecasts and Information/ {
  39.  
  40. /^Additional Forecasts and Information/ {b end}
  41.  
  42. /^Detailed Forecast/ { i \
  43.  
  44. b print
  45. }
  46.  
  47. /^(This|Today|Tonight|Overnight|Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday).*/ { i \
  48.  
  49. b print
  50. }
  51.  
  52. /^./ {
  53. s/^./ &/
  54. b print
  55. }
  56. }
  57.  
  58. # ab
  59. /^Current conditions at/ { h; n; n; H; x; s/\n/ /; p; b end }
  60.  
  61. /^Lat/,+8 {
  62. # /^Humidity/ b humidity
  63. /^Lat/ { s/(Lon|Elev)/ &/g; p; b; }
  64. /[][]/d;
  65. /^[^0-9].*[^CF]*$/ { h; n; n; H; n; n; H; x; s/\n/ /g; b print }
  66. }
  67.  
  68. : humidity
  69. /^Humidity/,/^$/ {
  70. /^./ { s/^./ &/; b print }
  71. /^%/ b print
  72. }
  73.  
  74.  
  75. /^Extended Forecast for/ { h; n; n; H; x; s/\n/ /g; b print }
  76.  
  77. /^(Last Update|Forecast Valid):/ { h; n; H; x; s/\n/ /g; b print }
  78.  
  79.  
  80. # Fix broken bullets
  81. /^ *. *$/ { h; n; H; x; s/\n/ /g; s/^( *.) {1,}/\1 /; b print }
  82.  
  83. /NOW:/ { h; n; H; x; s/\n/ /; p; b }
  84.  
  85.  
  86. : print
  87. # Capture any stuff we ought to delete.
  88. /En Espa.ol/d
  89. /Share/d
  90. /Click HERE/d
  91. /^Click here/d
  92. /^More Information:/d
  93. /^Local Forecast Office/d
  94. /[][]/d
  95. /^Forecast$/d
  96. s/Read More >//
  97. s/Similar City Names//
  98. p
  99.  
  100. : end
  101.  
  102. --------------------------------------------------------------------------------
  103.  
  104. #!/usr/bin/gawk -f
  105.  
  106. ## There are likely gawkisms here, One True Awk fans beware
  107.  
  108. # Print a horizontal bar
  109. function hbar (width) {
  110. for ( i=0; i<width; i++ ) printf( "_" )
  111. printf("\n")
  112. }
  113.  
  114. BEGIN {
  115. mode = "start"
  116. col = 80
  117. bullet_i = -1
  118.  
  119. printf( "Weather Forecast retrieved %s.\n", strftime( "%A %B %e, %Y %H:%M:%S", systime() ))
  120. hbar(col)
  121. printf("\n")
  122. }
  123.  
  124. {
  125. bullet = 0
  126. newmode = 0
  127. oldrecord = ""
  128. newrecord = ""
  129. mergeline = 0;
  130. }
  131.  
  132. # Detect bullets
  133. /^ *[^ A-Za-z0-9] / {
  134. bullet = 1
  135. }
  136.  
  137. /^Hazardous Weather Conditions/ { mode = "hazard"; newmode = 1 }
  138. /^Current conditions/ { mode = "current"; newmode = 1 }
  139. /^Extended Forecast/ { mode = "extended"; newmode = 1 }
  140. /^Detailed Forecast/ { mode = "detailed"; newmode = 1 }
  141. /^Last Update/ { mode = "update"; newmode = 1 }
  142.  
  143. {
  144. if ( mode == "hazard" ) next
  145.  
  146. # printf( "%-12s ", mode )
  147.  
  148. if ( mode == "update" && newmode == 1 ) {
  149. printf( "\n" )
  150. hbar(col)
  151. printf( "%s\n", $0 )
  152. next
  153. }
  154.  
  155. if ( mode != "extended" ) bullet_i = -1
  156.  
  157. if ( newmode == 1 ) {
  158. printf( "\n\n" )
  159. print $0
  160. hbar(col)
  161. next
  162. }
  163.  
  164. # Collapse short lines in extended mode...
  165. # ... bullets
  166. if ( bullet == 1 && mode == "extended" ) {
  167.  
  168. # Bullet lines themselves
  169.  
  170. bullet_i = 0
  171. oldline = gensub( "\\s*$", "", "g", $0 )
  172. getline
  173. newline = gensub( "^\\s*", "", "g", $0 )
  174. mergedline = gensub( "\\n", " ", "g", oldline " " newline )
  175.  
  176. if ( $0 !~ /^$/ ) printf ( "%s\n", mergedline )
  177. else printf( "%s\n\n", oldline )
  178.  
  179. next
  180. }
  181.  
  182. if ( mode == "extended" && bullet_i >= 0 && $0 !~ /^Detailed Forecast/ ) {
  183.  
  184. bullet_i++
  185.  
  186. # printf( " extended bullet " )
  187.  
  188. if ( $0 ~ /^$/ && bullet_i > 1 && length(oldline) > 1 ) {
  189. # printf( "shooting blanks: %s %s %s\n", bullet_i, length(oldline), $0 );
  190. printf( "%s\n", $0 );
  191. oldline = $0
  192. next
  193. }
  194.  
  195. if ( $0 !~ /^$/ ) {
  196. oldline = gensub( "\\s*$", "", "g", $0 )
  197. if ( length( oldline ) > 40 ) {
  198. printf( "%s\n", oldline ); next
  199. }
  200.  
  201. while ( getline ) {
  202.  
  203. # Do not overflow into detailed forecast ...
  204. if ( $0 ~ /^Detailed Forecast/ ) {
  205. mode = "detailed"
  206. bullet_i = -1
  207. newmode = 1
  208.  
  209. printf( "\n\n%s\n", $0 )
  210. hbar(col)
  211.  
  212. next
  213. }
  214.  
  215. if ( length($0) == 0 ) {
  216. printf( "%s\n\n", oldline )
  217. next
  218. }
  219.  
  220. else if ( length($0) > 0 ) {
  221. newline = gensub( "^\\s*", "", "g", $0 )
  222. mergedline = gensub( "\\n", " ", "g", oldline " " newline )
  223. oldline = mergedline
  224. continue
  225. }
  226.  
  227.  
  228. newline = gensub( "^\\s*", "", "g", $0 )
  229.  
  230. if ( length (oldline) < 40 && length(newline) > 0 ) {
  231. mergedline = gensub( "\\n", " ", "g", oldline " " newline )
  232. printf( "%s\n", mergedline )
  233. oldline = $0
  234. next
  235. }
  236. else {
  237. printf( "%s\n", oldline )
  238. printf( "%s\n", $0 )
  239. oldline = $0
  240. next
  241.  
  242. }
  243. }
  244. }
  245. }
  246. }
  247.  
  248. {printf( "%s\n", $0) }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment