LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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 
#define PTR(...)
Definition: base.h:41
A Psf defined by a Kernel.
Definition: KernelPsf.h:33
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
Image utility functions.
Define a collection of useful Functions.
Represent a Psf as a circularly symmetrical double Gaussian.
Include files required for standard LSST Exception handling.
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.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given 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.
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
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
afw::table::Key< double > b
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
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
dictionary Point
Definition: __init__.py:38
afw::table::SourceRecord * record