2+3 mylist = ['a', 'b', 'c', 'd'] mylist mylist[0] # 0-based index mydict = dict(a=1, b=2) mydict mydict['b'] # access element by key sum([1, 2, 3, 4]) exp(0) import numpy numpy.exp(0) for i in range(5): print i if 4 > 2: print 'no parenthesis' else: print 'nope' def myfunc(a, b=2): """ my documentation """ return a+b # functions are a particular type of variables myfunc myfunc(3) myfunc(3, b=5) class Ocean: T = 25 # ocean temperature cp = 4000 # sea water heat capacity V = 1e6 # volume indian = Ocean () indian indian.T class Ocean2: T = 25 # ocean temperature cp = 4000 # sea water heat capacity V = 1e6 # volume def energy(self): # class method, like a function defined within a class return self.cp*self.T*self.V indian = Ocean2() indian.energy() class Ocean3: T = 25 # ocean temperature cp = 4000 # sea water heat capacity V = 1e6 # volume def energy(self): # class method, like a function defined within a class return self.cp*self.T*self.V def __add__(self, other): res = Ocean3() res.T = 0.5*(self.T + other.T) res.cp = 0.5*(self.cp+other.cp) res.V = self.V+other.V return res indian = Ocean3() big = indian + indian big.energy() %pylab array a = array([2, 5, 7, 8, 3]) # define an array (same as np.array) a a[:3] # first three elements a[-3:] # last three (negative indexing) a[::2] # every second element array([1,2,3]) * 2 [1, 2, 3] * 2 arange(5) # 0...4 : five elements numbered from 0, same as r_[:5] arange(1900, 2000, 5) # alternative syntax: r_[1900:2000:5] linspace(-1, 1, 10) # 10 elements between -1 and 1 zeros(5) a = ones((2,3)) a a.ndim # number of dimensions a.shape a.size a.mean # a method, itself an object a.mean() mean(a) a = arange(2*3).reshape(2,3) a a[1,0] # multi-dimensional indexing: first index for line, second for columns a[1] # grab the whole line a[:, -1] # last column (negative integer start from the end) a.mean(axis=0) # mean over lines a.mean(axis=1) # mean over columns #%pylab %matplotlib inline x = arange(10) y = x**2 plot(x, y, label='my line') plot(x, y*0.5, label='my other line') legend(loc='upper left') xlabel('x axis') ylabel('y axis') title('My Figure') import dimarray as da data = [[1.,2,3], [4,5,6]] a = da.DimArray(data, axes=[['grl', 'ant'], [1950, 1960, 1970]], dims=['item', 'time']) a a.values a.axes ax = a.axes[1] ax.name , ax.values a.dims # grab axis names (the dimensions) a.labels # grab axis values a['grl', 1970] a.ix[0, -1] a.mean(axis='time') a['ant', 1950] = nan a a.mean(axis='time', skipna=True) import os datadir = da.get_datadir() os.chdir(datadir) ls datadir = da.get_datadir() ds = da.read_nc('cmip5.MPI-ESM-MR.nc') ds temp = ds['temp'] temp %matplotlib inline temp.plot() temp2 = da.read_nc('cmip5.IPSL-CM5A-LR.nc', 'temp') temp2 ref1 = temp[1986:2005].mean(axis='time') ref1 ano1 = temp - ref1 ano1 ano2 = temp2 - temp2[1986:2005].mean(axis='time') # same as for ano1 ano1.shape, ano2.shape difference = ano2 - ano1 difference diffvalid = difference.dropna(axis='scenario', minvalid=1) diffvalid #difference.plot() diffvalid.plot() newa = da.stack((temp, temp2), axis='model', align=True, keys=['MPI-ESM-MR', 'IPSL-CM5A-LR']) newa newa.name = 'temperature' newa.date = '22nd May 2014' newa.description = 'my dimarray test' newa._metadata newa.write_nc('/tmp/mydata.nc') ds = da.read_nc('/tmp/mydata.nc') ds ds['temperature']._metadata ds = da.read_nc('cmip5.*.nc', align=True, axis='model') ds a = ds.to_array(axis='variable') a #a['ohu',:,2100, 'rcp45'] # rename model axis def rename_axis(model_ax): return [ax.replace('data/cmip5.','') for ax in model_ax] a.model[:] = rename_axis(a.model) a change = a[:,:, 2081:2099].mean('time') - a[:,:,1986:2005].mean('time') change #rounded = change['temp'].apply(lambda x: np.round(x, 2)) # roudn to 2 decimals #rounded.to_pandas() change['temp'].to_pandas() #rounded = change['temp'].apply(lambda x: np.round(x, 2)) # roudn to 2 decimals #rounded.to_pandas() grouped = change[:,:,['rcp26','rcp85']].group('model','scenario') grouped grouped.to_pandas() grouped.ungroup() change.reshape('variable,scenario','model') lon = linspace(-179.5, 179.5, 360) lat = linspace(-89.5, 89.5, 180) LON, LAT = meshgrid(lon, lat) values = cos(radians(LON)) + cos(radians(LAT)) basic = da.DimArray(values, axes=[lat, lon], dims=['lat', 'lon']) basic.contourf(); colorbar() basic.mean() from dimarray.geo import GeoArray geo = GeoArray(basic) geo.mean() geo.axes['lat'].weights geo.axes['lat'].weights(geo.lat) geo.axes['lon'].modulo geo[:,[280.5]]