add get_wcs_weights
import copy
+from functools import lru_cache
import numpy as np
from astropy import units as u
from astropy.coordinates import SkyCoord
@@ -62,6 +63,9 @@ class RegionGeom(Geom):
self._wcs = wcs
self.ndim = len(self.data_shape)
+ # define cached methods
+ self.get_wcs_weights = lru_cache()(self.get_wcs_weights)
+
@property
def frame(self):
"""Coordinate system, either Galactic ("galactic") or Equatorial
@@ -291,6 +295,25 @@ class RegionGeom(Geom):
region_coord = wcs_geom.get_coord().apply_mask(common_coord)
return region_coord
+ def get_wcs_weights(self):
+ """Get an array of weights that represent the
+ fraction of each pixel from the minimal
+ equivalent geometry contained in the region.
+
+ Returns
+ -------
+ weights : `~Map`
+ """
+ factor = 10
+ wcs_geom = self.to_wcs_geom()
+ wcs_geom_upsampled = wcs_geom.upsample(factor=factor)
+ data = self.contains(wcs_geom_upsampled.get_coord()).astype(float)
+
+ weights = Map.from_geom(wcs_geom_upsampled, data = data)
+ weights = weights.downsample(factor=factor)
+ weights.data /= weights.data.max()
+ return weights
+
def to_binsz(self, binsz):
"""Returns self"""
return self
