Virtual python environments give you the ability to install your own python packages in a given directory.
This allows you to test many python packages while also allowing you to have just the packages you need for a particular computation.
All of the packages available on the system will still be available when you run in your virtual environment (unless you create the virtual environment with --no-site-packages).
cd $HOME
mkdir virtpy
virtualenv virtpy
source ./virtpy/bin/activate
Now my prompt changes, to tell me I'm in a virtual python environment:
(virtpy)[jduckles@bison ~]$
When your virtual environment is activated it will make sure when you run python or pip they run from your virtualenvironment.
pandas - R-like data frames for Python and more!
PyPi - The python package index
In [333]:
    
from osgeo import gdal 
import numpy as np
import matplotlib.pyplot as plt
    
Now lets build a single VRT which has each of the by-band mosaics as layers.
gdalbuildvrt -separate stack.vrt *.vrt
In [251]:
    
vrt_stack = gdal.Open('/Users/jduckles/tmp/ETM/stack.vrt')
b1 = vrt_stack.GetRasterBand(1).ReadAsArray() / 255.0 # normalize for plotting
plt.imshow(b1)
    
    
    Out[251]:
    
In [252]:
    
b2 = vrt_stack.GetRasterBand(2).ReadAsArray() / 255.0 # normalize for plotting
plt.imshow(b2)
    
    Out[252]:
    
In [226]:
    
bands = ['/Users/jduckles/tmp/ETM/L71028035_03520060608_B10.TIF', 
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B20.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B30.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B40.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B50.TIF']
rasters = np.array([ gdal.Open(band).ReadAsArray() for band in bands ])
    
In [234]:
    
squares1 = []
for x in range(10):
    squares1.append(x**2)
print squares1
    
    
In [233]:
    
# is the same as:
squares2 = [x**2 for x in range(10)]
print squares2
    
    
In [332]:
    
# inline if's are possible too.
[x**2 for x in range(60) if x % 4 == 0]
    
    Out[332]:
In [244]:
    
pets = ['cat','dog','frog','rabbit']
stats = [ [pet.upper(),len(pet)] for pet in pets]
print stats
    
    
In [265]:
    
bands = ['/Users/jduckles/tmp/ETM/L71028035_03520060608_B10.TIF', 
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B20.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B30.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B40.TIF',
         '/Users/jduckles/tmp/ETM/L71028035_03520060608_B50.TIF']
rasters = np.array([ gdal.Open(band).ReadAsArray() for band in bands ])  # <-- See the list comprehension?
    
We now have a 3-d array with dimesions (spectral band, x, y)
In [254]:
    
rasters.shape
    
    Out[254]:
In [256]:
    
print rasters[:, 1500, 1500] # all bands for one x,y
    
    
In [259]:
    
print rasters[0:3,1500,1500] # first three bands for one location
    
    
In [266]:
    
print rasters [:, 1500:1505, 1500:1505] # all bands for a sampling box
    
    
In [290]:
    
help(np.apply_along_axis)
    
    
In [284]:
    
def total(a):
    return(np.sum(a))
np.apply_along_axis(total, 0, rasters[:,1500:1550,1500:1550])
    
    Out[284]:
In [314]:
    
print rasters[:,1500:1505,1500:1505]
def plusone(a):
    return(a + 1)
np.apply_along_axis(plusone, 0, rasters[:,1500:1505,1500:1505])
    
    
    Out[314]:
In [283]:
    
def ndvi(a):
    return( float(a[1] - a[0])/float(a[1]+a[0]))
    
ndvi = np.apply_along_axis(ndvi, 0, rasters[2:4,1500:1510,1500:1510]/255.0)
print ndvi
    
    
In [312]:
    
mydata = np.random.randn(5,1000,1000)
def mean(a):
    return(np.sum(a) / len(a))
%time np.apply_along_axis(mean,0,mydata)
    
    
    Out[312]: