This is part of Python for Geosciences notes.
================
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities
set_printoptions(precision=3 , suppress= True) # this is just to make the output look better
Load data¶
I am going to use some real data as an example of array manipulations. This will be the AO index downloaded by wget through a system call (you have to be on Linux of course):
!wget www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii
This is how data in the file look like (we again use system call for head command):
!head monthly.ao.index.b50.current.ascii
Load data in to a variable:
ao = loadtxt('monthly.ao.index.b50.current.ascii')
ao
ao.shape
So it's a row-major order. Matlab and Fortran use column-major order for arrays.
type(ao)
Numpy arrays are statically typed, which allow faster operations
ao.dtype
You can't assign value of different type to element of the numpy array:
ao[0,0] = 'Year'
Slicing works similarly to Matlab:
ao[0:5,:]
One can look at the data. This is done by matplotlib module and you have to start IPython with --pylab inline option to make it work:
plot(ao[:,2])
Index slicing¶
In general it is similar to Matlab
First 12 elements of second column (months). Remember that indexing starts with 0:
ao[0:12,1]
First raw:
ao[0,:]
We can create mask, selecting all raws where values in second raw (months) equals 10 (October):
mask = (ao[:,1]==10)
Here we apply this mask and show only first 5 rowd of the array:
ao[mask][:5,:]
You don't have to create separate variable for mask, but apply it directly. Here instead of first five rows I show five last rows:
ao[ao[:,1]==10][-5:,:]
You can combine conditions. In this case we select October-December data (only first 10 elements are shown):
ao[(ao[:,1]>=10)&(ao[:,1]<=12)][0:10,:]
Basic operations¶
Create example array from first 12 values of second column and perform some basic operations:
months = ao[0:12,1]
months
months+10
months*20
months*months
Basic statistics¶
Create ao_values that will contain onlu data values:
ao_values = ao[:,2]
Simple statistics:
ao_values.min()
ao_values.max()
ao_values.mean()
ao_values.std()
ao_values.sum()
You can also use sum function:
sum(ao_values)
One can make operations on the subsets:
mean(ao[ao[:,1]==1,2]) # January monthly mean
Result will be the same if we use method on our selected data:
ao[ao[:,1]==1,2].mean()
Saving data¶
You can save your data as a text file
savetxt('ao_only_values.csv',ao[:, 2], fmt='%.4f')
Head of resulting file:
!head ao_only_values.csv
You can also save it as binary:
f=open('ao_only_values.bin', 'w')
ao[:,2].tofile(f)
f.close()
Comments !