LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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"
34 
36 
37 namespace lsst { namespace meas { namespace algorithms {
38 
39 namespace {
40 
41 // Read-only singleton struct containing the schema and keys that a double-Gaussian Psf is mapped
42 // to in record persistence.
43 struct DoubleGaussianPsfPersistenceHelper : private boost::noncopyable {
44  afw::table::Schema schema;
45  afw::table::Key< afw::table::Point<int> > dimensions;
46  afw::table::Key<double> sigma1;
47  afw::table::Key<double> sigma2;
48  afw::table::Key<double> b;
49 
50  static DoubleGaussianPsfPersistenceHelper const & get() {
51  static DoubleGaussianPsfPersistenceHelper instance;
52  return instance;
53  }
54 
55 private:
56  DoubleGaussianPsfPersistenceHelper() :
57  schema(),
58  dimensions(
59  schema.addField< afw::table::Point<int> >("dimensions", "width/height of kernel", "pixels")
60  ),
61  sigma1(schema.addField<double>("sigma1", "radius of inner Gaussian", "pixels")),
62  sigma2(schema.addField<double>("sigma2", "radius of outer Gaussian", "pixels")),
63  b(schema.addField<double>("b", "central amplitude of outer Gaussian (inner amplitude == 1)"))
64  {
65  schema.getCitizen().markPersistent();
66  }
67 };
68 
69 class DoubleGaussianPsfFactory : public afw::table::io::PersistableFactory {
70 public:
71 
72  virtual PTR(afw::table::io::Persistable)
73  read(InputArchive const & archive, CatalogVector const & catalogs) const {
74  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
75  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
76  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
77  afw::table::BaseRecord const & record = catalogs.front().front();
78  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
79  return boost::make_shared<DoubleGaussianPsf>(
80  record.get(keys.dimensions.getX()),
81  record.get(keys.dimensions.getY()),
82  record.get(keys.sigma1),
83  record.get(keys.sigma2),
84  record.get(keys.b)
85  );
86  }
87 
88  DoubleGaussianPsfFactory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
89 };
90 
91 // Helper function for ctor: need to construct the kernel to pass to KernelPsf, because we
92 // can't change it after construction.
93 PTR(afw::math::Kernel) makeDoubleGaussianKernel(
94  int width, int height, double sigma1, double & sigma2, double b
95 ) {
96  if (b == 0.0 && sigma2 == 0.0) {
97  sigma2 = 1.0; // avoid 0/0 at centre of Psf
98  }
99  if (sigma1 <= 0 || sigma2 <= 0) {
100  throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
101  (boost::format("sigma may not be 0: %g, %g") % sigma1 % sigma2).str());
102  }
103  afw::math::DoubleGaussianFunction2<double> dg(sigma1, sigma2, b);
104  PTR(afw::math::Kernel) kernel(new afw::math::AnalyticKernel(width, height, dg));
105  return kernel;
106 }
107 
108 std::string getDoubleGaussianPsfPersistenceName() { return "DoubleGaussianPsf"; }
109 
110 DoubleGaussianPsfFactory registration(getDoubleGaussianPsfPersistenceName());
111 
112 } // anonymous
113 
114 DoubleGaussianPsf::DoubleGaussianPsf(int width, int height, double sigma1, double sigma2, double b) :
115  KernelPsf(makeDoubleGaussianKernel(width, height, sigma1, sigma2, b)),
116  _sigma1(sigma1), _sigma2(sigma2), _b(b)
117 {}
118 
120  return boost::make_shared<DoubleGaussianPsf>(
121  getKernel()->getWidth(),
122  getKernel()->getHeight(),
123  _sigma1, _sigma2, _b
124  );
125 }
126 
127 std::string DoubleGaussianPsf::getPersistenceName() const { return getDoubleGaussianPsfPersistenceName(); }
128 
130  static DoubleGaussianPsfPersistenceHelper const & keys = DoubleGaussianPsfPersistenceHelper::get();
131  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
132  PTR(afw::table::BaseRecord) record = catalog.addNew();
133  (*record).set(keys.dimensions.getX(), getKernel()->getWidth());
134  (*record).set(keys.dimensions.getY(), getKernel()->getHeight());
135  (*record).set(keys.sigma1, getSigma1());
136  (*record).set(keys.sigma2, getSigma2());
137  (*record).set(keys.b, getB());
138  handle.saveCatalog(catalog);
139 }
140 
141 }}} // namespace lsst::meas::algorithms
142 
143 namespace lsst { namespace afw { namespace detection {
144 
146 daf::persistence::FormatterRegistration
147 PsfFormatter::doubleGaussianPsfRegistration(
148  "DoubleGaussianPsf", typeid(meas::algorithms::DoubleGaussianPsf), createInstance
149 );
151 
152 }}} // namespace lsst::afw::detection
153 
Image utility functions.
A Psf defined by a Kernel.
Definition: KernelPsf.h:33
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.
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
afw::table::Key< afw::table::Point< int > > dimensions
Define a collection of useful Functions.
#define PTR(...)
Definition: base.h:41
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.
tbl::Schema schema
Definition: CoaddPsf.cc:324
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.
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
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
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.
dictionary Point
Definition: __init__.py:38
Include files required for standard LSST Exception handling.