#!/usr/bin/env python
from scipy import *
from pylab import *
import time
import manc

# We create class, which inherits from Rectangle, so that it can be called with an Axes                                                                             
# instance, causing the rectangle to update its shape to match the                                                                                                  
# bounds of the Axes                                                                                                                                                
class UpdatingRect(Rectangle):
    def __call__(self, ax):
        self.set_bounds(*ax.viewLim.bounds)  # resize the picture to user specified size                                                                            
        ax.figure.canvas.draw_idle()         # redraws the picture                                                                                                  

def ax_update(ax):  # actual plotting routine
    ax.set_autoscale_on(False) # Otherwise, infinite loop                                                                                                           

    #Get the range for the new area                                                                                                                                 
    xstart,ystart,xdelta,ydelta = ax.viewLim.bounds
    xend = xstart + xdelta
    yend = ystart + ydelta

    ext=[xstart,xend,ystart,yend]
    data = zeros((Ny,Nx))
    manc.mand(data, ext, Nx, Ny, max_steps)
    
    # Update the image object with our new data and extent                                                                                                          
    im = ax.images[-1]  # take the latest object                                                                                                                    
    im.set_data(transpose(data))   # update it with new data                                                                                                                   

    im.set_extent(ext)           # change the extent                                                                                                                
    ax.figure.canvas.draw_idle() # finally redraw                                                                                                                   
    
    
if __name__ == '__main__':
    Nx = 1000
    Ny = 1000
    max_steps = 50
    ext = [-2,1,-1,1]


    t0 = time.clock()
    data = zeros((Ny,Nx))
    manc.mand(data, ext, Nx, Ny, max_steps)
    print 'time=', time.clock()-t0


    fig,ax = subplots(1,1)
    ax.imshow(transpose(data), extent=ext,origin='lower')

    rect = UpdatingRect([0,0],0,0,facecolor='None', edgecolor='black') # (arguments: xy, width, height, angle=0.0, **kwargs)   
    rect.set_bounds(*ax.viewLim.bounds)
    # Connect for changing the view limits                                                                                                                          
    ax.callbacks.connect('xlim_changed', rect)
    ax.callbacks.connect('ylim_changed', rect)
    ax.callbacks.connect('xlim_changed', ax_update)
    ax.callbacks.connect('ylim_changed', ax_update)
    show()
    
    
    
