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