niallygee
niallygee

Reputation: 43

store values and continue running function

I am using astropy to detect sources in an image. I am trying to write a function that will detect my sources, with an option of storing their coordinates in an array, and another option to plot the sources. This is my code:

def SourceDetect(file, SIG, FWHM, THRESH, store = False, PlotStars = False):
    image = pf.open(file)
    im = image[0].data
    mean, median, std = sigma_clipped_stats(im, sigma=SIG)
    daofind = DAOStarFinder(fwhm = FWHM, threshold = THRESH * std) 
    sources = daofind(im - median)
    positions = np.transpose((sources['xcentroid'], sources['ycentroid']))
    if(store):
        return positions
    if(PlotStars):
        apertures = CircularAperture(positions, r = 6.) 
        norm = ImageNormalize(stretch = SqrtStretch())
        plt.imshow(im, cmap = 'Greys', origin = 'lower', norm = norm, 
        interpolation = 'nearest')
        apertures.plot(color = 'blue', lw = 1.5, alpha = 1)
        for i in range(0, len(sources)):
            plt.text(sources[i]['xcentroid'], sources[i]['ycentroid'], i, color='black');

However when I run this code with both store and plot set to True only the store part of the function runs and I can't get it to plot unless I make store False. Is there a way to write this code where I will be able to have my coordinates stored and plotted?

Upvotes: 0

Views: 41

Answers (1)

furas
furas

Reputation: 143002

Simple change order - first PlotStars, next store

    if PlotStars:
        # ... code ...
    if store:
        return positions

and this will first display plot and later it exits function with value.

But if you want first get value and later plot then you should run it two times - first only with store=True and later only with PlotStars=True

Upvotes: 1

Related Questions