def filter_mag(filterfile, filterzero, syn_spec, syn_err=None, wrange=None): ''' ###################################################### # Input # # -------------------------------------------------- # # filterfile: file containing filter function # # filterzero: flux [Jy] corresponding to mag=0 # # syn_spec: synphot spectrum object of source # # wrange: (start,end) observed wavelength range # # -------------------------------------------------- # # Output # # -------------------------------------------------- # # mag: magnitude of source in filter system. # ###################################################### ''' from synphot import SpectralElement, Observation, Empirical1D, SourceSpectrum import astropy.units as u #Load ascii filter function if filterfile.split('/')[-1][:6] == 'Bessel': filt = SpectralElement.from_file(filterfile, wave_unit='nm') else: filt = SpectralElement.from_file(filterfile, wave_unit='AA') if wrange is None: fwave = filt.waveset swave = syn_spec.waveset wrange = (max(fwave[0],swave[0]),min(fwave[-1],swave[-1])) waves = np.linspace(wrange[0], wrange[-1], 10000) #Synthetic observation obs = Observation(syn_spec, filt, force='extrap') flux = obs.effstim(flux_unit='jy', waverange=wrange).value #flux = obs.effstim(flux_unit='jy', wavelengths=waves).value #Calibrate magnitude with zero point mag = -2.512*np.log10(flux/filterzero) #Synthetic observation of error spectrum if syn_err is not None: #square filter and error spectrum filt2 = SpectralElement(Empirical1D, points=fwave, lookup_table=np.square(filt(fwave))) pseudo_flux = np.square(syn_err(swave,flux_unit='jy')).value syn_err2 = SourceSpectrum(Empirical1D, points=swave, lookup_table=pseudo_flux) #sum errors in quadrature obs = Observation(syn_err2, filt2, force='extrap') #flux_err = np.sqrt(obs.effstim(waverange=wrange).value) flux_err = np.sqrt(obs.effstim(wavelengths=waves).value) #convert to magnitude error mag_err = (2.5/np.log(10))*(flux_err/flux) return mag, mag_err else: return mag
def filter_flux(filterfile, syn_spec, syn_err=None, wrange=None): ''' ###################################################### # Input # # -------------------------------------------------- # # filterfile: file containing filter function # # syn_spec: synphot spectrum object of source # # wrange: (start,end) observed wavelength range # # -------------------------------------------------- # # Output # # -------------------------------------------------- # # flux: flux of source in filter system. # ###################################################### ''' from synphot import SpectralElement, Observation, Empirical1D, SourceSpectrum import astropy.units as u #Load ascii filter function if filterfile.split('/')[-1][:6] == 'Bessel': filt = SpectralElement.from_file(filterfile, wave_unit='nm') else: filt = SpectralElement.from_file(filterfile, wave_unit='AA') if wrange is None: fwave = filt.waveset swave = syn_spec.waveset wrange = (max(fwave[0],swave[0]),min(fwave[-1],swave[-1])) wlengths = fwave[np.logical_and(fwave>wrange[0], fwave<wrange[1])] else: fwave = filt.waveset wlengths = fwave[np.logical_and(fwave>wrange[0]*u.AA, fwave<wrange[1]*u.AA)] #Synthetic observation obs = Observation(syn_spec, filt, force='extrap') flux = obs.effstim(flux_unit='jy', waverange=wrange).value #flux = obs.effstim(flux_unit='jy', wavelengths=wlengths).value #Synthetic observation of error spectrum if syn_err is not None: #square filter and error spectrum filt2 = SpectralElement(Empirical1D, points=fwave, lookup_table=np.square(filt(fwave))) pseudo_flux = np.square(syn_err(swave,flux_unit='jy')).value syn_err2 = SourceSpectrum(Empirical1D, points=swave, lookup_table=pseudo_flux) #sum errors in quadrature obs = Observation(syn_err2, filt2, force='extrap') flux_err = np.sqrt(obs.effstim(waverange=wrange).value) return flux, flux_err else: return flux
def get_phot(waves, exts, bands): """ Compute the extinction in the requested bands Parameters ---------- waves : numpy.ndarray The wavelengths exts : numpy.ndarray The extinction values at wavelengths "waves" bands: list of strings Bands requested Outputs ------- band extinctions : numpy array Calculated band extinctions """ # create a SourceSpectrum object from the extinction curve spectrum = SourceSpectrum( Empirical1D, points=waves * 1e4, lookup_table=exts, ) # path for band response curves band_path = ( "/Users/mdecleir/measure_extinction/measure_extinction/data/Band_RespCurves/" ) # dictionary linking the bands to their response curves bandnames = { "J": "2MASSJ", "H": "2MASSH", "K": "2MASSKs", "IRAC1": "IRAC1", "IRAC2": "IRAC2", "WISE1": "WISE1", "WISE2": "WISE2", "L": "AAOL", "M": "AAOM", } # compute the extinction value in each band band_ext = np.zeros(len(bands)) for k, band in enumerate(bands): # create the bandpass (as a SpectralElement object) bp = SpectralElement.from_file( "%s%s.dat" % (band_path, bandnames[band])) # assumes wavelengths are in Angstrom!! # integrate the extinction curve over the bandpass, only if the bandpass fully overlaps with the extinction curve (this actually excludes WISE2) if bp.check_overlap(spectrum) == "full": obs = Observation(spectrum, bp) band_ext[k] = obs.effstim().value else: band_ext[k] = np.nan return band_ext
def calculate_values(detector, filt, mjd, aper): # parameters can be removed from obsmode as needed obsmode = 'wfc3,{},{},mjd#{},aper#{}'.format(detector, filt, mjd, aper) bp = stsyn.band(obsmode) # STMag photflam = bp.unit_response(stsyn.conf.area) # inverse sensitivity in flam stmag = -21.1 - 2.5 * log10(photflam.value) # Pivot Wavelength and bandwidth photplam = bp.pivot() # pivot wavelength in angstroms bandwidth = bp.photbw() # bandwidth in angstroms # ABMag abmag = stmag - 5 * log10(photplam.value) + 18.6921 # Vegamag #for some reason stsyn.Vega doesn't load so we redefine it stsyn.Vega = SourceSpectrum.from_vega() obs = Observation( stsyn.Vega, bp, binset=bp.binset ) # synthetic observation of vega in bandpass using vega spectrum vegamag = -obs.effstim(flux_unit='obmag', area=stsyn.conf.area) return obsmode, photplam.value, bandwidth.value, photflam.value, stmag, abmag, vegamag.value
def _observe_through_filter(bp, B, unit): if not synphot: raise AstropyWarning('synphot is required for bandpass filtering.') obs = Observation(B, bp) wave = obs.effective_wavelength() fluxd = obs.effstim(unit) return wave, fluxd
def get_phot(spec, bands): """ Compute the fluxes in the requested bands. Parameters ---------- spec : SpecData object the spectrum bands: list of strings bands requested Outputs ------- band fluxes : numpy array calculated band fluxes """ # create a SourceSpectrum object from the spectrum, excluding the bad regions spectrum = SourceSpectrum( Empirical1D, points=spec.waves.to(u.Angstrom)[spec.npts != 0], lookup_table=spec.fluxes[spec.npts != 0], ) # path for band response curves band_path = pkg_resources.resource_filename("measure_extinction", "data/Band_RespCurves/") # dictionary linking the bands to their response curves bandnames = { "J": "2MASSJ", "H": "2MASSH", "K": "2MASSKs", "IRAC1": "IRAC1", "IRAC2": "IRAC2", "WISE1": "WISE1", "WISE2": "WISE2", "L": "AAOL", "M": "AAOM", } # define the units of the output fluxes funit = u.erg / (u.s * u.cm * u.cm * u.Angstrom) # compute the flux in each band fluxes = np.zeros(len(bands)) for k, band in enumerate(bands): # create the bandpass (as a SpectralElement object) bp = SpectralElement.from_file( "%s%s.dat" % (band_path, bandnames[band])) # assumes wavelengths are in Angstrom!! # integrate the spectrum over the bandpass, only if the bandpass fully overlaps with the spectrum (this actually excludes WISE2) if bp.check_overlap(spectrum) == "full": obs = Observation(spectrum, bp) fluxes[k] = obs.effstim(funit).value else: fluxes[k] = np.nan return fluxes
def filter_vega_zp(filterfile, filterzero): ''' ###################################################### # Input # # -------------------------------------------------- # # filterfile: file containing filter function # # filterzero: flux corresponding to mag=0 # # -------------------------------------------------- # # Output # # -------------------------------------------------- # # mag_0: magnitude of Vega in filter system. # ###################################################### ''' from synphot import SourceSpectrum, SpectralElement, Observation #load Vega spectrum spec = SourceSpectrum.from_vega() #Load ascii filter function if filterfile.split('/')[-1][:6] == 'Bessel': filt = SpectralElement.from_file(filterfile, wave_unit='nm') else: filt = SpectralElement.from_file(filterfile, wave_unit='AA') wave = filt.waveset #Synthetic observation obs = Observation(spec, filt) flux = obs.effstim(flux_unit='jy', waverange=(wave[0],wave[-1])) #Calibrate to zero point (zp) mag_0 = -2.512*np.log10(flux/filterzero) return mag_0
def kvega_to_f335abmag(self, kmag): '''Convert K band vegamag to F335M ABmag''' kmag = kmag * units.VEGAMAG new_g2v = self.g2v.normalize(kmag, band=self.kband, area=self.telescope_area, vegaspec=self.vega) obs = Observation(new_g2v, self.bp, binset=self.wave_bins) abmag = obs.effstim('ABmag', area=self.telescope_area) return abmag
def f335abmag_to_kvega(self, abmag): '''Convert F335M ABmag to K band vegamag''' abmag = abmag * u.ABmag new_g2v = self.g2v.normalize(abmag, band=self.bp, area=self.telescope_area, vegaspec=self.vega) obs = Observation(new_g2v, self.kband, binset=self.wave_bins) kmag = obs.effstim('Vegamag', area=self.telescope_area, vegaspec=self.vega) return kmag
def zero_mag_flux(filter_name, photometric_system, return_filter=False): """ Returns the zero magnitude photon flux for a filter Acceptable filter names are those given in ``scopesim.effects.ter_curves_utils.FILTER_DEFAULTS`` or a string with an appropriate name for a filter in the Spanish-VO filter-service. Such strings must use the naming convention: observatory/instrument.filter. E.g: ``paranal/HAWKI.Ks``, or ``Gemini/GMOS-N.CaT``. Parameters ---------- filter_name : str Name of the filter - see above photometric_system : str ["vega", "AB", "ST"] Name of the photometric system return_filter : bool, optional If True, also returns the filter curve object Returns ------- flux : float [PHOTLAM] filt : ``synphot.SpectralElement`` If ``return_filter`` is True """ filt = get_filter(filter_name) spec = get_zero_mag_spectrum(photometric_system) obs = Observation(spec, filt) flux = obs.effstim(flux_unit=PHOTLAM) if return_filter: return flux, filt else: return flux
def synphot_calcs(self, bpfile): """ Calculate zeropoint for a given bandpass in several photometric systems Arguments: ---------- bpfile -- Text file containing the throuput table for a single bandpass Returns: -------- Bandpass zeropoint value in Vega mags, AB mags, ST mags, photflam, photfnu, and megajansky. Also returns pivot wavelength """ # Define wavelength list to use #wave_bins = np.arange(0.5, 5, 0.1) * u.micron # Use refactored synphot to calculate zeropoints orig_bp = SpectralElement.from_file(bpfile) # Now reduce the PCE curve by a factor equal to # the gain, in order to get the output to translate # from ADU/sec rather than e/sec bp = orig_bp / self.gain #bp.model.lookup_table = bp.model.lookup_table / self.gain photflam = bp.unit_response(self.telescope_area) photplam = bp.pivot() st_zpt = -2.5 * np.log10(photflam.value) - 21.1 ab_zpt = (-2.5 * np.log10(photflam.value) - 21.1 - 5 * np.log10(photplam.value) + 18.6921) #mjy_zpt = units.convert_flux(photplam,photflam, 'MJy') mjy_zpt = photflam.to(u.MJy, u.spectral_density(photplam)) obs = Observation(self.vega, bp, binset=wave_bins) vega_zpt = -obs.effstim(flux_unit='obmag', area=self.telescope_area) photfnu = units.convert_flux(photplam, photflam, units.FNU) return (vega_zpt.value, ab_zpt, st_zpt, photflam.value, photfnu.value, mjy_zpt.value, photplam.value)