numpy meetup 07/02/2013
TRANSCRIPT
Data manipulation with Numpy
Francesco Benincasa – 07/02/2013
1. Basics
● N-dimensional arrays (ndarray) creation:● From another python structure (tuple, list, …)● Numpy funcions (arange, zeros, empty, …)● From text (or csv) files (loadtxt, genfromtxt, ...)
2a. Array objects
● Indexing
>> x=np.arange(10).reshape(2,5)
>> x[1][2]=x[1,2]
>> x.repeat(2).reshape(10,2)
● Slicing
>> x[0,0:5:2]
array([0, 2, 4])
>> x[:,0]
array([0, 5])
2b. Array objects
● Masks
>> x[x>5]
array([6, 7, 8, 9])
>> print np.ma.masked_where(x<5, x)
[[-- -- -- -- –][5 6 7 8 9]]
● Ignoring extreme values
>> np.ma.masked_outside(x,1,8)
masked_array(data =
[[-- 1 2 3 4]
[5 6 7 8 --]],
mask =
[[ True False False False False]
[False False False False True]],
fill_value = 999999)
3. NetCDF files
● Pupynere: available into scipy library or stand-alone
● Read/Write● Handle
● Variables● Dimensions● Attributes
(example1)
Visualization – 4. Maps + Contours
● Read NetCDF data>> sc = f.variables['SCONC_DUST'].getValue()
● Create grid:>> x, y = meshgrid(lons, lats)
● Draw contour for each timestep:>> for i in range(sc.shape[0]):
>> m.contourf(x, y, sc[i], cmap=cm.YlOrBr)
(example 2)
References
● Numpy reference:
http://docs.scipy.org/doc/numpy/reference/
● NetCDF APIs:
https://bitbucket.org/robertodealmeida/pupynere/
http://gfesuite.noaa.gov/developer/netCDFPythonInterface.html
● Matplotlib (docs, examples …):
http://matplotlib.sourceforge.net/
Thank you!Thank you!