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