@@ -20,41 +20,57 @@ using Measurements, Cosmology, CairoMakie, SkyCoords
2020using Spectra
2121```
2222
23- ## Part 1: Creating a spectrum
23+ ## Part 1: Loading a real SDSS spectrum
2424
25- We build a synthetic galaxy spectrum — a smooth stellar continuum plus an Hα emission line.
25+ We use a publicly available spectrum from SDSS DR14 — a galaxy on
26+ plate 1323, MJD 52797, fiber 12. The FITS file stores flux in units
27+ of 10⁻¹⁷ erg s⁻¹ cm⁻² Å⁻¹ and an ` ivar ` (inverse-variance) column
28+ that encodes real per-pixel measurement uncertainties.
2629``` julia
27- using Unitful, UnitfulAstro, Measurements, CairoMakie, SkyCoords
28- using Spectra, DustExtinction, Cosmology
30+ using Downloads, FITSIO, Unitful, UnitfulAstro, Measurements
2931
30- # Synthetic galaxy: continuum + Hα emission line
31- wave_raw = collect (range (3815.0 , 9206.6 , length = 3827 ))
32- flux_raw = @. 2.5e-17 * exp (- ((wave_raw - 6000.0 )/ 2000.0 )^ 2 ) +
33- 8e-16 * exp (- ((wave_raw - 6563.0 )/ 5.0 )^ 2 )
32+ # Download the spectrum (skipped if already present)
33+ sdss_url = " https://dr14.sdss.org/optical/spectrum/view/data/format=fits/spec=lite" *
34+ " ?plateid=1323&mjd=52797&fiberid=12"
35+ sdss_file = joinpath (@__DIR__ , " sdss_example.fits" )
36+ isfile (sdss_file) || Downloads. download (sdss_url, sdss_file)
3437
35- # 5% per-pixel noise (simulating SDSS ivar: σ = 1/√ivar)
36- sigma_raw = abs .(flux_raw) .* 0.05 .+ 1e-18
38+ # Read spectral columns
39+ f = FITS (sdss_file)
40+ loglam = read (f[2 ], " loglam" ) # log₁₀(λ / Å)
41+ flux_raw = read (f[2 ], " flux" ) # 10⁻¹⁷ erg s⁻¹ cm⁻² Å⁻¹
42+ ivar = read (f[2 ], " ivar" ) # inverse variance
43+ close (f)
3744
38- # Attach physical units + Measurements.jl uncertainties
39- wave_aa = wave_raw .* u " angstrom "
40- flux_meas = measurement .(flux_raw, sigma_raw) .* ( 1e-17 u " erg/s/cm^2/ angstrom" )
45+ # Convert wavelength + attach units
46+ wave_raw = 10 .^ loglam # Å (plain numbers for dust map)
47+ wave_aa = wave_raw .* u " angstrom" # with units
4148
49+ # Real per-pixel σ from ivar: σ = 1/√ivar
50+ sigma_raw = map (iv -> iv > 0 ? 1 / sqrt (iv) : 0.0 , ivar)
51+
52+ # Attach units + Measurements.jl uncertainties
53+ flux_meas = ((flux_raw .± sigma_raw) .* 1e-17 ) * u " erg/s/cm^2/angstrom"
54+
55+ using Spectra
4256spec = spectrum (wave_aa, flux_meas)
57+
4358println (" Wavelength : " , round (wave_raw[1 ], digits= 1 ),
44- " – " , round (wave_raw[end ], digits= 1 ), " Å" )
59+ " — " , round (wave_raw[end ], digits= 1 ), " Å" )
4560println (" Pixels : " , length (wave_raw))
61+ println (" Example px : " , flux_meas[100 ]) # shows value ± uncertainty
4662```
4763
4864Plot the spectrum with its uncertainty band:
4965``` julia
50- wv = ustrip .(spec . wave )
51- fv = Measurements. value .(ustrip .(spec . flux ))
52- fe = Measurements. uncertainty .(ustrip .(spec . flux ))
66+ wv = ustrip .(wave_aa )
67+ fv = Measurements. value .(ustrip .(flux_meas ))
68+ fe = Measurements. uncertainty .(ustrip .(flux_meas ))
5369
5470fig, ax = lines (wv, fv;
5571 axis = (xlabel = " Wavelength (Å)" ,
5672 ylabel = " Flux Density (erg s⁻¹ cm⁻² Å⁻¹)" ,
57- title = " Synthetic Galaxy Spectrum " ))
73+ title = " SDSS Galaxy — plate 1323, fiber 12 " ))
5874band! (ax, wv, fv .- fe, fv .+ fe; color = (:steelblue , 0.3 ))
5975fig
6076```
0 commit comments