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 &) {
108 geom::Point2D p = coaddWcs.skyToPixel(i->getWcs()->pixelToSky(i->getPsf()->getAveragePosition()));
109 AvgPosItem item(p.getX(), p.getY(), i->get(weightKey));
110 if (goodPixKey.isValid()) {
111 item.w *= i->get(goodPixKey);
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();
143 : _coaddWcs(coaddWcs),
144 _warpingKernelName(warpingKernelName),
145 _warpingControl(
std::make_shared<afw::math::WarpingControl>(warpingKernelName,
"", cacheSize)) {
152 mapper.addMapping(goodPixKey,
true);
159 _weightKey =
mapper.addMapping(weightKey, weightField);
164 record->assign(*i,
mapper);
167 _averagePosition = computeAveragePosition(_catalog, _coaddWcs, _weightKey);
182 for (
unsigned int i = 0; i < imgVector.size(); i++) {
195 assert(imgVector.size() == weightVector.
size());
196 for (
unsigned int i = 0; i < imgVector.size(); i++) {
198 double weight = weightVector[i];
199 double sum = ndarray::asEigenMatrix(componentImg->getArray()).sum();
211 targetSubImage.
scaledPlus(weight / sum, cSubImage);
217 if (subcat.
empty()) {
220 (boost::format(
"Cannot compute BBox at point %s; no input images at that point.") % ccdXY)
225 for (
auto const &exposureRecord : subcat) {
229 geom::Box2I componentBBox = warpedPsf.computeBBox(ccdXY, color);
240 if (subcat.
empty()) {
243 (boost::format(
"Cannot compute CoaddPsf at point %s; no input images at that point.") % ccdXY)
246 double weightSum = 0.0;
254 for (
auto const &exposureRecord : subcat) {
262 LSST_EXCEPT_ADD(exc, (boost::format(
"Computing WarpedPsf kernel image for id=%d") %
263 exposureRecord.getId())
267 imgVector.push_back(componentImg);
268 weightSum += exposureRecord.get(_weightKey);
269 weightVector.
push_back(exposureRecord.get(_weightKey));
288 return _catalog[index].getPsf();
295 return *_catalog[index].getWcs();
302 return _catalog[index].getValidPolygon();
309 return _catalog[index].
get(_weightKey);
316 return _catalog[index].getId();
323 return _catalog[index].getBBox();
335 class CoaddPsfPersistenceHelper {
343 static CoaddPsfPersistenceHelper
const &
get() {
344 static CoaddPsfPersistenceHelper
const instance;
349 CoaddPsfPersistenceHelper()
351 coaddWcs(schema.addField<
int>(
"coaddwcs",
"archive ID of the coadd's WCS")),
352 cacheSize(schema.addField<
int>(
"cachesize",
"size of the warping cache")),
354 schema,
"avgpos",
"PSF accessors default position",
"pixel")),
356 schema.addField<
std::string>(
"warpingkernelname",
"warping kernel name", 32)) {}
365 if (catalogs.size() == 1u) {
369 return readV0(archive, catalogs);
372 CoaddPsfPersistenceHelper
const &keys1 = CoaddPsfPersistenceHelper::get();
378 record1.get(keys1.averagePosition), record1.get(keys1.warpingKernelName),
379 record1.get(keys1.cacheSize)));
391 auto coaddWcs = internalCat.back().getWcs();
392 internalCat.pop_back();
398 weightKey = internalCat.getSchema()[
"weight"];
401 auto averagePos = computeAveragePosition(internalCat, *coaddWcs, weightKey);
410 std::string getCoaddPsfPersistenceName() {
return "CoaddPsf"; }
421 CoaddPsfPersistenceHelper
const &keys1 = CoaddPsfPersistenceHelper::get();
424 auto coaddWcsPtr = std::make_shared<afw::geom::SkyWcs>(_coaddWcs);
425 record1->set(keys1.coaddWcs, handle.put(coaddWcsPtr));
426 record1->set(keys1.cacheSize, _warpingControl->getCacheSize());
427 record1->set(keys1.averagePosition, _averagePosition);
428 record1->set(keys1.warpingKernelName, _warpingKernelName);
429 handle.saveCatalog(cat1);
437 _weightKey(_catalog.getSchema()[
"weight"]),
438 _averagePosition(averagePosition),
439 _warpingKernelName(warpingKernelName),
440 _warpingControl(new afw::math::WarpingControl(warpingKernelName,
"", cacheSize)) {}
void write(OutputArchiveHandle &handle) const override
afw::table::Key< std::string > warpingKernelName
double getWeight(int index)
Get the weight of the component image at index.
ExposureCatalogT subsetContaining(lsst::geom::SpherePoint const &coord, bool includeValidPolygon=false) const
#define LSST_ARCHIVE_ASSERT(EXPR)
std::vector< SchemaItem< Flag > > * items
std::shared_ptr< Table > getTable() const
geom::Box2I getBBox(int index)
Get the bounding box (in component image Pixel coordinates) of the component image at index...
boost::shared_ptr< afw::detection::Psf::Image > doComputeKernelImage(geom::Point2D const &ccdXY, afw::image::Color const &color) const override
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
static Schema makeMinimalSchema()
constexpr Angle operator+(Angle a, Angle d) noexcept
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
boost::shared_ptr< afw::detection::Psf > clone() const override
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
ItemVariant const * other
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.
afw::table::Key< int > cacheSize
afw::table::Schema schema
int getComponentCount() const
Return the number of component Psfs in this CoaddPsf.
afw::table::RecordId getId(int index)
Get the exposure ID of the component image at index.
Factory(std::string const &name)
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
std::string getPersistenceName() const override
boost::shared_ptr< afw::detection::Psf > resized(int width, int height) const override
Return a clone with specified kernel dimensions.
constexpr Angle operator-(Angle a, Angle d) noexcept
void push_back(Record const &r)
std::string getPythonModule() const override
afw::table::Key< int > coaddWcs
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
afw::table::PointKey< double > averagePosition
CoaddPsf is the Psf derived to be used for non-PSF-matched Coadd images.
std::shared_ptr< afw::table::io::Persistable > readV0(InputArchive const &archive, CatalogVector const &catalogs) const
void scaledPlus(double const c, Image< PixelT > const &rhs)
std::unique_ptr< SchemaItem< U > > result
std::shared_ptr< TransformPoint2ToPoint2 > makeWcsPairTransform(SkyWcs const &src, SkyWcs const &dst)
#define LSST_EXCEPT(type,...)
Base::const_iterator const_iterator
void include(Point2I const &point)
afw::geom::SkyWcs getWcs(int index)
Get the Wcs of the component image at index.
geom::Box2I getOverallBBox(std::vector< boost::shared_ptr< afw::image::Image< double > >> const &imgVector)
static ExposureCatalogT readFromArchive(io::InputArchive const &archive, BaseCatalog const &catalog)
void clip(Box2I const &other) noexcept
void writeToArchive(io::OutputArchiveHandle &handle, bool ignoreUnpersistable=true) const
void addToImage(boost::shared_ptr< afw::image::Image< double > > image, std::vector< boost::shared_ptr< afw::image::Image< double > >> const &imgVector, std::vector< double > const &weightVector)
std::shared_ptr< Image > computeKernelImage(lsst::geom::Point2D position=makeNullPoint(), image::Color color=image::Color(), ImageOwnerEnum owner=COPY) const
std::shared_ptr< RecordT > const get(size_type i) const
io::OutputArchiveHandle OutputArchiveHandle
geom::Box2I doComputeBBox(geom::Point2D const &position, afw::image::Color const &color) const override
afw::table::Key< double > b
afw::table::Key< double > weight
ExposureCatalogT< ExposureRecord > ExposureCatalog
#define LSST_EXCEPT_ADD(e, m)
A Psf class that maps an arbitrary Psf through a coordinate transformation.
boost::shared_ptr< afw::detection::Psf const > getPsf(int index)
Get the Psf of the component image at index.
boost::shared_ptr< afw::geom::polygon::Polygon const > getValidPolygon(int index)
Get the validPolygon (in component image Pixel coordinates) of the component image at index...
std::shared_ptr< BaseRecord > addNew()