LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
CoaddBoundedField.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014, 2010 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
25 #include "lsst/geom/Box.h"
29 #include "lsst/afw/table/io/Persistable.cc"
32 
33 namespace lsst {
34 namespace afw {
35 namespace table {
36 namespace io {
37 
40 
41 } // namespace io
42 } // namespace table
43 } // namespace afw
44 namespace meas {
45 namespace algorithms {
46 namespace {
47 
48 /*
49  * Compare two pointers of the same type
50  *
51  * If both pointers are set then return *a == *b
52  * else return a == b
53  */
54 template <typename T>
55 bool ptrEquals(T a, T b) {
56  if (a == b) {
57  // test this first for efficiency
58  return true;
59  } else if (a && b) {
60  // both pointers are set, so it is safe to check value equality
61  return *a == *b;
62  }
63  return false;
64 }
65 
66 } // namespace
67 
68 bool CoaddBoundedFieldElement::operator==(CoaddBoundedFieldElement const& rhs) const {
69  return ptrEquals(field, rhs.field) && ptrEquals(wcs, rhs.wcs) &&
70  ptrEquals(validPolygon, rhs.validPolygon) && (weight == rhs.weight);
71 }
72 
73 CoaddBoundedField::CoaddBoundedField(geom::Box2I const& bbox, PTR(afw::geom::SkyWcs const) coaddWcs,
74  ElementVector const& elements)
75  : afw::math::BoundedField(bbox),
76  _throwOnMissing(true),
77  _default(0.0), // unused
78  _coaddWcs(coaddWcs),
79  _elements(elements) {}
80 
82  ElementVector const& elements, double default_)
83  : afw::math::BoundedField(bbox),
84  _throwOnMissing(false),
85  _default(default_),
86  _coaddWcs(coaddWcs),
87  _elements(elements) {}
88 
89 double CoaddBoundedField::evaluate(geom::Point2D const& position) const {
90  auto coord = _coaddWcs->pixelToSky(position);
91  double sum = 0.0;
92  double wSum = 0.0;
93  for (ElementVector::const_iterator i = _elements.begin(); i != _elements.end(); ++i) {
94  geom::Point2D transformedPosition = i->wcs->skyToPixel(coord);
95  bool inValidArea = i->validPolygon ? i->validPolygon->contains(transformedPosition) : true;
96  if (geom::Box2D(i->field->getBBox()).contains(transformedPosition) && inValidArea) {
97  sum += i->weight * i->field->evaluate(transformedPosition);
98  wSum += i->weight;
99  }
100  }
101  if (wSum == 0.0) {
102  if (_throwOnMissing) {
104  (boost::format("No constituent fields to evaluate at point %f, %f") %
105  position.getX() % position.getY())
106  .str());
107  } else {
108  return _default;
109  }
110  }
111  return sum / wSum;
112 }
113 
114 // ---------- Persistence -----------------------------------------------------------------------------------
115 
116 // For persistence of CoaddBoundedField, we have two catalogs: the first has just one record, and contains
117 // the archive ID of the coadd WCS and the parameters that control missing data. The other catalog
118 // has one record for each element, with fields corresponding to the data members of the Element struct.
119 
120 namespace {
121 
122 // Singleton class that manages the first persistence catalog's schema and keys
123 class CoaddBoundedFieldPersistenceKeys1 {
124 public:
125  afw::table::Schema schema;
126  afw::table::PointKey<int> bboxMin;
127  afw::table::PointKey<int> bboxMax;
128  afw::table::Key<int> coaddWcs;
129  afw::table::Key<afw::table::Flag> throwOnMissing;
130  afw::table::Key<double> default_;
131 
132  static CoaddBoundedFieldPersistenceKeys1 const& get() {
133  static CoaddBoundedFieldPersistenceKeys1 const instance;
134  return instance;
135  }
136 
137  // No copying
138  CoaddBoundedFieldPersistenceKeys1(const CoaddBoundedFieldPersistenceKeys1&) = delete;
139  CoaddBoundedFieldPersistenceKeys1& operator=(const CoaddBoundedFieldPersistenceKeys1&) = delete;
140 
141  // No moving
142  CoaddBoundedFieldPersistenceKeys1(CoaddBoundedFieldPersistenceKeys1&&) = delete;
143  CoaddBoundedFieldPersistenceKeys1& operator=(CoaddBoundedFieldPersistenceKeys1&&) = delete;
144 
145 private:
146  CoaddBoundedFieldPersistenceKeys1()
147  : schema(),
149  "lower-left corner of bounding box", "pixel")),
151  "upper-right corner of bounding box", "pixel")),
152  coaddWcs(schema.addField<int>("coaddWcs", "archive ID of the coadd's WCS")),
153  throwOnMissing(schema.addField<afw::table::Flag>(
154  "throwOnMissing", "whether to throw an exception on missing data")),
155  default_(schema.addField<double>("default",
156  "default value to use when throwOnMissing is False")) {
157  schema.getCitizen().markPersistent();
158  }
159 };
160 
161 // Singleton class that manages the second persistence catalog's schema and keys
162 class CoaddBoundedFieldPersistenceKeys2 {
163 public:
164  afw::table::Schema schema;
165  afw::table::Key<int> field;
166  afw::table::Key<int> wcs;
167  afw::table::Key<int> validPolygon;
168  afw::table::Key<double> weight;
169 
170  static CoaddBoundedFieldPersistenceKeys2 const& get() {
171  static CoaddBoundedFieldPersistenceKeys2 const instance;
172  return instance;
173  }
174 
175  // No copying
176  CoaddBoundedFieldPersistenceKeys2(const CoaddBoundedFieldPersistenceKeys2&) = delete;
177  CoaddBoundedFieldPersistenceKeys2& operator=(const CoaddBoundedFieldPersistenceKeys2&) = delete;
178 
179  // No moving
180  CoaddBoundedFieldPersistenceKeys2(CoaddBoundedFieldPersistenceKeys2&&) = delete;
181  CoaddBoundedFieldPersistenceKeys2& operator=(CoaddBoundedFieldPersistenceKeys2&&) = delete;
182 
183 private:
184  CoaddBoundedFieldPersistenceKeys2()
185  : schema(),
186  field(schema.addField<int>("field", "archive ID of the BoundedField to be coadded")),
187  wcs(schema.addField<int>("wcs", "archive ID of the Wcs associated with this element")),
188  validPolygon(schema.addField<int>("validPolygon",
189  "archive ID of the Polygon associated with this element")),
190  weight(schema.addField<double>("weight", "weight value for this element")) {
191  schema.getCitizen().markPersistent();
192  }
193 };
194 
195 } // namespace
196 
198 public:
200  read(InputArchive const& archive, CatalogVector const& catalogs) const {
201  CoaddBoundedFieldPersistenceKeys1 const& keys1 = CoaddBoundedFieldPersistenceKeys1::get();
202  CoaddBoundedFieldPersistenceKeys2 const& keys2 = CoaddBoundedFieldPersistenceKeys2::get();
203  LSST_ARCHIVE_ASSERT(catalogs.size() == 2u);
204  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys1.schema);
205  LSST_ARCHIVE_ASSERT(catalogs.back().getSchema() == keys2.schema);
206  afw::table::BaseRecord const& record1 = catalogs.front().front();
207  ElementVector elements;
208  elements.reserve(catalogs.back().size());
209  for (afw::table::BaseCatalog::const_iterator i = catalogs.back().begin(); i != catalogs.back().end();
210  ++i) {
211  elements.push_back(Element(archive.get<afw::math::BoundedField>(i->get(keys2.field)),
212  archive.get<afw::geom::SkyWcs>(i->get(keys2.wcs)),
213  archive.get<afw::geom::polygon::Polygon>(i->get(keys2.validPolygon)),
214  i->get(keys2.weight)));
215  }
216  return std::make_shared<CoaddBoundedField>(
217  geom::Box2I(record1.get(keys1.bboxMin), record1.get(keys1.bboxMax)),
218  archive.get<afw::geom::SkyWcs>(record1.get(keys1.coaddWcs)), elements,
219  record1.get(keys1.default_));
220  }
221 
222  Factory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
223 };
224 
225 namespace {
226 
227 std::string getCoaddBoundedFieldPersistenceName() { return "CoaddBoundedField"; }
228 
229 CoaddBoundedField::Factory registration(getCoaddBoundedFieldPersistenceName());
230 
231 } // namespace
232 
233 std::string CoaddBoundedField::getPersistenceName() const { return getCoaddBoundedFieldPersistenceName(); }
234 
235 std::string CoaddBoundedField::getPythonModule() const { return "lsst.meas.algorithms"; }
236 
238  CoaddBoundedFieldPersistenceKeys1 const& keys1 = CoaddBoundedFieldPersistenceKeys1::get();
239  CoaddBoundedFieldPersistenceKeys2 const& keys2 = CoaddBoundedFieldPersistenceKeys2::get();
240  afw::table::BaseCatalog cat1 = handle.makeCatalog(keys1.schema);
241  PTR(afw::table::BaseRecord) record1 = cat1.addNew();
242  record1->set(keys1.bboxMin, getBBox().getMin());
243  record1->set(keys1.bboxMax, getBBox().getMax());
244  record1->set(keys1.coaddWcs, handle.put(_coaddWcs));
245  record1->set(keys1.default_, _default);
246  handle.saveCatalog(cat1);
247  afw::table::BaseCatalog cat2 = handle.makeCatalog(keys2.schema);
248  for (ElementVector::const_iterator i = _elements.begin(); i != _elements.end(); ++i) {
249  PTR(afw::table::BaseRecord) record2 = cat2.addNew();
250  record2->set(keys2.field, handle.put(i->field));
251  record2->set(keys2.wcs, handle.put(i->wcs));
252  record2->set(keys2.validPolygon, handle.put(i->validPolygon));
253  record2->set(keys2.weight, i->weight);
254  }
255  handle.saveCatalog(cat2);
256 }
257 
259  throw LSST_EXCEPT(pex::exceptions::NotFoundError, "Scaling of CoaddBoundedField is not implemented");
260 }
261 
263  auto rhsCasted = dynamic_cast<CoaddBoundedField const*>(&rhs);
264  if (!rhsCasted) return false;
265 
266  return (getBBox() == rhsCasted->getBBox()) && (_default == rhsCasted->_default) &&
267  ptrEquals(_coaddWcs, rhsCasted->_coaddWcs) && (_elements == rhsCasted->_elements);
268 }
269 
270 } // namespace algorithms
271 } // namespace meas
272 } // namespace lsst
Struct used to hold one Exposure&#39;s data in a CoaddBoundedField.
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Definition: SkyWcs.h:115
boost::shared_ptr< afw::geom::SkyWcs const > wcs
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
afw::table::Key< double > default_
A floating-point coordinate rectangle geometry.
Definition: Box.h:294
An object passed to Persistable::write to allow it to persist itself.
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
table::Key< int > b
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add a pair of _x, _y fields to a Schema, and return a PointKey that points to them.
Definition: aggregates.cc:36
table::Key< int > a
Reports arguments outside the domain of an operation.
Definition: Runtime.h:57
T end(T... args)
double evaluate(geom::Point2D const &position) const override
Evaluate the field at the given point.
afw::table::Schema schema
afw::table::PointKey< int > bboxMax
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
BoundedField & operator=(BoundedField const &)=delete
afw::table::PointKey< int > bboxMin
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
T push_back(T... args)
afw::table::Key< int > field
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal...
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
BoundedField(BoundedField const &)=default
A base class for image defects.
#define PTR(...)
Definition: base.h:41
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Iterator class for CatalogT.
Definition: Catalog.h:38
afw::table::Key< int > coaddWcs
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
boost::shared_ptr< afw::math::BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
table::Box2IKey bbox
Definition: Detector.cc:166
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
afw::table::Key< double > weight
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
Base class for all records.
Definition: BaseRecord.h:31
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
T begin(T... args)
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Cartesian polygons.
Definition: Polygon.h:58
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
afw::table::Key< int > validPolygon
CoaddBoundedField(geom::Box2I const &bbox, boost::shared_ptr< afw::geom::SkyWcs const > coaddWcs, ElementVector const &elements)
afw::table::Key< int > wcs
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
An integer coordinate rectangle.
Definition: Box.h:54
afw::table::Key< afw::table::Flag > throwOnMissing
boost::shared_ptr< afw::math::BoundedField > field
T reserve(T... args)
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:472
boost::shared_ptr< afw::geom::polygon::Polygon const > validPolygon