def compprep(spectrum, sn_name, z, source): old_wave = spectrum[:, 0] # wavelengths old_flux = spectrum[:, 1] # fluxes try: old_error = spectrum[:, 2] # check if supernovae has error array except IndexError: old_error = np.array([0]) # if not, set default if sn_name == '2011fe': old_error = np.sqrt(old_error) old_ivar = df.genivar(old_wave, old_flux, old_error) # generate inverse variance snr = prep.getsnr(old_flux, old_ivar) if source == 'cfa': # choosing source dataset sne = prep.ReadExtin('extinction.dat') if source == 'bsnip': sne = prep.ReadExtin('extinctionbsnip.dat') if source == 'csp': sne = prep.ReadExtin('extinctioncsp.dat') old_wave *= 1 + float(z) # Redshift back if source == 'uv': sne = prep.ReadExtin('extinctionuv.dat') if source == 'other': sne = prep.ReadExtin('extinctionother.dat') newdata = [] old_wave = old_wave * u.Angstrom old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(old_wave, old_flux) test_flux = test_dered.dered(sne, sn_name, spec1d.wavelength, spec1d.flux) new_flux = test_flux.value old_wave = old_wave.value old_wave = old_wave / (1. + z) old_flux = np.asarray(old_flux) new_flux = np.asarray(new_flux) # s = scale_composites_in_range(old_flux, new_flux) # old_flux = s*old_flux new_wave = old_wave / (1. + z) new_error = old_error new_ivar = df.genivar(new_wave, new_flux, new_error) newdata = prep.Interpo(new_wave, new_flux, new_ivar) return newdata, snr
def compprep(spectrum, sn_name, z, source, use_old_error=True, testing=False, filename=None, mjd=None, mjd_max=None): """ Performs clipping, deredshifting, variance spectrum generation, MW extinction correction, and interpolation. If testing is True, several plots will be made to assess the quality of this processing. """ old_wave = spectrum[:, 0] # wavelengths old_flux = spectrum[:, 1] # fluxes try: old_error = spectrum[:, 2] # check if supernovae has error array except IndexError: old_error = None # if not, set default if sn_name == '2011fe' and source == 'other': old_error = np.sqrt(old_error) if old_error is not None: old_var = old_error**2. else: old_var = None if old_var is not None: num_non_zeros = np.count_nonzero(old_var) if len(old_var) - num_non_zeros > 100: old_var = None elif old_var[-1] == 0.: old_var[-1] = old_var[-2] elif True in np.isnan(old_var): nan_inds = np.transpose(np.argwhere(np.isnan(old_var)))[0] for ind in nan_inds: if ind != 0: old_var[ind] = old_var[ind - 1] else: old_var[ind] = old_var[ind + 1] # if testing: # plt.plot(old_wave, old_flux) # plt.plot(old_wave/(1.+z), old_flux) # plt.plot(old_wave*(1.+z), old_flux) # plt.xlim(5800,6000) # # plt.show() # if old_var is not None: # plt.plot(old_wave, old_var) # plt.show() # old_var = None vexp, SNR = df.find_vexp(old_wave, old_flux, var_y=old_var) if testing: print vexp, SNR if source != 'csp': #already deredshifted old_wave = old_wave / (1. + z) #deredshift for clipping old_wave, old_flux, old_var = df.clip( old_wave, old_flux, old_var, vexp, testing=testing, filename=filename) #clip emission/absorption lines old_wave = old_wave * (1. + z) #reredshift for MW extinction correction temp_ivar, SNR = df.genivar(old_wave, old_flux, old_var, vexp=vexp, testing=testing, source=source) # generate inverse variance #code to save foundation spec for david # print filename # plt.plot(old_wave, old_flux) # plt.show() # plt.plot(old_wave, temp_ivar) # plt.show() # file_path = '../../Foundation/mod_TNS_spec/' + filename.split('.')[0] + '_modified.flm' # print file_path # with open(file_path, 'w') as file: # file.write('# Orginal file name = ' + filename + '\n') # file.write('# z = ' + str(z) + '\n') # # file.write('# MJD = ' + str(mjd) + '\n') # # file.write('# MJD_max = ' + str(mjd_max) + '\n') # file.write('\n') # err = np.sqrt(1./np.asarray(temp_ivar)) # data = np.c_[old_wave,old_flux,err] # table = tabulate(data, headers=['Wavelength', 'Flux', 'Error'], # tablefmt = 'ascii') # file.write(table) if testing: print SNR if old_var is not None: old_ivar = 1. / old_var else: old_ivar = temp_ivar # snr = getsnr(old_flux, old_ivar) if source == 'cfa': # choosing source dataset # z = ReadParam() sne = ReadExtin('extinction.dat') if source == 'bsnip': sne = ReadExtin('extinctionbsnip.dat') if source == 'csp': sne = ReadExtin('extinctioncsp.dat') if source == 'uv': sne = ReadExtin('extinctionuv.dat') if source == 'other': sne = ReadExtin('extinctionother.dat') if source == 'swift_uv': sne = ReadExtin('extinctionswiftuv.dat') if source == 'foley_hst': sne = ReadExtin('extinctionhst.dat') if source == 'foundation': sne = ReadExtin('extinctionfoundation.dat') # host_reddened = ReadExtin('../data/info_files/ryan_av.txt') newdata = [] old_wave = old_wave * u.Angstrom # wavelengths old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(old_wave, old_flux) spec1d_ivar = Spectrum1D.from_array(old_wave, old_ivar) dered_flux, dered_ivar = test_dered.dered( sne, sn_name, spec1d.wavelength, spec1d.flux, spec1d_ivar.flux, source=source ) # Dereddening (see if sne in extinction files match the SN name) # new_flux = host_correction(sne, sn_name, old_wave, new_flux) # new_flux = old_flux if testing: new_flux_plot = copy.deepcopy(dered_flux) new_ivar_plot = copy.deepcopy(dered_ivar) old_wave_plot = copy.deepcopy(old_wave) new_flux = dered_flux.value new_ivar = dered_ivar.value old_wave = old_wave.value if testing: av_specific = 0.2384 #2005lz av_specific = 0.4089 #2007af r_v = 2.5 new_flux_host, new_ivar_host = test_dered.host_correction( av_specific, r_v, sn_name, old_wave_plot, new_flux_plot, new_ivar_plot) new_flux_host = new_flux_host.value old_flux = old_flux.value s = scale_composites_in_range(new_flux, old_flux) new_flux_scaled = s * new_flux s = scale_composites_in_range(new_flux_host, old_flux) new_flux_host_scaled = s * new_flux_host valid_data = np.where(old_wave > 4000) norm = 10. / np.nanmax(new_flux_host_scaled[valid_data]) old_flux_norm = old_flux * norm new_flux_norm = new_flux_scaled * norm new_flux_host_norm = new_flux_host_scaled * norm plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20) # ax.xaxis.set_ticks(np.arange(np.round(wave[0],-3),np.round(wave[-1],-3),1000)) plt.yticks(fontsize=20) plt.tick_params(which='major', bottom='on', top='on', left='on', right='on', length=10) plt.tick_params(which='minor', bottom='on', top='on', left='on', right='on', length=5) plt.plot(old_wave, old_flux_norm, linewidth=2, color='#000080', label='Before Dereddening') plt.plot(old_wave, new_flux_norm, linewidth=2, color='gold', label='Milky Way Corrected') # plt.plot(old_wave, new_flux_host_norm, linewidth = 2, color = '#d95f02', label='Host Corrected') plt.ylabel('Relative Flux', fontsize=30) plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30) plt.xlim([old_wave[0] - 200, old_wave[-1] + 200]) plt.legend(loc=1, fontsize=20) # plt.savefig('../../../Paper_Drafts/reprocessing_updated/red_corr.pdf', dpi = 300, bbox_inches = 'tight') plt.show() # plt.plot(old_wave, old_ivar) # plt.plot(old_wave, new_ivar) # plt.show() new_wave = old_wave / (1. + z) # Deredshifting if not use_old_error: new_var = None else: new_var = old_var # Placeholder if it needs to be changed #var = new_flux*0+1 # newdata = Interpo(new_wave, new_flux, new_ivar) # Do the interpolation newdata, scale, var_final = Interpo_flux_conserving(new_wave, new_flux, new_ivar, testing=testing) if testing: # newdata_test = Interpo(new_wave, new_flux_host_norm, new_ivar) newdata_test, scale, var_final = Interpo_flux_conserving( new_wave, new_flux_host_norm, new_ivar) interp_wave = newdata_test[0, :] interp_flux = newdata_test[1, :] plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20) # ax.xaxis.set_ticks(np.arange(np.round(wave[0],-3),np.round(wave[-1],-3),1000)) plt.yticks(fontsize=20) plt.tick_params(which='major', bottom='on', top='on', left='on', right='on', length=10) plt.tick_params(which='minor', bottom='on', top='on', left='on', right='on', length=5) plt.plot(new_wave, 2. * new_flux_host_norm, linewidth=2, color='#d95f02', label='Before Interpolation') plt.plot(interp_wave, interp_flux, linewidth=2, color='darkgreen', label='After Interpolation') plt.ylabel('Relative Flux', fontsize=30) plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30) plt.xlim([new_wave[0] - 200, new_wave[-1] + 200]) plt.legend(loc=1, fontsize=20) # plt.savefig('../../../Paper_Drafts/reprocessing_updated/interp_deredshift.pdf', dpi = 300, bbox_inches = 'tight') plt.show() return newdata, SNR
def compprep(spectrum, sn_name, z, source): old_wave = spectrum[:, 0] # wavelengths old_flux = spectrum[:, 1] # fluxes try: old_error = spectrum[:, 2] # check if supernovae has error array except IndexError: old_error = np.array([0]) # if not, set default old_ivar = df.genivar(old_wave, old_flux, old_error) # generate inverse variance snr = prep.getsnr(old_flux, old_ivar) if source == 'cfa': # choosing source dataset # z = ReadParam() sne = prep.ReadExtin('extinction.dat') if source == 'bsnip': sne = prep.ReadExtin('extinctionbsnip.dat') if source == 'csp': sne = prep.ReadExtin('extinctioncsp.dat') old_wave *= 1 + float(z) # Redshift back if source == 'uv': sne = prep.ReadExtin('extinctionuv.dat') if source == 'other': sne = prep.ReadExtin('extinctionother.dat') # host_reddened = ReadExtin('../data/info_files/ryan_av.txt') newdata = [] dered_wave = old_wave / (1. + z) new_error = old_error # Placeholder if it needs to be changed norm = 1. / np.amax(old_flux) new_flux = old_flux * norm new_ivar = df.genivar(dered_wave, new_flux, new_error) # generate new inverse variance #var = new_flux*0+1 dered_wave, new_flux, new_ivar = clip(dered_wave, new_flux, new_ivar) newdata = Interpo(dered_wave, new_flux, new_ivar, sn_name, plot=True) # Do the interpolation interp_wave = newdata[0] * u.Angstrom # wavelengths interp_flux = newdata[1] * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(interp_wave, interp_flux) test_flux = test_dered.dered( sne, sn_name, spec1d.wavelength, spec1d.flux ) # Deredenning (see if sne in extinction files match the SN name) # new_flux = host_correction(sne, sn_name, old_wave, new_flux) # new_flux = old_flux mw_flux = test_flux.value interp_wave = interp_wave.value interp_flux = interp_flux.value interp_flux = np.nan_to_num(interp_flux) mw_flux = np.nan_to_num(mw_flux) s = scale_composites_in_range(mw_flux, interp_flux) mw_flux = s * mw_flux if sn_name == '2006sr': av = .1294 #2006sr name = '2006sr' else: av = 2.9711 #2005a name = '2005a' # name = '2005a' host_wave = interp_wave * u.Angstrom # wavelengths host_flux = mw_flux * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(host_wave, host_flux) new_flux_host, new_ivar_host = test_dered.host_correction( av, 2.5, name, spec1d.wavelength, spec1d.flux, [0]) new_flux_host = np.asarray(new_flux_host.value) s = scale_composites_in_range(new_flux_host, interp_flux) new_flux_host = s * new_flux_host scale = 10. valid_data = np.where(interp_wave > 3600) norm = 1. / np.nanmax(new_flux_host[valid_data]) new_flux_host = new_flux_host * norm * scale mw_flux = mw_flux * norm * scale interp_flux = interp_flux * norm * scale interp_flux[interp_flux == 0] = np.nan mw_flux[mw_flux == 0] = np.nan new_flux_host[new_flux_host == 0] = np.nan plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20) ax.xaxis.set_ticks( np.arange(np.round(interp_wave[0], -3), np.round(interp_wave[-1], -3), 1000)) plt.yticks(fontsize=20) plt.tick_params(which='major', bottom='on', top='on', left='on', right='on', length=10) plt.tick_params(which='minor', bottom='on', top='on', left='on', right='on', length=5) plt.plot(interp_wave, interp_flux, linewidth=2, color='#d95f02', label='Before Dereddening') plt.plot(interp_wave, mw_flux, linewidth=2, color='#1b9e77', label='MW Corrected') plt.plot(interp_wave, new_flux_host, linewidth=2, color='#7570b3', label='Host Corrected') plt.ylabel('Relative Flux', fontsize=30) plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30) # plt.savefig('../../Paper_Drafts/red_corr.pdf', dpi = 300, bbox_inches = 'tight') plt.xlim([dered_wave[0] - 200, dered_wave[-1] + 200]) plt.legend(loc=1, fontsize=20) if sn_name == '2005a': plt.ylim([-.05 * scale, 1.05 * scale]) # plt.savefig('../../../Paper_Drafts/reprocessing/red_corr_large_av.pdf', dpi = 300, bbox_inches = 'tight') else: plt.ylim([-.05 * scale, 1.05 * scale]) # plt.savefig('../../../Paper_Drafts/reprocessing/red_corr_small_av.pdf', dpi = 300, bbox_inches = 'tight') plt.show() # new_wave = old_wave/(1.+z) # Deredshifting # print 'new spectra',newdata return newdata, snr
def compprep(spectrum, sn_name, z, source): old_wave = spectrum[:, 0] # wavelengths old_flux = spectrum[:, 1] # fluxes try: old_error = spectrum[:, 2] # check if supernovae has error array except IndexError: old_error = np.array([0]) # if not, set default old_ivar = df.genivar(old_wave, old_flux, old_error) # generate inverse variance snr = prep.getsnr(old_flux, old_ivar) if source == 'cfa': # choosing source dataset # z = ReadParam() sne = prep.ReadExtin('extinction.dat') if source == 'bsnip': sne = prep.ReadExtin('extinctionbsnip.dat') if source == 'csp': sne = prep.ReadExtin('extinctioncsp.dat') old_wave *= 1 + float(z) # Redshift back if source == 'uv': sne = prep.ReadExtin('extinctionuv.dat') if source == 'other': sne = prep.ReadExtin('extinctionother.dat') # host_reddened = ReadExtin('../data/info_files/ryan_av.txt') newdata = [] old_wave = old_wave * u.Angstrom # wavelengths old_flux = old_flux * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(old_wave, old_flux) test_flux = test_dered.dered( sne, sn_name, spec1d.wavelength, spec1d.flux ) # Deredenning (see if sne in extinction files match the SN name) # new_flux = host_correction(sne, sn_name, old_wave, new_flux) # new_flux = old_flux new_flux = test_flux.value old_wave = old_wave.value old_wave = old_wave / (1. + z) old_flux = np.asarray(old_flux) new_flux = np.asarray(new_flux) s = scale_composites_in_range(old_flux, new_flux) old_flux = s * old_flux # plt.rc('font', family='serif') # fig, ax = plt.subplots(1,1) # fig.set_size_inches(10, 8, forward = True) # ax.get_yaxis().set_ticks([]) # plt.plot(old_wave, old_flux, linewidth = 2, color = 'r') # plt.plot(old_wave, new_flux, linewidth = 2, color = '#3F5D7D') # plt.ylabel('Relative Flux') # plt.xlabel('Wavelength ' + "($\mathrm{\AA}$)") # # plt.savefig('../../Paper_Drafts/MW_corr.png', dpi = 300, bbox_inches = 'tight') # plt.show() av = .1294 #2006sr # av = 2.9711 #2005a name = '2006sr' # name = '2005a' host_wave = old_wave * u.Angstrom # wavelengths host_flux = new_flux * u.Unit('W m-2 angstrom-1 sr-1') spec1d = Spectrum1D.from_array(host_wave, host_flux) new_flux_host, new_ivar_host = test_dered.host_correction( av, 2.5, name, spec1d.wavelength, spec1d.flux, [0]) new_flux = np.asarray(new_flux) new_flux_host = np.asarray(new_flux_host.value) s = scale_composites_in_range(new_flux_host, new_flux) new_flux_host = s * new_flux_host norm = 1. / np.amax(new_flux_host) new_flux_host = new_flux_host * norm new_flux = new_flux * norm old_flux = old_flux * norm plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20) ax.xaxis.set_ticks( np.arange(np.round(old_wave[0], -3), np.round(old_wave[-1], -3), 1000)) plt.yticks(fontsize=20) plt.tick_params(which='major', bottom='on', top='on', left='on', right='on', length=10) plt.tick_params(which='minor', bottom='on', top='on', left='on', right='on', length=5) plt.plot(old_wave, old_flux, linewidth=2, color='#d95f02') plt.plot(old_wave, new_flux, linewidth=2, color='#1b9e77') plt.plot(host_wave.value, new_flux_host, linewidth=2, color='#7570b3') plt.ylabel('Relative Flux', fontsize=30) plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30) plt.savefig('../../Paper_Drafts/red_corr.pdf', dpi=300, bbox_inches='tight') # plt.ylim([-.2,1.01]) # plt.savefig('../../Paper_Drafts/red_corr_large.pdf', dpi = 300, bbox_inches = 'tight') plt.show() # new_wave = old_wave/(1.+z) # Deredshifting new_wave = old_wave new_error = old_error # Placeholder if it needs to be changed norm = 1. / np.amax(new_flux) new_flux = new_flux * norm new_ivar = df.genivar(new_wave, new_flux, new_error) # generate new inverse variance #var = new_flux*0+1 newdata = prep.Interpo(new_wave, new_flux, new_ivar) # Do the interpolation plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20) ax.xaxis.set_ticks( np.arange(np.round(old_wave[0], -3), np.round(old_wave[-1], -3), 1000)) plt.yticks(fontsize=20) plt.tick_params(which='major', bottom='on', top='on', left='on', right='on', length=10) plt.tick_params(which='minor', bottom='on', top='on', left='on', right='on', length=5) plt.plot(old_wave, new_flux, linewidth=2, color='r') plt.plot(newdata[0], newdata[1], linewidth=2, color='#3F5D7D') plt.ylabel('Relative Flux', fontsize=30) plt.xlabel('Rest Wavelength ' + "($\mathrm{\AA}$)", fontsize=30) plt.savefig('../../Paper_Drafts/interp.pdf', dpi=300, bbox_inches='tight') # plt.ylim([-.3,1.]) # plt.savefig('../../Paper_Drafts/interp_large.pdf', dpi = 300, bbox_inches = 'tight') plt.show() # print 'new spectra',newdata return newdata, snr
data = bsnip_vals[sn_name.lower() + '-' + longdate] redshift = data[1] / c newdata, snr = compprep(spectrum, sn_name, redshift, source) raise TypeError ### ENDS HERE fname = '../data/spectra/cfa/sn2003kg/sn2003kg-20031129.12-fast.flm' spectrum = np.loadtxt(fname) old_wave = spectrum[:, 0] old_flux = spectrum[:, 1] real_error = spectrum[:, 2] if real_error[0] != 0.: old_error = np.zeros(len(old_wave), float) new_ivar = df.genivar(old_wave, old_flux, old_error) new_var = 1. / new_ivar real_var = real_error * real_error scale = scale_composites_in_range(new_var, real_var) # new_var = new_var*scale new_var = new_var * 2.02 print scale norm = 1. / np.amax(real_var) real_var = real_var * norm new_var = new_var * norm plt.rc('font', family='serif') fig, ax = plt.subplots(1, 1) fig.set_size_inches(10, 8, forward=True) plt.minorticks_on() plt.xticks(fontsize=20)