Bo Zhang (NAOC, mailto:bozhang@nao.cas.cn) will have a few lessons on python.
python to process astronomical data.These lectures are organized as below:
In [1]:
    
%pylab inline
a = np.arange(10)
print a
    
    
In [2]:
    
print type(a)
print len(a)
print size(a)
print shape(a)
print a.shape
print a.reshape((-1, 5)).shape
    
    
In [3]:
    
b = a + np.random.randn(10)
    
In [4]:
    
plt.plot(b)
    
    Out[4]:
    
In [5]:
    
print 'a+b'
print a+b
print 'a-b'
print a-b
print 'a*b'
print a*b
print 'a/b'
print a/b
print 'a^b'
print a**b
    
    
In [6]:
    
%pylab inline
np.__version__
    
    
    Out[6]:
In [7]:
    
print 1/2
print 1./2
    
    
In [8]:
    
np.argsort(np.random.randn(100,))
    
    Out[8]:
In [9]:
    
a = np.arange(100)
b = np.zeros(a.shape)
a_list = [_ for _ in a]
print a_list
    
    
In [10]:
    
print a_list*3
    
    
In [11]:
    
%%time
for i in xrange(len(a)):
    b[i] = a[i]*10
    
    
In [12]:
    
%%time
c = a*10
    
    
In [13]:
    
a = np.random.randn(5, 10)
b = np.random.randn(10, 5)
    
In [14]:
    
%%time
print np.dot(a, b)
    
    
In [15]:
    
%%time
print a*b
    
    
In [ ]:
    
    
try to use np.sin, np.cos, np.dot to build a function which can translate a sightline from Equatorial coordinates to Galactic coordinates. Usage:
l, b = func(ra=194., dec=30.5)
print l, b
Given a mask image (True/False), generate a list of pixels met requirements:
The distance from the generated pixels to at least 1 of the True pixel is within a given upper limit, say d0
Usage:
>>> def random_dots(mask_image, d_upper=20., n_dots=100):
>>>    pass
>>> mask_ = np.loadtxt('./data/mask_image/mask_image.dat')>0
>>> d_upper = 20.
>>> n_dots = 100
>>> x_rand, y_rand = random_dots(mask_image, d_upper=d_upper, n_dots=n_dots)
>>> plt.plot(x_rand, y_rand, 'rx')
In [16]:
    
# import numpy as np
len1 = 700
len2 = 900
mask_image_data = np.random.randn(len1, len2)
mesh_x, mesh_y  = meshgrid(np.arange(len2), np.arange(len1))
mask = mask_image_data>4
print '# of center pixels: ', np.sum(mask)
plt.imshow(mask, cmap='jet')
plt.plot(mesh_x[mask], mesh_y[mask], 'o', ms=30, mfc='w', markeredgecolor='w')
plt.plot(mesh_x[mask], mesh_y[mask], 'kx')
plt.xlim(0, len2)
plt.ylim(0, len1)
plt.title('scheme of valid random dots area');
    
    
    
In [17]:
    
np.savetxt('./data/mask_image/mask_image.dat', mask, fmt='%d')
    
In [ ]: