def make_spec_plot(s, fname, smooth_factor=10, angles=True, components=False, with_composite=False): ''' make a spectrum plot from astropy.table.table.Table object Parameters ---------- s: astropy.table.table.Table table containing spectrum data outputted from Python fname: str filename to save as e.g. sv smooth_factor: int factor you would like to smooth by, default 10 angles: Bool Would you like to plot the viewing angle spectra? components: Bool would you like to plot the individual components e.g. Disk Wind Returns ---------- Success returns 0 Failure returns 1 Saves output as "spectrum_%s.png" % (fname) ''' if type(s) != astropy.table.table.Table: raise TypeError( "make_spec_plot takes astropy.table.table.Table object as first arg" ) return 1 if s.colnames[8] != "Scattered": print("Warning- colnames are not in expected order! {} != Scattered". format(s.colnames[8])) ncomponents = 9 if angles: # first make viewing angle plot p.figure(figsize=(8, 12)) nspecs = len(s.dtype.names) - ncomponents nx = 1 ny = nspecs if nspecs > 4: nx = 2 ny = (1 + nspecs) / nx print("Making a {} by {} plot, {} spectra".format(nx, ny, nspecs)) if with_composite: lambda_composite, f_composite, errors = np.loadtxt( "%s/examples/telfer_qso_composite_hst.asc" % (os.environ["PYTHON"]), unpack=True, comments="#") for i in range(nspecs): p.subplot(ny, nx, i + 1) if with_composite: f_1100 = util.get_flux_at_wavelength( s["Lambda"], s[s.dtype.names[ncomponents + i]], 1100.0) p.plot(s["Lambda"], util.smooth(s[s.dtype.names[ncomponents + i]] / f_1100, window_len=smooth_factor), label="Model") p.plot(lambda_composite, f_composite, label="HST composite") p.legend() else: p.plot( s["Lambda"], util.smooth(s[s.dtype.names[ncomponents + i]], window_len=smooth_factor)) p.title(s.dtype.names[ncomponents + i]) p.xlabel("Wavelength") p.ylabel("Flux") p.savefig("spectrum_%s.png" % (fname), dpi=300) p.clf() if components: p.figure(figsize=(8, 12)) p.subplot(211) p.plot(s["Lambda"], util.smooth(s["Created"], window_len=smooth_factor), label="Created") p.plot(s["Lambda"], util.smooth(s["Emitted"], window_len=smooth_factor), label="Emitted") p.legend() p.subplot(212) for i in range(4, 9): p.plot(s["Lambda"], util.smooth(s[s.dtype.names[i]], window_len=smooth_factor), label=s.dtype.names[i]) p.xlabel("Wavelength") p.ylabel("Flux") p.legend() p.savefig("spec_components_%s.png" % (fname), dpi=300) p.clf() return 0
def make_spec_plot_from_class(s, fname, smooth_factor=10, angles=True, components=False): ''' make a spectrum plot from py_classes.specclass object Parameters ---------- s: specclass object table containing spectrum data outputted from Python fname: str filename to save as e.g. sv smooth_factor: int factor you would like to smooth by, default 10 angles: Bool Would you like to plot the viewing angle spectra? components: Bool would you like to plot the individual components e.g. Disk Wind Returns ---------- Success returns 0 Failure returns 1 Saves output as "spectrum_%s.png" % (fname) ''' if angles: # first make viewing angle plot p.figure(figsize=(8, 12)) nspecs = len(s.spec) nx = 1 ny = nspecs if nspecs > 4: nx = 2 ny = (1 + nspecs) / nx print("Making a {} by {} plot, {} spectra".format(nx, ny, nspecs)) for i in range(nspecs): p.subplot(ny, nx, i + 1) p.plot(s.wavelength, util.smooth(s.spec[i], window_len=smooth_factor)) p.xlabel("Wavelength") p.ylabel("Flux") p.savefig("spectrum_%s.png" % (fname)) p.clf() if components: p.figure(figsize=(8, 12)) p.subplot(211) p.plot(s.wavelength, util.smooth(s.created, window_len=smooth_factor), label="Created") p.plot(s.wavelength, util.smooth(s.emitted, window_len=smooth_factor), label="Emitted") p.subplot(212) p.plot(s.wavelength, util.smooth(s.censrc, window_len=smooth_factor), label="CenSrc") p.plot(s.wavelength, util.smooth(s.disk, window_len=smooth_factor), label="Disk") p.plot(s.wavelength, util.smooth(s.wind, window_len=smooth_factor), label="Wind") p.plot(s.wavelength, util.smooth(s.hitsurf, window_len=smooth_factor), label="HitSurf") p.plot(s.wavelength, util.smooth(s.scattered, window_len=smooth_factor), label="Scattered") p.xlabel("Wavelength") p.ylabel("Flux") p.legend() p.savefig("spec_components_%s.png" % (fname)) p.clf() return 0
def make_spec_comparison_plot(s_array, labels, fname="comparison", smooth_factor=10, angles=True, components=False): ''' make a spectrum comparison plot from array of astropy.table.table.Table objects Parameters ---------- s_array: array-like of astropy.table.table.Table objects table containing spectrum data outputted from Python labels: array-like strings of labels for each spectrum fname: str filename to save as e.g. sv smooth_factor: int factor you would like to smooth by, default 10 angles: Bool Would you like to plot the viewing angle spectra? components: Bool would you like to plot the individual components e.g. Disk Wind Returns ---------- Success returns 0 Failure returns 1 Saves output as "spectrum_%s.png" % (fname) ''' ncomponents = 9 if angles: # first make viewing angle plot p.figure(figsize=(8, 12)) nspecs = len(s_array[0].dtype.names) - ncomponents nx = 1 ny = nspecs if nspecs > 4: nx = 2 ny = (1 + nspecs) / nx print("Making a {} by {} comparison plot, {} spectra".format( nx, ny, nspecs)) for j in range(len(s_array)): for i in range(nspecs): p.subplot(ny, nx, i + 1) p.plot(s_array[j]["Lambda"], util.smooth( s_array[j][s_array[j].dtype.names[ncomponents + i]], window_len=smooth_factor), label=labels[j]) p.xlabel("Wavelength") p.ylabel("Flux") if i == 0 and j == (len(s_array) - 1): p.legend() p.savefig("spectrum_%s.png" % (fname), dpi=300) p.clf() if components: p.figure(figsize=(8, 12)) n = len(s_array) for j in range(n): p.subplot(j + 1, 1, 1) s = s_array[j] p.plot(s["Lambda"], util.smooth(s["Created"], window_len=smooth_factor), label="Created") p.plot(s["Lambda"], util.smooth(s["Emitted"], window_len=smooth_factor), label="Emitted") for i in range(4, 9): p.plot(s["Lambda"], util.smooth(s[s.dtype.names[i]], window_len=smooth_factor), label=s.dtype.names[i]) p.xlabel("Wavelength") p.ylabel("Flux") p.legend() p.savefig("spec_components_%s.png" % (fname), dpi=300) p.clf() return 0
# spectrum_wl = False # except KeyError or IndexError: # spectrum_wl = False spectrum = spectrum_wave if spectrum: ly_alpha_mask = (np.fabs(wrest - 1215.0) > 2.0) flux_mask = (flux > 0.0) #line_mask = (np.fabs(w - 1550.) < 300.0) mask = flux_mask * ly_alpha_mask wuse = w[mask] fuse = util.smooth(flux[mask]) try: wmin = min(wuse) wmax = max(wuse) except ValueError: spectrum = False #spectrum = False if spectrum: # flags to decide if we print out or not bal_flags_this_file = np.zeros(len(lines))