19
19
def _get_boxcar_weights (center , hwidth , npix ):
20
20
"""
21
21
Compute weights given an aperture center, half width,
22
- and number of pixels
22
+ and number of pixels.
23
+
24
+ Based on `get_boxcar_weights()` from a JDAT Notebook by Karl Gordon:
25
+ https://github.com/spacetelescope/jdat_notebooks/blob/main/notebooks/MIRI_LRS_spectral_extraction/miri_lrs_spectral_extraction.ipynb
26
+
27
+ Parameters
28
+ ----------
29
+ center : float, required
30
+ The index of the aperture's center pixel on the larger image's
31
+ cross-dispersion axis.
32
+
33
+ hwidth : float, required
34
+ Half of the aperture's width in the cross-dispersion direction.
35
+
36
+ npix : float, required
37
+ The number of pixels in the larger image's cross-dispersion
38
+ axis.
39
+
40
+ Returns
41
+ -------
42
+ weights : `~numpy.ndarray`
43
+ A 2D image with weights assigned to pixels that fall within the
44
+ defined aperture.
23
45
"""
24
- weights = np .zeros ((npix ))
46
+ weights = np .zeros (npix )
47
+
48
+ # shift center from integer to pixel space, where pixel N is [N-0.5, N+0.5),
49
+ # not [N, N+1). a pixel's integer index corresponds to its middle, not edge
50
+ center += 0.5
25
51
26
- # pixels with full weight
27
- fullpixels = [max (0 , int (center - hwidth + 1 )),
28
- min (int (center + hwidth ), npix )]
29
- weights [fullpixels [0 ]:fullpixels [1 ]] = 1.0
52
+ # pixels given full weight because they sit entirely within the aperture
53
+ fullpixels = [max (0 , int (np . ceil ( center - hwidth ) )),
54
+ min (int (np . floor ( center + hwidth ) ), npix )]
55
+ weights [fullpixels [0 ]:fullpixels [1 ]] = 1
30
56
31
- # pixels at the edges of the boxcar with partial weight
57
+ # pixels at the edges of the boxcar with partial weight, if any
32
58
if fullpixels [0 ] > 0 :
33
- w = hwidth - (center - fullpixels [0 ] + 0.5 )
34
- if w >= 0 :
35
- weights [fullpixels [0 ] - 1 ] = w
36
- else :
37
- weights [fullpixels [0 ]] = 1. + w
59
+ weights [fullpixels [0 ] - 1 ] = hwidth - (center - fullpixels [0 ])
38
60
if fullpixels [1 ] < npix :
39
- weights [fullpixels [1 ]] = hwidth - (fullpixels [1 ] - center - 0.5 )
61
+ weights [fullpixels [1 ]] = hwidth - (fullpixels [1 ] - center )
40
62
41
63
return weights
42
64
@@ -46,23 +68,26 @@ def _ap_weight_image(trace, width, disp_axis, crossdisp_axis, image_shape):
46
68
"""
47
69
Create a weight image that defines the desired extraction aperture.
48
70
71
+ Based on `ap_weight_images()` from a JDAT Notebook by Karl Gordon:
72
+ https://github.com/spacetelescope/jdat_notebooks/blob/main/notebooks/MIRI_LRS_spectral_extraction/miri_lrs_spectral_extraction.ipynb
73
+
49
74
Parameters
50
75
----------
51
- trace : Trace
76
+ trace : `~specreduce.tracing. Trace`, required
52
77
trace object
53
- width : float
78
+ width : float, required
54
79
width of extraction aperture in pixels
55
- disp_axis : int
80
+ disp_axis : int, required
56
81
dispersion axis
57
- crossdisp_axis : int
82
+ crossdisp_axis : int, required
58
83
cross-dispersion axis
59
- image_shape : tuple with 2 elements
84
+ image_shape : tuple with 2 elements, required
60
85
size (shape) of image
61
86
62
87
Returns
63
88
-------
64
- wimage : 2D image
65
- weight image defining the aperture
89
+ wimage : `~numpy.ndarray`
90
+ a 2D weight image defining the aperture
66
91
"""
67
92
wimage = np .zeros (image_shape )
68
93
hwidth = 0.5 * width
0 commit comments