diff --git a/measure_extinction/extdata.py b/measure_extinction/extdata.py index 7ddda16..7c659ab 100644 --- a/measure_extinction/extdata.py +++ b/measure_extinction/extdata.py @@ -27,6 +27,7 @@ "SpeX_SXD", "SpeX_LXD", "IRS", + "NIRCam_SS", "MIRI_IFU", ] @@ -262,7 +263,7 @@ class ExtData: """ Extinction for a single line-of-sight - Atributes: + Attributes: type : string extinction curve type (e.g., elx or alax) diff --git a/measure_extinction/stardata.py b/measure_extinction/stardata.py index 7d0bec1..cd1dd64 100644 --- a/measure_extinction/stardata.py +++ b/measure_extinction/stardata.py @@ -825,6 +825,30 @@ def read_irs(self, line, path="./", use_corfac=True, corfac=None): 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) + + self.fluxes = self.fluxes.to( + fluxunit, equivalencies=u.spectral_density(self.waves) + ) + self.uncs = self.uncs.to(fluxunit, equivalencies=u.spectral_density(self.waves)) + def read_miri_ifu(self, line, path="./"): """ Read in Webb/MRS IFU spectra @@ -833,7 +857,7 @@ def read_miri_ifu(self, line, path="./"): ---------- 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 @@ -1106,6 +1130,16 @@ def read(self, deredden=False, only_bands=None): ) 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( + line, + path=self.path, + ) + else: + warnings.warn(f"{fname} does not exist", UserWarning) elif line.find("MIRI_IFU") == 0: fname = _getspecfilename(line, self.path) if os.path.isfile(fname): diff --git a/measure_extinction/utils/merge_miri_ifu_spec.py b/measure_extinction/utils/merge_miri_ifu_spec.py index 24a6cce..7bca238 100644 --- a/measure_extinction/utils/merge_miri_ifu_spec.py +++ b/measure_extinction/utils/merge_miri_ifu_spec.py @@ -37,14 +37,10 @@ args = parser.parse_args() sfilename = f"{args.inpath}{args.starname}*order*.fits" - print(sfilename) sfiles = glob.glob(sfilename) - print(sfiles) stable = [] for cfile in sfiles: - print(cfile) cdata = QTable.read(cfile) - print(cdata.colnames) cdata.rename_column("wavelength", "WAVELENGTH") cdata.rename_column("flux", "FLUX") cdata.rename_column("unc", "ERROR") @@ -104,6 +100,9 @@ ax.set_xlabel(r"$\lambda$ [$\AA$]") ax.set_ylabel(r"F($\lambda$)") + ax.set_xscale("log") + ax.set_yscale("log") + ax.legend() fig.tight_layout() diff --git a/measure_extinction/utils/merge_nircam_spec.py b/measure_extinction/utils/merge_nircam_spec.py new file mode 100644 index 0000000..55401e0 --- /dev/null +++ b/measure_extinction/utils/merge_nircam_spec.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python + +import glob +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_nircam_ss_obsspec + + +fluxunit = u.erg / (u.cm * u.cm * u.s * u.angstrom) + + +if __name__ == "__main__": + + # commandline parser + parser = argparse.ArgumentParser() + parser.add_argument("starname", help="name of star (filebase)") + parser.add_argument( + "--inpath", + help="path where original data files are stored", + default=pkg_resources.resource_filename("measure_extinction", "data/Orig"), + ) + parser.add_argument( + "--outpath", + help="path where merged spectra will be stored", + default=pkg_resources.resource_filename("measure_extinction", "data/Out"), + ) + parser.add_argument("--outname", help="Output filebase") + parser.add_argument("--png", help="save figure as a png file", action="store_true") + parser.add_argument("--eps", help="save figure as an eps file", action="store_true") + parser.add_argument("--pdf", help="save figure as a pdf file", action="store_true") + args = parser.parse_args() + + sfilename = f"{args.inpath}{args.starname}*order*.fits" + sfiles = glob.glob(sfilename) + stable = [] + for cfile in sfiles: + cdata = QTable.read(cfile) + cdata.rename_column("wavelength", "WAVELENGTH") + cdata.rename_column("flux", "FLUX") + cdata.rename_column("unc", "ERROR") + cdata["WAVELENGTH"] *= u.micron + cdata["NPTS"] = np.full((len(cdata["FLUX"])), 1.0) + cdata["NPTS"][cdata["FLUX"] == 0.0] = 0.0 + stable.append(cdata) + + rb_mrs = merge_nircam_ss_obsspec(stable) + if args.outname: + outname = args.outname + else: + outname = args.starname.lower() + mrs_file = f"{outname}_nircam_ss.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) + + fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 5.5)) + + for ctable in stable: + gvals = ctable["NPTS"] > 0 + cfluxes = ( + ctable["FLUX"] + .to(fluxunit, equivalencies=u.spectral_density(ctable["WAVELENGTH"])) + .value + ) + ax.plot( + ctable["WAVELENGTH"][gvals], + cfluxes[gvals], + "k-", + alpha=0.5, + label="orig", + ) + gvals = rb_mrs["NPTS"] > 0 + cfluxes = ( + rb_mrs["FLUX"] + .to(fluxunit, equivalencies=u.spectral_density(ctable["WAVELENGTH"])) + .value + ) + + ax.plot( + 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$)") + + ax.set_xscale("log") + ax.set_yscale("log") + + ax.legend() + fig.tight_layout() + + fname = mrs_file.replace(".fits", "") + if args.png: + fig.savefig(f"{fname}.png") + elif args.pdf: + fig.savefig(f"{fname}.pdf") + else: + plt.show() \ No newline at end of file