def get_fiber_flux(file,fwhm=20,threshold=100):

    image,header = qi.readimage(file,plot=False)
    mean, median, std = sigma_clipped_stats(image, sigma=3.0)

    source = daofind(image - median, fwhm=fwhm, threshold=threshold*std)
    xcen = np.int(np.round(source['xcentroid'][0]))
    ycen = np.int(np.round(source['ycentroid'][0]))

    plt.plot([xcen],[ycen],'rx',markersize=20)

    params = fitgaussian(image)
    
    y,x = params[1],params[2]

    apdict = tp.optimal_aperture(x,y,image,[150,160])
    return apdict
import thacherphot as tp
import numpy as np
import matplotlib.pyplot as plt
from length import length
import quick_image as qi
import glob
from fitgaussian import *
from astropy.stats import sigma_clipped_stats

path = '/Users/ONeill/Dropbox/Observatory/Data/minerva/FiberTests/'
files = glob.glob(path+"lamp2*fit")

image,header = qi.readimage(files[0],plot=True,siglo=2,sighi=2)

params = fitgaussian(image)

#using params make model: amplitude, x center, y center, x width, y width, angle rotated
#using thacherphot to overplot contours of gaussian
#for every x y pixel have data value and model value, so subtract from each other and find residuals - should just look like noise

fit = gaussian(*params)
level = (np.max(image) - np.median(image))*np.array([0.95,0.5,0.1])
plt.figure(1)
plt.contour(fit(*indices(image.shape)),level,colors='blue')

def find_flux(image):
    model = fit(*indices(image.shape))
    qi.display_image(model)

    resid = model - image