Skip to content

Commit 602bd8a

Browse files
committed
Improve code for estimating pixel area
1 parent faab09a commit 602bd8a

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

jwst/resample/resample.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -657,30 +657,34 @@ def _get_boundary_points(xmin, xmax, ymin, ymax, dx=None, dy=None, shrink=0):
657657
ymin += shrink
658658
ymax -= shrink
659659

660-
size = 2 * sx + 2 * sy + 1
660+
size = 2 * sx + 2 * sy
661661
x = np.empty(size)
662662
y = np.empty(size)
663663

664-
x[0:sx] = np.linspace(xmin, xmax, sx, False)
665-
y[0:sx] = ymin
666-
x[sx:sx + sy] = xmax
667-
y[sx:sx + sy] = np.linspace(ymin, ymax, sy, False)
668-
x[sx + sy:2 * sx + sy] = np.linspace(xmax, xmin, sx, False)
669-
y[sx + sy:2 * sx + sy] = ymax
670-
x[2 * sx + sy:2 * sx + 2 * sy] = xmin
671-
y[2 * sx + sy:2 * sx + 2 * sy] = np.linspace(ymax, ymin, sy, False)
672-
x[-1] = xmin
673-
y[-1] = ymin
664+
b = np.s_[0:sx] # bottom edge
665+
r = np.s_[sx:sx + sy] # right edge
666+
t = np.s_[sx + sy:2 * sx + sy] # top edge
667+
l = np.s_[2 * sx + sy:2 * sx + 2 * sy] # left
668+
669+
x[b] = np.linspace(xmin, xmax, sx, False)
670+
y[b] = ymin
671+
x[r] = xmax
672+
y[r] = np.linspace(ymin, ymax, sy, False)
673+
x[t] = np.linspace(xmax, xmin, sx, False)
674+
y[t] = ymax
675+
x[l] = xmin
676+
y[l] = np.linspace(ymax, ymin, sy, False)
674677

675678
area = (xmax - xmin) * (ymax - ymin)
676679
center = (0.5 * (xmin + xmax), 0.5 * (ymin + ymax))
677680

678-
return x, y, area, center
681+
return x, y, area, center, b, r, t, l
679682

680683

681684
def _compute_image_pixel_area(wcs):
682685
""" Computes pixel area in steradians.
683686
"""
687+
684688
if wcs.array_shape is None:
685689
raise ValueError("WCS must have array_shape attribute set.")
686690

@@ -695,17 +699,23 @@ def _compute_image_pixel_area(wcs):
695699
xmax = min(nx - 1, int(xmax - 0.5))
696700
ymin = max(0, int(ymin + 0.5))
697701
ymax = min(ny - 1, int(ymax - 0.5))
702+
if xmin > xmax:
703+
(xmin, xmax) = (xmax, xmin)
704+
if ymin > ymax:
705+
(ymin, ymax) = (ymax, ymin)
706+
707+
k = 0
708+
dxy = [1, -1, -1, 1]
698709

699-
for shrink in range(5):
710+
while xmin < xmax and ymin < ymax:
700711
try:
701-
x, y, image_area, center = _get_boundary_points(
712+
x, y, image_area, center, b, r, t, l = _get_boundary_points(
702713
xmin=xmin,
703714
xmax=xmax,
704715
ymin=ymin,
705716
ymax=ymax,
706717
dx=min((xmax - xmin) // 4, 15),
707-
dy=min((ymax - ymin) // 4, 15),
708-
shrink=shrink
718+
dy=min((ymax - ymin) // 4, 15)
709719
)
710720
except ValueError:
711721
return None
@@ -714,10 +724,23 @@ def _compute_image_pixel_area(wcs):
714724
ra = world[spatial_idx[0]]
715725
dec = world[spatial_idx[1]]
716726

717-
if (np.all(np.isfinite(ra)) and np.all(np.isfinite(dec))):
727+
limits = [ymin, xmax, ymax, xmin]
728+
729+
for j in range(4):
730+
sl = [b, r, t, l][k]
731+
if not (np.all(np.isfinite(ra[sl])) and
732+
np.all(np.isfinite(dec[sl]))):
733+
limits[k] += dxy[k]
734+
ymin, xmax, ymax, xmin = limits
735+
k = (k + 1) % 4
736+
break
737+
k = (k + 1) % 4
738+
else:
718739
valid_polygon = True
719740
break
720741

742+
ymin, xmax, ymax, xmin = limits
743+
721744
if not valid_polygon:
722745
return None
723746

0 commit comments

Comments
 (0)