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

Showing posts with label urllib2. Show all posts
Showing posts with label urllib2. Show all posts

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)


Thursday, June 11, 2015

Python and MesoWest API

Uses the MesoWest API to get ozone concentration data and plot them. An example of formatting plot datetime plot tick marks is also shown.

# Brian Blaylock
# June 9, 2015

# Uses the MesoWest API to get find the maximum ozone
# from in-situ stations during GSLSO3S

# Tips for creating API request:
# -look at MesoWest API documentation: http://mesowest.org/api/docs/
# -use JSON viewer to see request results: http://jsonviewer.stack.hu/


import json
import numpy as np
import urllib2
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import DateFormatter, YearLocator, MonthLocator, DayLocator, HourLocator

token = '1234567890' #contact mesowest@lists.utah.edu to get your own token.

station = 'mtmet'
# dateformat YYYYMMDDHHMM
start_time = '201506050000'
end_time = '201506060000'
variables = 'ozone_concentration'
time_option = 'local'
URL = 'http://api.mesowest.net/v2/stations/timeseries?stid='+station+'&start='+start_time+'&end='+end_time+'&vars='+variables+'&obtimezone='+time_option+'&token='+token

#Open URL and read the content
f = urllib2.urlopen(URL)
data = f.read()

# convert that json string into some python readable format
data = json.loads(data)

stn_name = data['STATION'][0]['NAME']
# get ozone data convert to numpy array
ozone = data["STATION"][0]["OBSERVATIONS"]["ozone_concentration_set_1"]
#convert ozone into a numpy array: setting dtype=float replaces None value with a np.nan
ozone = np.array(ozone,dtype=float)

# get date and times and convert to datetime and put into a numpy array
dates = data["STATION"][0]["OBSERVATIONS"]["date_time"]
DATES = np.array([]) # first make an empty array
for i in dates:
   if time_option=='utc':
      converted_time = datetime.strptime(i,'%Y-%m-%dT%H:%M:%SZ')
   else:
      converted_time = datetime.strptime(i,'%Y-%m-%dT%H:%M:%S-0600')
   DATES = np.append(DATES,converted_time)


# make a simple ozone time series
#----------------------------------------------------------
ax = plt.subplot(1,1,1)
plt.plot(DATES,ozone)
plt.title('Ozone Concentration (ppb) for '+stn_name)
plt.xlabel('date')
plt.xticks(rotation=30)
#Now we format the date ticks
# Format Ticks
# Find months
months = MonthLocator()
# Find days
days = DayLocator()
# Find each 0 and 12 hours
hours = HourLocator(byhour=[0,6,12,18])
# Find all hours
hours_each = HourLocator()
# Tick label format style
dateFmt = DateFormatter('%b %d\n%H:%M')
# Set the x-axis major tick marks
ax.xaxis.set_major_locator(hours)
# Set the x-axis labels
ax.xaxis.set_major_formatter(dateFmt)
# For additional, unlabeled ticks, set x-axis minor axis
ax.xaxis.set_minor_locator(hours_each)


plt.savefig(start_time+'_'+end_time+'.png', format='png')