def specsens(specfile, outfile, stdfile, extfile, airmass=None, exptime=None, stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, clobber=True, logfile='salt.log',verbose=True): with logging(logfile,debug) as log: #read in the specfile and create a spectrum object obs_spectra=st.readspectrum(specfile, error=True, ftype='ascii') #read in the std file and convert from magnitudes to fnu #then convert it to fwave (ergs/s/cm2/A) std_spectra=st.readspectrum(stdfile, error=False, ftype='ascii') std_spectra.flux=Spectrum.magtoflux(std_spectra.flux, stdzp) std_spectra.flux=Spectrum.fnutofwave(std_spectra.wavelength, std_spectra.flux) #read in the extinction file (leave in magnitudes) ext_spectra=st.readspectrum(extfile, error=False, ftype='ascii') #determine the airmass if not specified if saltio.checkfornone(airmass) is None: message='Airmass was not supplied' raise SALTSpecError(message) #determine the exptime if not specified if saltio.checkfornone(airmass) is None: message='Exposure Time was not supplied' raise SALTSpecError(message) #calculate the calibrated spectra log.message('Calculating the calibration curve for %s' % specfile) cal_spectra=sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime) #fit the spectra--first take a first cut of the spectra #using the median absolute deviation to throw away bad points cmed=np.median(cal_spectra.flux) cmad=saltstat.mad(cal_spectra.flux) mask=(abs(cal_spectra.flux-cmed)<thresh*cmad) mask=(cal_spectra.flux>0) #now fit the data fit=interfit(cal_spectra.wavelength[mask], cal_spectra.flux[mask], function=function, order=order, thresh=thresh, niter=niter) fit.interfit() #print 'plotting...' #figure() #plot(cal_spectra.wavelength, cal_spectra.flux) #plot(obs_spectra.wavelength, obs_spectra.flux*cal_spectra.flux.mean()/obs_spectra.flux.mean()) #plot(std_spectra.wavelength, std_spectra.flux*cal_spectra.flux.mean()/std_spectra.flux.mean()) #plot(cal_spectra.wavelength, fit(cal_spectra.wavelength)) #show() #write the spectra out cal_spectra.flux=fit(cal_spectra.wavelength) st.writespectrum(cal_spectra, outfile, ftype='ascii')
def speccal(specfile, outfile, calfile, extfile, airmass=None, exptime=None, clobber=True, logfile='salt.log', verbose=True): with logging(logfile, debug) as log: # read in the specfile and create a spectrum object obs_spectra = st.readspectrum(specfile, error=True, ftype='ascii') # read in the std file and convert from magnitudes to fnu # then convert it to fwave (ergs/s/cm2/A) cal_spectra = st.readspectrum(calfile, error=False, ftype='ascii') # read in the extinction file (leave in magnitudes) ext_spectra = st.readspectrum(extfile, error=False, ftype='ascii') # determine the airmass if not specified if saltio.checkfornone(airmass) is None: message = 'Airmass was not supplied' raise SALTSpecError(message) # determine the exptime if not specified if saltio.checkfornone(airmass) is None: message = 'Exposure Time was not supplied' raise SALTSpecError(message) # calculate the calibrated spectra log.message('Calculating the calibration curve for %s' % specfile) error = False try: if obs_spectra.var is not None: error = True except: error = False flux_spectra = calfunc( obs_spectra, cal_spectra, ext_spectra, airmass, exptime, error) # write the spectra out st.writespectrum(flux_spectra, outfile, ftype='ascii', error=error)
def speccal( specfile, outfile, calfile, extfile, airmass=None, exptime=None, clobber=True, logfile="salt.log", verbose=True ): with logging(logfile, debug) as log: # read in the specfile and create a spectrum object obs_spectra = st.readspectrum(specfile, error=True, ftype="ascii") # read in the std file and convert from magnitudes to fnu # then convert it to fwave (ergs/s/cm2/A) cal_spectra = st.readspectrum(calfile, error=False, ftype="ascii") # read in the extinction file (leave in magnitudes) ext_spectra = st.readspectrum(extfile, error=False, ftype="ascii") # determine the airmass if not specified if saltio.checkfornone(airmass) is None: message = "Airmass was not supplied" raise SALTSpecError(message) # determine the exptime if not specified if saltio.checkfornone(airmass) is None: message = "Exposure Time was not supplied" raise SALTSpecError(message) # calculate the calibrated spectra log.message("Calculating the calibration curve for %s" % specfile) error = False try: if obs_spectra.var is not None: error = True except: error = False flux_spectra = calfunc(obs_spectra, cal_spectra, ext_spectra, airmass, exptime, error) # write the spectra out st.writespectrum(flux_spectra, outfile, ftype="ascii", error=error)
def specsens(specfile, outfile, stdfile, extfile, airmass=None, exptime=None, stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, fitter='gaussian', clobber=True, logfile='salt.log', verbose=True): with logging(logfile, debug) as log: # read in the specfile and create a spectrum object obs_spectra = st.readspectrum(specfile.strip(), error=True, ftype='ascii') # smooth the observed spectrum # read in the std file and convert from magnitudes to fnu # then convert it to fwave (ergs/s/cm2/A) std_spectra = st.readspectrum(stdfile.strip(), error=False, ftype='ascii') std_spectra.flux = Spectrum.magtoflux(std_spectra.flux, stdzp) std_spectra.flux = Spectrum.fnutofwave( std_spectra.wavelength, std_spectra.flux) # Get the typical bandpass of the standard star, std_bandpass = np.diff(std_spectra.wavelength).mean() # Smooth the observed spectrum to that bandpass obs_spectra.flux = st.boxcar_smooth(obs_spectra, std_bandpass) # read in the extinction file (leave in magnitudes) ext_spectra = st.readspectrum(extfile.strip(), error=False, ftype='ascii') # determine the airmass if not specified if saltio.checkfornone(airmass) is None: message = 'Airmass was not supplied' raise SALTSpecError(message) # determine the exptime if not specified if saltio.checkfornone(exptime) is None: message = 'Exposure Time was not supplied' raise SALTSpecError(message) # calculate the calibrated spectra log.message('Calculating the calibration curve for %s' % specfile) cal_spectra = sensfunc( obs_spectra, std_spectra, ext_spectra, airmass, exptime) # plot(cal_spectra.wavelength, cal_spectra.flux * std_spectra.flux) # fit the spectra--first take a first cut of the spectra # using the median absolute deviation to throw away bad points cmed = np.median(cal_spectra.flux) cmad = saltstat.mad(cal_spectra.flux) mask = (abs(cal_spectra.flux - cmed) < thresh * cmad) mask = np.logical_and(mask, (cal_spectra.flux > 0)) # now fit the data # Fit using a gaussian process. if fitter=='gaussian': from sklearn.gaussian_process import GaussianProcess #Instanciate a Gaussian Process model dy = obs_spectra.var[mask] ** 0.5 dy /= obs_spectra.flux[mask] / cal_spectra.flux[mask] y = cal_spectra.flux[mask] gp = GaussianProcess(corr='squared_exponential', theta0=1e-2, thetaL=1e-4, thetaU=0.1, nugget=(dy / y) ** 2.0) X = np.atleast_2d(cal_spectra.wavelength[mask]).T # Fit to data using Maximum Likelihood Estimation of the parameters gp.fit(X, y) x = np.atleast_2d(cal_spectra.wavelength).T # Make the prediction on the meshed x-axis (ask for MSE as well) y_pred = gp.predict(x) cal_spectra.flux = y_pred else: fit=interfit(cal_spectra.wavelength[mask], cal_spectra.flux[mask], function=function, order=order, thresh=thresh, niter=niter) fit.interfit() cal_spectra.flux=fit(cal_spectra.wavelength) # write the spectra out st.writespectrum(cal_spectra, outfile, ftype='ascii')
def specsens(specfile, outfile, stdfile, extfile, airmass=None, exptime=None, stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, fitter='gaussian', clobber=True, logfile='salt.log', verbose=True): with logging(logfile, debug) as log: # read in the specfile and create a spectrum object obs_spectra = st.readspectrum(specfile.strip(), error=True, ftype='ascii') # smooth the observed spectrum # read in the std file and convert from magnitudes to fnu # then convert it to fwave (ergs/s/cm2/A) std_spectra = st.readspectrum(stdfile.strip(), error=False, ftype='ascii') std_spectra.flux = Spectrum.magtoflux(std_spectra.flux, stdzp) std_spectra.flux = Spectrum.fnutofwave(std_spectra.wavelength, std_spectra.flux) # Get the typical bandpass of the standard star, std_bandpass = np.diff(std_spectra.wavelength).mean() # Smooth the observed spectrum to that bandpass obs_spectra.flux = st.boxcar_smooth(obs_spectra, std_bandpass) # read in the extinction file (leave in magnitudes) ext_spectra = st.readspectrum(extfile.strip(), error=False, ftype='ascii') # determine the airmass if not specified if saltio.checkfornone(airmass) is None: message = 'Airmass was not supplied' raise SALTSpecError(message) # determine the exptime if not specified if saltio.checkfornone(exptime) is None: message = 'Exposure Time was not supplied' raise SALTSpecError(message) # calculate the calibrated spectra log.message('Calculating the calibration curve for %s' % specfile) cal_spectra = sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime) # plot(cal_spectra.wavelength, cal_spectra.flux * std_spectra.flux) # fit the spectra--first take a first cut of the spectra # using the median absolute deviation to throw away bad points cmed = np.median(cal_spectra.flux) cmad = saltstat.mad(cal_spectra.flux) mask = (abs(cal_spectra.flux - cmed) < thresh * cmad) mask = np.logical_and(mask, (cal_spectra.flux > 0)) # now fit the data # Fit using a gaussian process. if fitter == 'gaussian': from sklearn.gaussian_process import GaussianProcess #Instanciate a Gaussian Process model dy = obs_spectra.var[mask]**0.5 dy /= obs_spectra.flux[mask] / cal_spectra.flux[mask] y = cal_spectra.flux[mask] gp = GaussianProcess(corr='squared_exponential', theta0=1e-2, thetaL=1e-4, thetaU=0.1, nugget=(dy / y)**2.0) X = np.atleast_2d(cal_spectra.wavelength[mask]).T # Fit to data using Maximum Likelihood Estimation of the parameters gp.fit(X, y) x = np.atleast_2d(cal_spectra.wavelength).T # Make the prediction on the meshed x-axis (ask for MSE as well) y_pred = gp.predict(x) cal_spectra.flux = y_pred else: fit = interfit(cal_spectra.wavelength[mask], cal_spectra.flux[mask], function=function, order=order, thresh=thresh, niter=niter) fit.interfit() cal_spectra.flux = fit(cal_spectra.wavelength) # write the spectra out st.writespectrum(cal_spectra, outfile, ftype='ascii')