lsst.meas.algorithms  16.0-7-ge62bbd2a+2
DoubleGaussianPsf.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 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 
24 #include <cmath>
25 
26 #include "lsst/pex/exceptions.h"
35 
37 
38 namespace lsst {
39 namespace meas {
40 namespace algorithms {
41 
42 namespace {
43 
44 // Read-only singleton struct containing the schema and keys that a double-Gaussian Psf is mapped
45 // to in record persistence.
46 struct DoubleGaussianPsfPersistenceHelper {
47  afw::table::Schema schema;
48  afw::table::PointKey<int> dimensions;
49  afw::table::Key<double> sigma1;
50  afw::table::Key<double> sigma2;
51  afw::table::Key<double> b;
52 
53  static DoubleGaussianPsfPersistenceHelper const& get() {
54  static DoubleGaussianPsfPersistenceHelper instance;
55  return instance;
56  }
57 
58  // No copying
59  DoubleGaussianPsfPersistenceHelper(const DoubleGaussianPsfPersistenceHelper&) = delete;
60  DoubleGaussianPsfPersistenceHelper& operator=(const DoubleGaussianPsfPersistenceHelper&) = delete;
61 
62  // No moving
63  DoubleGaussianPsfPersistenceHelper(DoubleGaussianPsfPersistenceHelper&&) = delete;
64  DoubleGaussianPsfPersistenceHelper& operator=(DoubleGaussianPsfPersistenceHelper&&) = delete;
65 
66 private:
67  DoubleGaussianPsfPersistenceHelper()
68  : schema(),
69  dimensions(afw::table::PointKey<int>::addFields(schema, "dimensions", "width/height of kernel",
70  "pixel")),
71  sigma1(schema.addField<double>("sigma1", "radius of inner Gaussian", "pixel")),
72  sigma2(schema.addField<double>("sigma2", "radius of outer Gaussian", "pixel")),
73  b(schema.addField<double>("b", "central amplitude of outer Gaussian (inner amplitude == 1)")) {
74  schema.getCitizen().markPersistent();
75  }
76 };
77 
78 class DoubleGaussianPsfFactory : public afw::table::io::PersistableFactory {
79 public:
80  virtual PTR(afw::table::io::Persistable)
81  read(InputArchive const& archive, CatalogVector const& catalogs) const {
82  static DoubleGaussianPsfPersistenceHelper const& keys = DoubleGaussianPsfPersistenceHelper::get();
83  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
84  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
85  afw::table::BaseRecord const& record = catalogs.front().front();
86  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
87  return std::make_shared<DoubleGaussianPsf>(
88  record.get(keys.dimensions.getX()), record.get(keys.dimensions.getY()),
89  record.get(keys.sigma1), record.get(keys.sigma2), record.get(keys.b));
90  }
91 
92  DoubleGaussianPsfFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
93 };
94 
95 // Helper function for ctor: need to construct the kernel to pass to KernelPsf, because we
96 // can't change it after construction.
97 PTR(afw::math::Kernel)
98 makeDoubleGaussianKernel(int width, int height, double sigma1, double& sigma2, double b) {
99  if (b == 0.0 && sigma2 == 0.0) {
100  sigma2 = 1.0; // avoid 0/0 at centre of Psf
101  }
102  if (sigma1 <= 0 || sigma2 <= 0) {
103  throw LSST_EXCEPT(pex::exceptions::DomainError,
104  (boost::format("sigma may not be 0: %g, %g") % sigma1 % sigma2).str());
105  }
106  afw::math::DoubleGaussianFunction2<double> dg(sigma1, sigma2, b);
107  PTR(afw::math::Kernel) kernel(new afw::math::AnalyticKernel(width, height, dg));
108  return kernel;
109 }
110 
111 std::string getDoubleGaussianPsfPersistenceName() { return "DoubleGaussianPsf"; }
112 
113 DoubleGaussianPsfFactory registration(getDoubleGaussianPsfPersistenceName());
114 
115 } // namespace
116 
117 DoubleGaussianPsf::DoubleGaussianPsf(int width, int height, double sigma1, double sigma2, double b)
118  : KernelPsf(makeDoubleGaussianKernel(width, height, sigma1, sigma2, b)),
119  _sigma1(sigma1),
120  _sigma2(sigma2),
121  _b(b) {}
122 
124  return std::make_shared<DoubleGaussianPsf>(getKernel()->getWidth(), getKernel()->getHeight(), _sigma1,
125  _sigma2, _b);
126 }
127 
128 PTR(afw::detection::Psf) DoubleGaussianPsf::resized(int width, int height) const {
129  return std::make_shared<DoubleGaussianPsf>(width, height, _sigma1, _sigma2, _b);
130 }
131 
132 std::string DoubleGaussianPsf::getPersistenceName() const { return getDoubleGaussianPsfPersistenceName(); }
133 
135  static DoubleGaussianPsfPersistenceHelper const& keys = DoubleGaussianPsfPersistenceHelper::get();
136  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
137  PTR(afw::table::BaseRecord) record = catalog.addNew();
138  (*record).set(keys.dimensions.getX(), getKernel()->getWidth());
139  (*record).set(keys.dimensions.getY(), getKernel()->getHeight());
140  (*record).set(keys.sigma1, getSigma1());
141  (*record).set(keys.sigma2, getSigma2());
142  (*record).set(keys.b, getB());
143  handle.saveCatalog(catalog);
144 }
145 
146 } // namespace algorithms
147 } // namespace meas
148 } // namespace lsst
149 
150 namespace lsst {
151 namespace afw {
152 namespace detection {
153 
155 daf::persistence::FormatterRegistration PsfFormatter::doubleGaussianPsfRegistration(
156  "DoubleGaussianPsf", typeid(meas::algorithms::DoubleGaussianPsf), createInstance);
158 
159 } // namespace detection
160 } // namespace afw
161 } // namespace lsst
def keys(self)
A Psf defined by a Kernel.
Definition: KernelPsf.h:36
virtual void write(OutputArchiveHandle &handle) const
#define LSST_ARCHIVE_ASSERT(EXPR)
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
double getSigma1() const
Return the radius of the inner Gaussian.
#define PTR(...)
boost::shared_ptr< afw::math::Kernel const > getKernel() const
Return the Kernel used to define this Psf.
Definition: KernelPsf.h:52
DoubleGaussianPsf(int width, int height, double sigma1, double sigma2=0.0, double b=0.0)
Constructor for a DoubleGaussianPsf.
string name
STL class.
Represent a Psf as a circularly symmetrical double Gaussian.
virtual std::string getPersistenceName() const
afw::table::PointKey< int > dimensions
virtual boost::shared_ptr< afw::detection::Psf > resized(int width, int height) const
Return a clone with specified kernel dimensions.
double getSigma2() const
Return the radius of the outer Gaussian.
#define LSST_EXCEPT(type,...)
afw::table::Key< double > sigma1
afw::table::Schema schema
double getB() const
Return the ratio of Gaussian peak amplitudes: outer/inner.
io::OutputArchiveHandle OutputArchiveHandle
afw::table::Key< double > b
virtual boost::shared_ptr< afw::detection::Psf > clone() const
Polymorphic deep copy. Usually unnecessary, as Psfs are immutable.
afw::table::Key< double > sigma2
std::shared_ptr< BaseRecord > addNew()