def getCentroids(fnames):
    '''True
    From a list of filenames, load the filenames, clean up the images, find stars
    in the images, and return a list of centroids and the number of stars found in
    each image.
    '''
    
    n = len(fnames)
    centroids = []
    numstars = []
    for count,fname in enumerate(fnames):
        # Display status:
        print 'Loading and centroiding filename ' + str(count+1) + ' of ' + str(n) + '.'
        
        # Load the image:
        image = imgutil.loadimg(fname,from_database=True)
        
        # Clean up image, if it hasn't been already:
        if not fname.find('_norm.tif'):
            image = flatfield.ImgNormalize(image, Method="mean")
        
        # Find stars in image:
        #centers = centroid.findstars(image)
        # Nighttime:
        centers = centroid.findstars(image,zreject=4, zthresh=3.2, zpeakthresh=5, min_pix_per_star=6, max_pix_per_star=60, oblongness=2,debug=False)
        # Daytime:        
        #centers = centroid.findstars(image,zreject=3, zthresh=3.0, zpeakthresh=4, min_pix_per_star=6, max_pix_per_star=60, oblongness=2,debug=False)
        
        # Get centroids:
        centroids.append(centroid.imgcentroid(image,centers))
        
        # store number of stars centroided per frame
        numstars.append(len(centroids[count]))
        
    return centroids,numstars
print 'Opening: ' + fname
image = imgutil.loadimg(fname, from_database=True)

tic = time.clock()
# Clean-up the image:
#image = flatfield.NormalizeColumnGains(image,Plot=0,Wiener=0)
image = flatfield.ImgNormalize(image, Method="mean")
toc = time.clock()

# Find star centroids:
centroids = []
(centers) = centroid.findstars(image,
                               zreject=3,
                               zthresh=3.0,
                               zpeakthresh=4,
                               min_pix_per_star=6,
                               max_pix_per_star=60,
                               oblongness=2,
                               debug=True)
centroids = centroid.imgcentroid(image, centers)

# Display image with stars circled:
vf = 3
if centroids:
    imgutil.circstars(image, centroids, 20, viewfactor=vf)
else:
    print 'Oopsies... we found ZERO centroids. Now how do you feel?'
    imgutil.dispimg(image, viewfactor=vf)

print 'Total time: ', toc - tic, ' s'
image = imgutil.loadimg('/home/sticky/Daystar/day1.dat')
#image = imgutil.loadimg('/home/sticky/Daystar/img_1348355543_717188_00015_00000_0.dat', 
#                        load_full=True)
#image = imgutil.loadimg('/home/sticky/Daystar/img_1348370070_656182_00172_00000_1.dat')


# Apply flatfield
img1 = flat.ImgNormalize(image, Method="mean", source="image")
#img1 = flat.ImgNormalize(img1, Method="mean", source="image")
img2 = sm.colmeansub(image)

# Find stars
centers = centroid.findstars(img1, zreject=3, zthresh=3.0, zpeakthresh=4, min_pix_per_star=6, max_pix_per_star=60, oblongness=5,debug=False)


C = centroid.imgcentroid(image,centers)



#img3 = sm.colmeansub(img2)
#img4 = sm.colmeansub(image2)

#img3 = np.delete(img3, np.r_[1080:1080+32], 0)

#imgutil.dispimg(image,1)
#imgutil.dispimg(img1,1)
#imgutil.dispimg(img2,6)

#imgutil.dispimg(sm.bgsub(img1), 6)
#imgutil.dispimg(sm.colmeansub(img1), 6)
print 'saving plots of centroid methods on same star'
db = database.Connect()
name = db.select('select raw_fn from rawdata where burst_num = 172 limit 1'
                 ).raw_fn.tolist()[0]
img = imgutil.loadimg(name, from_database=True)
# find good star
centers = centroid.findstars(img)
print centers
goodstar = []
goodstar.append(centers[10])
#goodstar = list((tuple(centers[10])) # good centroid at about 1417,275

(x, y) = goodstar[0][0]
(w, h) = goodstar[0][1]

centroid_iwc = centroid.imgcentroid(img, goodstar, method="iwc")
centroid_cog = centroid.imgcentroid(img, goodstar, method="cog")
centroid_gauss = centroid.imgcentroid(img, goodstar, method="gauss")

(frame, (xframe, yframe), frame_centroid) = sm.windowsub(img, (x, y), (w, h),
                                                         neg=True,
                                                         scale=1)

cents = []
cents.append((centroid_iwc[0][0] - xframe, centroid_iwc[0][1] - yframe))
cents.append((centroid_cog[0][0] - xframe, centroid_cog[0][1] - yframe))
cents.append((centroid_gauss[0][0] - xframe, centroid_gauss[0][1] - yframe))
print cents

pl.figure()
pl.gray()
# ----------------- Plot Different Centroid Methods on Same Star-------------------------------
print 'saving plots of centroid methods on same star'
db = database.Connect()
name = db.select('select raw_fn from rawdata where burst_num = 172 limit 1').raw_fn.tolist()[0]
img = imgutil.loadimg(name,from_database=True)
# find good star
centers = centroid.findstars(img)
print centers
goodstar = []
goodstar.append(centers[10])
#goodstar = list((tuple(centers[10])) # good centroid at about 1417,275

(x,y) = goodstar[0][0]
(w,h) = goodstar[0][1]

centroid_iwc   = centroid.imgcentroid(img, goodstar, method="iwc")
centroid_cog   = centroid.imgcentroid(img, goodstar, method="cog")
centroid_gauss = centroid.imgcentroid(img, goodstar, method="gauss")
        
(frame, (xframe,yframe), frame_centroid) = sm.windowsub(img, (x,y), (w,h), neg=True, scale=1)

cents = []
cents.append((centroid_iwc[0][0]-xframe,centroid_iwc[0][1]-yframe)) 
cents.append((centroid_cog[0][0]-xframe,centroid_cog[0][1]-yframe)) 
cents.append((centroid_gauss[0][0]-xframe,centroid_gauss[0][1]-yframe)) 
print cents

pl.figure()
pl.gray()
pl.imshow(frame, cmap=None, norm=None, aspect=None,
            interpolation='nearest', vmin=0, vmax=2048, origin='upper')