lsst.skymap  14.0-2-g8373656+17
healpixSkyMap.py
Go to the documentation of this file.
1 from builtins import zip
2 from builtins import object
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010, 2012 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 import numpy
26 
27 # We want to register the HealpixSkyMap, but want "healpy" to be an
28 # optional dependency. However, the HealpixSkyMap requires the use
29 # of healpy. Therefore, we'll only raise an exception on the healpy
30 # import when it comes time to using it.
31 try:
32  import healpy
33 except Exception as e:
34  class DummyHealpy(object):
35  """An object which blows up when we try to read it"""
36 
37  def __getattr__(self, name):
38  raise RuntimeError("Was unable to import healpy: %s" % e)
39  healpy = DummyHealpy()
40 
41 from lsst.pex.config import Field
42 from lsst.afw.coord import IcrsCoord
43 import lsst.afw.geom as afwGeom
44 from .cachingSkyMap import CachingSkyMap
45 from .tractInfo import TractInfo
46 
47 __all__ = ['HealpixSkyMapConfig', 'HealpixSkyMap']
48 
49 def angToCoord(thetaphi):
50  """Convert healpy's ang to an afw Coord
51 
52  The ang is provided as a single object, thetaphi, so the output
53  of healpy functions can be directed to this function without
54  additional translation.
55  """
56  return IcrsCoord(float(thetaphi[1])*afwGeom.radians, float(thetaphi[0] - 0.5*numpy.pi)*afwGeom.radians)
57 
58 
59 def coordToAng(coord):
60  """Convert an afw Coord to a healpy ang (theta, phi)
61 
62  The Healpix convention is that 0 <= theta <= pi, 0 <= phi < 2pi.
63  """
64  return (coord.getLatitude().asRadians() + 0.5*numpy.pi, coord.getLongitude().asRadians())
65 
66 
68  """Tract for the HealpixSkyMap"""
69 
70  def __init__(self, nSide, ident, nest, patchInnerDimensions, patchBorder, ctrCoord, tractOverlap, wcs):
71  """Set vertices from nside, ident, nest"""
72  theta, phi = healpy.vec2ang(numpy.transpose(healpy.boundaries(nSide, ident, nest=nest)))
73  vertexList = [angToCoord(thetaphi) for thetaphi in zip(theta, phi)]
74  super(HealpixTractInfo, self).__init__(ident, patchInnerDimensions, patchBorder, ctrCoord,
75  vertexList, tractOverlap, wcs)
76 
77 
78 class HealpixSkyMapConfig(CachingSkyMap.ConfigClass):
79  """Configuration for the HealpixSkyMap"""
80  log2NSide = Field(dtype=int, default=0, doc="Number of sides, expressed in powers of 2")
81  nest = Field(dtype=bool, default=False, doc="Use NEST ordering instead of RING?")
82 
83  def setDefaults(self):
84  self.rotation = 45 # HEALPixels are oriented at 45 degrees
85 
86 
88  """HEALPix-based sky map pixelization.
89 
90  We put a Tract at the position of each HEALPixel.
91  """
92  ConfigClass = HealpixSkyMapConfig
93  _version = (1, 0) # for pickle
94  numAngles = 4 # Number of angles for vertices
95 
96  def __init__(self, config, version=0):
97  """Constructor
98 
99  @param[in] config: an instance of self.ConfigClass; if None the default config is used
100  @param[in] version: software version of this class, to retain compatibility with old instances
101  """
102  self._nside = 1 << config.log2NSide
103  numTracts = healpy.nside2npix(self._nside)
104  super(HealpixSkyMap, self).__init__(numTracts, config, version)
105 
106  def findTract(self, coord):
107  """Find the tract whose inner region includes the coord."""
108  theta, phi = coordToAng(coord.toIcrs())
109  index = healpy.ang2pix(self._nside, theta, phi, nest=self.config.nest)
110  return self[index]
111 
112  def generateTract(self, index):
113  """Get the TractInfo for a particular index"""
114  center = angToCoord(healpy.pix2ang(self._nside, index, nest=self.config.nest))
115  wcs = self._wcsFactory.makeWcs(crPixPos=afwGeom.Point2D(0, 0), crValCoord=center)
116  return HealpixTractInfo(self._nside, index, self.config.nest, self.config.patchInnerDimensions,
117  self.config.patchBorder, center, self.config.tractOverlap*afwGeom.degrees,
118  wcs)
def __init__(self, config, version=0)
def __init__(self, nSide, ident, nest, patchInnerDimensions, patchBorder, ctrCoord, tractOverlap, wcs)