def velpoint(self, xr, yr, plot=True): wave = np.zeros(self.nframe) flux = np.zeros(self.nframe) for i in range(self.nframe): l = self.transform[i] t_x = [float(x) for x in l[0:3]] t_y = [float(x) for x in l[3:6]] #open the data and background subtract it hdu = self.hdulist[i] data = hdu[0].data #get the header keywords et1z = hdu[0].header['ET1Z'] fpA = hdu[0].header['FPA'] fpB = hdu[0].header['FPB'] fpC = hdu[0].header['FPC'] fpD = hdu[0].header['FPD'] fpE = hdu[0].header['FPE'] fpF = hdu[0].header['FPF'] utctime = hdu[0].header['UTC-OBS'] utctime = datetime.datetime.strptime(utctime, '%H:%M:%S.%f') fpcoef = [fpA, fpB, fpC, fpD, fpE, fpF] #determine the matching indices w, v = invtransform(t_x, t_y, xr, yr) radius = ((w - self.axc)**2 + (v - self.ayc)**2)**0.5 mid = (int(v), int(w)) wave[i] = fpfunc(et1z, radius, 0, fpcoef) flux[i] = data[mid] * self.norm[i] if plot: print xr, yr, w, v, et1z, radius, wave[i], flux[i], self.norm[ i] if plot: fd = fitdata(wave, flux) m = dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0]) s = 5 h = flux.max() fd.data = fd.data + fd.continuum - flux.min() fd.continuum = flux.min() fd.fitgauss(m, s, h) print dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0]) print qcent(wave, flux) print fd.mu(), fd.sigma(), flux.max(), fd.height(), fd.continuum w = np.arange(wave.min(), wave.max(), 0.1) pl.figure() pl.plot(wave[:10], flux[:10], marker='o', ls='') pl.plot(wave[10:], flux[10:], marker='o', ls='') pl.plot(w, fd.gauss(w) + fd.continuum) pl.show() return wave, flux, dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0])
def velpoint(self, xr, yr, plot=True): wave = np.zeros(self.nframe) flux = np.zeros(self.nframe) for i in range(self.nframe): l = self.transform[i] t_x = [float(x) for x in l[0:3]] t_y = [float(x) for x in l[3:6]] # open the data and background subtract it hdu = self.hdulist[i] data = hdu[0].data # get the header keywords et1z = hdu[0].header["ET1Z"] fpA = hdu[0].header["FPA"] fpB = hdu[0].header["FPB"] fpC = hdu[0].header["FPC"] fpD = hdu[0].header["FPD"] fpE = hdu[0].header["FPE"] fpF = hdu[0].header["FPF"] utctime = hdu[0].header["UTC-OBS"] utctime = datetime.datetime.strptime(utctime, "%H:%M:%S.%f") fpcoef = [fpA, fpB, fpC, fpD, fpE, fpF] # determine the matching indices w, v = invtransform(t_x, t_y, xr, yr) radius = ((w - self.axc) ** 2 + (v - self.ayc) ** 2) ** 0.5 mid = (int(v), int(w)) wave[i] = fpfunc(et1z, radius, 0, fpcoef) flux[i] = data[mid] * self.norm[i] if plot: print xr, yr, w, v, et1z, radius, wave[i], flux[i], self.norm[i] if plot: fd = fitdata(wave, flux) m = dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0]) s = 5 h = flux.max() fd.data = fd.data + fd.continuum - flux.min() fd.continuum = flux.min() fd.fitgauss(m, s, h) print dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0]) print qcent(wave, flux) print fd.mu(), fd.sigma(), flux.max(), fd.height(), fd.continuum w = np.arange(wave.min(), wave.max(), 0.1) pl.figure() pl.plot(wave[:10], flux[:10], marker="o", ls="") pl.plot(wave[10:], flux[10:], marker="o", ls="") pl.plot(w, fd.gauss(w) + fd.continuum) pl.show() return wave, flux, dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0])
def velpoint(self, xr,yr, plot=True): wave=np.zeros(self.nframe) flux=np.zeros(self.nframe) for i in range(self.nframe): l=self.transform[i] t_x=[float(x) for x in l[0:3]] t_y=[float(x) for x in l[3:6]] #open the data and background subtract it hdu=self.hdulist[i] data=hdu[0].data #get the header keywords et1z=hdu[0].header['ET1Z'] fpA=hdu[0].header['FPA'] fpB=hdu[0].header['FPB'] fpC=hdu[0].header['FPC'] fpD=hdu[0].header['FPD'] fpE=hdu[0].header['FPE'] fpF=hdu[0].header['FPF'] utctime=hdu[0].header['UTC-OBS'] utctime=datetime.datetime.strptime(utctime, '%H:%M:%S.%f') fpcoef=[fpA, fpB, fpC, fpD, fpE, fpF] #determine the matching indices w,v=invtransform(t_x, t_y, xr, yr) radius=((w-self.axc)**2+(v-self.ayc)**2)**0.5 mid=(int(v), int(w)) wave[i]=fpfunc(et1z, radius, 0, fpcoef) flux[i]=data[mid]*self.norm[i] #if plot: print xr, yr, w, v, et1z, radius, wave[i], flux[i], self.norm[i] return wave, flux, dl.centroid(wave, flux, kern=[0,-1,0,1,0])
def mcentroid(xarr, yarr, kern=default_kernal, xc=None, xdiff=None, mode='same'): """Find the centroid of a line following a similar algorithm as the centroid algorithm in IRAF. xarr and yarr should be an area around the desired feature to be centroided. The default kernal is used if the user does not specific one. The algorithm solves for the solution to the equation ..math:: \int (I-I_0) f(x-x_0) dx = 0 These are the following parameters: xarr -- array of x values yarr -- array of y values kern -- kernal to convolve the array with xc -- initial gues xdiff-- Pixels around xc to use for convolution mode -- Mode of convolution returns xc """ if xdiff<len(kern): xdiff=len(kern) if xc is not None and xdiff: mask=(abs(xarr-xc)<xdiff) else: mask=np.ones(len(xarr), dtype=bool) return detectlines.centroid(xarr, yarr, kern=kern, mask=mask, mode=mode)
def detect_lines(w_arr, f_arr, sigma=3, bsigma=None, niter=5, mask=None, kern=default_kernal, center=False): """Detect lines goes through a 1-D spectra and detect peaks w_arr--xaxis array (pixels, wavelength, etc) f_arr--yaxis array (flux, counts, etc) sigma--Threshold for detecting sources bsigma--Threshold for determining background statistics niter--iterations to determine background center--return centroids and not pixels mask--Pixels not to use """ # set up the variables if bsigma is None: bsigma = sigma if mask: f_arr = f_arr[mask] w_arr = w_arr[mask] # find all peaks peaks = find_peaks(f_arr, sigma, niter, bsigma=bsigma) # set the output values xp = w_arr[peaks] if center: xdiff = int(0.5 * len(kern) + 1) x_arr = np.arange(len(w_arr)) xp = xp * 1.0 for i in range(len(peaks)): cmask = (abs(x_arr - peaks[i]) < xdiff) xp[i] = detectlines.centroid(w_arr, f_arr, kern=kern, mask=cmask) return xp
def velpoint(self, xr, yr, plot=True): wave = np.zeros(self.nframe) flux = np.zeros(self.nframe) for i in range(self.nframe): l = self.transform[i] t_x = [float(x) for x in l[0:3]] t_y = [float(x) for x in l[3:6]] #open the data and background subtract it hdu = self.hdulist[i] data = hdu[0].data #get the header keywords et1z = hdu[0].header['ET1Z'] fpA = hdu[0].header['FPA'] fpB = hdu[0].header['FPB'] fpC = hdu[0].header['FPC'] fpD = hdu[0].header['FPD'] fpE = hdu[0].header['FPE'] fpF = hdu[0].header['FPF'] utctime = hdu[0].header['UTC-OBS'] utctime = datetime.datetime.strptime(utctime, '%H:%M:%S.%f') fpcoef = [fpA, fpB, fpC, fpD, fpE, fpF] #determine the matching indices w, v = invtransform(t_x, t_y, xr, yr) radius = ((w - self.axc)**2 + (v - self.ayc)**2)**0.5 mid = (int(v), int(w)) wave[i] = fpfunc(et1z, radius, 0, fpcoef) flux[i] = data[mid] * self.norm[i] #if plot: print xr, yr, w, v, et1z, radius, wave[i], flux[i], self.norm[i] return wave, flux, dl.centroid(wave, flux, kern=[0, -1, 0, 1, 0])
def imagemap(img_list, x1, x2, y1, y2, rx,ry): nimg=len(img_list) dx=x2-x1 dy=y2-y1 data=np.zeros((nimg, dy, dx)) warr=np.zeros(nimg) flux=np.zeros(nimg) rx1=rx-10 rx2=rx+10 ry1=ry-10 ry2=ry+10 for i,f in enumerate(img_list): hdu=pyfits.open(f.strip()) data[i,:,:]=hdu[1].data[y1:y2,x1:x2] warr[i]=hdu[0].header['ET1A']+hdu[0].header['ET1B']*hdu[0].header['ET1Z'] flux[i]=hdu[1].data[ry1:ry2,rx1:rx2].sum() print i, warr[i] pl.figure() dim=np.zeros((dy,dx)) for i in range(dx): for j in range(dy): d=data[:, j,i]/flux #dim[j,i]= d.sum() #(warr*d).sum()/ d.sum() if d.sum() > 0.025: dim[j,i]=dl.centroid(warr, d, kern=[0,-1,0,1,0]) else: dim[j,i]=np.nan pl.plot(warr, data[:,300,100]) pl.plot(warr, data[:,250,150]) pl.plot(warr, data[:,150,100]) pl.plot(warr, data[:,100,250]) d=data[:,300,100]/flux print (data[:,100,250]/flux).sum() print (warr*(d-d.min())).sum()/(d-d.min()).sum() print dl.centroid(warr, d, kern=[0,-1,0,1,0]) #pl.imshow(dim, vmin=6697, vmax=6722) pl.show()