示例#1
0
def fat_angel(findstr, num_ap, EXTEN=0, OUTPUT=0,
              PIXELPITCH=1, FIBERSIZE=500):
    '''
    Description:
        Fat_angel is deisgned to process a large number of data from
        the laser bench. It takes an input string, findstr, and computes
        the width and radius of the ring in each data image. Surface brightness
        profiles are found using Annulize in the ADEUtils package with
        the number of annuli specfied in the num_ap input. Output can be
        directed to a file for plotting bliss.
    
    Inputs:
        findstr- Str
                 A string containing the names of the data files to use.
                 Wildcards are allowed so you can chose a single file or
                 as many as want. Files are assumed to be FITS files.
        num_ap - Int
                 The number of annuli to use when constructing the surface
                 brightness profile.
        EXTEN  - Int
                 The FITS extension where the primary data is stored. As of
                 right now there is no way to specify different extensions
                 for different files.
        OUTPUT - Str
                 Name of output file. Output contains angle, ring radius, and
                 ring width in column form
        PIXELPITCH - Float
                 Size of the camera pixels in micrometers. This is only used to 
                 get the correct scale on the debug plots.
    
    Output:
        Output is a tuple of Numpy vectors containing angle, ring radius, and
        ring width.
    
    Example:
        Assume you have a bunch of data called X_red.fits where X is some
        data iterator.

        >>lb.fat_angel('*_red.fits',150,EXTEN=1,OUTPUT='my_data.dat')
    '''


    file_list = glob.glob(findstr)
    numfiles = len(file_list)

    'initialize some data arrays'
    widths = np.zeros(numfiles)
    radii = np.zeros(numfiles)
    angles = np.zeros(numfiles)
    r1_vec = np.zeros(numfiles)
    r2_vec = np.zeros(numfiles)
    frd_widths = np.zeros(numfiles)
    xcent = np.zeros(numfiles)
    ycent = np.zeros(numfiles)

    t1 = time.time()
    for i in range(numfiles):
        print(file_list[i])

        ADE.mediclean(file_list[i],'clean'+file_list[i],exten=EXTEN)

        huds = pyfits.open('clean'+file_list[i])
        data = huds[EXTEN].data
        
        'get the angle from the FITS header. MUCH easier in python than IDL!'
        angles[i] = huds[EXTEN].header['ANGLE']

        '''poor man's background subtraction'''
        mode = ADE.mode(data)[0][0]
        if debug: print 'Mode = '+str( mode)

 
        'Annulize!'
        if debug:
            fig0 = plt.figure(0)
            plt.clf()
            t0 = time.time()
            (r_vec,fluxes,center) = ADE.annulize(data,num_ap,NOREAD=1)#,MODE=mode)
            print "Annulize took "+str(time.time() - t0)+" seconds"
            plt.plot(r_vec*PIXELPITCH,fluxes)
            fig0.show()
        else:
            (r_vec,fluxes,center) = ADE.annulize(data,num_ap,NOREAD=1)#,MODE=mode) 

        '''Compute the cdf from the pdf (fluxes). Working with the CDF
        allows us to assume that the annulus is gaussian-ish without 
        having to worry about the particulars'''
        (rm_idx,r1,r2) = find_peak(r_vec,fluxes)
        
        rm = r_vec[rm_idx]
 #       r1 = r_vec[r1_idx]
  #      r2 = r_vec[r2_idx]
        
        'Now deconvolve and find the width of the FRD smearing kernel'
#        (frd_width,frd) = decon(r_vec,fluxes,rm_idx,r2,r1,FIBERSIZE,PIXELPITCH)

#        global frd_sav
#        frd_sav = frd
        
        if debug:
            fig1 = plt.figure(fignum)
            plt.clf()
            sp0 = fig1.add_subplot(222)
            sp0.plot(r_vec*PIXELPITCH, 
                     np.cumsum(fluxes)/np.max(np.cumsum(fluxes)))
            sp0.axvline(x=rm*PIXELPITCH,ls='--',lw=0.3)
            sp0.axvline(x=r1*PIXELPITCH,ls='--',lw=0.3)
            sp0.axvline(x=r2*PIXELPITCH,ls='--',lw=0.3)
            sp0.set_xlabel("Radius (um)")
            sp0.set_ylabel("% of total counts")
            sp0.set_title("Normalized CDF")

#            plt.figure(0)
            sp1 = fig1.add_subplot(221)
            sp1.plot(r_vec*PIXELPITCH,fluxes)
            sp1.axvline(x=rm*PIXELPITCH,ls='--',lw=0.3)
            sp1.axvline(x=r1*PIXELPITCH,ls='--',lw=0.3)
            sp1.axvline(x=r2*PIXELPITCH,ls='--',lw=0.3)
            sp1.set_xlabel("Radius (um)")
            sp1.set_ylabel("Counts")
            sp1.set_title("Ring Profile")

#            sp2 = fig1.add_subplot(224)
#            sp2.plot(r_vec*PIXELPITCH, frd)
#            sp2.set_xlabel("Radius (um)")
#            sp2.set_ylabel("??")
#            sp2.set_title("FRD kernel")

            plt.suptitle("Angle = "+str(angles[i])+" degrees\n"+file_list[i])
            fig1.show()

            print "Center: "+str(center)

            if numfiles > 1: raw_input("press enter to continue...\n")


        widths[i] = r2 - r1
        radii[i] = rm
        r1_vec[i] = r1
        r2_vec[i] = r2
#        frd_widths[i] = frd_width
        xcent[i] = center[0]
        ycent[i] = center[1]

    
    print "Total annulize time was "+str(time.time()-t1)+" seconds"
    'We sort the data just make the output a little more readable'
    sort_idx = np.argsort(angles)

    widths *= PIXELPITCH
    radii *= PIXELPITCH
    r1_vec *= PIXELPITCH
    r2_vec *= PIXELPITCH
    frd_widths *= PIXELPITCH
    
    if OUTPUT:
        f = open(OUTPUT, 'w')
        f.write('#angle       radius     width     r1        r2          frd width        center\n')
        for i in range(angles.shape[0]):
            np.array([angles[sort_idx][i],
                      radii[sort_idx][i],
                      widths[sort_idx][i],
                      r1_vec[sort_idx][i],
                      r2_vec[sort_idx][i],
                      frd_widths[sort_idx][i],
                      xcent[sort_idx][i],
                      ycent[sort_idx][i]]).\
                      tofile(f, sep='   ',format='%3.4f')
            f.write('\n')
        
    return (angles[sort_idx],
            radii[sort_idx],
            widths[sort_idx],
            r1_vec[sort_idx],
            r2_vec[sort_idx],
            frd_widths[sort_idx],
            xcent[sort_idx],
            ycent[sort_idx])