lsst.skymap  14.0-4-g3609236+7
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 import struct
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, e=e):
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 
50 def angToCoord(thetaphi):
51  """Convert healpy's ang to an afw Coord
52 
53  The ang is provided as a single object, thetaphi, so the output
54  of healpy functions can be directed to this function without
55  additional translation.
56  """
57  return IcrsCoord(float(thetaphi[1])*afwGeom.radians, float(thetaphi[0] - 0.5*numpy.pi)*afwGeom.radians)
58 
59 
60 def coordToAng(coord):
61  """Convert an afw Coord to a healpy ang (theta, phi)
62 
63  The Healpix convention is that 0 <= theta <= pi, 0 <= phi < 2pi.
64  """
65  return (coord.getLatitude().asRadians() + 0.5*numpy.pi, coord.getLongitude().asRadians())
66 
67 
69  """Tract for the HealpixSkyMap"""
70 
71  def __init__(self, nSide, ident, nest, patchInnerDimensions, patchBorder, ctrCoord, tractOverlap, wcs):
72  """Set vertices from nside, ident, nest"""
73  theta, phi = healpy.vec2ang(numpy.transpose(healpy.boundaries(nSide, ident, nest=nest)))
74  vertexList = [angToCoord(thetaphi) for thetaphi in zip(theta, phi)]
75  super(HealpixTractInfo, self).__init__(ident, patchInnerDimensions, patchBorder, ctrCoord,
76  vertexList, tractOverlap, wcs)
77 
78 
79 class HealpixSkyMapConfig(CachingSkyMap.ConfigClass):
80  """Configuration for the HealpixSkyMap"""
81  log2NSide = Field(dtype=int, default=0, doc="Number of sides, expressed in powers of 2")
82  nest = Field(dtype=bool, default=False, doc="Use NEST ordering instead of RING?")
83 
84  def setDefaults(self):
85  self.rotation = 45 # HEALPixels are oriented at 45 degrees
86 
87 
89  """HEALPix-based sky map pixelization.
90 
91  We put a Tract at the position of each HEALPixel.
92  """
93  ConfigClass = HealpixSkyMapConfig
94  _version = (1, 0) # for pickle
95  numAngles = 4 # Number of angles for vertices
96 
97  def __init__(self, config, version=0):
98  """Constructor
99 
100  @param[in] config: an instance of self.ConfigClass; if None the default config is used
101  @param[in] version: software version of this class, to retain compatibility with old instances
102  """
103  self._nside = 1 << config.log2NSide
104  numTracts = healpy.nside2npix(self._nside)
105  super(HealpixSkyMap, self).__init__(numTracts, config, version)
106 
107  def findTract(self, coord):
108  """Find the tract whose inner region includes the coord."""
109  theta, phi = coordToAng(coord.toIcrs())
110  index = healpy.ang2pix(self._nside, theta, phi, nest=self.config.nest)
111  return self[index]
112 
113  def generateTract(self, index):
114  """Get the TractInfo for a particular index"""
115  center = angToCoord(healpy.pix2ang(self._nside, index, nest=self.config.nest))
116  wcs = self._wcsFactory.makeWcs(crPixPos=afwGeom.Point2D(0, 0), crValCoord=center)
117  return HealpixTractInfo(self._nside, index, self.config.nest, self.config.patchInnerDimensions,
118  self.config.patchBorder, center, self.config.tractOverlap*afwGeom.degrees,
119  wcs)
120 
121  def updateSha1(self, sha1):
122  """Add subclass-specific state or configuration options to the SHA1."""
123  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)