Split pad into multiple private methods.
@@ -367,6 +367,17 @@ class WcsNDMap(WcsMap):
geom = self.geom.pad(pad_width[:2])
if self.geom.is_regular and mode != 'interp':
+ return self._pad_np(geom, pad_width, mode, cval)
+ else:
+ return self._pad_coadd(geom, pad_width, mode, cval)
+
+ return map_out
+
+ def _pad_np(self, geom, pad_width, mode, cval):
+ """Pad a map with `~np.pad`. This method only works for regular
+ geometries but should be more efficient when working with
+ large maps.
+ """
kw = {}
if mode == 'constant':
kw['constant_values'] = cval
@@ -374,17 +385,20 @@ class WcsNDMap(WcsMap):
pad_width = [(t, t) for t in pad_width]
data = np.pad(self.data, pad_width[::-1], mode, **kw)
map_out = self.__class__(geom, data)
- else:
+ return map_out
+
+ def _pad_coadd(self, geom, pad_width, mode, cval):
+ """Pad a map manually by coadding the original map with the new
+ map."""
idx_in = self.geom.get_idx(flat=True)
idx_in = tuple([t + w for t, w in zip(idx_in, pad_width)])[::-1]
idx_out = geom.get_idx(flat=True)[::-1]
map_out = self.__class__(geom)
-
+ map_out.coadd(self)
+ if mode == 'constant':
pad_msk = np.zeros_like(map_out.data, dtype=bool)
pad_msk[idx_out] = True
pad_msk[idx_in] = False
- map_out.coadd(self)
- if mode == 'constant':
map_out.data[pad_msk] = cval
else:
raise NotImplementedError
