Пример #1
0
def sodom(findstr, exten=0):

    angles = {}
    file_list = glob.glob(findstr)

    for image in file_list:
        angle = pyfits.open(image)[exten].header['ANGLE']
        if angle not in angles.keys():
            angles[angle] = []

        angles[angle].append(image)

    iraf.imcombine.combine = 'average'
    iraf.imcombin.reject = 'none'

    for theta in angles.keys():
        if angles[theta][0].find('../') > -1: s = 3
        else: s = 0
        name = 'final_'+angles[theta][0][s:angles[theta][0].rfind('_')]+'.fits'
        
        iraf.imcombine(','.join([s+'['+str(exten)+']' for s in angles[theta]]),name)
        ADE.mediclean(name,name)

    return
Пример #2
0
def clean_VI(searchstr):
    """Takes FITS files generated by LabView and:
    1) Poor man's bkgrnd subtraction with ADE.mediclean
    2) Sets the Primary Hdu to 0 instead of LabView's 1

    """

    in_files = glob(searchstr)

    for image in in_files:

        HDU = pyfits.open(image)[1]
        cleaned_data = ADE.mediclean(HDU.data[220:870,290:960])
        clean_name = image.split('.fits')[0].split('/')[-1] + '.cleaned.fits'
        pyfits.PrimaryHDU(cleaned_data,HDU.header).writeto(clean_name)

    return
Пример #3
0
def fat_angel(findstr, num_ap, phi, dp, f, 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. The images
        are first cleaned using a median subtraction algorithm, which
        assumes that your S/N is very good. 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.
        phi    - Float
                 The angle between the screen normal and the detector normal
                 in RADIANS.
        dp     - Float
                 The distance, in millimeters, from the center of the screen
                 to the front glass of the camera lens.
        f      - Float
                 The nominal focal length of the camera lens in millimeters.
                 Read this number off of the lens body.
        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.
    
    Output:
        Output is a tuple of Numpy vectors that each contain the folling info:
             Field:       Description:
               0          input angle
               1          ring radius (mm)
               2          ring width (mm)
               3          inner ring radius (mm)
               4          outer ring radius (mm)
               5,6        the x and y coordinates of the ring center (pixels)

    
    Example:
        Assume you have a bunch of data called X_red.fits where X is some
        data iterator.

        >>lb.fat_angel('*_red.fits',150,0.319,1084,26,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)
    xcent = np.zeros(numfiles)
    ycent = np.zeros(numfiles)

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


        huds = pyfits.open(file_list[i])
        data = np.float32(huds[exten].data)

        'get the angle from the FITS header. MUCH easier in python than IDL!'
        angles[i] = huds[exten].header['ANGLE']

        '''get rid of the background noise with mediclean. This algorithm
        is very good if you have bodacious S/N'''
        data = ADE.mediclean(data)

        '''given the nominal focal length, f, and object distance, dp, we
        can approximate the image distance, sp, using the thin lens eq.'''
        sp = (1/float(f) - 1/float(dp))**-1

        '''find the center of the image by minimizing the reported ring width'''
        center = cent_test(data,phi,dp,sp,pixelpitch)
#        center = ADE.centroid(data)

        '''t_dist holds the transformation from the detector space to screen space'''
        t_dist = metatron(data,center,phi,dp,sp,pixelpitch)

        'Annulize!'
        if debug:
            t0 = time.time()
            (r_vec,fluxes) = ADE.annulize(data,num_ap,distances=t_dist)
            print "Annulize took "+str(time.time() - t0)+" seconds"
        else:
            (r_vec,fluxes) = ADE.annulize(data,num_ap,distances=t_dist)

        '''find_peak uses the CDF of the fluxes to find the peak
        and interpolation to find the limits of the FWHM. 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]
        
        if debug:
            '''plot a bunch of stuff'''
            fig1 = plt.figure(fignum)
            plt.clf()
            sp0 = fig1.add_subplot(212)
            sp0.plot(r_vec, 
                     np.cumsum(fluxes)/np.max(np.cumsum(fluxes)))
            sp0.axvline(x=rm,ls='--',lw=0.3)
            sp0.axvline(x=r1,ls='--',lw=0.3)
            sp0.axvline(x=r2,ls='--',lw=0.3)
            sp0.set_xlabel("Radius (mm)")
            sp0.set_ylabel("% of total counts")
            sp0.set_title("Normalized CDF")

            sp1 = fig1.add_subplot(211)
            sp1.plot(r_vec,fluxes)
            sp1.axvline(x=rm,ls='--',lw=0.3)
            sp1.axvline(x=r1,ls='--',lw=0.3)
            sp1.axvline(x=r2,ls='--',lw=0.3)
            sp1.set_xlabel("Radius (mm)")
            sp1.set_ylabel("Counts")
            sp1.set_title("Ring Profile")

            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
        xcent[i] = center[0]
        ycent[i] = center[1]

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

    if output:
        f = open(output, 'w')
        f.write('#angle       radius     width     r1        r2       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],
                      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],
            xcent[sort_idx],
            ycent[sort_idx])
Пример #4
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])
Пример #5
0
def fat_angel(name, num_ap, phi, dp, f, fsg, pixelpitch, exten, q, lock):
    '''
    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. The images
        are first cleaned using a median subtraction algorithm, which
        assumes that your S/N is very good. 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.
        phi    - Float
                 The angle between the screen normal and the detector normal
                 in RADIANS.
        dp     - Float
                 The distance, in millimeters, from the center of the screen
                 to the front glass of the camera lens.
        f      - Float
                 The nominal focal length of the camera lens in millimeters.
                 Read this number off of the lens body.
        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.
    
    Output:
        Output is a tuple of Numpy vectors that each contain the folling info:
             Field:       Description:
               0          input angle
               1          ring radius (mm)
               2          ring width (mm)
               3          inner ring radius (mm)
               4          outer ring radius (mm)
               5,6        the x and y coordinates of the ring center (pixels)

    
    Example:
        Assume you have a bunch of data called X_red.fits where X is some
        data iterator.

        >>lb.fat_angel('*_red.fits',150,0.319,1084,26,exten=1,output='my_data.dat')
    '''

    print name

    huds = pyfits.open(name)
    data = np.float32(huds[exten].data)

    'get the angle from the FITS header. MUCH easier in python than IDL!'
    angle = huds[exten].header['ANGLE']

    '''get rid of the background noise with mediclean. This algorithm
    is very good if you have bodacious S/N'''
    data = ADE.mediclean(data)

    '''given the nominal focal length, f, and object distance, dp, we
    can approximate the image distance, sp, using the thin lens eq.'''
    sp = (1/float(f) - 1/float(dp))**-1

    '''find the center of the image by minimizing the reported ring width'''
    center = cent_test(data,phi,dp,sp,pixelpitch) #slow and right
#    center = ADE.centroid(data) #fast and wrong

    '''t_dist holds the transformation from the detector space to 
    screen space'''
    t_dist = metatron(data,center,phi,dp,sp,pixelpitch)

    'Annulize!'
    (r_vec,fluxes,errors) = ADE.annulize(data,num_ap,distances=t_dist)

    '''find_peak uses the CDF of the fluxes to find the peak
    and interpolation to find the limits of the FWHM. Working with 
    the CDF allows us to assume that the annulus is gaussian-ish 
    without having to worry about the particulars'''
    (rp,r1,r2) = find_peak(r_vec,fluxes)
        
    ap = np.arctan(rp/fsg)*180/np.pi
    a1 = np.arctan(r1/fsg)*180/np.pi
    a2 = np.arctan(r2/fsg)*180/np.pi
        
    awidth = a2 - a1
    rwidth = r2 - r1
    xcent = center[0]
    ycent = center[1]

    if full_output:
        'write power profile info for use by theModule'
        if not os.path.exists(full_dir): os.makedirs(full_dir)
        angs = np.arctan(r_vec/fsg)*180/np.pi
        write_full(angs,fluxes,angle,ap)

    q.put((angle,ap,rp,awidth,rwidth,r1,r2,xcent,ycent))
    
    return