This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Monday, July 6, 2015

Plotting Sounding Data from University of Wyoming's Website

Plotting sounding data can be difficult, but it is made easy with this python module: https://pypi.python.org/pypi/SkewT

It requires a .txt file with data in the format of the University of Wyoming sounding data archive (example) or you can create your own dictionary.

Here I grab the sounding data from the University of Wyoming's website and process it with the following steps:

  1. Use urllib2 to open the url and read the data
  2. Parse out the html tags using BeautifulSoup
  3. Separate the text by a new line "\n"
  4. Write each line of text to a new .txt file reinserting the new line with +"\n" and skipping the first three lines of the file (this puts it in the same format as the University of Wyoming website)
  5. Draw the Skew-T Plot (NOTE: requires the SkewT package here)
Here is the download function on github
As always, please share other solutions you may come up with :)

# Brian Blaylock
# July 6, 2015

# Download, process, and Plot Sounding Data from University of Wyoming

import urllib2
from bs4 import BeautifulSoup
from skewt import SkewT

stn = '72572' #72572 is ID for SLC
year= '2015'
month = '06'
day = '12'
hour = '12' #either 12 or 00

# 1)
# Wyoming URL to download Sounding from
url = 'http://weather.uwyo.edu/cgi-bin/sounding?region=naconf&TYPE=TEXT%3ALIST&YEAR='+year+'&MONTH='+month+'&FROM='+day+hour+'&TO='+day+hour+'&STNM='+stn
content = urllib2.urlopen(url).read()

# 2)
# Remove the html tags
soup = BeautifulSoup(content)
data_text = soup.get_text()

# 3)
# Split the content by new line.
splitted = data_text.split("\n",data_text.count("\n"))

# 4)
# Write this splitted text to a .txt document
Sounding_filename = str(stn)+'.'+str(year)+str(month)+str(day)+str(hour)+'.txt'
f = open(Sounding_filename,'w')
for line in splitted[4:]:
    f.write(line+'\n')
f.close()

# 5)
sounding = SkewT.Sounding(filename=Sounding_filename)
sounding.plot_skewt()


And if you wish to plot more than one sounding on the same plot do this...
S = SkewT.Sounding(filename=Sounding_filename)
T = SkewT.Sounding(filename="72572.2015061212.txt")
S.make_skewt_axes(tmax=55)
S.add_profile(color='r',linewidth=5,bloc=0)
S.soundingdata=T.soundingdata
S. add_profile(color="b",bloc=1)