def ds2array(ds, band=1):
    """
    Given a datasource and optionally a band number, 
    return the array of values
    """
    num_pix_wide, num_pix_high = ds.RasterXSize, ds.RasterYSize
    if band > ds.RasterCount:
        raise Exception('Band %s requested but raster only has %s bands' % (
            band, ds.RasterCount
        ))
    return ds.GetRasterBand(band).ReadAsArray(0, 0, num_pix_wide, num_pix_high)