Skip to main content

Posts

Showing posts from May, 2010

Analysis of a 3 degree of freedom building in an earthquake with scipy

This is my building, I want to know how much each floor moves in an earthquake... Also it would be nice to know the acceleration experienced by each floor.




First off we need some data. I have an earthquake data file containing ground acceleration data from the Kobe earthquake of 1995. It is a matlab file, so the data will have to be extracted into a numpy format. Scipy has an io module which contains a matlab submodule. Since we want to visualize this data somehow, the pylab package will also be used.

Firstly I'll make a small helper function that enforces complex symmetry, useful for after the frequency domain analysis:

fromscipy.ioimportmatlabasmiofrompylabimportplot,show,ylabel,xlabel,title,figure,legend,annotateimportnumpyasnp
defenforce_complex_sym(array,nyquist):'''Enforce complex symmetry'''array[:,nyquist+1:]=np.conj(array[:,nyquist-1:0:-1])Next I'll write two plotting functions, that will annotate the maximum of some time indexed data:

defanalyse_…

Python and Gmail with IMAP

Today I had to automatically access my Gmail inbox from Python. I needed the ability to get an unread email count, the subjects of those unread emails and then download them. I found a Gmail.py library on sourceforge, but it actually opened the normal gmail webpage and site scraped the info. I wanted something much faster, luckily gmail can now be accessed with both pop and imap.

After a tiny amount of research I decided imap was the better albiet slightly more difficult protocol. Enabling imap in gmail is straight forward, it was under labs.

The address for gmail's imap server is:

imap.gmail.com:993

Python has a library module called imaplib, we will make heavy use of that to access our emails. I'm going to assume that we have already defined two globals - username and password. To connect and login to the gmail server and select the inbox we can do:

importimaplibimap_server=imaplib.IMAP4_SSL("imap.gmail.com",993)imap_server.login(username,password)imap_server.select(…