def _medfilter(self,im,r,rad): """ Median filtering of each slice in the cube """ log = self.log pa = self.pa bsize = self.boxcar_size # Boxcar smoothing size (def:5) mdfw = self.medfilt_width # Medfiltering width (def:11) # to accelerate the code, we bin the image and the 'r' # image down to 256x256 and do the radial profile on the binned versions sz = np.shape(im) bsy = sz[0]/4 bsx = sz[1]/4 rbin = nt.rebin(r,bsy,bsx) tmp = np.zeros(self.imsize,dtype=np.float32) # COPY: We are not modifying input image tmp[:,:] = im #NOTE: We are not using reset_num so far imbin = nt.rebin(tmp, bsy,bsx) # bin down to 256x256 for rr in range(8,np.size(rad)): # determine the median profile beyond r=8 # (irrelevant inside this because of focal plane mask) if rr < 50: ss = tmp[abs(r-rr) < .5] rad[rr] = np.median(ss[np.where(np.isfinite(ss))]) else: ss = imbin[abs(rbin-rr) < 3] rad[rr] = np.median(ss[np.where(np.isfinite(ss))]) # smooth the radial profile beyond r=22 rad[22:] = boxcar(rad,(bsize,))[22:] rad[40:] = boxcar(rad,(bsize,))[40:] # smooth even more tmp[:,:] -= gi.ginterpolate(rad,r) tmp[:,:] -= scipy.signal.medfilt2d(np.nan_to_num(tmp),mdfw) #t = time.time() #line = '%.2f' % (t-ti) #log.info('medfilter['+chan+']: ['+str(i+1)+'/'+str(sz[0])+']' +line) return tmp
def TTlinearize_image(image,zzfunction): """ Linearize image using the 2D funcion zz Linearize the image applying the surface function evaluator from the method 'fit_image'. Using the inverse function evaluator for each row of the input image: pixel = self.zinv(lambda, row) Generating a set of lambdas with a dispertion value (cdelt = (self.z(nx) - self.z(1))/nx) as (lambdas = (ixx-crpix)*cdelt + crval), where 'ixx' is the array of indices (1..nx) along the dispersion axis. With the inverse function we obtain the pixel coordinates corresponding to each lambda value. Interpolating the input image values at each of these new pixel coordinates using spline interpolation we linearize the input image. *Input* image: (None) If an ndarray is supplied use this one as the input image to linearize. It should have the same dispersion characteristics since it uses the ARC image dispersion and fitting functions. zzfunction: Dictionary with functions evaluator {'zzfit' and 'zzinvfit'}. These are constructed using the class Eval2 (see above) *Output* new_image: Ndarray of the same shape as the input image. """ import ginterpolate as gi zz = zzfunction['zzfit'] zinv = zzfunction['zinvfit'] data = image # The output arrays ny,nx = np.shape(data) outdtype = data.dtype linear_im = np.zeros((ny,nx),dtype=outdtype) ym = ny/2 z = lambda x: zz(x,ym) # ----- Linearize ------ # Dispersion. We use the already calculated mapping function z # such that lambda=z(pixels). The dispersion direction is # along the x-axis. cdelt = (z(nx) - z(1))/nx crpix = 1 if cdelt < 0: crval = z(nx) cdelt = -cdelt else: crval = z(1) # The linear array in wavelength units for the dispersion axis lams = (np.arange(nx)-crpix)*cdelt + crval lamf = lambda x: (x-crpix)*cdelt + crval for y in range(ny): # Calculate the pixel position for each lambda # using the inverse function. pixels = zinv(lams,y) line = gi.ginterpolate(data[y,:],pixels,order=4) linear_im[y,:] = np.asarray(line,dtype=outdtype) # Keep the input datatype # Calculate the linear WCS #self.linear_wcs((ny,nx)) return linear_im