def voigt_model(spec, line, Npix=None, flg_ret=1): """Generates a Voigt model from a line or list of lines Parameters: spec: wave array or Spectrum line: Abs_Line, List of Abs_line, or array of parameters flg_ret : int (1) Byte-wise Flag for return 1: vmodel 2: tau ToDo: 1. May need to more finely sample the wavelength array JXP 01 Nov 2014 """ # Imports #from barak import spec as bs from barak import convolve as bc import copy from specutils.spectrum1d import Spectrum1D # Spectrum input if isinstance(spec,np.ndarray): # Standard wavelength array vmodel = Spectrum1D.from_array(spec, np.zeros(len(spec))) elif isinstance(spec,Spectrum1D): vmodel = copy.deepcopy(spec) else: raise ValueError('voigt_model: Unknown input') # Line input if isinstance(line,abs_line.Abs_Line): # Single line as a Abs_Line Class par = [0*i for i in range(6)] # Dummy list par[0] = line.attrib['N'] par[1] = line.z par[2] = line.attrib['b'] par[3] = line.wrest par[4] = line.atomic['fval'] par[5] = line.atomic['gamma'] elif isinstance(line,list): if isinstance(line[0],abs_line.Abs_Line): # List of Abs_Line tau = np.zeros(len(vmodel.flux)) for iline in line: tau += voigt_model(vmodel.dispersion, iline, Npix=None, flg_ret=2) #xdb.set_trace() else: par = line # Single line as a vector else: raise ValueError('voigt: Unknown type for voigt line') # tau if 'tau' not in locals(): cold = 10.0**par[0] / u.cm / u.cm zp1=par[1]+1.0 wv=par[3].to(u.cm) #*1.0e-8 nujk = (const.c / wv).to(u.Hz) dnu = ((par[2]*u.km/u.s) / wv).to('Hz') avoigt = ((par[5]/u.s)/( 4 * np.pi * dnu)).to(u.dimensionless_unscaled) uvoigt = ( ((const.c / (vmodel.dispersion/zp1)) - nujk) / dnu).to(u.dimensionless_unscaled) ''' wv=par[3].to(u.cm) #*1.0e-8 bl=((par[2]*u.km/u.s)*wv/const.c).to(u.cm) # 2.99792458E5 a= ((par[5]/u.s)*wv*wv/( 4 * np.pi * const.c.to('cm/s') * bl)).to(u.dimensionless_unscaled) cns=wv*wv*par[4]/(bl*2.002134602291006E12) * u.cm # Converting to Voigt units cne=cold*cns ww=(vmodel.dispersion.to('cm'))/zp1 v=wv*ww*((1.0/ww)-(1.0/wv))/bl ''' # Voigt cne = 0.01497 * cold * par[4] * u.cm * u.cm * u.Hz tau = cne * voigtking(uvoigt,avoigt) / (np.sqrt(np.pi) * dnu) # Flux if vmodel.unit is None: try: vmodel.flux = (np.exp(-1.0*tau)).value except AttributeError: vmodel.flux = np.exp(-1.0*tau) else: vmodel.flux = np.exp(-1.0*tau).to(vmodel.unit) # Convolve if Npix is not None: vmodel.gauss_smooth(npix=Npix) # Return ret_val = [] if flg_ret % 2 == 1: ret_val.append(vmodel) if flg_ret % 4 >= 2: ret_val.append(tau) #xdb.set_trace() if len(ret_val) == 1: ret_val = ret_val[0] return ret_val
# Test ions if (flg_test % 2**1) >= 2**0: print('-------------------------') tmp1.get_ions() print('C IV: ') print(tmp1.ions[(6,4)]) print(tmp1.ions.trans[5]) # CIV 1550 # Plot the LLS if (flg_test % 2**2) >= 2**1: print('-------------------------') tmp1.fill_lls_lines() from specutils.spectrum1d import Spectrum1D wa=np.linspace(3400.0,5000.0,10000) spec = Spectrum1D.from_array(wa,np.ones(len(wa))) model = tmp1.flux_model(spec, smooth=4) model.qck_plot() #xdb.set_trace() # Test zpeak if (flg_test % 2**3) >= 2**2: print('-------------------------') tmp1.fill_ions() ion,vpeak = tmp1.get_zpeak() print('zpeak = {:g}'.format(tmp1.zpeak)) # Write .dat if (flg_test % 2**4) >= 2**3: tmp1.write_dat_file()
doppler = 3.0 fval = 1.669 gamma = 2.5E9 wrest = 1206.5*u.AA # SiIII line = [coldens,zabs,doppler,wrest,fval,gamma] plt.clf() if flg_test % 16 >= 8: vmodel = voigt_model(wave, line, Npix=None) # No smoothing xdb.xplot(vmodel.dispersion, vmodel.flux, drawstyle='steps-mid') vmodel2 = voigt_model(wave, line, Npix=4.) # Smoothing print('voigt: Smooth test') xdb.xplot(vmodel.dispersion, vmodel.flux) # Test pass back if flg_test % 32 >= 16: vmodel,tau = voigt_model(wave, line, Npix=None, flg_ret=3) # No smoothing vmodel.qck_plot() print('voigt: Passing test. Tau plot') xdb.xplot(wave,tau) # Test spec input if flg_test % 64 >= 32: from specutils.spectrum1d import Spectrum1D spec = Spectrum1D.from_array(wave, np.zeros(len(wave))) vmodel = voigt_model(spec, line, Npix=None, flg_ret=1) # No smoothing print('voigt: Spec input test') vmodel.qck_plot() print('voigt: All done testing..')
def test_from_spec1d(): spec = Spectrum1D.from_array(np.array([1,2,3]), np.array([1,1,1])) xspec = XSpectrum1D.from_spec1d(spec)