Ejemplo n.º 1
0
def peak2halo(files,exten=None,image=None):

    if (image is not None): 
        im = image
        xcen = 0.0
        ycen = 0.0
        ratio = 0.0
        nfiles = 1 
    else:   
        if isinstance(files,(tuple,list)):
           nfiles = size(files)
        else:
           nfiles = 1   
           files = [files]
        xcen = np.empty(nfiles,dtype=float)
        ycen = np.empty(nfiles,dtype=float)
        ratio = np.empty(nfiles,dtype=float)
        if exten is None: exten = 1
        if exten != 1 and exten != 2 :
            print 'the EXTEN keyword must be equal to 1 OR 2.'
            return

    k = 0
    for nfic in range(nfiles):

        #print 'peak2halo ['+str(nfic+1)+'/'+str(nfiles)+']'

        if (image is None):
            hdr = pf.getheader(files[nfic])
            if hdr['instrume']!='NICI':
                print files[nfic],' is not NICI file. Skip it'
                return
                #continue
            elif hdr['obsclass']!= 'science':
                print files[nfic],' is not NICI science file. Skip it'
                return
                #continue
                
            im = pf.getdata(files[nfic],exten)
                 # reads the fits image. We will measure the peak/halo 
                 # ratio only in the extension=exten
        bin = medbin(im,32,32)
        # # calculates de std inside
        #dev = bin[5:27,5:27].std()
        #bin = bin[5:27,5:27] - np.median(bin[5:27,5:27])
        
        ## see if we have a mask in the frame
        #nelem_mask = np.size(np.where(abs(bin)>dev*5))/2
        #if nelem_mask == 0:
        #    return -99,-1,-1

        im = congrid( (bin > 3*robust_sigma(bin))*1 ,(1024,1024))*im

        px = np.zeros(1024,dtype=float)
        py = np.zeros(1024,dtype=float)

        for i in range(64):
            py = np.clip(py,\
                np.nan_to_num(np.median(im[:,i*16:i*16+16],axis=1)),np.amax(py))
        for i in range(64):
            px = np.clip(px, \
                np.nan_to_num(np.median(im[i*16:i*16+16,:],axis=0)),np.amax(px))
                 # extract the maximum value seen in the image for a 
                 # 16 pixel-wide stripe- in the x and y direction 
        px = np.clip(px,min(px),max(px)*.25)
        py = np.clip(py,min(py),max(py)*.25)


        offset = 1023 - np.arange(2047.) 
                # cross-correlate this profile with a 'flipped' version 
                # of itself. A peak at pixel 512 will have a '0' offset
        xrr = size(px) - np.arange(size(px))-1
        cx = c_correlate(px,px[xrr],offset) 
                # a peak at pixel N will have a 2*(512-N) offset
        cy = c_correlate(py,py[size(py)-np.arange(size(py))-1],offset)
       
        ix = cx.argmax()
        xcen_tmp0 = 512-offset[ix]/2.
        iy = cy.argmax()
        ycen_tmp0 = 512-offset[iy]/2.    # derive the position of the peakmax
               # get a fine centroid at this position

        medfilter = medfilt2d(im,7)
        tmp=(im-medfilter)[ycen_tmp0-20:ycen_tmp0+20,xcen_tmp0-20:xcen_tmp0+20]
        try:
            id = tmp.argmax()
            xcen_tmp0 += ((id%40)-20)
            ycen_tmp0 += (id/40-20)

            xcen_tmp,ycen_tmp = gcentroid(im,xcen_tmp0,ycen_tmp0,9) 
        except:
            xcen_tmp = -1

        if xcen_tmp < 0:
            peak=-1; halo=1
            break 
        if mt.sqrt((xcen_tmp0-xcen_tmp)**2+(ycen_tmp0-ycen_tmp)**2) > 9: 
           peak=-1; halo=1
           break

        xc = round(xcen_tmp)
        yc = round(ycen_tmp)
        #x = np.arange(xc-3,xc+4)
        #y = np.arange(yc-3,yc+4)
        #z = im[yc-3:yc+4,xc-3:xc+4]
        x = np.arange(xcen_tmp-2,xcen_tmp+5)
        y = np.arange(ycen_tmp-2,ycen_tmp+5)
        z = im[yc-2:yc+5,xc-2:xc+5]
        try:
            pxy = interp2d(x,y,z,kind='cubic')
            peak = float(pxy(xcen_tmp,ycen_tmp))
        except:
            peak =-1; halo=1
            break

            # get an accurate estimate of the peak value
        r = dist_circle([1024,1024],xcen=xcen_tmp,ycen=ycen_tmp)
        g = np.where((r > 20) & (r < 30)) 
             # get the median value between 20 and 30 pixels of this peak
        halo = np.median(im[g])
       
         
        if nfiles > 1:
            ratio[k] = peak/halo
            xcen[k] = xcen_tmp
            ycen[k] = ycen_tmp
        else:
           if xcen_tmp < 0:
               ratio = -1
           else: ratio = peak/halo
           xcen = xcen_tmp
           ycen = ycen_tmp
           return ratio,np.float(xcen_tmp),np.float(ycen_tmp)
        
        k += 1
    
    if (nfiles == 1):
       if xcen_tmp < 0:
           ratio = -1
           xcen = -1; ycen = -1
       else:
           ratio = peak/halo
           xcen = np.float(xcen_tmp)
           ycen = np.float(ycen_tmp)

    return ratio,xcen,ycen
Ejemplo n.º 2
0
def nici_cntrd(im,hdr,center_im=True):
    """
    Read xcen,ycen and update header if necessary
    If the automatic finding of the center mask fails, then
    the interactive session will start. The SAOIMAGE/ds9
    display must be up. If the default port is busy, then it will
    use port 5199, so make sure you start "ds9 -port 5199".
    """

    xcen = hdr.get('xcen')
    ycen = hdr.get('ycen')
    updated = False
    if (xcen == None):
        ratio,xc,yc = peak2halo('',image=im)
        #xcen = xc[0]
        #ycen = yc[0]
        xcen = xc
        ycen = yc
        if (xcen < 0 or ycen < 0):
            try:
                ndis.display(im)
            except IOError,err:
                sys.stderr.write('\n ***** ERROR: %s Start DS9.\n' % str(err))
                sys.exit(1)
            print " Mark center with left button, then use 'q' to continue, 's' to skip."
            cursor = ndis.readcursor(sample=0)
            cursor = cursor.split()
            if cursor[3] == 's':
                hdr.update("XCEN",-1, "Start mask x-center")
                hdr.update("YCEN",-1, "Start mask y-center")
                updated = True 
                print '\nFrame skipped... ****Make sure not to use it in your science script. ***\n'
                #return updated,xcen,ycen,im
                return xcen,ycen,im
            x1 = float(cursor[0])
            y1 = float(cursor[1])


            box = im[y1-64:y1+64,x1-64:x1+64].copy()
            box -= scipy.signal.signaltools.medfilt2d(np.float32(box),11)
            box = box[32:32+64,32:32+64]

            bbx = box * ((box>(-robust_sigma(box)*5)) & \
                         (box <(15*robust_sigma(box))))

            imbb = congrid(bbx,(1024,1024))
            ndis.display(imbb, name='bbx')
            del imbb

            cursor = ndis.readcursor(sample=0)
            cursor = cursor.split()
            x2 = float(cursor[0])
            y2 = float(cursor[1])

            xcen,ycen = gcentroid(box, x2/16., y2/16., 4)

            xcen = (xcen+x1)[0] - 32
            ycen = (ycen+y1)[0] - 32

        hdr.update("XCEN",xcen, "Start mask x-center")
        hdr.update("YCEN",ycen, "Start mask y-center")

        updated = True