-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwst_sens_v2p1.py
543 lines (465 loc) · 30.8 KB
/
jwst_sens_v2p1.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
'''Making Jane's sensitivity plots comparing JWST to previous observatories. Now,
with post-commissioning (for Cycle 2) sensitivities from pandeia 2.0, courtesy Klaus.
jrigby, Dec 2022'''
from __future__ import print_function
from builtins import range
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import ScalarFormatter, FuncFormatter
from matplotlib.backends.backend_pdf import PdfPages
from os.path import expanduser, exists, basename
from os import remove
import glob
import re
import pandas
import jrr
import pandas
from astropy.io import ascii, fits
from astropy.table import Table
from pathlib import Path
def load_KeckMOSFIRE_sensitivity():
# Read in Keck MOSFIRE sensitivity from Wirth et al. 2015, From Figure 14
# of https://iopscience.iop.org/article/10.1088/0004-6256/150/5/153
keckmosfire_file = '../Wirth_etal_2015_ApJ_Fig14.txt'
columns = ('filtname', 'wave', 'redshiftHa', 'flux_limit')
# wave is observed wavelength in microns. Flux limit is in units of 1e-17 erg/s/cm^2
df = pandas.read_csv(keckmosfire_file, delim_whitespace=True, comment='#', skiprows=24, encoding='latin-1', names=columns)
# Next line converts SNR=3 to SNR=10, convert from 1 hr to 1E4 s, and units to cgs (erg/s/cm^2)
df['scaled_flux_lim'] = df['flux_limit'] * (10./3.) * np.sqrt(3600. / 1E4) *1E-17
keck_gb = df.groupby('filtname') # return separate dataframes for each filter of MOSFIRE
(H, J, K, Y) = [keck_gb.get_group(x) for x in keck_gb.groups]
df2 = {}
df2['H'] = H ; df2['J'] = J; df2['K'] = K; df2['Y'] = Y
return(df, df2) # return dataframe of all data, as well as dict of dataframes, one for each bandpass
def load_comparison_photometry() :
# Adding the comparison to other observatories. Switching from gnuplot to python
phot_codes = {'NIRSpec' : 0, 'NIRCam': 1, 'MIRI' : 2, 'HST' : 4, 'WISE' : 5, 'Spitzer' : 6, 'Gemini' :10 , 'Herschel' : 11, 'SOFIA' : 12}
#13=alma(cycle0), 14=alma(finished), 15=VLA, 16=EVLA
names = ('name', 'wave', 'limfnu', 'code')
phot_df = pandas.read_csv(pandir + '../jwst-phot_v2.dat', comment="#", delim_whitespace=True, usecols=[0,1,2,3], names=names)
phot_df['wave'] = pandas.to_numeric(phot_df['wave'])
return(phot_df, phot_codes)
def load_comparison_spectroscopy() : # input units are W/m^2
# Adding the comparison to other observatories. Switching from gnuplot to python
spec_codes = { 'nirspec' : 0, 'NIRCAM' : 1, 'MIRI' : 2, 'HST' : 4, 'WISE' : 5, 'Spitzer' : 6, 'Keck' : 9, 'Gemini' : 10, 'VLT' : 11, 'SOFIA' : 12, 'TMT' : 13, 'ELT' : 14}
names = ('name', 'wave', 'limflux_wm2', 'R', 'code', 'comment')
spec_df = pandas.read_csv(pandir + '../jwst-spec_v2.dat', comment='#', delim_whitespace=True, names=names)
spec_df['limflux_cgs'] = spec_df['limflux_wm2'] * 1E3
return(spec_df, spec_codes)
def load_comparison_lowres_spec() : # input are Jy per spectral resoln element
names = ('telescope', 'instrument', 'wave', 'limflux_Jy', 'foo', 'R', 'code', 'comment')
df = pandas.read_csv(pandir + '../jwst-lowres-spec_v2.dat', comment='#', delim_whitespace=True, names=names)
df['limflux_mJy'] = df.limflux_Jy *1000
jrr.spec.pssc_to_pssl_df(df, PSSLcol='PSSL', PSSCcol='limflux_mJy', wavecol='wave', Rcol='R') # should do same as below
df['PSSL2'] = (df.limflux_mJy) * 3.0E-15 / (df.R * df.wave) * 1E3 #The last 1E3 converts to cgs
return(df)
def add_annotations():
plt.annotate("NIRISS", xy=(1.4,0.8E-17), color='red', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate("MIRI MRS", xy=(14,3.E-17), color='k', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate("MIRI LRS", xy=(6, 0.6E-16), color='grey', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate("NIRSpec MOS", xy=(0.6,0.6E-18), color='orange', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate("& IFU", xy=(1.7, 0.6E-18), color='green', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate("NIRCam grism", xy=(2.5, 1.2E-17), color='blue', xycoords='data', fontsize=fs2, weight='bold')
plt.annotate('SOFIA FLITECAM', xy=(1, 1.3E-15), color='dodgerblue', xycoords='data', fontsize=fs1)
plt.annotate('VLT ISAAC', xy=(1, 4E-16), color='green', xycoords='data', fontsize=fs1)
plt.annotate('Gemini NIRI', xy=(0.8, 1E-16), color='peru', xycoords='data', fontsize=fs1)
plt.annotate('Keck MOSFIRE', xy=(0.72, 5E-17), color='darkviolet', xycoords='data', fontsize=fs1)
plt.annotate('Spitzer IRS H', xy=(5.2, 1E-15), color='purple', xycoords='data', fontsize=fs1)
plt.annotate('Spitzer IRS L', xy=(5.2, 2.5E-16), color='hotpink', xycoords='data', fontsize=fs1)
def add_annotations_lowres():
plt.annotate("NIRISS", xy=(1,2E-7), color='red', xycoords='data', fontsize=fs2)
plt.annotate("MIRI MRS", xy=(10,5E-3), color='k', xycoords='data', fontsize=fs2)
plt.annotate("MIRI LRS", xy=(10,2E-6), color='grey', xycoords='data', fontsize=fs2)
plt.annotate("NIRSpec MOS", xy=(0.6,3E-8), color='orange', xycoords='data', fontsize=fs2)
plt.annotate("& IFU", xy=(1.5, 3E-8), color='green', xycoords='data', fontsize=fs2)
plt.annotate("NIRCam grism", xy=(2.5, 2E-5), color='blue', xycoords='data', fontsize=fs2)
def pretty_plot() :
plt.xlabel("wavelength (micron)", fontsize=fs3)
plt.ylabel('limiting flux density (Jy)', fontsize=fs3)
#plt.legend()
plt.yscale('log')
plt.xscale('log')
plt.annotate('[email protected], pandeia v2.0', xy=(0.64,0.16), xycoords='figure fraction')
jrr.plot.force_axisticks_linear((ax.xaxis,))
plt.xticks(fontsize=fs3)
plt.yticks(fontsize=fs3)
plt.tight_layout()
def load_nirspec_postlaunch() : # Load nirspec's post-commissioning curves, from their PASP paper
# files from Box, via Nimisha Kumari of STScI
ndir = '/Users/jrrigby1/MISSIONS/JWST/Sens/Postlaunch/Nirspec_post-commissioning_Sensitivity_Curves_IFS_MOS/'
IFS_files = [basename(x) for x in glob.glob(ndir + 'IFS-*Jy.src')]
MOS_files = [basename(x) for x in glob.glob(ndir + 'MOS-*Jy.src')]
#print(IFS_files, MOS_files)
df_ifs = {} ; df_mos = {}
for thisfile in IFS_files : df_ifs[thisfile] = pandas.read_json(ndir + thisfile)
for thisfile in MOS_files : df_mos[thisfile] = pandas.read_json(ndir + thisfile)
return(df_ifs, df_mos)
def load_nircam_grism_postlaunch() : # Load nircam grism post-commissioning curves, from their PASP paper.
ndir = '/Users/jrrigby1/MISSIONS/JWST/Sens/NIRCam_WFSS_post-commissioning_sens/'
grism_files = [basename(x) for x in glob.glob(ndir + 'F*.dat')]
#columns = ('wavelength', 'cont_flux_uJy', 'cont_flux_uJy_old', 'line_flux_Em18', 'line_flux_Em18_old')
df_nircam = {}
for thisfile in grism_files: df_nircam[thisfile] = pandas.read_csv(ndir + thisfile, delim_whitespace=True, comment='#', skiprows=1)
return(df_nircam)
def grab_R_for_nirspec(grating) : #grating as in 'prism', 'g140m', 'g235h'
ndir = '/Users/jrrigby1/MISSIONS/JWST/Sens/Postlaunch/NIRSpec_R/'
Rfilename = 'jwst_nirspec_' + grating.lower() + '_disp.fits'
RR, Rheader = fits.getdata(ndir + Rfilename, header=True)
return(RR, Rheader)
def grab_niriss_wfss():
ndir = '/Users/jrrigby1/MISSIONS/JWST/Sens/Postlaunch/'
filenames = ['niriss-wfss-line-sens.txt', 'niriss-wfss-cont-sens.txt']
df_line = pandas.read_csv(ndir + filenames[0], delim_whitespace=True, comment='#')
df_line['sens_cgs'] = df_line['Sensitivity'] * 1E-20 * 1E3 # sensitivity is in units of (10^-20 W/m^2)
df_cont = pandas.read_csv(ndir + filenames[1], delim_whitespace=True, comment='#')
# need to test df_cont
return(df_line, df_cont)
def spec_compare_to_other_obs(df_lowres_spec): # compare to other observatories
otherspec_df, spec_codes = load_comparison_spectroscopy()
shortcodes = ('SOFIA', 'Spitzer', 'Gemini', 'VLT')
colors = {'SOFIA': 'dodgerblue', 'Gemini' : 'peru', 'VLT': 'green', 'Spitzer': 'darkmagenta'}
for instr in shortcodes : # spec_codes :
subset = otherspec_df.loc[otherspec_df['code'] == spec_codes[instr]]
if instr == 'Spitzer' : label='Spitzer R=600'
elif instr == 'Keck' : label='Keck MOSFIRE'
else : label=instr
plt.plot(subset['wave'], subset['limflux_cgs'], label=label, marker='o', linestyle='dashed', color=colors[instr])
subset = df_lowres_spec.loc[df_lowres_spec.telescope == 'Spitzer'] # Add low-resoln spitzer
plt.plot(subset.wave, subset.PSSL, color='hotpink', label='Spitzer R=60-100', marker='o', linestyle='dashed')
# Deal w Keck separately, to split by grating (don't connect Y J H K, it's misleading)
plot_Gwen_ETC = False # Gwen Rudie's ETC gives results that seem v optimistic. Will plot measured sens instead.
if plot_Gwen_ETC :
subset = otherspec_df.loc[otherspec_df['code'] == spec_codes['Keck']]
keck_gb = subset.groupby('R')
(J, Y, K, H) = [keck_gb.get_group(x) for x in keck_gb.groups]
plt.plot(Y.wave, Y.limflux_cgs, label='Keck', marker='o', linestyle='dashed', color='darkviolet') #only label Y
for df in (J, H, K):
plt.plot(df.wave, df.limflux_cgs, label='_nolegend_', marker='o', linestyle='dashed', color='darkviolet')
#plt.legend(loc='upper left')
return(0)
##############################################################################################################################
debug=True
pandeia_ver = "v2.0"
###############################################################
pandir = '/Users/jrrigby1/MISSIONS/JWST/Sens/Pandeia_v2.0-sensitivity/' # post-commissioning sensitivities.
# Downloaded from https://github.com/spacetelescope/pandeia-verification/tree/master/2.0
# They are pickles, read by JR's custom jrr.instruments.load_Pandeia_sensitivities(pandir)
###############################################################
dash = (0, (5, 10)) # a loosely dashed line
fs1 = 13 ; fs2 = 16 ; fs3 = 22 # fontsizes
# Which curves from the IDTs to plot?
#phot_compare_IDT = {'nircam': False, 'niriss': False}
#spec_compare_IDT = {'nircam': False, 'niriss': False, 'nirspec': False}
phot_compare_IDT = {'nircam': True, 'niriss': True}
spec_compare_IDT = {'nircam': True, 'niriss': True, 'nirspec': True}
df_sens = jrr.instruments.load_Pandeia_sensitivities(pandir) # From pandeia, continuum sensitivity, in mJy, PER PIXEL (not per resoln element)
df_disp = jrr.instruments.load_Pandeia_dispersions(pandir=None) # From pandeia, spectral resolutions. Not on same pixel scale, must interpolate
print("I think I imported all the Pandeia files:", list(df_sens.keys()))
plt.ion()
#pp = PdfPages("jwst_sensitivity_jrigby_v2p1.pdf") # output
pp = PdfPages("jwst_sensitivity_jrigby_v2p1_compare2IDTs.pdf") # output
plt.close("all")
########################################################################################################################
# PHOTOMETRY PLOT
lw = 1.5
outfile = 'pandeia_imaging_sensitivities_' + pandeia_ver + '.readme'
outfile2 = re.sub('readme', 'csv', outfile)
header_text = '# lim_fluxes is the continuum flux density of the point source that can be detected at SNR=10 in 1E4s,\n'
header_text += '# from Pandeia ' + pandeia_ver + ', via the Pandeia verification files, as \n'
header_text += '# downloaded from https://github.com/spacetelescope/pandeia-verification/tree/master/2.0\n'
header_text += '# Unit: wavelengths column is in microns, and lim_fluxes column is limiting flux DENSITY in milliJanskies\n'
header_text += '# read from pandeia verification pickle into CSV, [email protected], Dec 2022.\n'
header_text += '# Read this file into python as: df_imaging_sens = pandas.read_csv(\''
header_text += outfile2 + '\', comment=\"#\")\n#\n'
jrr.util.put_header_on_file('/dev/null', header_text, outfile)
if exists(outfile2): remove(outfile2) # remove outfile if it exists, for the imaging sensitivity
cols_to_list = ['mode', 'filter', 'wavelengths', 'lim_fluxes']
figsize = (8.5,6)
fig, ax = plt.subplots(figsize=figsize)
modes = ['nircam_sw', 'nircam_lw', 'miri_imaging', 'niriss_imaging']
labels = ['JWST NIRCam', '_nolegend_', 'JWST MIRI', 'JWST NIRISS']
colors = ['blue', 'blue', 'k', 'red']
# Plot imaging
for ii, mode in enumerate(modes) :
thismode = mode + '_sensitivity'
df_sens[thismode]['mode'] = mode # label it
subset = df_sens[thismode].loc[df_sens[thismode]['configs'].astype(str).str.contains('w') & \
~df_sens[thismode]['configs'].astype(str).str.contains('w2')].sort_values(by='wavelengths') # Skip W2
# above is only W filters, but not W2
print(subset.head())
plt.plot(subset['wavelengths'], subset['lim_fluxes']*1E-3, label=labels[ii], color=colors[ii], marker='o', linestyle='-', markersize=10, alpha=1.0, lw=lw)
df_sens[thismode][cols_to_list].to_csv(outfile2, mode='a', index=False, header=bool(not ii)) # save to file
jrr.util.put_header_on_file(outfile2, header_text, outfile2)
# Harvesting Jane's sensitivities for previous missions
(phot_df, phot_codes) = load_comparison_photometry()
df_lowres_spec = load_comparison_lowres_spec() # load the prelaunch lowres spec file
#instrs = ['NIRCam', 'MIRI', 'HST']
instrs = ['HST', 'Spitzer', 'Gemini']
for instr in instrs:
subset = phot_df.loc[phot_df['code'] == phot_codes[instr]]
print(subset.head())
plt.plot(subset['wave'], subset['limfnu'], label=instr, marker='o', linestyle='dashed')
plt.xlim(0.5, 28)
plt.ylim(3E-9, 1.1E-4)
plt.title("Imaging sensitivity, pt src, SNR=10 in " + r'$10^4$' + "s", fontsize=fs2)
plt.text(1.1, 2.5E-6, "Gemini", color='green', fontsize=fs3)
plt.text(0.8, 8E-8, "Hubble", color='#1f77b4', fontsize=fs3)
plt.text(4.7, 0.5E-7, "JWST MIRI", color='black', fontsize=fs3)
plt.text(4.7, 1.8E-8, "JWST NIRCam", color='blue', fontsize=fs3)
plt.text(4.7, 7E-9, "JWST NIRISS", color='red', fontsize=fs3)
plt.text(8.8, 4.5E-5, "Spitzer", color='orange', fontsize=fs3)
pretty_plot()
# Compare to post-commissioning sensitivity estimates from NIRCam and NIRISS IDTs
df_postlaunch_phot = pandas.read_csv("/Users/jrrigby1/MISSIONS/JWST/Sens/Postlaunch/jwst_postlaunch.dat", comment='#', delim_whitespace=True)
df_postlaunch_nircam = df_postlaunch_phot.loc[df_postlaunch_phot.instrument.str.contains('nircam')].sort_values(by='wavelength')
df_postlaunch_niriss = df_postlaunch_phot.loc[df_postlaunch_phot.instrument.str.contains('niriss')].sort_values(by='wavelength')
subset_M = df_postlaunch_nircam.loc[df_postlaunch_nircam.filtname.str.contains('M')] # just nircam M
subset_W = df_postlaunch_nircam.loc[df_postlaunch_phot.filtname.str.contains('W')] # just nircam W
if phot_compare_IDT['nircam'] :
plt.plot(subset_W.wavelength, subset_W.limiting_sensitivity_Jy, marker='*', color='blue', alpha=0.5, linestyle='dashed')
#plt.plot(subset_M.wavelength, subset_M.limiting_sensitivity_Jy, marker='*', color='aqua') # medium band NIRCam
if phot_compare_IDT['niriss'] :
plt.plot(df_postlaunch_niriss.wavelength, df_postlaunch_niriss.limiting_sensitivity_Jy, marker='*', color='red', linestyle='dashed', alpha=0.5)
pp.savefig()
plt.show()
if debug: print("DEBUG, Npixels per spectrally unresolved line.")
########################################################################################################################
# SPECTROSCOPY PLOT IN LINE FLUX
# Originally I did this wrong, using the Spitzer PSSC--> PSSL formula. After iteration w Klaus, he helped me figure out
# that that's not correct, bc PSSC is continuum per resoln element, while continuum sensitivities from Pandeia are per pixel.
# My PSSLs were wring. Remove them. It matters most for MIRI b/c the Npixel sampling of a spectral line changes a lot.
# For NIRSpec it's a well-behaved 2 to 2.2.
# Plot limiting sensitivity to an unresolved line (in erg/s/cm^2). What a spectroscopist wants.
fig2, ax2 = plt.subplots(figsize=figsize)
cuton = { 'f277w': 2.46, 'f322w2': 2.46, 'f356w': 3.15, 'f444w': 3.94} # clean up plotting of NIRCam grism mode
cutoff = { 'f277w': 3.1, 'f322w2': 3.95, 'f356w': 3.95, 'f444w': 4.93} # clean up plotting of NIRCam grism mode
df_specsens = {} # make a smaller dict of sensitivities for the spectroscopic modes only
#specmodes = ['nirspec_fs','nirspec_ifu', 'nirspec_msa', 'miri_mrs']# 'nircam_wfgrism', 'miri_lrs', 'niriss_wfss','niriss_soss'
specmodes = ['nirspec_ifu', 'nirspec_msa', 'miri_mrs', 'nircam_wfgrism', 'niriss_wfss']
just_plot_grisms = ('f444w', 'f322w2')
# MIRI LRS is pickled weird in pandeia verification. Treat it separately
df_lrs_slitless, df_lrs_slit = jrr.instruments.import_Pandeia_miri_lrs_special()
disp_key = 'jwst_miri_p750l'
for df in df_lrs_slitless, df_lrs_slit :
df['R'] = jrr.spec.rebin_spec_new(df_disp[disp_key]['WAVELENGTH'], df_disp[disp_key]['R'], df.wavelengths.astype('float'))
#df['PSSL'] = df.lim_fluxes * 3.0E-15 / ( df.R * df.wavelengths) * 1E3
# next lines are from Pandeia verification_tools/calc_limits.py
px_width_micron = np.abs(df.wavelengths-np.roll(df.wavelengths,1))
px_width_micron[:1] = px_width_micron[1]
freqs = 2.99792458e14/df.wavelengths
px_width_hz = np.abs(freqs-np.roll(freqs,1))
px_width_hz[:1] = px_width_hz[1]
line_width_px = df.wavelengths / df.R / px_width_micron
if debug : print("DEBUG, MIRI LRS", np.round(np.median(line_width_px), 3))
df['scale'] = 1e-3*1e-26 * px_width_hz * line_width_px / np.sqrt(line_width_px) # thing to scale lim_flux by. in W/m^2
df['jrr_linelim'] = df.lim_fluxes * df.scale * 1E3 # last 1E3 converts to cgs
# Loop through the spectroscopic modes, computing limiting line flux from limiting continuum flux
for specmode in specmodes:
thismode = specmode + '_sensitivity'
df_specsens[thismode] = df_sens[thismode].copy(deep=True)
df_specsens[thismode]['mode'] = specmode
df_specsens[thismode]['R'] = df_specsens[thismode]['wavelengths'] * 0 # initialize
df_specsens[thismode]['scale'] = df_specsens[thismode]['wavelengths'] * 0 # initialize
df_specsens[thismode]['jrr_linelim'] = df_specsens[thismode]['wavelengths'] * 0 # initialize
for index, row in df_specsens[thismode].iterrows() : # Go row by row, for each grating, order, etc
if 'miri' in thismode :
if 'lrs' in specmode: raise Exception("MIRI LRS is pickled weirdly. Don't add to specmodes, treat separately.")
else: disp_key = 'jwst_miri_' + row.aperture + '-' + row.disperser
elif 'nircam' in thismode: disp_key = 'jwst_nircam'
elif 'nirspec' in thismode: disp_key = 'jwst_' + re.split('_', thismode)[0] + '_' + row.disperser
elif 'niriss' in thismode:
disp_key = 'jwst_niriss_' + row.configs['disperser'] + '-ord1' # should have an order attached. Use first order
df_disp[disp_key].rename(columns={'Wavelength': 'WAVELENGTH'}, inplace=True) # regularize this
rebinnedR = jrr.spec.rebin_spec_new(df_disp[disp_key]['WAVELENGTH'], df_disp[disp_key]['R'], row.wavelengths)
df_specsens[thismode].loc[index, 'R'] = rebinnedR
# next lines are from Pandeia verification_tools/calc_limits.py
px_width_micron = np.abs(row.wavelengths-np.roll(row.wavelengths,1))
px_width_micron[:1] = px_width_micron[1]
freqs = 2.99792458e14/row.wavelengths
px_width_hz = np.abs(freqs-np.roll(freqs,1))
px_width_hz[:1] = px_width_hz[1]
line_width_px = row.wavelengths / rebinnedR / px_width_micron
if debug: print("DEBUG!", specmode, row.configs, np.round(np.median(line_width_px), 3))
scale = 1e-3*1e-26 * px_width_hz * line_width_px / np.sqrt(line_width_px) # thing to scale lim_flux by. in W/m^2
df_specsens[thismode].loc[index, 'scale'] = scale
df_specsens[thismode].loc[index, 'jrr_linelim'] = row.lim_fluxes * scale * 1E3 # last 1E3 converts to cgs
# Now plot it. I've separated computing limits (above) from plotting them (here)
for specmode in specmodes:
thismode = specmode + '_sensitivity'
for index, row in df_specsens[thismode].iterrows() : # Go row by row, for each grating, order, etc
clip = 2. # clip this much off edges, in percent
length = len(row.wavelengths)
#At Klaus's suggestion, clip the first few and last few percent
ind1 = list(np.arange( int(np.floor(length * clip/100.)), int(np.ceil(length* (1 - clip/100)))))
thresh = { 'miri_mrs': 1E6, 'nircam_wfgrism': 2.4, 'nirspec_ifu': 10, 'nirspec_msa': 10, 'niriss_wfss': 1.9 } #threshold for each mode
indices = list((row.jrr_linelim < (thresh[specmode] * np.nanmedian(row.jrr_linelim))).nonzero()[0])
both_ind = list(set(indices).intersection(set(ind1))) # python magic. indices that are in both ind1 and indices
if 'miri' in thismode :
plt.plot(row.wavelengths[both_ind], row.jrr_linelim[both_ind], color='k', linestyle='solid', lw=lw)
#plt.plot(row.wavelengths, row.line_limits*1E3, color='pink', alpha=1) # These agree w my calcs now
elif specmode == 'nircam_wfgrism' :
if row.configs['filter'] in just_plot_grisms: # only plot predicted grism sens for wide filters!
plt.plot(row.wavelengths[both_ind], row.jrr_linelim[both_ind], color='blue', alpha=1, linestyle='solid', lw=lw)
#plt.annotate(row.configs['filter'], (np.median(row.wavelengths), row.PSSL.min()), fontsize=10, color='blue')
elif 'nirspec' in thismode:
if 'ifu' in thismode:
plt.plot(row.wavelengths[both_ind], row.jrr_linelim[both_ind], color='green', alpha=1, linestyle='solid', lw=lw)
elif 'msa' in thismode:
plt.plot(row.wavelengths[both_ind], row.jrr_linelim[both_ind], color='orange', alpha=1, linestyle='solid', lw=lw)
elif 'niriss' in thismode:
plt.plot(row.wavelengths[both_ind], row.jrr_linelim[both_ind], color='red', alpha=1, linestyle='solid', lw=lw)
else:
plt.plot(row.wavelengths, row.jrr_linelim, linestyle='solid', lw=lw)
for df in df_lrs_slitless, df_lrs_slit :
plt.plot(df.wavelengths, df.jrr_linelim, color='grey', linestyle='solid', lw=lw)
# Add Keck MOSFIRE, first from Jung et al. 2020, ApJ 94, 144, Y-band only
#Intae_flux_scaled = 6.3 * 3E-18 #cgs units, erg/s/cm^2, scaled from 10hr to 1E4s, and SNR=3 to SNR=10
#plt.plot((0.98, 1.12), (Intae_flux_scaled, Intae_flux_scaled), color='darkviolet', lw=3, linestyle='dashed')
# More Keck MOSFIRE, from Wirth et al. 20915, ApJ, 150, 153
df_mosfire, df2_mosfire = load_KeckMOSFIRE_sensitivity()
#plt.plot(df_mosfire.wave, df_mosfire['scaled_flux_lim'], color='pink', lw=0.5)
for thisband in df2_mosfire.keys() : # plot each band separately
df2_mosfire[thisband]['scaled_flux_median'] = df2_mosfire[thisband]['scaled_flux_lim'].rolling(30).median()
#plt.plot(df2_mosfire[thisband].wave, df2_mosfire[thisband].scaled_flux_median, color='k', lw=0.5)
plt.plot(df2_mosfire[thisband].wave, df2_mosfire[thisband].scaled_flux_lim, color='darkviolet', lw=0.2, linestyle='dashed')
plt.ylabel(r"line flux det. at SNR=10 in $10^4$s (erg s$^-1$ cm$^2$)", fontsize=fs2)
plt.title("Spectroscopic line sensitivity, pt src, SNR=10 in " + r'$10^4$' + "s", fontsize=fs2)
bigspec_df = pandas.concat(df_specsens)
if spec_compare_IDT['nirspec']:
(df_ifs, df_mos) = load_nirspec_postlaunch() # post-commissioning nirspec sensitivities
threshold = 10 # Don't plot the edges of the filter curve, just where it has sensitivity
for df1 in (df_ifs, df_mos) :
for key in df1.keys():
df = df1[key]
# Parse key to get grating
grating = re.split('-|_', key)[2]
RR, Rheader = grab_R_for_nirspec(grating)
df['limflux_mJy'] = df.data *1E3
df['wave_um'] = df.wavelength *1E6
df['R'] = jrr.spec.rebin_spec_new(RR.WAVELENGTH, RR.R, df.wave_um) # Interpolate R onto the sensitivity curve
# next lines are from Pandeia verification_tools/calc_limits.py
px_width_micron = np.abs(df.wave_um-np.roll(df.wave_um,1))
px_width_micron[:1] = px_width_micron[1]
freqs = 2.99792458e14/df.wave_um
px_width_hz = np.abs(freqs-np.roll(freqs,1))
px_width_hz[:1] = px_width_hz[1]
line_width_px = df.wave_um / df.R / px_width_micron
if debug: print("DEBUG, NIRSpec IDT", np.round(np.median(line_width_px), 3))
df['scale'] = 1e-3*1e-26 * px_width_hz * line_width_px / np.sqrt(line_width_px) # thing to scale lim_flux by. in W/m^2
df['jrr_linelim'] = df.limflux_mJy * df.scale * 1E3 # last 1E3 converts to cgs
color = 'green' if ('IFS' in key) else 'orange'
plt.plot(df.wave_um, df.jrr_linelim, color=color, linestyle=dash, alpha=0.5)
if spec_compare_IDT['nircam']:
df_nircamgrism = load_nircam_grism_postlaunch() #post-commissioning nircam sensitivities
threshold = 14 # Don't plot the edges of the filter cuve, just where it has sensitivity
for key in df_nircamgrism.keys():
df = df_nircamgrism[key]
subset = df.loc[ (df['line_flux_E-18'] < threshold * df['line_flux_E-18'].min())]
plt.plot(subset.wavelength, subset['line_flux_E-18'] * 1E-18, color='lightblue', linestyle=dash) # data is in cgs, unit of 10^-18 erg/s/cm^2.
#plt.annotate(key, (np.median(subset.wavelength), subset['line_flux_E-18'].min() *1E-18), fontsize=10, color='blue', alpha=0.5)
# post-commissioning niriss wfss sensitivity
if spec_compare_IDT['niriss']:
df_niriss_wfss_line, df_niriss_wfss_cont = grab_niriss_wfss()
for thisfilter in df_niriss_wfss_line.Filter.unique():
subset = df_niriss_wfss_line.loc[df_niriss_wfss_line.Filter == thisfilter]
plt.plot(subset.Wavelength, subset.sens_cgs, color='red', linestyle=dash, alpha=1)
spec_compare_to_other_obs(df_lowres_spec)
pretty_plot()
plt.ylabel(r'limiting line flux (erg s$^{-1}$ cm$^{-2}$ )', fontsize=fs3)
plt.xlim(0.5,30)
plt.ylim(4E-19,2.5E-15)
ax2.xaxis.set_major_formatter(ScalarFormatter())
add_annotations()
plt.tight_layout()
pp.savefig()
plt.show()
###################################################################################################################
### Spectroscopy plot with continuum sensitivity
# What are the units here? From above, looks like mJy. Double check!
#
cols_to_list = ['mode', 'wavelengths', 'R', 'jrr_linelim']
fig, ax = plt.subplots(figsize=figsize)
for specmode in specmodes:
thismode = specmode + '_sensitivity'
for index, row in df_specsens[thismode].iterrows() : # Go row by row, for each grating, order, etc
if 'miri' in thismode :
plt.plot(row.wavelengths, row.lim_fluxes *1E-3, color='k')
elif specmode == 'nircam_wfgrism' :
threshold = 1.4
indices = np.where(row.lim_fluxes < threshold * row.lim_fluxes.min())
if row.configs['filter'] in just_plot_grisms: # only plot predicted grism sens for wide filters!
plt.plot(row.wavelengths[indices], row.lim_fluxes[indices] *1E-3, color='blue', alpha=0.5)
elif 'nirspec' in thismode:
if 'ifu' in thismode: plt.plot(row.wavelengths, row.lim_fluxes *1E-3, color='green')
elif 'msa' in thismode: plt.plot(row.wavelengths, row.lim_fluxes *1E-3, color='orange')
elif 'niriss' in thismode:
threshold = 2
indices = np.where(row.lim_fluxes < threshold * row.lim_fluxes.min())
plt.plot(row.wavelengths[indices], row.lim_fluxes[indices] *1E-3, color='red')
else:
plt.plot(row.wavelengths, row.lim_fluxes)
for df in df_lrs_slitless, df_lrs_slit : # Better way to plot miri LRS
plt.plot(df.wavelengths, df.lim_fluxes *1E-3, color='grey')
if spec_compare_IDT['nircam'] :
for key in df_nircamgrism.keys():
df = df_nircamgrism[key]
subset = df.loc[ (df['cont_flux_uJy'] < threshold * df['cont_flux_uJy'].min())]
plt.plot(subset.wavelength, subset['cont_flux_uJy'] * 1E-6, color='blue', linestyle='dashed', alpha=0.5) # convert from microJy to Jy
# post-commissioning nirspec sensitivities
if spec_compare_IDT['nirspec'] :
threshold = 10 # Don't plot the edges of the filter curve, just where it has sensitivity
for df1 in (df_ifs, df_mos) :
for key in df1.keys():
df = df1[key]
# Parse key to get grating
grating = re.split('-|_', key)[2]
color = 'green' if ('IFS' in key) else 'orange'
subset = df.loc[df.data < threshold * df.data.min()]
plt.plot(subset.wavelength *1E6, subset.data, color=color, linestyle='dashed', alpha=0.5)
# post-commissioning NIRISS sensitivity
if spec_compare_IDT['niriss']:
for thisfilter in df_niriss_wfss_cont.Filter.unique():
subset = df_niriss_wfss_cont.loc[df_niriss_wfss_cont.Filter == thisfilter]
plt.plot(subset.Wavelength, subset.Sensitivity * 1E-6, color='k', linestyle='dashed', alpha=0.5)
# plot comparisons:
subset = df_lowres_spec.loc[df_lowres_spec.telescope == 'HST']
plt.scatter(subset.wave, subset.limflux_Jy, color='red', label='HST')
#
subset = df_lowres_spec.loc[df_lowres_spec.telescope == 'Spitzer']
plt.scatter(subset.wave, subset.limflux_Jy, color='purple', label='Spitzer')
#
plt.title("Spectroscopic sensitivity, pt src, per pixel, SNR=10 in " + r'$10^4$' + "s", fontsize=fs2)
plt.legend()
add_annotations_lowres()
pretty_plot()
pp.savefig()
plt.show()
######### Temp plot
#fig, ax = plt.subplots(figsize=figsize)
#for specmode in specmodes:
# thismode = specmode + '_sensitivity'
# for index, row in df_specsens[thismode].iterrows() : # Go row by row, for each grating, order, etc
# if 'miri' in thismode :
# plt.plot(row.wavelengths, row.PSSL / (row.line_limits*1E3), color='k')
#plt.xlabel("wavelength (micron)")
#plt.ylabel("PSSL / (line_limits * 1E3)")
#
#for df in df_lrs_slitless, df_lrs_slit :
# plt.plot(df.wavelengths, df.PSSL / (df.line_limits*1E3), color='grey')
#
#pp.savefig()
#plt.show()
pp.close()
outfile = 'pandeia_lineflux_sensitivities_' + pandeia_ver + '.readme'
header_text = '# jrr_linelim, units erg/s/cm^2, is the line flux detectable for a spectrally unresolved line, \n'
header_text += '# point source, at SNR=10, in 1E4s, from Pandeia ' + pandeia_ver + '\n'
header_text += '# Converted from per-pixel continuum line flux density in mJy as f_linelim = f_cont^pix * 1E-3 * 1E-26 * pix_width_Hz * sqrt(line_width_pix)\n'
header_text += '# In Python pandas, read this file as: df = pandas.read_csv(\'' + outfile + '\', index_col=[0,1], comment=\'#\')\n'
header_text += '# [email protected], Dec 2022.\n#\n'
jrr.util.put_header_on_file('/dev/null', header_text, outfile)
bigspec_df.to_pickle(re.sub('readme', 'pcl', outfile))
# read this as df_sens = pandas.read_pickle('pandeia_lineflux_sensitivities_v1.5.0.pcl')
# TODO:
# DONE Add actual NIRCam and NIRISS sensitivities from Marcia's paper and the SciPerf paper
# DONE Import actual NIRSpec sensitivities from Torsten
# DONE, used Gwen Rudie's IDL ETC