Skip to content

Commit ce1b817

Browse files
committed
Cleanup loop from plotting, and rely on quantity support for units
1 parent 69fa874 commit ce1b817

File tree

1 file changed

+19
-53
lines changed

1 file changed

+19
-53
lines changed

tutorials/euclid_access/3_Euclid_intro_1D_spectra.md

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import urllib
5555
5656
import numpy as np
5757
import matplotlib.pyplot as plt
58-
from matplotlib.lines import Line2D
5958
6059
from astropy.io import fits
6160
from astropy.table import QTable
@@ -123,6 +122,10 @@ with fits.open(file_uri) as hdul:
123122
spectra
124123
```
125124

125+
```{code-cell} ipython3
126+
spec_header
127+
```
128+
126129
## 4. Plot the image of the extracted spectrum
127130

128131
```{tip}
@@ -133,7 +136,6 @@ As we use astropy.visualization's ``quantity_support``, matplotlib automatically
133136
quantity_support()
134137
```
135138

136-
137139
```{note}
138140
The 1D combined spectra table contains 6 columns, below are a few highlights:
139141
@@ -142,76 +144,40 @@ The 1D combined spectra table contains 6 columns, below are a few highlights:
142144
- MASK values can be used to determine which flux bins to discard. MASK = odd and MASK >=64 means the flux bins not be used.
143145
```
144146

145-
+++
147+
```{code-cell} ipython3
148+
signal_scaled = spectra['SIGNAL'] * spec_header['FSCALE']
149+
```
146150

147151
We investigate the MASK column to see which flux bins are recommended to keep vs "Do Not Use"
148152

149153
```{code-cell} ipython3
150-
plt.plot(spectra['WAVELENGTH']/10000., spectra['MASK'])
151-
plt.xlabel('Wavelength ($\mu$m)')
154+
plt.plot(spectra['WAVELENGTH'].to(u.micron), spectra['MASK'])
152155
plt.ylabel('Mask value')
153156
plt.title('Values of MASK by flux bin')
154157
```
155158

156-
```{code-cell} ipython3
157-
spec_header
158-
```
159-
160-
## 4. Plot the image of the extracted spectrum
161-
162-
```{tip}
163-
As we use astropy.visualization's ``quantity_support``, matplotlib automatically picks up the axis units from the quantitites we plot.
164-
```
165-
166-
- Convert the wavelength to microns
167-
- Multiple the signal by FSCALE from the header
168-
- Use the MASK column to discard any values where MASK = odd or >=64
169-
- Use the VAR column (variance) to plot the error on the data
170-
171-
```{code-cell} ipython3
172-
signal_scaled = spectra['SIGNAL'] * spec_header['FSCALE']
173-
174-
plt.plot(spectra['WAVELENGTH'].to(u.micron), signal_scaled)
175-
plt.title(obj_id)
176-
```
159+
We use the MASK column to create a boolean mask for values to ignore. We use the inverse of this mask to mark the flux bins to use.
177160

178161
```{code-cell} ipython3
179-
## Use the MASK column to create a "good mask", just the flux bins to use
180162
bad_mask = (spectra['MASK'].value % 2 == 1) | (spectra['MASK'].value >= 64)
181-
good_mask = ~bad_mask
182-
183-
## Plot the spectrum in black for the good flux bins and in red for the masked (do not use) flux bins.
184-
for i in range(1, len(wavelength)):
185-
## Plot good data (black)
186-
if good_mask[i]:
187-
plt.plot(wavelength[i-1:i+1], signal_scaled[i-1:i+1], color='black')
188-
## Plot bad data (red)
189-
elif bad_mask[i]:
190-
plt.plot(wavelength[i-1:i+1], signal_scaled[i-1:i+1], color='red')
191163
164+
plt.plot(spectra['WAVELENGTH'][~bad_mask].to(u.micron), signal_scaled[~bad_mask], color='black', label='Spectrum')
165+
plt.plot(spectra['WAVELENGTH'][bad_mask], signal_scaled[bad_mask], color='red', label='Do not use')
166+
plt.plot(spectra['WAVELENGTH'], np.sqrt(spectra['VAR']) * spec_header['FSCALE'], color='grey', label='Error')
192167
193-
plt.plot(wavelength, np.sqrt(spectra['VAR']) * spec_header['FSCALE'], color='grey', label='Error')
194-
195-
## Add the line names to the legend
196-
spectrum_line = Line2D([0], [0], color='black', lw=2, label='Spectrum')
197-
bad_line = Line2D([0], [0], color='red', lw=2, label='Do Not Use')
198-
error_line = Line2D([0], [0], color='grey', lw=2, label='Error')
199-
plt.legend(handles=[spectrum_line, bad_line,error_line], loc='upper right')
200-
201-
202-
plt.xlabel('Wavelength ($\mu$m)')
203-
plt.ylabel('Flux ($\mathrm{erg\,\mu m^{-1}\,s^{-1}\,cm^{-2}}$)')
168+
plt.legend(loc='upper right')
204169
plt.ylim(-0.15E-16, 0.25E-16)
205-
# plt.xlim(1.25,1.85)
206-
plt.title('Object ID is ' + str(obj_id))
207-
208-
plt.show()
170+
plt.title(f'Object ID {obj_id}')
209171
```
210172

211173
## About this Notebook
212174

213175
**Author**: Tiffany Meshkat, Anahita Alavi, Anastasia Laity, Andreas Faisst, Brigitta Sipőcz, Dan Masters, Harry Teplitz, Jaladh Singhal, Shoubaneh Hemmati, Vandana Desai
214176

215-
**Updated**: 2025-03-28
177+
**Updated**: 2025-03-31
216178

217179
**Contact:** [the IRSA Helpdesk](https://irsa.ipac.caltech.edu/docs/help_desk.html) with questions or reporting problems.
180+
181+
```{code-cell} ipython3
182+
183+
```

0 commit comments

Comments
 (0)