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

Wednesday, May 7, 2014

Plotting Ceilometer data with imshow()

I have plotted a month of ceilometer data with matplotlib using imshow(). This figure shows backscatter at the mountain meteorology lab for January 2014.

Simple error catching

I always get stuck with errors when building scripts. Sometimes I want the program to do something even if there is an error. Here is an example of a simple way to catch errors:

try:
file = h5py.File(file.h5, 'r')
print 'Yes, this file exists'

except:
print 'Didn't work'
sys.exit()

Here I try to open the HDF5 file called 'file.h5'. If the file exists we print "Yes, this file exists."
If there is an error when the computer tries to open the file--for example, the file can not be found--then we print "Didn't work" and the python program closes.

Save matplotlib figures and save to a server

On my personal computer I have created many figures with matplotlib. I've always used  plt.show() to show the figures and plt.savefig() to save the images, but I've had a lot of trouble saving a figure onto the server. 
At work, python is installed on the server, but saving the figures has never worked. I always get the error no display name and no $DISPLAY environment variable
Turns out I was just missing a single line of code. After importing matplotlib type mpl.use('Agg'). This will fix the problem.

Example:

import matplotlib as mpl
mpl.use('Agg')   #Not sure why, but this makes saving to a server possible.
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,50,.2)
y = np.sin(x)

plt.plot(x,y)
plt.title("This works!!! I'm so excited")
plt.savefig(path to save image + image_name.png)
plt.close()

Results in:

Monday, April 28, 2014

Downloads

Where can you download Python and Python packages???

Python is distributed through Enthought Canopy.

Packages can be downloaded through Surgeforce, the package website, and I also found this website that has a lot of packages in one place: http://www.lfd.uci.edu/~gohlke/pythonlibs/

Friday, November 22, 2013

Python Skew_T

November 21 to 23, 2012 was a downslope windstorm that affected the Wasatch Front from Salt Lake City to Ogden. I helped launch several weather balloons during the event from the Bountiful Bench, right below the "B" on the mountain. Anyways, I wanted to use Python to plot the data on a Skew-T, Log-P chart. I don't claim to be the best programmer. In fact, the basics often slip my mind. After sifting through several different scripts on the internet I found this one, and modified it to make it work. There is a better way to do this, so I'll figure out how to do that someday. I'd like to put wind barbs on the side as well. Wind speeds are important to these soundings, especially when you want to study the downslope windstorms.