24 #include "lsst/pex/exceptions/Runtime.h" 25 #include "lsst/afw/table/io/CatalogVector.h" 26 #include "lsst/afw/table/io/OutputArchive.h" 27 #include "lsst/afw/table/io/InputArchive.h" 28 #include "lsst/afw/table/aggregates.h" 31 namespace lsst {
namespace meas {
namespace algorithms {
35 afw::geom::Box2I
const & bbox,
39 afw::math::BoundedField(bbox),
40 _throwOnMissing(true),
47 afw::geom::Box2I
const & bbox,
52 afw::math::BoundedField(bbox),
53 _throwOnMissing(false),
60 PTR(afw::coord::Coord) coord = _coaddWcs->pixelToSky(position);
63 for (ElementVector::const_iterator i = _elements.begin(); i != _elements.end(); ++i) {
64 afw::geom::Point2D transformedPosition = i->wcs->skyToPixel(*coord);
65 bool inValidArea = i->validPolygon ? i->validPolygon->contains(transformedPosition) :
true;
66 if (afw::geom::Box2D(i->field->getBBox()).contains(transformedPosition) && inValidArea) {
67 sum += i->weight * i->field->evaluate(transformedPosition);
72 if (_throwOnMissing) {
74 pex::exceptions::DomainError,
75 (boost::format(
"No constituent fields to evaluate at point %f, %f")
76 % position.getX() % position.getY()).str()
93 namespace tbl = afw::table;
96 class CoaddBoundedFieldPersistenceKeys1 {
105 static CoaddBoundedFieldPersistenceKeys1
const &
get() {
106 static CoaddBoundedFieldPersistenceKeys1
const instance;
111 CoaddBoundedFieldPersistenceKeys1 (
const CoaddBoundedFieldPersistenceKeys1&) =
delete;
112 CoaddBoundedFieldPersistenceKeys1& operator=(
const CoaddBoundedFieldPersistenceKeys1&) =
delete;
115 CoaddBoundedFieldPersistenceKeys1 (CoaddBoundedFieldPersistenceKeys1&&) =
delete;
116 CoaddBoundedFieldPersistenceKeys1& operator=(CoaddBoundedFieldPersistenceKeys1&&) =
delete;
119 CoaddBoundedFieldPersistenceKeys1() :
121 bboxMin(tbl::PointKey<int>::addFields(
122 schema,
"bbox_min",
"lower-left corner of bounding box",
"pixel")),
123 bboxMax(tbl::PointKey<int>::addFields(
124 schema,
"bbox_max",
"upper-right corner of bounding box",
"pixel")),
125 coaddWcs(schema.addField<
int>(
"coaddWcs",
"archive ID of the coadd's WCS")),
127 "whether to throw an exception on missing data")),
128 default_(schema.addField<
double>(
"default",
"default value to use when throwOnMissing is False"))
130 schema.getCitizen().markPersistent();
135 class CoaddBoundedFieldPersistenceKeys2 {
143 static CoaddBoundedFieldPersistenceKeys2
const &
get() {
144 static CoaddBoundedFieldPersistenceKeys2
const instance;
149 CoaddBoundedFieldPersistenceKeys2 (
const CoaddBoundedFieldPersistenceKeys2&) =
delete;
150 CoaddBoundedFieldPersistenceKeys2& operator=(
const CoaddBoundedFieldPersistenceKeys2&) =
delete;
153 CoaddBoundedFieldPersistenceKeys2 (CoaddBoundedFieldPersistenceKeys2&&) =
delete;
154 CoaddBoundedFieldPersistenceKeys2& operator=(CoaddBoundedFieldPersistenceKeys2&&) =
delete;
157 CoaddBoundedFieldPersistenceKeys2() :
159 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField to be coadded")),
160 wcs(schema.addField<
int>(
"wcs",
"archive ID of the Wcs associated with this element")),
161 validPolygon(schema.addField<
int>(
"validPolygon",
"archive ID of the Polygon associated with this element")),
162 weight(schema.addField<
double>(
"weight",
"weight value for this element"))
164 schema.getCitizen().markPersistent();
173 virtual PTR(tbl::io::Persistable)
174 read(InputArchive const & archive, CatalogVector const & catalogs)
const {
175 CoaddBoundedFieldPersistenceKeys1
const & keys1 = CoaddBoundedFieldPersistenceKeys1::get();
176 CoaddBoundedFieldPersistenceKeys2
const & keys2 = CoaddBoundedFieldPersistenceKeys2::get();
177 LSST_ARCHIVE_ASSERT(catalogs.size() == 2u);
178 LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys1.schema);
179 LSST_ARCHIVE_ASSERT(catalogs.back().getSchema() == keys2.schema);
180 tbl::BaseRecord
const & record1 = catalogs.front().front();
182 elements.reserve(catalogs.back().size());
183 for (tbl::BaseCatalog::const_iterator i = catalogs.back().begin(); i != catalogs.back().end(); ++i) {
186 archive.get<afw::math::BoundedField>(i->get(keys2.field)),
187 archive.get<afw::image::Wcs>(i->get(keys2.wcs)),
188 archive.get<afw::geom::polygon::Polygon>(i->get(keys2.validPolygon)),
193 return std::make_shared<CoaddBoundedField>(
194 afw::geom::Box2I(record1.get(keys1.bboxMin), record1.get(keys1.bboxMax)),
195 archive.get<afw::image::Wcs>(record1.get(keys1.coaddWcs)),
197 record1.get(keys1.default_)
201 Factory(std::string
const & name) : tbl::io::PersistableFactory(name) {}
207 std::string getCoaddBoundedFieldPersistenceName() {
return "CoaddBoundedField"; }
218 CoaddBoundedFieldPersistenceKeys1
const & keys1 = CoaddBoundedFieldPersistenceKeys1::get();
219 CoaddBoundedFieldPersistenceKeys2
const & keys2 = CoaddBoundedFieldPersistenceKeys2::get();
220 tbl::BaseCatalog cat1 = handle.makeCatalog(keys1.schema);
221 PTR(tbl::BaseRecord) record1 = cat1.addNew();
222 record1->set(keys1.bboxMin, getBBox().getMin());
223 record1->set(keys1.bboxMax, getBBox().getMax());
224 record1->set(keys1.coaddWcs, handle.put(_coaddWcs));
225 record1->set(keys1.default_, _default);
226 handle.saveCatalog(cat1);
227 tbl::BaseCatalog cat2 = handle.makeCatalog(keys2.schema);
228 for (ElementVector::const_iterator i = _elements.begin(); i != _elements.end(); ++i) {
229 PTR(tbl::BaseRecord) record2 = cat2.addNew();
230 record2->set(keys2.field, handle.put(i->field));
231 record2->set(keys2.wcs, handle.put(i->wcs));
232 record2->set(keys2.validPolygon, handle.put(i->validPolygon));
233 record2->set(keys2.weight, i->weight);
235 handle.saveCatalog(cat2);
239 throw LSST_EXCEPT(pex::exceptions::NotFoundError,
"Scaling of CoaddBoundedField is not implemented");
244 if (!rhsCasted)
return false;
246 return (getBBox() == rhsCasted->getBBox()) && (_default == rhsCasted->_default) &&
247 ((*_coaddWcs) == (*rhsCasted->_coaddWcs)) && (_elements == rhsCasted->_elements);
virtual std::string getPersistenceName() const
virtual std::string getPythonModule() const
tbl::Key< double > weight
std::vector< Element > ElementVector
CoaddBoundedFieldElement Element
virtual void write(OutputArchiveHandle &handle) const
tbl::Key< int > validPolygon
virtual double evaluate(afw::geom::Point2D const &position) const
virtual bool operator==(BoundedField const &rhs) const
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal...
tbl::Key< double > default_
CoaddBoundedField(afw::geom::Box2I const &bbox, boost::shared_ptr< afw::image::Wcs const > coaddWcs, ElementVector const &elements)
Factory(std::string const &name)
tbl::PointKey< int > bboxMax
tbl::Key< tbl::Flag > throwOnMissing
virtual boost::shared_ptr< afw::math::BoundedField > operator*(double const scale) const
tbl::PointKey< int > bboxMin