lsst.afw  13.0-100-ge0459d59f+5
PsfFormatter.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * Implementation of PsfFormatter class
4  */
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
9 #include "boost/serialization/nvp.hpp"
10 
12 #include "lsst/afw/detection/Psf.h"
13 #include "lsst/daf/persistence/FormatterImpl.h"
14 #include "lsst/daf/persistence/LogicalLocation.h"
15 #include "lsst/daf/persistence/BoostStorage.h"
16 #include "lsst/daf/persistence/XmlStorage.h"
17 #include "lsst/pex/exceptions.h"
18 #include "lsst/log/Log.h"
19 #include "lsst/pex/policy/Policy.h"
20 
21 BOOST_CLASS_EXPORT(lsst::afw::detection::Psf)
22 
23 namespace {
24 LOG_LOGGER _log = LOG_GET("afw.detection.PsfFormatter");
25 }
26 
27 namespace lsst {
28 namespace afw {
29 namespace detection {
30 
31 namespace dafBase = lsst::daf::base;
33 namespace pexPolicy = lsst::pex::policy;
34 
35 using boost::serialization::make_nvp;
36 
37 dafPersist::FormatterRegistration PsfFormatter::registration("Psf", typeid(Psf), createInstance);
38 
39 PsfFormatter::PsfFormatter(std::shared_ptr<pexPolicy::Policy> policy)
40  : dafPersist::Formatter(typeid(this)), _policy(policy) {}
41 
43 
44 void PsfFormatter::write(dafBase::Persistable const* persistable,
45  std::shared_ptr<dafPersist::FormatterStorage> storage,
46  std::shared_ptr<dafBase::PropertySet>) {
47  LOGL_DEBUG(_log, "PsfFormatter write start");
48  Psf const* ps = dynamic_cast<Psf const*>(persistable);
49  if (ps == 0) {
50  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Psf");
51  }
52  // TODO: Replace this with something better in DM-10776
53  auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
54  if (boost) {
55  LOGL_DEBUG(_log, "PsfFormatter write BoostStorage");
56  boost->getOArchive() & ps;
57  LOGL_DEBUG(_log, "PsfFormatter write end");
58  return;
59  }
60  auto xml = std::dynamic_pointer_cast<dafPersist::XmlStorage>(storage);
61  if (xml) {
62  LOGL_DEBUG(_log, "PsfFormatter write XmlStorage");
63  xml->getOArchive() & make_nvp("psf", ps);
64  LOGL_DEBUG(_log, "PsfFormatter write end");
65  return;
66  }
67  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Psf");
68 }
69 
70 dafBase::Persistable* PsfFormatter::read(std::shared_ptr<dafPersist::FormatterStorage> storage,
71  std::shared_ptr<dafBase::PropertySet>) {
72  LOGL_DEBUG(_log, "PsfFormatter read start");
73  Psf* ps;
74 
75  // TODO: Replace this with something better in DM-10776
76  auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
77  if (boost) {
78  LOGL_DEBUG(_log, "PsfFormatter read BoostStorage");
79  boost->getIArchive() & ps;
80  LOGL_DEBUG(_log, "PsfFormatter read end");
81  return ps;
82  }
83  auto xml = std::dynamic_pointer_cast<dafPersist::XmlStorage>(storage);
84  if (xml) {
85  LOGL_DEBUG(_log, "PsfFormatter read XmlStorage");
86  xml->getIArchive() & make_nvp("psf", ps);
87  LOGL_DEBUG(_log, "PsfFormatter read end");
88  return ps;
89  }
90  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Psf");
91 }
92 
93 void PsfFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
94  std::shared_ptr<dafBase::PropertySet>) {
95  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Psf");
96 }
97 
98 template <class Archive>
99 void PsfFormatter::delegateSerialize(Archive& ar, unsigned int const, dafBase::Persistable* persistable) {
100  LOGL_DEBUG(_log, "PsfFormatter delegateSerialize start");
101  Psf* ps = dynamic_cast<Psf*>(persistable);
102  if (ps == 0) {
103  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Psf");
104  }
105 #if 0 // not present in baseclass
106  ar & make_nvp("width", ps->_width) & make_nvp("height", ps->_height);
107  ar & make_nvp("k", ps->_kernel);
108 #endif
109 
110  LOGL_DEBUG(_log, "PsfFormatter delegateSerialize end");
111 }
112 
113 std::shared_ptr<dafPersist::Formatter> PsfFormatter::createInstance(
114  std::shared_ptr<pexPolicy::Policy> policy) {
115  return std::shared_ptr<dafPersist::Formatter>(new PsfFormatter(policy));
116 }
117 }
118 }
119 } // end lsst::afw::detection
Definition: Span.h:37
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
Serialize a Psf to a Boost archive.
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Definition: PsfFormatter.cc:70
A base class for image defects.
Definition: cameraGeom.dox:3
virtual ~PsfFormatter(void)
Minimal destructor.
Definition: PsfFormatter.cc:42
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Definition: PsfFormatter.cc:93
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Definition: PsfFormatter.cc:44
Formatter for persistence of Psf instances.
Definition: PsfFormatter.h:21
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:70