33 #include "boost/iterator/iterator_adaptor.hpp"
34 #include "boost/iterator/transform_iterator.hpp"
35 #include "ndarray/eigen.h"
60 namespace algorithms {
71 explicit AvgPosItem(
double wx_ = 0.0,
double wy_ = 0.0,
double w_ = 0.0) :
wx(wx_),
wy(wy_),
w(w_) {}
77 bool operator<(AvgPosItem
const &other)
const {
return w < other.w; }
79 AvgPosItem &
operator+=(AvgPosItem
const &other) {
86 AvgPosItem &
operator-=(AvgPosItem
const &other) {
93 friend AvgPosItem
operator+(AvgPosItem a, AvgPosItem
const &
b) {
return a +=
b; }
95 friend AvgPosItem
operator-(AvgPosItem a, AvgPosItem
const &
b) {
return a -=
b; }
99 afw::geom::SkyWcs
const &
coaddWcs, afw::table::Key<double> weightKey) {
100 afw::table::Key<int> goodPixKey;
102 goodPixKey = catalog.getSchema()[
"goodpix"];
103 }
catch (pex::exceptions::NotFoundError &) {
106 items.reserve(catalog.size());
109 AvgPosItem item(p.getX(), p.getY(), i->get(weightKey));
110 if (goodPixKey.isValid()) {
111 item.w *= i->get(goodPixKey);
115 items.push_back(item);
126 catalog.subsetContaining(result.getPoint(),
coaddWcs,
true).empty(); ++iter) {
127 if (iter ==
items.end()) {
132 "Could not find a valid average position for CoaddPsf");
136 return result.getPoint();
152 mapper.addMapping(goodPixKey,
true);
159 _weightKey =
mapper.addMapping(weightKey, weightField);
164 record->assign(*i,
mapper);
167 _averagePosition = computeAveragePosition(_catalog, _coaddWcs, _weightKey);
174 _weightKey(_catalog.getSchema()[
"weight"]),
191 for (
unsigned int i = 0; i < imgVector.size(); i++) {
204 assert(imgVector.size() == weightVector.
size());
205 for (
unsigned int i = 0; i < imgVector.size(); i++) {
207 double weight = weightVector[i];
208 double sum = ndarray::asEigenMatrix(componentImg->getArray()).sum();
226 if (subcat.
empty()) {
229 (boost::format(
"Cannot compute BBox at point %s; no input images at that point.") % ccdXY)
234 for (
auto const &exposureRecord : subcat) {
237 WarpedPsf warpedPsf =
WarpedPsf(exposureRecord.getPsf(), exposureToCoadd, _warpingControl);
249 if (subcat.
empty()) {
252 (boost::format(
"Cannot compute CoaddPsf at point %s; no input images at that point.") % ccdXY)
255 double weightSum = 0.0;
263 for (
auto const &exposureRecord : subcat) {
268 WarpedPsf warpedPsf =
WarpedPsf(exposureRecord.getPsf(), exposureToCoadd, _warpingControl);
271 LSST_EXCEPT_ADD(exc, (boost::format(
"Computing WarpedPsf kernel image for id=%d") %
272 exposureRecord.getId())
277 weightSum += exposureRecord.get(_weightKey);
278 weightVector.
push_back(exposureRecord.get(_weightKey));
297 return _catalog[index].getPsf();
304 return *_catalog[index].getWcs();
311 return _catalog[index].getValidPolygon();
318 return _catalog[index].
get(_weightKey);
325 return _catalog[index].getId();
332 return _catalog[index].getBBox();
344 class CoaddPsfPersistenceHelper {
352 static CoaddPsfPersistenceHelper
const &get() {
353 static CoaddPsfPersistenceHelper
const instance;
358 CoaddPsfPersistenceHelper()
360 coaddWcs(
schema.addField<int>(
"coaddwcs",
"archive ID of the coadd's WCS")),
361 cacheSize(
schema.addField<int>(
"cachesize",
"size of the warping cache")),
363 schema,
"avgpos",
"PSF accessors default position",
"pixel")),
365 schema.addField<
std::string>(
"warpingkernelname",
"warping kernel name", 32)) {}
374 if (catalogs.size() == 1u) {
378 return readV0(archive, catalogs);
381 CoaddPsfPersistenceHelper
const &keys1 = CoaddPsfPersistenceHelper::get();
387 record1.
get(keys1.averagePosition), record1.
get(keys1.warpingKernelName),
388 record1.
get(keys1.cacheSize)));
400 auto coaddWcs = internalCat.back().getWcs();
401 internalCat.pop_back();
407 weightKey = internalCat.getSchema()[
"weight"];
410 auto averagePos = computeAveragePosition(internalCat, *
coaddWcs, weightKey);
419 std::string getCoaddPsfPersistenceName() {
return "CoaddPsf"; }
421 CoaddPsf::Factory registration(getCoaddPsfPersistenceName());
430 CoaddPsfPersistenceHelper
const &keys1 = CoaddPsfPersistenceHelper::get();
433 auto coaddWcsPtr = std::make_shared<afw::geom::SkyWcs>(_coaddWcs);
434 record1->set(keys1.coaddWcs, handle.put(coaddWcsPtr));
435 record1->set(keys1.cacheSize, _warpingControl->getCacheSize());
436 record1->set(keys1.averagePosition, _averagePosition);
437 record1->set(keys1.warpingKernelName, _warpingKernelName);
438 handle.saveCatalog(cat1);
table::Key< std::string > name
std::vector< SchemaItem< Flag > > * items
afw::table::Key< double > weight
afw::table::Key< int > cacheSize
afw::table::PointKey< double > averagePosition
afw::table::Key< int > coaddWcs
afw::table::Schema schema
afw::table::Key< std::string > warpingKernelName
afw::table::Key< double > b
#define LSST_EXCEPT_ADD(e, m)
#define LSST_EXCEPT(type,...)
#define LSST_ARCHIVE_ASSERT(EXPR)
lsst::geom::Box2I computeBBox(lsst::geom::Point2D position, image::Color color=image::Color()) const
std::shared_ptr< Image > computeKernelImage(lsst::geom::Point2D position, image::Color color=image::Color(), ImageOwnerEnum owner=COPY) const
void scaledPlus(double const c, Image< PixelT > const &rhs)
Field< T >::Value get(Key< T > const &key) const
std::shared_ptr< BaseRecord > addNew()
std::shared_ptr< Table > getTable() const
std::shared_ptr< RecordT > const get(size_type i) const
void push_back(Record const &r)
Base::const_iterator const_iterator
static ExposureCatalogT readFromArchive(io::InputArchive const &archive, BaseCatalog const &catalog)
void writeToArchive(io::OutputArchiveHandle &handle, bool ignoreUnpersistable=true) const
ExposureCatalogT subsetContaining(lsst::geom::SpherePoint const &coord, bool includeValidPolygon=false) const
static Schema makeMinimalSchema()
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
PersistableFactory(std::string const &name)
io::OutputArchiveHandle OutputArchiveHandle
void clip(Box2I const &other) noexcept
void include(Point2I const &point)
std::shared_ptr< afw::table::io::Persistable > readV0(InputArchive const &archive, CatalogVector const &catalogs) const
Factory(std::string const &name)
virtual std::shared_ptr< afw::table::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
std::string getPersistenceName() const override
std::shared_ptr< afw::detection::Psf > clone() const override
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
std::shared_ptr< afw::detection::Psf const > getPsf(int index)
Get the Psf of the component image at index.
void write(OutputArchiveHandle &handle) const override
std::string getPythonModule() const override
double getWeight(int index)
Get the weight of the component image at index.
afw::table::RecordId getId(int index)
Get the exposure ID of the component image at index.
geom::Box2I getBBox(int index)
Get the bounding box (in component image Pixel coordinates) of the component image at index.
std::shared_ptr< afw::detection::Psf::Image > doComputeKernelImage(geom::Point2D const &ccdXY, afw::image::Color const &color) const override
std::shared_ptr< afw::detection::Psf > resized(int width, int height) const override
Return a clone with specified kernel dimensions.
std::shared_ptr< afw::geom::polygon::Polygon const > getValidPolygon(int index)
Get the validPolygon (in component image Pixel coordinates) of the component image at index.
CoaddPsf(afw::table::ExposureCatalog const &catalog, afw::geom::SkyWcs const &coaddWcs, std::string const &weightFieldName="weight", std::string const &warpingKernelName="lanczos3", int cacheSize=10000)
Main constructors for CoaddPsf.
geom::Box2I doComputeBBox(geom::Point2D const &position, afw::image::Color const &color) const override
int getComponentCount() const
Return the number of component Psfs in this CoaddPsf.
afw::geom::SkyWcs getWcs(int index)
Get the Wcs of the component image at index.
A Psf class that maps an arbitrary Psf through a coordinate transformation.
std::shared_ptr< TransformPoint2ToPoint2 > makeWcsPairTransform(SkyWcs const &src, SkyWcs const &dst)
ExposureCatalogT< ExposureRecord > ExposureCatalog
constexpr Angle operator+(Angle a, Angle d) noexcept
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
constexpr Angle operator-(Angle a, Angle d) noexcept
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
Point< double, 2 > Point2D
void addToImage(std::shared_ptr< afw::image::Image< double >> image, std::vector< std::shared_ptr< afw::image::Image< double >>> const &imgVector, std::vector< double > const &weightVector)
geom::Box2I getOverallBBox(std::vector< std::shared_ptr< afw::image::Image< double >>> const &imgVector)