; docformat = 'rst' ; ; NAME: ; specToMag ; PURPOSE: ; Compute the ST, AB, or Vega magnitude of a spectrum given a filter ; ;+ ; :Description: ; Compute the ST, AB, or Vega magnitude of a spectrum given a filter ; ; :Categories: ; Colors ; ; :Params: ; spectrum: in, required, type=structure ; A structure with the spectrum. Fields are:: ; .wave: wavelength for each spectral bin ; .WavelengthUnit: unit of wavelength (micron|angstrom) ; .flux: associated flux ; filter: in, required, type=structure ; A structure defining the filter. Fields are:: ; .wave: wavelength for each spectral bin (micron|angstrom) ; .WavelengthUnit: unit of wavelength (micron|angstrom) ; .trans: associated transmission ; ; :Keywords: ; AB: in, optional, type=boolean, default=0 ; If set, returns the magnitude in the AB system [Oke, J.B. 1974, ApJS, 27, 21] ; ST: in, optional, type=boolean, default=0 ; If set, returns the magnitude in the STMAG system [Stone, R.P.S. 1996, ApJS, 107, 423] ; Vega: in, optional, type=boolean, default=1 ; If set, returns the magnitude in the Vega system [Bessel, M. S. 2005, ARA&A, 43, 293] ; ; :Returns: The magnitude in the specified filter, in either AB or Vega ; ; :Examples: ; ; :Uses: ; readcol ; ; :Author: ; B.Carry (OCA) ; ; :History: ; Change History:: ; Written in August 2017, B. Carry (OCA) ;- function specToMag, spectrum, filter, AB=AB, ST=ST, Vega=Vega COMPILE_OPT hidden, idl2 ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--- TAG --- I -- Initialization And Input Verification -----------------------; ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--I.1-- Check Input Parameters --------------------------------------------------------------; if n_params() lt 2 then begin message, /ioError, 'syntax: mag = specToMag( spectrum, filter [, /AB, /ST, /Vega] )' return, -1 endif ;--I.2-- Check Validity of Input Spectra -----------------------------------------------------; requiredTag = ['wave','wavelengthunit','flux'] nbTag=n_elements(requiredTag) for kTag=0, nbTag-1 do begin if not tag_exist( spectrum, requiredTag[kTag] ) then begin message, 'Field '+requiredTag[kTag]+' missing in spectrum structure' return, -1 endif endfor ;--I.3-- Check Validity of Input Filter ------------------------------------------------------; requiredTag = ['wave','wavelengthunit','wavelengthpivot','trans'] nbTag=n_elements(requiredTag) for kTag=0, nbTag-1 do begin if not tag_exist( filter, requiredTag[kTag] ) then begin message, 'Field '+requiredTag[kTag]+' missing in filter structure' return, -1 endif endfor ;--I.4-- Type of Magnitude: Vega or AB -------------------------------------------------------; ;--I.4.1-- Avoid Double Calls photSys=[keyword_set(AB), keyword_set(ST), keyword_set(Vega)] if total(photSys) eq 0 then Vega=1 if total(photSys) gt 1 then begin ST = 0 AB = 0 Vega= 1 endif ;--I.4.2-- Set Reference Magnitude Constant constMag = 0 if keyword_set(AB) then constMag = -48.6 if keyword_set(ST) then constMag = -21.1 ;--I.5-- Constants and Vega Spectrum ---------------------------------------------------------; vegaFile='/astrodata/Catalog/Taxonomy/SolarColors/templates/kurucz9500-4.0-0.5.csv' cSpeed = 299792458.0d ;-m/s cAngPerSec = cSpeed * 1e10 ;-Angstrom/s minTrans = 1.0e-5 ;-Minimum transmission to consider ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--- TAG --- II -- Wavelength Homogeneization -----------------------; ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--II.1-- Wavelength in Angstrom -------------------------------------------------------------; case strLowCase(spectrum.WavelengthUnit) of 'micron': wSpec=spectrum.wave*1e4 'nm': wSpec=spectrum.wave*1e1 else: wSpec=spectrum.wave endcase case strLowCase(filter.WavelengthUnit) of 'micron': wFilt=filter.wave*1e4 'nm': wFilt=filter.wave*1e1 else: wFilt=filter.wave endcase ;--II.2-- Spectral Range of Interest ---------------------------------------------------------; pFilt = where( filter.trans ge minTrans ) pSpec = where( wSpec ge min(wFilt[pFilt]) and $ wSpec le max(wFilt[pFilt]) ) ;--II.3-- Spectrum Wavelength Interval -------------------------------------------------------; dS = wSpec[pSpec]-shift(wSpec[pSpec],1) dS = median(dS[where(dS gt 0)]) ;--II.4-- Filter Wavelength Interval ---------------------------------------------------------; dF = wFilt[pFilt]-shift(wFilt[pFilt],1) dF = median(dF[where(dF gt 0)]) ;--II.5-- Define Reference Wavelength Vector -------------------------------------------------; ;--II.5.1-- Spectrum is the Reference if dS lt dF then begin ;--II.5.1/A-- Keep Spectrum uniWave = wSpec uniSpec = spectrum.flux ;--II.5.1/B-- Interpolate Filter uniFilt = interpol( filter.trans, wFilt, uniWave ) ;--II.5.1/C-- Clean Filter bad = where(uniFilt lt 0) if bad[0] ne -1 then uniFilt[bad]=0 ;--II.5.2-- Filter is the Reference endif else begin ;--II.5.2/A-- Keep Filter uniWave = wFilt uniFilt = filter.trans ;--II.5.2/B-- Interpolate Spectrum uniSpec = interpol( spectrum.flux, wSpec, uniWave ) ;--II.5.2/C-- Clean Spectrum bad = where(uniSpec lt 0) if bad[0] ne -1 then uniSpec[bad]=0 endelse ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--- TAG --- III -- Compute Magnitude in the Filter Bandpass -----------------------; ;-----------------------------------------------------------------------------------------------; ;-----------------------------------------------------------------------------------------------; ;--III.1-- Spectra times Transmission --------------------------------------------------------; sumSpec = total( uniSpec*uniFilt ) sumNorm = total( uniFilt ) magInst = -2.5*alog10( sumSpec/sumNorm ) ;--III.2-- Zero Point: Vega System -----------------------------------------------------------; if keyword_set(Vega) then begin ;--III.1.1-- Read Template Spectrum of Vega readcol, vegaFile, wv,fv,/silent ;--III.1.2-- Interpolate Vega over Reference Wavelength uniNorm = interpol( fv, wv, uniWave ) ;--III.1.3-- Clean Vega bad = where(uniNorm lt 0) if bad[0] ne -1 then uniNorm[bad]=0 ZP = -2.5*alog10( total( uniNorm*uniFilt )/sumNorm ) endif ;--III.3-- Zero Point: AB System -------------------------------------------------------------; if keyword_set(AB) then begin case strLowCase(filter.WavelengthUnit) of 'angstrom': pivot=filter.WavelengthPivot*filter.WavelengthPivot /cAngPerSec 'micron': pivot=filter.WavelengthPivot*filter.WavelengthPivot*(1e8)/cAngPerSec 'nm': pivot=filter.WavelengthPivot*filter.WavelengthPivot*(1e2)/cAngPerSec else: pivot=filter.WavelengthPivot*filter.WavelengthPivot*(1e2)/cSpeed endcase ZP = 48.6 + 2.5*alog10( pivot ) endif ;--III.4-- Zero Point: ST System -------------------------------------------------------------; if keyword_set(ST) then ZP = 21.1 ;--III.5-- Compute Magnitude -----------------------------------------------------------------; mag = MagInst - ZP return, mag end