LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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) {
109  throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
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 DoubleGaussianPsf DoubleGaussianPsf::resized(int width, int height) const{
137  return 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 
162 daf::persistence::FormatterRegistration
163 PsfFormatter::doubleGaussianPsfRegistration(
164  "DoubleGaussianPsf", typeid(meas::algorithms::DoubleGaussianPsf), createInstance
165 );
167 
168 }}} // namespace lsst::afw::detection
169 
DoubleGaussianPsf resized(int width, int height) const
Return a clone with specified kernel dimensions.
A Psf defined by a Kernel.
Definition: KernelPsf.h:33
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double getSigma2() const
Return the radius of the outer Gaussian.
An object passed to Persistable::write to allow it to persist itself.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:45
A custom container class for records, based on std::vector.
Definition: Catalog.h:95
afw::table::Schema schema
Definition: GaussianPsf.cc:41
Include files required for standard LSST Exception handling.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Define a collection of useful Functions.
DoubleGaussianPsf(int width, int height, double sigma1, double sigma2=0.0, double b=0.0)
Constructor for a DoubleGaussianPsf.
Image utility functions.
Represent a Psf as a circularly symmetrical double Gaussian.
boost::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:470
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:42
void ImageT ImageT int float saturatedPixelValue int const width
Definition: saturated.cc:44
boost::shared_ptr< afw::math::Kernel const > getKernel() const
Return the Kernel used to define this Psf.
Definition: KernelPsf.h:52
double getB() const
Return the ratio of Gaussian peak amplitudes: outer/inner.
void ImageT ImageT int float saturatedPixelValue int const height
Definition: saturated.cc:44
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
Base class for all records.
Definition: BaseRecord.h:27
Interface for PsfFormatter class.
afw::table::Key< double > sigma1
#define PTR(...)
Definition: base.h:41
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
double const _b
Definition: RandomImage.cc:78
afw::table::Key< double > b
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
double getSigma1() const
Return the radius of the inner Gaussian.
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
afw::table::Key< double > sigma2