Skip to content

Commit 7ea238a

Browse files
authored
Merge pull request #86 from bsipocz/euclid_cleanup_more_astropy
ENH: More cleanups for euclid notebooks: remove bytesIO and astropy-ify notebook 1&5
2 parents 171dc26 + 1613744 commit 7ea238a

File tree

4 files changed

+114
-127
lines changed

4 files changed

+114
-127
lines changed

tutorials/euclid_access/1_Euclid_intro_MER_images.md

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ Each MER image is approximately 1.47 GB. Downloading can take some time.
6565
import re
6666
6767
import numpy as np
68-
import pandas as pd
6968
7069
import matplotlib.pyplot as plt
7170
from matplotlib.patches import Ellipse
@@ -80,9 +79,6 @@ from astropy import units as u
8079
8180
from astroquery.ipac.irsa import Irsa
8281
import sep
83-
84-
# Copy-on-write is more performant and avoids unexpected modifications of the original DataFrame.
85-
pd.options.mode.copy_on_write = True
8682
```
8783

8884
## 1. Search for multiwavelength Euclid Q1 MER mosaics that cover the star HD 168151
@@ -123,20 +119,23 @@ science_images
123119
Note that 'access_estsize' is in units of kb
124120

125121
```{code-cell} ipython3
126-
filename = science_images[science_images['energy_bandpassname']=='VIS']['access_url'][0]
127-
filesize = science_images[science_images['energy_bandpassname']=='VIS']['access_estsize'][0]/1000000
128-
122+
filename = science_images[science_images['energy_bandpassname'] == 'VIS']['access_url'][0]
123+
filesize = science_images[science_images['energy_bandpassname'] == 'VIS']['access_estsize'][0] / 1000000
129124
print(filename)
130125
131126
print(f'Please note this image is {filesize} GB. With 230 Mbps internet download speed, it takes about 1 minute to download.')
132127
```
133128

129+
```{code-cell} ipython3
130+
science_images
131+
```
132+
134133
### Extract the tileID of this image from the filename
135134

136135
```{code-cell} ipython3
137-
tileID=re.search(r'TILE\s*(\d{9})', filename).group(1)
136+
tileID = science_images[science_images['energy_bandpassname'] == 'VIS']['obs_id'][0][:9]
138137
139-
print('The MER tile ID for this object is :',tileID)
138+
print(f'The MER tile ID for this object is : {tileID}')
140139
```
141140

142141
Retrieve the MER image -- note this file is about 1.46 GB
@@ -146,7 +145,7 @@ fname = download_file(filename, cache=True)
146145
hdu_mer_irsa = fits.open(fname)
147146
print(hdu_mer_irsa.info())
148147
149-
head_mer_irsa = hdu_mer_irsa[0].header
148+
header_mer_irsa = hdu_mer_irsa[0].header
150149
```
151150

152151
If you would like to save the MER mosaic to disk, uncomment the following cell.
@@ -160,21 +159,22 @@ Please also define a suitable download directory; by default it will be `data` a
160159
Have a look at the header information for this image.
161160

162161
```{code-cell} ipython3
163-
head_mer_irsa
162+
header_mer_irsa
164163
```
165164

166165
Lets extract just the primary image.
167166

168167
```{code-cell} ipython3
169-
im_mer_irsa=hdu_mer_irsa[0].data
168+
im_mer_irsa = hdu_mer_irsa[0].data
170169
171170
print(im_mer_irsa.shape)
172171
```
173172

174173
Due to the large field of view of the MER mosaic, let's cut out a smaller section (2"x2")of the MER mosaic to inspect the image
175174

176175
```{code-cell} ipython3
177-
plt.imshow(im_mer_irsa[0:1200,0:1200], cmap='gray', origin='lower', norm=ImageNormalize(im_mer_irsa[0:1200,0:1200], interval=PercentileInterval(99.9), stretch=AsinhStretch()))
176+
plt.imshow(im_mer_irsa[0:1200,0:1200], cmap='gray', origin='lower',
177+
norm=ImageNormalize(im_mer_irsa[0:1200,0:1200], interval=PercentileInterval(99.9), stretch=AsinhStretch()))
178178
colorbar = plt.colorbar()
179179
```
180180

@@ -203,21 +203,20 @@ urls
203203
Create an array with the instrument and filter name so we can add this to the plots.
204204

205205
```{code-cell} ipython3
206-
df_im_euclid.loc[:, "filters"] = df_im_euclid["instrument_name"] + "_" + df_im_euclid["energy_bandpassname"]
206+
science_images['filters'] = science_images['instrument_name'] + "_" + science_images['energy_bandpassname']
207207
208-
## Note that VIS_VIS appears in the filters, so update that filter to just say VIS
209-
df_im_euclid.loc[df_im_euclid["filters"] == "VIS_VIS", "filters"] = "VIS"
208+
# VIS_VIS appears in the filters, so update that filter to just say VIS
209+
science_images['filters'][science_images['filters']== 'VIS_VIS'] = "VIS"
210210
211-
filters = df_im_euclid['filters'].to_numpy()
212-
filters
211+
science_images['filters']
213212
```
214213

215214
## The image above is very large, so let's cut out a smaller image to inspect these data.
216215

217216
```{code-cell} ipython3
218217
######################## User defined section ############################
219218
## How large do you want the image cutout to be?
220-
im_cutout= 1.0 * u.arcmin
219+
im_cutout = 1.0 * u.arcmin
221220
222221
## What is the center of the cutout?
223222
## For now choosing a random location on the image
@@ -229,7 +228,7 @@ dec = 64.525
229228
# ra = 273.474451
230229
# dec = 64.397273
231230
232-
coords_cutout = SkyCoord(ra, dec, unit=(u.deg, u.deg), frame='icrs')
231+
coords_cutout = SkyCoord(ra, dec, unit='deg', frame='icrs')
233232
234233
##########################################################################
235234
@@ -275,7 +274,7 @@ rows = -(-num_images // columns)
275274
fig, axes = plt.subplots(rows, columns, figsize=(4 * columns, 4 * rows), subplot_kw={'projection': WCS(final_hdulist[0].header)})
276275
axes = axes.flatten()
277276
278-
for idx, (ax, filt) in enumerate(zip(axes, filters)):
277+
for idx, (ax, filt) in enumerate(zip(axes, science_images['filters'])):
279278
image_data = final_hdulist[idx].data
280279
norm = ImageNormalize(image_data, interval=PercentileInterval(99.9), stretch=AsinhStretch())
281280
ax.imshow(image_data, cmap='gray', origin='lower', norm=norm)
@@ -296,13 +295,9 @@ plt.show()
296295
First we list all the filters so you can choose which cutout you want to extract sources on. We will choose VIS.
297296

298297
```{code-cell} ipython3
299-
filters
300-
```
301-
302-
```{code-cell} ipython3
303-
filt_index = np.where(filters == 'VIS')[0][0]
298+
filt_index = np.where(science_images['filters'] == 'VIS')[0][0]
304299
305-
img1=final_hdulist[filt_index].data
300+
img1 = final_hdulist[filt_index].data
306301
```
307302

308303
### Extract some sources from the cutout using sep (python package based on source extractor)
@@ -386,8 +381,8 @@ for i in range(len(sources_thr)):
386381

387382
## About this Notebook
388383

389-
**Author**: Tiffany Meshkat (IPAC Scientist)
384+
**Author**: Tiffany Meshkat, Anahita Alavi, Anastasia Laity, Andreas Faisst, Brigitta Sipőcz, Dan Masters, Harry Teplitz, Jaladh Singhal, Shoubaneh Hemmati, Vandana Desai
390385

391-
**Updated**: 2025-03-19
386+
**Updated**: 2025-03-31
392387

393388
**Contact:** [the IRSA Helpdesk](https://irsa.ipac.caltech.edu/docs/help_desk.html) with questions or reporting problems.

tutorials/euclid_access/3_Euclid_intro_1D_spectra.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ Open the large FITS file without loading it entirely into memory, pulling out ju
113113

114114
```{code-cell} ipython3
115115
with fits.open(file_uri) as hdul:
116-
spectra = QTable.read(hdul[result['hdu'][0]], format='fits')
116+
spectrum = QTable.read(hdul[result['hdu'][0]], format='fits')
117117
118118
spec_header = hdul[result['hdu'][0]].header
119119
```
120120

121121
```{code-cell} ipython3
122-
spectra
122+
spectrum
123123
```
124124

125125
```{code-cell} ipython3
@@ -145,25 +145,25 @@ The 1D combined spectra table contains 6 columns, below are a few highlights:
145145
```
146146

147147
```{code-cell} ipython3
148-
signal_scaled = spectra['SIGNAL'] * spec_header['FSCALE']
148+
signal_scaled = spectrum['SIGNAL'] * spec_header['FSCALE']
149149
```
150150

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

153153
```{code-cell} ipython3
154-
plt.plot(spectra['WAVELENGTH'].to(u.micron), spectra['MASK'])
154+
plt.plot(spectrum['WAVELENGTH'].to(u.micron), spectrum['MASK'])
155155
plt.ylabel('Mask value')
156156
plt.title('Values of MASK by flux bin')
157157
```
158158

159159
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.
160160

161161
```{code-cell} ipython3
162-
bad_mask = (spectra['MASK'].value % 2 == 1) | (spectra['MASK'].value >= 64)
162+
bad_mask = (spectrum['MASK'].value % 2 == 1) | (spectrum['MASK'].value >= 64)
163163
164-
plt.plot(spectra['WAVELENGTH'].to(u.micron), np.ma.masked_where(bad_mask, signal_scaled), color='black', label='Spectrum')
165-
plt.plot(spectra['WAVELENGTH'], np.ma.masked_where(~bad_mask, signal_scaled), color='red', label='Do not use')
166-
plt.plot(spectra['WAVELENGTH'], np.sqrt(spectra['VAR']) * spec_header['FSCALE'], color='grey', label='Error')
164+
plt.plot(spectrum['WAVELENGTH'].to(u.micron), np.ma.masked_where(bad_mask, signal_scaled), color='black', label='Spectrum')
165+
plt.plot(spectrum['WAVELENGTH'], np.ma.masked_where(~bad_mask, signal_scaled), color='red', label='Do not use')
166+
plt.plot(spectrum['WAVELENGTH'], np.sqrt(spectrum['VAR']) * spec_header['FSCALE'], color='grey', label='Error')
167167
168168
plt.legend(loc='upper right')
169169
plt.ylim(-0.15E-16, 0.25E-16)

tutorials/euclid_access/4_Euclid_intro_PHZ_catalog.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ If you have questions about this notebook, please contact the [IRSA helpdesk](ht
5151

5252
```{code-cell} ipython3
5353
# Uncomment the next line to install dependencies if needed.
54-
# !pip install requests matplotlib pandas 'astropy>=5.3' 'astroquery>=0.4.10' fsspec firefly_client
54+
# !pip install matplotlib pandas 'astropy>=5.3' 'astroquery>=0.4.10' fsspec firefly_client
5555
```
5656

5757
```{code-cell} ipython3
58-
from io import BytesIO
5958
import os
6059
import re
60+
import urllib
6161
62-
import requests
6362
import matplotlib.pyplot as plt
6463
6564
from astropy.coordinates import SkyCoord
@@ -324,20 +323,15 @@ df2=result2.to_table().to_pandas()
324323
df2
325324
```
326325

327-
```{code-cell} ipython3
328-
## Create the full filename/url
329-
irsa_url='https://irsa.ipac.caltech.edu/'
326+
Pull out the file name from the ``result`` table:
330327

331-
file_url=irsa_url+df2['uri'].iloc[0]
332-
file_url
328+
```{code-cell} ipython3
329+
file_uri = urllib.parse.urljoin(Irsa.tap_url, result2['uri'][0])
330+
file_uri
333331
```
334332

335333
```{code-cell} ipython3
336-
## Open the large FITS file without loading it entirely into memory
337-
## pulling out just the extension we want for the 1D spectra of our object
338-
response = requests.get(file_url)
339-
340-
with fits.open(BytesIO(response.content), memmap=True) as hdul:
334+
with fits.open(file_uri) as hdul:
341335
hdu = hdul[df2['hdu'].iloc[0]]
342336
dat = Table.read(hdu, format='fits', hdu=1)
343337
df_obj_irsa = dat.to_pandas()

0 commit comments

Comments
 (0)