LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
ImageFormatter.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 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
43 #include "boost/scoped_ptr.hpp"
44 #include "boost/serialization/shared_ptr.hpp"
45 #include "boost/serialization/binary_object.hpp"
46 #include "boost/serialization/nvp.hpp"
47 
48 #include <boost/archive/text_oarchive.hpp>
49 #include <boost/archive/text_iarchive.hpp>
50 #include <boost/archive/binary_oarchive.hpp>
51 #include <boost/archive/binary_iarchive.hpp>
52 
53 #include "lsst/daf/base.h"
54 #include "lsst/daf/persistence.h"
55 #include "lsst/pex/logging/Trace.h"
57 #include "lsst/afw/image/Image.h"
58 
59 
60 #define EXEC_TRACE 20
61 static void execTrace(std::string s, int level = EXEC_TRACE) {
62  lsst::pex::logging::Trace("afw.ImageFormatter", level, s);
63 }
64 
65 using boost::serialization::make_nvp;
72 
73 namespace afwImg = lsst::afw::image;
74 namespace afwGeom = lsst::afw::geom;
75 
76 namespace lsst {
77 namespace afw {
78 namespace formatters {
79 
80 template <typename ImagePixelT>
82 public:
83  static std::string name();
84 };
85 
87  static std::string name = "ImageU";
88  return name;
89 }
90 template<> std::string ImageFormatterTraits<int>::name() {
91  static std::string name = "ImageI";
92  return name;
93 }
94 template<> std::string ImageFormatterTraits<float>::name() {
95  static std::string name = "ImageF";
96  return name;
97 }
98 template<> std::string ImageFormatterTraits<double>::name() {
99  static std::string name = "ImageD";
100  return name;
101 }
103  static std::string name = "ImageL";
104  return name;
105 }
106 
107 template <typename ImagePixelT>
110  typeid(Image<ImagePixelT>),
111  createInstance);
112 
113 template <typename ImagePixelT>
116  )
117  :
118  lsst::daf::persistence::Formatter(typeid(this))
119 {
120 }
121 
122 template <typename ImagePixelT>
124 {
125 }
126 
127 namespace {
128 namespace dafBase = lsst::daf::base;
129 namespace afwImage = lsst::afw::image;
130 }
131 
132 template <typename ImagePixelT>
134  Persistable const* persistable,
135  Storage::Ptr storage,
137 
138  execTrace("ImageFormatter write start");
139  Image<ImagePixelT> const* ip = dynamic_cast<Image<ImagePixelT> const*>(persistable);
140  if (ip == 0) {
141  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Image");
142  }
143  if (typeid(*storage) == typeid(BoostStorage)) {
144  execTrace("ImageFormatter write BoostStorage");
145  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
146  boost->getOArchive() & *ip;
147  execTrace("ImageFormatter write end");
148  return;
149  }
150  else if (typeid(*storage) == typeid(XmlStorage)) {
151  execTrace("ImageFormatter write XmlStorage");
152  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
153  boost->getOArchive() & make_nvp("img", *ip);
154  execTrace("ImageFormatter write end");
155  return;
156  }
157  else if (typeid(*storage) == typeid(FitsStorage)) {
158  execTrace("ImageFormatter write FitsStorage");
159  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
160  typedef Image<ImagePixelT> Image;
161 
162  ip->writeFits(fits->getPath());
163  // \todo Do something with these fields?
164  // int _X0;
165  // int _Y0;
166  execTrace("ImageFormatter write end");
167  return;
168  }
169  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Image");
170 }
171 
172 template <typename ImagePixelT>
174  lsst::daf::base::PropertySet::Ptr additionalData) {
175  execTrace("ImageFormatter read start");
176  if (typeid(*storage) == typeid(BoostStorage)) {
177  execTrace("ImageFormatter read BoostStorage");
178  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
180  boost->getIArchive() & *ip;
181  execTrace("ImageFormatter read end");
182  return ip;
183  }
184  else if (typeid(*storage) == typeid(XmlStorage)) {
185  execTrace("ImageFormatter read XmlStorage");
186  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
188  boost->getIArchive() & make_nvp("img", *ip);
189  execTrace("ImageFormatter read end");
190  return ip;
191  }
192  else if(typeid(*storage) == typeid(FitsStorage)) {
193 
194  execTrace("ImageFormatter read FitsStorage");
195  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
196  geom::Box2I box;
197  if (additionalData->exists("llcX")) {
198  int llcX = additionalData->get<int>("llcX");
199  int llcY = additionalData->get<int>("llcY");
200  int width = additionalData->get<int>("width");
201  int height = additionalData->get<int>("height");
202  box = geom::Box2I(
203  geom::Point2I(llcX, llcY),
204  geom::Extent2I(width, height)
205  );
206  }
208  if (additionalData->exists("imageOrigin")) {
209  std::string originStr = additionalData->get<std::string>("imageOrigin");
210  if (originStr == "LOCAL") {
211  origin = afwImg::LOCAL;
212  } else if (originStr == "PARENT") {
213  origin = afwImg::PARENT;
214  } else {
215  throw LSST_EXCEPT(
216  lsst::pex::exceptions::RuntimeError,
217  (boost::format("Unknown ImageOrigin type %s specified in additional"
218  "data for retrieving Image from fits")%originStr
219 
220  ).str()
221  );
222  }
223  }
225 
227  fits->getPath(), fits->getHdu(),
229  box, origin
230  );
231  // \note We're throwing away the metadata
232  // \todo Do something with these fields?
233  // int _X0;
234  // int _Y0;
235  execTrace("ImageFormatter read end");
236  return ip;
237  }
238  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Image");
239 }
240 
241 template <typename ImagePixelT>
243  Persistable*,
244  Storage::Ptr,
246  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Image");
247 }
248 
249 template <typename ImagePixelT> template <class Archive>
251  Archive& ar, int const, Persistable* persistable) {
252  execTrace("ImageFormatter delegateSerialize start");
253  Image<ImagePixelT>* ip = dynamic_cast<Image<ImagePixelT>*>(persistable);
254  if (ip == 0) {
255  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Image");
256  }
257  int width, height;
258  if (Archive::is_saving::value) {
259  width = ip->getWidth();
260  height = ip->getHeight();
261  }
262  ar & make_nvp("width", width) & make_nvp("height", height);
263  if (Archive::is_loading::value) {
264  boost::scoped_ptr<Image<ImagePixelT> > ni(
265  new Image<ImagePixelT>(geom::Extent2I(width, height))
266  );
267  typename Image<ImagePixelT>::Array array = ni->getArray();
268  ar & make_nvp("array",
269  boost::serialization::make_array(array.getData(), array.getNumElements()));
270  ip->swap(*ni);
271  } else {
273  if(array.empty())
274  array = ndarray::copy(ip->getArray());
275  ar & make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
276  }
277 }
278 
279 template <typename ImagePixelT>
283 }
284 
285 #define InstantiateFormatter(ImagePixelT) \
286  template class ImageFormatter<ImagePixelT >; \
287  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::text_oarchive&, int const, Persistable*); \
288  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::text_iarchive&, int const, Persistable*); \
289  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::xml_oarchive&, int const, Persistable*); \
290  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::xml_iarchive&, int const, Persistable*); \
291  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::binary_oarchive&, int const, Persistable*); \
292  template void ImageFormatter<ImagePixelT >::delegateSerialize(boost::archive::binary_iarchive&, int const, Persistable*);
293 
294 InstantiateFormatter(boost::uint16_t);
296 InstantiateFormatter(float);
297 InstantiateFormatter(double);
298 InstantiateFormatter(boost::uint64_t);
299 
300 #undef InstantiateSerializer
301 
302 }}} // namespace lsst::afw::formatters
Class for XML file storage.
Definition: XmlStorage.h:57
table::Key< std::string > name
Definition: ApCorrMap.cc:71
boost::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
daf_persistence package header file
virtual boost::archive::text_iarchive & getIArchive(void)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
static lsst::daf::persistence::Formatter::Ptr createInstance(lsst::pex::policy::Policy::Ptr policy)
Array< T, N, C_ > dynamic_dimension_cast(Array< T, N, C > const &array)
Definition: casts.h:105
definition of the Trace messaging facilities
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
static lsst::daf::persistence::FormatterRegistration registration
#define __attribute__(x)
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
Class for FITS file storage.
Definition: FitsStorage.h:52
virtual boost::archive::xml_iarchive & getIArchive(void)
Definition: XmlStorage.cc:114
void writeFits(std::string const &fileName, boost::shared_ptr< lsst::daf::base::PropertySet const > metadata=boost::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
ImageFormatter(lsst::pex::policy::Policy::Ptr policy)
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
int getNumElements() const
Return the total number of elements in the array.
Definition: ArrayBase.h:142
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
#define InstantiateFormatter(ImagePixelT)
Support for 2-D images.
Class implementing persistence and retrieval for Images.
Element * getData() const
Return a raw pointer to the first element of the array.
Definition: ArrayBase.h:117
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
bool empty() const
Return true if the first dimension has no elements.
virtual boost::archive::text_oarchive & getOArchive(void)
A multidimensional strided array.
Definition: Array.h:47
virtual boost::archive::xml_oarchive & getOArchive(void)
Definition: XmlStorage.cc:107
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Class for boost::serialization storage.
Definition: BoostStorage.h:58
Base class for all persistable classes.
Definition: Persistable.h:74
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
void swap(Image &rhs)
Definition: Image.cc:606
A class to represent a 2-dimensional array of pixels.
Definition: PSF.h:43
#define EXEC_TRACE
Abstract base class for storage implementations.
Definition: Storage.h:60
Interface for ImageFormatter class.