def on_keypress_smooth(self, event): """ Smooth the flux with a gaussian. Requires attributes self.fl and self.nsmooth, self.artists['fl'] and self.fig.""" if event.key == 'S': if self.nsmooth > 0: self.nsmooth += 0.5 else: self.nsmooth = 1 sfl = convolve_psf(self.fl, self.nsmooth) self.artists['fl'].set_ydata(sfl) self.fig.canvas.draw() elif event.key == 'U': self.nsmooth = 0 self.artists['fl'].set_ydata(self.fl) self.fig.canvas.draw()
def get_new_spec(self, i): """ Change to a new spectrum """ self.i = i filename = self.filenames[i] if self.spec[i] is None: print 'Reading %s' % filename self.spec[i] = barak.spec.read(filename) self.models[i] = None self.ticks = None if self.opt.f26 is not None: self.calc_model() s = self.spec[i] self.ax.cla() co = s.co if self.opt.co_is_sky: co = s.co * np.median(s.fl) / np.median(s.co) * 0.1 self.artists['spec'] = barak.spec.plot( s.wa, s.fl, s.er, co, ax=self.ax, show=0, yperc=0.90) self.artists['fl'] = self.artists['spec'][0] self.artists['co'] = self.artists['spec'][2] if self.nsmooth > 0: sfl = convolve_psf(s.fl, self.nsmooth, edge='reflect') self.artists['fl'].set_data(s.wa, sfl) self.artists['template'], = self.ax.plot([], [], 'y') self.artists['contpoints'], = self.ax.plot( [0], [0], 'x', mfc='None', mew=0.5, ms=8, mec='r') line_artists = barak.spec.plotlines( self.zp1-1, self.ax, labels=1, fontsize=10, lcolor='0.3', offsets=False) self.artists['lines'] = line_artists self.fl = s.fl self.wa = s.wa self.co = co self.name = self.filenames[i]
fnu = np.log10(X**alpha) ind = X.searchsorted(0.99) ax2.semilogx(X, fnu - fnu[ind] + N0, '--c', lw=2, label=r'$J_\nu\propto\ \nu^{%.3g}$' % alpha) # plot the starburst spectrum x = (starburst.wa * u.AA).to(u.rydberg, equivalencies=u.equivalencies.spectral()) x = x.value X = x[::-1] F = starburst.logfnu[::-1] ind = X.searchsorted(1.02) from barak.convolve import convolve_psf ax2.semilogx(X, convolve_psf(F - F[ind] + N0,50), 'r', lw=2, label='$\mathrm{starburst}$') # apply dust extinction from barak.extinction import starburst_Calzetti00 ext = starburst_Calzetti00(starburst.wa, EBmV=0.2) Fnu = np.log10(10**starburst.logfnu * np.exp(-ext.tau)) F = Fnu[::-1] ax2.semilogx(X, convolve_psf(F - F[ind] + N0, 50), 'r', lw=0.5, label='$\mathrm{starburst},\ E(B-V)=0.2$') ax2.legend(frameon=0, loc='lower left', fontsize=12) #plt.grid(ls='solid', lw=0.5, color='0.7') xvals = (IP * u.eV).to(u.Ry).value
def update(self, z): if self.opt.f26 is not None: wa, nfl, ner, nco, model, artists, options = (self.wa, self.nfl, self.ner, self.co, self.model, self.artists, self.opt) else: wa, nfl, ner, nco, artists, options = (self.wa, self.nfl, self.ner, self.co, self.artists, self.opt) self.z = z ymult = self.ymult zp1 = z + 1 betamin = self.vmin / c_kms betamax = self.vmax / c_kms for t in artists['ticklabels']: t.remove() for t in artists['ticks']: t.remove() artists['ticklabels'] = [] artists['ticks'] = [] if artists['ew'] is not None: artists['ew'].remove() artists['ew'] = None for r in artists['regions']: r.remove() for s in artists['sky']: s.remove() for s in artists['aod']: s.remove() artists['regions'] = [] artists['sky'] = [] artists['aod'] = [] # want plots to appear from top down, so we need reversed() for i, trans in enumerate(reversed(options.linelist)): atom, ion = split_trans_name(trans['name'].split()[0]) iax, ioff = divmod(i, self.num_per_panel) ax = self.axes[iax] offset = ioff * 1.5 #if i >= self.num_per_panel: # offset = (i - self.num_per_panel) * 1.5 # ax = self.axes[1] watrans = trans['wa'] obswa = watrans * zp1 wmin = obswa * (1 + 3 * betamin) wmax = obswa * (1 + 3 * betamax) if self.opt.showticks and len(self.allticks) > 0: ticks = self.allticks tickwmin = obswa * (1 + betamin) tickwmax = obswa * (1 + betamax) wticks = ticks.wa cond = between(wticks, tickwmin, tickwmax) if cond.any(): vel = (wticks[cond] / obswa - 1) * c_kms for j, t in enumerate(ticks[cond]): T, Tlabels = plot_tick_vel(ax, vel[j], offset, t, tickz=options.tickz) artists['ticklabels'].extend(Tlabels) artists['ticks'].extend(T) cond = between(wa, wmin, wmax) #good = ~np.isnan(fl) & (er > 0) & ~np.isnan(co) # remove ultra-low S/N regions to help plotting cond &= ner < 1.5 cond &= ymult * (nfl - 1) + 1 > -0.1 fl = nfl[cond] co = nco[cond] vel = (wa[cond] / obswa - 1) * c_kms if options.f26 is not None and options.f26.regions is not None: artists['regions'].extend( plot_velocity_regions(wmin, wmax, options.f26.regions.wmin, options.f26.regions.wmax, obswa, ax, offset, vel, ymult * (fl - 1) + 1)) #import pdb; pdb.set_trace() if hasattr(options, 'aod'): # print ranges used for AOD calculation. # find the right transition c0 = ((np.abs(options.aod['wrest'] - trans['wa']) < 0.01) & (options.aod['atom'] == atom)) itrans = np.flatnonzero(c0) for row in options.aod[itrans]: c0 = between(wa[cond], row['wmin'], row['wmax']) if np.any(c0): artists['aod'].append( ax.fill_between(vel[c0], offset, offset + 1.5, facecolor='0.8', lw=0, zorder=0)) vranges = [] for w0, w1 in unrelated: c0 = between(wa[cond], w0, w1) if c0.any(): vranges.append(c0) for w0, w1 in ATMOS: c0 = between(wa[cond], w0, w1) if c0.any(): artists['sky'].append( ax.fill_between(vel[c0], offset, offset + 1.5, facecolor='0.9', lw=0)) if self.smoothby > 1: if len(fl) > 3 * self.smoothby: fl = convolve_psf(fl, self.smoothby) artists['fl'][i].set_xdata(vel) artists['fl'][i].set_ydata(ymult * (fl - 1) + 1 + offset) artists['co'][i].set_xdata(vel) artists['co'][i].set_ydata(ymult * (co - 1) + 1 + offset) #pdb.set_trace() if self.opt.residuals: resid = (fl - model[cond]) / ner[cond] c0 = np.abs(resid) < 5 artists['resid'][i].set_xdata(vel[c0]) artists['resid'][i].set_ydata(resid[c0] * 0.05 + offset - 0.1) if self.opt.f26 is not None: artists['model'][i].set_xdata(vel) artists['model'][i].set_ydata(ymult * (model[cond] - 1) + 1 + offset) tr_id = trans['name'], tuple(trans['tr']) #import pdb; pdb.set_trace() art = self.artists['models'][tr_id] for line in self.models: m = self.models[line] if line not in art: art[line] = ax.plot(vel, ymult * (m[cond] - 1) + 1 + offset, 'k', lw=0.2)[0] else: art[line].set_xdata(vel) art[line].set_ydata(ymult * (m[cond] - 1) + 1 + offset) for ax in self.axes: ax.set_xlim(self.vmin, self.vmax) ax.set_ylim(-0.5, self.num_per_panel * 1.5) self.artists['title'].set_text('$z = %.5f$' % self.z) if not self.opt.ticklabels: for t in artists['ticklabels']: t.set_visible(False) self.fig.canvas.draw() self.artists = artists
'--c', lw=2, label=r'$J_\nu\propto\ \nu^{%.3g}$' % alpha) # plot the starburst spectrum x = (starburst.wa * u.AA).to(u.rydberg, equivalencies=u.equivalencies.spectral()) x = x.value X = x[::-1] F = starburst.logfnu[::-1] ind = X.searchsorted(1.02) from barak.convolve import convolve_psf ax2.semilogx(X, convolve_psf(F - F[ind] + N0, 50), 'r', lw=2, label='$\mathrm{starburst}$') # apply dust extinction from barak.extinction import starburst_Calzetti00 ext = starburst_Calzetti00(starburst.wa, EBmV=0.2) Fnu = np.log10(10**starburst.logfnu * np.exp(-ext.tau)) F = Fnu[::-1] ax2.semilogx(X, convolve_psf(F - F[ind] + N0, 50), 'r', lw=0.5, label='$\mathrm{starburst},\ E(B-V)=0.2$')
def update(self, z): if self.opt.f26 is not None: wa, nfl, ner, nco, model, artists, options = ( self.wa, self.nfl, self.ner, self.co, self.model, self.artists, self.opt) else: wa, nfl, ner, nco, artists, options = ( self.wa, self.nfl, self.ner, self.co, self.artists, self.opt) self.z = z zp1 = z + 1 betamin = self.vmin / c_kms betamax = self.vmax / c_kms for t in artists['ticklabels']: t.remove() for t in artists['ticks']: t.remove() artists['ticklabels'] = [] artists['ticks'] = [] if artists['ew'] is not None: artists['ew'].remove() artists['ew'] = None for r in artists['regions']: r.remove() for s in artists['sky']: s.remove() artists['regions'] = [] artists['sky'] = [] # want plots to appear from top down, so we need reversed() for i, trans in enumerate(reversed(options.linelist)): ax = self.axes[0] offset = i * 1.5 if i >= self.num_per_panel: offset = (i - self.num_per_panel) * 1.5 ax = self.axes[1] watrans = trans['wa'] obswa = watrans * zp1 wmin = obswa * (1 + 3*betamin) wmax = obswa * (1 + 3*betamax) if self.opt.showticks and len(self.allticks) > 0: ticks = self.allticks tickwmin = obswa * (1 + betamin) tickwmax = obswa * (1 + betamax) wticks = ticks.wa cond = between(wticks, tickwmin, tickwmax) if cond.any(): vel = (wticks[cond] / obswa - 1) * c_kms for j,t in enumerate(ticks[cond]): T,Tlabels = plot_tick_vel(ax, vel[j], offset, t, tickz=options.tickz) artists['ticklabels'].extend(Tlabels) artists['ticks'].extend(T) if options.f26 is not None and options.f26.regions is not None: artists['regions'].extend(plot_velocity_regions( wmin, wmax, options.f26.regions.wmin, options.f26.regions.wmax, obswa, ax, offset)) cond = between(wa, wmin, wmax) #good = ~np.isnan(fl) & (er > 0) & ~np.isnan(co) # remove ultra-low S/N regions to help plotting cond &= ner < 1.5 fl = nfl[cond] co = nco[cond] vel = (wa[cond] / obswa - 1) * c_kms vranges = [] for w0, w1 in unrelated: c0 = between(wa[cond], w0, w1) if c0.any(): vranges.append(c0) for w0, w1 in ATMOS: c0 = between(wa[cond], w0, w1) if c0.any(): artists['sky'].append( ax.fill_between(vel[c0], offset, offset + 1.5, facecolor='0.9', lw=0)) if self.smoothby > 1: if len(fl) > 3 * self.smoothby: fl = convolve_psf(fl, self.smoothby) artists['fl'][i].set_xdata(vel) artists['fl'][i].set_ydata(fl + offset) artists['co'][i].set_xdata(vel) artists['co'][i].set_ydata(co + offset) #pdb.set_trace() if self.opt.residuals: resid = (fl - model[cond]) / ner[cond] c0 = np.abs(resid) < 5 artists['resid'][i].set_xdata(vel[c0]) artists['resid'][i].set_ydata(resid[c0] * 0.05 + offset - 0.1) if self.opt.f26 is not None: artists['model'][i].set_xdata(vel) artists['model'][i].set_ydata(model[cond] + offset) for ax in self.axes: ax.set_xlim(self.vmin, self.vmax) ax.set_ylim(-0.5, self.num_per_panel * 1.5) self.artists['title'].set_text('$z = %.5f$' % self.z) if not self.opt.ticklabels: for t in artists['ticklabels']: t.set_visible(False) self.fig.canvas.draw() self.artists = artists
def update(self, z): if self.opt.f26 is not None: wa, nfl, ner, nco, model, artists, options = ( self.wa, self.nfl, self.ner, self.co, self.model, self.artists, self.opt) else: wa, nfl, ner, nco, artists, options = ( self.wa, self.nfl, self.ner, self.co, self.artists, self.opt) self.z = z ymult = self.ymult zp1 = z + 1 betamin = self.vmin / c_kms betamax = self.vmax / c_kms for t in artists['ticklabels']: t.remove() for t in artists['ticks']: t.remove() artists['ticklabels'] = [] artists['ticks'] = [] if artists['ew'] is not None: artists['ew'].remove() artists['ew'] = None for r in artists['regions']: r.remove() for s in artists['sky']: s.remove() for s in artists['aod']: s.remove() artists['regions'] = [] artists['sky'] = [] artists['aod'] = [] # want plots to appear from top down, so we need reversed() for i, trans in enumerate(reversed(options.linelist)): atom, ion = split_trans_name(trans['name'].split()[0]) iax, ioff = divmod(i, self.num_per_panel) ax = self.axes[iax] offset = ioff * 1.5 #if i >= self.num_per_panel: # offset = (i - self.num_per_panel) * 1.5 # ax = self.axes[1] watrans = trans['wa'] obswa = watrans * zp1 wmin = obswa * (1 + 3*betamin) wmax = obswa * (1 + 3*betamax) if self.opt.showticks and len(self.allticks) > 0: ticks = self.allticks tickwmin = obswa * (1 + betamin) tickwmax = obswa * (1 + betamax) wticks = ticks.wa cond = between(wticks, tickwmin, tickwmax) if cond.any(): vel = (wticks[cond] / obswa - 1) * c_kms for j,t in enumerate(ticks[cond]): T,Tlabels = plot_tick_vel(ax, vel[j], offset, t, tickz=options.tickz) artists['ticklabels'].extend(Tlabels) artists['ticks'].extend(T) cond = between(wa, wmin, wmax) #good = ~np.isnan(fl) & (er > 0) & ~np.isnan(co) # remove ultra-low S/N regions to help plotting cond &= ner < 1.5 cond &= ymult*(nfl-1) + 1 > -0.1 fl = nfl[cond] co = nco[cond] vel = (wa[cond] / obswa - 1) * c_kms if options.f26 is not None and options.f26.regions is not None: artists['regions'].extend(plot_velocity_regions( wmin, wmax, options.f26.regions.wmin, options.f26.regions.wmax, obswa, ax, offset, vel, ymult*(fl-1) + 1)) #import pdb; pdb.set_trace() if hasattr(options, 'aod'): # print ranges used for AOD calculation. # find the right transition c0 = ((np.abs(options.aod['wrest'] - trans['wa']) < 0.01) & (options.aod['atom'] == atom)) itrans = np.flatnonzero(c0) for row in options.aod[itrans]: c0 = between(wa[cond], row['wmin'], row['wmax']) if np.any(c0): artists['aod'].append( ax.fill_between(vel[c0], offset, offset+1.5, facecolor='0.8', lw=0, zorder=0) ) vranges = [] for w0, w1 in unrelated: c0 = between(wa[cond], w0, w1) if c0.any(): vranges.append(c0) for w0, w1 in ATMOS: c0 = between(wa[cond], w0, w1) if c0.any(): artists['sky'].append( ax.fill_between(vel[c0], offset, offset + 1.5, facecolor='0.9', lw=0)) if self.smoothby > 1: if len(fl) > 3 * self.smoothby: fl = convolve_psf(fl, self.smoothby) artists['fl'][i].set_xdata(vel) artists['fl'][i].set_ydata(ymult*(fl-1) + 1 + offset) artists['co'][i].set_xdata(vel) artists['co'][i].set_ydata(ymult*(co-1) + 1 + offset) #pdb.set_trace() if self.opt.residuals: resid = (fl - model[cond]) / ner[cond] c0 = np.abs(resid) < 5 artists['resid'][i].set_xdata(vel[c0]) artists['resid'][i].set_ydata(resid[c0] * 0.05 + offset - 0.1) if self.opt.f26 is not None: artists['model'][i].set_xdata(vel) artists['model'][i].set_ydata(ymult*(model[cond]-1) + 1 + offset) tr_id = trans['name'], tuple(trans['tr']) #import pdb; pdb.set_trace() art = self.artists['models'][tr_id] for line in self.models: m = self.models[line] if line not in art: art[line] = ax.plot(vel, ymult*(m[cond]-1) + 1 + offset, 'k', lw=0.2)[0] else: art[line].set_xdata(vel) art[line].set_ydata(ymult*(m[cond]-1) + 1 + offset) for ax in self.axes: ax.set_xlim(self.vmin, self.vmax) ax.set_ylim(-0.5, self.num_per_panel * 1.5) self.artists['title'].set_text('$z = %.5f$' % self.z) if not self.opt.ticklabels: for t in artists['ticklabels']: t.set_visible(False) self.fig.canvas.draw() self.artists = artists
# comoponents in wings? npix = 1.39 / dw * 0.8 tau, ticks = find_tau(wa, lines, atom) taulo, ticks = find_tau(wa, lineslo, atom) tauhi, ticks = find_tau(wa, lineshi, atom) fig = pl.figure(figsize=(4.3, 3)) fig.subplots_adjust(bottom=0.16, left=0.16, right=0.98, top=0.96) ax = fig.add_subplot(111) ax.plot(wa, sp.fl,color='0.3', lw=0.5,drawstyle='steps-mid') ax.plot(wa, sp.co, '0.7', ls='dashed') ax.plot(wa, sp.co*convolve_psf(np.exp(-taulo), npix), 'r', lw=0.3) ax.plot(wa, sp.co*convolve_psf(np.exp(-tau), npix), 'r') ax.plot(wa, sp.co*convolve_psf(np.exp(-tauhi), npix), 'r', lw=0.3) ax.axhline(0, color='k', lw=0.3) ax.minorticks_on() ax.set_xlim(1881, 1904.9) #ax.set_xlim(1861, 1924.9) ymax = 3.49 resid = (sp.fl - sp.co*convolve_psf(np.exp(-tau), npix)) / sp.er ax.axhline((-0.12-0.03)*ymax, color='0.7', lw=0.5) ax.axhline((-0.12+0.03)*ymax, color='0.7', lw=0.5) ax.scatter(wa, -0.12*ymax + 0.03*ymax*resid, c='g', marker='.', s=20, faceted=False) ax.set_ylim(-0.22*ymax, ymax) ax.set_xlabel('$\mathrm{Wavelength}\ (\AA)$') pl.figtext(0.012, 0.55,