Example #1
0
def _findxycenter(hdr, im,ext,log):

    def xy_tran(out):
        """
          Function to transform blue coordinates to red frame coordinates
        """
        xref = out[1] - 990.5897
        yref = out[0] - 37.82109
        x = -0.9985259*xref - 0.0178984*yref
        y = -0.0181331*xref + 0.9991414*yref
        return y,x
    
    #hdr = pf.getheader(file)
    #im = pf.getdata(file)
    xcen = hdr.get('xcen')
    ycen = hdr.get('ycen')
    if (xcen == None):
        #ratio,xc,yc = peak2halo('',image=im)
        #xcen = xc
        #ycen = yc
        #if (xcen < 0 or ycen < 0):
        if True:
            if ext == 2:
                # register blue frame to red's frame coordinate system
                im = nd.geometric_transform (im,xy_tran)


            try:
                ndis.display(im,zscale=False)
            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])

            xcen,ycen = gcentroid(im, x1,y1,9)
            xcen = xcen[0]
            ycen = ycen[0]
 
            hdr.update("XCEN",xcen, "Start mask x-center")
            hdr.update("YCEN",ycen, "Start mask y-center")
            log.info('                    (%.2f,%.2f) :: ' % (xcen,ycen)+('red','blue')[ext-1])
Example #2
0
    def getCpoints(self):
        """
          Get control points from a pair of Red and Blue pinholes
          frames from the NICI camera.
          You already have frames 1 and 2 (red and blue)
          from a pinhole image up in DS9.
          Please select at least 6 corresponding points. 

          Once you click 'r' in the Red frame, a red circle is drawn
          around the pinhole and a Blue circle is drawn 'near' the
          blue pinhole; click 'b' there to record the position.
          Press 'q' to finish.
        """
 
        i=0; j=0
        xkey = ''
        print "*** Please get at least 6 corresponding pinholes."
        while xkey != 'q':
            cursor = ndis.readcursor(sample=0)
            ss = cursor.split()
            print ss
            xkey = ss.pop()
            if xkey not in ('r','b','q'):
               print "Please choose a control point on the 'r' or 'b' frame,"
               print " then hit 'r' for red frame or 'b' for 'blue' frame"
               continue
            im = {'r':self.imr, 'b':self.imb, 'q':0}[xkey]
            if xkey == 'q': break
            
            x,y = np.asfarray(ss[:2])
            xc,yc = gcentroid(im[y-10:y+10,x-10:x+10],10,10)
            xc,yc = x+xc[0]-10,y+yc[0]-10

            if xkey == 'r':
                #print "xc,yc: ",xc,yc,x+xc-10,y+yc-10
                self.xyr[i] = xc,yc; i+=1
                ov.circle(x=xc,y=yc,radius=10,color=ov.C_RED,frame=1)
                xn = 990.5897 - 0.9985259*x - 0.0178984*y
                yn = 37.82109 - 0.0181331*x + 0.9991414*y
                #xc,yc = gcentroid(self.imb[yn-13:y+13,xn-13:x+13],13,13)
                #xc,yc = xn+xc[0]-10,yn+yc[0]-10
                ov.circle(x=xn,y=yn,radius=10,color=ov.C_BLUE, frame=2)
                print self.xyr[i-1]
            if xkey == 'b':
                self.xyb[j] = xc,yc; j+=1
                print self.xyb[j-1]
            
        self.npin = j
        self.xyr = np.resize(self.xyr,[j,2])
        self.xyb = np.resize(self.xyb,[j,2])
Example #3
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
Example #4
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