Skip to content

Instantly share code, notes, and snippets.

@andycasey
Created September 18, 2025 23:58
Show Gist options
  • Select an option

  • Save andycasey/a2c0cc88ca3edf406516dceb0b1594aa to your computer and use it in GitHub Desktop.

Select an option

Save andycasey/a2c0cc88ca3edf406516dceb0b1594aa to your computer and use it in GitHub Desktop.
Plotting all BOSS exposures taken in a night
import os
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
np.random.seed(16)
expand_path = lambda path: os.path.expandvars(os.path.expanduser(path))
spAll_path = expand_path(
f"$SAS_BASE_DIR/ipl-4/spectro/boss/redux/v6_2_1/summary/daily/spAll-v6_2_1.fits.gz"
)
def get_specFull_path(spec_file):
_, field_id, mjd, *_ = spec_file.split("-")
field_id, mjd = map(int, (field_id, mjd))
return expand_path(
"$SAS_BASE_DIR/ipl-4/spectro/boss/redux/v6_2_1/spectra/daily/full/"
f"{str(field_id // 1000):0>3}XXX/{str(field_id).zfill(6)}/"
f"{mjd}/{spec_file}"
)
# This part takes a long time because the spAll is large and compressed
with fits.open(spAll_path) as spAll:
spec_files = spAll[1].data["SPEC_FILE"]
# Let's select a random visit spectrum
spec_file, = np.random.choice(spec_files, size=1)
specFull_path = get_specFull_path(spec_file)
with fits.open(specFull_path) as image:
# specFull data model is here: https://data.sdss.org/dsi/file/select-specFull/main/section-bhm
# extensions 5 onwards (0-indexed) contain individual exposures
# Let's plot all the individual exposures for that night
cmap = plt.get_cmap("tab10")
fig, ax = plt.subplots(figsize=(8, 8), dpi=300)
offset = 0
for i, hdu in enumerate(image[5:]):
λ = 10**hdu.data["LOGLAM"]
flux = hdu.data["FLUX"] + offset
ivar = hdu.data["IVAR"]
c = cmap(i)
ax.plot(λ, flux, c=c, ds="steps-mid", label=f"{hdu.header['MJD']} {hdu.header['EXPOSURE']}")
ax.fill_between(λ, flux - 1/np.sqrt(ivar), flux + 1/np.sqrt(ivar), color=c, step="pre", alpha=0.2)
offset += np.nanmedian(hdu.data["FLUX"])
ax.set_ylim(0, ax.get_ylim()[1])
ax.set_ylabel(f"Flux + offset (10$^{{-17}}$ erg/s/cm$^2$/Å)")
ax.set_xlabel("Wavelength (Å)")
ax.legend()
figure_path = f"{spec_file[:-5]}.png"
fig.savefig(figure_path, bbox_inches="tight")
@andycasey
Copy link
Author

Example output:

spec-101179-59744-27021597906270740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment