LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
CentroidUtilities.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2014 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
25 
26 namespace lsst { namespace meas { namespace base {
27 
29  x(std::numeric_limits<CentroidElement>::quiet_NaN()),
30  y(std::numeric_limits<CentroidElement>::quiet_NaN()),
31  xSigma(std::numeric_limits<ErrElement>::quiet_NaN()),
32  ySigma(std::numeric_limits<ErrElement>::quiet_NaN()),
33  x_y_Cov(std::numeric_limits<ErrElement>::quiet_NaN())
34 {}
35 
36 Centroid const CentroidResult::getCentroid() const { return Centroid(x, y); }
37 
39  x = centroid.getX();
40  y = centroid.getY();
41 }
42 
44  CentroidCov m;
45  m <<
48  return m;
49 }
50 
52  xSigma = std::sqrt(matrix(0, 0));
53  ySigma = std::sqrt(matrix(1, 1));
54  x_y_Cov = matrix(0, 1);
55 }
56 
58  xSigma = _xSigma;
59  ySigma = _ySigma;
60  x_y_Cov = 0.0;
61 }
62 
65  std::string const & name,
66  std::string const & doc,
67  UncertaintyEnum uncertainty
68 ) {
71  schema,
72  name,
73  doc,
74  "pixels"
75  );
76  if (uncertainty != NO_UNCERTAINTY) {
77  std::vector< afw::table::Key<ErrElement> > sigma(2);
78  std::vector< afw::table::Key<ErrElement> > cov;
79  sigma[0] = schema.addField<ErrElement>(
80  schema.join(name, "xSigma"), "1-sigma uncertainty on x position", "pixels"
81  );
82  sigma[1] = schema.addField<ErrElement>(
83  schema.join(name, "ySigma"), "1-sigma uncertainty on y position", "pixels"
84  );
85  if (uncertainty == FULL_COVARIANCE) {
86  cov.push_back(
87  schema.addField<ErrElement>(
88  schema.join(name, "x_y_Cov"), "uncertainty covariance in x and y", "pixels^2"
89  )
90  );
91  }
93  }
94  return r;
95 }
96 
97 namespace {
98 
99 std::vector<std::string> getNameVector() {
100  std::vector<std::string> v;
101  v.push_back("x");
102  v.push_back("y");
103  return v;
104 }
105 
106 } // anonymous
107 
109  _centroid(s)
110 {
111  static std::vector<std::string> names = getNameVector(); // C++11 TODO: just use initializer list
112  try {
114  } catch (pex::exceptions::NotFoundError &) {}
115 }
116 
118  CentroidResult r;
119  r.setCentroid(record.get(_centroid));
120  if (_centroidErr.isValid()) {
121  r.setCentroidErr(record.get(_centroidErr));
122  }
123  return r;
124 }
125 
126 void CentroidResultKey::set(afw::table::BaseRecord & record, CentroidResult const & value) const {
127  record.set(_centroid, value.getCentroid());
128  if (_centroidErr.isValid()) {
129  record.set(_centroidErr, value.getCentroidErr());
130  }
131 }
132 
134  std::string const & name,
135  afw::table::SchemaMapper & mapper
136 ) :
137  BaseTransform{name}
138 {
139  // Map the flag through to the output
140  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(name + "_flag").key);
141 
142  // Add keys for the coordinates
143  auto & s = mapper.editOutputSchema();
144  _coordKey = afw::table::CoordKey::addFields(s, name, "ICRS coordinates");
145 
146  // If the centroid has an error key we also include one on the celestial
147  // coordinates; otherwise, it isn't necessary. Note that if we provide for
148  // errors in celestial coordinates, we always need the full covariance.
149  if (CentroidResultKey(mapper.getInputSchema()[name]).getCentroidErr().isValid()) {
150  std::vector< afw::table::Key<ErrElement> > sigma(2);
151  std::vector< afw::table::Key<ErrElement> > cov(1);
152  sigma[0] = s.addField<ErrElement>(s.join(name, "raSigma"), "Uncertainty on RA", "radians");
153  sigma[1] = s.addField<ErrElement>(s.join(name, "decSigma"), "Uncertainty on dec", "radians");
154  cov[0] = s.addField<ErrElement>(s.join(name, "ra_dec_Cov"),
155  "Uncertainty covariance in RA and dec", "radians^2");
157  }
158 
159 }
160 
162  afw::table::SourceCatalog const & inputCatalog,
163  afw::table::BaseCatalog & outputCatalog,
164  afw::image::Wcs const & wcs,
165  afw::image::Calib const & calib
166 ) const {
167  checkCatalogSize(inputCatalog, outputCatalog);
168  CentroidResultKey centroidResultKey(inputCatalog.getSchema()[_name]);
169 
170  afw::table::SourceCatalog::const_iterator inSrc = inputCatalog.begin();
171  afw::table::BaseCatalog::iterator outSrc = outputCatalog.begin();
172 
173  for (; inSrc != inputCatalog.end() && outSrc != outputCatalog.end(); ++inSrc, ++outSrc) {
174  CentroidResult centroidResult = centroidResultKey.get(*inSrc);
175 
176  _coordKey.set(*outSrc, *wcs.pixelToSky(centroidResult.getCentroid()));
177 
178  if (centroidResultKey.getCentroidErr().isValid()) {
179  CentroidCov centroidCov = centroidResult.getCentroidErr();
180  if (!(utils::isnan(centroidCov(0,0)) || utils::isnan(centroidCov(1,1)))) {
181  auto transform = wcs.linearizePixelToSky(centroidResult.getCentroid(),
182  afw::geom::radians).getLinear().getMatrix();
183  _coordErrKey.set(*outSrc, (transform * centroidResult.getCentroidErr().cast<double>() *
184  transform.transpose()).cast<ErrElement>());
185  }
186  }
187  }
188 }
189 
190 }}} // lsst::meas::base
int y
Defines the fields and offsets for a table.
Definition: Schema.h:46
A proxy type for name lookups in a Schema.
Definition: Schema.h:330
ErrElement ySigma
1-Sigma uncertainty on y (sqrt of variance)
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
geom::AffineTransform linearizePixelToSky(coord::Coord const &coord, geom::AngleUnit skyUnit=geom::degrees) const
Return the local linear approximation to Wcs::pixelToSky at a point given in sky coordinates.
Definition: Wcs.cc:934
UncertaintyEnum
An enum used to specify how much uncertainty information measurement algorithms provide.
Definition: constants.h:41
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
void checkCatalogSize(afw::table::BaseCatalog const &cat1, afw::table::BaseCatalog const &cat2) const
Ensure that catalogs have the same size.
Definition: Transform.h:101
afw::table::Schema schema
Definition: GaussianPsf.cc:41
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
A reusable struct for centroid measurements.
Centroid const getCentroid() const
Return a Point object containing the measured x and y.
ErrElement xSigma
1-Sigma uncertainty on x (sqrt of variance)
The full covariance matrix is provided.
Definition: constants.h:44
CentroidElement x
x (column) coordinate of the measured position
tbl::Key< int > wcs
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
bool isValid() const
Return True if the centroid key is valid.
boost::shared_ptr< coord::Coord > pixelToSky(double pix1, double pix2) const
Convert from pixel position to sky coordinates (e.g. RA/dec)
Definition: Wcs.cc:858
void setCentroid(Centroid const &centroid)
Set the struct fields from the given Point object.
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
int isnan(T t)
Definition: ieee.h:110
static CoordKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc)
float ErrElement
Definition: constants.h:53
A coordinate class intended to represent absolute positions.
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:43
CentroidResult()
Constructor; initializes everything to NaN.
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:55
ErrElement x_y_Cov
x,y term in the uncertainty convariance matrix
AngleUnit const radians
constant with units of radians
Definition: Angle.h:91
Iterator class for CatalogT.
Definition: Catalog.h:34
virtual void set(BaseRecord &record, coord::IcrsCoord const &value) const
Set an IcrsCoord in the given record.
virtual CentroidResult get(afw::table::BaseRecord const &record) const
Get a CentroidResult from the given record.
static CentroidResultKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc, UncertaintyEnum uncertainty)
Add the appropriate fields to a Schema, and return a CentroidResultKey that manages them...
double x
virtual void operator()(afw::table::SourceCatalog const &inputCatalog, afw::table::BaseCatalog &outputCatalog, afw::image::Wcs const &wcs, afw::image::Calib const &calib) const
afw::table::CovarianceMatrixKey< ErrElement, 2 > _coordErrKey
CentroidTransform(std::string const &name, afw::table::SchemaMapper &mapper)
virtual void set(afw::table::BaseRecord &record, CentroidResult const &value) const
Set a CentroidResult in the given record.
Base class for all records.
Definition: BaseRecord.h:27
tuple m
Definition: lsstimport.py:48
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
Algorithm provides no uncertainy information at all.
Definition: constants.h:42
virtual void set(BaseRecord &record, Eigen::Matrix< T, N, N > const &value) const
Set a covariance matrix in the given record (uses only the lower triangle of the given matrix) ...
CentroidCov const getCentroidErr() const
Return the 2x2 symmetric covariance matrix, with rows and columns ordered (x, y)
A FunctorKey for CentroidResult.
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:136
CentroidElement y
y (row) coordinate of the measured position
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:123
Eigen::Matrix< ErrElement, 2, 2, Eigen::DontAlign > CentroidCov
Definition: constants.h:57
afw::geom::Point< CentroidElement, 2 > Centroid
Definition: constants.h:56
bool isValid() const
Return True if all the constituent sigma Keys are valid.
afw::table::PointKey< CentroidElement > _centroid
CentroidResultKey()
Default constructor; instance will not be usuable unless subsequently assigned to.
double CentroidElement
Definition: constants.h:54
afw::table::CovarianceMatrixKey< ErrElement, 2 > _centroidErr
Schema getSchema() const
Return the schema associated with the catalog&#39;s table.
Definition: Catalog.h:114
Matrix const getMatrix() const
void setCentroidErr(CentroidCov const &matrix)
Set the struct uncertainty fields from the given matrix, with rows and columns ordered (x...