23 todo: Consider tweaking pixel scale so the average scale is as specified,
24 rather than the scale at the center.
27 __all__ = [
"BaseSkyMapConfig",
"BaseSkyMap"]
34 from lsst.geom import SpherePoint, Angle, arcseconds, degrees
39 patchInnerDimensions = pexConfig.ListField(
40 doc=
"dimensions of inner region of patches (x,y pixels)",
45 patchBorder = pexConfig.Field(
46 doc=
"border between patch inner and outer bbox (pixels)",
50 tractOverlap = pexConfig.Field(
51 doc=
"minimum overlap between adjacent sky tracts, on the sky (deg)",
55 pixelScale = pexConfig.Field(
56 doc=
"nominal pixel scale (arcsec/pixel)",
60 projection = pexConfig.Field(
61 doc=
"one of the FITS WCS projection codes, such as:"
62 "- STG: stereographic projection"
63 "- MOL: Molleweide's projection"
64 "- TAN: tangent-plane projection",
68 rotation = pexConfig.Field(
69 doc=
"Rotation for WCS (deg)",
76 """A collection of overlapping Tracts that map part or all of the sky.
78 See TractInfo for more information.
82 config : `BaseSkyMapConfig` or None (optional)
83 The configuration for this SkyMap; if None use the default config.
87 BaseSkyMap is an abstract base class. Subclasses must do the following:
88 define ``__init__`` and have it construct the TractInfo objects and put
89 them in ``__tractInfoList__`` define ``__getstate__`` and ``__setstate__``
90 to allow pickling (the butler saves sky maps using pickle);
91 see DodecaSkyMap for an example of how to do this. (Most of that code could
92 be moved into this base class, but that would make it harder to handle
93 older versions of pickle data.) define updateSha1 to add any
94 subclass-specific state to the hash.
96 All SkyMap subclasses must be conceptually immutable; they must always
97 refer to the same set of mathematical tracts and patches even if the in-
98 memory representation of those objects changes.
101 ConfigClass = BaseSkyMapConfig
110 pixelScale=
Angle(self.
configconfig.pixelScale, arcseconds),
111 projection=self.
configconfig.projection,
112 rotation=
Angle(self.
configconfig.rotation, degrees),
114 self.
_sha1_sha1 =
None
117 """Find the tract whose center is nearest the specified coord.
121 coord : `lsst.geom.SpherePoint`
122 ICRS sky coordinate to search for.
127 TractInfo of tract whose center is nearest the specified coord.
131 - If coord is equidistant between multiple sky tract centers then one
132 is arbitrarily chosen.
134 - The default implementation is not very efficient; subclasses may wish
138 If tracts do not cover the whole sky then the returned tract may not
141 distTractInfoList = []
142 for i, tractInfo
in enumerate(self):
143 angSep = coord.separation(tractInfo.getCtrCoord()).asDegrees()
145 distTractInfoList.append((angSep, i, tractInfo))
146 distTractInfoList.sort()
147 return distTractInfoList[0][2]
150 """Find tracts and patches that overlap a region.
154 coordList : `list` of `lsst.geom.SpherePoint`
155 List of ICRS sky coordinates to search for.
159 reList : `list` of (`TractInfo`, `list` of `PatchInfo`)
160 For tracts and patches that contain, or may contain, the specified
161 region. The list will be empty if there is no overlap.
166 This uses a naive algorithm that may find some tracts and patches
167 that do not overlap the region (especially if the region is not a
168 rectangle aligned along patch x, y).
171 for tractInfo
in self:
172 patchList = tractInfo.findPatchList(coordList)
174 retList.append((tractInfo, patchList))
178 """Find closest tract and patches that overlap coordinates.
182 coordList : `lsst.geom.SpherePoint`
183 List of ICRS sky coordinates to search for.
188 list of (TractInfo, list of PatchInfo) for tracts and patches
189 that contain, or may contain, the specified region.
190 The list will be empty if there is no overlap.
193 for coord
in coordList:
194 tractInfo = self.
findTractfindTract(coord)
195 patchList = tractInfo.findPatchList(coordList)
196 if patchList
and not (tractInfo, patchList)
in retList:
197 retList.append((tractInfo, patchList))
210 return hash(self.
getSha1getSha1())
214 return self.
getSha1getSha1() == other.getSha1()
215 except AttributeError:
216 return NotImplemented
219 return not (self == other)
222 """Write information about a sky map to supplied log
227 Log object that information about skymap will be written
229 log.info(
"sky map has %s tracts" % (len(self),))
230 for tractInfo
in self:
231 wcs = tractInfo.getWcs()
239 skyPosList = [wcs.pixelToSky(pos).getPosition(geom.degrees)
for pos
in pixelPosList]
240 posStrList = [
"(%0.3f, %0.3f)" % tuple(skyPos)
for skyPos
in skyPosList]
241 log.info(
"tract %s has corners %s (RA, Dec deg) and %s x %s patches" %
242 (tractInfo.getId(),
", ".join(posStrList),
243 tractInfo.getNumPatches()[0], tractInfo.getNumPatches()[1]))
246 """Return a SHA1 hash that uniquely identifies this SkyMap instance.
251 A 20-byte hash that uniquely identifies this SkyMap instance.
255 Subclasses should almost always override ``updateSha1`` instead of
256 this function to add subclass-specific state to the hash.
258 if self.
_sha1_sha1
is None:
259 sha1 = hashlib.sha1()
260 sha1.update(
type(self).__name__.encode(
'utf-8'))
261 configPacked = struct.pack(
263 self.
configconfig.patchInnerDimensions[0],
264 self.
configconfig.patchInnerDimensions[1],
265 self.
configconfig.patchBorder,
266 self.
configconfig.tractOverlap,
267 self.
configconfig.pixelScale,
268 self.
configconfig.projection.encode(
'ascii'),
269 self.
configconfig.rotation
271 sha1.update(configPacked)
273 self.
_sha1_sha1 = sha1.digest()
274 return self.
_sha1_sha1
277 """Add subclass-specific state or configuration options to the SHA1.
281 sha1 : `hashlib.sha1`
282 A hashlib object on which `update` can be called to add
283 additional state to the hash.
287 This method is conceptually "protected" : it should be reimplemented by
288 all subclasses, but called only by the base class implementation of
291 raise NotImplementedError()
293 SKYMAP_RUN_COLLECTION_NAME =
"skymaps"
295 SKYMAP_DATASET_TYPE_NAME =
"skyMap"
298 """Add skymap, tract, and patch Dimension entries to the given Gen3
304 The name of the skymap.
305 butler : `lsst.daf.butler.Butler`
306 The butler to add to.
310 lsst.daf.butler.registry.ConflictingDefinitionError
311 Raised if a different skymap exists with the same name.
315 Registering the same skymap multiple times (with the exact same
316 definition) is safe, but inefficient; most of the work of computing
317 the rows to be inserted must be done first in order to check for
318 consistency between the new skymap and any existing one.
320 Re-registering a skymap with different tract and/or patch definitions
321 but the same summary information may not be detected as a conflict but
322 will never result in updating the skymap; there is intentionally no
323 way to modify a registered skymap (aside from manual administrative
324 operations on the database), as it is hard to guarantee that this can
325 be done without affecting reproducibility.
331 for tractInfo
in self:
332 nx, ny = tractInfo.getNumPatches()
333 nxMax =
max(nxMax, nx)
334 nyMax =
max(nyMax, ny)
335 region = tractInfo.getOuterSkyPolygon()
337 tractRecords.append({
339 "tract": tractInfo.getId(),
341 "ra": centroid.getRa().asDegrees(),
342 "dec": centroid.getDec().asDegrees(),
344 for patchInfo
in tractInfo:
345 cellX, cellY = patchInfo.getIndex()
346 patchRecords.append({
348 "tract": tractInfo.getId(),
349 "patch": tractInfo.getSequentialPatchIndex(patchInfo),
352 "region": patchInfo.getOuterSkyPolygon(tractInfo.getWcs()),
357 "tract_max": len(self),
358 "patch_nx_max": nxMax,
359 "patch_ny_max": nyMax,
365 from lsst.daf.butler
import DatasetType
366 from lsst.daf.butler.registry
import ConflictingDefinitionError
367 datasetType = DatasetType(
369 dimensions=[
"skymap"],
370 storageClass=
"SkyMap",
371 universe=butler.registry.dimensions
373 butler.registry.registerDatasetType(datasetType)
374 with butler.transaction():
376 inserted = butler.registry.syncDimensionData(
"skymap", skyMapRecord)
377 except ConflictingDefinitionError
as err:
378 raise ConflictingDefinitionError(
379 f
"SkyMap with hash {self.getSha1().hex()} is already registered with a different name."
382 butler.registry.insertDimensionData(
"tract", *tractRecords)
383 butler.registry.insertDimensionData(
"patch", *patchRecords)
A class representing an angle.
A floating-point coordinate rectangle geometry.
Point in an unspecified spherical coordinate system.
def findTract(self, coord)
def register(self, name, butler)
def updateSha1(self, sha1)
def findClosestTractPatchList(self, coordList)
string SKYMAP_RUN_COLLECTION_NAME
string SKYMAP_DATASET_TYPE_NAME
def logSkyMapInfo(self, log)
def findTractPatchList(self, coordList)
def __init__(self, config=None)
def __getitem__(self, ind)