-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_fluxes.py
235 lines (203 loc) · 6.94 KB
/
model_fluxes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import glob
import argparse
import numpy as np
import matplotlib.pyplot as plt
import astropy.units as u
from astropy.table import QTable, vstack, hstack
import warnings
from astropy.units import UnitsWarning
from jwstabsfluxcal.Webb.read_webb import read_miri
from jwstabsfluxcal.Spitzer.read_spitzer import read_irac
model_names = {
"g191b2b": "G 191-B2B",
"gd71": "GD 71",
"gd153": "GD 153",
"lds749b": "LDS 749B",
"lawd87": "LAWD 87", # same as LDS 749B
"wd1057_719": "WD 1057+719",
"pg1057_719": "PG 1057+719", # same as WD 1057+719
"1743045": "2MASS J17430448+6655015",
"1757132": "2MASS J17571324+6703409",
"1802271": "2MASS J18022716+6043356",
"bd60d1753": "BD+60 1753",
"hd163466": "HD 163466",
"hd167060": "HD 167060",
"hd180609": "HD 180609",
"hd106252": "HD 106252",
"hd128998": "HD 128998", # same as HR 5467
"hd142331": "HD 142331",
"hd159222": "HD 159222",
"hd205905": "HD 205905",
"hd101452": "HD 101452",
"hd37962": "HD 37962",
"hd55677": "HD 55677",
"hd2811": "HD 2811",
"hr5467": "HR 5467",
"p177d": "GSPC P177-D",
"p330e": "GSPC P330-E",
"16cygb": "16 Cyg B",
"delumi": "del UMi",
"lamlep": "lam Lep",
"mucol": "mu Col",
"10lac": "10 Lac",
"18sco": "18 Sco",
"snap2": "SNAP-2",
"ngc2506g31": "NGC2506 G31",
"c26202": "C26202",
}
def compute_bandflux(wave, flux_source, bwave, bandpass):
"""
Compute the band flux given the bandpass, reference spectrum,
and source spectrum. Assumes a flat reference spectrum
(for motivation see Gordon et al. 2022).
Parameters
----------
wave : nd float array
the wavelengths of flux_source
flux_source : nd float array
source flux density F(lambda) as a function of wave
bwave : nd float array
the wavelengths of bandpass
bandpass : nd float array
end-to-end, total throughput bandpass of filter in fractional units
"""
flux_source_bp = np.interp(bwave, wave, flux_source)
# compute the the integrals
inttop = np.trapz(bwave * bandpass * flux_source_bp, bwave)
intbot = np.trapz(bwave * bandpass, bwave)
return inttop / intbot
def get_band_fluxes(cfile, bandpasses, imgfile=None, grieke=False):
"""
Calculated the band fluxes for each possible filter
"""
# replace with G. Rieke's models when available
cname = cfile.split("/")[1].split("_")[0]
grieke_stars = [
"p330e",
"p177d",
"16cygb",
"18sco",
"hd106252",
"hd142331",
"hd159222",
"hd167060",
"hd205905",
"hd37962",
"ngc2506_g31",
]
if (cname in grieke_stars) & grieke:
gcfile = f"Models/grieke23_{cname}.dat"
print(f"using grieke model for {cname}")
modspec = QTable.read(gcfile, format="ascii")
mwave = modspec["wave_um"].value * u.micron
mflux = modspec["flux_W_cm-2_um-1"].value * u.W / (u.cm * u.cm * u.micron)
mflux_Jy = mflux.to(u.Jy, equivalencies=u.spectral_density(mwave))
if cname == "p177d":
mflux *= 1e-3
# save the file with Jy units
otab = QTable()
otab["wave"] = mwave
otab["flux"] = mflux_Jy
otab.write(gcfile.replace(".dat", ".fits"), overwrite=True)
otab.write(
gcfile.replace(".dat", "_ipac.dat"), format="ascii.ipac", overwrite=True
)
else:
# surpress the annoying units warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UnitsWarning)
modspec = QTable.read(cfile)
mwave = (modspec["WAVELENGTH"].value * u.angstrom).to(u.micron)
mflux = modspec["FLUX"].value * u.erg / (u.cm * u.cm * u.s * u.angstrom)
mflux_Jy = mflux.to(u.Jy, equivalencies=u.spectral_density(mwave))
# get the bandpasses
bfluxes = QTable()
rwaves = {}
for cband in bandpasses.keys():
rwave, cwave, ceff = bandpasses[cband]
rwaves[cband.upper()] = rwave
bflux_lambda = compute_bandflux(mwave, mflux, cwave, ceff)
bflux_nu = bflux_lambda.to(
u.Jy, equivalencies=u.spectral_density(rwave * u.micron)
)
bfluxes[cband.upper()] = [bflux_nu]
if imgfile is not None:
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, 6))
gvals = (mwave > 0.01 * u.micron) & (mwave < 32.0 * u.micron)
ax.plot(mwave[gvals], mflux_Jy[gvals] * (mwave[gvals] ** 2), "k-", alpha=0.5)
for cband in bfluxes.keys():
ax.plot(
rwaves[cband], bfluxes[cband] * (rwaves[cband] ** 2), "go", alpha=0.5
)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"wavelength [$\mu$m]")
ax.set_ylabel(r"Flux [Jy $\mu$m$^2$]")
# plt.show()
fig.suptitle(f"{cfile}")
plt.tight_layout()
plt.savefig(imgfile)
plt.close()
return bfluxes
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--irac", help="Do irac instead of miri", action="store_true")
parser.add_argument(
"--grieke", help="Use grieke models for some G stars", action="store_true"
)
args = parser.parse_args()
# models to use
# modfiles = glob.glob("Models/*_mod_*.fits")
modfiles = glob.glob("Models/*_stis*.fits")
# bandpasses to use
if args.irac:
bandpasses = read_irac()
else:
bandpasses = read_miri()
# save the band response functions
for cband in bandpasses.keys():
rwave, cwave, ceff = bandpasses[cband]
otab = QTable()
otab["wave"] = cwave
otab["bandpass"] = ceff
otab.write(
f"Models/bandpass_{cband}.dat",
format="ascii.commented_header",
overwrite=True,
)
mmods = []
for cfile in modfiles:
ntab = QTable()
ntab["name"] = [model_names[(cfile.split("/")[1]).split("_stis")[0]]]
print(f"working on {cfile}")
onemod = get_band_fluxes(
cfile,
bandpasses,
cfile.replace(".fits", "_absfluxbands.png"),
grieke=args.grieke,
)
onemod = hstack([ntab, onemod])
onemod["modfile"] = cfile
if mmods is None:
mmods = onemod
else:
mmods = vstack([mmods, onemod])
# remove "col0" column added by vstack
mmods.remove_column("col0")
# save table
if args.irac:
extstr = "_irac"
else:
extstr = ""
if args.grieke:
extstr = f"{extstr}_grieke"
mmods.write(f"Models/model_phot{extstr}.fits", overwrite=True)
mmods.write(f"Models/model_phot{extstr}.dat", format="ascii.ipac", overwrite=True)
print(mmods)