lsst.meas.algorithms  14.0-11-gae0d9e9a
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 { namespace meas { namespace algorithms {
39 
40 namespace {
41 
42 // Read-only singleton struct containing the schema and keys that a double-Gaussian Psf is mapped
43 // to in record persistence.
44 struct DoubleGaussianPsfPersistenceHelper {
45  afw::table::Schema schema;
46  afw::table::PointKey<int> dimensions;
47  afw::table::Key<double> sigma1;
48  afw::table::Key<double> sigma2;
49  afw::table::Key<double> b;
50 
51  static DoubleGaussianPsfPersistenceHelper const & get() {
52  static DoubleGaussianPsfPersistenceHelper instance;
53  return instance;
54  }
55 
56  // No copying
57  DoubleGaussianPsfPersistenceHelper (const DoubleGaussianPsfPersistenceHelper&) = delete;
58  DoubleGaussianPsfPersistenceHelper& operator=(const DoubleGaussianPsfPersistenceHelper&) = delete;
59 
60  // No moving
61  DoubleGaussianPsfPersistenceHelper (DoubleGaussianPsfPersistenceHelper&&) = delete;
62  DoubleGaussianPsfPersistenceHelper& operator=(DoubleGaussianPsfPersistenceHelper&&) = delete;
63 
64 private:
65  DoubleGaussianPsfPersistenceHelper() :
66  schema(),
67  dimensions(
68  afw::table::PointKey<int>::addFields(schema, "dimensions", "width/height of kernel", "pixel")
69  ),
70  sigma1(schema.addField<double>("sigma1", "radius of inner Gaussian", "pixel")),
71  sigma2(schema.addField<double>("sigma2", "radius of outer Gaussian", "pixel")),
72  b(schema.addField<double>("b", "central amplitude of outer Gaussian (inner amplitude == 1)"))
73  {
74  schema.getCitizen().markPersistent();
75  }
76 };
77 
78 class DoubleGaussianPsfFactory : public afw::table::io::PersistableFactory {
79 public:
80 
81  virtual PTR(afw::table::io::Persistable)
82  read(InputArchive const & archive, CatalogVector const & catalogs) const {
83  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
84  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
85  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
86  afw::table::BaseRecord const & record = catalogs.front().front();
87  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
88  return std::make_shared<DoubleGaussianPsf>(
89  record.get(keys.dimensions.getX()),
90  record.get(keys.dimensions.getY()),
91  record.get(keys.sigma1),
92  record.get(keys.sigma2),
93  record.get(keys.b)
94  );
95  }
96 
97  DoubleGaussianPsfFactory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
98 };
99 
100 // Helper function for ctor: need to construct the kernel to pass to KernelPsf, because we
101 // can't change it after construction.
102 PTR(afw::math::Kernel) makeDoubleGaussianKernel(
103  int width, int height, double sigma1, double & sigma2, double b
104 ) {
105  if (b == 0.0 && sigma2 == 0.0) {
106  sigma2 = 1.0; // avoid 0/0 at centre of Psf
107  }
108  if (sigma1 <= 0 || sigma2 <= 0) {
110  (boost::format("sigma may not be 0: %g, %g") % sigma1 % sigma2).str());
111  }
112  afw::math::DoubleGaussianFunction2<double> dg(sigma1, sigma2, b);
113  PTR(afw::math::Kernel) kernel(new afw::math::AnalyticKernel(width, height, dg));
114  return kernel;
115 }
116 
117 std::string getDoubleGaussianPsfPersistenceName() { return "DoubleGaussianPsf"; }
118 
119 DoubleGaussianPsfFactory registration(getDoubleGaussianPsfPersistenceName());
120 
121 } // anonymous
122 
123 DoubleGaussianPsf::DoubleGaussianPsf(int width, int height, double sigma1, double sigma2, double b) :
124  KernelPsf(makeDoubleGaussianKernel(width, height, sigma1, sigma2, b)),
125  _sigma1(sigma1), _sigma2(sigma2), _b(b)
126 {}
127 
129  return std::make_shared<DoubleGaussianPsf>(
130  getKernel()->getWidth(),
131  getKernel()->getHeight(),
132  _sigma1, _sigma2, _b
133  );
134 }
135 
136 PTR(afw::detection::Psf) DoubleGaussianPsf::resized(int width, int height) const{
137  return std::make_shared<DoubleGaussianPsf>(
138  width, height,
139  _sigma1, _sigma2, _b
140  );
141 }
142 
143 std::string DoubleGaussianPsf::getPersistenceName() const { return getDoubleGaussianPsfPersistenceName(); }
144 
146  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
147  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
148  PTR(afw::table::BaseRecord) record = catalog.addNew();
149  (*record).set(keys.dimensions.getX(), getKernel()->getWidth());
150  (*record).set(keys.dimensions.getY(), getKernel()->getHeight());
151  (*record).set(keys.sigma1, getSigma1());
152  (*record).set(keys.sigma2, getSigma2());
153  (*record).set(keys.b, getB());
154  handle.saveCatalog(catalog);
155 }
156 
157 }}} // namespace lsst::meas::algorithms
158 
159 namespace lsst { namespace afw { namespace detection {
160 
163 PsfFormatter::doubleGaussianPsfRegistration(
164  "DoubleGaussianPsf", typeid(meas::algorithms::DoubleGaussianPsf), createInstance
165 );
167 
168 }}} // namespace lsst::afw::detection
169 
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
A Psf defined by a Kernel.
Definition: KernelPsf.h:33
virtual void write(OutputArchiveHandle &handle) const
#define LSST_ARCHIVE_ASSERT(EXPR)
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< RecordT > addNew()