LSSTApplications  19.0.0-16-gf391218+0d4bdb7e99,20.0.0-11-g973f35b+fc3da11c03,20.0.0-18-g2b8c1f54+f8fc1aa9fb,20.0.0-2-g103fe59+1a747c5cf5,20.0.0-2-g1367e85+b8fe48c8a0,20.0.0-2-g7f82c8f+cb8155b8e8,20.0.0-2-g8dde007+aaa4e16c0f,20.0.0-2-ga326454+cb8155b8e8,20.0.0-2-ga51b5d4+6432c6fd8d,20.0.0-2-gde069b7+44dbdb3492,20.0.0-2-gfc62afb+b8fe48c8a0,20.0.0-20-g54e6697de+ce03d725a7,20.0.0-22-g54e2caa+e2d4a7822a,20.0.0-25-g5eafb0f+f4a3e7a5aa,20.0.0-28-g73474ed+96410ea1f8,20.0.0-3-g5242d73+b8fe48c8a0,20.0.0-3-ga63a54e+af87b3138b,20.0.0-30-g597c966+cf8eea114e,20.0.0-4-g2909d54+37efb4748c,20.0.0-4-g35ce6d5+f4a3e7a5aa,20.0.0-4-g45278ab+d529cf1a41,20.0.0-4-g4bc9b9f+a7376eab67,20.0.0-4-g66bcc37+f4a3e7a5aa,20.0.0-4-ge17e5af+b8fe48c8a0,20.0.0-4-gecfae73+77584f1b13,20.0.0-4-gf910f65+37efb4748c,20.0.0-46-gd70f10e1+f17cbaf3d1,20.0.0-5-g8f08a60+4fac22bdb7,20.0.0-5-ga885a99+c1363b8d99,20.0.0-5-gc738bc1+47717dd73f,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-gd222c45+88b21af515,20.0.0-6-gdef485e+46cd8289a4,20.0.0-7-gaa929c8+96410ea1f8,20.0.0-74-g0218c7a+939693069d,20.0.0-8-g5dd23de+1834535a4e,20.0.0-8-ge712728+fb4ad641b9,20.0.0-9-g5051ac2+d529cf1a41,w.2020.48-1-gdf601d2
LSSTDataManagementBasePackage
Classes | Functions
lsst.meas.algorithms.loadReferenceObjects Namespace Reference

Classes

class  _FilterCatalog
 
class  LoadReferenceObjectsConfig
 
class  LoadReferenceObjectsTask
 Abstract base class to load objects from reference catalogs. More...
 
class  ReferenceObjectLoader
 

Functions

def isOldFluxField (name, units)
 
def hasNanojanskyFluxUnits (schema)
 
def getFormatVersionFromRefCat (refCat)
 
def convertToNanojansky (catalog, log, doConvert=True)
 
def getRefFluxField (schema, filterName=None)
 
def getRefFluxKeys (schema, filterName=None)
 
def joinMatchListWithCatalogImpl (refObjLoader, matchCat, sourceCat)
 
def applyProperMotionsImpl (log, catalog, epoch)
 

Function Documentation

◆ applyProperMotionsImpl()

def lsst.meas.algorithms.loadReferenceObjects.applyProperMotionsImpl (   log,
  catalog,
  epoch 
)
Apply proper motion correction to a reference catalog.

Adjust position and position error in the ``catalog``
for proper motion to the specified ``epoch``,
modifying the catalog in place.

Parameters
----------
log : `lsst.log.Log`
    Log object to write to.
catalog : `lsst.afw.table.SimpleCatalog`
    Catalog of positions, containing:

    - Coordinates, retrieved by the table's coordinate key.
    - ``coord_raErr`` : Error in Right Ascension (rad).
    - ``coord_decErr`` : Error in Declination (rad).
    - ``pm_ra`` : Proper motion in Right Ascension (rad/yr,
        East positive)
    - ``pm_raErr`` : Error in ``pm_ra`` (rad/yr), optional.
    - ``pm_dec`` : Proper motion in Declination (rad/yr,
        North positive)
    - ``pm_decErr`` : Error in ``pm_dec`` (rad/yr), optional.
    - ``epoch`` : Mean epoch of object (an astropy.time.Time)
epoch : `astropy.time.Time`
    Epoch to which to correct proper motion.

Definition at line 1494 of file loadReferenceObjects.py.

1494 def applyProperMotionsImpl(log, catalog, epoch):
1495  """Apply proper motion correction to a reference catalog.
1496 
1497  Adjust position and position error in the ``catalog``
1498  for proper motion to the specified ``epoch``,
1499  modifying the catalog in place.
1500 
1501  Parameters
1502  ----------
1503  log : `lsst.log.Log`
1504  Log object to write to.
1505  catalog : `lsst.afw.table.SimpleCatalog`
1506  Catalog of positions, containing:
1507 
1508  - Coordinates, retrieved by the table's coordinate key.
1509  - ``coord_raErr`` : Error in Right Ascension (rad).
1510  - ``coord_decErr`` : Error in Declination (rad).
1511  - ``pm_ra`` : Proper motion in Right Ascension (rad/yr,
1512  East positive)
1513  - ``pm_raErr`` : Error in ``pm_ra`` (rad/yr), optional.
1514  - ``pm_dec`` : Proper motion in Declination (rad/yr,
1515  North positive)
1516  - ``pm_decErr`` : Error in ``pm_dec`` (rad/yr), optional.
1517  - ``epoch`` : Mean epoch of object (an astropy.time.Time)
1518  epoch : `astropy.time.Time`
1519  Epoch to which to correct proper motion.
1520  """
1521  if "epoch" not in catalog.schema or "pm_ra" not in catalog.schema or "pm_dec" not in catalog.schema:
1522  log.warn("Proper motion correction not available from catalog")
1523  return
1524  if not catalog.isContiguous():
1525  raise RuntimeError("Catalog must be contiguous")
1526  catEpoch = astropy.time.Time(catalog["epoch"], scale="tai", format="mjd")
1527  log.info("Correcting reference catalog for proper motion to %r", epoch)
1528  # Use `epoch.tai` to make sure the time difference is in TAI
1529  timeDiffsYears = (epoch.tai - catEpoch).to(astropy.units.yr).value
1530  coordKey = catalog.table.getCoordKey()
1531  # Compute the offset of each object due to proper motion
1532  # as components of the arc of a great circle along RA and Dec
1533  pmRaRad = catalog["pm_ra"]
1534  pmDecRad = catalog["pm_dec"]
1535  offsetsRaRad = pmRaRad*timeDiffsYears
1536  offsetsDecRad = pmDecRad*timeDiffsYears
1537  # Compute the corresponding bearing and arc length of each offset
1538  # due to proper motion, and apply the offset
1539  # The factor of 1e6 for computing bearing is intended as
1540  # a reasonable scale for typical values of proper motion
1541  # in order to avoid large errors for small values of proper motion;
1542  # using the offsets is another option, but it can give
1543  # needlessly large errors for short duration
1544  offsetBearingsRad = numpy.arctan2(pmDecRad*1e6, pmRaRad*1e6)
1545  offsetAmountsRad = numpy.hypot(offsetsRaRad, offsetsDecRad)
1546  for record, bearingRad, amountRad in zip(catalog, offsetBearingsRad, offsetAmountsRad):
1547  record.set(coordKey,
1548  record.get(coordKey).offset(bearing=bearingRad*lsst.geom.radians,
1549  amount=amountRad*lsst.geom.radians))
1550  # Increase error in RA and Dec based on error in proper motion
1551  if "coord_raErr" in catalog.schema:
1552  catalog["coord_raErr"] = numpy.hypot(catalog["coord_raErr"],
1553  catalog["pm_raErr"]*timeDiffsYears)
1554  if "coord_decErr" in catalog.schema:
1555  catalog["coord_decErr"] = numpy.hypot(catalog["coord_decErr"],
1556  catalog["pm_decErr"]*timeDiffsYears)

◆ convertToNanojansky()

def lsst.meas.algorithms.loadReferenceObjects.convertToNanojansky (   catalog,
  log,
  doConvert = True 
)
Convert fluxes in a catalog from jansky to nanojansky.

Parameters
----------
catalog : `lsst.afw.table.SimpleCatalog`
    The catalog to convert.
log : `lsst.log.Log`
    Log to send messages to.
doConvert : `bool`, optional
    Return a converted catalog, or just identify the fields that need to be converted?
    This supports the "write=False" mode of `bin/convert_to_nJy.py`.

Returns
-------
catalog : `lsst.afw.table.SimpleCatalog` or None
    The converted catalog, or None if ``doConvert`` is False.

Notes
-----
Support for old units in reference catalogs will be removed after the
release of late calendar year 2019.
Use `meas_algorithms/bin/convert_to_nJy.py` to update your reference catalog.

Definition at line 89 of file loadReferenceObjects.py.

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 

◆ getFormatVersionFromRefCat()

def lsst.meas.algorithms.loadReferenceObjects.getFormatVersionFromRefCat (   refCat)
"Return the format version stored in a reference catalog header.

Parameters
----------
refCat : `lsst.afw.table.SimpleCatalog`
    Reference catalog to inspect.

Returns
-------
version : `int` or `None`
    Format version integer, or `None` if the catalog has no metadata
    or the metadata does not include a "REFCAT_FORMAT_VERSION" key.

Definition at line 66 of file loadReferenceObjects.py.

66 def getFormatVersionFromRefCat(refCat):
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 

◆ getRefFluxField()

def lsst.meas.algorithms.loadReferenceObjects.getRefFluxField (   schema,
  filterName = None 
)
Get the name of a flux field from a schema.

return the alias of "anyFilterMapsToThis", if present
else if filterName is specified:
    return "*filterName*_camFlux" if present
    else return "*filterName*_flux" if present (camera filter name
        matches reference filter name)
    else throw RuntimeError
else:
    return "camFlux", if present,
    else throw RuntimeError

Parameters
----------
schema : `lsst.afw.table.Schema`
    Reference catalog schema.
filterName : `str`, optional
    Name of camera filter. If not specified, ``defaultFilter`` needs to be
    set in the refcat loader config.

Returns
-------
fluxFieldName : `str`
    Name of flux field.

Raises
------
RuntimeError
    If an appropriate field is not found.

Definition at line 691 of file loadReferenceObjects.py.

691 def getRefFluxField(schema, filterName=None):
692  """Get the name of a flux field from a schema.
693 
694  return the alias of "anyFilterMapsToThis", if present
695  else if filterName is specified:
696  return "*filterName*_camFlux" if present
697  else return "*filterName*_flux" if present (camera filter name
698  matches reference filter name)
699  else throw RuntimeError
700  else:
701  return "camFlux", if present,
702  else throw RuntimeError
703 
704  Parameters
705  ----------
706  schema : `lsst.afw.table.Schema`
707  Reference catalog schema.
708  filterName : `str`, optional
709  Name of camera filter. If not specified, ``defaultFilter`` needs to be
710  set in the refcat loader config.
711 
712  Returns
713  -------
714  fluxFieldName : `str`
715  Name of flux field.
716 
717  Raises
718  ------
719  RuntimeError
720  If an appropriate field is not found.
721  """
722  if not isinstance(schema, afwTable.Schema):
723  raise RuntimeError("schema=%s is not a schema" % (schema,))
724  try:
725  return schema.getAliasMap().get("anyFilterMapsToThis")
726  except LookupError:
727  pass # try the filterMap next
728 
729  if filterName:
730  fluxFieldList = [filterName + "_camFlux", filterName + "_flux"]
731  else:
732  fluxFieldList = ["camFlux"]
733  for fluxField in fluxFieldList:
734  if fluxField in schema:
735  return fluxField
736 
737  raise RuntimeError("Could not find flux field(s) %s" % (", ".join(fluxFieldList)))
738 
739 

◆ getRefFluxKeys()

def lsst.meas.algorithms.loadReferenceObjects.getRefFluxKeys (   schema,
  filterName = None 
)
Return keys for flux and flux error.

Parameters
----------
schema : `lsst.afw.table.Schema`
    Reference catalog schema.
filterName : `str`
    Name of camera filter.

Returns
-------
keys : `tuple` of (`lsst.afw.table.Key`, `lsst.afw.table.Key`)
    Two keys:

    - flux key
    - flux error key, if present, else None

Raises
------
RuntimeError
    If flux field not found.

Definition at line 740 of file loadReferenceObjects.py.

740 def getRefFluxKeys(schema, filterName=None):
741  """Return keys for flux and flux error.
742 
743  Parameters
744  ----------
745  schema : `lsst.afw.table.Schema`
746  Reference catalog schema.
747  filterName : `str`
748  Name of camera filter.
749 
750  Returns
751  -------
752  keys : `tuple` of (`lsst.afw.table.Key`, `lsst.afw.table.Key`)
753  Two keys:
754 
755  - flux key
756  - flux error key, if present, else None
757 
758  Raises
759  ------
760  RuntimeError
761  If flux field not found.
762  """
763  fluxField = getRefFluxField(schema, filterName)
764  fluxErrField = fluxField + "Err"
765  fluxKey = schema[fluxField].asKey()
766  try:
767  fluxErrKey = schema[fluxErrField].asKey()
768  except Exception:
769  fluxErrKey = None
770  return (fluxKey, fluxErrKey)
771 
772 

◆ hasNanojanskyFluxUnits()

def lsst.meas.algorithms.loadReferenceObjects.hasNanojanskyFluxUnits (   schema)
Return True if the units of all flux and fluxErr are correct (nJy).

Definition at line 57 of file loadReferenceObjects.py.

57 def hasNanojanskyFluxUnits(schema):
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 

◆ isOldFluxField()

def lsst.meas.algorithms.loadReferenceObjects.isOldFluxField (   name,
  units 
)
Return True if this name/units combination corresponds to an
"old-style" reference catalog flux field.

Definition at line 46 of file loadReferenceObjects.py.

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 

◆ joinMatchListWithCatalogImpl()

def lsst.meas.algorithms.loadReferenceObjects.joinMatchListWithCatalogImpl (   refObjLoader,
  matchCat,
  sourceCat 
)
Relink an unpersisted match list to sources and reference
objects.

A match list is persisted and unpersisted as a catalog of IDs
produced by afw.table.packMatches(), with match metadata
(as returned by the astrometry tasks) in the catalog's metadata
attribute. This method converts such a match catalog into a match
list, with links to source records and reference object records.

Parameters
----------
refObjLoader
    Reference object loader to use in getting reference objects
matchCat : `lsst.afw.table.BaseCatalog`
    Unperisted packed match list.
    ``matchCat.table.getMetadata()`` must contain match metadata,
    as returned by the astrometry tasks.
sourceCat : `lsst.afw.table.SourceCatalog`
    Source catalog. As a side effect, the catalog will be sorted
    by ID.

Returns
-------
matchList : `lsst.afw.table.ReferenceMatchVector`
    Match list.

Definition at line 1433 of file loadReferenceObjects.py.

1433 def joinMatchListWithCatalogImpl(refObjLoader, matchCat, sourceCat):
1434  """Relink an unpersisted match list to sources and reference
1435  objects.
1436 
1437  A match list is persisted and unpersisted as a catalog of IDs
1438  produced by afw.table.packMatches(), with match metadata
1439  (as returned by the astrometry tasks) in the catalog's metadata
1440  attribute. This method converts such a match catalog into a match
1441  list, with links to source records and reference object records.
1442 
1443  Parameters
1444  ----------
1445  refObjLoader
1446  Reference object loader to use in getting reference objects
1447  matchCat : `lsst.afw.table.BaseCatalog`
1448  Unperisted packed match list.
1449  ``matchCat.table.getMetadata()`` must contain match metadata,
1450  as returned by the astrometry tasks.
1451  sourceCat : `lsst.afw.table.SourceCatalog`
1452  Source catalog. As a side effect, the catalog will be sorted
1453  by ID.
1454 
1455  Returns
1456  -------
1457  matchList : `lsst.afw.table.ReferenceMatchVector`
1458  Match list.
1459  """
1460  matchmeta = matchCat.table.getMetadata()
1461  version = matchmeta.getInt('SMATCHV')
1462  if version != 1:
1463  raise ValueError('SourceMatchVector version number is %i, not 1.' % version)
1464  filterName = matchmeta.getString('FILTER').strip()
1465  try:
1466  epoch = matchmeta.getDouble('EPOCH')
1468  epoch = None # Not present, or not correct type means it's not set
1469  if 'RADIUS' in matchmeta:
1470  # This is a circle style metadata, call loadSkyCircle
1471  ctrCoord = lsst.geom.SpherePoint(matchmeta.getDouble('RA'),
1472  matchmeta.getDouble('DEC'), lsst.geom.degrees)
1473  rad = matchmeta.getDouble('RADIUS') * lsst.geom.degrees
1474  refCat = refObjLoader.loadSkyCircle(ctrCoord, rad, filterName, epoch=epoch).refCat
1475  elif "INNER_UPPER_LEFT_RA" in matchmeta:
1476  # This is the sky box type (only triggers in the LoadReferenceObject class, not task)
1477  # Only the outer box is required to be loaded to get the maximum region, all filtering
1478  # will be done by the unpackMatches function, and no spatial filtering needs to be done
1479  # by the refObjLoader
1480  box = []
1481  for place in ("UPPER_LEFT", "UPPER_RIGHT", "LOWER_LEFT", "LOWER_RIGHT"):
1482  coord = lsst.geom.SpherePoint(matchmeta.getDouble(f"OUTER_{place}_RA"),
1483  matchmeta.getDouble(f"OUTER_{place}_DEC"),
1484  lsst.geom.degrees).getVector()
1485  box.append(coord)
1486  outerBox = sphgeom.ConvexPolygon(box)
1487  refCat = refObjLoader.loadRegion(outerBox, filterName=filterName, epoch=epoch).refCat
1488 
1489  refCat.sort()
1490  sourceCat.sort()
1491  return afwTable.unpackMatches(matchCat, refCat, sourceCat)
1492 
1493 
lsst.pex::exceptions::NotFoundError
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
lsst::afw::table::Schema
Defines the fields and offsets for a table.
Definition: Schema.h:50
strip
bool strip
Definition: fits.cc:911
lsst::meas::algorithms.loadReferenceObjects.isOldFluxField
def isOldFluxField(name, units)
Definition: loadReferenceObjects.py:46
lsst::meas::algorithms.loadReferenceObjects.getRefFluxField
def getRefFluxField(schema, filterName=None)
Definition: loadReferenceObjects.py:691
lsst::meas::algorithms.loadReferenceObjects.getFormatVersionFromRefCat
def getFormatVersionFromRefCat(refCat)
Definition: loadReferenceObjects.py:66
lsst::meas::algorithms.loadReferenceObjects.applyProperMotionsImpl
def applyProperMotionsImpl(log, catalog, epoch)
Definition: loadReferenceObjects.py:1494
lsst::meas::algorithms.loadReferenceObjects.joinMatchListWithCatalogImpl
def joinMatchListWithCatalogImpl(refObjLoader, matchCat, sourceCat)
Definition: loadReferenceObjects.py:1433
lsst::meas::algorithms.loadReferenceObjects.convertToNanojansky
def convertToNanojansky(catalog, log, doConvert=True)
Definition: loadReferenceObjects.py:89
lsst::afw::table::SchemaMapper
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
lsst::meas::algorithms.loadReferenceObjects.getRefFluxKeys
def getRefFluxKeys(schema, filterName=None)
Definition: loadReferenceObjects.py:740
to
table::Key< int > to
Definition: TransformMap.cc:349
lsst::afw::table::Field
A description of a field in a table.
Definition: Field.h:24
lsst::afw::table::SimpleTable::makeMinimalSchema
static Schema makeMinimalSchema()
Return a minimal schema for Simple tables and records.
Definition: Simple.h:140
lsst.pex::exceptions::TypeError
Reports errors from accepting an object of an unexpected or inappropriate type.
Definition: Runtime.h:167
lsst::geom::SpherePoint
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
lsst::meas::algorithms.loadReferenceObjects.hasNanojanskyFluxUnits
def hasNanojanskyFluxUnits(schema)
Definition: loadReferenceObjects.py:57
lsst::afw::table::unpackMatches
template SourceMatchVector unpackMatches(BaseCatalog const &, SourceCatalog const &, SourceCatalog const &)
lsst::afw::table::SortedCatalogT
Custom catalog class for record/table subclasses that are guaranteed to have an ID,...
Definition: fwd.h:63