LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 : private boost::noncopyable {
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 private:
57  DoubleGaussianPsfPersistenceHelper() :
58  schema(),
59  dimensions(
60  afw::table::PointKey<int>::addFields(schema, "dimensions", "width/height of kernel", "pixels")
61  ),
62  sigma1(schema.addField<double>("sigma1", "radius of inner Gaussian", "pixels")),
63  sigma2(schema.addField<double>("sigma2", "radius of outer Gaussian", "pixels")),
64  b(schema.addField<double>("b", "central amplitude of outer Gaussian (inner amplitude == 1)"))
65  {
66  schema.getCitizen().markPersistent();
67  }
68 };
69 
70 class DoubleGaussianPsfFactory : public afw::table::io::PersistableFactory {
71 public:
72 
73  virtual PTR(afw::table::io::Persistable)
74  read(InputArchive const & archive, CatalogVector const & catalogs) const {
75  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
76  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
77  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
78  afw::table::BaseRecord const & record = catalogs.front().front();
79  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
80  return boost::make_shared<DoubleGaussianPsf>(
81  record.get(keys.dimensions.getX()),
82  record.get(keys.dimensions.getY()),
83  record.get(keys.sigma1),
84  record.get(keys.sigma2),
85  record.get(keys.b)
86  );
87  }
88 
89  DoubleGaussianPsfFactory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
90 };
91 
92 // Helper function for ctor: need to construct the kernel to pass to KernelPsf, because we
93 // can't change it after construction.
94 PTR(afw::math::Kernel) makeDoubleGaussianKernel(
95  int width, int height, double sigma1, double & sigma2, double b
96 ) {
97  if (b == 0.0 && sigma2 == 0.0) {
98  sigma2 = 1.0; // avoid 0/0 at centre of Psf
99  }
100  if (sigma1 <= 0 || sigma2 <= 0) {
101  throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
102  (boost::format("sigma may not be 0: %g, %g") % sigma1 % sigma2).str());
103  }
104  afw::math::DoubleGaussianFunction2<double> dg(sigma1, sigma2, b);
105  PTR(afw::math::Kernel) kernel(new afw::math::AnalyticKernel(width, height, dg));
106  return kernel;
107 }
108 
109 std::string getDoubleGaussianPsfPersistenceName() { return "DoubleGaussianPsf"; }
110 
111 DoubleGaussianPsfFactory registration(getDoubleGaussianPsfPersistenceName());
112 
113 } // anonymous
114 
115 DoubleGaussianPsf::DoubleGaussianPsf(int width, int height, double sigma1, double sigma2, double b) :
116  KernelPsf(makeDoubleGaussianKernel(width, height, sigma1, sigma2, b)),
117  _sigma1(sigma1), _sigma2(sigma2), _b(b)
118 {}
119 
121  return boost::make_shared<DoubleGaussianPsf>(
122  getKernel()->getWidth(),
123  getKernel()->getHeight(),
124  _sigma1, _sigma2, _b
125  );
126 }
127 
128 std::string DoubleGaussianPsf::getPersistenceName() const { return getDoubleGaussianPsfPersistenceName(); }
129 
131  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
132  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
133  PTR(afw::table::BaseRecord) record = catalog.addNew();
134  (*record).set(keys.dimensions.getX(), getKernel()->getWidth());
135  (*record).set(keys.dimensions.getY(), getKernel()->getHeight());
136  (*record).set(keys.sigma1, getSigma1());
137  (*record).set(keys.sigma2, getSigma2());
138  (*record).set(keys.b, getB());
139  handle.saveCatalog(catalog);
140 }
141 
142 }}} // namespace lsst::meas::algorithms
143 
144 namespace lsst { namespace afw { namespace detection {
145 
147 daf::persistence::FormatterRegistration
148 PsfFormatter::doubleGaussianPsfRegistration(
149  "DoubleGaussianPsf", typeid(meas::algorithms::DoubleGaussianPsf), createInstance
150 );
152 
153 }}} // namespace lsst::afw::detection
154 
#define PTR(...)
Definition: base.h:41
A Psf defined by a Kernel.
Definition: KernelPsf.h:33
table::Key< std::string > name
Definition: ApCorrMap.cc:71
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
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:47
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
Define a collection of useful Functions.
Image utility functions.
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:469
Represent a Psf as a circularly symmetrical double Gaussian.
afw::table::PointKey< int > dimensions
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.
tbl::Schema schema
Eigen::VectorXd _b
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Base class for all records.
Definition: BaseRecord.h:27
Interface for PsfFormatter class.
afw::table::Key< double > sigma1
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
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
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Include files required for standard LSST Exception handling.