26 from .patchInfo
import PatchInfo
28 __all__ = [
"TractInfo"]
32 """Information about a tract in a SkyMap sky pixelization 34 The tract is subdivided into rectangular patches. Each patch has the following properties: 35 - An inner region defined by an inner bounding. The inner regions of the patches exactly tile the tract, 36 and all inner regions have the same dimensions. The tract is made larger as required to make this work. 37 - An outer region defined by an outer bounding box. The outer region extends beyond the inner region 38 by patchBorder pixels in all directions, except there is no border at the edges of the tract. 39 Thus patches overlap each other but never extend off the tract. If you do not want any overlap 40 between adjacent patches then set patchBorder to 0. 41 - An index that consists of a pair of integers: 42 0 <= x index < numPatches[0] 43 0 <= y index < numPatches[1] 44 Patch 0,0 is at the minimum corner of the tract bounding box. 47 def __init__(self, id, patchInnerDimensions, patchBorder, ctrCoord, vertexCoordList, tractOverlap, wcs):
48 """Construct a TractInfo 50 @param[in] id: tract ID 51 @param[in] patchInnerDimensions: dimensions of inner region of patches (x,y pixels) 52 @param[in] patchBorder: overlap between adjacent patches (in pixels, one int) 53 @param[in] ctrCoord: ICRS sky coordinate of center of inner region of tract 54 as an lsst.afw.geom.SpherePoint; also used as the CRVAL for the WCS. 55 @param[in] vertexCoordList: list of ICRS sky coordinates (lsst.afw.geom.SpherePoint) 56 of vertices that define the boundaries of the inner region 57 @param[in] tractOverlap: minimum overlap between adjacent sky tracts; an afwGeom.Angle; 58 this defines the minimum distance the tract extends beyond the inner region in all directions 59 @param[in,out] wcs: an afwImage.Wcs; the reference pixel will be shifted as required 60 so that the lower left-hand pixel (index 0,0) has pixel position 0.0, 0.0 63 - It is not enforced that ctrCoord is the center of vertexCoordList, but SkyMap relies on it 64 - vertexCoordList will likely become a geom SphericalConvexPolygon someday. 68 assert len(patchInnerDimensions) == 2
71 raise TypeError(
"patchInnerDimensions=%s; must be two ints" % (patchInnerDimensions,))
81 def _minimumBoundingBox(self, wcs):
82 """Calculate the minimum bounding box for the tract, given the WCS 84 The bounding box is created in the frame of the supplied WCS, 85 so that it's OK if the coordinates are negative. 87 We compute the bounding box that holds all the vertices and the 90 minBBoxD = afwGeom.Box2D()
94 minBBoxD.include(wcs.skyToPixel(vertexCoord))
97 angleIncr = afwGeom.Angle(360.0, afwGeom.degrees) / float(numAngles)
98 for i
in range(numAngles):
99 offAngle = angleIncr * i
100 offCoord = vertexCoord.offset(offAngle, halfOverlap)
101 pixPos = wcs.skyToPixel(offCoord)
102 minBBoxD.include(pixPos)
105 def _setupPatches(self, minBBox, wcs):
106 """Setup for patches of a particular size. 108 We grow the bounding box to hold an exact multiple of 109 the desired size (patchInnerDimensions), while keeping 110 the center roughly the same. We return the final 111 bounding box, and the number of patches in each dimension 114 @param minBBox Minimum bounding box for tract 115 @param wcs Wcs object 116 @return final bounding box, number of patches 118 bbox = afwGeom.Box2I(minBBox)
119 bboxMin = bbox.getMin()
120 bboxDim = bbox.getDimensions()
121 numPatches = afwGeom.Extent2I(0, 0)
123 num = (bboxDim[i] + innerDim - 1) // innerDim
124 deltaDim = (innerDim * num) - bboxDim[i]
126 bboxDim[i] = innerDim * num
127 bboxMin[i] -= deltaDim // 2
129 bbox = afwGeom.Box2I(bboxMin, bboxDim)
130 return bbox, numPatches
132 def _finalOrientation(self, bbox, wcs):
133 """Determine the final orientation 135 We offset everything so the lower-left corner is at 0,0 136 and compute the final Wcs. 138 @param bbox Current bounding box 139 @param wcs Current Wcs 140 @return revised bounding box, revised Wcs 142 finalBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0), bbox.getDimensions())
145 pixPosOffset = afwGeom.Extent2D(finalBBox.getMinX() - bbox.getMinX(),
146 finalBBox.getMinY() - bbox.getMinY())
147 wcs = wcs.copyAtShiftedPixelOrigin(pixPosOffset)
148 return finalBBox, wcs
151 """Return a single integer that uniquely identifies the given patch 154 x, y = patchInfo.getIndex()
159 """Find the patch containing the specified coord 161 @param[in] coord: ICRS sky coordinate (lsst.afw.geom.SpherePoint) 162 @return PatchInfo of patch whose inner bbox contains the specified coord 164 @raise LookupError if coord is not in tract or we cannot determine the 165 pixel coordinate (which likely means the coord is off the tract). 167 @note This routine will be more efficient if coord is ICRS. 170 pixel = self.
getWcs().skyToPixel(coord)
173 raise LookupError(
"Unable to determine pixel position for coordinate %s" % (coord,))
174 pixelInd = afwGeom.Point2I(pixel)
176 raise LookupError(
"coord %s is not in tract %s" % (coord, self.
getId()))
181 """Find patches containing the specified list of coords 183 @param[in] coordList: list of sky coordinates (lsst.afw.geom.SpherePoint) 184 @return list of PatchInfo for patches that contain, or may contain, the specified region. 185 The list will be empty if there is no overlap. 188 * This may give incorrect answers on regions that are larger than a tract 189 * This uses a naive algorithm that may find some patches that do not overlap the region 190 (especially if the region is not a rectangle aligned along patch x,y). 192 box2D = afwGeom.Box2D()
193 for coord
in coordList:
195 pixelPos = self.
getWcs().skyToPixel(coord)
199 box2D.include(pixelPos)
200 bbox = afwGeom.Box2I(box2D)
209 for xInd
in range(llPatchInd[0], urPatchInd[0]+1)
210 for yInd
in range(llPatchInd[1], urPatchInd[1]+1))
213 """Get bounding box of tract (as an afwGeom.Box2I) 215 return afwGeom.Box2I(self._bbox)
218 """Get ICRS sky coordinate of center of tract (as an lsst.afw.geom.SpherePoint) 228 """Get the number of patches in x, y 230 @return the number of patches in x, y 237 @return patch border (pixels) 242 """Return information for the specified patch 244 @param[in] index: index of patch, as a pair of ints 245 @return patch info, an instance of PatchInfo 247 @raise IndexError if index is out of range 251 raise IndexError(
"Patch index %s is not in range [0-%d, 0-%d]" %
255 if not self._bbox.
contains(innerBBox):
257 "Bug: patch index %s valid but inner bbox=%s not contained in tract bbox=%s" %
258 (index, innerBBox, self._bbox))
259 outerBBox = afwGeom.Box2I(innerBBox)
261 outerBBox.clip(self._bbox)
269 """Get dimensions of inner region of the patches (all are the same) 271 @return dimensions of inner region of the patches (as an afwGeom Extent2I) 276 """Get minimum overlap of adjacent sky tracts 278 @return minimum overlap between adjacent sky tracts, as an afwGeom Angle 283 """Get list of sky coordinates of vertices that define the boundary of the inner region 285 @warning: this is not a deep copy 286 @warning vertexCoordList will likely become a geom SphericalConvexPolygon someday. 291 """Return the tract region as a sphgeom.ConvexPolygon. 299 @warning: this is not a deep copy 304 return "TractInfo(id=%s)" % (self.
_id,)
307 return "TractInfo(id=%s, ctrCoord=%s)" % (self.
_id, self.
_ctrCoord.getVector())
311 for y
in range(yNum):
312 for x
in range(xNum):
323 """Does this tract contain the coordinate?""" 325 pixels = self.
getWcs().skyToPixel(coord)
333 """Information for a tract specified explicitly 335 A tract is placed at the explicitly defined coordinates, with the nominated 336 radius. The tracts are square (i.e., the radius is really a half-size). 339 def __init__(self, ident, patchInnerDimensions, patchBorder, ctrCoord, radius, tractOverlap, wcs):
343 super(ExplicitTractInfo, self).
__init__(ident, patchInnerDimensions, patchBorder, ctrCoord,
344 vertexList, tractOverlap, wcs)
346 bboxD = afwGeom.BoxD(self.
getBBox())
351 def _minimumBoundingBox(self, wcs):
352 """The minimum bounding box is calculated using the nominated radius""" 353 bbox = afwGeom.Box2D()
356 pixPos = wcs.skyToPixel(cornerCoord)
def getTractOverlap(self)
def _finalOrientation(self, bbox, wcs)
def _setupPatches(self, minBBox, wcs)
def getPatchInfo(self, index)
def contains(self, coord)
def getPatchInnerDimensions(self)
def __getitem__(self, index)
def _minimumBoundingBox(self, wcs)
def getSequentialPatchIndex(self, patchInfo)
def findPatch(self, coord)
def findPatchList(self, coordList)
def __init__(self, ident, patchInnerDimensions, patchBorder, ctrCoord, radius, tractOverlap, wcs)
def __init__(self, id, patchInnerDimensions, patchBorder, ctrCoord, vertexCoordList, tractOverlap, wcs)