LSST Applications g00d0e8bbd7+edbf708997,g03191d30f7+6b31559d11,g118115db7c+ac820e85d2,g199a45376c+5137f08352,g1fd858c14a+90100aa1a7,g262e1987ae+64df5f6984,g29ae962dfc+1eb4aece83,g2cef7863aa+73c82f25e4,g3541666cd7+1e37cdad5c,g35bb328faa+edbf708997,g3fd5ace14f+fb4e2866cc,g47891489e3+19fcc35de2,g53246c7159+edbf708997,g5b326b94bb+d622351b67,g64539dfbff+dfe1dff262,g67b6fd64d1+19fcc35de2,g74acd417e5+cfdc02aca8,g786e29fd12+af89c03590,g7aefaa3e3d+dc1a598170,g87389fa792+a4172ec7da,g88cb488625+60ba2c3075,g89139ef638+19fcc35de2,g8d4809ba88+dfe1dff262,g8d7436a09f+db94b797be,g8ea07a8fe4+79658f16ab,g90f42f885a+6577634e1f,g9722cb1a7f+d8f85438e7,g98df359435+7fdd888faa,ga2180abaac+edbf708997,ga9e74d7ce9+128cc68277,gbf99507273+edbf708997,gca7fc764a6+19fcc35de2,gd7ef33dd92+19fcc35de2,gdab6d2f7ff+cfdc02aca8,gdbb4c4dda9+dfe1dff262,ge410e46f29+19fcc35de2,ge41e95a9f2+dfe1dff262,geaed405ab2+062dfc8cdc,w.2025.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
lsst.meas.algorithms.loadReferenceObjects Namespace Reference

Classes

class  _FilterCatalog
 
class  LoadReferenceObjectsConfig
 
class  ReferenceObjectLoader
 

Functions

 getFormatVersionFromRefCat (refCat)
 
 filterRefCat (refCat, refFluxField, maxRefObjects=None, minRefMag=None, log=None)
 
 getRefFluxField (schema, filterName)
 
 getRefFluxKeys (schema, filterName)
 
 applyProperMotionsImpl (log, catalog, epoch)
 

Function Documentation

◆ applyProperMotionsImpl()

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` or `logging.Logger`
    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 1003 of file loadReferenceObjects.py.

1003def applyProperMotionsImpl(log, catalog, epoch):
1004 """Apply proper motion correction to a reference catalog.
1005
1006 Adjust position and position error in the ``catalog``
1007 for proper motion to the specified ``epoch``,
1008 modifying the catalog in place.
1009
1010 Parameters
1011 ----------
1012 log : `lsst.log.Log` or `logging.Logger`
1013 Log object to write to.
1014 catalog : `lsst.afw.table.SimpleCatalog`
1015 Catalog of positions, containing:
1016
1017 - Coordinates, retrieved by the table's coordinate key.
1018 - ``coord_raErr`` : Error in Right Ascension (rad).
1019 - ``coord_decErr`` : Error in Declination (rad).
1020 - ``pm_ra`` : Proper motion in Right Ascension (rad/yr,
1021 East positive)
1022 - ``pm_raErr`` : Error in ``pm_ra`` (rad/yr), optional.
1023 - ``pm_dec`` : Proper motion in Declination (rad/yr,
1024 North positive)
1025 - ``pm_decErr`` : Error in ``pm_dec`` (rad/yr), optional.
1026 - ``epoch`` : Mean epoch of object (an astropy.time.Time)
1027 epoch : `astropy.time.Time`
1028 Epoch to which to correct proper motion.
1029 """
1030 if "epoch" not in catalog.schema or "pm_ra" not in catalog.schema or "pm_dec" not in catalog.schema:
1031 log.warning("Proper motion correction not available from catalog")
1032 return
1033 if not catalog.isContiguous():
1034 raise RuntimeError("Catalog must be contiguous")
1035 catEpoch = astropy.time.Time(catalog["epoch"], scale="tai", format="mjd")
1036 log.info("Correcting reference catalog for proper motion to %r", epoch)
1037 # Use `epoch.tai` to make sure the time difference is in TAI
1038 timeDiffsYears = (epoch.tai - catEpoch).to(astropy.units.yr).value
1039 coordKey = catalog.table.getCoordKey()
1040 # Compute the offset of each object due to proper motion
1041 # as components of the arc of a great circle along RA and Dec
1042 pmRaRad = catalog["pm_ra"]
1043 pmDecRad = catalog["pm_dec"]
1044 offsetsRaRad = pmRaRad*timeDiffsYears
1045 offsetsDecRad = pmDecRad*timeDiffsYears
1046 # Compute the corresponding bearing and arc length of each offset
1047 # due to proper motion, and apply the offset.
1048 # The factor of 1e6 for computing bearing is intended as
1049 # a reasonable scale for typical values of proper motion
1050 # in order to avoid large errors for small values of proper motion;
1051 # using the offsets is another option, but it can give
1052 # needlessly large errors for short duration.
1053 offsetBearingsRad = numpy.arctan2(offsetsDecRad*1e6, offsetsRaRad*1e6)
1054 offsetAmountsRad = numpy.hypot(offsetsRaRad, offsetsDecRad)
1055 for record, bearingRad, amountRad in zip(catalog, offsetBearingsRad, offsetAmountsRad):
1056 record.set(coordKey,
1057 record.get(coordKey).offset(bearing=bearingRad*geom.radians,
1058 amount=amountRad*geom.radians))
1059 # TODO DM-36979: this needs to incorporate the full covariance!
1060 # Increase error in RA and Dec based on error in proper motion
1061 if "coord_raErr" in catalog.schema:
1062 catalog["coord_raErr"] = numpy.hypot(catalog["coord_raErr"],
1063 catalog["pm_raErr"]*timeDiffsYears)
1064 if "coord_decErr" in catalog.schema:
1065 catalog["coord_decErr"] = numpy.hypot(catalog["coord_decErr"],
1066 catalog["pm_decErr"]*timeDiffsYears)

◆ filterRefCat()

lsst.meas.algorithms.loadReferenceObjects.filterRefCat ( refCat,
refFluxField,
maxRefObjects = None,
minRefMag = None,
log = None )
Sub-select a number of reference objects starting from the brightest
and maxing out at the number specified by maxRefObjects.

No further trimming is done if len(refCat) > maxRefObjects after trimming
to minRefMag.

Parameters
----------
refCat : `lsst.afw.table.SimpleCatalog`
    Catalog of reference objects to trim.
refFluxField : `str`
    Field of refCat to use for flux.
maxRefObjects : `int` or `None`, optional
    Maximum number of reference objects (i.e. trim refCat down to
    this number of objects).
minRefMag : `int` or `None`, optional
    Minimum (i.e. brightest) magnitude to include in the reference
    catalog.
log : `lsst.log.Log` or `logging.Logger` or `None`, optional
    Logger object used to write out messages. If `None`, no messages
    will be logged.

Returns
-------
filteredCat : `lsst.afw.table.SimpleCatalog`
    Catalog trimmed to the maximum brightness and/or maximum number set
    in the task config from the brightest flux down.

Definition at line 848 of file loadReferenceObjects.py.

848def filterRefCat(refCat, refFluxField, maxRefObjects=None, minRefMag=None, log=None):
849 """Sub-select a number of reference objects starting from the brightest
850 and maxing out at the number specified by maxRefObjects.
851
852 No further trimming is done if len(refCat) > maxRefObjects after trimming
853 to minRefMag.
854
855 Parameters
856 ----------
857 refCat : `lsst.afw.table.SimpleCatalog`
858 Catalog of reference objects to trim.
859 refFluxField : `str`
860 Field of refCat to use for flux.
861 maxRefObjects : `int` or `None`, optional
862 Maximum number of reference objects (i.e. trim refCat down to
863 this number of objects).
864 minRefMag : `int` or `None`, optional
865 Minimum (i.e. brightest) magnitude to include in the reference
866 catalog.
867 log : `lsst.log.Log` or `logging.Logger` or `None`, optional
868 Logger object used to write out messages. If `None`, no messages
869 will be logged.
870
871 Returns
872 -------
873 filteredCat : `lsst.afw.table.SimpleCatalog`
874 Catalog trimmed to the maximum brightness and/or maximum number set
875 in the task config from the brightest flux down.
876 """
877 if maxRefObjects is None and minRefMag is None:
878 if log is not None:
879 log.debug("No filtering of the reference catalog has been actually requested "
880 "(i.e maxRefObjects and minRefMag are both `None`) Returning "
881 "original catalog.")
882 return refCat
883
884 filteredCat = refCat.copy(deep=True)
885
886 if minRefMag is not None:
887 refFlux = filteredCat.get(refFluxField)
888 refMag = (refFlux*astropy.units.nJy).to_value(astropy.units.ABmag)
889 if numpy.nanmin(refMag) <= minRefMag:
890 filteredCat = filteredCat[(refMag > minRefMag)].copy(deep=True)
891 if log is not None:
892 log.info("Trimming the loaded reference catalog to %s > %.2f", refFluxField, minRefMag)
893
894 if maxRefObjects is not None:
895 if len(filteredCat) <= maxRefObjects:
896 if log is not None:
897 log.debug("Number of reference objects in reference catalog = %d (max is %d). "
898 "Returning catalog without further filtering.",
899 len(filteredCat), maxRefObjects)
900 return filteredCat
901 if log is not None:
902 log.info("Trimming number of reference objects in refCat from %d down to %d. ",
903 len(refCat), maxRefObjects)
904 if not filteredCat.isContiguous():
905 filteredCat = filteredCat.copy(deep=True)
906
907 refFlux = filteredCat.get(refFluxField)
908 sortedRefFlux = refFlux[refFlux.argsort()]
909 minRefFlux = sortedRefFlux[-(maxRefObjects + 1)]
910
911 selected = (filteredCat.get(refFluxField) > minRefFlux)
912 filteredCat = filteredCat[selected]
913
914 if not filteredCat.isContiguous():
915 filteredCat = filteredCat.copy(deep=True)
916
917 if log is not None:
918 if len(filteredCat) > 0:
919 refFlux = filteredCat[refFluxField]
920 refMag = (refFlux*astropy.units.nJy).to_value(astropy.units.ABmag)
921 log.info("Reference catalog magnitude range for filter %s after trimming: refMagMin = %.2f; "
922 "refMagMax = %.2f", refFluxField, numpy.nanmin(refMag), numpy.nanmax(refMag))
923 else:
924 log.warning("Length of reference catalog after filtering is 0.")
925 return filteredCat
926
927

◆ getFormatVersionFromRefCat()

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`
    Format version integer.

Raises
------
ValueError
    Raised if the catalog is version 0, has no metadata, or does not
    include a "REFCAT_FORMAT_VERSION" key.

Definition at line 41 of file loadReferenceObjects.py.

41def getFormatVersionFromRefCat(refCat):
42 """"Return the format version stored in a reference catalog header.
43
44 Parameters
45 ----------
46 refCat : `lsst.afw.table.SimpleCatalog`
47 Reference catalog to inspect.
48
49 Returns
50 -------
51 version : `int`
52 Format version integer.
53
54 Raises
55 ------
56 ValueError
57 Raised if the catalog is version 0, has no metadata, or does not
58 include a "REFCAT_FORMAT_VERSION" key.
59 """
60 errMsg = "Version 0 refcats are no longer supported: refcat fluxes must have nJy units."
61 md = refCat.getMetadata()
62 if md is None:
63 raise ValueError(f"No metadata found in refcat header. {errMsg}")
64
65 try:
66 version = md.getScalar("REFCAT_FORMAT_VERSION")
67 if version == 0:
68 raise ValueError(errMsg)
69 else:
70 return version
71 except KeyError:
72 raise ValueError(f"No version number found in refcat header metadata. {errMsg}")
73
74

◆ getRefFluxField()

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

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

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

Notes
-----
Return the alias of ``anyFilterMapsToThis``, if present
else, return ``*filterName*_camFlux`` if present,
else, return ``*filterName*_flux`` if present (camera filter name
matches reference filter name), else raise an exception.

Raises
------
RuntimeError
    Raised if an appropriate field is not found.

Definition at line 928 of file loadReferenceObjects.py.

928def getRefFluxField(schema, filterName):
929 """Get the name of a flux field from a schema.
930
931 Parameters
932 ----------
933 schema : `lsst.afw.table.Schema`
934 Reference catalog schema.
935 filterName : `str`
936 Name of camera filter.
937
938 Returns
939 -------
940 fluxFieldName : `str`
941 Name of flux field.
942
943 Notes
944 -----
945 Return the alias of ``anyFilterMapsToThis``, if present
946 else, return ``*filterName*_camFlux`` if present,
947 else, return ``*filterName*_flux`` if present (camera filter name
948 matches reference filter name), else raise an exception.
949
950 Raises
951 ------
952 RuntimeError
953 Raised if an appropriate field is not found.
954 """
955 if not isinstance(schema, afwTable.Schema):
956 raise RuntimeError("schema=%s is not a schema" % (schema,))
957 try:
958 return schema.getAliasMap().get("anyFilterMapsToThis")
959 except LookupError:
960 pass # try the filterMap next
961
962 fluxFieldList = [filterName + "_camFlux", filterName + "_flux"]
963 for fluxField in fluxFieldList:
964 if fluxField in schema:
965 return fluxField
966
967 raise RuntimeError("Could not find flux field(s) %s" % (", ".join(fluxFieldList)))
968
969
Defines the fields and offsets for a table.
Definition Schema.h:51

◆ getRefFluxKeys()

lsst.meas.algorithms.loadReferenceObjects.getRefFluxKeys ( schema,
filterName )
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 970 of file loadReferenceObjects.py.

970def getRefFluxKeys(schema, filterName):
971 """Return keys for flux and flux error.
972
973 Parameters
974 ----------
975 schema : `lsst.afw.table.Schema`
976 Reference catalog schema.
977 filterName : `str`
978 Name of camera filter.
979
980 Returns
981 -------
982 keys : `tuple` of (`lsst.afw.table.Key`, `lsst.afw.table.Key`)
983 Two keys:
984
985 - flux key
986 - flux error key, if present, else None
987
988 Raises
989 ------
990 RuntimeError
991 If flux field not found.
992 """
993 fluxField = getRefFluxField(schema, filterName)
994 fluxErrField = fluxField + "Err"
995 fluxKey = schema[fluxField].asKey()
996 try:
997 fluxErrKey = schema[fluxErrField].asKey()
998 except Exception:
999 fluxErrKey = None
1000 return (fluxKey, fluxErrKey)
1001
1002