@@ -127,7 +127,7 @@ def fourier_imshift(image, shift):
127
127
return offset
128
128
129
129
130
- def align_array (reference , target , mask = None ):
130
+ def align_array (reference , target , mask = None , return_aligned = True ):
131
131
"""
132
132
Computes shifts between target image (or image "slices") and the reference
133
133
image and re-aligns input images to the target.
@@ -160,21 +160,26 @@ def align_array(reference, target, mask=None):
160
160
161
161
if len (target .shape ) == 2 :
162
162
shifts = align_fourierLSQ (reference , target , mask = mask )
163
- aligned = fourier_imshift (target , - shifts )
163
+ if return_aligned :
164
+ aligned = fourier_imshift (target , - shifts )
164
165
165
166
elif len (target .shape ) == 3 :
166
167
nslices = target .shape [0 ]
167
168
shifts = np .empty ((nslices , 3 ), dtype = float )
168
- aligned = np .empty_like (target )
169
+ if return_aligned :
170
+ aligned = np .empty_like (target )
169
171
170
172
for m in range (nslices ):
171
173
sh = align_fourierLSQ (reference , target [m ], mask = mask )
172
174
shifts [m , :] = sh
173
- aligned [m , :, :] = fourier_imshift (target [m ], - sh )
175
+ if return_aligned :
176
+ aligned [m , :, :] = fourier_imshift (target [m ], - sh )
174
177
175
178
else :
176
179
raise ValueError ("Input target image must be either a 2D or 3D array." )
177
180
181
+ if not return_aligned :
182
+ return shifts
178
183
return aligned , shifts
179
184
180
185
@@ -216,17 +221,20 @@ def align_models(reference, target, mask):
216
221
output_model = QuadModel (quad_shape )
217
222
output_model .update (target )
218
223
224
+ # Compute the shifts of the PSF ("target") images relative to
225
+ # the science ("reference") image in the first integration
226
+ shifts = align_array (
227
+ reference .data [0 ].astype (np .float64 ),
228
+ target .data .astype (np .float64 ),
229
+ mask = mask .data , return_aligned = False )
230
+
219
231
# Loop over all integrations of the science exposure
220
232
for k in range (nrefslices ):
221
233
222
- # Compute the shifts of the PSF ("target") images relative to
223
- # the science ("reference") image in this integration, and apply
224
- # the shifts to the PSF images
225
- d , shifts = align_array (
226
- reference .data [k ].astype (np .float64 ),
234
+ # Apply the shifts to the PSF images
235
+ output_model .data [k ] = fourier_imshift (
227
236
target .data .astype (np .float64 ),
228
- mask .data )
229
- output_model .data [k ] = d
237
+ - shifts )
230
238
231
239
# Apply the same shifts to the PSF error arrays, if they exist
232
240
if target .err is not None :
@@ -237,5 +245,4 @@ def align_models(reference, target, mask):
237
245
# TODO: in the future we need to add shifts and other info (such as
238
246
# slice ID from the reference image to which target was aligned)
239
247
# to output cube metadata (or property).
240
-
241
248
return output_model
0 commit comments