lsst.afw  14.0-22-gc76603d1b
DecoratedImageFormatter.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Implementation of DecoratedImageFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <cstdint>
35 #include <memory>
36 #include <string>
37 #include "boost/serialization/shared_ptr.hpp"
38 #include "boost/serialization/binary_object.hpp"
39 #include "boost/serialization/nvp.hpp"
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/binary_iarchive.hpp>
42 
43 #include "lsst/daf/base.h"
44 #include "lsst/daf/persistence.h"
45 #include "lsst/log/Log.h"
47 #include "lsst/afw/image/Image.h"
48 
49 namespace {
50 LOG_LOGGER _log = LOG_GET("afw.DecoratedImageFormatter");
51 }
52 
53 using boost::serialization::make_nvp;
60 
61 namespace lsst {
62 namespace afw {
63 namespace formatters {
64 
65 template <typename ImagePixelT>
67 public:
68  static std::string name();
69 };
70 
71 template <>
73  static std::string name = "DecoratedImageU";
74  return name;
75 }
76 template <>
78  static std::string name = "DecoratedImageI";
79  return name;
80 }
81 template <>
83  static std::string name = "DecoratedImageF";
84  return name;
85 }
86 template <>
88  static std::string name = "DecoratedImageD";
89  return name;
90 }
91 template <>
93  static std::string name = "DecoratedImageL";
94  return name;
95 }
96 
97 template <typename ImagePixelT>
100  createInstance);
101 
102 template <typename ImagePixelT>
104  : lsst::daf::persistence::Formatter(typeid(this)) {}
105 
106 template <typename ImagePixelT>
108 
109 template <typename ImagePixelT>
113  LOGL_DEBUG(_log, "DecoratedImageFormatter write start");
114  DecoratedImage<ImagePixelT> const* ip = dynamic_cast<DecoratedImage<ImagePixelT> const*>(persistable);
115  if (ip == 0) {
116  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-DecoratedImage");
117  }
118 
119  // TODO: Replace this with something better in DM-10776
121  if (boost) {
122  LOGL_DEBUG(_log, "DecoratedImageFormatter write BoostStorage");
123  boost->getOArchive() & *ip;
124  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
125  return;
126  }
127  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
128  if (xml) {
129  LOGL_DEBUG(_log, "DecoratedImageFormatter write XmlStorage");
130  xml->getOArchive() & make_nvp("img", *ip);
131  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
132  return;
133  }
134  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
135  if (fits) {
136  LOGL_DEBUG(_log, "DecoratedImageFormatter write FitsStorage");
137 
138  ip->writeFits(fits->getPath());
139  // @todo Do something with these fields?
140  // int _X0;
141  // int _Y0;
142  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
143  return;
144  }
146  "Unrecognized FormatterStorage for DecoratedImage");
147 }
148 
149 template <typename ImagePixelT>
152  LOGL_DEBUG(_log, "DecoratedImageFormatter read start");
153  // TODO: Replace this with something better in DM-10776
155  if (boost) {
156  LOGL_DEBUG(_log, "DecoratedImageFormatter read BoostStorage");
158  boost->getIArchive() & *ip;
159  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
160  return ip;
161  }
162  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
163  if (xml) {
164  LOGL_DEBUG(_log, "DecoratedImageFormatter read XmlStorage");
166  boost->getIArchive() & make_nvp("img", *ip);
167  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
168  return ip;
169  }
170  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
171  if (fits) {
172  LOGL_DEBUG(_log, "DecoratedImageFormatter read FitsStorage");
173 
174  DecoratedImage<ImagePixelT>* ip = new DecoratedImage<ImagePixelT>(fits->getPath(), fits->getHdu());
175  // @todo Do something with these fields?
176  // int _X0;
177  // int _Y0;
178  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
179  return ip;
180  }
182  "Unrecognized FormatterStorage for DecoratedImage");
183 }
184 
185 template <typename ImagePixelT>
189  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for DecoratedImage");
190 }
191 
192 template <typename ImagePixelT>
193 template <class Archive>
195  LOGL_DEBUG(_log, "DecoratedImageFormatter delegateSerialize start");
196  DecoratedImage<ImagePixelT>* ip = dynamic_cast<DecoratedImage<ImagePixelT>*>(persistable);
197  if (ip == 0) {
198  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-DecoratedImage");
199  }
201  "DecoratedImage serialization not yet implemented");
202 }
203 
204 template <typename ImagePixelT>
209 }
210 
211 #define InstantiateFormatter(ImagePixelT) \
212  template class DecoratedImageFormatter<ImagePixelT>; \
213  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, \
214  int const, Persistable*); \
215  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, \
216  int const, Persistable*); \
217  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
218  int const, Persistable*); \
219  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
220  int const, Persistable*);
221 
224 InstantiateFormatter(float);
225 InstantiateFormatter(double);
227 
228 #undef InstantiateFormatter
229 } // namespace formatters
230 } // namespace afw
231 } // namespace lsst
#define __attribute__(x)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
#define LOGL_DEBUG(logger, message...)
STL class.
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)
A base class for image defects.
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write a FITS file.
T dynamic_pointer_cast(T... args)
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
#define LSST_EXCEPT(type,...)
#define InstantiateFormatter(ImagePixelT)
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Class implementing persistence and retrieval for DecoratedImages.
#define LOG_GET(logger)
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
A container for an Image and its associated metadata.
Definition: Image.h:391