Example #1
0
File: extract.py Project: gmace/plp
def test4(band, lname='ohlines'):
    
    if band == 'H':
        ap_num = 23
    else: 
        ap_num = 20
    
    cwv, cflx = ip.read_lines(lname+'.dat')
         
    for k in range(ap_num):
        
        strdesc = 'IGRINS_%s_%s.%03d' % (band, lname, k)
        strip, hdr = ip.readfits(MANUAL_PATH+strdesc+'.fits')
        z1, z2 = ip.zscale(strip)   
        f2 = plt.figure(2,figsize=(12,5),dpi=200)
        a1 = f2.add_subplot(211, aspect='auto')
        
        ny, nx = strip.shape
        a1.imshow(strip, cmap='hot', aspect=nx/1000.0, vmin=z1, vmax=z2)
        a1.set_xlim((0,nx))
        a1.set_ylim((0,ny))
        a1.set_title('%s' % (strdesc,))
        
        f2.savefig(PNG_PATH+strdesc+'.png')
        
        strdesc = 'tIGRINS_%s_%s.%03d' % (band, lname, k)
        strip, hdr = ip.readfits(MANUAL_PATH+strdesc+'.fits')
        wave = np.loadtxt(MANUAL_PATH+strdesc+'.wave')
        draw_strips(strip, wave, desc=strdesc, linedata=[cwv, cflx])
Example #2
0
def test4(band, lname='ohlines'):

    if band == 'H':
        ap_num = 23
    else:
        ap_num = 20

    cwv, cflx = ip.read_lines(lname + '.dat')

    for k in range(ap_num):

        strdesc = 'IGRINS_%s_%s.%03d' % (band, lname, k)
        strip, hdr = ip.readfits(MANUAL_PATH + strdesc + '.fits')
        z1, z2 = ip.zscale(strip)
        f2 = plt.figure(2, figsize=(12, 5), dpi=200)
        a1 = f2.add_subplot(211, aspect='auto')

        ny, nx = strip.shape
        a1.imshow(strip, cmap='hot', aspect=nx / 1000.0, vmin=z1, vmax=z2)
        a1.set_xlim((0, nx))
        a1.set_ylim((0, ny))
        a1.set_title('%s' % (strdesc, ))

        f2.savefig(PNG_PATH + strdesc + '.png')

        strdesc = 'tIGRINS_%s_%s.%03d' % (band, lname, k)
        strip, hdr = ip.readfits(MANUAL_PATH + strdesc + '.fits')
        wave = np.loadtxt(MANUAL_PATH + strdesc + '.wave')
        draw_strips(strip, wave, desc=strdesc, linedata=[cwv, cflx])
Example #3
0
def test2(band, lname='ohlines'):
    '''
    Draw the original image   
    '''
    timg, thdr = ip.readfits(IMAGE_PATH + 'IGRINS_%s_%s.fits' % (band, lname))
    z1, z2 = ip.zscale(timg, contrast=0.2)

    f1 = plt.figure(1, figsize=(12, 12), dpi=200)
    a1 = f1.add_subplot(111, aspect='equal')
    a1.imshow(timg, cmap='hot', vmin=z1, vmax=z2)
    a1.set_xlim((0, 2048))
    a1.set_ylim((0, 2048))
    a1.set_title('IGRINS %s band - %s (simulated)' % (band, lname))
    a1.set_xlabel('X [pixel]')
    a1.set_ylabel('Y [pixel]')
    f1.savefig(PNG_PATH + 'IGRINS_%s_%s.png' % (band, lname))
    plt.close('all')
Example #4
0
File: extract.py Project: gmace/plp
def test2(band, lname='ohlines'): 
          
    '''
    Draw the original image   
    '''
    timg, thdr = ip.readfits(IMAGE_PATH+'IGRINS_%s_%s.fits' % (band,lname))
    z1, z2 = ip.zscale(timg, contrast=0.2)
    
    f1 = plt.figure(1,figsize=(12,12),dpi=200)
    a1 = f1.add_subplot(111, aspect='equal')
    a1.imshow(timg, cmap='hot', vmin=z1, vmax=z2)
    a1.set_xlim((0,2048))
    a1.set_ylim((0,2048))
    a1.set_title('IGRINS %s band - %s (simulated)' % (band,lname))
    a1.set_xlabel('X [pixel]')
    a1.set_ylabel('Y [pixel]')
    f1.savefig(PNG_PATH+'IGRINS_%s_%s.png' % (band,lname))
    plt.close('all')
Example #5
0
File: extract.py Project: gmace/plp
def draw_strips(strip, wave, desc='', linedata=[], lampname='', grayplot=False, \
                target_path=PNG_PATH):
    '''
    Draw the strips with wavelength 
     - INPUTS:
      1. strip data 
      2. wavelength data
      3. line data 
     - OUTPUTS:
      PNG files      
    '''
    #print 'draw_strips', lampname
    if len(linedata) > 0: 
        cwv, cflx = linedata
    wmin, wmax = (np.min(wave), np.max(wave))
    
    z1, z2 = ip.zscale(strip)
    
    f2 = plt.figure(2,figsize=(12,5),dpi=200)
    a1 = f2.add_subplot(211, aspect='auto')
    a2 = f2.add_subplot(212, aspect='auto')
    
    ny, nx = strip.shape
    cuty = ny/2-8
    a1.imshow(strip, cmap='hot', aspect=nx/1000.0, vmin=z1, vmax=z2)
    a1.plot([0,nx],[cuty,cuty], 'g--', alpha=0.8, linewidth=3)
    a1.set_xlim((0,nx))
    a1.set_ylim((0,ny))
    a1.set_title('%s' % (desc,))
    
    a2.plot(wave, strip[cuty,:], 'b-', linewidth=1)
    ww = np.where((cwv < wmax) & (cwv > wmin))[0]
    fmax = np.zeros([len(ww)]) + np.max(strip[cuty,:])*1.05
    a2.plot(cwv[ww], fmax, 'r|', markersize=10, markeredgewidth=2, alpha=0.7 )
    if lampname != '': lampname = '('+lampname+')'
    a2.set_title('%s %s' % (desc,lampname))
    a2.set_xlabel('Wavelength [um]')
    a2.set_xlim((wmin, wmax))
    a2.set_ylim((0, fmax[0]*1.1))
#    f2.savefig(target_path+'%s_prof.png' % (desc,)) #2013-11-21 cksim commented this
    f2.savefig(target_path+'%s.png' % (desc,)) #2013-11-21 cksim
    
    if grayplot == True:
        f3 = plt.figure(3, figsize=(12,5),dpi=200)
        a3 = f3.add_subplot(111)
        
        a3.imshow(strip, cmap='gray', aspect=nx/200.0, vmin=z1, vmax=z2)
        x, y = (np.arange(nx), np.sum(strip, axis=0))
        a3.plot(x, (y-np.min(y))/(np.max(y)-np.min(y))*ny*0.9,\
                'b-', linewidth=3, alpha=0.7)
        x, y = ( (cwv[ww]-wmin)/(wmax-wmin)*(nx-1), np.zeros(len(ww))+ny*1.05) 
        a3.plot(x, y, 'r|', markersize=10, markeredgewidth=2, alpha=0.7)
        a3.set_xlim((0,nx))
        a3.set_ylim((0,ny*1.1))
        cticks = wticks[np.where((wticks > wmin) & (wticks < wmax))]
        xticks = (cticks-wmin)/(wmax-wmin)*(nx-1)
        #print xticks
        a3.set_yticks([])
        a3.set_xticks(xticks)
        a3.set_xticklabels(['%.3f' % x for x in cticks])
        a3.set_xlabel('Wavelength [um]')
        f3.savefig(target_path+'%s_gray.png' % (desc,))
    
    plt.close('all')    
Example #6
0
File: extract.py Project: gmace/plp
def line_identify(stripfile, linedata=[], outputfile=None, \
                  npoints=30, spix=5, dpix=5, thres=15000):
    '''
    Identify the lines in the strip based on the line database
    - inputs : 
     1. stripfile (with wavelength data or not) 
     2. linefile (for line database) 
    '''
    
    def key_press(event): 
        
        ax = event.inaxes 
        if ax == None: return 
        ax_title = ax.get_title()
        
        if event.key == 'q': plt.close('all')

        if event.key == 'm':
            click_x, click_y = event.xdata, event.ydata
            rr = np.arange((x2-2*dpix),(x2+2*dpix+1), dtype=np.int)
            #p = ip.gauss_fit(rr, row[rr], p0=[1, x2, 1])
            #a2.plot(rr, ip.gauss(rr, *p), 'r--')
            #mx = p[1]
            mx, mval = ip.find_features(rr, row[rr], gpix=dpix)
            mx, mval = mx[0], mval[0]
            a2.plot(mx, mval, 'ro')
            fig.canvas.draw()
            fwv = cwv[np.argmin(np.abs(cwv-wav[mx]))]
            strtmp = raw_input('input wavelength at (%d, %d) = %.7f:' % (mx, my, fwv))
            if strtmp == '': strtmp = fwv
            try:
                mwv = np.float(strtmp)
                lxx.append(mx)
                lyy.append(my)
                lwv.append(mwv)
                a1.plot(mx, my, 'ro')
                fig.canvas.draw()
                print '%.7f at (%d, %d)' % (mwv, mx, my)
            except:
                print 'No input, again...'
                                   
    
    if len(linedata) == 2: 
        cwv, cflx  = linedata
    else:
        print 'No input data for line information'
        cwv, cflx = None, None 
        
    strip, hdr = ip.readfits(stripfile)
    fpath, fname = ip.split_path(stripfile)
    if outputfile == None: outputfile = fpath+'c'+fname
    # extract the file name only without extension
    name = '.'.join(fname.split('.')[:-1])

    ny, nx = strip.shape
    yy, xx = np.indices(strip.shape)
    
    if 'WV-DIM' in hdr.keys(): 
        xdim, ydim = np.array(hdr.get('WV-DIM').split(','), dtype=np.int)
        wl_coeff = np.zeros([xdim*ydim]) 
        for i in range(xdim):
            tmp = hdr.get('WV-X%03d' % (i,))
            wl_coeff[(i*ydim):(i*ydim+ydim)] = np.array(tmp.split(','), dtype=np.double)
        awave = ip.polyval2d(xx, yy, wl_coeff, deg=[xdim-1, ydim-1])
    else:
        print 'No wavelength data in FITS header'
        awave = None 
        
    
    # define figure object 
    fig = plt.figure(figsize=(14,7))
    a1 = fig.add_subplot(211, title='strip')
    a2 = fig.add_subplot(212, title='line')
    
    # draw the strip image
    z1, z2 = ip.zscale(strip)         
    a1.imshow(strip, cmap='gray',vmin=z1, vmax=z2, aspect='auto')
    a1.set_xlim(0,nx)
    a1.set_ylim(-10,ny+10)
            
    lspec = np.sum(strip, axis=0)
    lxpos = np.arange(nx)
    a2.plot(lxpos, lspec)
    a2.set_xlim(0,nx)
    # draw the lines in the database  
    if (awave != None) & (cwv != None):
        twave = awave[int(ny/2),:]
        print twave
        wv1, wv2 = np.min(twave), np.max(twave)
        for wv0 in cwv[(cwv > wv1) & (cwv < wv2)]:
            x0 = np.argmin(np.abs(twave-wv0))
            a2.plot([x0, x0], [0, ny], 'r--', linewidth=1, alpha=0.4)
  
    # make the array for emission feature points 
    lxx, lyy, lwave = ([], [], [])
    fig.canvas.mpl_connect('key_press_event', key_press)
    print '[m] mark the line and input the wavelength'
    print '[q] quit from this aperture'
    
    plt.show()
                
    return 
Example #7
0
File: manual.py Project: gmace/plp
def _line_identify_old(stripfile, linefile=None, \
                  npoints=30, spix=4, dpix=5, thres=13000, fdeg=5, desc=''):

    def key_press(event): 
        
        if event.key == 'q': plt.close('all')
        
        if event.key == 'a':
            line_idx = np.argmin( (event.xdata - ysignal)**2 ) 
            line_x = ysignal[line_idx]
        
            print line_idx, line_x
            # mark the points for fitting 
            ll, = np.where((xx < line_x+dpix) & (xx > line_x-dpix))
            print len(ll)
            l1 = ax.plot([line_x, line_x], [0,ny], 'r-', linewidth=5, alpha=0.5)
            l2 = ax.plot(xx[ll], yy[ll], 'ro')
            coeff = np.polynomial.polynomial.polyfit(xx[ll],yy[ll],fdeg)
            fig.canvas.draw() 
        
            print coeff 
            ycoeff[line_idx,:] = np.array(coeff)
            # redraw for the marked points 
        
            # read the wavelength for this line 
            input = raw_input('input wavelength:')
            try:
                line_wv = np.float(input)
            except: 
                line_wv = 0.0 
                
            print line_idx, line_x, line_wv
            ywave[line_idx] = line_wv
            
    strip, hdr = ip.readfits(stripfile)
    
    ny, nx = strip.shape
    z1, z2 = ip.zscale(strip)
    
    # fitting the base level 
    snr = 10
    ysum = np.average(strip[(ny/2-spix):(ny/2+spix),:], axis=0)
    ll = np.arange(nx)
    nrejt = 0
    while (1):
        yc = np.polynomial.polynomial.polyfit(ll, ysum[ll], fdeg)
        yfit = np.polynomial.polynomial.polyval(np.arange(nx), yc)
        ll, = np.where(ysum < yfit*(1.0 + 1.0/snr))
        #print nrejt 
        if nrejt == len(ll): break 
        nrejt = len(ll)

    ysignal = np.array(peak_find(ysum, thres=thres, dpix=dpix ))
      
    # define figure object 
    fig = plt.figure(figsize=(14,4))
    ax = fig.add_subplot(111)
    
    # draw the strip image         
    ax.imshow(strip, cmap='gray',vmin=z1, vmax=z2, aspect='auto')
    ax.set_xlim(0,nx)
    ax.set_ylim(-5,ny+5)
    # mark the number of lines 
    for i, ypeak in enumerate(ysignal): 
        if i % 2 == 1:
            ytext = ny
        else:
            ytext = -5
        ax.text(ypeak, ytext, i+1, fontsize=15)
        
    # find the emission feature in each row 
    ysteps = np.linspace(spix, ny-spix, npoints)
    # make the array for emission feature points 
    xx, yy = ([], [])
    for ypos in ysteps:
        irow = np.mean(strip[(ypos-spix/2):(ypos+spix/2),:],axis=0)
        #a2.plot(irow)
        # find the peak positions 
        xpos_list = peak_find(irow, dpix=dpix, thres=thres)
        # draw and save the peak positions (x, y)  
        for xpos in xpos_list:
            ax.plot(xpos, ypos, 'go', markersize=5, alpha=0.5)
            xx.append(xpos)
            yy.append(ypos)
    # convert into the numpy array         
    xx = np.array(xx, dtype=np.float)
    yy = np.array(yy, dtype=np.float)
    
    # generate the wavelength array for each line 
    ywave = np.zeros(len(ysignal))
    ycoeff = np.zeros([len(ysignal),fdeg+1])
    fig.canvas.mpl_connect('key_press_event', key_press)
    print '[a] add line and input wavelength'
    print '[r] reset'
    print '[q] quit from this aperture'
    plt.show()

           
    print 'thanks'    
Example #8
0
File: manual.py Project: gmace/plp
def ap_tracing_strip(band, filename, npoints=40, spix=10, dpix=20, thres=14000, \
                start_col=None, ap_thres=None, degree=None, \
                devide=8.0, target_path=MANUAL_PATH):
    ''' 
    Trace the apertures starting at "start_col" position (for FLAT spectrum)
     - inputs
        0. band 
        1. npoints : slicing number along the column direction
        2. spix : summing pixel size along vertical column for finding strips
        3. dpix : detecting pixel range of feature to be regarded as the same  
        4. thres : detecting intensity level for emission features 
        5. start_col : starting column number for tracing 
        6. ap_thres : threshold of pixel range to identify nearby aperture features
        7. degree : degree of polynomial fitting      
     - outputs
        1. coefficients of polynomial fittings 
           for start position of each aperture on y-axis   
        2. coefficients of polynomial fittings 
           for end position of each aperture on y-axis
    '''
    img, hdr = ip.readfits(filename)
    img, hdr = ip.readfits(filename)
    fpath, fname = ip.split_path(filename)
    if ip.exist_path(target_path) == False: target_path = fpath
    name = '.'.join(fname.split('.')[:-1])
    # image size 
    ny, nx = img.shape  
    
    # define the threshold pixel range to be matched with nearby aperture positions 
    if ap_thres == None: ap_thres= nx/npoints/4
    # define the degree of polynomials for fitting 
    if degree == None: degree = 3 #7
    
    # find the apertures in the middle column
    #if start_col == None: start_col = start_col #1500 #nx/2
    icol = np.mean(img[:,(start_col-spix/2):(start_col+spix/2)], axis=1)
    #print 'icol', icol, len(icol)
    mcaps1, mcaps2 = strip_find(band, icol)
    n_ap = len(mcaps1)

    # make set of (x, y) positions for each aperture
    # add the (x, y) positions at default column 
    apx1, apx2 = [], []
    apy1, apy2 = [], []
    for mcap1, mcap2 in zip(mcaps1, mcaps2):
        apx1.append([start_col]) 
        apx2.append([start_col])
        apy1.append([mcap1])
        apy2.append([mcap2])
    
    # define the tracing direction (left, right) 
    xsteps = np.linspace(spix,nx-spix,npoints)
    xlefts = xsteps[(xsteps < start_col)]
    xrights = xsteps[(xsteps > start_col)]
    
    # the left side columns 
    pcaps1, pcaps2 = np.copy(mcaps1), np.copy(mcaps2)
    for xpos in reversed(xlefts):
        icol = np.mean(img[:,(xpos-spix/2):(xpos+spix/2)],axis=1)
        caps1, caps2 = np.array(strip_find(band, icol, dpix=dpix, margin=10))
        #print xpos, n_ap, len(caps1), len(caps2)
        # check the aperture number based on mcap positions 
        for i, pcap1, pcap2 in zip(range(n_ap), pcaps1, pcaps2): 
            tcap1 = caps1[(caps1 < pcap1+ap_thres) & ( caps1 > pcap1-ap_thres)]
            if len(tcap1) == 1: 
                # save this position to the (x, y) position list
                apx1[i].append(xpos)
                apy1[i].append(tcap1[0])
                # save this position for the next tracing loop 
                pcaps1[i] = tcap1[0] 
            elif len(tcap1) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap1))

            tcap2 = caps2[(caps2 < pcap2+ap_thres) & ( caps2 > pcap2-ap_thres)]
            if len(tcap2) == 1: 
                # save this position to the (x, y) position list
                apx2[i].append(xpos)
                apy2[i].append(tcap2[0])
                # save this position for the next tracing loop 
                pcaps2[i] = tcap2[0] 
            elif len(tcap2) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap2))

    
    # the right side columns 
    pcaps1, pcaps2 = np.copy(mcaps1), np.copy(mcaps2)                      
    for xpos in xrights:
        icol = np.mean(img[:,(xpos-spix/2):(xpos+spix/2)],axis=1)
        caps1, caps2 = np.array(strip_find(band, icol, dpix=dpix))
        print xpos, n_ap, len(caps1), len(caps2)
        # check the aperture number based on mcap positions 
        for i, pcap1, pcap2 in zip(range(n_ap), pcaps1, pcaps2): 
            tcap1 = caps1[(caps1 < pcap1+ap_thres) & ( caps1 > pcap1-ap_thres)]
            if len(tcap1) == 1: 
                # save this position to the (x, y) position list
                apx1[i].append(xpos)
                apy1[i].append(tcap1[0])
                # save this position for the next tracing loop 
                pcaps1[i] = tcap1[0] 
            elif len(tcap1) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap1))

            tcap2 = caps2[(caps2 < pcap2+ap_thres) & ( caps2 > pcap2-ap_thres)]
            if len(tcap2) == 1: 
                # save this position to the (x, y) position list
                apx2[i].append(xpos)
                apy2[i].append(tcap2[0])
                # save this position for the next tracing loop 
                pcaps2[i] = tcap2[0] 
            elif len(tcap2) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap2))

    
    # sorting the (x, y) positions along x-direction for each aperture  
    # polynomial fitting with degree 
    ap_coeffs1, ap_coeffs2 = [], []
    z1, z2 = ip.zscale(img)
    plt.figure(figsize=(15,15))
    plt.imshow(img, cmap='gray', vmin=z1, vmax=z2)
    for k in range(len(apx1)): #n_ap): 
        tapx1 = np.array(apx1[k],dtype=np.float)
        tapx2 = np.array(apx2[k],dtype=np.float)
        tapy1 = np.array(apy1[k],dtype=np.float)
        tapy2 = np.array(apy2[k],dtype=np.float)
        
        tsort1 = np.argsort(tapx1)
        tsort2 = np.argsort(tapx2)
        coeff1 = np.polynomial.polynomial.polyfit(tapx1[tsort1],tapy1[tsort1],degree)
        coeff2 = np.polynomial.polynomial.polyfit(tapx2[tsort2],tapy2[tsort2],degree)
        
        # save the fitting coefficients 
        ap_coeff = np.zeros([2,degree+1])
        ap_coeff[0,:] = coeff1
        ap_coeff[1,:] = coeff2
        np.savetxt(target_path+'apmap_%s_%02d.%03d.dat' % (band, degree, k), ap_coeff)
        
        ap_coeffs1.append(coeff1)
        ap_coeffs2.append(coeff2)
        
        yfit1 = np.polynomial.polynomial.polyval(tapx1[tsort1],coeff1)
        yfit2 = np.polynomial.polynomial.polyval(tapx2[tsort2],coeff2)
        
        plt.plot(tapx1[tsort1],tapy1[tsort1], 'go')
        plt.plot(tapx2[tsort2],tapy2[tsort2], 'ro')
        
        print 'ap[%d]: delta_y1 = %12.8f %12.8f ' \
          % (k, np.mean(tapy1[tsort1]-yfit1), np.std(tapy1[tsort1]-yfit1))
        print 'ap[%d]: delta_y2 = %12.8f %12.8f ' \
          % (k, np.mean(tapy2[tsort2]-yfit2), np.std(tapy2[tsort2]-yfit2))
        
        xx = np.arange(nx)
        yfit1 = np.polynomial.polynomial.polyval(xx,coeff1)
        yfit2 = np.polynomial.polynomial.polyval(xx,coeff2)
        
        plt.plot(xx,yfit1,'g-', linewidth=3, alpha=0.6)
        plt.plot(xx,yfit2,'r-', linewidth=3, alpha=0.6)
        
    plt.xlim(0,nx)
    plt.ylim(0,ny)
    plt.show()
    plt.savefig(target_path+name+'_aptracing2.png')
    plt.close('all')
    
    return ap_coeffs1, ap_coeffs2
Example #9
0
File: manual.py Project: gmace/plp
def ap_tracing_peak(img, npoints=40, spix=10, dpix=20, thres=1400, \
                start_col=None, ap_thres=None, degree=None):
    ''' 
    Trace the apertures starting at "start_col" position (for stellar spectrum) 
     - inputs
        1. npoints : slicing number along the column direction
        2. spix : summing pixel size along column for finding peaks
        3. dpix : detecting pixel range of feature to be regarded as the same  
        4. thres : detecting intensity level for emission features 
        5. start_col : starting column number for tracing 
        6. ap_thres : threshold of pixel range to identify nearby aperture features
        7. degree : degree of polynomial fitting      
     - outputs
        coefficients of polynomial fittings for each aperture   
    '''
    # image size 
    ny, nx = img.shape  
    
    # define the threshold pixel range to be matched with nearby aperture positions 
    if ap_thres == None: ap_thres= nx/npoints/2
    # define the degree of polynomials for fitting 
    if degree == None: degree = 3 #5
    
    # find the apertures in the middle column
    if start_col == None: start_col = nx/2
    icol = np.mean(img[:,(start_col-spix/2):(start_col+spix/2)],axis=1)
    mcaps = peak_find(icol, thres=thres, dpix=dpix)
    n_ap = len(mcaps)

    # make set of (x, y) positions for each aperture
    # add the (x, y) positions at default column 
    apx = []
    apy = []
    for mcap in mcaps:
        apx.append([start_col]) 
        apy.append([mcap])
    
    # define the tracing direction (left, right) 
    xsteps = np.linspace(spix,nx-spix,npoints)
    xlefts = xsteps[(xsteps < start_col)]
    xrights = xsteps[(xsteps > start_col)]
    
    # the left side columns 
    pcaps = np.copy(mcaps) 
    for xpos in reversed(xlefts):
        icol = np.mean(img[:,(xpos-spix/2):(xpos+spix/2)],axis=1)
        caps = np.array(peak_find(icol, dpix=dpix, thres=thres))
        print xpos, len(caps), n_ap
        # check the aperture number based on mcap positions 
        for i, pcap in enumerate(pcaps): 
            tcap = caps[(caps < pcap+ap_thres) & ( caps > pcap-ap_thres)]
            if len(tcap) == 1: 
                # save this position to the (x, y) position list
                apx[i].append(xpos)
                apy[i].append(tcap[0])
                # save this position for the next tracing loop 
                pcaps[i] = tcap[0] 
            elif len(tcap) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap))
    
    # the right side columns 
    pcaps = np.copy(mcaps)                      
    for xpos in xrights:
        icol = np.mean(img[:,(xpos-spix/2):(xpos+spix/2)],axis=1)
        caps = np.array(peak_find(icol, dpix=dpix, thres=thres))
        print xpos, len(caps), n_ap
        # check the aperture number based on mcap positions 
        for i, pcap in enumerate(pcaps): 
            tcap = caps[(caps < pcap+ap_thres) & ( caps > pcap-ap_thres)]
            if len(tcap) == 1: 
                # save this position to the (x, y) position list
                apx[i].append(xpos)
                apy[i].append(tcap[0])
                # save this position for the next tracing loop
                pcaps[i] = tcap[0]
            elif len(tcap) == 0: 
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else: 
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap))
                         
    # sorting the (x, y) positions along x-direction for each aperture  
    # polynomial fitting with degree 
    ap_coeffs = []
    z1, z2 = ip.zscale(img)
    plt.imshow(img, cmap='hot', vmin=z1, vmax=z2)
    for i in range(n_ap-1): 
        tapx = np.array(apx[i],dtype=np.float)
        tapy = np.array(apy[i],dtype=np.float)
        tsort = np.argsort(tapx)
        coeff = np.polynomial.polynomial.polyfit(tapx[tsort],tapy[tsort],degree)
        ap_coeffs.append(coeff)
        yfit = np.polynomial.polynomial.polyval(tapx[tsort],coeff)
        plt.plot(tapx[tsort],tapy[tsort], 'go')
        plt.plot(tapx[tsort],yfit,'b-', linewidth=10, alpha=0.3)
        print 'ap[%d]: delta_y = %12.8f %12.8f ' \
          % (i, np.mean(tapy[tsort]-yfit), np.std(tapy[tsort]-yfit))
        
    plt.xlim(0,2048)
    plt.ylim(0,2048)
    #plt.show()
    return ap_coeffs
Example #10
0
File: manual.py Project: gmace/plp
def _line_identify_old(stripfile, linefile=None, \
                  npoints=30, spix=4, dpix=5, thres=13000, fdeg=5, desc=''):
    def key_press(event):

        if event.key == 'q': plt.close('all')

        if event.key == 'a':
            line_idx = np.argmin((event.xdata - ysignal)**2)
            line_x = ysignal[line_idx]

            print line_idx, line_x
            # mark the points for fitting
            ll, = np.where((xx < line_x + dpix) & (xx > line_x - dpix))
            print len(ll)
            l1 = ax.plot([line_x, line_x], [0, ny],
                         'r-',
                         linewidth=5,
                         alpha=0.5)
            l2 = ax.plot(xx[ll], yy[ll], 'ro')
            coeff = np.polynomial.polynomial.polyfit(xx[ll], yy[ll], fdeg)
            fig.canvas.draw()

            print coeff
            ycoeff[line_idx, :] = np.array(coeff)
            # redraw for the marked points

            # read the wavelength for this line
            input = raw_input('input wavelength:')
            try:
                line_wv = np.float(input)
            except:
                line_wv = 0.0

            print line_idx, line_x, line_wv
            ywave[line_idx] = line_wv

    strip, hdr = ip.readfits(stripfile)

    ny, nx = strip.shape
    z1, z2 = ip.zscale(strip)

    # fitting the base level
    snr = 10
    ysum = np.average(strip[(ny / 2 - spix):(ny / 2 + spix), :], axis=0)
    ll = np.arange(nx)
    nrejt = 0
    while (1):
        yc = np.polynomial.polynomial.polyfit(ll, ysum[ll], fdeg)
        yfit = np.polynomial.polynomial.polyval(np.arange(nx), yc)
        ll, = np.where(ysum < yfit * (1.0 + 1.0 / snr))
        #print nrejt
        if nrejt == len(ll): break
        nrejt = len(ll)

    ysignal = np.array(peak_find(ysum, thres=thres, dpix=dpix))

    # define figure object
    fig = plt.figure(figsize=(14, 4))
    ax = fig.add_subplot(111)

    # draw the strip image
    ax.imshow(strip, cmap='gray', vmin=z1, vmax=z2, aspect='auto')
    ax.set_xlim(0, nx)
    ax.set_ylim(-5, ny + 5)
    # mark the number of lines
    for i, ypeak in enumerate(ysignal):
        if i % 2 == 1:
            ytext = ny
        else:
            ytext = -5
        ax.text(ypeak, ytext, i + 1, fontsize=15)

    # find the emission feature in each row
    ysteps = np.linspace(spix, ny - spix, npoints)
    # make the array for emission feature points
    xx, yy = ([], [])
    for ypos in ysteps:
        irow = np.mean(strip[(ypos - spix / 2):(ypos + spix / 2), :], axis=0)
        #a2.plot(irow)
        # find the peak positions
        xpos_list = peak_find(irow, dpix=dpix, thres=thres)
        # draw and save the peak positions (x, y)
        for xpos in xpos_list:
            ax.plot(xpos, ypos, 'go', markersize=5, alpha=0.5)
            xx.append(xpos)
            yy.append(ypos)
    # convert into the numpy array
    xx = np.array(xx, dtype=np.float)
    yy = np.array(yy, dtype=np.float)

    # generate the wavelength array for each line
    ywave = np.zeros(len(ysignal))
    ycoeff = np.zeros([len(ysignal), fdeg + 1])
    fig.canvas.mpl_connect('key_press_event', key_press)
    print '[a] add line and input wavelength'
    print '[r] reset'
    print '[q] quit from this aperture'
    plt.show()

    print 'thanks'
Example #11
0
File: manual.py Project: gmace/plp
def line_identify(stripfile, linedata=[], outputfile=None, \
                  npoints=30, spix=5, dpix=5, thres=15000):
    '''
    Identify the lines in the strip based on the line database
    - inputs : 
     1. stripfile (with wavelength data or not) 
     2. linefile (for line database) 
    '''
    def key_press(event):

        ax = event.inaxes
        if ax == None: return
        ax_title = ax.get_title()

        if event.key == 'q': plt.close('all')

        if event.key == 'm':
            click_x, click_y = event.xdata, event.ydata
            rr = np.arange((click_x - 2 * dpix), (click_x + 2 * dpix + 1),
                           dtype=np.int)
            #p = ip.gauss_fit(rr, row[rr], p0=[1, x2, 1])
            #a2.plot(rr, ip.gauss(rr, *p), 'r--')
            #mx = p[1]
            mx, mval = ip.find_features(rr, row[rr], gpix=dpix)
            mx, mval = mx[0], mval[0]
            a2.plot(mx, mval, 'ro')
            fig.canvas.draw()
            fwv = cwv[np.argmin(np.abs(cwv - wav[mx]))]
            strtmp = raw_input('input wavelength at (%d, %d) = %.7f:' %
                               (mx, my, fwv))
            if strtmp == '': strtmp = fwv
            try:
                mwv = np.float(strtmp)
                lxx.append(mx)
                lyy.append(my)
                lwv.append(mwv)
                a1.plot(mx, my, 'ro')
                fig.canvas.draw()
                print '%.7f at (%d, %d)' % (mwv, mx, my)
            except:
                print 'No input, again...'

    if len(linedata) == 2:
        cwv, cflx = linedata
    else:
        print 'No input data for line information'
        cwv, cflx = None, None

    strip, hdr = ip.readfits(stripfile)
    fpath, fname = ip.split_path(stripfile)
    if outputfile == None: outputfile = fpath + 'c' + fname
    # extract the file name only without extension
    name = '.'.join(fname.split('.')[:-1])

    ny, nx = strip.shape
    yy, xx = np.indices(strip.shape)

    if 'WV-DIM' in hdr.keys():
        xdim, ydim = np.array(hdr.get('WV-DIM').split(','), dtype=np.int)
        wl_coeff = np.zeros([xdim * ydim])
        for i in range(xdim):
            tmp = hdr.get('WV-X%03d' % (i, ))
            wl_coeff[(i * ydim):(i * ydim + ydim)] = np.array(tmp.split(','),
                                                              dtype=np.double)
        awave = ip.polyval2d(xx, yy, wl_coeff, deg=[xdim - 1, ydim - 1])
    else:
        print 'No wavelength data in FITS header'
        awave = None

    # define figure object
    fig = plt.figure(figsize=(14, 7))
    a1 = fig.add_subplot(211, title='strip')
    a2 = fig.add_subplot(212, title='line')

    # draw the strip image
    z1, z2 = ip.zscale(strip)
    a1.imshow(strip, cmap='gray', vmin=z1, vmax=z2, aspect='auto')
    a1.set_xlim(0, nx)
    a1.set_ylim(-10, ny + 10)

    lspec = np.sum(strip, axis=0)
    lxpos = np.arange(nx)
    a2.plot(lxpos, lspec)
    a2.set_xlim(0, nx)
    # draw the lines in the database
    if (awave != None) & (cwv != None):
        twave = awave[int(ny / 2), :]
        print twave
        wv1, wv2 = np.min(twave), np.max(twave)
        for wv0 in cwv[(cwv > wv1) & (cwv < wv2)]:
            x0 = np.argmin(np.abs(twave - wv0))
            a2.plot([x0, x0], [0, ny], 'r--', linewidth=1, alpha=0.4)

    # make the array for emission feature points
    lxx, lyy, lwave = ([], [], [])
    fig.canvas.mpl_connect('key_press_event', key_press)
    print '[m] mark the line and input the wavelength'
    print '[q] quit from this aperture'

    plt.show()

    return
Example #12
0
File: manual.py Project: gmace/plp
def ap_tracing_strip(band, filename, npoints=40, spix=10, dpix=20, thres=14000, \
                start_col=None, ap_thres=None, degree=None, \
                devide=8.0, target_path=MANUAL_PATH):
    ''' 
    Trace the apertures starting at "start_col" position (for FLAT spectrum)
     - inputs
        0. band 
        1. npoints : slicing number along the column direction
        2. spix : summing pixel size along vertical column for finding strips
        3. dpix : detecting pixel range of feature to be regarded as the same  
        4. thres : detecting intensity level for emission features 
        5. start_col : starting column number for tracing 
        6. ap_thres : threshold of pixel range to identify nearby aperture features
        7. degree : degree of polynomial fitting      
     - outputs
        1. coefficients of polynomial fittings 
           for start position of each aperture on y-axis   
        2. coefficients of polynomial fittings 
           for end position of each aperture on y-axis
    '''
    img, hdr = ip.readfits(filename)
    img, hdr = ip.readfits(filename)
    fpath, fname = ip.split_path(filename)
    if ip.exist_path(target_path) == False: target_path = fpath
    name = '.'.join(fname.split('.')[:-1])
    # image size
    ny, nx = img.shape

    # define the threshold pixel range to be matched with nearby aperture positions
    if ap_thres == None: ap_thres = nx / npoints / 4
    # define the degree of polynomials for fitting
    if degree == None: degree = 3  #7

    # find the apertures in the middle column
    #if start_col == None: start_col = start_col #1500 #nx/2
    icol = np.mean(img[:, (start_col - spix / 2):(start_col + spix / 2)],
                   axis=1)
    #print 'icol', icol, len(icol)
    mcaps1, mcaps2 = strip_find(band, icol)
    n_ap = len(mcaps1)

    # make set of (x, y) positions for each aperture
    # add the (x, y) positions at default column
    apx1, apx2 = [], []
    apy1, apy2 = [], []
    for mcap1, mcap2 in zip(mcaps1, mcaps2):
        apx1.append([start_col])
        apx2.append([start_col])
        apy1.append([mcap1])
        apy2.append([mcap2])

    # define the tracing direction (left, right)
    xsteps = np.linspace(spix, nx - spix, npoints)
    xlefts = xsteps[(xsteps < start_col)]
    xrights = xsteps[(xsteps > start_col)]

    # the left side columns
    pcaps1, pcaps2 = np.copy(mcaps1), np.copy(mcaps2)
    for xpos in reversed(xlefts):
        icol = np.mean(img[:, (xpos - spix / 2):(xpos + spix / 2)], axis=1)
        caps1, caps2 = np.array(strip_find(band, icol, dpix=dpix, margin=10))
        #print xpos, n_ap, len(caps1), len(caps2)
        # check the aperture number based on mcap positions
        for i, pcap1, pcap2 in zip(range(n_ap), pcaps1, pcaps2):
            tcap1 = caps1[(caps1 < pcap1 + ap_thres)
                          & (caps1 > pcap1 - ap_thres)]
            if len(tcap1) == 1:
                # save this position to the (x, y) position list
                apx1[i].append(xpos)
                apy1[i].append(tcap1[0])
                # save this position for the next tracing loop
                pcaps1[i] = tcap1[0]
            elif len(tcap1) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap1))

            tcap2 = caps2[(caps2 < pcap2 + ap_thres)
                          & (caps2 > pcap2 - ap_thres)]
            if len(tcap2) == 1:
                # save this position to the (x, y) position list
                apx2[i].append(xpos)
                apy2[i].append(tcap2[0])
                # save this position for the next tracing loop
                pcaps2[i] = tcap2[0]
            elif len(tcap2) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap2))

    # the right side columns
    pcaps1, pcaps2 = np.copy(mcaps1), np.copy(mcaps2)
    for xpos in xrights:
        icol = np.mean(img[:, (xpos - spix / 2):(xpos + spix / 2)], axis=1)
        caps1, caps2 = np.array(strip_find(band, icol, dpix=dpix))
        print xpos, n_ap, len(caps1), len(caps2)
        # check the aperture number based on mcap positions
        for i, pcap1, pcap2 in zip(range(n_ap), pcaps1, pcaps2):
            tcap1 = caps1[(caps1 < pcap1 + ap_thres)
                          & (caps1 > pcap1 - ap_thres)]
            if len(tcap1) == 1:
                # save this position to the (x, y) position list
                apx1[i].append(xpos)
                apy1[i].append(tcap1[0])
                # save this position for the next tracing loop
                pcaps1[i] = tcap1[0]
            elif len(tcap1) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap1))

            tcap2 = caps2[(caps2 < pcap2 + ap_thres)
                          & (caps2 > pcap2 - ap_thres)]
            if len(tcap2) == 1:
                # save this position to the (x, y) position list
                apx2[i].append(xpos)
                apy2[i].append(tcap2[0])
                # save this position for the next tracing loop
                pcaps2[i] = tcap2[0]
            elif len(tcap2) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap2))

    # sorting the (x, y) positions along x-direction for each aperture
    # polynomial fitting with degree
    ap_coeffs1, ap_coeffs2 = [], []
    z1, z2 = ip.zscale(img)
    plt.figure(figsize=(15, 15))
    plt.imshow(img, cmap='gray', vmin=z1, vmax=z2)
    for k in range(len(apx1)):  #n_ap):
        tapx1 = np.array(apx1[k], dtype=np.float)
        tapx2 = np.array(apx2[k], dtype=np.float)
        tapy1 = np.array(apy1[k], dtype=np.float)
        tapy2 = np.array(apy2[k], dtype=np.float)

        tsort1 = np.argsort(tapx1)
        tsort2 = np.argsort(tapx2)
        coeff1 = np.polynomial.polynomial.polyfit(tapx1[tsort1], tapy1[tsort1],
                                                  degree)
        coeff2 = np.polynomial.polynomial.polyfit(tapx2[tsort2], tapy2[tsort2],
                                                  degree)

        # save the fitting coefficients
        ap_coeff = np.zeros([2, degree + 1])
        ap_coeff[0, :] = coeff1
        ap_coeff[1, :] = coeff2
        np.savetxt(target_path + 'apmap_%s_%02d.%03d.dat' % (band, degree, k),
                   ap_coeff)

        ap_coeffs1.append(coeff1)
        ap_coeffs2.append(coeff2)

        yfit1 = np.polynomial.polynomial.polyval(tapx1[tsort1], coeff1)
        yfit2 = np.polynomial.polynomial.polyval(tapx2[tsort2], coeff2)

        plt.plot(tapx1[tsort1], tapy1[tsort1], 'go')
        plt.plot(tapx2[tsort2], tapy2[tsort2], 'ro')

        print 'ap[%d]: delta_y1 = %12.8f %12.8f ' \
          % (k, np.mean(tapy1[tsort1]-yfit1), np.std(tapy1[tsort1]-yfit1))
        print 'ap[%d]: delta_y2 = %12.8f %12.8f ' \
          % (k, np.mean(tapy2[tsort2]-yfit2), np.std(tapy2[tsort2]-yfit2))

        xx = np.arange(nx)
        yfit1 = np.polynomial.polynomial.polyval(xx, coeff1)
        yfit2 = np.polynomial.polynomial.polyval(xx, coeff2)

        plt.plot(xx, yfit1, 'g-', linewidth=3, alpha=0.6)
        plt.plot(xx, yfit2, 'r-', linewidth=3, alpha=0.6)

    plt.xlim(0, nx)
    plt.ylim(0, ny)
    plt.show()
    plt.savefig(target_path + name + '_aptracing2.png')
    plt.close('all')

    return ap_coeffs1, ap_coeffs2
Example #13
0
File: manual.py Project: gmace/plp
def ap_tracing_peak(img, npoints=40, spix=10, dpix=20, thres=1400, \
                start_col=None, ap_thres=None, degree=None):
    ''' 
    Trace the apertures starting at "start_col" position (for stellar spectrum) 
     - inputs
        1. npoints : slicing number along the column direction
        2. spix : summing pixel size along column for finding peaks
        3. dpix : detecting pixel range of feature to be regarded as the same  
        4. thres : detecting intensity level for emission features 
        5. start_col : starting column number for tracing 
        6. ap_thres : threshold of pixel range to identify nearby aperture features
        7. degree : degree of polynomial fitting      
     - outputs
        coefficients of polynomial fittings for each aperture   
    '''
    # image size
    ny, nx = img.shape

    # define the threshold pixel range to be matched with nearby aperture positions
    if ap_thres == None: ap_thres = nx / npoints / 2
    # define the degree of polynomials for fitting
    if degree == None: degree = 3  #5

    # find the apertures in the middle column
    if start_col == None: start_col = nx / 2
    icol = np.mean(img[:, (start_col - spix / 2):(start_col + spix / 2)],
                   axis=1)
    mcaps = peak_find(icol, thres=thres, dpix=dpix)
    n_ap = len(mcaps)

    # make set of (x, y) positions for each aperture
    # add the (x, y) positions at default column
    apx = []
    apy = []
    for mcap in mcaps:
        apx.append([start_col])
        apy.append([mcap])

    # define the tracing direction (left, right)
    xsteps = np.linspace(spix, nx - spix, npoints)
    xlefts = xsteps[(xsteps < start_col)]
    xrights = xsteps[(xsteps > start_col)]

    # the left side columns
    pcaps = np.copy(mcaps)
    for xpos in reversed(xlefts):
        icol = np.mean(img[:, (xpos - spix / 2):(xpos + spix / 2)], axis=1)
        caps = np.array(peak_find(icol, dpix=dpix, thres=thres))
        print xpos, len(caps), n_ap
        # check the aperture number based on mcap positions
        for i, pcap in enumerate(pcaps):
            tcap = caps[(caps < pcap + ap_thres) & (caps > pcap - ap_thres)]
            if len(tcap) == 1:
                # save this position to the (x, y) position list
                apx[i].append(xpos)
                apy[i].append(tcap[0])
                # save this position for the next tracing loop
                pcaps[i] = tcap[0]
            elif len(tcap) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap))

    # the right side columns
    pcaps = np.copy(mcaps)
    for xpos in xrights:
        icol = np.mean(img[:, (xpos - spix / 2):(xpos + spix / 2)], axis=1)
        caps = np.array(peak_find(icol, dpix=dpix, thres=thres))
        print xpos, len(caps), n_ap
        # check the aperture number based on mcap positions
        for i, pcap in enumerate(pcaps):
            tcap = caps[(caps < pcap + ap_thres) & (caps > pcap - ap_thres)]
            if len(tcap) == 1:
                # save this position to the (x, y) position list
                apx[i].append(xpos)
                apy[i].append(tcap[0])
                # save this position for the next tracing loop
                pcaps[i] = tcap[0]
            elif len(tcap) == 0:
                print 'ap[%d](x=%d) : The matching aperture position was not found' \
                   % (i, xpos)
            else:
                print 'ap[%d](x=%d) : The matching aperture position was too many(%d)' \
                   % (i, xpos, len(tcap))

    # sorting the (x, y) positions along x-direction for each aperture
    # polynomial fitting with degree
    ap_coeffs = []
    z1, z2 = ip.zscale(img)
    plt.imshow(img, cmap='hot', vmin=z1, vmax=z2)
    for i in range(n_ap - 1):
        tapx = np.array(apx[i], dtype=np.float)
        tapy = np.array(apy[i], dtype=np.float)
        tsort = np.argsort(tapx)
        coeff = np.polynomial.polynomial.polyfit(tapx[tsort], tapy[tsort],
                                                 degree)
        ap_coeffs.append(coeff)
        yfit = np.polynomial.polynomial.polyval(tapx[tsort], coeff)
        plt.plot(tapx[tsort], tapy[tsort], 'go')
        plt.plot(tapx[tsort], yfit, 'b-', linewidth=10, alpha=0.3)
        print 'ap[%d]: delta_y = %12.8f %12.8f ' \
          % (i, np.mean(tapy[tsort]-yfit), np.std(tapy[tsort]-yfit))

    plt.xlim(0, 2048)
    plt.ylim(0, 2048)
    #plt.show()
    return ap_coeffs
Example #14
0
def check(stripfile, aperture, tstrip, lxx_t, lwave, b, linear_wave, pngpath='', datpath='', outputname=''):

    # Input images
    img, ahdr = ip.readfits(stripfile)
    ny, nx = img.shape
    

    wavel = np.array(lwave)
    npix = 9 # Range pixel of each identify lines
    
    # The belove steps are used to check the transform processes.

    ti_peakline, tlwidth = peak_reidentify(tstrip, npix, lxx_t)

    npix = np.zeros(len(lxx_t))
    for i in range(len(lxx_t)):
       npix[i] = round(np.average(tlwidth[i]))*2 + 1

    tilxx = np.zeros(len(lxx_t))
    for i in range(len(lxx_t)):
        tilxx[i] = ti_peakline[i][2]

    #print 'identify lxx', tilxx 

    peak_w = reidentify(tstrip, npix, list(tilxx))

    # Check peak line stable of Gaussian profile
    for i in range(len(peak_w)):
        for j in range(len(peak_w[i])):
            if (peak_w[i][j] - np.median(peak_w[i])) > 3:
               peak_w[i][j] =  np.median(peak_w[i])
            if (peak_w[i][j] - np.median(peak_w[i])) < -3:
               peak_w[i][j] =  np.median(peak_w[i])

    # Using polynomial to fit the emission lines

    tfit_x, tvmiddle = linefitting(tstrip, peak_w, lxx_t)

    # Test distortion of emission lines
    residual(peak_w, tvmiddle)

    # txx values transform

    step = int(ny/6)
    txx = np.zeros(len(lxx_t))
    for i in range(0, len(lxx_t)):
        txx[i] = peak_w[i][2]  # Sum of 10 lines in the middle stripfile

    #print 'center of identify lines after transform', txx

    # Second order
    coeff2 = np.polynomial.polynomial.polyfit(wavel, txx, 2)
    #print 'cofficient fitting linear wave', coeff2

    tp_min = [coeff2[2], coeff2[1], coeff2[0]]
    tvalue_min = np.roots(tp_min)

    tp_max = [coeff2[2], coeff2[1], coeff2[0] - 2047]
    tvalue_max = np.roots(tp_max)

    tlamda_min = find_nearest(tvalue_min, min(wavel))
    tlamda_max = find_nearest(tvalue_max, max(wavel))

    #print 'position', np.where(tvalue_min == tlamda_min)[0], np.where(tvalue_max == tlamda_max)[0]    
    #print 'lambda min and max', tlamda_min, tlamda_max

    # Position array
    position = np.where(tvalue_max == tlamda_max)[0]
    
    # Check in case of complex values
    
    if (np.iscomplexobj(tlamda_min)):
        lamda_min = np.real(tlamda_min)
    elif (np.iscomplexobj(tlamda_max)):
        lamda_max = np.real(tlamda_max)
        
    tlamda = np.zeros(nx)
    for i in range(nx):
        pt = [coeff2[2], coeff2[1], coeff2[0] - i]
        if (np.iscomplexobj(np.roots(pt))):
            temp = np.real(np.roots(pt))
            tlamda[i] = temp[position]
        else:
            tlamda[i] = np.roots(pt)[position]

    

    # 4nd order
    '''
    coeff2 = np.polynomial.polynomial.polyfit(wavel, txx, 4)
    print 'cofficient fitting linear wave', coeff2

    tp_min = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0]]
    tvalue_min = np.roots(tp_min)

    tp_max = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0] - 2047]
    tvalue_max = np.roots(tp_max)

    tlamda_min = find_nearest(tvalue_min, min(wavel))
    tlamda_max = find_nearest(tvalue_max, max(wavel))

    print 'position', np.where(tvalue_min == tlamda_min)[0], np.where(tvalue_max == tlamda_max)[0]    
    print 'lambda min and max', tlamda_min, tlamda_max
    
    print 'lambda min and max after transform', tlamda_min, tlamda_max

    tlamda = np.zeros(nx)
    for i in range(nx):
         p = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0] - i]
         tlamda[i] = np.roots(p)[np.where(tvalue_min == tlamda_min)[0]]
    '''

    #fig = plt.figure(4, figsize=(10,8))
    #a = fig.add_subplot(111)
    #a.plot(tlamda, range(nx), 'r', wavel, txx, 'ko')
    #a.plot(wavel, txx, 'ko')
    #plt.ylabel('X-position [pixels]')
    #plt.xlabel('Wavelength [microns]')
    #plt.legend(['Polyfit', 'X-after transform'])
    #plt.show()

    # Test file of results

    # wavelength calculate from the linear equation from the
    # line lists
    f_linear = np.zeros(len(wavel))
    for i in range(len(wavel)):
        f_linear[i] = b[0] + b[1] * wavel[i]
    #print 'linear coeff', b

    
    f = open(datpath + outputname + str(aperture) + '.dat','w')
    A = np.arange(len(f_linear))
    for j in range(len(peak_w[0])):
       for i in A:
          temp = "%.6f %.6f %.6f %.6f \n" %(wavel[i], f_linear[i], np.array(peak_w[i][j]), \
                                              np.array(peak_w[i][j]) - f_linear[i])
          f.write(temp)

    f.close()
           
    # Draw the strip image         
    f6 = plt.figure(None, figsize=(14,6))
    ax6= f6.add_subplot(311, title= outputname + str(aperture))
    z1, z2 = ip.zscale(tstrip)   
    ax6.imshow(img, cmap='hot',vmin=z1, vmax=z2, aspect='auto')
    ax6.set_xlim(0,nx)
    ax6.set_ylim(-10,ny+10)
    plt.ylabel('Y [pixels]')
    #plt.xlabel('X [pixels]')


    #f7 = plt.figure(7, figsize=(14,3))
    ax7= f6.add_subplot(312)
    ax7.imshow(tstrip, cmap='hot',vmin=z1, vmax=z2, aspect='auto')
    #ax7.plot(range(nx), img_shift[ny/2,:], 'k.', trxx, tstrip[ny/2,:], 'r.')
    #plt.legend(['before transform', 'after transform'])
    ax7.set_xlim(0,nx)
    ax7.set_ylim(-10,ny+10)
    #plt.xlabel('X [pixels]')


    #f8 = plt.figure(8, figsize=(14,3))
    ax8 = f6.add_subplot(313, title='')
    ax8.plot(linear_wave, tstrip[ny/2,:], 'b')

    #ax4.set_xlim(0, len(tstrip[1,:]))
    #ax4.set_ylim(-10,ny+10)
    #ax4.set_xlim(min(w_fitting), max(w_fitting))
    ax8.set_xlim(min(linear_wave), max(linear_wave))
    plt.xlabel('Wavelength [microns]')

    plt.savefig(pngpath + outputname + str(aperture) + '.png')
    plt.close()

    return coeff2
Example #15
0
def draw_strips(strip, wave, desc='', linedata=[], lampname='', grayplot=False, \
                target_path=PNG_PATH):
    '''
    Draw the strips with wavelength 
     - INPUTS:
      1. strip data 
      2. wavelength data
      3. line data 
     - OUTPUTS:
      PNG files      
    '''
    #print 'draw_strips', lampname
    if len(linedata) > 0:
        cwv, cflx = linedata
    wmin, wmax = (np.min(wave), np.max(wave))

    z1, z2 = ip.zscale(strip)

    f2 = plt.figure(2, figsize=(12, 5), dpi=200)
    a1 = f2.add_subplot(211, aspect='auto')
    a2 = f2.add_subplot(212, aspect='auto')

    ny, nx = strip.shape
    cuty = ny / 2 - 8
    a1.imshow(strip, cmap='hot', aspect=nx / 1000.0, vmin=z1, vmax=z2)
    a1.plot([0, nx], [cuty, cuty], 'g--', alpha=0.8, linewidth=3)
    a1.set_xlim((0, nx))
    a1.set_ylim((0, ny))
    a1.set_title('%s' % (desc, ))

    a2.plot(wave, strip[cuty, :], 'b-', linewidth=1)
    ww = np.where((cwv < wmax) & (cwv > wmin))[0]
    fmax = np.zeros([len(ww)]) + np.max(strip[cuty, :]) * 1.05
    a2.plot(cwv[ww], fmax, 'r|', markersize=10, markeredgewidth=2, alpha=0.7)
    if lampname != '': lampname = '(' + lampname + ')'
    a2.set_title('%s %s' % (desc, lampname))
    a2.set_xlabel('Wavelength [um]')
    a2.set_xlim((wmin, wmax))
    a2.set_ylim((0, fmax[0] * 1.1))
    #    f2.savefig(target_path+'%s_prof.png' % (desc,)) #2013-11-21 cksim commented this
    f2.savefig(target_path + '%s.png' % (desc, ))  #2013-11-21 cksim

    if grayplot == True:
        f3 = plt.figure(3, figsize=(12, 5), dpi=200)
        a3 = f3.add_subplot(111)

        a3.imshow(strip, cmap='gray', aspect=nx / 200.0, vmin=z1, vmax=z2)
        x, y = (np.arange(nx), np.sum(strip, axis=0))
        a3.plot(x, (y-np.min(y))/(np.max(y)-np.min(y))*ny*0.9,\
                'b-', linewidth=3, alpha=0.7)
        x, y = ((cwv[ww] - wmin) / (wmax - wmin) * (nx - 1),
                np.zeros(len(ww)) + ny * 1.05)
        a3.plot(x, y, 'r|', markersize=10, markeredgewidth=2, alpha=0.7)
        a3.set_xlim((0, nx))
        a3.set_ylim((0, ny * 1.1))
        cticks = wticks[np.where((wticks > wmin) & (wticks < wmax))]
        xticks = (cticks - wmin) / (wmax - wmin) * (nx - 1)
        #print xticks
        a3.set_yticks([])
        a3.set_xticks(xticks)
        a3.set_xticklabels(['%.3f' % x for x in cticks])
        a3.set_xlabel('Wavelength [um]')
        f3.savefig(target_path + '%s_gray.png' % (desc, ))

    plt.close('all')
Example #16
0
def check(stripfile,
          aperture,
          tstrip,
          lxx_t,
          lwave,
          b,
          linear_wave,
          pngpath='',
          datpath='',
          outputname=''):

    # Input images
    img, ahdr = ip.readfits(stripfile)
    ny, nx = img.shape

    wavel = np.array(lwave)
    npix = 9  # Range pixel of each identify lines

    # The belove steps are used to check the transform processes.

    ti_peakline, tlwidth = peak_reidentify(tstrip, npix, lxx_t)

    npix = np.zeros(len(lxx_t))
    for i in range(len(lxx_t)):
        npix[i] = round(np.average(tlwidth[i])) * 2 + 1

    tilxx = np.zeros(len(lxx_t))
    for i in range(len(lxx_t)):
        tilxx[i] = ti_peakline[i][2]

    #print 'identify lxx', tilxx

    peak_w = reidentify(tstrip, npix, list(tilxx))

    # Check peak line stable of Gaussian profile
    for i in range(len(peak_w)):
        for j in range(len(peak_w[i])):
            if (peak_w[i][j] - np.median(peak_w[i])) > 3:
                peak_w[i][j] = np.median(peak_w[i])
            if (peak_w[i][j] - np.median(peak_w[i])) < -3:
                peak_w[i][j] = np.median(peak_w[i])

    # Using polynomial to fit the emission lines

    tfit_x, tvmiddle = linefitting(tstrip, peak_w, lxx_t)

    # Test distortion of emission lines
    residual(peak_w, tvmiddle)

    # txx values transform

    step = int(ny / 6)
    txx = np.zeros(len(lxx_t))
    for i in range(0, len(lxx_t)):
        txx[i] = peak_w[i][2]  # Sum of 10 lines in the middle stripfile

    #print 'center of identify lines after transform', txx

    # Second order
    coeff2 = np.polynomial.polynomial.polyfit(wavel, txx, 2)
    #print 'cofficient fitting linear wave', coeff2

    tp_min = [coeff2[2], coeff2[1], coeff2[0]]
    tvalue_min = np.roots(tp_min)

    tp_max = [coeff2[2], coeff2[1], coeff2[0] - 2047]
    tvalue_max = np.roots(tp_max)

    tlamda_min = find_nearest(tvalue_min, min(wavel))
    tlamda_max = find_nearest(tvalue_max, max(wavel))

    #print 'position', np.where(tvalue_min == tlamda_min)[0], np.where(tvalue_max == tlamda_max)[0]
    #print 'lambda min and max', tlamda_min, tlamda_max

    # Position array
    position = np.where(tvalue_max == tlamda_max)[0]

    # Check in case of complex values

    if (np.iscomplexobj(tlamda_min)):
        lamda_min = np.real(tlamda_min)
    elif (np.iscomplexobj(tlamda_max)):
        lamda_max = np.real(tlamda_max)

    tlamda = np.zeros(nx)
    for i in range(nx):
        pt = [coeff2[2], coeff2[1], coeff2[0] - i]
        if (np.iscomplexobj(np.roots(pt))):
            temp = np.real(np.roots(pt))
            tlamda[i] = temp[position]
        else:
            tlamda[i] = np.roots(pt)[position]

    # 4nd order
    '''
    coeff2 = np.polynomial.polynomial.polyfit(wavel, txx, 4)
    print 'cofficient fitting linear wave', coeff2

    tp_min = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0]]
    tvalue_min = np.roots(tp_min)

    tp_max = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0] - 2047]
    tvalue_max = np.roots(tp_max)

    tlamda_min = find_nearest(tvalue_min, min(wavel))
    tlamda_max = find_nearest(tvalue_max, max(wavel))

    print 'position', np.where(tvalue_min == tlamda_min)[0], np.where(tvalue_max == tlamda_max)[0]    
    print 'lambda min and max', tlamda_min, tlamda_max
    
    print 'lambda min and max after transform', tlamda_min, tlamda_max

    tlamda = np.zeros(nx)
    for i in range(nx):
         p = [coeff2[4], coeff2[3], coeff2[2], coeff2[1], coeff2[0] - i]
         tlamda[i] = np.roots(p)[np.where(tvalue_min == tlamda_min)[0]]
    '''

    #fig = plt.figure(4, figsize=(10,8))
    #a = fig.add_subplot(111)
    #a.plot(tlamda, range(nx), 'r', wavel, txx, 'ko')
    #a.plot(wavel, txx, 'ko')
    #plt.ylabel('X-position [pixels]')
    #plt.xlabel('Wavelength [microns]')
    #plt.legend(['Polyfit', 'X-after transform'])
    #plt.show()

    # Test file of results

    # wavelength calculate from the linear equation from the
    # line lists
    f_linear = np.zeros(len(wavel))
    for i in range(len(wavel)):
        f_linear[i] = b[0] + b[1] * wavel[i]
    #print 'linear coeff', b

    f = open(datpath + outputname + str(aperture) + '.dat', 'w')
    A = np.arange(len(f_linear))
    for j in range(len(peak_w[0])):
        for i in A:
            temp = "%.6f %.6f %.6f %.6f \n" %(wavel[i], f_linear[i], np.array(peak_w[i][j]), \
                                                np.array(peak_w[i][j]) - f_linear[i])
            f.write(temp)

    f.close()

    # Draw the strip image
    f6 = plt.figure(None, figsize=(14, 6))
    ax6 = f6.add_subplot(311, title=outputname + str(aperture))
    z1, z2 = ip.zscale(tstrip)
    ax6.imshow(img, cmap='hot', vmin=z1, vmax=z2, aspect='auto')
    ax6.set_xlim(0, nx)
    ax6.set_ylim(-10, ny + 10)
    plt.ylabel('Y [pixels]')
    #plt.xlabel('X [pixels]')

    #f7 = plt.figure(7, figsize=(14,3))
    ax7 = f6.add_subplot(312)
    ax7.imshow(tstrip, cmap='hot', vmin=z1, vmax=z2, aspect='auto')
    #ax7.plot(range(nx), img_shift[ny/2,:], 'k.', trxx, tstrip[ny/2,:], 'r.')
    #plt.legend(['before transform', 'after transform'])
    ax7.set_xlim(0, nx)
    ax7.set_ylim(-10, ny + 10)
    #plt.xlabel('X [pixels]')

    #f8 = plt.figure(8, figsize=(14,3))
    ax8 = f6.add_subplot(313, title='')
    ax8.plot(linear_wave, tstrip[ny / 2, :], 'b')

    #ax4.set_xlim(0, len(tstrip[1,:]))
    #ax4.set_ylim(-10,ny+10)
    #ax4.set_xlim(min(w_fitting), max(w_fitting))
    ax8.set_xlim(min(linear_wave), max(linear_wave))
    plt.xlabel('Wavelength [microns]')

    plt.savefig(pngpath + outputname + str(aperture) + '.png')
    plt.close()

    return coeff2