Skip to content

Commit 9df1255

Browse files
committed
Clean up type spec, spelling, and whitespace
1 parent 0958584 commit 9df1255

1 file changed

Lines changed: 28 additions & 33 deletions

File tree

tutorials/spherex/spherex_psf.md

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ The goal of this tutorial now is to find the PSF corresponding to our input coor
219219
+++
220220

221221
```{warning}
222-
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 6.5.6 and beyond.
222+
In the SPHEREx spectral image versions prior or equal to 6.5.5, there was a mismatch between the spatial layout of the PSF zones and the indexing of the PSF zones in the image header. This has now been fixed in versions 6.5.6 and beyond.
223223
224224
For more information about these changes, see the following webpage: [PSF Erratum](https://irsa.ipac.caltech.edu/data/SPHEREx/docs/psfhdrerr.html)
225225
@@ -235,7 +235,7 @@ def parse_version(v):
235235
# detect modifiers
236236
modifier = None
237237
base = v
238-
238+
239239
if "+" in v:
240240
base, modifier = v.split("+", 1)
241241
@@ -277,46 +277,45 @@ The function that can be used to update the header is shown below. The function
277277
* changes the PSF zone indexing and
278278
* changes the version of the header such that it is consistent with the new released images
279279

280-
Note that this function an work as standalone function to process many images.
280+
Note that this function can work as standalone function to process many images.
281281

282282
```{code-cell} ipython3
283283
def update_psf_header(old_hdul):
284284
"""
285-
Fix a old PSF FITS file header by rewriting only the per-plane header metadata
285+
Fix an old PSF FITS file header by rewriting only the per-plane header metadata
286286
so that plane k corresponds to x-fast ordering:
287287
k0 = iy * bins_x + ix
288288
289289
The cube data are left untouched.
290290
291291
Parameters
292292
----------
293-
old_hdul : astropy hdul
293+
old_hdul : fits.HDUList
294294
Old SPHEREx Spectral Image HDUL
295295
296296
Return
297297
----------
298-
new_hdul : astropy hdul
298+
new_hdul : fits.HDUList
299299
New SPHEREx Spectral Image HDUL with updated PSF zone data in header and updated version number
300-
301300
"""
302301
303302
def parse_version(v):
304303
# detect modifiers
305304
modifier = None
306305
base = v
307-
306+
308307
if "+" in v:
309308
base, modifier = v.split("+", 1)
310-
309+
311310
base_version = Version(base)
312-
311+
313312
if modifier is None:
314313
return (0, base_version, 0)
315-
314+
316315
# extract numeric part if present
317316
m = re.search(r'\d+', modifier)
318317
modnum = int(m.group()) if m else 0
319-
318+
320319
return (1, base_version, modnum)
321320
322321
## Check if old version
@@ -327,58 +326,58 @@ def update_psf_header(old_hdul):
327326
print(f"New version detected ({this_version}) -> Do not update header.")
328327
return(old_hdul)
329328
330-
## Define some auxillary functions -------
329+
## Define some auxiliary functions -------
331330
def parse_ixiy_from_comment(comment):
332331
_zone_pat = re.compile(r"\((\d+)\s*,\s*(\d+)\)")
333332
m = _zone_pat.search(str(comment))
334333
if not m:
335334
raise ValueError(f"Could not parse zone indices from comment: {comment!r}")
336335
return int(m.group(1)), int(m.group(2))
337-
336+
338337
def infer_grid_shape_from_header_comments(hdr, nzone):
339338
max_ix = -1
340339
max_iy = -1
341-
340+
342341
for k1 in range(1, nzone + 1):
343342
key = f"XCTR_{k1}"
344343
if key not in hdr:
345344
raise KeyError(f"Missing required key: {key}")
346345
ix, iy = parse_ixiy_from_comment(hdr.comments[key])
347346
max_ix = max(max_ix, ix)
348347
max_iy = max(max_iy, iy)
349-
348+
350349
bins_x = max_ix + 1
351350
bins_y = max_iy + 1
352-
351+
353352
if bins_x * bins_y != nzone:
354353
raise ValueError(
355354
f"Inconsistent grid inferred from comments: "
356355
f"bins_x={bins_x}, bins_y={bins_y}, nzone={nzone}"
357356
)
358-
357+
359358
return bins_x, bins_y
360-
359+
361360
def collect_axis_values_by_zone(hdr, nzone):
362361
"""
363362
Read the old header and collect unique x/y centers and widths by zone index
364363
labels found in the comments.
365-
364+
366365
This uses the old header only to recover the per-axis values for each ix, iy.
367366
It does NOT use the old plane ordering as truth.
368367
"""
369368
x_center_by_ix = {}
370369
y_center_by_iy = {}
371370
x_width_by_ix = {}
372371
y_width_by_iy = {}
373-
372+
374373
for k1 in range(1, nzone + 1):
375374
ix, iy = parse_ixiy_from_comment(hdr.comments[f"XCTR_{k1}"])
376-
375+
377376
xck = f"XCTR_{k1}"
378377
yck = f"YCTR_{k1}"
379378
xwk = f"XWID_{k1}"
380379
ywk = f"YWID_{k1}"
381-
380+
382381
if xck in hdr:
383382
val = hdr[xck]
384383
if ix in x_center_by_ix and not np.isclose(x_center_by_ix[ix], val):
@@ -387,7 +386,7 @@ def update_psf_header(old_hdul):
387386
f"{x_center_by_ix[ix]} vs {val}"
388387
)
389388
x_center_by_ix[ix] = val
390-
389+
391390
if yck in hdr:
392391
val = hdr[yck]
393392
if iy in y_center_by_iy and not np.isclose(y_center_by_iy[iy], val):
@@ -396,7 +395,7 @@ def update_psf_header(old_hdul):
396395
f"{y_center_by_iy[iy]} vs {val}"
397396
)
398397
y_center_by_iy[iy] = val
399-
398+
400399
if xwk in hdr:
401400
val = hdr[xwk]
402401
if ix in x_width_by_ix and not np.isclose(x_width_by_ix[ix], val):
@@ -405,7 +404,7 @@ def update_psf_header(old_hdul):
405404
f"{x_width_by_ix[ix]} vs {val}"
406405
)
407406
x_width_by_ix[ix] = val
408-
407+
409408
if ywk in hdr:
410409
val = hdr[ywk]
411410
if iy in y_width_by_iy and not np.isclose(y_width_by_iy[iy], val):
@@ -414,9 +413,9 @@ def update_psf_header(old_hdul):
414413
f"{y_width_by_iy[iy]} vs {val}"
415414
)
416415
y_width_by_iy[iy] = val
417-
416+
418417
return x_center_by_ix, y_center_by_iy, x_width_by_ix, y_width_by_iy
419-
## End defining some auxillary functions --------
418+
## End defining some auxiliary functions --------
420419
421420
## Get Header
422421
extname = "PSF"
@@ -484,7 +483,7 @@ def update_psf_header(old_hdul):
484483
hdr_out["HISTORY"] = "Rewrote PSF per-plane zone metadata to x-fast ordering."
485484
hdr_out["HISTORY"] = "Cube plane data left unchanged."
486485
487-
486+
488487
489488
new_hdu = fits.ImageHDU(data=cube, header=hdr_out, name=hdu.name)
490489
@@ -658,7 +657,3 @@ To use this PSF for forward modeling or fitting, you must:
658657
**Contact:** Contact [IRSA Helpdesk](https://irsa.ipac.caltech.edu/docs/help_desk.html) with questions or problems.
659658

660659
**Runtime:** Approximately 30 seconds.
661-
662-
```{code-cell} ipython3
663-
664-
```

0 commit comments

Comments
 (0)