In [1]:
from notebook.services.config import ConfigManager
cm = ConfigManager()
cm.update('livereveal', {
'theme': 'simple',
'transition': 'convex',
'start_slideshow_at': 'selected'
})
Out[1]:
Author: Barron H. Henderson
Attendees will learn how to leverage Python to interact with air pollution-related model and observational data. Air research and application relies on big data. In addition to the challenge presented by data size, researchers must understand a multitude of formats and meta-data standards. For example, CMAQ, CAMx, and GEOS-Chem all use different formats and different meta-data conventions. This tutorial provides format-independent and convention-independent tools.
In [2]:
# Prepare my slides
%pylab inline
%cd working
There will be hands-on exercises for a range of GEOS-Chem analysis tools, including plotting maps with overlays, converting bpch to netcdf, editing bpch/netcdf files, and evaluating GEOS-Chem against AQSD or ICARTT campaign files.
In [3]:
# ipython
!curl -kLO http://github.com/barronh/GCandPython/archive/master.zip
import zipfile
zf = zipfile.ZipFile('master.zip')
zf.extractall()
%mv GCandPython-master GCandPython
$ python /path/to/file.pyIn[1] %run /path/to/file.py*Windows replace $ with C:\>
In [4]:
def foo(bar):
return 'foo.' + bar
In [5]:
print(foo('bar'))
In [6]:
try:
print(foo(1))
except Exception as e:
print(e)
In [ ]:
int, float:
str:
*A method is a bound function...
In [7]:
1 + 1
Out[7]:
In [8]:
1 + 1.
Out[8]:
In [9]:
1 / 2.
Out[9]:
In [10]:
1 // 2
Out[10]:
In [11]:
"""
""".encode()
Out[11]:
In [12]:
"a b c".split()
Out[12]:
In [13]:
''.join(['h', 'ow', ' ', 'c', 'an', ' ', 'I ' 'su', 'bse', 't?'])
Out[13]:
In [14]:
teststr = 'this is not fun.'
print(teststr[:8])
lists : [item1, item2, item3, ...]
dict : {key1: value1, key2: value2, ...}
enumerate is a special function that returns the element number (0-based) and the element as a tuple
In [15]:
def foo(pieces):
out = type(pieces[0])()
for i, piece in enumerate(pieces):
out += piece
return out
In [16]:
a = foo('12345')
In [17]:
b = foo([1, 2, 3, 4, 5])
In [18]:
c = foo({0: 1, 1: 2, 2: 3, 3: 4, 4: 5})
In [19]:
print(a, b, c)
In [ ]:
In [20]:
%pylab tk
np.random.seed(50)
ozone = (np.random.normal(size = 2*3*4*5) + 40).reshape(2,3,4,5)
In [21]:
print(ozone.ndim)
In [22]:
print(ozone.shape, ozone[0, :, 2, 3].shape, ozone[0, :, 2, 3])
In [23]:
print(ozone.mean())
In [24]:
print(np.percentile(ozone, [5, 95]))
In [25]:
averagekernel = np.array([[0.5, 0.35, 0.15],
[0.25, 0.5, 0.25],
[0.1, 0.4, 0.5]])
averagekernelm = np.matrix(averagekernel)
In [26]:
print(averagekernel.shape)
print(averagekernelm.shape)
print(averagekernelm.T.shape)
In [27]:
averagekernel = np.array([0.25, 0.5, 0.25])
averagekernelm = np.matrix(averagekernel)
In [28]:
(ozone[0, :, 2, 3] * averagekernel).sum()
Out[28]:
ozone[0, :, 2, 3] * averagekernelm
In [29]:
ozone[0, :, 2, 3] * averagekernelm.T
Out[29]:
In [30]:
out = np.zeros_like(ozone[:, 0])
for t in range(ozone.shape[0]):
for j in range(ozone.shape[2]):
for i in range(ozone.shape[3]):
out[t, j, i] = ozone[t, :, j, i] * averagekernelm.T
out
Out[30]:
In [31]:
from scipy import constants
?constants
In [32]:
from scipy import stats
?stats
In [33]:
from scipy.stats import mstats
?mstats
In [34]:
%%timeit -n 20
from scipy.stats import ttest_ind, mannwhitneyu
mdifferent = []
tdifferent = []
n = 100
for i in range(n):
a = np.exp(np.random.normal(size = 20))
b = np.exp(np.random.normal(size = 20))
tresult = ttest_ind(a, b)
mresult = mannwhitneyu(a, b)
if tresult.pvalue < 0.05:
tdifferent.append(i);
if mresult.pvalue < 0.05:
mdifferent.append(i);
print(len(mdifferent)/n, end = '/')
print(len(tdifferent)/n, end = ', ')
In [35]:
np.array([0.06, 0.06, 0.01, 0.04, 0.05, 0.02, 0.06, 0.07, 0.02, 0.07, 0.04, 0.05, 0.03, 0.08, 0.04, 0.03, 0.03, 0.02, 0.04, 0.01, 0.06, 0.04, 0.05, 0.01, 0.03, 0.04, 0.02, 0.04, 0.03, 0.02, 0.04, 0.03, 0.06, 0.03, 0.02, 0.07, 0.05, 0.02, 0.01, 0.05, 0.03, 0.04, 0.04, 0.03, 0.03, 0.04, 0.04, 0.06, 0.03, 0.05, 0.05, 0.03, 0.02, 0.0,\
0.04, 0.05, 0.02, 0.07, 0.05, 0.05]).mean()
Out[35]:
In [ ]: