First, we need to grab a .mat file from the server:
import shutil
import mimetypes
import os
import urllib2
import urlparse
def download_file_locally(url, dest):
req, filename, content_type = download_file(url)
if dest.endswith('/'):
dest = os.path.join(dest, filename)
with open(dest, 'wb') as f:
shutil.copyfileobj(req, f)
req.close()
def filename_from_url(url):
return os.path.basename(urlparse.urlsplit(url)[2])
def download_file(url):
"""Create an urllib2 request and return the request plus some useful info"""
name = filename_from_url(url)
r = urllib2.urlopen(urllib2.Request(url))
info = r.info()
if 'Content-Disposition' in info:
# If the response has Content-Disposition, we take filename from it
name = info['Content-Disposition'].split('filename=')[1]
if name[0] == '"' or name[0] == "'":
name = name[1:-1]
elif r.geturl() != url:
# if we were redirected, take the filename from the final url
name = filename_from_url(r.geturl())
content_type = None
if 'Content-Type' in info:
content_type = info['Content-Type'].split(';')[0]
# Try to guess missing info
if not name and not content_type:
name = 'unknown'
elif not name:
name = 'unknown' + mimetypes.guess_extension(content_type) or ''
elif not content_type:
content_type = mimetypes.guess_type(name)[0]
return r, name, content_type
url = "ftp://anonymous@ftp.mrc-lmb.cam.ac.uk/pub/tjucikas/wormdatabase/results-12-06-08/Laura%20Grundy/unc-8/n491n1192/MT2611/on_food/XX/30m_wait/L/tracker_2/2010-03-19___09_14_57/unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat"
download_file_locally(url, "./")
Then, we need to open the file up. Since this is a Matlab file from version greater than 7.3, we need to turn to the h5py library to open these .mat files without using Matlab.
Let's confirm we have the right tools:
import h5py
f = h5py.File('unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat')
print f
<HDF5 file "unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat" (mode r+)>
Check!
import h5py
f = h5py.File('unc-8%20(rev)%20on%20food%20R_2010_03_19__09_14_57___2___2_features.mat')
print list(f)
[u'#refs#', u'info', u'worm']