def test_mileshc(): tpl = TemplateLibrary('MILESHC', match_resolution=False, velscale_ratio=4, spectral_step=1e-4, log=True, hardcopy=False) # Shape assert tpl.ntpl == 42, 'Incorrect number of templates' assert tpl['FLUX'].data.shape == ( 42, 12634), 'Incorrect shape of the flux array' # Wavelength and velocity coordinates assert numpy.isclose(spectral_coordinate_step(tpl['WAVE'].data, log=True), 1e-4/4), \ 'Coordinate step is wrong' assert numpy.isclose(spectrum_velocity_scale(tpl['WAVE'].data), astropy.constants.c.to('km/s').value*1e-4*numpy.log(10.)/4), \ 'Velocity step is wrong' # Mask nmasked = {} for k in tpl.bitmask.keys(): nmasked[k] = numpy.sum(tpl.bitmask.flagged(tpl['MASK'].data, flag=k)) assert numpy.sum(list(nmasked.values())) == 192, 'Masking has changed' assert nmasked['NO_DATA'] == 0, 'There should be no NO_DATA flags' # Flux values flux = numpy.ma.MaskedArray(tpl['FLUX'].data, mask=tpl['MASK'].data > 0) assert numpy.isclose(numpy.ma.mean(flux), 1.0), 'Templates should be normalized'
def fom_lambda(plt, ifu, wave, flux, error, model, fit='sc', wave_limits=None, ofile=None): if wave_limits is not None: indx = (wave < wave_limits[0]) | (wave > wave_limits[1]) flux[indx, :] = numpy.ma.masked error[indx, :] = numpy.ma.masked model[indx, :] = numpy.ma.masked else: indx = numpy.arange(len(wave))[numpy.any(numpy.invert(model.mask), axis=1)] wave_limits = [wave[indx[0]], wave[indx[-1]]] nspec = flux.shape[1] resid = numpy.ma.absolute(flux - model) fresid = numpy.ma.absolute(numpy.ma.divide(resid, model)) chi = numpy.ma.absolute(numpy.ma.divide(resid, error)) snr = numpy.ma.divide(flux, error) mean_flux = numpy.ma.mean(flux, axis=1) mean_snr = numpy.ma.mean(snr, axis=1) mean_resid = numpy.ma.mean(resid, axis=1) mean_fresid = numpy.ma.mean(fresid, axis=1) mean_chi = numpy.ma.mean(chi, axis=1) bc = BoxcarFilter(100) resid_sm = bc.smooth(resid) fresid_sm = bc.smooth(fresid) chi_sm = bc.smooth(chi) spec_lim = [-10, nspec + 10] flux_lim = numpy.exp( growth_lim(numpy.ma.log(mean_flux).compressed(), 0.99, fac=2.0)) snr_lim = numpy.exp( growth_lim(numpy.ma.log(mean_snr).compressed(), 0.99, fac=2.0)) resid_lim = numpy.exp( growth_lim(numpy.ma.log(mean_resid).compressed(), 0.99, fac=2.0)) fresid_lim = numpy.exp( growth_lim(numpy.ma.log(mean_fresid).compressed(), 0.99, fac=2.0)) chi_lim = numpy.exp( growth_lim(numpy.ma.log(mean_chi).compressed(), 0.99, fac=2.0, midpoint=0)) l0 = numpy.log10(wave[0]) dl = spectral_coordinate_step(wave, log=True) wave_bins = numpy.power(10, l0 - dl / 2 + dl * numpy.arange(len(wave) + 1)) spec_bins = 0.5 + numpy.arange(nspec + 1) font = {'size': 10} rc('font', **font) w, h = pyplot.figaspect(1) fig = pyplot.figure(figsize=(1.5 * w, 1.5 * h)) ax = init_ax(fig, [0.2, 0.87, 0.60, 0.12], facecolor='0.95', grid=True) ax.set_xlim(wave_limits) ax.set_ylim(flux_lim) ax.set_yscale('log') ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.plot(wave, numpy.ma.mean(flux, axis=1), color='k', lw=0.5) ax.text(-0.15, 0.5, r'Flux', horizontalalignment='center', verticalalignment='center', rotation='vertical', transform=ax.transAxes) axt = ax.twinx() axt.set_xlim(wave_limits) axt.set_ylim(snr_lim) axt.set_yscale('log') axt.tick_params(axis='y', colors='0.5') axt.xaxis.set_major_formatter(ticker.NullFormatter()) axt.plot(wave, numpy.ma.mean(snr, axis=1), color='0.5', lw=0.5) axt.text(1.1, 0.5, r'S/N', horizontalalignment='center', verticalalignment='center', rotation='vertical', transform=axt.transAxes, color='0.5') ax = init_ax(fig, [0.2, 0.75, 0.60, 0.12], facecolor='0.95', grid=True) ax.set_xlim(wave_limits) ax.set_ylim(resid_lim) ax.set_yscale('log') ax.xaxis.set_major_formatter(ticker.NullFormatter()) # ax.plot(wave, numpy.ma.mean(resid_sm, axis=0), color='C3') ax.plot(wave, numpy.ma.mean(resid, axis=1), color='k', lw=0.5) ax.text(-0.15, 0.5, r'$|\Delta|$', horizontalalignment='center', verticalalignment='center', rotation='vertical', transform=ax.transAxes) ax = init_ax(fig, [0.2, 0.63, 0.60, 0.12], facecolor='0.95', grid=True) ax.set_xlim(wave_limits) ax.set_ylim(fresid_lim) ax.set_yscale('log') ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.plot(wave, numpy.ma.mean(fresid, axis=1), color='k', lw=0.5) ax.text(-0.15, 0.5, r'$|\Delta|/m$', horizontalalignment='center', verticalalignment='center', rotation='vertical', transform=ax.transAxes) ax = init_ax(fig, [0.2, 0.51, 0.60, 0.12], facecolor='0.95', grid=True) ax.set_xlim(wave_limits) ax.set_ylim(chi_lim) ax.set_yscale('log') ax.plot(wave, numpy.ma.mean(chi, axis=1), color='k', lw=0.5) ax.text(0.5, -0.41, r'Wavelength ($\AA$)', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes) ax.text(-0.15, 0.5, r'$|\Delta|/\epsilon$', horizontalalignment='center', verticalalignment='center', rotation='vertical', transform=ax.transAxes) label = '{0}-{1}'.format(plt, ifu) label += (' STELLAR' if fit == 'sc' else ' EMLINE') ax.text(1.23, -0.3, label, horizontalalignment='center', verticalalignment='bottom', rotation='vertical', transform=ax.transAxes, fontsize=22) ax = init_ax(fig, [0.07, 0.23, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.xaxis.set_major_formatter(ticker.NullFormatter()) im = ax.pcolormesh(wave_bins, spec_bins, resid.T, norm=colors.LogNorm(vmin=resid_lim[0], vmax=resid_lim[1]), cmap='viridis', zorder=4, lw=0, rasterized=True) ax = init_ax(fig, [0.07, 0.07, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.pcolormesh(wave_bins, spec_bins, resid_sm.T, norm=colors.LogNorm(vmin=resid_lim[0], vmax=resid_lim[1]), cmap='viridis', zorder=4, lw=0, rasterized=True) cax = fig.add_axes([0.07, 0.395, 0.20, 0.01]) cb = pyplot.colorbar( im, cax=cax, orientation='horizontal') #, format=FormatStrFormatter('%d')) cb.ax.tick_params(axis='x', which='both', bottom=False, top=True, labelbottom=False, labeltop=True) cax.text(1.05, 0.7, r'$|\Delta|$', horizontalalignment='left', verticalalignment='center', transform=cax.transAxes, fontsize=10) ax = init_ax(fig, [0.37, 0.23, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.yaxis.set_major_formatter(ticker.NullFormatter()) im = ax.pcolormesh(wave_bins, spec_bins, fresid.T, norm=colors.LogNorm(vmin=fresid_lim[0], vmax=fresid_lim[1]), cmap='viridis', zorder=4, lw=0, rasterized=True) ax = init_ax(fig, [0.37, 0.07, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.yaxis.set_major_formatter(ticker.NullFormatter()) ax.pcolormesh(wave_bins, spec_bins, fresid_sm.T, norm=colors.LogNorm(vmin=fresid_lim[0], vmax=fresid_lim[1]), cmap='viridis', zorder=4, lw=0, rasterized=True) ax.text(0.5, -0.30, r'Wavelength ($\AA$)', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes) cax = fig.add_axes([0.37, 0.395, 0.20, 0.01]) cb = pyplot.colorbar( im, cax=cax, orientation='horizontal') #, format=FormatStrFormatter('%d')) cb.ax.tick_params(axis='x', which='both', bottom=False, top=True, labelbottom=False, labeltop=True) cax.text(1.05, 0.7, r'$|\Delta|/m$', horizontalalignment='left', verticalalignment='center', transform=cax.transAxes, fontsize=10) ax = init_ax(fig, [0.67, 0.23, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.yaxis.set_major_formatter(ticker.NullFormatter()) im = ax.pcolormesh(wave_bins, spec_bins, chi.T, norm=colors.LogNorm(vmin=chi_lim[0], vmax=chi_lim[1]), cmap='RdBu_r', zorder=4, lw=0, rasterized=True) ax = init_ax(fig, [0.67, 0.07, 0.30, 0.16], facecolor='0.95') ax.set_xlim(wave_limits) ax.set_ylim(spec_lim) ax.yaxis.set_major_formatter(ticker.NullFormatter()) ax.pcolormesh(wave_bins, spec_bins, chi_sm.T, norm=colors.LogNorm(vmin=chi_lim[0], vmax=chi_lim[1]), cmap='RdBu_r', zorder=4, lw=0, rasterized=True) cax = fig.add_axes([0.67, 0.395, 0.20, 0.01]) cb = pyplot.colorbar( im, cax=cax, orientation='horizontal') #, format=FormatStrFormatter('%d')) cb.ax.tick_params(axis='x', which='both', bottom=False, top=True, labelbottom=False, labeltop=True) cax.text(1.05, 0.7, r'$|\Delta|/\epsilon$', horizontalalignment='left', verticalalignment='center', transform=cax.transAxes, fontsize=10) if ofile is None: pyplot.show() else: print('Writing: {0}'.format(ofile)) fig.canvas.print_figure(ofile, bbox_inches='tight', dpi=100) fig.clear() pyplot.close(fig)
def main(args): # Get the g and r filter curves filter_root = os.path.join(defaults.dap_data_root(), 'filter_response') f = os.path.join(filter_root, 'gunn_2001_g_response.db') if not os.path.isfile(f): raise FileNotFoundError('No file: {0}'.format(f)) db = numpy.genfromtxt(os.path.join(f)) g = interpolate.interp1d(db[:, 0], db[:, 1], fill_value=0.0, bounds_error=False) f = os.path.join(filter_root, 'gunn_2001_r_response.db') if not os.path.isfile(f): raise FileNotFoundError('No file: {0}'.format(f)) db = numpy.genfromtxt(os.path.join(f)) r = interpolate.interp1d(db[:, 0], db[:, 1], fill_value=0.0, bounds_error=False) # Build the template library tpl = TemplateLibrary(args.key, velscale_ratio=args.velscale_ratio, spectral_step=args.step, log=not args.linear, hardcopy=False) # Get the sampling nwave = len(tpl['WAVE'].data) dwave = spectral_coordinate_step(tpl['WAVE'].data, log=not args.linear) if not args.linear: dwave *= tpl['WAVE'].data * numpy.log(10.) flxmid = (numpy.amax(tpl['FLUX'].data, axis=1) + numpy.amin(tpl['FLUX'].data, axis=1)) / 2.0 flxrng = numpy.amax(tpl['FLUX'].data, axis=1) - numpy.amin( tpl['FLUX'].data, axis=1) g_tc = g(tpl['WAVE'].data) r_tc = r(tpl['WAVE'].data) g_tc_sum = numpy.sum(g_tc * dwave) r_tc_sum = numpy.sum(r_tc * dwave) gflux = numpy.sum(tpl['FLUX'].data * g_tc[None, :] * dwave[None, :], axis=1) / g_tc_sum rflux = numpy.sum(tpl['FLUX'].data * r_tc[None, :] * dwave[None, :], axis=1) / r_tc_sum numpy.savetxt( args.ofile, numpy.array([ numpy.arange(tpl.ntpl), flxmid, flxrng, gflux, rflux, -2.5 * numpy.log10(gflux / rflux) ]).T, fmt=['%5d', '%15.8e', '%15.8e', '%15.8e', '%15.8e', '%15.8e'], header='{0:>3} {1:>15} {2:>15} {3:>15} {4:>15} {5:>15}'.format( 'ID', 'FLXMID', 'FLXRNG', 'MEANg', 'MEANr', '-2.5LOG(g/r)'))