Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize NIRCam spectra support, updates for STIS, etc. #119

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion measure_extinction/extdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"SpeX_SXD",
"SpeX_LXD",
"IRS",
"NIRCam_SS",
"MIRI_IFU",
]

Expand Down Expand Up @@ -263,7 +264,7 @@ class ExtData:
"""
Extinction for a single line-of-sight

Atributes:
Attributes:

type : string
extinction curve type (e.g., elx or alax)
Expand Down
7 changes: 4 additions & 3 deletions measure_extinction/merge_obsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def merge_gen_obsspec(obstables, wave_range, output_resolution=100):
Parameters
----------
obstables : list of astropy Table objects
list of tables containing the observed IRS spectra
list of tables containing the observed spectra
usually the result of reading tables

wave_range : 2 element float
Expand Down Expand Up @@ -367,7 +367,7 @@ def merge_irs_obsspec(obstables, output_resolution=150):
"""
wave_range = [5.0, 40.0] * u.micron
otable = merge_gen_obsspec(
obstables, wave_range, output_resolution=output_resolution
obstables, wave_range, output_resolution=output_resolution,
)
return otable

Expand Down Expand Up @@ -421,6 +421,7 @@ def merge_miri_ifu_obsspec(obstables, output_resolution=3000):
"""
wave_range = [4.8, 29.0] * u.micron
otable = merge_gen_obsspec(
obstables, wave_range, output_resolution=output_resolution
obstables, wave_range, output_resolution=output_resolution,
)

return otable
6 changes: 3 additions & 3 deletions measure_extinction/modeldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def stellar_sed(self, params, velocity=None):

return sed

def dust_extinguished_sed_FM90_G23(self, params, sed, velocity=0.0):
def dust_extinguished_sed(self, params, sed, velocity=0.0):
"""
Dust extinguished sed given the extinction parameters

Expand Down Expand Up @@ -254,7 +254,7 @@ def dust_extinguished_sed_FM90_G23(self, params, sed, velocity=0.0):
params[6],
optnir_axav_x.value,
optnir_axav_y,
[0.2, 11.0],
[0.033, 11.0],
"FM90_G23_measure_extinction",
)
ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0]))
Expand All @@ -270,7 +270,7 @@ def dust_extinguished_sed_FM90_G23(self, params, sed, velocity=0.0):

return ext_sed

def dust_extinguished_sed(self, params, sed, velocity=0.0):
def dust_extinguished_sed_FM04(self, params, sed, velocity=0.0):
"""
Dust extinguished sed given the extinction parameters

Expand Down
86 changes: 69 additions & 17 deletions measure_extinction/stardata.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
self.band_waves = OrderedDict()
self.band_fluxes = OrderedDict()

def read_bands(self, lines):
def read_bands(self, lines, only_bands=None):
"""
Read the photometric band data from a DAT file
and upate class variables.
Expand All @@ -92,6 +92,8 @@
----------
lines : list of string
lines from a DAT formatted file
only_bands : list
Only read in the bands given

Returns
-------
Expand All @@ -110,15 +112,23 @@
elif line.find(";") != -1 and line.find("mJy") != -1:
colpos = min(line.find(";"), line.find("mJy"))
band_name = line[0:eqpos].strip()
self.bands[band_name] = (
float(line[eqpos + 1 : pmpos]),
float(line[pmpos + 3 : colpos]),
)
# units
if line.find("mJy") >= 0:
self.band_units[band_name] = "mJy"

save_band = False
if only_bands is None:
save_band = True
else:
self.band_units[band_name] = "mag"
if band_name in only_bands:
save_band = True

Check warning on line 121 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L120-L121

Added lines #L120 - L121 were not covered by tests
if save_band:
self.bands[band_name] = (
float(line[eqpos + 1 : pmpos]),
float(line[pmpos + 3 : colpos]),
)
# units
if line.find("mJy") >= 0:
self.band_units[band_name] = "mJy"
else:
self.band_units[band_name] = "mag"

self.n_bands = len(self.bands)

Expand Down Expand Up @@ -815,6 +825,30 @@
self.fluxes = self.fluxes.value * (u.Jy)
self.uncs = self.uncs.value * (u.Jy)

def read_nircam_ss(self, line, path="./"):
"""
Read in Webb/NIRCam slitless spectra

Parameters
----------
line : string
formatted line from DAT file
example: 'NIRCam_SS = hd029647_irs.fits'

path : string, optional
location of the FITS files path

Returns
-------
Updates self.(file, wave_range, waves, flux, uncs, npts, n_waves)
"""
self.read_spectra(line, path)

Check warning on line 845 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L845

Added line #L845 was not covered by tests

self.fluxes = self.fluxes.to(

Check warning on line 847 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L847

Added line #L847 was not covered by tests
fluxunit, equivalencies=u.spectral_density(self.waves)
)
self.uncs = self.uncs.to(fluxunit, equivalencies=u.spectral_density(self.waves))

Check warning on line 850 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L850

Added line #L850 was not covered by tests

def read_miri_ifu(self, line, path="./"):
"""
Read in Webb/MRS IFU spectra
Expand All @@ -823,7 +857,7 @@
----------
line : string
formatted line from DAT file
example: 'IRS = hd029647_irs.fits'
example: 'MIRI_IFU = hd029647_irs.fits'

path : string, optional
location of the FITS files path
Expand All @@ -834,9 +868,6 @@
"""
self.read_spectra(line, path)

# add units
self.fluxes = self.fluxes.value * u.Jy
self.uncs = self.uncs.value * u.Jy
self.fluxes = self.fluxes.to(
fluxunit, equivalencies=u.spectral_density(self.waves)
)
Expand Down Expand Up @@ -934,7 +965,13 @@
"""

def __init__(
self, datfile, path="", photonly=False, use_corfac=True, deredden=False
self,
datfile,
path="",
photonly=False,
use_corfac=True,
deredden=False,
only_bands=None,
):
"""
Parameters
Expand All @@ -955,6 +992,9 @@
deredden : boolean [default=False]
Deredden the data based on dereddening parameters given in the DAT file.
Generally used to deredden standards.

only_bands : list
Only read in the bands given
"""
self.file = datfile
self.path = path
Expand All @@ -969,9 +1009,9 @@
self.dereddened = deredden

if self.file is not None:
self.read(deredden=deredden)
self.read(deredden=deredden, only_bands=only_bands)

def read(self, deredden=False):
def read(self, deredden=False, only_bands=None):
"""
Populate the object from a DAT file + spectral files

Expand All @@ -980,6 +1020,8 @@
deredden : boolean [default=False]
Deredden the data based on dereddening parameters given in the DAT file.
Generally used to deredden standards.
only_bands : list
Only read in the bands given
"""

# open and read all the lines in the file
Expand All @@ -988,7 +1030,7 @@
f.close()
# get the photometric band data
self.data["BAND"] = BandData("BAND")
self.data["BAND"].read_bands(self.datfile_lines)
self.data["BAND"].read_bands(self.datfile_lines, only_bands=only_bands)

# covert the photoemtric band data to fluxes in all possible bands
self.data["BAND"].get_band_fluxes()
Expand Down Expand Up @@ -1093,6 +1135,16 @@
)
else:
warnings.warn(f"{fname} does not exist", UserWarning)
elif line.find("NIRCam_SS") == 0:
fname = _getspecfilename(line, self.path)
if os.path.isfile(fname):
self.data["NIRCam_SS"] = SpecData("NIRCam_SS")
self.data["NIRCam_SS"].read_miri_ifu(

Check warning on line 1142 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L1139-L1142

Added lines #L1139 - L1142 were not covered by tests
line,
path=self.path,
)
else:
warnings.warn(f"{fname} does not exist", UserWarning)

Check warning on line 1147 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L1147

Added line #L1147 was not covered by tests
elif line.find("MIRI_IFU") == 0:
fname = _getspecfilename(line, self.path)
if os.path.isfile(fname):
Expand Down
72 changes: 67 additions & 5 deletions measure_extinction/utils/merge_miri_ifu_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import argparse
import numpy as np
import pkg_resources
import matplotlib.pyplot as plt

from astropy.table import QTable
import astropy.units as u

from measure_extinction.merge_obsspec import merge_miri_ifu_obsspec


fluxunit = u.erg / (u.cm * u.cm * u.s * u.angstrom)


if __name__ == "__main__":

# commandline parser
Expand All @@ -31,15 +36,15 @@
parser.add_argument("--pdf", help="save figure as a pdf file", action="store_true")
args = parser.parse_args()

sfilename = f"{args.inpath}{args.starname}*.fits"
print(sfilename)
sfilename = f"{args.inpath}{args.starname}*order*.fits"

Check warning on line 39 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L39

Added line #L39 was not covered by tests
sfiles = glob.glob(sfilename)
print(sfiles)
stable = []
for cfile in sfiles:
print(cfile)
cdata = QTable.read(cfile)
cdata.rename_column("FLUX_ERROR", "ERROR")
cdata.rename_column("wavelength", "WAVELENGTH")
cdata.rename_column("flux", "FLUX")
cdata.rename_column("unc", "ERROR")
cdata["WAVELENGTH"] *= u.micron

Check warning on line 47 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L44-L47

Added lines #L44 - L47 were not covered by tests
cdata["NPTS"] = np.full((len(cdata["FLUX"])), 1.0)
cdata["NPTS"][cdata["FLUX"] == 0.0] = 0.0
stable.append(cdata)
Expand All @@ -51,3 +56,60 @@
outname = args.starname.lower()
mrs_file = f"{outname}_miri_ifu.fits"
rb_mrs.write(f"{args.outpath}/{mrs_file}", overwrite=True)

# plot the original and merged Spectra
fontsize = 14
font = {"size": fontsize}
plt.rc("font", **font)
plt.rc("lines", linewidth=2)
plt.rc("axes", linewidth=2)
plt.rc("xtick.major", width=2)
plt.rc("ytick.major", width=2)

Check warning on line 67 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L61-L67

Added lines #L61 - L67 were not covered by tests

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 5.5))

Check warning on line 69 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L69

Added line #L69 was not covered by tests

for ctable in stable:
gvals = ctable["NPTS"] > 0
cfluxes = (

Check warning on line 73 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L71-L73

Added lines #L71 - L73 were not covered by tests
ctable["FLUX"]
.to(fluxunit, equivalencies=u.spectral_density(ctable["WAVELENGTH"]))
.value
)
ax.plot(

Check warning on line 78 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L78

Added line #L78 was not covered by tests
ctable["WAVELENGTH"][gvals],
cfluxes[gvals],
"k-",
alpha=0.5,
label="orig",
)
gvals = rb_mrs["NPTS"] > 0
cfluxes = (

Check warning on line 86 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L85-L86

Added lines #L85 - L86 were not covered by tests
rb_mrs["FLUX"]
.to(fluxunit, equivalencies=u.spectral_density(ctable["WAVELENGTH"]))
.value
)

ax.plot(

Check warning on line 92 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L92

Added line #L92 was not covered by tests
rb_mrs["WAVELENGTH"][gvals].to(u.micron),
rb_mrs["FLUX"][gvals],
"b-",
alpha=0.5,
label="merged",
)

ax.set_xlabel(r"$\lambda$ [$\AA$]")
ax.set_ylabel(r"F($\lambda$)")

Check warning on line 101 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L100-L101

Added lines #L100 - L101 were not covered by tests

ax.set_xscale("log")
ax.set_yscale("log")

Check warning on line 104 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L103-L104

Added lines #L103 - L104 were not covered by tests

ax.legend()
fig.tight_layout()

Check warning on line 107 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L106-L107

Added lines #L106 - L107 were not covered by tests

fname = mrs_file.replace(".fits", "")
if args.png:
fig.savefig(f"{fname}.png")
elif args.pdf:
fig.savefig(f"{fname}.pdf")

Check warning on line 113 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L109-L113

Added lines #L109 - L113 were not covered by tests
else:
plt.show()

Check warning on line 115 in measure_extinction/utils/merge_miri_ifu_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_miri_ifu_spec.py#L115

Added line #L115 was not covered by tests
Loading
Loading