def do_n_extended_fits(nfits, xsh, ysh, imsize,  gaussfit=False,
        maxoff=None, return_error=False, powerlaw=2.0, noise=1.0,
        unsharp_mask=False, smoothfactor=5, zeropad=0,
        shift_func=cross_correlation_shifts, sfkwargs={},
        doplot=False,
        **kwargs):

    try: 
        import progressbar
        widgets = [progressbar.FormatLabel('Processed: %(value)d offsets in %(elapsed)s)'), progressbar.Percentage()]
        progress = progressbar.ProgressBar(widgets=widgets)
    except ImportError:
        def progress(x):
            yield x

    image = make_extended(imsize, powerlaw=powerlaw)
    if zeropad > 0:
        newsize = [s+zeropad for s in image.shape]
        ylen,xlen = newsize
        xcen = xlen/2-(1-xlen%2) 
        ycen = ylen/2-(1-ylen%2) 
        newim = np.zeros(newsize)
        newim[ycen-image.shape[0]/2:ycen+image.shape[0]/2, xcen-image.shape[1]/2:xcen+image.shape[1]/2] = image
        image = newim


    if unsharp_mask:
        from AG_fft_tools import smooth
        offsets = []
        for ii in progress(xrange(nfits)):
            inim = image-smooth(image,smoothfactor)
            offim = make_offset_extended(image, xsh, ysh, noise=noise, **kwargs)
            offim -= smooth(offim,smoothfactor)
            offsets.append( shift_func( inim, offim,  return_error=return_error, **sfkwargs) )
    else:
        offsets = []
        if doplot:
            import pylab
            pylab.figure(3); pylab.subplot(221); pylab.imshow(image-image.mean()); pylab.subplot(222); pylab.imshow(offim-offim.mean())
            #subplot(223); pylab.imshow((abs(fft2(image-image.mean())*conj(fft2(offim-offim.mean())))))
            pylab.subplot(223); pylab.imshow(abs(ifft2((fft2(image)*conj(fft2(offim))))))
            pylab.subplot(224); pylab.imshow(abs(ifft2((fft2(image-image.mean())*conj(fft2(offim-offim.mean()))))))
            draw()
        for ii in progress(xrange(nfits)):
            offim = make_offset_extended(image, xsh, ysh, noise=noise, **kwargs)
            offsets.append( shift_func( 
                image,
                offim,
                return_error=return_error, **sfkwargs)
                )

    return offsets
def edge_weight(imsize, smoothsize=5, power=2):
    img = np.ones([imsize,imsize])
    smimg = smooth(img, smoothsize, ignore_edge_zeros=False)
    return smimg**power