Skip to content

Commit

Permalink
adding in NIRCam
Browse files Browse the repository at this point in the history
  • Loading branch information
karllark committed Feb 22, 2024
1 parent 9a9e6f5 commit 42cda18
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 6 deletions.
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 @@ -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)
Expand Down
36 changes: 35 additions & 1 deletion measure_extinction/stardata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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 @@ -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
Expand Down Expand Up @@ -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(

Check warning on line 1137 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L1134-L1137

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

Check warning on line 1142 in measure_extinction/stardata.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/stardata.py#L1142

Added line #L1142 was not covered by tests
elif line.find("MIRI_IFU") == 0:
fname = _getspecfilename(line, self.path)
if os.path.isfile(fname):
Expand Down
7 changes: 3 additions & 4 deletions measure_extinction/utils/merge_miri_ifu_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@
args = parser.parse_args()

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
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")
Expand Down Expand Up @@ -104,6 +100,9 @@
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

Expand Down
115 changes: 115 additions & 0 deletions measure_extinction/utils/merge_nircam_spec.py
Original file line number Diff line number Diff line change
@@ -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(

Check warning on line 23 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L21-L23

Added lines #L21 - L23 were not covered by tests
"--inpath",
help="path where original data files are stored",
default=pkg_resources.resource_filename("measure_extinction", "data/Orig"),
)
parser.add_argument(

Check warning on line 28 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L28

Added line #L28 was not covered by tests
"--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()

Check warning on line 37 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L33-L37

Added lines #L33 - L37 were not covered by tests

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)

Check warning on line 50 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L39-L50

Added lines #L39 - L50 were not covered by tests

rb_mrs = merge_nircam_ss_obsspec(stable)
if args.outname:
outname = args.outname

Check warning on line 54 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L52-L54

Added lines #L52 - L54 were not covered by tests
else:
outname = args.starname.lower()
mrs_file = f"{outname}_nircam_ss.fits"
rb_mrs.write(f"{args.outpath}/{mrs_file}", overwrite=True)

Check warning on line 58 in measure_extinction/utils/merge_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L56-L58

Added lines #L56 - L58 were not covered by tests

# 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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_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_nircam_spec.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/merge_nircam_spec.py#L115

Added line #L115 was not covered by tests

0 comments on commit 42cda18

Please sign in to comment.