def read_transitions(filename, atomdat): print 'Reading transitions from', filename with open(filename) as fh: linelist = [] for tr in fh: tr = tr.strip() if tr and not tr.startswith('#'): name, t = findtrans(tr, atomdat=atomdat)) temp.append(dict(name=name, wa=t['wa'], ) return linelist
def read_transitions(fh, atomdat): """ Read transitions from a file Parameters ---------- fh : str or file object atomdat : atom.dat object Read with `barak.absorb.readatom()` Returns ------- linelist : list of atom.dat entries Atom.dat entry for each transition Notes ----- Example file format: HI 1215 CIV 1549 CIV 1550 MgII 2796 MgII 2803 """ if isinstance(fh, basestring): print 'Reading transitions from', fh fh = open(fh, 'rt') linelist = [] for tr in fh: tr = tr.strip() if tr and not tr.startswith('#'): name, t = findtrans(tr, atomdat=atomdat) linelist.append((name, t)) fh.close() return linelist
def process_options(args): opt = adict() filename = os.path.abspath(__file__).rsplit('/', 1)[0] + '/default.cfg' opt = parse_config(filename) if os.path.lexists('./plot.cfg'): opt = parse_config('./plot.cfg', opt) opt.atom = readatom(molecules=True) if opt.Rfwhm is not None: if isinstance(opt.Rfwhm, basestring): if opt.Rfwhm == 'convolve_with_COS_FOS': if convolve_with_COS_FOS is None: raise ValueError('convolve_with_COS_FOS() not available') print('Using tailored FWHM for COS/FOS data') opt.Rfwhm = 'convolve_with_COS_FOS' elif opt.Rfwhm.endswith('fits'): print('Reading Resolution FWHM from', opt.Rfwhm) res = readtabfits(opt.Rfwhm) opt.Rfwhm = res.res / 2.354 else: print('Reading Resolution FWHM from', opt.Rfwhm) fh = open(opt.Rfwhm) opt.Rfwhm = 1 / 2.354 * np.array([float(r) for r in fh]) fh.close() else: opt.Rfwhm = float(opt.Rfwhm) if opt.features is not None: print('Reading feature list from', opt.features) opt.features = readtabfits(opt.features) if opt.f26 is not None: name = opt.f26 print('Reading ions and fitting regions from', name) opt.f26 = readf26(name) opt.f26.filename = name if opt.transitions is not None: print('Reading transitions from', opt.transitions) fh = open(opt.transitions) trans = list(fh) fh.close() temp = [] for tr in trans: tr = tr.strip() if tr and not tr.startswith('#'): junk = tr.split() tr = junk[0] + ' ' + junk[1] t = findtrans(tr, atomdat=opt.atom) temp.append(dict(name=t[0], wa=t[1][0], tr=t[1])) opt.linelist = temp else: opt.linelist = readtxt(get_data_path() + 'linelists/qsoabs_lines', names='wa,name,select') if opt.f26 is None and opt.taulines is not None: print('Reading ions from', opt.taulines) fh = open(opt.taulines) lines = [] for row in fh: if row.lstrip().startswith('#'): continue items = row.split() lines.append([items[0]] + list(map(float, items[1:]))) fh.close() opt.lines = lines if opt.show_regions is None: opt.show_regions = True if hasattr(opt, 'aodname'): opt.aod = Table.read(opt.aodname) return opt
def plot_model(pars): """ `regions`, `x` must be defined in the model.py module namespace """ from run_emcee.plot_mcmc import get_fig_axes, get_nrows_ncols linepars = pars dz = 0 if options['wa_vary']: linepars, regpars = pars[:-len(regions)], pars[-len(regions):] lines = copy_par_to_lines(linepars, lines_vary) nrows, ncols = get_nrows_ncols(len(regions)) fig, axes = get_fig_axes(nrows, ncols, len(regions), width=8.2) fig.subplots_adjust(wspace=1e-6, hspace=1e-6, right=1, top=1, left=0.07, bottom=0.06) z0 = lines.z[1] zp1 = lines.z.mean() colours = dict(J0='purple',J1='g',J2='r',J3='b') for ind, (wa, tau0, sp, (i, j), tname) in enumerate(regions): tau = tau0.copy() for l in lines: if options['wa_vary']: #import pdb; pdb.set_trace() dz = regpars[ind] / wa[len(wa)//2] * (l.z + 1) #print l.z, dz, l.logN trans = atom[l.name.strip()] tau += calc_iontau(wa, trans, l.z+1 + dz, l.logN, l.b) dwpix = wa[1] - wa[0] flmodel0 = convolve_with_COS_FOS(np.exp(-tau), wa, dwpix) ymod = np.interp(sp.wa[i:j], wa, flmodel0) ax = axes[ind] i1 = max(i-50, 0) j1 = min(j+50, len(sp.wa)-1) nfl = sp.fl[i1:j1] / sp.co[i1:j1] ner = sp.er[i1:j1] / sp.co[i1:j1] tstr, t = findtrans(tname, atom) obswa = t.wa * (1 + z0) dv = c_kms * (sp.wa[i:j] / obswa - 1) dv1 = c_kms * (sp.wa[i1:j1] / obswa - 1) tstr = tstr[2:] ax.axhline(0, color='0.5',lw=0.5) ax.axhline(1, color='k', lw=0.5, ls='--') ax.fill_between([-150, 150], -0.35, -0.15, color='0.9') ax.axhline(-0.25, color='k', lw=0.25) ax.plot(dv1, nfl, color='0.7', lw=0.5, drawstyle='steps-mid') ax.plot(dv, sp.fl[i:j]/sp.co[i:j], 'k', lw=0.5, drawstyle='steps-mid') ax.plot(dv, ymod, color='0.3') #ax.axvline(0, color='0.5', lw=0.5) ax.plot([-23]*2, [1.1, 1.4], '0.5') ax.plot([0]*2, [1.1, 1.4], '0.5') puttext(0.95, 0.95, tstr, ax, fontsize=9, va='top', ha='right', color=colours[tstr[:2]]) resid = (sp.fl[i:j]/sp.co[i:j] - ymod) / sp.er[i:j] * sp.co[i:j] ax.plot(dv, -0.25 + resid*0.1, '.', color='0.3', ms=2) ax.set_ylim(-0.5, 1.9) ax.set_xlim(-120, 120) ax.set_yticks([0, 0.5, 1, 1.5]) ax.set_yticklabels(['0.0', '0.5', '1.0', '1.5']) ax.set_xticks([-100, -50, 0, 50, 100]) ax.set_xticklabels(['', '-50', '0', '50', '']) if ind+1 < (ncols*(nrows-1)): ax.set_xticklabels([]) if ind % ncols: ax.set_yticklabels([]) fig.text(0.5, 0.00, 'Velocity offset (km/s)', fontsize=12, ha='center',va='bottom') fig.text(0.015, 0.5, 'Transmission', fontsize=12, rotation=90,va='center') ndf = len(ydata) - len(pars) print (((ydata - ymodel(x, pars))/ ysigma) **2).sum() / ndf pl.savefig('fig/model.pdf') pl.savefig('fig/model.png',dpi=300) return fig