Skip to content

Commit 380ab4e

Browse files
committed
update goals; remarks PSF header fix; correct version look up
1 parent fb6f95e commit 380ab4e

1 file changed

Lines changed: 49 additions & 29 deletions

File tree

tutorials/spherex/spherex_psf.md

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ kernelspec:
2222
+++
2323

2424
```{warning}
25-
In the SPHEREx spectral image versions prior to XXXX, there was a missmatch between the spatial layout of the PSF zones and the the indexing of the PSF zones in the header. This has now been fixed in versions post XXXX.
26-
However, users using the old versions will need to implement and extra step (described below in Section 5) to update the image header.
25+
In the SPHEREx spectral image versions prior or equal to 6.5.5, there was a missmatch between the spatial layout of the PSF zones and the the indexing of the PSF zones in the image header. This has now been fixed in versions post 6.5.5.
26+
However, users using the old versions will need to implement and extra step (described below in Section 5.1) to update the image header.
2727
28-
For more information, see the following webpage: [PSF Erratum](WEBPAGE)
28+
For more information about these changes, see the following webpage: [PSF Erratum](WEBPAGE)
2929
```
3030

3131
+++
3232

3333
## 1. Learning Goals
3434

3535
* Determine how pixels in a SPHEREx cutout map to the pixels in the parent SPHEREx spectral image.
36-
* Understand the structure of the PSF extension in a SPHEREx cutout (which is the same as the PSF extension in the parent spectral image)
36+
* Understand the structure of the PSF extension in a SPHEREx cutout (which is the same as the PSF extension in the parent spectral image).
3737
* Learn how to tell which version of the SPHEREx spectral image you are looking at, and how to interpret this information to obtain the correct PSF extension for the SPHEREx spectral images.
3838
* Learn which plane in a SPHEREx cutout PSF extension cube most accurately describes the coordinates you are interested in.
3939

@@ -70,6 +70,7 @@ import re
7070
import time
7171
import urllib.error
7272
import copy
73+
from packaging.version import Version
7374
7475
import astropy.units as u
7576
import matplotlib.pyplot as plt
@@ -171,7 +172,7 @@ for attempt in range(max_retries):
171172
time.sleep(10 * (attempt + 1))
172173
```
173174

174-
Examine the header.
175+
Let's examine the HDU list info.
175176

176177
```{code-cell} ipython3
177178
hdul.info()
@@ -215,24 +216,46 @@ The goal of this tutorial now is to find the PSF corresponding to our input coor
215216
+++
216217

217218
```{warning}
218-
In the SPHEREx spectral image versions prior to XXXX, there was a missmatch between the spatial layout of the PSF zones and the the indexing of the PSF zones in the header. This has now been fixed in versions post XXXX.
219+
In the SPHEREx spectral image versions prior or equal to 6.5.5, there was a missmatch between the spatial layout of the PSF zones and the the indexing of the PSF zones in the image header. This has now been fixed in versions post 6.5.5.
219220
220-
For more information, see the following webpage: [PSF Erratum](WEBPAGE)
221+
For more information about these changes, see the following webpage: [PSF Erratum](WEBPAGE)
221222
222223
**Users using the old versions will need to implement and extra step (described below) to update the image header.**
223224
```
224225

225-
Let's first check if a header update is necessary. We can do that by checking the `VERSION` keyword in the header.
226+
Let's first check here if a header update is necessary. We can do that by printing the `VERSION` keyword in the header.
227+
228+
For comparisons versions, we can use the Python-internal `Version()` function from the `packaging.version` package. However, since reprocessed images can have version names such as `6.5.4-001` (which are superior to `6.5.4`, for example), we have to write a little wrapper function such that `Version()` can interpret these correctly.
229+
230+
```{code-cell} ipython3
231+
def parse_version(v):
232+
'''
233+
Parses versions correctly such that "6.5.4-001" is later than "6.5.5".
234+
'''
235+
if "-" in v:
236+
base, mod = v.split("-", 1)
237+
return (1, Version(base), int(mod)) # modified versions always rank higher
238+
else:
239+
return (0, Version(v), 0)
240+
```
241+
242+
Now, we can use this function to properly compare versions.
226243

227244
```{code-cell} ipython3
228-
image_hdul['PRIMARY'].header["VERSION"]
245+
this_version = parse_version( image_hdul['PRIMARY'].header["VERSION"] )
246+
print(f"Current version is {this_version}")
247+
248+
if this_version <= parse_version("6.5.5"):
249+
print("PSF header needs to be updated! -> Go to Section 5.1 :(")
250+
else:
251+
print("PSF header is already up-to-date! -> Proceed to Section 6 :)")
229252
```
230253

231-
If the version of the SPHEREx spectral image is less than `6.4`, we will have to update the header. This is explained in Section 5.1. If the version is later than `6.4`, the header is already updated and the PSF issue is fixed. In this case, proceed to Section 6 directly.
254+
If the version of the SPHEREx spectral image is less or equal than `6.5.5`, we will have to update the header. This is explained in Section 5.1. If the version is later than `6.5.5`, the header is already updated and the PSF issue is fixed. In this case, proceed to Section 6 directly.
232255

233256
+++
234257

235-
### 5.1 Updating Old SPHEREx Spectral Image Data (`VERSIONS` < XXX)
258+
### 5.1 Updating Old SPHEREx Spectral Image Data (if version is $\leq$ 6.5.5)
236259

237260
+++
238261

@@ -264,11 +287,18 @@ def update_psf_header(old_hdul):
264287
265288
"""
266289
290+
def parse_version(v):
291+
if "-" in v:
292+
base, mod = v.split("-", 1)
293+
return (1, Version(base), int(mod)) # modified versions always rank higher
294+
else:
295+
return (0, Version(v), 0)
296+
267297
## Check if old version
268-
this_version = float( old_hdul['PRIMARY'].header["VERSION"] )
269-
if this_version <= 6.4:
298+
this_version = parse_version( old_hdul['PRIMARY'].header["VERSION"] )
299+
if this_version <= parse_version("6.5.5"):
270300
print(f"Old version detected ({this_version}) -> Update header.")
271-
elif this_version > 6.4:
301+
elif this_version > Version("6.5.5"):
272302
print(f"New version detected ({this_version}) -> Do not update header.")
273303
return(old_hdul)
274304
@@ -466,23 +496,13 @@ new_image_hdul['PSF'].header[22:40]
466496
Now we have to update the variables we have set above.
467497

468498
```{code-cell} ipython3
469-
### TODO UPDATE VARIABLES.
470-
```
471-
472-
```{code-cell} ipython3
473-
hdul['PSF'].header[22:40]
499+
cutout_header = new_image_hdul['IMAGE'].header
500+
psf_header = new_image_hdul['PSF'].header
501+
cutout = new_image_hdul['IMAGE'].data
502+
psfcube = new_image_hdul['PSF'].data
474503
```
475504

476-
We confirm that the oversampling factor (`OVERSAMP`) is 10.
477-
The PSFs are distributed in an even grid with 11x11 zones.
478-
Each of the 121 PSFs is responsible for one of these zones.
479-
The PSF header therefore includes the center position of these zones as well as the width of the zones.
480-
These center coordinate are specified with `XCTR_i` and `YCTR_i`, respectively, where i = 1...121.
481-
The widths are specified with `XWID_i` and `YWID_i`, respectively, where again i = 1...121.
482-
The zones have approximately equal widths and are arranged in an even grid.
483-
The size of the zones is sufficient to capture well the changes of the PSF size and structure with wavelength and spatial coordinates.
484-
485-
The goal of this tutorial now is to find the PSF corresponding to our input coordinates of interest.
505+
With this fix, we are now ready to proceed!
486506

487507
+++
488508

0 commit comments

Comments
 (0)