Commit 62bb3946832d450dae7ea273faf54393f45d783a
1 parent
4e17b793
Add skymap bin sky direction caching
Showing
1 changed file
with
20 additions
and
3 deletions
cscripts/cssrcdetect.py
| ... | ... | @@ -51,6 +51,7 @@ class cssrcdetect(ctools.cscript): |
| 51 | 51 | else: |
| 52 | 52 | self._map = gammalib.GSkyMap() |
| 53 | 53 | |
| 54 | + self._map_dirs = [] | |
| 54 | 55 | # Return |
| 55 | 56 | return |
| 56 | 57 | |
| ... | ... | @@ -166,7 +167,7 @@ class cssrcdetect(ctools.cscript): |
| 166 | 167 | for i in range(skymap.npix()): |
| 167 | 168 | if skymap[i] > value: |
| 168 | 169 | value = skymap[i] |
| 169 | - pos = skymap.inx2dir(i) | |
| 170 | + pos = self._map_dirs[i] | |
| 170 | 171 | |
| 171 | 172 | # Return sky direction of maximum |
| 172 | 173 | return value, pos |
| ... | ... | @@ -194,10 +195,13 @@ class cssrcdetect(ctools.cscript): |
| 194 | 195 | # Copy skymap |
| 195 | 196 | skymap_copy = skymap.copy() |
| 196 | 197 | |
| 198 | + # Cache the cosine of the radius | |
| 199 | + cos_radius = math.cos(math.radians(radius)) | |
| 200 | + | |
| 197 | 201 | # Loop over all pixels and find maximum |
| 198 | 202 | for i in range(skymap_copy.npix()): |
| 199 | - skymap_dir = skymap_copy.inx2dir(i) | |
| 200 | - if skymap_dir.dist_deg(pos) < radius: | |
| 203 | + skymap_dir = self._map_dirs[i] | |
| 204 | + if skymap_dir.cos_dist(pos) > cos_radius: | |
| 201 | 205 | skymap_copy[i] = value |
| 202 | 206 | |
| 203 | 207 | # Return copy of map |
| ... | ... | @@ -385,6 +389,17 @@ class cssrcdetect(ctools.cscript): |
| 385 | 389 | return skymap.extract(0) |
| 386 | 390 | |
| 387 | 391 | |
| 392 | + def _cache_map_dirs(self, skymap): | |
| 393 | + """ | |
| 394 | + Cache map pixel positions to save some computation time | |
| 395 | + """ | |
| 396 | + # Setup the skymap directions | |
| 397 | + if not skymap.is_empty(): | |
| 398 | + self._map_dirs = [skymap.inx2dir(i) for i in range(skymap.npix())] | |
| 399 | + else: | |
| 400 | + self._map_dirs = [] | |
| 401 | + | |
| 402 | + | |
| 388 | 403 | # Public methods |
| 389 | 404 | def run(self): |
| 390 | 405 | """ |
| ... | ... | @@ -404,6 +419,8 @@ class cssrcdetect(ctools.cscript): |
| 404 | 419 | if self._map.is_empty(): |
| 405 | 420 | self._map = self._load_skymap() |
| 406 | 421 | |
| 422 | + self._cache_map_dirs(self._map) | |
| 423 | + | |
| 407 | 424 | # Smooth the map |
| 408 | 425 | self._smooth_skymap(self._map) |
| 409 | 426 | ... | ... |