import os
import fitsio
import numpy as np
from lsst_inaf_agile import util
[docs]
FIL: dict[str, np.ndarray] = {}
[docs]
def get_band_egg(band, filter_db):
"""
Return the EGG filter transmission curve corresponding to the 'band'.
The data model follows that of the EGG FITS filters.
"""
if band in FIL:
return FIL[band]
band = band.replace("magabs_", "")
# 20250703: there was an unsafe ignored condition if filter was not found
# in the filter-db
filename = None
with open(filter_db, "r") as f:
for line in f:
name, _filename = line.strip().split("=")
if name == band:
filename = _filename
if filename is None:
raise ValueError("Filter {band} not found")
dirname = os.path.dirname(filter_db)
FIL[band] = util.read_fits(f"{dirname}/{filename}")
return FIL[band]
[docs]
def get_flux_band(lam, flux, band, filter_db):
"""
Integrate the SED through the filter transmission curve.
Parameters
----------
lam:
wavelength in microns
flux:
flux in uJy
band: str
EGG filter band filename
filter_db: str
EGG filter database filename
"""
# Get the filter
fil = get_band_egg(band, filter_db)
lam_filter = fil["LAM"][0]
res_filter = fil["RES"][0]
# 20241001
# NOTE: narrow-band filters? Estimate the flux at the lambda of the filter instead...
is_narrow_band = np.max(lam_filter) - np.min(lam_filter) < 1e-3
if is_narrow_band:
flux_filter = np.interp(lam_filter, lam, flux, left=0, right=0)
ret1 = np.trapezoid(flux_filter * res_filter, x=lam_filter)
return ret1
# Estimate and renormalize the filter at the datapoints
res_filter = np.interp(lam, lam_filter, res_filter, left=0, right=0)
norm = np.trapezoid(res_filter, lam)
# NOTE: no overlap between wavelengths and filter lambda?
if norm == 0:
return 0
res_filter /= norm
ret2 = np.trapezoid(flux * res_filter, x=lam)
return ret2
[docs]
def write(filename, lam_in_um, flux_in_uJy):
"""Save the SED in EGG format."""
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
fitsio.write(filename, {"LAMBDA": np.array([lam_in_um]), "FLUX": np.array([flux_in_uJy])}, clobber=True)