lsst.afw  13.0-49-g6f23dda
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
54 using lsst::daf::base::Persistable;
55 using lsst::daf::persistence::BoostStorage;
56 using lsst::daf::persistence::XmlStorage;
57 using lsst::daf::persistence::FitsStorage;
58 using lsst::daf::persistence::Storage;
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>
98 lsst::daf::persistence::FormatterRegistration DecoratedImageFormatter<ImagePixelT>::registration(
100  createInstance);
101 
102 template <typename ImagePixelT>
103 DecoratedImageFormatter<ImagePixelT>::DecoratedImageFormatter(std::shared_ptr<lsst::pex::policy::Policy>)
104  : lsst::daf::persistence::Formatter(typeid(this)) {}
105 
106 template <typename ImagePixelT>
108 
109 template <typename ImagePixelT>
110 void DecoratedImageFormatter<ImagePixelT>::write(Persistable const* persistable,
111  std::shared_ptr<Storage> storage,
112  std::shared_ptr<lsst::daf::base::PropertySet>) {
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  if (typeid(*storage) == typeid(BoostStorage)) {
119  LOGL_DEBUG(_log, "DecoratedImageFormatter write BoostStorage");
120  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
121  boost->getOArchive() & *ip;
122  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
123  return;
124  } else if (typeid(*storage) == typeid(XmlStorage)) {
125  LOGL_DEBUG(_log, "DecoratedImageFormatter write XmlStorage");
126  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
127  boost->getOArchive() & make_nvp("img", *ip);
128  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
129  return;
130  } else if (typeid(*storage) == typeid(FitsStorage)) {
131  LOGL_DEBUG(_log, "DecoratedImageFormatter write FitsStorage");
132  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
134 
135  ip->writeFits(fits->getPath());
136  // @todo Do something with these fields?
137  // int _X0;
138  // int _Y0;
139  LOGL_DEBUG(_log, "DecoratedImageFormatter write end");
140  return;
141  }
142  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for DecoratedImage");
143 }
144 
145 template <typename ImagePixelT>
146 Persistable* DecoratedImageFormatter<ImagePixelT>::read(std::shared_ptr<Storage> storage,
147  std::shared_ptr<lsst::daf::base::PropertySet>) {
148  LOGL_DEBUG(_log, "DecoratedImageFormatter read start");
149  if (typeid(*storage) == typeid(BoostStorage)) {
150  LOGL_DEBUG(_log, "DecoratedImageFormatter read BoostStorage");
151  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
153  boost->getIArchive() & *ip;
154  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
155  return ip;
156  } else if (typeid(*storage) == typeid(XmlStorage)) {
157  LOGL_DEBUG(_log, "DecoratedImageFormatter read XmlStorage");
158  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
160  boost->getIArchive() & make_nvp("img", *ip);
161  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
162  return ip;
163  } else if (typeid(*storage) == typeid(FitsStorage)) {
164  LOGL_DEBUG(_log, "DecoratedImageFormatter read FitsStorage");
165  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
166 
167  DecoratedImage<ImagePixelT>* ip = new DecoratedImage<ImagePixelT>(fits->getPath(), fits->getHdu());
168  // @todo Do something with these fields?
169  // int _X0;
170  // int _Y0;
171  LOGL_DEBUG(_log, "DecoratedImageFormatter read end");
172  return ip;
173  }
174  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for DecoratedImage");
175 }
176 
177 template <typename ImagePixelT>
178 void DecoratedImageFormatter<ImagePixelT>::update(lsst::daf::base::Persistable*,
179  std::shared_ptr<lsst::daf::persistence::Storage>,
180  std::shared_ptr<lsst::daf::base::PropertySet>) {
181  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for DecoratedImage");
182 }
183 
184 template <typename ImagePixelT>
185 template <class Archive>
186 void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(Archive&, int const, Persistable* persistable) {
187  LOGL_DEBUG(_log, "DecoratedImageFormatter delegateSerialize start");
188  DecoratedImage<ImagePixelT>* ip = dynamic_cast<DecoratedImage<ImagePixelT>*>(persistable);
189  if (ip == 0) {
190  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-DecoratedImage");
191  }
192  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
193  "DecoratedImage serialization not yet implemented");
194 }
195 
196 template <typename ImagePixelT>
197 std::shared_ptr<lsst::daf::persistence::Formatter> DecoratedImageFormatter<ImagePixelT>::createInstance(
198  std::shared_ptr<lsst::pex::policy::Policy> policy) {
199  return std::shared_ptr<lsst::daf::persistence::Formatter>(
201 }
202 
203 #define InstantiateFormatter(ImagePixelT) \
204  template class DecoratedImageFormatter<ImagePixelT>; \
205  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, \
206  int const, Persistable*); \
207  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, \
208  int const, Persistable*); \
209  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
210  int const, Persistable*); \
211  template void DecoratedImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
212  int const, Persistable*);
213 
214 InstantiateFormatter(std::uint16_t);
216 InstantiateFormatter(float);
217 InstantiateFormatter(double);
218 InstantiateFormatter(std::uint64_t);
219 
220 #undef InstantiateFormatter
221 }
222 }
223 } // namespace lsst::afw::formatters
#define __attribute__(x)
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
#define InstantiateFormatter(ImagePixelT)
Class implementing persistence and retrieval for DecoratedImages.
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.
A container for an Image and its associated metadata.
Definition: Image.h:760