def datasource(lat, lon, source_dir):
    """ Return a gdal datasource for a NED 10m lat, lon corner.
    
        If it doesn't already exist locally in source_dir, grab a new one.
    """
    # FIXME for southern/western hemispheres
    fmt = 'http://ned.stamen.com/1km/n%02dw%03d.tif.gz'
    url = (abs(lat), abs(lon)) % fmt
    
    #
    # Create a local filepath
    #
    s, host, path, p, q, f = urlparse(url)
    
    local_dir = md5(url).hexdigest()[:3]
    local_dir = join(source_dir, local_dir)
    
    local_base = join(local_dir, basename(path)[:-7])
    local_path = local_base + '.tif'
    local_none = local_base + '.404'
    
    #
    # Check if the file exists locally
    #
    if exists(local_path):
        return gdal.Open(local_path, gdal.GA_ReadOnly)

    if exists(local_none):
        return None

    if not exists(local_dir):
        makedirs(local_dir)
        chmod(local_dir, 0777)
    
    assert isdir(local_dir)
    
    #
    # Grab a fresh remote copy
    #
    print >> stderr, 'Retrieving', url, 'in DEM.NED1km.datasource().'
    
    conn = HTTPConnection(host, 80)
    conn.request('GET', path)
    resp = conn.getresponse()
    
    if resp.status in range(400, 500):
        # we're probably outside the coverage area
        print >> open(local_none, 'w'), url
        return None
    
    assert resp.status == 200, (resp.status, resp.read())
    
    body = StringIO(resp.read())
    file = GzipFile(fileobj=body, mode='r')
    
    open(local_path, mode='w').write(file.read())
    
    #
    # The file better exist locally now
    #
    return gdal.Open(local_path, gdal.GA_ReadOnly)
dataset/ETHPy150Open migurski/DEM-Tools/Hillup/data/NED1km.py/datasource/swapped operands of `%`