LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
loadReferenceObjects.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 #
4 # Copyright 2008-2017 AURA/LSST.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <https://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 __all__ = ["getRefFluxField", "getRefFluxKeys", "LoadReferenceObjectsTask", "LoadReferenceObjectsConfig",
25  "ReferenceObjectLoader"]
26 
27 import abc
28 import itertools
29 
30 import astropy.time
31 import astropy.units
32 import numpy
33 
34 import lsst.geom
35 import lsst.afw.table as afwTable
36 import lsst.pex.config as pexConfig
37 import lsst.pex.exceptions as pexExceptions
38 import lsst.pipe.base as pipeBase
39 import lsst.pex.exceptions as pexExcept
40 import lsst.log
41 from lsst import geom
42 from lsst import sphgeom
43 from lsst.daf.base import PropertyList
44 
45 
46 def isOldFluxField(name, units):
47  """Return True if this name/units combination corresponds to an
48  "old-style" reference catalog flux field.
49  """
50  unitsCheck = units != 'nJy' # (units == 'Jy' or units == '' or units == '?')
51  isFlux = name.endswith('_flux')
52  isFluxSigma = name.endswith('_fluxSigma')
53  isFluxErr = name.endswith('_fluxErr')
54  return (isFlux or isFluxSigma or isFluxErr) and unitsCheck
55 
56 
58  """Return True if the units of all flux and fluxErr are correct (nJy).
59  """
60  for field in schema:
61  if isOldFluxField(field.field.getName(), field.field.getUnits()):
62  return False
63  return True
64 
65 
67  """"Return the format version stored in a reference catalog header.
68 
69  Parameters
70  ----------
71  refCat : `lsst.afw.table.SimpleCatalog`
72  Reference catalog to inspect.
73 
74  Returns
75  -------
76  version : `int` or `None`
77  Format version integer, or `None` if the catalog has no metadata
78  or the metadata does not include a "REFCAT_FORMAT_VERSION" key.
79  """
80  md = refCat.getMetadata()
81  if md is None:
82  return None
83  try:
84  return md.getScalar("REFCAT_FORMAT_VERSION")
85  except KeyError:
86  return None
87 
88 
89 def convertToNanojansky(catalog, log, doConvert=True):
90  """Convert fluxes in a catalog from jansky to nanojansky.
91 
92  Parameters
93  ----------
94  catalog : `lsst.afw.table.SimpleCatalog`
95  The catalog to convert.
96  log : `lsst.log.Log`
97  Log to send messages to.
98  doConvert : `bool`, optional
99  Return a converted catalog, or just identify the fields that need to be converted?
100  This supports the "write=False" mode of `bin/convert_to_nJy.py`.
101 
102  Returns
103  -------
104  catalog : `lsst.afw.table.SimpleCatalog` or None
105  The converted catalog, or None if ``doConvert`` is False.
106 
107  Notes
108  -----
109  Support for old units in reference catalogs will be removed after the
110  release of late calendar year 2019.
111  Use `meas_algorithms/bin/convert_to_nJy.py` to update your reference catalog.
112  """
113  # Do not share the AliasMap: for refcats, that gets created when the
114  # catalog is read from disk and should not be propagated.
115  mapper = lsst.afw.table.SchemaMapper(catalog.schema, shareAliasMap=False)
116  mapper.addMinimalSchema(lsst.afw.table.SimpleTable.makeMinimalSchema())
117  input_fields = []
118  output_fields = []
119  for field in catalog.schema:
120  oldName = field.field.getName()
121  oldUnits = field.field.getUnits()
122  if isOldFluxField(oldName, oldUnits):
123  units = 'nJy'
124  # remap Sigma flux fields to Err, so we can drop the alias
125  if oldName.endswith('_fluxSigma'):
126  name = oldName.replace('_fluxSigma', '_fluxErr')
127  else:
128  name = oldName
129  newField = lsst.afw.table.Field[field.dtype](name, field.field.getDoc(), units)
130  mapper.addMapping(field.getKey(), newField)
131  input_fields.append(field.field)
132  output_fields.append(newField)
133  else:
134  mapper.addMapping(field.getKey())
135 
136  fluxFieldsStr = '; '.join("(%s, '%s')" % (field.getName(), field.getUnits()) for field in input_fields)
137 
138  if doConvert:
139  newSchema = mapper.getOutputSchema()
140  output = lsst.afw.table.SimpleCatalog(newSchema)
141  output.extend(catalog, mapper=mapper)
142  for field in output_fields:
143  output[field.getName()] *= 1e9
144  log.info(f"Converted refcat flux fields to nJy (name, units): {fluxFieldsStr}")
145  return output
146  else:
147  log.info(f"Found old-style refcat flux fields (name, units): {fluxFieldsStr}")
148  return None
149 
150 
152  """This is a private helper class which filters catalogs by
153  row based on the row being inside the region used to initialize
154  the class.
155 
156  Parameters
157  ----------
158  region : `lsst.sphgeom.Region`
159  The spatial region which all objects should lie within
160  """
161  def __init__(self, region):
162  self.region = region
163 
164  def __call__(self, refCat, catRegion):
165  """This call method on an instance of this class takes in a reference
166  catalog, and the region from which the catalog was generated.
167 
168  If the catalog region is entirely contained within the region used to
169  initialize this class, then all the entries in the catalog must be
170  within the region and so the whole catalog is returned.
171 
172  If the catalog region is not entirely contained, then the location for
173  each record is tested against the region used to initialize the class.
174  Records which fall inside this region are added to a new catalog, and
175  this catalog is then returned.
176 
177  Parameters
178  ---------
179  refCat : `lsst.afw.table.SourceCatalog`
180  SourceCatalog to be filtered.
181  catRegion : `lsst.sphgeom.Region`
182  Region in which the catalog was created
183  """
184  if catRegion.isWithin(self.region):
185  # no filtering needed, region completely contains refcat
186  return refCat
187 
188  filteredRefCat = type(refCat)(refCat.table)
189  for record in refCat:
190  if self.region.contains(record.getCoord().getVector()):
191  filteredRefCat.append(record)
192  return filteredRefCat
193 
194 
196  """ This class facilitates loading reference catalogs with gen 3 middleware
197 
198  The middleware preflight solver will create a list of datarefs that may
199  possibly overlap a given region. These datarefs are then used to construct
200  and instance of this class. The class instance should then be passed into
201  a task which needs reference catalogs. These tasks should then determine
202  the exact region of the sky reference catalogs will be loaded for, and
203  call a corresponding method to load the reference objects.
204  """
205  def __init__(self, dataIds, butler, config, log=None):
206  """ Constructs an instance of ReferenceObjectLoader
207 
208  Parameters
209  ----------
210  dataIds : iterable of `lsst.daf.butler.DataIds`
211  An iterable object of DataSetRefs which point to reference catalogs
212  in a gen 3 repository
213  bulter : `lsst.daf.bulter.Butler`
214  A gen 3 butler instance
215  log : `lsst.log.Log`
216  Logger object used to write out messages. If `None` (default) the default
217  lsst logger will be used
218 
219  """
220  self.dataIds = dataIds
221  self.butler = butler
223  self.config = config
224 
225  @staticmethod
226  def _makeBoxRegion(BBox, wcs, BBoxPadding):
227  outerLocalBBox = geom.Box2D(BBox)
228  innerLocalBBox = geom.Box2D(BBox)
229 
230  # Grow the bounding box to make sure the spherical geometry bbox will contain
231  # the same region, as non-padded boxes may contain different regions because of optical distortion.
232  # Also create an inner region that is sure to be inside the bbox
233  outerLocalBBox.grow(BBoxPadding)
234  innerLocalBBox.grow(-1*BBoxPadding)
235 
236  # Handle the fact that the inner bounding box shrunk to a zero sized region in at least one
237  # dimension, in which case all reference catalogs must be checked fully against the input
238  # bounding box
239  if innerLocalBBox.getDimensions() == geom.Extent2D(0, 0):
240  innerSkyRegion = sphgeom.Box()
241  else:
242  innerBoxCorners = innerLocalBBox.getCorners()
243  innerSphCorners = [wcs.pixelToSky(corner).getVector() for corner in innerBoxCorners]
244 
245  innerSkyRegion = sphgeom.ConvexPolygon(innerSphCorners)
246 
247  # Convert the corners of the box to sky coordinates
248  outerBoxCorners = outerLocalBBox.getCorners()
249  outerSphCorners = [wcs.pixelToSky(corner).getVector() for corner in outerBoxCorners]
250 
251  outerSkyRegion = sphgeom.ConvexPolygon(outerSphCorners)
252 
253  return innerSkyRegion, outerSkyRegion, innerSphCorners, outerSphCorners
254 
255  def loadPixelBox(self, bbox, wcs, filterName=None, epoch=None, photoCalib=None, bboxPadding=100):
256  """Load reference objects that are within a pixel-based rectangular region
257 
258  This algorithm works by creating a spherical box whose corners correspond
259  to the WCS converted corners of the input bounding box (possibly padded).
260  It then defines a filtering function which will look at a reference
261  objects pixel position and accept objects that lie within the specified
262  bounding box.
263 
264  The spherical box region and filtering function are passed to the generic
265  loadRegion method which will load and filter the reference objects from
266  the datastore and return a single catalog containing all reference objects
267 
268  Parameters
269  ----------
270  bbox : `lsst.geom.box2I`
271  Box which bounds a region in pixel space
272  wcs : `lsst.afw.geom.SkyWcs`
273  Wcs object defining the pixel to sky (and inverse) transform for the space
274  of pixels of the supplied bbox
275  filterName : `str`
276  Name of camera filter, or None or blank for the default filter
277  epoch : `astropy.time.Time` (optional)
278  Epoch to which to correct proper motion and parallax,
279  or None to not apply such corrections.
280  photoCalib : None
281  Deprecated and ignored, only included for api compatibility
282  bboxPadding : `int`
283  Number describing how much to pad the input bbox by (in pixels), defaults
284  to 100. This parameter is necessary because optical distortions in telescopes
285  can cause a rectangular pixel grid to map into a non "rectangular" spherical
286  region in sky coordinates. This padding is used to create a spherical
287  "rectangle", which will for sure enclose the input box. This padding is only
288  used to determine if the reference catalog for a sky patch will be loaded from
289  the data store, this function will filter out objects which lie within the
290  padded region but fall outside the input bounding box region.
291 
292  Returns
293  -------
294  referenceCatalog : `lsst.afw.table.SimpleCatalog`
295  Catalog containing reference objects inside the specified bounding box
296 
297  Raises
298  ------
299  `lsst.pex.exception.RuntimeError`
300  Raised if no reference catalogs could be found for the specified region
301 
302  `lsst.pex.exception.TypeError`
303  Raised if the loaded reference catalogs do not have matching schemas
304  """
305  innerSkyRegion, outerSkyRegion, _, _ = self._makeBoxRegion(bbox, wcs, bboxPadding)
306 
307  def _filterFunction(refCat, region):
308  # Add columns to the reference catalog relating to center positions and use afwTable
309  # to populate those columns
310  refCat = self.remapReferenceCatalogSchema(refCat, position=True)
311  afwTable.updateRefCentroids(wcs, refCat)
312  # no need to filter the catalog if it is sure that it is entirely contained in the region
313  # defined by given bbox
314  if innerSkyRegion.contains(region):
315  return refCat
316  # Create a new reference catalog, and populate it with records which fall inside the bbox
317  filteredRefCat = type(refCat)(refCat.table)
318  centroidKey = afwTable.Point2DKey(refCat.schema['centroid'])
319  for record in refCat:
320  pixCoords = record[centroidKey]
321  if bbox.contains(geom.Point2I(pixCoords)):
322  filteredRefCat.append(record)
323  return filteredRefCat
324  return self.loadRegion(outerSkyRegion, filtFunc=_filterFunction, epoch=epoch, filterName=filterName)
325 
326  def loadRegion(self, region, filtFunc=None, filterName=None, epoch=None):
327  """ Load reference objects within a specified region
328 
329  This function loads the DataIds used to construct an instance of this class
330  which intersect or are contained within the specified region. The reference
331  catalogs which intersect but are not fully contained within the input region are
332  further filtered by the specified filter function. This function will return a
333  single source catalog containing all reference objects inside the specified region.
334 
335  Parameters
336  ----------
337  region : `lsst.sphgeom.Region`
338  This can be any type that is derived from `lsst.sphgeom.region` and should
339  define the spatial region for which reference objects are to be loaded.
340  filtFunc : callable
341  This optional parameter should be a callable object that takes a reference
342  catalog and its corresponding region as parameters, filters the catalog by
343  some criteria and returns the filtered reference catalog. If the value is
344  left as the default (None) than an internal filter function is used which
345  filters according to if a reference object falls within the input region.
346  filterName : `str`
347  Name of camera filter, or None or blank for the default filter
348  epoch : `astropy.time.Time` (optional)
349  Epoch to which to correct proper motion and parallax,
350  or None to not apply such corrections.
351 
352  Returns
353  -------
354  referenceCatalog : `lsst.afw.table.SourceCatalog`
355  Catalog containing reference objects which intersect the input region,
356  filtered by the specified filter function
357 
358  Raises
359  ------
360  `lsst.pex.exception.RuntimeError`
361  Raised if no reference catalogs could be found for the specified region
362 
363  `lsst.pex.exception.TypeError`
364  Raised if the loaded reference catalogs do not have matching schemas
365 
366  """
367  regionBounding = region.getBoundingBox()
368  self.log.info("Loading reference objects from region bounded by {}, {} lat lon".format(
369  regionBounding.getLat(), regionBounding.getLon()))
370  if filtFunc is None:
371  filtFunc = _FilterCatalog(region)
372  # filter out all the regions supplied by the constructor that do not overlap
373  overlapList = []
374  for dataId in self.dataIds:
375  # SphGeom supports some objects intersecting others, but is not symmetric,
376  # try the intersect operation in both directions
377  try:
378  intersects = dataId.region.intersects(region)
379  except TypeError:
380  intersects = region.intersects(dataId.region)
381 
382  if intersects:
383  overlapList.append(dataId)
384 
385  if len(overlapList) == 0:
386  raise pexExceptions.RuntimeError("No reference tables could be found for input region")
387 
388  firstCat = self.butler.get('ref_cat', overlapList[0])
389  refCat = filtFunc(firstCat, overlapList[0].region)
390  trimmedAmount = len(firstCat) - len(refCat)
391 
392  # Load in the remaining catalogs
393  for dataId in overlapList[1:]:
394  tmpCat = self.butler.get('ref_cat', dataId)
395 
396  if tmpCat.schema != firstCat.schema:
397  raise pexExceptions.TypeError("Reference catalogs have mismatching schemas")
398 
399  filteredCat = filtFunc(tmpCat, dataId.region)
400  refCat.extend(filteredCat)
401  trimmedAmount += len(tmpCat) - len(filteredCat)
402 
403  self.log.debug(f"Trimmed {trimmedAmount} out of region objects, leaving {len(refCat)}")
404  self.log.info(f"Loaded {len(refCat)} reference objects")
405 
406  if epoch is not None and "pm_ra" in refCat.schema:
407  # check for a catalog in a non-standard format
408  if isinstance(refCat.schema["pm_ra"].asKey(), lsst.afw.table.KeyAngle):
409  applyProperMotionsImpl(self.log, refCat, epoch)
410  else:
411  self.log.warn("Catalog pm_ra field is not an Angle; not applying proper motion")
412 
413  # Verify the schema is in the correct units and has the correct version; automatically convert
414  # it with a warning if this is not the case.
415  if not hasNanojanskyFluxUnits(refCat.schema) or not getFormatVersionFromRefCat(refCat) >= 1:
416  self.log.warn("Found version 0 reference catalog with old style units in schema.")
417  self.log.warn("run `meas_algorithms/bin/convert_refcat_to_nJy.py` to convert fluxes to nJy.")
418  self.log.warn("See RFC-575 for more details.")
419  refCat = convertToNanojansky(refCat, self.log)
420 
421  expandedCat = self.remapReferenceCatalogSchema(refCat, position=True)
422 
423  # Add flux aliases
424  self.addFluxAliases(expandedCat, self.config.defaultFilter, self.config.filterMap)
425 
426  # Ensure that the returned reference catalog is continuous in memory
427  if not expandedCat.isContiguous():
428  expandedCat = refCat.copy(deep=True)
429 
430  fluxField = getRefFluxField(schema=expandedCat.schema, filterName=filterName)
431  return pipeBase.Struct(refCat=expandedCat, fluxField=fluxField)
432 
433  def loadSkyCircle(self, ctrCoord, radius, filterName=None, epoch=None):
434  """Load reference objects that lie within a circular region on the sky
435 
436  This method constructs a circular region from an input center and angular radius,
437  loads reference catalogs which are contained in or intersect the circle, and
438  filters reference catalogs which intersect down to objects which lie within
439  the defined circle.
440 
441  Parameters
442  ----------
443  ctrCoord : `lsst.geom.SpherePoint`
444  Point defining the center of the circular region
445  radius : `lsst.geom.Angle`
446  Defines the angular radius of the circular region
447  filterName : `str`
448  Name of camera filter, or None or blank for the default filter
449  epoch : `astropy.time.Time` (optional)
450  Epoch to which to correct proper motion and parallax,
451  or None to not apply such corrections.
452 
453  Returns
454  -------
455  referenceCatalog : `lsst.afw.table.SourceCatalog`
456  Catalog containing reference objects inside the specified bounding box
457 
458  Raises
459  ------
460  `lsst.pex.exception.RuntimeError`
461  Raised if no reference catalogs could be found for the specified region
462 
463  `lsst.pex.exception.TypeError`
464  Raised if the loaded reference catalogs do not have matching schemas
465 
466  """
467  centerVector = ctrCoord.getVector()
468  sphRadius = sphgeom.Angle(radius.asRadians())
469  circularRegion = sphgeom.Circle(centerVector, sphRadius)
470  return self.loadRegion(circularRegion, filterName=filterName, epoch=None)
471 
472  def joinMatchListWithCatalog(self, matchCat, sourceCat):
473  """Relink an unpersisted match list to sources and reference
474  objects.
475 
476  A match list is persisted and unpersisted as a catalog of IDs
477  produced by afw.table.packMatches(), with match metadata
478  (as returned by the astrometry tasks) in the catalog's metadata
479  attribute. This method converts such a match catalog into a match
480  list, with links to source records and reference object records.
481 
482  Parameters
483  ----------
484  matchCat : `lsst.afw.table.BaseCatalog`
485  Unpersisted packed match list.
486  ``matchCat.table.getMetadata()`` must contain match metadata,
487  as returned by the astrometry tasks.
488  sourceCat : `lsst.afw.table.SourceCatalog`
489  Source catalog. As a side effect, the catalog will be sorted
490  by ID.
491 
492  Returns
493  -------
494  matchList : `lsst.afw.table.ReferenceMatchVector`
495  Match list.
496  """
497  return joinMatchListWithCatalogImpl(self, matchCat, sourceCat)
498 
499  @classmethod
500  def getMetadataBox(cls, bbox, wcs, filterName=None, photoCalib=None, epoch=None, bboxPadding=100):
501  """Return metadata about the load
502 
503  This metadata is used for reloading the catalog (e.g., for
504  reconstituting a normalised match list.)
505 
506  Parameters
507  ----------
508  bbox : `lsst.geom.Box2I`
509  Bounding bos for the pixels
510  wcs : `lsst.afw.geom.SkyWcs
511  WCS object
512  filterName : `str` or None
513  filterName of the camera filter, or None or blank for the default filter
514  photoCalib : None
515  Deprecated, only included for api compatibility
516  epoch : `astropy.time.Time` (optional)
517  Epoch to which to correct proper motion and parallax,
518  or None to not apply such corrections.
519  bboxPadding : `int`
520  Number describing how much to pad the input bbox by (in pixels), defaults
521  to 100. This parameter is necessary because optical distortions in telescopes
522  can cause a rectangular pixel grid to map into a non "rectangular" spherical
523  region in sky coordinates. This padding is used to create a spherical
524  "rectangle", which will for sure enclose the input box. This padding is only
525  used to determine if the reference catalog for a sky patch will be loaded from
526  the data store, this function will filter out objects which lie within the
527  padded region but fall outside the input bounding box region.
528  Returns
529  -------
530  md : `lsst.daf.base.PropertyList`
531  """
532  _, _, innerCorners, outerCorners = cls._makeBoxRegion(bbox, wcs, bboxPadding)
533  md = PropertyList()
534  for box, corners in zip(("INNER", "OUTER"), (innerCorners, outerCorners)):
535  for (name, corner) in zip(("UPPER_LEFT", "UPPER_RIGHT", "LOWER_LEFT", "LOWER_RIGHT"),
536  corners):
537  md.add(f"{box}_{name}_RA", geom.SpherePoint(corner).getRa().asDegrees(), f"{box}_corner")
538  md.add(f"{box}_{name}_DEC", geom.SpherePoint(corner).getDec().asDegrees(), f"{box}_corner")
539  md.add("SMATCHV", 1, 'SourceMatchVector version number')
540  filterName = "UNKNOWN" if filterName is None else str(filterName)
541  md.add('FILTER', filterName, 'filter name for photometric data')
542  # Calling str on the epoch is a workaround for code that never worked anyway and was not used
543  # see DM-17438
544  md.add('EPOCH', "NONE" if epoch is None else str(epoch), 'Epoch (TAI MJD) for catalog')
545  return md
546 
547  @staticmethod
548  def getMetadataCircle(coord, radius, filterName, photoCalib=None, epoch=None):
549  """Return metadata about the load
550 
551  This metadata is used for reloading the catalog (e.g. for reconstituting
552  a normalized match list.)
553 
554  Parameters
555  ----------
556  coord : `lsst.geom.SpherePoint`
557  ICRS center of a circle
558  radius : `lsst.geom.angle`
559  radius of a circle
560  filterName : `str` or None
561  filterName of the camera filter, or None or blank for the default filter
562  photoCalib : None
563  Deprecated, only included for api compatibility
564  epoch : `astropy.time.Time` (optional)
565  Epoch to which to correct proper motion and parallax,
566  or None to not apply such corrections.
567 
568  Returns
569  -------
570  md : `lsst.daf.base.PropertyList`
571  """
572  md = PropertyList()
573  md.add('RA', coord.getRa().asDegrees(), 'field center in degrees')
574  md.add('DEC', coord.getDec().asDegrees(), 'field center in degrees')
575  md.add('RADIUS', radius.asDegrees(), 'field radius in degrees, minimum')
576  md.add('SMATCHV', 1, 'SourceMatchVector version number')
577  filterName = "UNKNOWN" if filterName is None else str(filterName)
578  md.add('FILTER', filterName, 'filter name for photometric data')
579  md.add('EPOCH', "NONE" if epoch is None else epoch, 'Epoch (TAI MJD) for catalog')
580  return md
581 
582  @staticmethod
583  def addFluxAliases(refCat, defaultFilter, filterReferenceMap):
584  """This function creates a new catalog containing the information of the input refCat
585  as well as added flux columns and aliases between camera and reference flux.
586 
587  Parameters
588  ----------
589  refCat : `lsst.afw.table.SimpleCatalog`
590  Catalog of reference objects
591  defaultFilter : `str`
592  Name of the default reference filter
593  filterReferenceMap : `dict` of `str`
594  Dictionary with keys corresponding to a filter name, and values which
595  correspond to the name of the reference filter.
596 
597  Returns
598  -------
599  refCat : `lsst.afw.table.SimpleCatalog`
600  Reference catalog with columns added to track reference filters
601 
602  Raises
603  ------
604  `RuntimeError`
605  If specified reference filter name is not a filter specifed as a key in the
606  reference filter map.
607  """
608  refCat = ReferenceObjectLoader.remapReferenceCatalogSchema(refCat,
609  filterNameList=filterReferenceMap.keys())
610  aliasMap = refCat.schema.getAliasMap()
611 
612  if filterReferenceMap is None:
613  filterReferenceMap = {}
614  for filterName, refFilterName in itertools.chain([(None, defaultFilter)],
615  filterReferenceMap.items()):
616  if refFilterName:
617  camFluxName = filterName + "_camFlux" if filterName is not None else "camFlux"
618  refFluxName = refFilterName + "_flux"
619  if refFluxName not in refCat.schema:
620  raise RuntimeError("Unknown reference filter %s" % (refFluxName,))
621  aliasMap.set(camFluxName, refFluxName)
622 
623  refFluxErrName = refFluxName + "Err"
624  camFluxErrName = camFluxName + "Err"
625  aliasMap.set(camFluxErrName, refFluxErrName)
626 
627  return refCat
628 
629  @staticmethod
630  def remapReferenceCatalogSchema(refCat, *, filterNameList=None, position=False, photometric=False):
631  """This function takes in a reference catalog and creates a new catalog with additional
632  columns defined the remaining function arguments.
633 
634  Parameters
635  ----------
636  refCat : `lsst.afw.table.SimpleCatalog`
637  Reference catalog to map to new catalog
638 
639  Returns
640  -------
641  expandedCat : `lsst.afw.table.SimpleCatalog`
642  Deep copy of input reference catalog with additional columns added
643  """
644  mapper = afwTable.SchemaMapper(refCat.schema, True)
645  mapper.addMinimalSchema(refCat.schema, True)
646  mapper.editOutputSchema().disconnectAliases()
647  if filterNameList:
648  for filterName in filterNameList:
649  mapper.editOutputSchema().addField(f"{filterName}_flux",
650  type=numpy.float64,
651  doc=f"flux in filter {filterName}",
652  units="Jy"
653  )
654  mapper.editOutputSchema().addField(f"{filterName}_fluxErr",
655  type=numpy.float64,
656  doc=f"flux uncertanty in filter {filterName}",
657  units="Jy"
658  )
659 
660  if position:
661  mapper.editOutputSchema().addField("centroid_x", type=float, doReplace=True)
662  mapper.editOutputSchema().addField("centroid_y", type=float, doReplace=True)
663  mapper.editOutputSchema().addField("hasCentroid", type="Flag", doReplace=True)
664  mapper.editOutputSchema().getAliasMap().set("slot_Centroid", "centroid")
665 
666  if photometric:
667  mapper.editOutputSchema().addField("photometric",
668  type="Flag",
669  doc="set if the object can be used for photometric" +
670  "calibration",
671  )
672  mapper.editOutputSchema().addField("resolved",
673  type="Flag",
674  doc="set if the object is spatially resolved"
675  )
676  mapper.editOutputSchema().addField("variable",
677  type="Flag",
678  doc="set if the object has variable brightness"
679  )
680 
681  expandedCat = afwTable.SimpleCatalog(mapper.getOutputSchema())
682  expandedCat.setMetadata(refCat.getMetadata())
683  expandedCat.extend(refCat, mapper=mapper)
684 
685  return expandedCat
686 
687 
688 def getRefFluxField(schema, filterName=None):
689  """Get the name of a flux field from a schema.
690 
691  if filterName is specified:
692  return *filterName*_camFlux if present
693  else return *filterName*_flux if present (camera filter name matches reference filter name)
694  else throw RuntimeError
695  else:
696  return camFlux, if present,
697  else throw RuntimeError
698 
699  Parameters
700  ----------
701  schema : `lsst.afw.table.Schema`
702  Reference catalog schema.
703  filterName : `str`
704  Name of camera filter.
705 
706  Returns
707  -------
708  fluxFieldName : `str`
709  Name of flux field.
710 
711  Raises
712  ------
713  RuntimeError
714  If an appropriate field is not found.
715  """
716  if not isinstance(schema, afwTable.Schema):
717  raise RuntimeError("schema=%s is not a schema" % (schema,))
718  if filterName:
719  fluxFieldList = [filterName + "_camFlux", filterName + "_flux"]
720  else:
721  fluxFieldList = ["camFlux"]
722  for fluxField in fluxFieldList:
723  if fluxField in schema:
724  return fluxField
725 
726  raise RuntimeError("Could not find flux field(s) %s" % (", ".join(fluxFieldList)))
727 
728 
729 def getRefFluxKeys(schema, filterName=None):
730  """Return keys for flux and flux error.
731 
732  Parameters
733  ----------
734  schema : `lsst.afw.table.Schema`
735  Reference catalog schema.
736  filterName : `str`
737  Name of camera filter.
738 
739  Returns
740  -------
741  keys : `tuple` of (`lsst.afw.table.Key`, `lsst.afw.table.Key`)
742  Two keys:
743 
744  - flux key
745  - flux error key, if present, else None
746 
747  Raises
748  ------
749  RuntimeError
750  If flux field not found.
751  """
752  fluxField = getRefFluxField(schema, filterName)
753  fluxErrField = fluxField + "Err"
754  fluxKey = schema[fluxField].asKey()
755  try:
756  fluxErrKey = schema[fluxErrField].asKey()
757  except Exception:
758  fluxErrKey = None
759  return (fluxKey, fluxErrKey)
760 
761 
762 class LoadReferenceObjectsConfig(pexConfig.Config):
763  pixelMargin = pexConfig.RangeField(
764  doc="Padding to add to 4 all edges of the bounding box (pixels)",
765  dtype=int,
766  default=300,
767  min=0,
768  )
769  defaultFilter = pexConfig.Field(
770  doc="Default reference catalog filter to use if filter not specified in exposure; "
771  "if blank then filter must be specified in exposure",
772  dtype=str,
773  default="",
774  )
775  filterMap = pexConfig.DictField(
776  doc="Mapping of camera filter name: reference catalog filter name; "
777  "each reference filter must exist",
778  keytype=str,
779  itemtype=str,
780  default={},
781  )
782  requireProperMotion = pexConfig.Field(
783  doc="Require that the fields needed to correct proper motion "
784  "(epoch, pm_ra and pm_dec) are present?",
785  dtype=bool,
786  default=False,
787  )
788 
789 # The following comment block adds a link to this task from the Task Documentation page.
790 
796 
797 
798 class LoadReferenceObjectsTask(pipeBase.Task, metaclass=abc.ABCMeta):
799  r"""!Abstract base class to load objects from reference catalogs
800 
801  @anchor LoadReferenceObjectsTask_
802 
803  @section meas_algorithms_loadReferenceObjects_Contents Contents
804 
805  - @ref meas_algorithms_loadReferenceObjects_Purpose
806  - @ref meas_algorithms_loadReferenceObjects_Initialize
807  - @ref meas_algorithms_loadReferenceObjects_IO
808  - @ref meas_algorithms_loadReferenceObjects_Schema
809  - @ref meas_algorithms_loadReferenceObjects_Config
810 
811  @section meas_algorithms_loadReferenceObjects_Purpose Description
812 
813  Abstract base class for tasks that load objects from a reference catalog
814  in a particular region of the sky.
815 
816  Implementations must subclass this class, override the loadSkyCircle method,
817  and will typically override the value of ConfigClass with a task-specific config class.
818 
819  @section meas_algorithms_loadReferenceObjects_Initialize Task initialisation
820 
821  @copydoc \_\_init\_\_
822 
823  @section meas_algorithms_loadReferenceObjects_IO Invoking the Task
824 
825  @copydoc loadPixelBox
826 
827  @section meas_algorithms_loadReferenceObjects_Schema Schema of the reference object catalog
828 
829  Reference object catalogs are instances of lsst.afw.table.SimpleCatalog with the following schema
830  (other fields may also be present).
831  The units use astropy quantity conventions, so a 2 suffix means squared.
832  See also makeMinimalSchema.
833  - coord: ICRS position of star on sky (an lsst.geom.SpherePoint)
834  - centroid: position of star on an exposure, if relevant (an lsst.afw.Point2D)
835  - hasCentroid: is centroid usable? (a Flag)
836  - *referenceFilterName*_flux: brightness in the specified reference catalog filter (nJy)
837  Note: you can use astropy.units to convert from AB Magnitude to nJy:
838  `u.Magnitude(value, u.ABmag).to_value(u.nJy)`
839  - *referenceFilterName*_fluxErr (optional): brightness standard deviation (nJy);
840  omitted if no data is available; possibly nan if data is available for some objects but not others
841  - camFlux: brightness in default camera filter (nJy); omitted if defaultFilter not specified
842  - camFluxErr: brightness standard deviation for default camera filter;
843  omitted if defaultFilter not specified or standard deviation not available that filter
844  - *cameraFilterName*_camFlux: brightness in specified camera filter (nJy)
845  - *cameraFilterName*_camFluxErr (optional): brightness standard deviation
846  in specified camera filter (nJy); omitted if no data is available;
847  possibly nan if data is available for some objects but not others
848  - photometric (optional): is the object usable for photometric calibration? (a Flag)
849  - resolved (optional): is the object spatially resolved? (a Flag)
850  - variable (optional): does the object have variable brightness? (a Flag)
851  - coord_raErr: uncertainty in `coord` along the direction of right ascension (radian, an Angle)
852  = uncertainty in ra * cos(dec); nan if unknown
853  - coord_decErr: uncertainty in `coord` along the direction of declination (radian, an Angle);
854  nan if unknown
855 
856  The following are optional; fields should only be present if the
857  information is available for at least some objects.
858  Numeric values are `nan` if unknown:
859  - epoch: date of observation as TAI MJD (day)
860 
861  - pm_ra: proper motion along the direction of right ascension (rad/year, an Angle) = dra/dt * cos(dec)
862  - pm_dec: proper motion along the direction of declination (rad/year, and Angle)
863  - pm_raErr: uncertainty in `pm_ra` (rad/year)
864  - pm_decErr: uncertainty in `pm_dec` (rad/year)
865  - pm_ra_dec_Cov: covariance between pm_ra and pm_dec (rad2/year2)
866  - pm_flag: set if proper motion, error or covariance is bad
867 
868  - parallax: parallax (rad, an Angle)
869  - parallaxErr: uncertainty in `parallax` (rad)
870  - parallax_flag: set if parallax value or parallaxErr is bad
871 
872  - coord_ra_pm_ra_Cov: covariance between coord_ra and pm_ra (rad2/year)
873  - coord_ra_pm_dec_Cov: covariance between coord_ra and pm_dec (rad2/year)
874  - coord_ra_parallax_Cov: covariance between coord_ra and parallax (rad2/year)
875  - coord_dec_pm_ra_Cov: covariance between coord_dec and pm_ra (rad2/year)
876  - coord_dec_pm_dec_Cov: covariance between coord_dec and pm_dec (rad2/year)
877  - coord_dec_parallax_Cov: covariance between coord_dec and parallax (rad2/year)
878  - pm_ra_parallax_Cov: covariance between pm_ra and parallax (rad2/year)
879  - pm_dec_parallax_Cov: covariance between pm_dec and parallax (rad2/year)
880 
881  @section meas_algorithms_loadReferenceObjects_Config Configuration parameters
882 
883  See @ref LoadReferenceObjectsConfig for a base set of configuration parameters.
884  Most subclasses will add configuration variables.
885  """
886  ConfigClass = LoadReferenceObjectsConfig
887  _DefaultName = "LoadReferenceObjects"
888 
889  def __init__(self, butler=None, *args, **kwargs):
890  """Construct a LoadReferenceObjectsTask
891 
892  Parameters
893  ----------
894  butler : `lsst.daf.persistence.Butler`
895  Data butler, for access reference catalogs.
896  """
897  pipeBase.Task.__init__(self, *args, **kwargs)
898  self.butler = butler
899 
900  @pipeBase.timeMethod
901  def loadPixelBox(self, bbox, wcs, filterName=None, photoCalib=None, epoch=None):
902  """Load reference objects that overlap a rectangular pixel region.
903 
904  Parameters
905  ----------
906  bbox : `lsst.geom.Box2I` or `lsst.geom.Box2D`
907  Bounding box for pixels.
908  wcs : `lsst.afw.geom.SkyWcs`
909  WCS; used to convert pixel positions to sky coordinates
910  and vice-versa.
911  filterName : `str`
912  Name of filter, or `None` or `""` for the default filter.
913  This is used for flux values in case we have flux limits
914  (which are not yet implemented).
915  photoCalib : `lsst.afw.image.PhotoCalib` (optional)
916  Calibration, or `None` if unknown.
917  epoch : `astropy.time.Time` (optional)
918  Epoch to which to correct proper motion and parallax,
919  or None to not apply such corrections.
920 
921  Returns
922  -------
923  results : `lsst.pipe.base.Struct`
924  A Struct containing the following fields:
925  refCat : `lsst.afw.catalog.SimpleCatalog`
926  A catalog of reference objects with the standard
927  schema, as documented in the main doc string for
928  `LoadReferenceObjects`.
929  The catalog is guaranteed to be contiguous.
930  fluxField : `str`
931  Name of flux field for specified `filterName`.
932 
933  Notes
934  -----
935  The search algorithm works by searching in a region in sky
936  coordinates whose center is the center of the bbox and radius
937  is large enough to just include all 4 corners of the bbox.
938  Stars that lie outside the bbox are then trimmed from the list.
939  """
940  circle = self._calculateCircle(bbox, wcs)
941 
942  # find objects in circle
943  self.log.info("Loading reference objects using center %s and radius %s deg" %
944  (circle.coord, circle.radius.asDegrees()))
945  loadRes = self.loadSkyCircle(circle.coord, circle.radius, filterName)
946  refCat = loadRes.refCat
947  numFound = len(refCat)
948 
949  # trim objects outside bbox
950  refCat = self._trimToBBox(refCat=refCat, bbox=circle.bbox, wcs=wcs)
951  numTrimmed = numFound - len(refCat)
952  self.log.debug("trimmed %d out-of-bbox objects, leaving %d", numTrimmed, len(refCat))
953  self.log.info("Loaded %d reference objects", len(refCat))
954 
955  # make sure catalog is contiguous
956  if not refCat.isContiguous():
957  loadRes.refCat = refCat.copy(deep=True)
958 
959  return loadRes
960 
961  @abc.abstractmethod
962  def loadSkyCircle(self, ctrCoord, radius, filterName=None, epoch=None):
963  """Load reference objects that overlap a circular sky region.
964 
965  Parameters
966  ----------
967  ctrCoord : `lsst.geom.SpherePoint`
968  ICRS center of search region.
969  radius : `lsst.geom.Angle`
970  Radius of search region.
971  filterName : `str` (optional)
972  Name of filter, or `None` or `""` for the default filter.
973  This is used for flux values in case we have flux limits
974  (which are not yet implemented).
975  epoch : `astropy.time.Time` (optional)
976  Epoch to which to correct proper motion and parallax,
977  or None to not apply such corrections.
978 
979  Returns
980  -------
981  results : `lsst.pipe.base.Struct`
982  A Struct containing the following fields:
983  refCat : `lsst.afw.catalog.SimpleCatalog`
984  A catalog of reference objects with the standard
985  schema, as documented in the main doc string for
986  `LoadReferenceObjects`.
987  The catalog is guaranteed to be contiguous.
988  fluxField : `str`
989  Name of flux field for specified `filterName`.
990 
991  Notes
992  -----
993  Note that subclasses are responsible for performing the proper motion
994  correction, since this is the lowest-level interface for retrieving
995  the catalog.
996  """
997  return
998 
999  @staticmethod
1000  def _trimToBBox(refCat, bbox, wcs):
1001  """Remove objects outside a given pixel bounding box and set
1002  centroid and hasCentroid fields.
1003 
1004  Parameters
1005  ----------
1006  refCat : `lsst.afw.table.SimpleCatalog`
1007  A catalog of objects. The schema must include fields
1008  "coord", "centroid" and "hasCentroid".
1009  The "coord" field is read.
1010  The "centroid" and "hasCentroid" fields are set.
1011  bbox : `lsst.geom.Box2D`
1012  Pixel region
1013  wcs : `lsst.afw.geom.SkyWcs`
1014  WCS; used to convert sky coordinates to pixel positions.
1015 
1016  @return a catalog of reference objects in bbox, with centroid and hasCentroid fields set
1017  """
1018  afwTable.updateRefCentroids(wcs, refCat)
1019  centroidKey = afwTable.Point2DKey(refCat.schema["centroid"])
1020  retStarCat = type(refCat)(refCat.table)
1021  for star in refCat:
1022  point = star.get(centroidKey)
1023  if bbox.contains(point):
1024  retStarCat.append(star)
1025  return retStarCat
1026 
1027  def _addFluxAliases(self, schema):
1028  """Add aliases for camera filter fluxes to the schema.
1029 
1030  If self.config.defaultFilter then adds these aliases:
1031  camFlux: <defaultFilter>_flux
1032  camFluxErr: <defaultFilter>_fluxErr, if the latter exists
1033 
1034  For each camFilter: refFilter in self.config.filterMap adds these aliases:
1035  <camFilter>_camFlux: <refFilter>_flux
1036  <camFilter>_camFluxErr: <refFilter>_fluxErr, if the latter exists
1037 
1038  Parameters
1039  ----------
1040  schema : `lsst.afw.table.Schema`
1041  Schema for reference catalog.
1042 
1043  Throws
1044  ------
1045  RuntimeError
1046  If any reference flux field is missing from the schema.
1047  """
1048  aliasMap = schema.getAliasMap()
1049 
1050  def addAliasesForOneFilter(filterName, refFilterName):
1051  """Add aliases for a single filter
1052 
1053  Parameters
1054  ----------
1055  filterName : `str` (optional)
1056  Camera filter name. The resulting alias name is
1057  <filterName>_camFlux, or simply "camFlux" if `filterName`
1058  is `None` or `""`.
1059  refFilterName : `str`
1060  Reference catalog filter name; the field
1061  <refFilterName>_flux must exist.
1062  """
1063  camFluxName = filterName + "_camFlux" if filterName is not None else "camFlux"
1064  refFluxName = refFilterName + "_flux"
1065  if refFluxName not in schema:
1066  raise RuntimeError("Unknown reference filter %s" % (refFluxName,))
1067  aliasMap.set(camFluxName, refFluxName)
1068  refFluxErrName = refFluxName + "Err"
1069  if refFluxErrName in schema:
1070  camFluxErrName = camFluxName + "Err"
1071  aliasMap.set(camFluxErrName, refFluxErrName)
1072 
1073  if self.config.defaultFilter:
1074  addAliasesForOneFilter(None, self.config.defaultFilter)
1075 
1076  for filterName, refFilterName in self.config.filterMap.items():
1077  addAliasesForOneFilter(filterName, refFilterName)
1078 
1079  @staticmethod
1080  def makeMinimalSchema(filterNameList, *, addCentroid=True,
1081  addIsPhotometric=False, addIsResolved=False,
1082  addIsVariable=False, coordErrDim=2,
1083  addProperMotion=False, properMotionErrDim=2,
1084  addParallax=False, addParallaxErr=True):
1085  """Make a standard schema for reference object catalogs.
1086 
1087  Parameters
1088  ----------
1089  filterNameList : `list` of `str`
1090  List of filter names. Used to create <filterName>_flux fields.
1091  addIsPhotometric : `bool`
1092  If True then add field "photometric".
1093  addIsResolved : `bool`
1094  If True then add field "resolved".
1095  addIsVariable : `bool`
1096  If True then add field "variable".
1097  coordErrDim : `int`
1098  Number of coord error fields; must be one of 0, 2, 3:
1099 
1100  - If 2 or 3: add fields "coord_raErr" and "coord_decErr".
1101  - If 3: also add field "coord_radecErr".
1102  addProperMotion : `bool`
1103  If True add fields "epoch", "pm_ra", "pm_dec" and "pm_flag".
1104  properMotionErrDim : `int`
1105  Number of proper motion error fields; must be one of 0, 2, 3;
1106  ignored if addProperMotion false:
1107  - If 2 or 3: add fields "pm_raErr" and "pm_decErr".
1108  - If 3: also add field "pm_radecErr".
1109  addParallax : `bool`
1110  If True add fields "epoch", "parallax", "parallaxErr"
1111  and "parallax_flag".
1112  addParallaxErr : `bool`
1113  If True add field "parallaxErr"; ignored if addParallax false.
1114 
1115  Returns
1116  -------
1117  schema : `lsst.afw.table.Schema`
1118  Schema for reference catalog, an
1119  `lsst.afw.table.SimpleCatalog`.
1120 
1121  Notes
1122  -----
1123  Reference catalogs support additional covariances, such as
1124  covariance between RA and proper motion in declination,
1125  that are not supported by this method, but can be added after
1126  calling this method.
1127  """
1128  schema = afwTable.SimpleTable.makeMinimalSchema()
1129  if addCentroid:
1130  afwTable.Point2DKey.addFields(
1131  schema,
1132  "centroid",
1133  "centroid on an exposure, if relevant",
1134  "pixel",
1135  )
1136  schema.addField(
1137  field="hasCentroid",
1138  type="Flag",
1139  doc="is position known?",
1140  )
1141  for filterName in filterNameList:
1142  schema.addField(
1143  field="%s_flux" % (filterName,),
1144  type=numpy.float64,
1145  doc="flux in filter %s" % (filterName,),
1146  units="nJy",
1147  )
1148  for filterName in filterNameList:
1149  schema.addField(
1150  field="%s_fluxErr" % (filterName,),
1151  type=numpy.float64,
1152  doc="flux uncertainty in filter %s" % (filterName,),
1153  units="nJy",
1154  )
1155  if addIsPhotometric:
1156  schema.addField(
1157  field="photometric",
1158  type="Flag",
1159  doc="set if the object can be used for photometric calibration",
1160  )
1161  if addIsResolved:
1162  schema.addField(
1163  field="resolved",
1164  type="Flag",
1165  doc="set if the object is spatially resolved",
1166  )
1167  if addIsVariable:
1168  schema.addField(
1169  field="variable",
1170  type="Flag",
1171  doc="set if the object has variable brightness",
1172  )
1173  if coordErrDim not in (0, 2, 3):
1174  raise ValueError("coordErrDim={}; must be (0, 2, 3)".format(coordErrDim))
1175  if coordErrDim > 0:
1176  afwTable.CovarianceMatrix2fKey.addFields(
1177  schema=schema,
1178  prefix="coord",
1179  names=["ra", "dec"],
1180  units=["rad", "rad"],
1181  diagonalOnly=(coordErrDim == 2),
1182  )
1183 
1184  if addProperMotion or addParallax:
1185  schema.addField(
1186  field="epoch",
1187  type=numpy.float64,
1188  doc="date of observation (TAI, MJD)",
1189  units="day",
1190  )
1191 
1192  if addProperMotion:
1193  schema.addField(
1194  field="pm_ra",
1195  type="Angle",
1196  doc="proper motion in the right ascension direction = dra/dt * cos(dec)",
1197  units="rad/year",
1198  )
1199  schema.addField(
1200  field="pm_dec",
1201  type="Angle",
1202  doc="proper motion in the declination direction",
1203  units="rad/year",
1204  )
1205  if properMotionErrDim not in (0, 2, 3):
1206  raise ValueError("properMotionErrDim={}; must be (0, 2, 3)".format(properMotionErrDim))
1207  if properMotionErrDim > 0:
1208  afwTable.CovarianceMatrix2fKey.addFields(
1209  schema=schema,
1210  prefix="pm",
1211  names=["ra", "dec"],
1212  units=["rad/year", "rad/year"],
1213  diagonalOnly=(properMotionErrDim == 2),
1214  )
1215  schema.addField(
1216  field="pm_flag",
1217  type="Flag",
1218  doc="Set if proper motion or proper motion error is bad",
1219  )
1220 
1221  if addParallax:
1222  schema.addField(
1223  field="parallax",
1224  type=numpy.float64,
1225  doc="parallax",
1226  units="rad",
1227  )
1228  if addParallaxErr:
1229  schema.addField(
1230  field="parallaxErr",
1231  type=numpy.float64,
1232  doc="uncertainty in parallax",
1233  units="rad",
1234  )
1235  schema.addField(
1236  field="parallax_flag",
1237  type="Flag",
1238  doc="Set if parallax or parallax error is bad",
1239  )
1240  return schema
1241 
1242  def _calculateCircle(self, bbox, wcs):
1243  """Compute on-sky center and radius of search region.
1244 
1245  Parameters
1246  ----------
1247  bbox : `lsst.geom.Box2I` or `lsst.geom.Box2D`
1248  Pixel bounding box.
1249  wcs : `lsst.afw.geom.SkyWcs`
1250  WCS; used to convert pixel positions to sky coordinates.
1251 
1252  Returns
1253  -------
1254  results : `lsst.pipe.base.Struct`
1255  A Struct containing:
1256 
1257  - coord : `lsst.geom.SpherePoint`
1258  ICRS center of the search region.
1259  - radius : `lsst.geom.Angle`
1260  Radius of the search region.
1261  - bbox : `lsst.geom.Box2D`
1262  Bounding box used to compute the circle.
1263  """
1264  bbox = lsst.geom.Box2D(bbox) # make sure bbox is double and that we have a copy
1265  bbox.grow(self.config.pixelMargin)
1266  coord = wcs.pixelToSky(bbox.getCenter())
1267  radius = max(coord.separation(wcs.pixelToSky(pp)) for pp in bbox.getCorners())
1268  return pipeBase.Struct(coord=coord, radius=radius, bbox=bbox)
1269 
1270  def getMetadataBox(self, bbox, wcs, filterName=None, photoCalib=None, epoch=None):
1271  """Return metadata about the load.
1272 
1273  This metadata is used for reloading the catalog (e.g., for
1274  reconstituting a normalised match list.
1275 
1276  Parameters
1277  ----------
1278  bbox : `lsst.geom.Box2I` or `lsst.geom.Box2D`
1279  Pixel bounding box.
1280  wcs : `lsst.afw.geom.SkyWcs`
1281  WCS; used to convert pixel positions to sky coordinates.
1282  filterName : `str`
1283  Name of camera filter, or `None` or `""` for the default
1284  filter.
1285  photoCalib : `lsst.afw.image.PhotoCalib` (optional)
1286  Calibration, or `None` if unknown.
1287  epoch : `astropy.time.Time` (optional)
1288  Epoch to which to correct proper motion and parallax,
1289  or None to not apply such corrections.
1290 
1291  Returns
1292  -------
1293  metadata : lsst.daf.base.PropertyList
1294  Metadata about the load.
1295  """
1296  circle = self._calculateCircle(bbox, wcs)
1297  return self.getMetadataCircle(circle.coord, circle.radius, filterName, photoCalib)
1298 
1299  def getMetadataCircle(self, coord, radius, filterName, photoCalib=None, epoch=None):
1300  """Return metadata about the load.
1301 
1302  This metadata is used for reloading the catalog (e.g., for
1303  reconstituting a normalised match list.
1304 
1305  Parameters
1306  ----------
1307  coord : `lsst.geom.SpherePoint`
1308  ICRS center of the search region.
1309  radius : `lsst.geom.Angle`
1310  Radius of the search region.
1311  filterName : `str`
1312  Name of camera filter, or `None` or `""` for the default
1313  filter.
1314  photoCalib : `lsst.afw.image.PhotoCalib` (optional)
1315  Calibration, or `None` if unknown.
1316  epoch : `astropy.time.Time` (optional)
1317  Epoch to which to correct proper motion and parallax,
1318  or None to not apply such corrections.
1319 
1320  Returns
1321  -------
1322  metadata : lsst.daf.base.PropertyList
1323  Metadata about the load
1324  """
1325  md = PropertyList()
1326  md.add('RA', coord.getRa().asDegrees(), 'field center in degrees')
1327  md.add('DEC', coord.getDec().asDegrees(), 'field center in degrees')
1328  md.add('RADIUS', radius.asDegrees(), 'field radius in degrees, minimum')
1329  md.add('SMATCHV', 1, 'SourceMatchVector version number')
1330  filterName = "UNKNOWN" if filterName is None else str(filterName)
1331  md.add('FILTER', filterName, 'filter name for photometric data')
1332  md.add('EPOCH', "NONE" if epoch is None else epoch, 'Epoch (TAI MJD) for catalog')
1333  return md
1334 
1335  def joinMatchListWithCatalog(self, matchCat, sourceCat):
1336  """Relink an unpersisted match list to sources and reference
1337  objects.
1338 
1339  A match list is persisted and unpersisted as a catalog of IDs
1340  produced by afw.table.packMatches(), with match metadata
1341  (as returned by the astrometry tasks) in the catalog's metadata
1342  attribute. This method converts such a match catalog into a match
1343  list, with links to source records and reference object records.
1344 
1345  Parameters
1346  ----------
1347  matchCat : `lsst.afw.table.BaseCatalog`
1348  Unperisted packed match list.
1349  ``matchCat.table.getMetadata()`` must contain match metadata,
1350  as returned by the astrometry tasks.
1351  sourceCat : `lsst.afw.table.SourceCatalog`
1352  Source catalog. As a side effect, the catalog will be sorted
1353  by ID.
1354 
1355  Returns
1356  -------
1357  matchList : `lsst.afw.table.ReferenceMatchVector`
1358  Match list.
1359  """
1360  return joinMatchListWithCatalogImpl(self, matchCat, sourceCat)
1361 
1362  def applyProperMotions(self, catalog, epoch):
1363  """Apply proper motion correction to a reference catalog.
1364 
1365  Adjust position and position error in the ``catalog``
1366  for proper motion to the specified ``epoch``,
1367  modifying the catalong in place.
1368 
1369  Parameters
1370  ----------
1371  catalog : `lsst.afw.table.SimpleCatalog`
1372  Catalog of positions, containing:
1373 
1374  - Coordinates, retrieved by the table's coordinate key.
1375  - ``coord_raErr`` : Error in Right Ascension (rad).
1376  - ``coord_decErr`` : Error in Declination (rad).
1377  - ``pm_ra`` : Proper motion in Right Ascension (rad/yr,
1378  East positive)
1379  - ``pm_raErr`` : Error in ``pm_ra`` (rad/yr), optional.
1380  - ``pm_dec`` : Proper motion in Declination (rad/yr,
1381  North positive)
1382  - ``pm_decErr`` : Error in ``pm_dec`` (rad/yr), optional.
1383  - ``epoch`` : Mean epoch of object (an astropy.time.Time)
1384  epoch : `astropy.time.Time` (optional)
1385  Epoch to which to correct proper motion and parallax,
1386  or None to not apply such corrections.
1387  """
1388  if ("epoch" not in catalog.schema or "pm_ra" not in catalog.schema or "pm_dec" not in catalog.schema):
1389  if self.config.requireProperMotion:
1390  raise RuntimeError("Proper motion correction required but not available from catalog")
1391  self.log.warn("Proper motion correction not available from catalog")
1392  return
1393  applyProperMotionsImpl(self.log, catalog, epoch)
1394 
1395 
1396 def joinMatchListWithCatalogImpl(refObjLoader, matchCat, sourceCat):
1397  """Relink an unpersisted match list to sources and reference
1398  objects.
1399 
1400  A match list is persisted and unpersisted as a catalog of IDs
1401  produced by afw.table.packMatches(), with match metadata
1402  (as returned by the astrometry tasks) in the catalog's metadata
1403  attribute. This method converts such a match catalog into a match
1404  list, with links to source records and reference object records.
1405 
1406  Parameters
1407  ----------
1408  refObjLoader
1409  Reference object loader to use in getting reference objects
1410  matchCat : `lsst.afw.table.BaseCatalog`
1411  Unperisted packed match list.
1412  ``matchCat.table.getMetadata()`` must contain match metadata,
1413  as returned by the astrometry tasks.
1414  sourceCat : `lsst.afw.table.SourceCatalog`
1415  Source catalog. As a side effect, the catalog will be sorted
1416  by ID.
1417 
1418  Returns
1419  -------
1420  matchList : `lsst.afw.table.ReferenceMatchVector`
1421  Match list.
1422  """
1423  matchmeta = matchCat.table.getMetadata()
1424  version = matchmeta.getInt('SMATCHV')
1425  if version != 1:
1426  raise ValueError('SourceMatchVector version number is %i, not 1.' % version)
1427  filterName = matchmeta.getString('FILTER').strip()
1428  try:
1429  epoch = matchmeta.getDouble('EPOCH')
1431  epoch = None # Not present, or not correct type means it's not set
1432  if 'RADIUS' in matchmeta:
1433  # This is a circle style metadata, call loadSkyCircle
1434  ctrCoord = lsst.geom.SpherePoint(matchmeta.getDouble('RA'),
1435  matchmeta.getDouble('DEC'), lsst.geom.degrees)
1436  rad = matchmeta.getDouble('RADIUS') * lsst.geom.degrees
1437  refCat = refObjLoader.loadSkyCircle(ctrCoord, rad, filterName, epoch=epoch).refCat
1438  elif "INNER_UPPER_LEFT_RA" in matchmeta:
1439  # This is the sky box type (only triggers in the LoadReferenceObject class, not task)
1440  # Only the outer box is required to be loaded to get the maximum region, all filtering
1441  # will be done by the unpackMatches function, and no spatial filtering needs to be done
1442  # by the refObjLoader
1443  box = []
1444  for place in ("UPPER_LEFT", "UPPER_RIGHT", "LOWER_LEFT", "LOWER_RIGHT"):
1445  coord = lsst.geom.SpherePoint(matchmeta.getDouble(f"OUTER_{place}_RA"),
1446  matchmeta.getDouble(f"OUTER_{place}_DEC"),
1447  lsst.geom.degrees).getVector()
1448  box.append(coord)
1449  outerBox = sphgeom.ConvexPolygon(box)
1450  refCat = refObjLoader.loadRegion(outerBox, filterName=filterName, epoch=epoch).refCat
1451 
1452  refCat.sort()
1453  sourceCat.sort()
1454  return afwTable.unpackMatches(matchCat, refCat, sourceCat)
1455 
1456 
1457 def applyProperMotionsImpl(log, catalog, epoch):
1458  """Apply proper motion correction to a reference catalog.
1459 
1460  Adjust position and position error in the ``catalog``
1461  for proper motion to the specified ``epoch``,
1462  modifying the catalong in place.
1463 
1464  Parameters
1465  ----------
1466  log : `lsst.log.log`
1467  log object to write to
1468  catalog : `lsst.afw.table.SimpleCatalog`
1469  Catalog of positions, containing:
1470 
1471  - Coordinates, retrieved by the table's coordinate key.
1472  - ``coord_raErr`` : Error in Right Ascension (rad).
1473  - ``coord_decErr`` : Error in Declination (rad).
1474  - ``pm_ra`` : Proper motion in Right Ascension (rad/yr,
1475  East positive)
1476  - ``pm_raErr`` : Error in ``pm_ra`` (rad/yr), optional.
1477  - ``pm_dec`` : Proper motion in Declination (rad/yr,
1478  North positive)
1479  - ``pm_decErr`` : Error in ``pm_dec`` (rad/yr), optional.
1480  - ``epoch`` : Mean epoch of object (an astropy.time.Time)
1481  epoch : `astropy.time.Time` (optional)
1482  Epoch to which to correct proper motion and parallax,
1483  or None to not apply such corrections.
1484  """
1485  if "epoch" not in catalog.schema or "pm_ra" not in catalog.schema or "pm_dec" not in catalog.schema:
1486  log.warn("Proper motion correction not available from catalog")
1487  return
1488  if not catalog.isContiguous():
1489  raise RuntimeError("Catalog must be contiguous")
1490  catEpoch = astropy.time.Time(catalog["epoch"], scale="tai", format="mjd")
1491  log.debug("Correcting reference catalog for proper motion to %r", epoch)
1492  # Use `epoch.tai` to make sure the time difference is in TAI
1493  timeDiffsYears = (epoch.tai - catEpoch).to(astropy.units.yr).value
1494  coordKey = catalog.table.getCoordKey()
1495  # Compute the offset of each object due to proper motion
1496  # as components of the arc of a great circle along RA and Dec
1497  pmRaRad = catalog["pm_ra"]
1498  pmDecRad = catalog["pm_dec"]
1499  offsetsRaRad = pmRaRad*timeDiffsYears
1500  offsetsDecRad = pmDecRad*timeDiffsYears
1501  # Compute the corresponding bearing and arc length of each offset
1502  # due to proper motion, and apply the offset
1503  # The factor of 1e6 for computing bearing is intended as
1504  # a reasonable scale for typical values of proper motion
1505  # in order to avoid large errors for small values of proper motion;
1506  # using the offsets is another option, but it can give
1507  # needlessly large errors for short duration
1508  offsetBearingsRad = numpy.arctan2(pmDecRad*1e6, pmRaRad*1e6)
1509  offsetAmountsRad = numpy.hypot(offsetsRaRad, offsetsDecRad)
1510  for record, bearingRad, amountRad in zip(catalog, offsetBearingsRad, offsetAmountsRad):
1511  record.set(coordKey,
1512  record.get(coordKey).offset(bearing=bearingRad*lsst.geom.radians,
1513  amount=amountRad*lsst.geom.radians))
1514  # Increase error in RA and Dec based on error in proper motion
1515  if "coord_raErr" in catalog.schema:
1516  catalog["coord_raErr"] = numpy.hypot(catalog["coord_raErr"],
1517  catalog["pm_raErr"]*timeDiffsYears)
1518  if "coord_decErr" in catalog.schema:
1519  catalog["coord_decErr"] = numpy.hypot(catalog["coord_decErr"],
1520  catalog["pm_decErr"]*timeDiffsYears)
def getMetadataCircle(self, coord, radius, filterName, photoCalib=None, epoch=None)
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:739
Defines the fields and offsets for a table.
Definition: Schema.h:50
def loadPixelBox(self, bbox, wcs, filterName=None, epoch=None, photoCalib=None, bboxPadding=100)
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
def addFluxAliases(refCat, defaultFilter, filterReferenceMap)
static Schema makeMinimalSchema()
Return a minimal schema for Simple tables and records.
Definition: Simple.h:140
A floating-point coordinate rectangle geometry.
Definition: Box.h:305
def loadSkyCircle(self, ctrCoord, radius, filterName=None, epoch=None)
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
def getMetadataBox(self, bbox, wcs, filterName=None, photoCalib=None, epoch=None)
daf::base::PropertySet * set
Definition: fits.cc:884
def getMetadataCircle(coord, radius, filterName, photoCalib=None, epoch=None)
Box represents a rectangle in spherical coordinate space that contains its boundary.
Definition: Box.h:54
void updateRefCentroids(geom::SkyWcs const &wcs, ReferenceCollection &refList)
Update centroids in a collection of reference objects.
Definition: wcsUtils.cc:73
def convertToNanojansky(catalog, log, doConvert=True)
Definition: Log.h:691
def loadPixelBox(self, bbox, wcs, filterName=None, photoCalib=None, epoch=None)
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
table::Key< int > to
table::Key< int > type
Definition: Detector.cc:167
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:63
def loadRegion(self, region, filtFunc=None, filterName=None, epoch=None)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
A description of a field in a table.
Definition: Field.h:24
Abstract base class to load objects from reference catalogs.
int max
def makeMinimalSchema(filterNameList, addCentroid=True, addIsPhotometric=False, addIsResolved=False, addIsVariable=False, coordErrDim=2, addProperMotion=False, properMotionErrDim=2, addParallax=False, addParallaxErr=True)
Circle is a circular region on the unit sphere that contains its boundary.
Definition: Circle.h:46
ConvexPolygon is a closed convex polygon on the unit sphere.
Definition: ConvexPolygon.h:57
def joinMatchListWithCatalogImpl(refObjLoader, matchCat, sourceCat)
Angle represents an angle in radians.
Definition: Angle.h:43
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
def loadSkyCircle(self, ctrCoord, radius, filterName=None, epoch=None)
template SourceMatchVector unpackMatches(BaseCatalog const &, SourceCatalog const &, SourceCatalog const &)
def getMetadataBox(cls, bbox, wcs, filterName=None, photoCalib=None, epoch=None, bboxPadding=100)
Reports errors from accepting an object of an unexpected or inappropriate type.
Definition: Runtime.h:167
def remapReferenceCatalogSchema(refCat, filterNameList=None, position=False, photometric=False)
bool strip
Definition: fits.cc:883
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104