示例#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
示例#2
0
    def medfilter(self):

        clobber = self.clobber
        gphu =    self.gphu
        odir =    self.odir
        suffix =  self.suffix

        log = self.log
        #self._get_filters_scaling(self.idir+self.inputs[0])

        if self._file_exists('','cube_sdi'): return

        l1 =      self.l1  
        l2 =      self.l2  

        rcube =  self._read_fits('red','cube')
        bcube =  self._read_fits('blue','cube')
        #rcube = self.cubes['red']
        #bcube = self.cubes['blue']

        sz = np.shape(rcube)
        if not self.central:               # r is a 2d image with pixel values
            r = dist_circle([1024,1024])   # corresponding to the distance to the
        else:                              # center of the image
            r = dist_circle([512,512])

        # Find pixels between 25 and 50 pixels from the center of the image.
        # These values should be modified when using a mask other than 0.32''
        g = np.where((r > 25) & (r < 50))
       
        # The masks data is:  Scale is 0.018 ''/pixels
        #Focal Plane Mask keyword name: FPMW
        # Values are:
        #    Clear
        #    0.90 arcsec
        #    0.65 arcsec
        #    0.46 arcsec
        #    0.32 arcsec radious    18 pixels
        #    0.22 arcsec
        #    Grid
        #    User


        rad = np.zeros(round(np.amax(r))-10,dtype=np.float32)

        cube_diff = np.zeros(np.shape(rcube),dtype=np.float32)

        # Scale rcube or bcube to the shorther wlen (depends on the set
        # of filter used)
        scale = min(l1,l2)/l1
        fn = 0
        if scale == 1.0:
            scale = min(l1,l2)/l2
            fn = 1
        
        ft = lambda i,o: '%.2f %d:%.2d' % (time.time()-i,(time.time()-o)/60,(time.time()-o)%60)
        to = time.time()
        for i in range(sz[0]):
            ti = time.time()
            rim = self._medfilter(rcube[i,:,:],r,rad)
            bim = self._medfilter(bcube[i,:,:],r,rad)
            log.info('medfilter:      ['+str(i+1)+'/'+str(sz[0])+'] ' + ft(ti,to))
            rcube[i,:,:] = rim
            bcube[i,:,:] = bim
            # scale r or b
            ti = time.time()
            sim = self._scale_im((rim,bim)[fn],scale)
            log.info('scale:                 ' +ft(ti,to))
            ti = time.time()
            cube_diff[i,:,:] = self._sdi(((sim,bim),(rim,sim))[fn],self.pa[i],g)
            log.info('sdi (red-blue):        ' +ft(ti,to))

        self._write_fits(cube_diff,'','cube_sdi')
        self._write_fits(np.median(cube_diff,axis=0),'','sdi_medcrunch')

        self._write_fits(rcube,'red','cube_medfilter')
        self._write_fits(bcube,'blue','cube_medfilter')

        self._shift_medfilter(rcube,'red')
        self._shift_medfilter(bcube,'blue')

        rcube = self._rotate_cube(rcube)
        bcube = self._rotate_cube(bcube)
        self._write_fits(np.median(rcube,axis=0),'red','medfilter_medcrunch_rotate')
        self._write_fits(np.median(bcube,axis=0),'blue','medfilter_medcrunch_rotate')

        # Release memory
        del rcube,bcube,cube_diff