def evaluate(self, wavelength, flux, a_v, r_v): from specutils import extinction extinction_factor = np.ones_like(wavelength) valid_wavelength = ((wavelength > 910) & (wavelength < 33333)) extinction_factor[valid_wavelength] = 10 ** (-0.4 * extinction.extinction_ccm89( wavelength[valid_wavelength] * u.angstrom, a_v=np.abs(a_v), r_v=np.abs(r_v))) return wavelength, extinction_factor * flux
def __call__(self, spectrum): from specutils import extinction extinction_factor = 10**(-0.4*extinction.extinction_ccm89( spectrum.wavelength, a_v=self.a_v, r_v=self.r_v)) return Spectrum1D.from_array( spectrum.wavelength.value, extinction_factor * spectrum.flux, dispersion_unit=spectrum.wavelength.unit, unit=spectrum.unit)
def __call__(self, spectrum): from specutils import extinction extinction_factor = 10**(-0.4 * extinction.extinction_ccm89( spectrum.wavelength, a_v=self.a_v, r_v=self.r_v)) return Spectrum1D.from_array(spectrum.wavelength.value, extinction_factor * spectrum.flux, dispersion_unit=spectrum.wavelength.unit, unit=spectrum.unit)
def evaluate(self, wavelength, flux, a_v, r_v): from specutils import extinction extinction_factor = np.ones_like(wavelength) valid_wavelength = ((wavelength > 910) & (wavelength < 33333)) extinction_factor[valid_wavelength] = 10**( -0.4 * extinction.extinction_ccm89( wavelength[valid_wavelength] * u.angstrom, a_v=np.abs(a_v), r_v=np.abs(r_v))) return wavelength, extinction_factor * flux
def __call__(self, spectrum): from specutils import extinction extinction_factor = np.ones_like(spectrum.wavelength.value) valid_wavelength = ((spectrum.wavelength > 910 * u.angstrom) & (spectrum.wavelength < 33333 * u.angstrom)) extinction_factor[valid_wavelength] = 10 ** (-0.4 * extinction.extinction_ccm89( spectrum.wavelength[valid_wavelength], a_v=self.a_v, r_v=self.r_v).to(u.angstrom).value) return Spectrum1D.from_array(spectrum.wavelength, extinction_factor * spectrum.flux)
def __call__(self, spectrum): from specutils import extinction extinction_factor = np.ones_like(spectrum.wavelength.value) valid_wavelength = ((spectrum.wavelength > 910 * u.angstrom) & (spectrum.wavelength < 33333 * u.angstrom)) extinction_factor[valid_wavelength] = 10**( -0.4 * extinction.extinction_ccm89(spectrum.wavelength[valid_wavelength], a_v=self.a_v, r_v=self.r_v).to(u.angstrom).value) return Spectrum1D.from_array(spectrum.wavelength, extinction_factor * spectrum.flux)
def spec_proc(data_tab): s= Table.read(data_tab) #use myBALs.fits for i in range(len(s)): #print i z= s['Z_HW'][i] # use redshifts from Hewett and Wild 2010 Av= s['AV_SandF'][i] #Av (Schlafly and Finkbeiner 2011) see extinction_tab.txt spec_name= "./data/spec-"+str(s['plate'][i]).zfill(4)+"-"+str(s['MJD_spec'][i])+"-"+str(s['fiberid'][i]).zfill(4)+".fits" # fits file name spec= fits.open(spec_name) # read file flx= spec[1].data.field(0) # flux array w= 10.**(spec[1].data.field(1)) # wavelength array wlen= w.astype(double) #c0 = spec[0].header['coeff0'] #c0= spec[1].data.field(1)[0] #c1 = spec[0].header['coeff1'] #npix= spec[1].header['NAXIS2'] #wlen = 10.**(c0 + c1 * np.arange(npix)) #wlen= 10.**(spec[0].header['coeff0']+ spec[0].header['coeff1'] * np.arange(len(spec[1].data.field(1)))) # wavelength, coeff0: starting wavelength, coeff1: dispersion #flx= spec[0].data[0] # flux #wlen= 10.**(spec[0].header['coeff0']+ spec[0].header['coeff1'] * np.arange(len(spec[0].data[0]))) # wavelength, coeff0: starting wavelength, coeff1: dispersion ext_lambda= extinction.extinction_ccm89(wlen * u.angstrom, a_v= Av, r_v= 3.1) tau_lambda= ext_lambda/((1 * u.angstrom) * 1.086) # the unit seems necessary on my desktop but not needed on laptop :/ #tau_lambda= ext_lambda/(1.086) dered_flx= flx * np.exp(tau_lambda) # extinction corrected flux rest_wlen= wlen /(1+z) # restframe wavelength x= np.arange(1100, 4000, 0.5) # the wavelength values for the rebinned spectrum with bin width = 0.5 A rebin_dered_flx= np.interp(x, rest_wlen, dered_flx) # resampled flux with delta lambda= 0.5 A proc_spec= np.vstack((x, rebin_dered_flx)) # 2D array. 1st row: restframe wavelength, 2nd row: corrected flux hdu= fits.PrimaryHDU(proc_spec) # HDU with flux and wavelngth to be saved as a fits file new_spec_name= './proc_data/spec-'+str(s['plate'][i])+'-'+str(s['MJD_spec'][i])+'-'+str(s['fiberid'][i]).zfill(4)+'_proc.fits' # name of new processed spectrum hdu.writeto(new_spec_name) return
#s= Table.read('sample_myflags.csv') s = data[(data['Z_PCA'] >1.6) & (data['Z_PCA'] <2.1) & (data['ERR_REWE_CIII'] < data['REWE_CIII']/10) & (data['ERR_REWE_CIV'] < data['REWE_CIV']/10) & (data['ERR_REWE_MGII'] < data['REWE_MGII']/10) & (data['SNR_1700'] > 3)] #s = data[(data['Z_PCA'] >1.6) & (data['Z_PCA'] <2.1) & (data['REWE_CIII'] >0) & (data['REWE_CIII'] <2000) & (data['REWE_CIV'] >0) & (data['REWE_CIV'] <2000) & (data['REWE_MGII'] >0) & (data['REWE_MGII'] <2000) & (data['BAL_FLAG_VI'] ==0)] # subsample with: upper and lower redshift limits, reasonable measurements for EW, and BAL quasars excluded for i in range(len(s)): print i z= s['Z_PCA'][i] mg= 3.1 * s['EXTINCTION'][i][1] #use the extinction in the g magnitude E(B-V) spec_name= './data/spec-'+str(s['PLATE'][i])+'-'+str(s['MJD'][i])+'-'+str(s['FIBERID'][i]).zfill(4)+'.fits' # fits file name spec= fits.open(spec_name) # read file flx= spec[1].data.field(0) # flux wlen= 10.**(spec[0].header['coeff0']+ spec[0].header['coeff1'] * np.arange(len(spec[1].data.field(1)))) # wavelength, coeff0: starting wavelength, coeff1: dispersion ext_lambda= extinction.extinction_ccm89(wlen * u.angstrom, a_v= mg, r_v= 3.1) tau_lambda= ext_lambda/((1 * u.angstrom) * 1.086) # for some reason the extinction has a unit of angstrom (should be magnitude i.e. unitless) dered_flx= flx * np.exp(tau_lambda) # extinction corrected flux rest_wlen= wlen /(1+z) # restframe wavelength x= np.arange(1100, 4000, 0.5) # the wavelength values for the rebinned spectrum with bin width = 0.5 A rebin_dered_flx= np.interp(x, rest_wlen, dered_flx) # resampled flux with delta lambda= 0.5 A # norm_flx= rebin_dered_flx/np.mean(rebin_dered_flx[2360:2390]) # normalize spectra proc_spec= np.vstack((x, rebin_dered_flx)) # 2D array. 1st row: restframe wavelength, 2nd row: corrected flux hdu= fits.PrimaryHDU(proc_spec) # HDU with flux and wavelngth to be saved as a fits file new_spec_name= './new_proc_data/spec-'+str(s['PLATE'][i])+'-'+str(s['MJD'][i])+'-'+str(s['FIBERID'][i]).zfill(4)+'_proc.fits' # name of new processed spectrum hdu.writeto(new_spec_name)
def __init__(self, wavelength, av, rv): self.extinction = extinction.extinction_ccm89( wavelength, av, rv)