## Reading and writing data files and images¶

For many standard scientific applications thr simple scipy/numpy functions loadtxt() and savetxt() will do the trick.

In [1]:
import scipy as sp

import urllib

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib notebook


#### Reading a white-space delimited data file

Here's a copy of test1.dat

# This line is a comment
#
#t x y
# ============
1.2 2.3 3.2
2.2 4.6 6.1
3.0 5.9 9.1

The file is in the format of a typical data file. Each line might be something like $(t,x,y)$.

Note that by default loadtxt ignores any lines in the data file that begin with the # character. (This can be changed.)

In [2]:
a = sp.loadtxt("test1.dat")
a

Out[2]:
array([[ 1.2,  2.3,  3.2],
[ 2.1,  4.6,  6.1],
[ 3. ,  5.9,  9.1]])

It's often useful to break the data into single arrays for $t$, $x$, and $y$. One way to do this with the transpose of a:

In [3]:
a.T

Out[3]:
array([[ 1.2,  2.1,  3. ],
[ 2.3,  4.6,  5.9],
[ 3.2,  6.1,  9.1]])
In [4]:
t, x, y = a.T

In [5]:
t

Out[5]:
array([ 1.2,  2.1,  3. ])

or

In [6]:
t, x, y = sp.loadtxt("test1.dat", unpack=True)


#### Reading a comma delimited, or comma separated value (CSV) data file¶

Here's a copy of test2.csv

# This line is a comment
#
#t x y
# ============
, 1, 2, 3
comment in column 0, 2, 4, 6
, 3, 9, 27

In [7]:
b = sp.loadtxt("test2.csv", delimiter = ',', usecols = (1,2,3), unpack = True)
b

Out[7]:
array([[  1.,   2.,   3.],
[  2.,   4.,   9.],
[  3.,   6.,  27.]])

#### Writing an array to a whitespace delimited file¶

In [8]:
c = 2*a
c

Out[8]:
array([[  2.4,   4.6,   6.4],
[  4.2,   9.2,  12.2],
[  6. ,  11.8,  18.2]])
In [9]:
sp.savetxt('testout1.dat', c, header='sample header (optional)' )


## Retrieve and display a JPG image¶

In [10]:
# Retrieve file from URL and create copy in local filesystem

urllib.request.urlretrieve(' http://www.atlasoftheuniverse.com/nebulae/m42.jpg','orion.jpg')

Out[10]:
('orion.jpg', <http.client.HTTPMessage at 0x7fe5d3a7d2e8>)
In [11]:
# Read in local file

plt.figure()
plt.imshow(img);


### Version Information¶

version_information is from J.R. Johansson (jrjohansson at gmail.com)
See Introduction to scientific computing with Python:
http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-0-Scientific-Computing-with-Python.ipynb

If version_information has been installed system wide (as it has been on linuxremotes), continue with next cell as written. If not, comment out top line in next cell and uncomment the second line.

In [12]:
%load_ext version_information

#%install_ext http://raw.github.com/jrjohansson/version_information/master/version_information.py

In [13]:
%version_information scipy, matplotlib, urllib

Out[13]:
SoftwareVersion
Python3.5.1 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython4.2.0
OSLinux 3.10.0 327.el7.x86_64 x86_64 with redhat 7.3 Maipo
scipy0.17.1
matplotlib1.5.1
urllibThe 'urllib' distribution was not found and is required by the application
Sat Jun 24 11:42:59 2017 EDT
In [ ]: