Python script to extract pixels (Fundamentals - Image Int. Primer Pt.2 @19:28)

Would it be possible to get the python script mentioned in the Fundamentals - Image Integration Primer Pt.2 at approx. the 19min28sec mark?

I'm interested in extracting some (noise) values from my images, to look at their distribution...

Thanks
Ivor

Comments

  • I wrote this several years ago when learning Python... so forgive the amateurish code (it still is that way...just more complex).


    #!python
    #
    #Usage:
    #  Reads the value at a particular pixel from a set of images.
    #  Generates a list of those values.
    #  Outputs a CSV file
    #  Also create a histogram graph

    import numpy as np
    from astropy.io import fits
    import matplotlib.pyplot as plt
    import glob 
    import argparse 

    def frange(start, stop, step):

        i = start
        while i < stop:
            yield i
            i += step

    def main():
         
        #Creation of the command line options and arguments
        parser = argparse.ArgumentParser()
        parser.add_argument('filefilter',help ='Use a wildcard to capture desired data files in directory. e.g. *.csv',action='store')
           
        args = parser.parse_args()
        
        #The file list constraint of the command line to identify all FIT files
        filefilter = args.filefilter
        
        FileList = []
        for file in glob.glob(filefilter):  #create a list of files from directory from command line
            FileList.append(file)   
            
        pixelstack = []    
        bins = []
        
        for i in range(len(FileList)):
            ccd_image = fits.getdata(FileList[i])
            ccd_image = ccd_image.astype(np.float64)
            x = 2165
            y = 2276
            pixelvalue = ccd_image[y,x]
            pixelstack.append(pixelvalue)
           # print(FileList[i],pixelvalue)
            
        with open('pixelvalues.csv', 'w') as f:
            header = ('Filename,PixelValue\n')
            f.write(header)
            for i in range(len(FileList)):
                row = (str(FileList[i]) + ',' + str(pixelstack[i]) + '\n')
                print(FileList[i]+' '+ str(pixelstack[i]))
                f.write(row)
        f.close()  
        
        #font formatting for the chart   
        font = {'family' : 'DejaVu Sans',
            'weight' : 'regular',
            'size'   : 15}  
        plt.rc('font', **font)
        #setup figure
        plt.title('Histogram')
        plt.xlabel('Pixel Value')
        plt.ylabel('Frequency')
        
        print(min(pixelstack),max(pixelstack))
        
        for j in frange(min(pixelstack),max(pixelstack)+.03, 5):
            bins.append(j)  
            
       
        plt.hist(pixelstack,bins)
        plt.show()    
        
        
        return()           
        
    # This is the standard boilerplate that calls the main() function.
    if __name__ == '__main__':
      main() 
  • Thanks Adam - that's really useful. I'm completely new to Python & Spyder, so it took me a while to figure out how to add 'astropy' & enter the file wildcards into the command line / modify the code for OSC fits, but it's working fine!

    Many thanks
    Ivor
Sign In or Register to comment.