def pad(x, sz, offset = 0, value = 0.):
    """
    Pads a n-dimensional array 'x' to size 'sz' with a real number specified by 'value'.
    With 'offset' an additional offset can be specified.

    Input:  x         n-dimensional numpy array              
            sz        size of padded  output                 
            offset    optional offset (0 by default)         
            value     value of padding (0 by default)        

    Output: z         padded n-dimensional array of size sz

    Copyright (C) 2010 Michael Hirsch 
    """
    from numpy import array, ones

    offset = offset * ones(x.ndim)
    sx = array(x.shape)
    sz = array(sz)
    z  = value * ones(sz)
    slices    = [slice(offset[i],offset[i]+sx[i]) for i in range(len(sz))]
    z[slices] = x
    
    return z

def crop(x, sz, offset=0):
    """
    Crops a n-dimensional array 'x' to size 'sz'.
    With 'offset' an additional offset can be specified.

    Input:  x         n-dimensional numpy array              
            sz        size of padded  output, sz > shape(x)!                
            offset    optional offset (0 by default)         

    Output: z         padded n-dimensional array of size sz

    Copyright (C) 2010 Michael Hirsch 
    """
    from numpy import ones, array

    sz = array(sz)
    offset = offset * ones(x.ndim)  
    slices = [slice(offset[i],offset[i]+sz[i]) for i in range(len(sz))]
    z = x[slices]
    
    return z

def circshift(X, T):

    from numpy import array, concatenate
     
    t = T % array(X.shape)
    
    X = concatenate((X[-t[0]:], X[:-t[0]]),0)
    X = concatenate((X[:,-t[1]:], X[:,:-t[1]]),1)
    
    return X       


   
