def comparespecs(stars, **kwargs): """ Parameters ---------- binfunc : function or 'all', optional Function for regriding spectbls (such as evenbin or powerbin from reduce). Default is powerbin from 1000 to 5000 AA with R=500.0 Set to 'all' to use the entire spectrum. axkw """ if 'binfunc' in kwargs: binfunc = kwargs['binfunc'] del kwargs['binfunc'] else: binfunc = lambda s: utils.powerbin(s, R=5000.0, lo=1100.0, hi=5000.0) plts = [] for star in stars: # read in panspectrum specfile = db.panpath(star) spec = io.read(specfile)[0] # interpolate it onto the desired bins if binfunc != 'all': spec = binfunc(spec) # plot plts.append(normspec(spec, **kwargs)) plt.legend(plts, stars)
def phxCompare(star, wlim=None, maxpow=None, mindw=None, ax=None): if ax is None: ax = plt.gca() xf = db.findfiles('ir', 'phx', star, fullpaths=True) pf = db.panpath(star) phx = io.read(xf)[0] pan = io.read(pf)[0] normfac = pan['normfac'][-1] if wlim is not None: phx, pan = utils.keepranges(phx, wlim), utils.keepranges(pan, wlim) if maxpow or mindw: pan = utils.fancyBin(pan, maxpow=maxpow, mindw=mindw) phx = utils.fancyBin(phx, maxpow=maxpow, mindw=mindw) Fbol = utils.bolo_integral(star) pan['normflux'] = pan['flux'] / Fbol pan['normerr'] = pan['error'] / Fbol phx['normflux'] = phx['flux'] * normfac / Fbol line = specstep(pan, key='normflux', label='Panspec', ax=ax) specstep(pan, key='normerr', label='Panspec Error', ax=ax, color=line.get_color(), ls=':') specstep(phx, key='normflux', label='Phoenix', ax=ax, color='r', alpha=0.5) ax.set_xlim(wlim) ax.legend(loc='best')
def stackpans(range=[1310, 1350], keeprange=[1100, 2000], xlim=None, norm=True, offfac=1.0): if xlim is None: xlim = keeprange pans = [] for star in rc.observed: p = io.read(db.panpath(star))[0] pans.append(utils.keepranges(p, *keeprange)) stackspecs(pans, range, norm=norm, offfac=offfac, xlim=xlim)
def phx_compare_single(star): pan = io.read(db.panpath(star))[0] xf = db.findfiles('ir', 'phx', star, fullpaths=True) phx = io.read(xf)[0] phx['flux'] *= pan['normfac'][-1] bands = [rc.fuv, rc.nuv, [rc.vis[0], 5700.]] (pff, pfe) , (pnf, pne), (pvf, pve) = [utils.flux_integral(pan, *b) for b in bands] (xff, _) , (xnf, _), (xvf, _) = [utils.flux_integral(phx, *b) for b in bands] return ((pff - xff)/pff, pfe/pff), ((pnf - xnf)/pnf, pne/pnf), ((pvf - xvf)/pvf, pve/pvf)
def compareEUV(star): euvfile = db.findfiles('u', 'euv', star, fullpaths=True)[0] w, f, _ = np.loadtxt(euvfile).T I0 = np.trapz(f, w) print 'Trapz from Allison\'s file: %g' % I0 spec = io.read(euvfile)[0] I1 = np.sum((spec['w1'] - spec['w0']) * spec['flux']) print 'Direct integration from spectbl version of Allison\'s file: %g' % I1 I2 = np.trapz(spec['flux'], (spec['w0'] + spec['w1']) / 2.0) print 'Trapz from spectbl version of Allison\'s file: %g' % I2 p = io.read(db.panpath(star))[0] keep = p['instrument'] == rc.getinsti('mod_euv_young') p = p[keep] I3 = np.sum((spec['w1'] - spec['w0']) * spec['flux']) print 'Direct integration from panspec EUV portion: %g' % I3
def vetpanspec(pan_or_star, constant_dw=None, redlim=8000.0): """Plot unnormalized components of the panspec with the panspec to see that all choices were good. Phoenix spectrum is excluded because it is too big.""" if type(pan_or_star) is str: star = pan_or_star panspec = io.read(db.panpath(star))[0] else: panspec = pan_or_star star = panspec.meta['STAR'] files = db.panfiles(star)[0] panspec = utils.keepranges(panspec, 0.0, redlim) if constant_dw is None: plotfun = specstep else: panspec = utils.evenbin(panspec, constant_dw) wbins = utils.wbins(panspec) def plotfun(spec, **kwargs): s = utils.rebin(spec, wbins) return specstep(s, **kwargs) for f in files: if 'phx' in f: continue specs = io.read(f) for spec in specs: p = plotfun(spec, err=True)[0] x = (spec['w0'][0] + spec['w0'][-1]) / 2.0 y = np.mean(spec['flux']) inst = db.parse_instrument(spec.meta['NAME']) plt.text(x, y, inst, bbox={ 'facecolor': 'w', 'alpha': 0.5, 'color': p.get_color() }, ha='center', va='center') plotfun(panspec, color='k', alpha=0.5) ymax = np.max(utils.keepranges(panspec, 3000.0, 8000.0)['flux']) plt.gca().set_ylim(-0.01 * ymax, 1.05 * ymax) plt.draw()
def bolo_integral(star_or_panspec, uplim=np.inf): """Compute the integral of all flux for the star.""" if star_or_panspec == 'sun': return rc.insolation if type(star_or_panspec) is str: star = star_or_panspec pan = io.read(db.panpath(star))[0] else: pan = star_or_panspec star = pan.meta['STAR'] if star == 'sun': return rc.insolation fit_unnormed = blackbody_fit(star) normfac = pan[-1]['normfac'] Ibody = flux_integral(pan)[0] Itail = normfac * quad(fit_unnormed, pan['w1'][-1], uplim)[0] I = Ibody + Itail return I
def spectralAnatomy( ax=plt.gca(), star='gj832', scale_fac=100., shortlabels=False): """Takes a set of two vertically stacked axes and plots some info. Adjusting the plot spacing is up to the user.""" # make plot of full spectrum spec = io.read(db.panpath(star))[0] spec_rebin = utils.fancyBin(spec, maxpow=30000) ymax = np.max(spec_rebin['flux'][spec_rebin['w1'] > 3000.]) ax.set_xscale('log') (ax0, line0), (ax1, line1) = splitSpec(spec_rebin, ax=ax, wsplit=3000., scale_fac=scale_fac) xlim = 5.0, 55000. ax0.set_xscale('log') ax1.set_xscale('log') ax0.set_xlim(xlim) ax1.set_xlim(xlim) # add instrument ranges to the top plot txt_offset_frac = 0.25 # first get size of text in axis units to put the bars in the right place h_txt_pts = mpl.rcParams['font.size'] pts2axx, pts2axy = pu.textSize(ax, 'axes') h_txt_ax = pts2axy * h_txt_pts y_bar_ax = 1 - h_txt_ax * 2 - 0.05 y_bot_ax = y_bar_ax - 0.05 # shrink data lines and add whitespace for the bars shrink_fac = y_bot_ax ylim = ymax / shrink_fac ax0.set_ylim(0.0, ylim / scale_fac) ax1.set_ylim(0.0, ylim) pts2datax, pts2datay = pu.textSize( ax0, 'data') # remember that pts2datax will be in log space y_bot_data = y_bot_ax * ax0.get_ylim()[1] ax0.axhspan(y_bot_data, ylim, fc='w', ec='w', alpha=0.7, zorder=3) # now add the bars for the instruments of note # coordinate crap h_txt_data = h_txt_pts * pts2datay y_bar_data = y_bar_ax * ax0.get_ylim()[1] # y_txt_data = y_bar_data + 0.05 * ylim/scale_fac # now add the bars insts = ['xobs', 'apec', 'euv', 'hst', 'phx'] rngs = [rc.instranges[key] for key in insts] instnames = rc.instabbrevs if shortlabels else rc.instnames labels = [instnames[key] for key in insts] [ rangebar(ylim * 0.9, r, l, ax=ax1, lbl_offset=ylim * 0.02) for r, l in zip(rngs, labels) ] # annotate lya model and Mg II lines def vtxt(w, label): ax0.text(w, y_bot_data, label + ' ', ha='right', va='top', rotation='vertical') vtxt(1215, 'Reconstructed Ly$\\alpha$') vtxt(2793, 'Mg$\ $II') ax0.set_ylabel(fluxlabel) ax1.set_ylabel(fluxlabel) ax0.set_xlabel('Wavelength [$\AA$]') plt.draw()
def readpan(star): if star == 'sun': return read_solar() return read(db.panpath(star))[0]