LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
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 
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, e:
32  class DummyHealpy(object):
33  """An object which blows up when we try to read it"""
34  def __getattr__(self, name):
35  raise RuntimeError("Was unable to import healpy: %s" % e)
36  healpy = DummyHealpy()
37 
38 from lsst.pex.config import Field
39 from lsst.afw.coord import IcrsCoord
40 import lsst.afw.geom as afwGeom
41 from .cachingSkyMap import CachingSkyMap
42 from .tractInfo import TractInfo
43 
44 
45 def angToCoord(thetaphi):
46  """Convert healpy's ang to an afw Coord
47 
48  The ang is provided as a single object, thetaphi, so the output
49  of healpy functions can be directed to this function without
50  additional translation.
51  """
52  return IcrsCoord(float(thetaphi[1])*afwGeom.radians, float(thetaphi[0] - 0.5*numpy.pi)*afwGeom.radians)
53 
54 def coordToAng(coord):
55  """Convert an afw Coord to a healpy ang (theta, phi)"""
56  return (coord.getLatitude().asRadians() - 0.5*numpy.pi, coord.getLongitude().asRadians())
57 
58 class HealpixTractInfo(TractInfo):
59  """Tract for the HealpixSkyMap"""
60  def __init__(self, nSide, ident, nest, patchInnerDimensions, patchBorder, ctrCoord, tractOverlap, wcs):
61  """Set vertices from nside, ident, nest"""
62  theta, phi = healpy.vec2ang(numpy.transpose(healpy.boundaries(nSide, ident, nest=nest)))
63  vertexList = [angToCoord(thetaphi) for thetaphi in zip(theta,phi)]
64  super(HealpixTractInfo, self).__init__(ident, patchInnerDimensions, patchBorder, ctrCoord,
65  vertexList, tractOverlap, wcs)
66 
67 
68 class HealpixSkyMapConfig(CachingSkyMap.ConfigClass):
69  """Configuration for the HealpixSkyMap"""
70  log2NSide = Field(dtype=int, default=0, doc="Number of sides, expressed in powers of 2")
71  nest = Field(dtype=bool, default=False, doc="Use NEST ordering instead of RING?")
72  def setDefaults(self):
73  self.rotation = 45 # HEALPixels are oriented at 45 degrees
74 
75 class HealpixSkyMap(CachingSkyMap):
76  """HEALPix-based sky map pixelization.
77 
78  We put a Tract at the position of each HEALPixel.
79  """
80  ConfigClass = HealpixSkyMapConfig
81  _version = (1, 0) # for pickle
82  numAngles = 4 # Number of angles for vertices
83 
84  def __init__(self, config, version=0):
85  """Constructor
86 
87  @param[in] config: an instance of self.ConfigClass; if None the default config is used
88  @param[in] version: software version of this class, to retain compatibility with old instances
89  """
90  self._nside = 1 << config.log2NSide
91  numTracts = healpy.nside2npix(self._nside)
92  super(HealpixSkyMap, self).__init__(numTracts, config, version)
93 
94  def findTract(self, coord):
95  """Find the tract whose inner region includes the coord."""
96  theta, phi = coordToAng(coord.toIcrs())
97  index = healpy.ang2pix(self._nside, theta, phi, nest=self.config.nest)
98  return self[index]
99 
100  def generateTract(self, index):
101  """Get the TractInfo for a particular index"""
102  center = angToCoord(healpy.pix2ang(self._nside, index, nest=self.config.nest))
103  wcs = self._wcsFactory.makeWcs(crPixPos=afwGeom.Point2D(0,0), crValCoord=center)
104  return HealpixTractInfo(self._nside, index, self.config.nest, self.config.patchInnerDimensions,
105  self.config.patchBorder, center, self.config.tractOverlap*afwGeom.degrees,
106  wcs)
107 
108 
A class to handle Icrs coordinates (inherits from Coord)
Definition: Coord.h:157