drizzle 2.2.0__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- drizzle/__init__.py +13 -0
- drizzle/cdrizzle.cp313-win_amd64.pyd +0 -0
- drizzle/resample.py +1158 -0
- drizzle/tests/__init__.py +0 -0
- drizzle/tests/helpers.py +215 -0
- drizzle/tests/test_cdrizzle.py +29 -0
- drizzle/tests/test_overlap_calc.py +287 -0
- drizzle/tests/test_resample.py +2202 -0
- drizzle/tests/test_utils.py +245 -0
- drizzle/util.py +35 -0
- drizzle/utils.py +278 -0
- drizzle-2.2.0.dist-info/METADATA +172 -0
- drizzle-2.2.0.dist-info/RECORD +16 -0
- drizzle-2.2.0.dist-info/WHEEL +5 -0
- drizzle-2.2.0.dist-info/licenses/LICENSE.rst +31 -0
- drizzle-2.2.0.dist-info/top_level.txt +1 -0
drizzle/resample.py
ADDED
|
@@ -0,0 +1,1158 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The `drizzle` module defines the `Drizzle` class, for combining input
|
|
3
|
+
images into a single output image using the drizzle algorithm.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import warnings
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from drizzle import cdrizzle
|
|
11
|
+
|
|
12
|
+
__all__ = ["Drizzle", "blot_image"]
|
|
13
|
+
|
|
14
|
+
SUPPORTED_DRIZZLE_KERNELS = [
|
|
15
|
+
"square",
|
|
16
|
+
"gaussian",
|
|
17
|
+
"point",
|
|
18
|
+
"turbo",
|
|
19
|
+
"lanczos2",
|
|
20
|
+
"lanczos3",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
CTX_PLANE_BITS = 32
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_DEPRECATED_ARG = object()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Drizzle:
|
|
30
|
+
"""
|
|
31
|
+
A class for managing resampling and co-adding of multiple images onto a
|
|
32
|
+
common output grid. The main method of this class is :py:meth:`add_image`.
|
|
33
|
+
The main functionality of this class is to resample and co-add multiple
|
|
34
|
+
images onto one output image using the "drizzle" algorithm described in
|
|
35
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_.
|
|
36
|
+
In the simplest terms, it redistributes flux
|
|
37
|
+
from input pixels to one or more output pixels based on the chosen kernel,
|
|
38
|
+
supplied weights, and input-to-output coordinate transformations as defined
|
|
39
|
+
by the ``pixmap`` argument. For more details, see :ref:`main-user-doc`.
|
|
40
|
+
|
|
41
|
+
This class keeps track of the total exposure time of all co-added images
|
|
42
|
+
and also of which input images have contributed to an output (resampled)
|
|
43
|
+
pixel. This is accomplished via *context image*.
|
|
44
|
+
|
|
45
|
+
Main outputs of :py:meth:`add_image` can be accessed as class properties
|
|
46
|
+
``out_img``, ``out_img2``, ``out_wht``, ``out_ctx``, and ``exptime``.
|
|
47
|
+
|
|
48
|
+
.. warning::
|
|
49
|
+
Output arrays (``out_img``, ``out_img2``, ``out_wht``, and ``out_ctx``)
|
|
50
|
+
can be pre-allocated by the caller and be passed to the initializer or
|
|
51
|
+
the class initializer can allocate these arrays based on other input
|
|
52
|
+
parameters such as ``output_shape``. If caller-supplied output arrays
|
|
53
|
+
have the correct type (`numpy.float32` for ``out_img``, ``out_img2``
|
|
54
|
+
and ``out_wht``, `numpy.int32` for the ``out_ctx`` array and
|
|
55
|
+
`numpy.uint32` for the ``out_dq`` array) and if
|
|
56
|
+
``out_ctx`` is large enough not to need to be resized, these arrays
|
|
57
|
+
will be used as is and may be modified by the :py:meth:`add_image`
|
|
58
|
+
method. If not, a copy of these arrays will be made when converting
|
|
59
|
+
to the expected type (or expanding the context array).
|
|
60
|
+
|
|
61
|
+
Scaling of input image data
|
|
62
|
+
---------------------------
|
|
63
|
+
|
|
64
|
+
It is important to highlight that the drizzle algorithm computes
|
|
65
|
+
*weighted mean* of input pixel values -- see equations (4) and (5) in
|
|
66
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_.
|
|
67
|
+
Therefore, it is important that all input pixel values that contribute
|
|
68
|
+
to an output pixel are from the same distribution. In other words,
|
|
69
|
+
input pixel values from different images must be on the same footing,
|
|
70
|
+
i.e., they must be comparable and must be representative of the same
|
|
71
|
+
physical quantity.
|
|
72
|
+
|
|
73
|
+
For example, for Hubble Space Telescope data, calibrated images
|
|
74
|
+
(i.e., ``*_flt.fits``, ``*_flc.fits``) are in unit of counts, counts per
|
|
75
|
+
second, electrons, or electrons per second. To convert them to flux
|
|
76
|
+
units (e.g., erg/cm^2/s/Angstrom), one needs to multiply these images
|
|
77
|
+
by the ``PHOTFLAM``. Sometimes, images that are drizzle-combined have been
|
|
78
|
+
observed at very different times (separated by many years) and the
|
|
79
|
+
sensitivity of the instrument (represented by ``PHOTFLAM``) may have
|
|
80
|
+
changed significantly. Other times a source is observed in different chips,
|
|
81
|
+
i.e., the two chips of the Wide Field Camera. In such cases detector's
|
|
82
|
+
sensitivity (``PHOTFLAM``) may be different for the images to be combined.
|
|
83
|
+
Consequently, pixel values in these images may not be directly comparable
|
|
84
|
+
and drizzle-combining such images would result in systematic errors.
|
|
85
|
+
|
|
86
|
+
In this case, it is important to rescale images to the same flux units
|
|
87
|
+
either by multiplying by the appropriate ``PHOTFLAM`` values or some
|
|
88
|
+
other appropriate scaling factor before combining them using drizzle.
|
|
89
|
+
This can be accomplished by using the ``iscale`` parameter of
|
|
90
|
+
:py:meth:`add_image` which simply multiplies each input image by
|
|
91
|
+
``iscale``.
|
|
92
|
+
|
|
93
|
+
Also, for the case of HST images that have flux units instead of surface
|
|
94
|
+
brightness, if input images have different pixel scales, then the pixel
|
|
95
|
+
values must be rescaled by the square of the pixel scale ratio (the linear
|
|
96
|
+
dimension of a side of an output pixel as seen in the input image's
|
|
97
|
+
coordinate frame) in order to preserve flux. In this case ``iscale`` is
|
|
98
|
+
equivalent to ``s**2`` factor in equations (3) and (5) of
|
|
99
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_
|
|
100
|
+
(``s`` may be different for each input image).
|
|
101
|
+
|
|
102
|
+
Output Science Image
|
|
103
|
+
--------------------
|
|
104
|
+
|
|
105
|
+
Output science image is obtained by computing *weighted mean* of input
|
|
106
|
+
pixel values according to equations (4) and (5) in
|
|
107
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_.
|
|
108
|
+
The weights and coefficients in those equations will depend on the chosen
|
|
109
|
+
kernel, input image weights, and pixel overlaps computed from ``pixmap``.
|
|
110
|
+
|
|
111
|
+
Output Weight Image
|
|
112
|
+
-------------------
|
|
113
|
+
|
|
114
|
+
Output weight image stores the total weight of output science pixels
|
|
115
|
+
according to equation (4) in
|
|
116
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_.
|
|
117
|
+
It depends on the chosen kernel, input image weights, and pixel overlaps
|
|
118
|
+
computed from ``pixmap``.
|
|
119
|
+
|
|
120
|
+
Output Context Image
|
|
121
|
+
--------------------
|
|
122
|
+
|
|
123
|
+
Each pixel in the context image is a bit field that encodes
|
|
124
|
+
information about which input image has contributed to the corresponding
|
|
125
|
+
pixel in the resampled data array. Context image uses 32 bit integers to
|
|
126
|
+
encode this information and hence it can keep track of only 32 input images.
|
|
127
|
+
The first bit corresponds to the first input image, the second bit
|
|
128
|
+
corresponds to the second input image, and so on.
|
|
129
|
+
We call this (0-indexed) order "context ID" which is represented by
|
|
130
|
+
the ``ctx_id`` parameter/property. If the number of
|
|
131
|
+
input images exceeds 32, then it is necessary to have multiple context
|
|
132
|
+
images ("planes") to hold information about all input images, with the first
|
|
133
|
+
plane encoding which of the first 32 images contributed to the output data
|
|
134
|
+
pixel, the second plane representing next 32 input images (number 33-64),
|
|
135
|
+
etc. For this reason, context array is either a 2D array (if the total
|
|
136
|
+
number of resampled images is less than 33) of the type `numpy.int32` and
|
|
137
|
+
shape ``(ny, nx)`` or a a 3D array of shape ``(np, ny, nx)`` where ``nx``
|
|
138
|
+
and ``ny`` are dimensions of the image data. ``np`` is the number of
|
|
139
|
+
"planes" computed as ``(number of input images - 1) // 32 + 1``. If a bit at
|
|
140
|
+
position ``k`` in a pixel with coordinates ``(p, y, x)`` is 0, then input
|
|
141
|
+
image number ``32 * p + k`` (0-indexed) did not contribute to the output
|
|
142
|
+
data pixel with array coordinates ``(y, x)`` and if that bit is 1, then
|
|
143
|
+
input image number ``32 * p + k`` did contribute to the pixel ``(y, x)``
|
|
144
|
+
in the resampled image.
|
|
145
|
+
|
|
146
|
+
As an example, let's assume we have 8 input images. Then, when ``out_ctx``
|
|
147
|
+
pixel values are displayed using binary representation (and decimal in
|
|
148
|
+
parenthesis), one could see values like this::
|
|
149
|
+
|
|
150
|
+
00000001 (1) - only first input image contributed to this output pixel;
|
|
151
|
+
00000010 (2) - 2nd input image contributed;
|
|
152
|
+
00000100 (4) - 3rd input image contributed;
|
|
153
|
+
10000000 (128) - 8th input image contributed;
|
|
154
|
+
10000100 (132=128+4) - 3rd and 8th input images contributed;
|
|
155
|
+
11001101 (205=1+4+8+64+128) - input images 1, 3, 4, 7, 8 have contributed
|
|
156
|
+
to this output pixel.
|
|
157
|
+
|
|
158
|
+
In order to test if a specific input image contributed to an output pixel,
|
|
159
|
+
one needs to use bitwise operations. Using the example above, to test
|
|
160
|
+
whether input images number 4 and 5 have contributed to the output pixel
|
|
161
|
+
whose corresponding ``out_ctx`` value is 205 (11001101 in binary form) we
|
|
162
|
+
can do the following:
|
|
163
|
+
|
|
164
|
+
>>> bool(205 & (1 << (5 - 1))) # (205 & 16) = 0 (== 0 => False): did NOT contribute
|
|
165
|
+
False
|
|
166
|
+
>>> bool(205 & (1 << (4 - 1))) # (205 & 8) = 8 (!= 0 => True): did contribute
|
|
167
|
+
True
|
|
168
|
+
|
|
169
|
+
In general, to get a list of all input images that have contributed to an
|
|
170
|
+
output resampled pixel with image coordinates ``(x, y)``, and given a
|
|
171
|
+
context array ``ctx``, one can do something like this:
|
|
172
|
+
|
|
173
|
+
.. doctest-skip::
|
|
174
|
+
|
|
175
|
+
>>> import numpy as np
|
|
176
|
+
>>> np.flatnonzero([v & (1 << k) for v in ctx[:, y, x] for k in range(32)])
|
|
177
|
+
|
|
178
|
+
For convenience, this functionality was implemented in the
|
|
179
|
+
:py:func:`~drizzle.utils.decode_context` function.
|
|
180
|
+
|
|
181
|
+
Output DQ Image
|
|
182
|
+
---------------
|
|
183
|
+
|
|
184
|
+
If DQ array of input image pixels is provided via ``dq`` parameter of
|
|
185
|
+
:py:meth:`add_image`, then an output DQ array will be computed by combining
|
|
186
|
+
(using bitwise-OR) DQ bitfields of all input pixels that contribute to
|
|
187
|
+
a given output pixel.
|
|
188
|
+
|
|
189
|
+
References
|
|
190
|
+
----------
|
|
191
|
+
A full description of the drizzling algorithm can be found in
|
|
192
|
+
`Fruchter and Hook, PASP 2002 <https://doi.org/10.1086/338393>`_.
|
|
193
|
+
|
|
194
|
+
Examples
|
|
195
|
+
--------
|
|
196
|
+
.. highlight:: python
|
|
197
|
+
.. code-block:: python
|
|
198
|
+
|
|
199
|
+
# wcs1 - WCS of the input image usually with distortions (to be resampled)
|
|
200
|
+
# wcs2 - WCS of the output image without distortions
|
|
201
|
+
|
|
202
|
+
import numpy as np
|
|
203
|
+
from drizzle.resample import Drizzle
|
|
204
|
+
from drizzle.utils import calc_pixmap
|
|
205
|
+
|
|
206
|
+
# simulate some data and a pixel map:
|
|
207
|
+
data = np.ones((240, 570))
|
|
208
|
+
pixmap = calc_pixmap(wcs1, wcs2)
|
|
209
|
+
# or simulate a mapping from input image to output image frame:
|
|
210
|
+
# y, x = np.indices((240, 570), dtype=np.float64)
|
|
211
|
+
# pixmap = np.dstack([x, y])
|
|
212
|
+
|
|
213
|
+
# initialize Drizzle object
|
|
214
|
+
d = Drizzle(out_shape=(240, 570))
|
|
215
|
+
d.add_image(data, exptime=15, pixmap=pixmap)
|
|
216
|
+
|
|
217
|
+
# access outputs:
|
|
218
|
+
d.out_img
|
|
219
|
+
d.out_ctx
|
|
220
|
+
d.out_wht
|
|
221
|
+
|
|
222
|
+
"""
|
|
223
|
+
|
|
224
|
+
def __init__(
|
|
225
|
+
self,
|
|
226
|
+
kernel="square",
|
|
227
|
+
fillval=None,
|
|
228
|
+
fillval2=None,
|
|
229
|
+
out_shape=None,
|
|
230
|
+
out_img=None,
|
|
231
|
+
out_wht=None,
|
|
232
|
+
out_ctx=None,
|
|
233
|
+
out_img2=None,
|
|
234
|
+
out_dq=None,
|
|
235
|
+
exptime=0.0,
|
|
236
|
+
begin_ctx_id=0,
|
|
237
|
+
max_ctx_id=None,
|
|
238
|
+
disable_ctx=False,
|
|
239
|
+
):
|
|
240
|
+
"""
|
|
241
|
+
kernel: str, optional
|
|
242
|
+
The name of the kernel used to combine the input. The choice of
|
|
243
|
+
kernel controls the distribution of flux over the kernel. The kernel
|
|
244
|
+
names are: "square", "gaussian", "point", "turbo",
|
|
245
|
+
"lanczos2", and "lanczos3". The square kernel is the default.
|
|
246
|
+
|
|
247
|
+
.. warning::
|
|
248
|
+
The "gaussian" and "lanczos2/3" kernels **DO NOT**
|
|
249
|
+
conserve flux.
|
|
250
|
+
|
|
251
|
+
out_shape : tuple, None, optional
|
|
252
|
+
Shape (`numpy` order ``(Ny, Nx)``) of the output images (context
|
|
253
|
+
image will have a third dimension of size proportional to the number
|
|
254
|
+
of input images). This parameter is helpful when neither
|
|
255
|
+
``out_img``, ``out_wht``, nor ``out_ctx`` images are provided.
|
|
256
|
+
|
|
257
|
+
fillval: float, None, str, optional
|
|
258
|
+
The value of output pixels that did not have contributions from
|
|
259
|
+
input images' pixels. When ``fillval`` is either `None` or
|
|
260
|
+
``"INDEF"`` and ``out_img`` is provided, the values of ``out_img``
|
|
261
|
+
will not be modified. When ``fillval`` is either `None` or
|
|
262
|
+
``"INDEF"`` and ``out_img`` is **not provided**, the values of
|
|
263
|
+
``out_img`` will be initialized to `numpy.nan`. If ``fillval``
|
|
264
|
+
is a string that can be converted to a number, then the output
|
|
265
|
+
pixels with no contributions from input images will be set to this
|
|
266
|
+
``fillval`` value.
|
|
267
|
+
|
|
268
|
+
fillval2: float, None, str, optional
|
|
269
|
+
Same as ``fillval`` but applies to ``out_img2``.
|
|
270
|
+
|
|
271
|
+
out_img : 2D array of float32, None, optional
|
|
272
|
+
A 2D numpy array containing the output image produced by
|
|
273
|
+
drizzling. On the first call the array values should be set to zero.
|
|
274
|
+
On subsequent calls it will hold the intermediate results.
|
|
275
|
+
|
|
276
|
+
out_img2 : 2D array of float32, list of 2D arrays of float32, None, optional
|
|
277
|
+
A 2D numpy array containing the output image produced by
|
|
278
|
+
drizzling *with squared weights*. This is useful when performing
|
|
279
|
+
standard error propagation using variance arrays. On the first call
|
|
280
|
+
the array values should be set to zero. On subsequent calls it will
|
|
281
|
+
hold the intermediate results.
|
|
282
|
+
|
|
283
|
+
Multiple output arrays (of the same shape as that of ``out_img``)
|
|
284
|
+
can be provided as a list of 2D arrays. The number of arrays must
|
|
285
|
+
match the number of data arrays that will be resampled and co-added
|
|
286
|
+
using squared weights (see argument ``data2`` in `add_data`.)
|
|
287
|
+
|
|
288
|
+
If ``out_img2`` is None, output arrays for the squared weights
|
|
289
|
+
co-adds will be created after the first call to `add_image` based
|
|
290
|
+
on the number of ``data2`` arrays.
|
|
291
|
+
|
|
292
|
+
out_wht : 2D array of float32, None, optional
|
|
293
|
+
A 2D numpy array containing the output counts. On the first
|
|
294
|
+
call it should be set to zero. On subsequent calls it will
|
|
295
|
+
hold the intermediate results.
|
|
296
|
+
|
|
297
|
+
out_ctx : 2D or 3D array of int32, None, optional
|
|
298
|
+
A 2D or 3D numpy array holding a bitmap of which image was an input
|
|
299
|
+
for each output pixel. Should be integer zero on first call.
|
|
300
|
+
Subsequent calls hold intermediate results. This parameter is
|
|
301
|
+
ignored when ``disable_ctx`` is `True`.
|
|
302
|
+
|
|
303
|
+
out_dq : 2D array of uint32, None, optional
|
|
304
|
+
A 2D `~numpy.ndarray` containing DQ bitfields of output (resampled)
|
|
305
|
+
pixels. It will be computed by combining (using bitwise-OR)
|
|
306
|
+
DQ bitfields of input pixels that contributed to the output pixel.
|
|
307
|
+
If provided, it must be a 2D array of the same shape as
|
|
308
|
+
``out_img`` and `numpy.uint32` type (unsigned 32-bit integer type).
|
|
309
|
+
If `None`, output DQ array will be created during
|
|
310
|
+
the first call to `add_image` and will be initialized to zero.
|
|
311
|
+
|
|
312
|
+
.. warning::
|
|
313
|
+
64-bit integer type is not supported and will raise
|
|
314
|
+
an exception. Contact the authors to add support for 64-bit DQ
|
|
315
|
+
if you need it.
|
|
316
|
+
|
|
317
|
+
exptime : float, optional
|
|
318
|
+
Exposure time of previously resampled images when provided via
|
|
319
|
+
parameters ``out_img`` and ``out_wht``.
|
|
320
|
+
|
|
321
|
+
begin_ctx_id : int, optional
|
|
322
|
+
The context ID number (0-based) of the first image that will be
|
|
323
|
+
resampled (using `add_image`). Subsequent images will be assigned
|
|
324
|
+
consecutively increasing ID numbers. This parameter is ignored
|
|
325
|
+
when ``disable_ctx`` is `True`.
|
|
326
|
+
|
|
327
|
+
max_ctx_id : int, None, optional
|
|
328
|
+
The largest integer context ID that is *expected* to be used for
|
|
329
|
+
an input image. When it is a non-negative number and ``out_ctx`` is
|
|
330
|
+
`None`, it allows to pre-allocate the necessary array for the output
|
|
331
|
+
context image. If the actual number of input images that will be
|
|
332
|
+
resampled will exceed initial allocation for the context image,
|
|
333
|
+
additional context planes will be added as needed (context array
|
|
334
|
+
will "grow" in the third dimension as new input images are added.)
|
|
335
|
+
The default value of `None` is equivalent to setting ``max_ctx_id``
|
|
336
|
+
equal to ``begin_ctx_id``. This parameter is ignored either when
|
|
337
|
+
``out_ctx`` is provided or when ``disable_ctx`` is `True`.
|
|
338
|
+
|
|
339
|
+
disable_ctx : bool, optional
|
|
340
|
+
Indicates to not create a context image. If ``disable_ctx`` is set
|
|
341
|
+
to `True`, parameters ``out_ctx``, ``begin_ctx_id``, and
|
|
342
|
+
``max_ctx_id`` will be ignored.
|
|
343
|
+
|
|
344
|
+
"""
|
|
345
|
+
self._ncoadds = 0
|
|
346
|
+
self._out_img2 = None
|
|
347
|
+
self._out_dq = None
|
|
348
|
+
self._disable_ctx = disable_ctx
|
|
349
|
+
|
|
350
|
+
if disable_ctx:
|
|
351
|
+
self._ctx_id = None
|
|
352
|
+
self._max_ctx_id = None
|
|
353
|
+
else:
|
|
354
|
+
if begin_ctx_id < 0:
|
|
355
|
+
raise ValueError("Invalid context image ID")
|
|
356
|
+
self._ctx_id = begin_ctx_id # the ID of the *last* image to be resampled
|
|
357
|
+
if max_ctx_id is None:
|
|
358
|
+
max_ctx_id = begin_ctx_id
|
|
359
|
+
elif max_ctx_id < begin_ctx_id:
|
|
360
|
+
raise ValueError("'max_ctx_id' cannot be smaller than 'begin_ctx_id'.")
|
|
361
|
+
self._max_ctx_id = max_ctx_id
|
|
362
|
+
|
|
363
|
+
if exptime < 0.0:
|
|
364
|
+
raise ValueError("Exposure time must be non-negative.")
|
|
365
|
+
|
|
366
|
+
if exptime > 0.0 and out_img is None and out_ctx is None and out_wht is None:
|
|
367
|
+
raise ValueError(
|
|
368
|
+
"Exposure time must be 0.0 for the first resampling "
|
|
369
|
+
"(when no output resampled images have been provided)."
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
if exptime == 0.0 and (
|
|
373
|
+
(out_ctx is not None and np.sum(out_ctx) > 0)
|
|
374
|
+
or (out_wht is not None and np.sum(out_wht) > 0)
|
|
375
|
+
):
|
|
376
|
+
raise ValueError(
|
|
377
|
+
"Inconsistent exposure time and context and/or weight images: "
|
|
378
|
+
"Exposure time cannot be 0 when context and/or weight arrays "
|
|
379
|
+
"are non-zero."
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
self._texptime = exptime
|
|
383
|
+
|
|
384
|
+
if kernel.lower() not in SUPPORTED_DRIZZLE_KERNELS:
|
|
385
|
+
raise ValueError(f"Kernel '{kernel}' is not supported.")
|
|
386
|
+
self._kernel = kernel
|
|
387
|
+
|
|
388
|
+
self._fillval = _process_fillval(out_img, fillval)
|
|
389
|
+
self._fillval2 = _process_fillval(out_img2, fillval2)
|
|
390
|
+
|
|
391
|
+
# shapes will collect user specified 'out_shape' and shapes of
|
|
392
|
+
# out_* arrays (if provided) in order to check all shapes are the same.
|
|
393
|
+
shapes = set()
|
|
394
|
+
|
|
395
|
+
if out_img is not None:
|
|
396
|
+
out_img = np.asarray(out_img, dtype=np.float32)
|
|
397
|
+
shapes.add(out_img.shape)
|
|
398
|
+
|
|
399
|
+
if out_wht is not None:
|
|
400
|
+
out_wht = np.asarray(out_wht, dtype=np.float32)
|
|
401
|
+
shapes.add(out_wht.shape)
|
|
402
|
+
|
|
403
|
+
if out_ctx is not None:
|
|
404
|
+
out_ctx = np.asarray(out_ctx, dtype=np.int32)
|
|
405
|
+
if out_ctx.ndim == 2:
|
|
406
|
+
out_ctx = out_ctx[None, :, :]
|
|
407
|
+
elif out_ctx.ndim != 3:
|
|
408
|
+
raise ValueError("'out_ctx' must be either a 2D or 3D array.")
|
|
409
|
+
shapes.add(out_ctx.shape[1:])
|
|
410
|
+
|
|
411
|
+
if out_dq is not None:
|
|
412
|
+
t = np.min_scalar_type(out_dq)
|
|
413
|
+
if t.kind not in ["i", "u"] or t.itemsize > 4:
|
|
414
|
+
raise TypeError(
|
|
415
|
+
"'out_dq' must be of an unsigned integer type with itemsize of 4 bytes or less."
|
|
416
|
+
)
|
|
417
|
+
out_dq = np.asarray(out_dq, dtype=np.uint32)
|
|
418
|
+
shapes.add(out_dq.shape)
|
|
419
|
+
self._out_dq = out_dq
|
|
420
|
+
|
|
421
|
+
if out_shape is not None:
|
|
422
|
+
shapes.add(tuple(out_shape))
|
|
423
|
+
|
|
424
|
+
if len(shapes) == 1:
|
|
425
|
+
self._out_shape = shapes.pop()
|
|
426
|
+
self._alloc_output_arrays(
|
|
427
|
+
out_shape=self._out_shape,
|
|
428
|
+
max_ctx_id=max_ctx_id,
|
|
429
|
+
out_img=out_img,
|
|
430
|
+
out_wht=out_wht,
|
|
431
|
+
out_ctx=out_ctx,
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
elif len(shapes) > 1:
|
|
435
|
+
raise ValueError(
|
|
436
|
+
"Inconsistent data shapes specified: 'out_shape' and/or "
|
|
437
|
+
"out_img, out_img2, out_wht, out_ctx, out_dq have different "
|
|
438
|
+
"shapes."
|
|
439
|
+
)
|
|
440
|
+
else:
|
|
441
|
+
self._out_shape = None
|
|
442
|
+
self._out_img = None
|
|
443
|
+
self._out_wht = None
|
|
444
|
+
self._out_ctx = None
|
|
445
|
+
|
|
446
|
+
if out_img2 is not None:
|
|
447
|
+
if self._out_shape is not None:
|
|
448
|
+
shapes.add(self._out_shape)
|
|
449
|
+
if isinstance(out_img2, np.ndarray):
|
|
450
|
+
out_img2 = np.asarray(out_img2, dtype=np.float32)
|
|
451
|
+
shapes.add(out_img2.shape)
|
|
452
|
+
else:
|
|
453
|
+
for img in out_img2:
|
|
454
|
+
if img is not None:
|
|
455
|
+
shapes.add(np.shape(img))
|
|
456
|
+
if len(shapes) > 1:
|
|
457
|
+
raise ValueError(
|
|
458
|
+
"Inconsistent data shapes specified: 'out_shape' "
|
|
459
|
+
"and/or out_img, out_img2, out_wht, out_ctx have "
|
|
460
|
+
"different shapes."
|
|
461
|
+
)
|
|
462
|
+
self._output_shapes = shapes
|
|
463
|
+
self._alloc_output_arrays2_init(out_img2=out_img2)
|
|
464
|
+
|
|
465
|
+
@property
|
|
466
|
+
def fillval(self):
|
|
467
|
+
"""Fill value for output pixels without contributions from input images."""
|
|
468
|
+
return self._fillval
|
|
469
|
+
|
|
470
|
+
@property
|
|
471
|
+
def fillval2(self):
|
|
472
|
+
"""Fill value for output pixels in ``out_img2`` without contributions
|
|
473
|
+
from input images.
|
|
474
|
+
|
|
475
|
+
"""
|
|
476
|
+
return self._fillval2
|
|
477
|
+
|
|
478
|
+
@property
|
|
479
|
+
def kernel(self):
|
|
480
|
+
"""Resampling kernel."""
|
|
481
|
+
return self._kernel
|
|
482
|
+
|
|
483
|
+
@property
|
|
484
|
+
def ctx_id(self):
|
|
485
|
+
"""Context image "ID" (0-based ) of the next image to be resampled."""
|
|
486
|
+
return self._ctx_id
|
|
487
|
+
|
|
488
|
+
@property
|
|
489
|
+
def out_img(self):
|
|
490
|
+
"""Output resampled image."""
|
|
491
|
+
return self._out_img
|
|
492
|
+
|
|
493
|
+
@property
|
|
494
|
+
def out_wht(self):
|
|
495
|
+
"""Output weight image."""
|
|
496
|
+
return self._out_wht
|
|
497
|
+
|
|
498
|
+
@property
|
|
499
|
+
def out_ctx(self):
|
|
500
|
+
"""Output "context" image."""
|
|
501
|
+
return self._out_ctx
|
|
502
|
+
|
|
503
|
+
@property
|
|
504
|
+
def out_img2(self):
|
|
505
|
+
"""Output resampled image(s) obtained with squared weights.
|
|
506
|
+
It is always a list of one or more 2D arrays.
|
|
507
|
+
|
|
508
|
+
"""
|
|
509
|
+
return self._out_img2
|
|
510
|
+
|
|
511
|
+
@property
|
|
512
|
+
def out_dq(self):
|
|
513
|
+
"""Output DQ image computed by OR-combining DQ bitfields of input
|
|
514
|
+
images' pixels that have contributed to a given output pixel.
|
|
515
|
+
|
|
516
|
+
"""
|
|
517
|
+
return self._out_dq
|
|
518
|
+
|
|
519
|
+
@property
|
|
520
|
+
def total_exptime(self):
|
|
521
|
+
"""Total exposure time of all resampled images."""
|
|
522
|
+
return self._texptime
|
|
523
|
+
|
|
524
|
+
def _alloc_output_arrays(self, out_shape, max_ctx_id, out_img, out_wht, out_ctx):
|
|
525
|
+
# allocate arrays as needed:
|
|
526
|
+
if out_wht is None:
|
|
527
|
+
self._out_wht = np.zeros(out_shape, dtype=np.float32)
|
|
528
|
+
else:
|
|
529
|
+
self._out_wht = out_wht
|
|
530
|
+
|
|
531
|
+
if self._disable_ctx:
|
|
532
|
+
self._out_ctx = None
|
|
533
|
+
else:
|
|
534
|
+
if out_ctx is None:
|
|
535
|
+
n_ctx_planes = max_ctx_id // CTX_PLANE_BITS + 1
|
|
536
|
+
ctx_shape = (n_ctx_planes,) + out_shape
|
|
537
|
+
self._out_ctx = np.zeros(ctx_shape, dtype=np.int32)
|
|
538
|
+
else:
|
|
539
|
+
self._out_ctx = out_ctx
|
|
540
|
+
|
|
541
|
+
if not (out_wht is None and out_ctx is None):
|
|
542
|
+
# check that input data make sense: weight of pixels with
|
|
543
|
+
# non-zero context values must be different from zero:
|
|
544
|
+
if np.any(np.bitwise_xor(self._out_wht > 0.0, np.sum(self._out_ctx, axis=0) > 0)):
|
|
545
|
+
raise ValueError(
|
|
546
|
+
"Inconsistent values of supplied 'out_wht' and "
|
|
547
|
+
"'out_ctx' arrays. Pixels with non-zero context "
|
|
548
|
+
"values must have positive weights and vice-versa."
|
|
549
|
+
)
|
|
550
|
+
|
|
551
|
+
if out_img is None:
|
|
552
|
+
if self._fillval.upper() in ["INDEF", "NAN"]:
|
|
553
|
+
fillval = np.nan
|
|
554
|
+
else:
|
|
555
|
+
fillval = float(self._fillval)
|
|
556
|
+
self._out_img = np.full(out_shape, fillval, dtype=np.float32)
|
|
557
|
+
else:
|
|
558
|
+
self._out_img = out_img
|
|
559
|
+
|
|
560
|
+
def _alloc_output_arrays2_init(self, out_img2=None):
|
|
561
|
+
if hasattr(self, "_out_img2") and self._out_img2 is not None:
|
|
562
|
+
raise AssertionError(
|
|
563
|
+
"It is expected that _alloc_output_arrays2_init is called "
|
|
564
|
+
"before Drizzle._out_img2 is set."
|
|
565
|
+
)
|
|
566
|
+
|
|
567
|
+
if out_img2 is None:
|
|
568
|
+
return
|
|
569
|
+
|
|
570
|
+
if isinstance(out_img2, np.ndarray):
|
|
571
|
+
out_img2 = [out_img2]
|
|
572
|
+
|
|
573
|
+
self._out_img2 = []
|
|
574
|
+
|
|
575
|
+
if isinstance(self._fillval2, str) and self._fillval2.strip().upper() == "INDEF":
|
|
576
|
+
fv = np.nan
|
|
577
|
+
else:
|
|
578
|
+
fv = np.float32(self._fillval2)
|
|
579
|
+
|
|
580
|
+
for i2 in out_img2:
|
|
581
|
+
if i2 is None:
|
|
582
|
+
if self._out_shape is None:
|
|
583
|
+
if len(self._output_shapes) == 1:
|
|
584
|
+
shape = next(iter(self._output_shapes))
|
|
585
|
+
else:
|
|
586
|
+
self._out_img2.append(None)
|
|
587
|
+
continue
|
|
588
|
+
else:
|
|
589
|
+
shape = self._out_shape
|
|
590
|
+
arr = np.full(shape, fill_value=fv, dtype=np.float32)
|
|
591
|
+
else:
|
|
592
|
+
arr = np.asarray(i2, dtype=np.float32)
|
|
593
|
+
self._out_img2.append(arr)
|
|
594
|
+
del arr
|
|
595
|
+
|
|
596
|
+
def _alloc_output_arrays2_add(self, ninputs2=None):
|
|
597
|
+
if isinstance(self._fillval2, str) and self._fillval2.strip().upper() == "INDEF":
|
|
598
|
+
fv = np.nan
|
|
599
|
+
else:
|
|
600
|
+
fv = np.float32(self._fillval2)
|
|
601
|
+
if self._out_img2 is None:
|
|
602
|
+
if ninputs2 is None or ninputs2 < 1:
|
|
603
|
+
# nothing to do
|
|
604
|
+
return
|
|
605
|
+
if self._ncoadds > 0:
|
|
606
|
+
raise ValueError(
|
|
607
|
+
"Mismatch between the number of 'out_img2' images and the number of inputs."
|
|
608
|
+
)
|
|
609
|
+
self._out_img2 = [
|
|
610
|
+
np.full(self._out_shape, fill_value=fv, dtype=np.float32) for _ in range(ninputs2)
|
|
611
|
+
]
|
|
612
|
+
|
|
613
|
+
else:
|
|
614
|
+
nimg2 = len(self._out_img2)
|
|
615
|
+
|
|
616
|
+
# replace None values with arrays of _out_shape:
|
|
617
|
+
for k, img in enumerate(self._out_img2):
|
|
618
|
+
if img is None:
|
|
619
|
+
self._out_img2[k] = np.full(self._out_shape, fill_value=fv, dtype=np.float32)
|
|
620
|
+
|
|
621
|
+
if (ninputs2 is not None and ninputs2 != nimg2) or (ninputs2 is None and nimg2 > 0):
|
|
622
|
+
raise ValueError(
|
|
623
|
+
"Mismatch between the number of 'out_img2' images "
|
|
624
|
+
"previously set and the number of inputs."
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
def _increment_ctx_id(self):
|
|
628
|
+
"""
|
|
629
|
+
Returns a pair of the *current* plane number and bit number in that
|
|
630
|
+
plane and increments context image ID
|
|
631
|
+
(after computing the return value).
|
|
632
|
+
"""
|
|
633
|
+
if self._disable_ctx:
|
|
634
|
+
return None, 0
|
|
635
|
+
|
|
636
|
+
self._plane_no = self._ctx_id // CTX_PLANE_BITS
|
|
637
|
+
depth = self._out_ctx.shape[0]
|
|
638
|
+
|
|
639
|
+
if self._plane_no >= depth:
|
|
640
|
+
# Add a new plane to the context image if planeid overflows
|
|
641
|
+
plane = np.zeros((1,) + self._out_shape, np.int32)
|
|
642
|
+
self._out_ctx = np.append(self._out_ctx, plane, axis=0)
|
|
643
|
+
|
|
644
|
+
plane_info = (self._plane_no, self._ctx_id % CTX_PLANE_BITS)
|
|
645
|
+
# increment ID for the *next* image to be added:
|
|
646
|
+
self._ctx_id += 1
|
|
647
|
+
|
|
648
|
+
return plane_info
|
|
649
|
+
|
|
650
|
+
def add_image(
|
|
651
|
+
self,
|
|
652
|
+
data,
|
|
653
|
+
exptime,
|
|
654
|
+
pixmap,
|
|
655
|
+
data2=None,
|
|
656
|
+
dq=None,
|
|
657
|
+
scale=_DEPRECATED_ARG,
|
|
658
|
+
iscale=1.0,
|
|
659
|
+
pixel_scale_ratio=1.0,
|
|
660
|
+
weight_map=None,
|
|
661
|
+
wht_scale=1.0,
|
|
662
|
+
pixfrac=1.0,
|
|
663
|
+
in_units="cps",
|
|
664
|
+
xmin=None,
|
|
665
|
+
xmax=None,
|
|
666
|
+
ymin=None,
|
|
667
|
+
ymax=None,
|
|
668
|
+
):
|
|
669
|
+
"""
|
|
670
|
+
Resample and add an image to the cumulative output image. Also, update
|
|
671
|
+
output total weight image and context images.
|
|
672
|
+
|
|
673
|
+
Parameters
|
|
674
|
+
----------
|
|
675
|
+
data : 2D numpy.ndarray
|
|
676
|
+
A 2D numpy array containing the input image to be drizzled.
|
|
677
|
+
|
|
678
|
+
exptime : float
|
|
679
|
+
The exposure time of the input image, a positive number. The
|
|
680
|
+
exposure time is used to scale the image if the units are counts.
|
|
681
|
+
|
|
682
|
+
pixmap : 3D array
|
|
683
|
+
A mapping from input image (``data``) coordinates to resampled
|
|
684
|
+
(``out_img``) coordinates. ``pixmap`` must be an array of shape
|
|
685
|
+
``(Ny, Nx, 2)`` where ``(Ny, Nx)`` is the shape of the input image.
|
|
686
|
+
``pixmap[..., 0]`` forms a 2D array of X-coordinates of input
|
|
687
|
+
pixels in the output frame and ``pixmap[..., 1]`` forms a 2D array of
|
|
688
|
+
Y-coordinates of input pixels in the output coordinate frame.
|
|
689
|
+
|
|
690
|
+
data2 : 2D array of float32, list of 2D arrays of float32 or None, None, optional
|
|
691
|
+
A 2D numpy array (or a list of 2D arrays) with image data to be
|
|
692
|
+
resampled and co-added using squared weights. The resampled output
|
|
693
|
+
image can be accessed via ``out_img2`` property of the `Drizzle`
|
|
694
|
+
object. This is useful for performing standard error propagation
|
|
695
|
+
using variance arrays.
|
|
696
|
+
|
|
697
|
+
Multiple data arrays (of the same shape as that of ``data``)
|
|
698
|
+
can be provided as a list of 2D arrays. The number of arrays must
|
|
699
|
+
match the number of output data arrays provided during
|
|
700
|
+
initialization via argument ``out_img2``. If an item in the list
|
|
701
|
+
is `None`, that item will not be resampled to the corresponding
|
|
702
|
+
``out_img2`` element.
|
|
703
|
+
|
|
704
|
+
.. note::
|
|
705
|
+
It is assumed that data in ``data2`` have squared units of
|
|
706
|
+
``data``. Therefore, when ``in_units`` are "counts",
|
|
707
|
+
``data2`` arrays will be rescaled by ``exptime**2`` to convert
|
|
708
|
+
to rate units before resampling.
|
|
709
|
+
|
|
710
|
+
dq : 2D array, None, optional
|
|
711
|
+
A 2D numpy array of type `numpy.uint32` (unsigned 32-bit integer
|
|
712
|
+
type) containing DQ bitfields of input pixels. It must
|
|
713
|
+
have the same shape as ``data``. If provided, output DQ array
|
|
714
|
+
(accessible via ``out_dq`` property) will be computed by combining
|
|
715
|
+
(using bitwise-OR) DQ bitfields of input pixels that contributed to
|
|
716
|
+
the output pixel. If `None`, DQ array of the output image will
|
|
717
|
+
not be computed.
|
|
718
|
+
|
|
719
|
+
.. warning::
|
|
720
|
+
64-bit integer type is not supported and will raise
|
|
721
|
+
an exception. Contact the authors to add support for 64-bit DQ
|
|
722
|
+
if you need it.
|
|
723
|
+
|
|
724
|
+
scale : float, optional
|
|
725
|
+
Deprecated: use ``iscale`` and ``pixel_scale_ratio`` instead.
|
|
726
|
+
It is a factor used both to rescale input image data
|
|
727
|
+
by ``scale**2`` AND to compute the correct kernel size for some
|
|
728
|
+
kernels ("turbo", "gaussian", and "lanczos"). It is recommended
|
|
729
|
+
``scale`` be set to pixel scale ratio: the linear dimension of
|
|
730
|
+
a side of an output pixel relative to the size of an input pixel
|
|
731
|
+
(or size of an output pixel in the input image's coordinate system).
|
|
732
|
+
|
|
733
|
+
iscale : float, optional
|
|
734
|
+
It is a multiplicative factor used to rescale input image data
|
|
735
|
+
by ``iscale`` value. ``data2`` images will be rescaled by
|
|
736
|
+
``iscale**2``. It may make sense to rescale input image (``data``)
|
|
737
|
+
by squared pixel scale ratio (the linear dimension of a side of an
|
|
738
|
+
output pixel as seen in the input image's coordinate frame)
|
|
739
|
+
depending on the units of the input image, i.e., counts vs
|
|
740
|
+
brightness. For more details see section
|
|
741
|
+
"Scaling of input image data" in :py:class:`Drizzle`.
|
|
742
|
+
|
|
743
|
+
pixel_scale_ratio : float, None, optional
|
|
744
|
+
It is a factor used to compute the correct kernel size in output
|
|
745
|
+
image's coordinate system for some of the kernels
|
|
746
|
+
("turbo", "gaussian", and "lanczos") from their nominal
|
|
747
|
+
sizes in input image pixels. For example, for the "lanczos3"
|
|
748
|
+
kernel, the nominal size is 3 input pixels. It is recommended that
|
|
749
|
+
``pixel_scale_ratio`` be set to pixel scale ratio: the linear dimension of
|
|
750
|
+
output pixel relative to the size of an input pixel. When
|
|
751
|
+
``pixel_scale_ratio`` is `None`, it will be estimated from ``pixmap`` but this
|
|
752
|
+
can impose a performance penalty.
|
|
753
|
+
|
|
754
|
+
weight_map : 2D array, None, optional
|
|
755
|
+
A 2D numpy array containing the pixel by pixel weighting.
|
|
756
|
+
Must have the same dimensions as ``data``.
|
|
757
|
+
|
|
758
|
+
When ``weight_map`` is `None`, the weight of input data pixels will
|
|
759
|
+
be assumed to be 1.
|
|
760
|
+
|
|
761
|
+
wht_scale : float
|
|
762
|
+
A scaling factor applied to the pixel by pixel weighting.
|
|
763
|
+
|
|
764
|
+
pixfrac : float, optional
|
|
765
|
+
The fraction of a pixel that the pixel flux is confined to. The
|
|
766
|
+
default value of 1 has the pixel flux evenly spread across the image.
|
|
767
|
+
A value of 0.5 confines it to half a pixel in the linear dimension,
|
|
768
|
+
so the flux is confined to a quarter of the pixel area when the square
|
|
769
|
+
kernel is used.
|
|
770
|
+
|
|
771
|
+
in_units : str
|
|
772
|
+
The units of the input image. The units can either be "counts"
|
|
773
|
+
or "cps" (counts per second.)
|
|
774
|
+
|
|
775
|
+
xmin : float, optional
|
|
776
|
+
This and the following three parameters set a bounding rectangle
|
|
777
|
+
on the input image. Only pixels on the input image inside this
|
|
778
|
+
rectangle will have their flux added to the output image. Xmin
|
|
779
|
+
sets the minimum value of the x dimension. The x dimension is the
|
|
780
|
+
dimension that varies quickest on the image. If the value is zero,
|
|
781
|
+
no minimum will be set in the x dimension. All four parameters are
|
|
782
|
+
zero based, counting starts at zero.
|
|
783
|
+
|
|
784
|
+
xmax : float, optional
|
|
785
|
+
Sets the maximum value of the x dimension on the bounding box
|
|
786
|
+
of the input image. If the value is zero, no maximum will
|
|
787
|
+
be set in the x dimension, the full x dimension of the output
|
|
788
|
+
image is the bounding box.
|
|
789
|
+
|
|
790
|
+
ymin : float, optional
|
|
791
|
+
Sets the minimum value in the y dimension on the bounding box. The
|
|
792
|
+
y dimension varies less rapidly than the x and represents the line
|
|
793
|
+
index on the input image. If the value is zero, no minimum will be
|
|
794
|
+
set in the y dimension.
|
|
795
|
+
|
|
796
|
+
ymax : float, optional
|
|
797
|
+
Sets the maximum value in the y dimension. If the value is zero, no
|
|
798
|
+
maximum will be set in the y dimension, the full x dimension
|
|
799
|
+
of the output image is the bounding box.
|
|
800
|
+
|
|
801
|
+
Returns
|
|
802
|
+
-------
|
|
803
|
+
nskip : float
|
|
804
|
+
The number of lines from the box defined by
|
|
805
|
+
``((xmin, xmax), (ymin, ymax))`` in the input image that were
|
|
806
|
+
ignored and did not contribute to the output image.
|
|
807
|
+
|
|
808
|
+
nmiss : float
|
|
809
|
+
The number of pixels from the box defined by
|
|
810
|
+
``((xmin, xmax), (ymin, ymax))`` in the input image that were
|
|
811
|
+
ignored and did not contribute to the output image.
|
|
812
|
+
|
|
813
|
+
"""
|
|
814
|
+
if scale is not _DEPRECATED_ARG:
|
|
815
|
+
warnings.warn(
|
|
816
|
+
"Argument 'scale' has been deprecated since version 3.0 and "
|
|
817
|
+
"it will be removed in a future release. "
|
|
818
|
+
"Use 'iscale' and 'pixel_scale_ratio' instead and set iscale=pixel_scale_ratio**2 "
|
|
819
|
+
"to achieve the same effect as with 'scale'.",
|
|
820
|
+
DeprecationWarning,
|
|
821
|
+
)
|
|
822
|
+
iscale = scale * scale
|
|
823
|
+
pixel_scale_ratio = scale
|
|
824
|
+
|
|
825
|
+
# this enables initializer to not need output image shape at all and
|
|
826
|
+
# set output image shape based on output coordinates from the pixmap.
|
|
827
|
+
#
|
|
828
|
+
if self._out_shape is None:
|
|
829
|
+
nshapes = len(self._output_shapes)
|
|
830
|
+
if nshapes == 0:
|
|
831
|
+
pmap_xmin = int(np.floor(np.nanmin(pixmap[:, :, 0])))
|
|
832
|
+
pmap_xmax = int(np.ceil(np.nanmax(pixmap[:, :, 0])))
|
|
833
|
+
pmap_ymin = int(np.floor(np.nanmin(pixmap[:, :, 1])))
|
|
834
|
+
pmap_ymax = int(np.ceil(np.nanmax(pixmap[:, :, 1])))
|
|
835
|
+
pixmap = pixmap.copy()
|
|
836
|
+
pixmap[:, :, 0] -= pmap_xmin
|
|
837
|
+
pixmap[:, :, 1] -= pmap_ymin
|
|
838
|
+
self._out_shape = (pmap_xmax - pmap_xmin + 1, pmap_ymax - pmap_ymin + 1)
|
|
839
|
+
elif nshapes == 1:
|
|
840
|
+
self._out_shape = next(iter(self._output_shapes))
|
|
841
|
+
else:
|
|
842
|
+
raise ValueError(
|
|
843
|
+
"Inconsistent data shapes: 'out_shape' and/or "
|
|
844
|
+
"out_img, out_img2, out_wht, out_ctx have different shapes."
|
|
845
|
+
) # pragma: no cover
|
|
846
|
+
|
|
847
|
+
self._alloc_output_arrays(
|
|
848
|
+
out_shape=self._out_shape,
|
|
849
|
+
max_ctx_id=self._max_ctx_id,
|
|
850
|
+
out_img=None,
|
|
851
|
+
out_wht=None,
|
|
852
|
+
out_ctx=None,
|
|
853
|
+
)
|
|
854
|
+
|
|
855
|
+
if data2 is None:
|
|
856
|
+
ninputs2 = None
|
|
857
|
+
else:
|
|
858
|
+
if isinstance(data2, np.ndarray):
|
|
859
|
+
ninputs2 = 1
|
|
860
|
+
if data2.shape != data.shape:
|
|
861
|
+
raise ValueError("'data2' shape is not consistent with 'data' shape.")
|
|
862
|
+
else:
|
|
863
|
+
shapes2 = set()
|
|
864
|
+
ninputs2 = len(data2)
|
|
865
|
+
data2 = list(data2)
|
|
866
|
+
for k, d in enumerate(data2):
|
|
867
|
+
if d is None or d.size == 0:
|
|
868
|
+
data2[k] = None
|
|
869
|
+
else:
|
|
870
|
+
shapes2.add(d.shape)
|
|
871
|
+
|
|
872
|
+
if (len(shapes2) == 1 and shapes2.pop() != data.shape) or len(shapes2) > 1:
|
|
873
|
+
raise ValueError("'data2' shape(s) is not consistent with 'data' shape.")
|
|
874
|
+
|
|
875
|
+
self._alloc_output_arrays2_add(ninputs2=ninputs2)
|
|
876
|
+
|
|
877
|
+
plane_no, id_in_plane = self._increment_ctx_id()
|
|
878
|
+
|
|
879
|
+
if exptime <= 0.0:
|
|
880
|
+
raise ValueError("'exptime' *must* be a strictly positive number.")
|
|
881
|
+
|
|
882
|
+
# Ensure that the fillval parameter gets properly interpreted
|
|
883
|
+
# for use with tdriz
|
|
884
|
+
if in_units == "cps":
|
|
885
|
+
expscale = 1.0
|
|
886
|
+
else:
|
|
887
|
+
expscale = exptime
|
|
888
|
+
|
|
889
|
+
self._texptime += exptime
|
|
890
|
+
|
|
891
|
+
data = np.asarray(data, dtype=np.float32)
|
|
892
|
+
pixmap = np.asarray(pixmap, dtype=np.float64)
|
|
893
|
+
in_ymax, in_xmax = data.shape
|
|
894
|
+
|
|
895
|
+
if pixmap.shape[:2] != data.shape:
|
|
896
|
+
raise ValueError("'pixmap' shape is not consistent with 'data' shape.")
|
|
897
|
+
|
|
898
|
+
if xmin is None or xmin < 0:
|
|
899
|
+
xmin = 0
|
|
900
|
+
|
|
901
|
+
if ymin is None or ymin < 0:
|
|
902
|
+
ymin = 0
|
|
903
|
+
|
|
904
|
+
if xmax is None or xmax > in_xmax - 1:
|
|
905
|
+
xmax = in_xmax - 1
|
|
906
|
+
|
|
907
|
+
if ymax is None or ymax > in_ymax - 1:
|
|
908
|
+
ymax = in_ymax - 1
|
|
909
|
+
|
|
910
|
+
if weight_map is not None:
|
|
911
|
+
weight_map = np.asarray(weight_map, dtype=np.float32)
|
|
912
|
+
if weight_map.shape != data.shape:
|
|
913
|
+
raise ValueError("'weight_map' shape is not consistent with 'data' shape.")
|
|
914
|
+
else: # TODO: this should not be needed after C code modifications
|
|
915
|
+
weight_map = np.ones_like(data)
|
|
916
|
+
|
|
917
|
+
pixmap = np.asarray(pixmap, dtype=np.float64)
|
|
918
|
+
|
|
919
|
+
if self._disable_ctx:
|
|
920
|
+
ctx_plane = None
|
|
921
|
+
else:
|
|
922
|
+
if self._out_ctx.ndim == 2:
|
|
923
|
+
raise AssertionError("Context image is expected to be 3D")
|
|
924
|
+
ctx_plane = self._out_ctx[plane_no]
|
|
925
|
+
|
|
926
|
+
if dq is not None:
|
|
927
|
+
t = np.min_scalar_type(dq)
|
|
928
|
+
if t.kind not in ["i", "u"] or t.itemsize > 4:
|
|
929
|
+
raise TypeError(
|
|
930
|
+
"'dq' must be of an unsigned integer type with itemsize of 4 bytes or less."
|
|
931
|
+
)
|
|
932
|
+
dq = np.asarray(dq, dtype=np.uint32)
|
|
933
|
+
if dq.shape != data.shape:
|
|
934
|
+
raise ValueError("'dq' shape is not consistent with 'data' shape.")
|
|
935
|
+
if self._out_dq is None:
|
|
936
|
+
self._out_dq = np.zeros(self._out_shape, dtype=np.uint32)
|
|
937
|
+
|
|
938
|
+
# TODO: probably tdriz should be modified to not return version.
|
|
939
|
+
# we should not have git, Python, C, ... versions
|
|
940
|
+
|
|
941
|
+
# TODO: While drizzle code in cdrizzlebox.c supports weight_map=None,
|
|
942
|
+
# cdrizzleapi.c does not. It should be modified to support this
|
|
943
|
+
# for performance reasons.
|
|
944
|
+
|
|
945
|
+
_vers, nmiss, nskip = cdrizzle.tdriz(
|
|
946
|
+
input=data,
|
|
947
|
+
weights=weight_map,
|
|
948
|
+
pixmap=pixmap,
|
|
949
|
+
output=self._out_img,
|
|
950
|
+
counts=self._out_wht,
|
|
951
|
+
context=ctx_plane,
|
|
952
|
+
input2=data2,
|
|
953
|
+
output2=self._out_img2,
|
|
954
|
+
dq=dq,
|
|
955
|
+
outdq=self._out_dq,
|
|
956
|
+
uniqid=id_in_plane + 1,
|
|
957
|
+
xmin=xmin,
|
|
958
|
+
xmax=xmax,
|
|
959
|
+
ymin=ymin,
|
|
960
|
+
ymax=ymax,
|
|
961
|
+
iscale=iscale, # scales image intensity. usually equal to 1 or
|
|
962
|
+
# (pixel scale ratio)**2
|
|
963
|
+
pscale_ratio=pixel_scale_ratio, # scales kernel size. usually equal to pixel scale ratio
|
|
964
|
+
pixfrac=pixfrac,
|
|
965
|
+
kernel=self._kernel,
|
|
966
|
+
in_units=in_units,
|
|
967
|
+
expscale=expscale,
|
|
968
|
+
wtscale=wht_scale,
|
|
969
|
+
fillstr=self._fillval,
|
|
970
|
+
fillstr2=self._fillval2,
|
|
971
|
+
)
|
|
972
|
+
self._cversion = _vers # TODO: probably not needed
|
|
973
|
+
self._ncoadds += 1
|
|
974
|
+
|
|
975
|
+
return nmiss, nskip
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
def blot_image(
|
|
979
|
+
data,
|
|
980
|
+
pixmap,
|
|
981
|
+
pix_ratio=_DEPRECATED_ARG,
|
|
982
|
+
exptime=_DEPRECATED_ARG,
|
|
983
|
+
output_pixel_shape=_DEPRECATED_ARG,
|
|
984
|
+
out_img=None,
|
|
985
|
+
fillval=0.0,
|
|
986
|
+
iscale=1.0,
|
|
987
|
+
interp="poly5",
|
|
988
|
+
sinscl=1.0,
|
|
989
|
+
):
|
|
990
|
+
"""
|
|
991
|
+
Resample the ``data`` input image onto an output grid defined by
|
|
992
|
+
the ``pixmap`` array. ``blot_image`` performs resampling using one of
|
|
993
|
+
the several interpolation algorithms and, unlike the "drizzle" algorithm
|
|
994
|
+
with 'square', 'turbo', and 'point' kernels, this resampling is not
|
|
995
|
+
flux-conserving.
|
|
996
|
+
|
|
997
|
+
This method works best for with well sampled images and thus it is
|
|
998
|
+
typically used to resample the output of :py:class:`Drizzle` back to the
|
|
999
|
+
coordinate grids of input images of :py:meth:`Drizzle.add_image`.
|
|
1000
|
+
The output of :py:class:`Drizzle` are usually well sampled images especially
|
|
1001
|
+
if it was created from a set of dithered images.
|
|
1002
|
+
|
|
1003
|
+
Parameters
|
|
1004
|
+
----------
|
|
1005
|
+
data : 2D array
|
|
1006
|
+
Input numpy array of the source image in units of 'cps'.
|
|
1007
|
+
|
|
1008
|
+
pixmap : 3D array
|
|
1009
|
+
A mapping from input image (``data``) coordinates to resampled
|
|
1010
|
+
(``out_img``) coordinates. ``pixmap`` must be an array of shape
|
|
1011
|
+
``(Ny, Nx, 2)`` where ``(Ny, Nx)`` is the shape of the input image.
|
|
1012
|
+
``pixmap[..., 0]`` forms a 2D array of X-coordinates of input
|
|
1013
|
+
pixels in the output frame and ``pixmap[..., 1]`` forms a 2D array of
|
|
1014
|
+
Y-coordinates of input pixels in the output coordinate frame.
|
|
1015
|
+
|
|
1016
|
+
pix_ratio : float
|
|
1017
|
+
Ratio of the input image pixel scale to the output image pixel scale as
|
|
1018
|
+
used in the ``drizzle`` context: input is a distorted image that was
|
|
1019
|
+
"drizzled" onto the output image. That is, it is the ratio of the
|
|
1020
|
+
scale of the pixels in the input ``data`` argument to the scale of
|
|
1021
|
+
pixels of the image array returned by ``blot_image()``.
|
|
1022
|
+
**It is used to scale the input image intensities to account
|
|
1023
|
+
for the change in pixel area.**
|
|
1024
|
+
|
|
1025
|
+
.. warning::
|
|
1026
|
+
Deprecated since version 3.0 and will be removed in a future
|
|
1027
|
+
release. Use ``iscale`` instead and set
|
|
1028
|
+
``iscale=1.0 / pix_ratio**2`` to achieve the same effect as with
|
|
1029
|
+
``pix_ratio``.
|
|
1030
|
+
|
|
1031
|
+
exptime : float
|
|
1032
|
+
The exposure time of the input image. If provided it is used to scale
|
|
1033
|
+
the output image values.
|
|
1034
|
+
|
|
1035
|
+
.. warning::
|
|
1036
|
+
Deprecated since version 3.0 and will be removed in a future
|
|
1037
|
+
release. Use ``iscale`` instead and set
|
|
1038
|
+
``iscale=exptime`` or ``exptime / pix_ratio**2`` to achieve the
|
|
1039
|
+
same effect as with ``exptime`` (and ``pix_ratio``).
|
|
1040
|
+
|
|
1041
|
+
output_pixel_shape : tuple of int
|
|
1042
|
+
A tuple of two integer numbers indicating the dimensions of the output
|
|
1043
|
+
image ``(Nx, Ny)``.
|
|
1044
|
+
|
|
1045
|
+
.. warning::
|
|
1046
|
+
Deprecated since version 3.0 and will be removed in a future
|
|
1047
|
+
release. It is not needed since the output image shape can be
|
|
1048
|
+
inferred from ``pixmap``.
|
|
1049
|
+
|
|
1050
|
+
output_image : 2D array of float32, None, optional
|
|
1051
|
+
A 2D numpy array to hold the output image produced by resampling
|
|
1052
|
+
the input image (``data``). If `None`, a new array will be allocated.
|
|
1053
|
+
|
|
1054
|
+
fillval: float, optional
|
|
1055
|
+
The value of output pixels that did not have contributions from
|
|
1056
|
+
input image' pixels.
|
|
1057
|
+
|
|
1058
|
+
iscale : float, optional
|
|
1059
|
+
A multiplicative factor used to rescale output image data by
|
|
1060
|
+
``iscale``. Depending on specific needs, it may make sense to rescale
|
|
1061
|
+
output image by inverse of squared pixel scale ratio (the linear
|
|
1062
|
+
dimension of a side of a resampled/drizzled (input) pixel as seen in
|
|
1063
|
+
the distorted (output) image's coordinate frame) depending on the units
|
|
1064
|
+
of the input image, i.e., counts (flux) vs surface brightness.
|
|
1065
|
+
For more details see section "Scaling of input image data" in
|
|
1066
|
+
:py:class:`Drizzle`.
|
|
1067
|
+
|
|
1068
|
+
interp : str, optional
|
|
1069
|
+
The type of interpolation used in the resampling. The
|
|
1070
|
+
possible values are:
|
|
1071
|
+
|
|
1072
|
+
- "nearest" (nearest neighbor interpolation);
|
|
1073
|
+
- "linear" (bilinear interpolation);
|
|
1074
|
+
- "poly3" (cubic polynomial interpolation);
|
|
1075
|
+
- "poly5" (quintic polynomial interpolation);
|
|
1076
|
+
- "sinc" (sinc interpolation);
|
|
1077
|
+
- "lan3" (3rd order Lanczos interpolation); and
|
|
1078
|
+
- "lan5" (5th order Lanczos interpolation).
|
|
1079
|
+
|
|
1080
|
+
.. warning::
|
|
1081
|
+
The "sinc" interpolation is currently investigated for possible
|
|
1082
|
+
issues, see https://github.com/spacetelescope/drizzle/issues/209,
|
|
1083
|
+
and its use is not recommended. Furthermore, sinc interpolation may
|
|
1084
|
+
be removed in future releases.
|
|
1085
|
+
|
|
1086
|
+
sincscl : float, optional
|
|
1087
|
+
The scaling factor for "sinc" interpolation.
|
|
1088
|
+
|
|
1089
|
+
Returns
|
|
1090
|
+
-------
|
|
1091
|
+
out_img : 2D numpy.ndarray
|
|
1092
|
+
A 2D numpy array containing the resampled image data.
|
|
1093
|
+
|
|
1094
|
+
"""
|
|
1095
|
+
if pix_ratio is not _DEPRECATED_ARG:
|
|
1096
|
+
warnings.warn(
|
|
1097
|
+
"Argument 'pix_ratio' has been deprecated since version 3.0 and "
|
|
1098
|
+
"it will be removed in a future release. "
|
|
1099
|
+
"Use 'iscale' instead and set iscale=1.0 / pix_ratio**2 "
|
|
1100
|
+
"to achieve the same effect as with 'pix_ratio'.",
|
|
1101
|
+
DeprecationWarning,
|
|
1102
|
+
)
|
|
1103
|
+
iscale /= pix_ratio * pix_ratio
|
|
1104
|
+
|
|
1105
|
+
if exptime is not _DEPRECATED_ARG:
|
|
1106
|
+
warnings.warn(
|
|
1107
|
+
"Argument 'exptime' has been deprecated since version 3.0 and "
|
|
1108
|
+
"it will be removed in a future release. "
|
|
1109
|
+
"Use 'iscale' instead and set iscale=exptime "
|
|
1110
|
+
"to achieve the same effect as with 'exptime'.",
|
|
1111
|
+
DeprecationWarning,
|
|
1112
|
+
)
|
|
1113
|
+
iscale *= exptime
|
|
1114
|
+
|
|
1115
|
+
if output_pixel_shape is _DEPRECATED_ARG:
|
|
1116
|
+
output_shape = tuple(pixmap.shape[:2])
|
|
1117
|
+
else:
|
|
1118
|
+
warnings.warn(
|
|
1119
|
+
"Argument 'output_pixel_shape' has been deprecated since version "
|
|
1120
|
+
"3.0 and it will be removed in a future release. It is not needed "
|
|
1121
|
+
"since the output image shape can be inferred from 'pixmap'.",
|
|
1122
|
+
DeprecationWarning,
|
|
1123
|
+
)
|
|
1124
|
+
output_shape = output_pixel_shape[::-1]
|
|
1125
|
+
|
|
1126
|
+
if out_img is None:
|
|
1127
|
+
out_img = np.empty(output_shape, dtype=np.float32)
|
|
1128
|
+
else:
|
|
1129
|
+
out_img = np.asarray(out_img, dtype=np.float32)
|
|
1130
|
+
if out_img.shape != output_shape:
|
|
1131
|
+
raise ValueError("'output_image' shape is not consistent with 'pixmap' shape.")
|
|
1132
|
+
|
|
1133
|
+
cdrizzle.tblot(
|
|
1134
|
+
data, pixmap, out_img, iscale=iscale, interp=interp, fillval=fillval, sinscl=sinscl
|
|
1135
|
+
)
|
|
1136
|
+
|
|
1137
|
+
return out_img
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
def _process_fillval(out_img, fillval):
|
|
1141
|
+
if fillval is None:
|
|
1142
|
+
fillval = "INDEF"
|
|
1143
|
+
|
|
1144
|
+
elif isinstance(fillval, str):
|
|
1145
|
+
fillval = fillval.strip()
|
|
1146
|
+
if fillval.upper() in ["", "INDEF"]:
|
|
1147
|
+
fillval = "INDEF"
|
|
1148
|
+
else:
|
|
1149
|
+
float(fillval)
|
|
1150
|
+
fillval = str(fillval)
|
|
1151
|
+
|
|
1152
|
+
else:
|
|
1153
|
+
fillval = str(fillval)
|
|
1154
|
+
|
|
1155
|
+
if out_img is None and fillval == "INDEF":
|
|
1156
|
+
fillval = "NaN"
|
|
1157
|
+
|
|
1158
|
+
return fillval
|