def kegstand(searchstr,EEfigs,Nfig,EE=0.50): """Given an input list of fits files this function will try to compute the f-ratio of the SDSS test stand input beam. """ R = np.array([]) dprime = np.array([]) pp = PDF(EEfigs) pp2 = PDF(Nfig) in_files = glob(searchstr) for image in in_files: print image HDU = pyfits.open(image)[0] dp = float(image.split('_')[1].split('.fits')[0]) dprime = np.append(dprime, dp) radius = get_radius(HDU.data,pp,EE) R = np.append(R, radius) print "d: {:4.3f}, R: {:4.3f}".format(dp,radius) dprime *= -1 # Ns = np.array([]) # for k in range(numtrys): # sampleidx = np.random.randint(dprime.size, size = dprime.size) # tempdp = dprime[sampleidx] # tempR = R[sampleidx] # Ns = np.append(Ns, # (2.*ADE.polyclip(tempdp,tempR,1,niter=10).c[0])**-1) fit_coef = ADE.polyclip(dprime,R,1,niter=10).c slope = fit_coef[0] ### compute the uncertainty N = (2.*slope)**-1 # N = np.mean(Ns) # N_err = np.std(Ns) Nfit = np.poly1d(fit_coef) fitd = np.linspace(dprime.min(),dprime.max(),50) ### compute the uncertainty see = (np.sum((R - np.polyval(fit_coef,dprime))**2)/(R.size - 2))**0.5 slope_err = see * (1/(np.sum((dprime - np.mean(dprime))**2)))**0.5 N_err = slope_err/(2.*slope**2) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(dprime,R,marker='s',linestyle='') ax.plot(fitd,Nfit(fitd),'k:',label='linear fit') ax.set_xlabel("$-d^'$ [mm]") ax.set_ylabel('$R$ [mm]') ax.legend(loc=0) ax.set_title('{}\nN: {:3.2f}$\pm$ {:3.2f}'.\ format(datetime.now().isoformat(' '),N,N_err)) pp2.savefig(fig) pp.close() pp2.close() return N, N_err
def fitms(spectrum,error,template_list, out_prefix, cut=0.75, pre=0, mdegree=25, degree=4): pd = PDF(out_prefix+'.pdf') #make the galaxy wavelength vector data_hdu = pyfits.open(spectrum) error_hdu = pyfits.open(error) ddw = data_hdu[0].header['CDELT1'] lambda0 = data_hdu[0].header['CRVAL1'] wave = lambda0+ \ np.arange(data_hdu[0].shape[1])*ddw # wave = wave[pre:] lamRange1 = np.array([wave.min(),wave.max()])#/1.008246 galaxy = data_hdu[0].data[0]#[pre:] loggalaxy, logLam1, velscale = pputil.log_rebin(lamRange1,galaxy) masklow = wave.min() - 500. maskhigh = wave.max() + 500. print data_hdu[0].shape print masklow,maskhigh c = 299792.458 FWHM_gal = 0.95/1.008246 #AA FWHM_tmp = 0.55 #AA FWHM_diff = np.sqrt(FWHM_gal**2 - FWHM_tmp**2) sigma = FWHM_diff/2.355/ddw temp_hdu = pyfits.open(template_list[0]) twave = temp_hdu[0].header['CRVAL1'] + \ np.arange(temp_hdu[0].data.shape[1])*temp_hdu[0].header['CDELT1'] mask = np.where((twave > masklow) & (twave < maskhigh)) template = temp_hdu[0].data[0][mask] twave = twave[mask] lamRange2 = np.array([twave.min(),twave.max()]) template = ndimage.gaussian_filter1d(template,sigma) logtmp, logLam2, velscale_tmp = pputil.log_rebin(lamRange2, template, velscale=velscale) bestfits = np.empty((1,wave.size)) templates = np.empty((logtmp.size)) print templates.shape for template_file in template_list: temp_hdu = pyfits.open(template_file)[0] data = temp_hdu.data header = temp_hdu.header twave = header['CRVAL1'] + np.arange(data.shape[1])*header['CDELT1'] mask = (twave > masklow) & (twave < maskhigh) twave = twave[mask] for i in range(data.shape[0]): td = data[i][mask] # tfit = ADE.polyclip(twave,td,order) # td /= tfit(twave) td = ndimage.gaussian_filter1d(td,sigma) logtd, logLam2, velscale = pputil.log_rebin(lamRange2,td, velscale=velscale) templates = np.vstack((templates,logtd/np.median(logtd))) templates = templates[1:].T print templates.shape, twave.shape for i in range(data_hdu[0].data.shape[0]): fig = plt.figure() galaxy = data_hdu[0].data[i]#[pre:] noise = error_hdu[0].data[i] # datafit = ADE.polyclip(wave,galaxy,order) # ax = fig.add_subplot(212) # ax.plot(wave,galaxy) # ax.plot(np.arange(templates[:,0].size),templates[:,0]) # ax.set_ylim(0,2) # ax.plot(wave,datafit(wave)) # galaxy /= datafit(wave) loggalaxy, logLam1, velscale = pputil.log_rebin(lamRange1,galaxy) logerror, _, _ = pputil.log_rebin(lamRange1,noise) # pyfits.PrimaryHDU(templates).writeto('templates.fits') print 'velscale:', velscale dv = (logLam2[0] - logLam1[0])*c print 'dv:', dv vel = c*1.008246 start = [0.,2.] loggalaxy /= np.median(loggalaxy) logerror /= np.median(loggalaxy) goodfit = ADE.polyclip(np.arange(loggalaxy.size),loggalaxy,4) tmpgood = np.where(np.abs(loggalaxy - goodfit(np.arange(loggalaxy.size))) < \ cut*np.std(loggalaxy))[0] tmpgood = tmpgood[tmpgood > pre] goodsmooth = np.zeros(loggalaxy.size) goodsmooth[tmpgood] = 1. goodsmooth = ndimage.gaussian_filter1d(goodsmooth,11) goodpixels = np.where(goodsmooth > 0.9)[0] pp = ppxf(templates,loggalaxy, logerror, velscale,start,bias=None,degree=degree,mdegree=mdegree, vsyst=dv,plot=True, goodpixels=goodpixels) print bestfits.shape bestfits = np.vstack((bestfits,pp.bestfit)) ###PLOT### plot_px = np.exp(logLam1) plot_gal = ndimage.gaussian_filter1d(loggalaxy,3) collection = collections.BrokenBarHCollection.span_where( plot_px, ymin=0,ymax=4, where=goodsmooth < 0.9, facecolor='red',alpha=0.5) collection2 = collections.BrokenBarHCollection.span_where( plot_px, ymin=0,ymax=4, where=np.arange(loggalaxy.size) <= pre, facecolor='red',alpha=0.5) ax2 = fig.add_subplot(411) ax2.plot(np.exp(logLam1),plot_gal) ax2.plot(np.exp(logLam1),goodfit(np.arange(plot_px.size))) ax2.add_collection(collection) ax2.add_collection(collection2) ax2.plot(plot_px,pp.bestfit) ax2.set_xlim(plot_px[0],plot_px[-1]) ax5 = ax2.twiny() ax5.set_xlim(0,loggalaxy.size) ax2.set_ylim(0.6,1.4) bbox2 = ax2.get_position().get_points().flatten() plt.setp(ax2.get_xticklabels(),visible=False) ax3 = fig.add_subplot(412,sharex=ax2) bbox3 = ax3.get_position().get_points().flatten() newpos = [bbox3[0], bbox2[1] - (bbox3[3] - bbox3[1]), bbox3[2] - bbox3[0], bbox3[3] - bbox3[1]] ax3.set_position(newpos) ax3.plot(plot_px,loggalaxy - pp.bestfit,'r',lw=0.7) ax3.set_ylim(-0.2,0.2) ax3.set_xlim(plot_px[0],plot_px[-1]) ax = fig.add_subplot(212) pidx = np.arange(1525,1725,1) ax.plot(plot_px[pidx],plot_gal[pidx]) ax.plot(plot_px[pidx],pp.bestfit[pidx]) ax4 = ax.twiny() velx = np.arange(pidx.size)*velscale velx -= velx[-1]/2. ax4.set_xlim(velx[0],velx[-1]) pd.savefig(fig) plt.close(fig) bestfits = bestfits[1:] pd.close() bfh = pyfits.PrimaryHDU(np.array(bestfits,dtype=np.float32)) bfh.header.update('CDELT1',ddw) bfh.header.update('CRVAL1',wave.min()) bfh.header.update('CRPIX1',1) bfh.header.update('CTYPE1','LINEAR') bfh.writeto(out_prefix+'.ms.fits',clobber=True) iraf.noao.onedspec.dispcor(out_prefix+'.ms.fits',out_prefix+'_lin.ms.fits', linearize=True,log=False,flux=False, dw=ddw,w1=wave.min(),w2=wave.max(),nw='INDEF', samedis=True) plt.close('all') return
def fratio(searchstr,EEfigs,apfigs,bigfig,EE=0.50): '''pronounced frat-eeo, not f ratio''' print int(EE*100) pp = PDF(EEfigs) pp2 = PDF(apfigs) pp3 = PDF(bigfig) inifiles = glob(searchstr) datadict = get_sizes(inifiles,pp,EE=EE) pp.close() ratiodict = {} aps = np.array([]) ratios = np.array([]) for apsize in datadict.keys(): x = np.array(datadict[apsize].keys(),dtype=float) r = np.array(datadict[apsize].values()) fit_coef = ADE.polyclip(x,r,1,niter=100).c slope = fit_coef[0] N = (2.*slope)**-1 fit = np.poly1d(fit_coef) ratiodict[apsize] = {'f/#':N, 'fl':float(apsize)/N} aps = np.append(aps,float(apsize)) ratios = np.append(ratios,N) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,r,marker='s',linestyle='') fitx = np.linspace(x.min(),x.max(),50) ax.plot(fitx,fit(fitx),'k:',label='linear fit') ax.set_xlabel('back distance - C [mm]') ax.set_ylabel('beam radius [mm]') ax.legend(loc=0) ax.set_title('Aperture: {:n} mm\nN: {:3.2f}'.format(float(apsize),N)) pp2.savefig(fig) pp2.close() ratiofit = ADE.polyclip(aps**-1,ratios,1,niter=50,clip_high=1,clip_low=1) # ratiofit = np.poly1d(np.polyfit(aps**-1,ratios,1)) ratioslope = ratiofit.c[0] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(aps**-1,ratios,marker='s',linestyle='') fitaps = np.linspace(aps.min(),aps.max(),50) ax.plot(fitaps**-1,ratiofit(fitaps**-1),':',label='linear fit') ax.set_xlabel('1/D [$mm^{-1}$]') ax.set_ylabel('f-ratio') # ax.set_ylim(0, ratiofit(fitaps.max())*1.1) ax.legend(loc=0) ax.set_title('slope = {:n} mm'.format(ratioslope)) pp3.savefig(fig) pp3.close() return ratiodict
def sortio(searchstr,Nfigs,apfigs,bigfig): inifiles = glob(searchstr) datadict = size_get(inifiles) ratiodict = {} aps = np.array([]) ratios = np.array([]) pp = PDF(Nfigs) pp2 = PDF(apfigs) pp3 = PDF(bigfig) for apsize in datadict.keys(): print '\nApsize = {:n} mm:\n\t{:7}{:7}\n\t'.format(int(apsize),'x','N_x')+'-'*10 xvec = np.array([]) Nprime = np.array([]) sortedkeys = datadict[apsize].keys()[:] sortedkeys.sort() for x in sortedkeys[::-1]: d = datadict[apsize][x].T[0] r = datadict[apsize][x].T[1] Npcoef = np.polyfit(d,r,1) xvec = np.append(xvec,x) Nprime = np.append(Nprime,(2.*Npcoef[0])**-1) print '\t{:4.3f} {:4.3f}'.format(x,(2.*Npcoef[0])**-1) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(d,r,marker='s',linestyle='') fitvec = np.linspace(d.min(),d.max(),50) ax.plot(fitvec,np.poly1d(Npcoef)(fitvec),':') ax.set_title('Aperture: {:3n} mm\n$x={:3.2f}\Rightarrow EE={:3.2f}$\n$N_x$: ${:3.2f}$'.\ format(int(apsize),x,1./x,(2.*Npcoef[0])**-1), fontsize=10) ax.set_xlabel('d') ax.set_ylabel('r') pp.savefig() bigNcoef = np.polyfit(xvec,Nprime,1) print bigNcoef N = bigNcoef[0] print (2.*N)**-1 Nfit = np.poly1d(bigNcoef) ratiodict[apsize] = {'f/#':N, 'fl':float(apsize)*N} aps = np.append(aps,float(apsize)) ratios = np.append(ratios,N) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(xvec,Nprime,marker='s',linestyle='') fitx = np.linspace(xvec.min(),xvec.max(),50) ax.plot(fitx,Nfit(fitx),'k:',label='linear fit') ax.set_xlabel('$x$',fontsize=14) ax.set_ylabel('$N_x$',fontsize=14) ax.legend(loc=0) ax.set_title('Aperture: {:n} mm\nN: {:3.4f}'.format(float(apsize),N)) pp2.savefig(fig) pp.close() pp2.close() ratiofit = ADE.polyclip(aps**-1,ratios,1,niter=50,clip_high=1,clip_low=1) # ratiofit = np.poly1d(np.polyfit(aps**-1,ratios,1)) ratioslope = ratiofit.c[0] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(aps**-1,ratios,marker='s',linestyle='') fitaps = np.linspace(aps.min(),aps.max(),50) ax.plot(fitaps**-1,ratiofit(fitaps**-1),':',label='linear fit') ax.set_xlabel('1/D [$mm^{-1}$]') ax.set_ylabel('f-ratio') # ax.set_ylim(0, ratiofit(fitaps.max())*1.1) ax.legend(loc=0) ax.set_title('slope = {:n} mm'.format(ratioslope)) pp3.savefig(fig) pp3.close() return ratiodict
def plot_line( datafile, radius, wavelength=5048.126, ax=False, central_lambda=[4901.416, 5048.126], flip=False, plot=True, window=20, velo=False, baseline=False, verbose=True, skywindow=20, **plotargs ): """ Plots a single line from a .ms file. It also needs a corresponding .slay file to get the pixel -> kpc radius conversion. datafile - str. can be the name either of a .slay or .ms file. This function requires both files to be present in the current dirctory. If you extracted lines with slayer() then everything should be good automatically Radius - in kpc. The distance from the center of the galaxy where you want to plot a spectrum. Can be negative or positve (for different sides of the galaxy) wavelength - in Angstroms, the central wavelength of the plotted region window - in Angstroms, the range of the plotted region ax - a matplotlilb.axes.AxesSubplot object that will be plotted on if provided. If ax is provided then no additional labels or titles will be added to it central_lambda - python list. The rest-frame wavelengths of the line centers that are contained in the .slay file. Used by openslay() plot - boolean. Set to True to actually plot something. This is here so that this function can be called as a helper to just return the output arrays flip - boolean. Passed to openslay(). Do you want to flip the galaxy around r=0? Changing this will change the positive/negative convention of the radius input """ if ".slay." in datafile: datafile = ".".join(datafile.split(".")[:-2] + ["ms.fits"]) slayfile = ".".join(datafile.split(".")[:-2] + ["slay.fits"]) kpcradii, _, _ = openslay(slayfile, central_lambda=central_lambda, flip=flip, moments=False) pxradii = pyfits.open(slayfile)[3].data row = np.where(np.abs(kpcradii - radius) == np.min(np.abs(kpcradii - radius)))[0][0] if verbose: print "using pixel value {} where radius is {} kpc".format(pxradii[row], kpcradii[row]) datahdus = pyfits.open(datafile) hdu = datahdus[0] CRVAL = hdu.header["CRVAL1"] Cdelt = hdu.header["CDELT1"] try: seperr = hdu.header["SEPERR"] except KeyError: seperr = False """get the width of the bin in kpc""" # print np.array([int(s) for s in hdu.header['APNUM{}'.format(row+1)].split()[2:]]) rwidthpx = np.diff(np.array([int(s) for s in hdu.header["APNUM{}".format(row + 1)].split()[2:]]))[0] rwidth = rwidthpx * 0.118 * 8.0 # 0.118 "/px (from KN 11.29.12) times 8x binning rwidth *= 34.1e3 / 206265.0 # distance (34.1Mpc) / 206265" if verbose: print "rwidth = {} px ({} kpc)".format(rwidthpx, rwidth) # We use '=f8' to force the endianess to be the same as the local # machine. This is so the precompiled bottleneck (bn) functions don't # complain if seperr: spectrum = np.array(hdu.data[row], dtype="=f8") errorfile = "{}_error.{}".format( os.path.basename(datafile).split(".")[0], ".".join(os.path.basename(datafile).split(".")[1:]) ) if os.path.dirname(datafile) != "": errorfile = "{}/{}".format(os.path.dirname(datafile), errorfile) error = pyfits.open(errorfile)[0].data[row] else: spectrum = np.array(hdu.data[row * 2], dtype="=f8") error = hdu.data[row * 2 + 1] wave = np.arange(spectrum.size) * Cdelt + CRVAL idx = np.where((wave >= wavelength - window / 2.0) & (wave <= wavelength + window / 2.0)) if baseline: fit = ADE.polyclip(wave, spectrum, baseline) spectrum -= fit(wave) if velo: wave = (wave - wavelength) / wavelength * 3e5 pwave = wave[idx] pspec = spectrum[idx] perr = error[idx] if not ax and plot: fig = plt.figure() ax = fig.add_subplot(111) if velo: ax.set_xlabel("Velocity [km/s]") else: ax.set_xlabel("Wavelength [Angstroms]") ax.set_ylabel("ADU/s") ax.set_title(datetime.now().isoformat(" ")) if plot: ax.errorbar(pwave, pspec, yerr=perr, **plotargs) fig = ax.figure fig.show() datahdus.close() return pwave, pspec, perr, rwidth