lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MaskedImageFormatter.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 MaskedImageFormatter 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 <iostream>
36 #include <string>
37 
38 #include "boost/serialization/shared_ptr.hpp"
39 #include <boost/archive/binary_iarchive.hpp>
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/text_iarchive.hpp>
42 #include <boost/archive/text_oarchive.hpp>
43 
44 #include "lsst/daf/base.h"
45 #include "lsst/daf/persistence.h"
46 #include "lsst/log/Log.h"
51 
52 namespace {
53 LOG_LOGGER _log = LOG_GET("afw.MaskedImageFormatter");
54 }
55 
56 using lsst::daf::base::Persistable;
57 using lsst::daf::persistence::BoostStorage;
58 using lsst::daf::persistence::FitsStorage;
59 using lsst::daf::persistence::Storage;
63 
64 namespace lsst {
65 namespace afw {
66 namespace formatters {
67 
68 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
70 public:
71  static std::string name();
72 };
73 
74 template <>
76  static std::string name = "MaskedImageU";
77  return name;
78 }
79 template <>
81  static std::string name = "MaskedImageI";
82  return name;
83 }
84 template <>
86  static std::string name = "MaskedImageF";
87  return name;
88 }
89 template <>
91  static std::string name = "MaskedImageD";
92  return name;
93 }
94 template <>
96  static std::string name = "MaskedImageL";
97  return name;
98 }
99 
100 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
101 lsst::daf::persistence::FormatterRegistration
105 
106 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
108  std::shared_ptr<lsst::pex::policy::Policy>)
109  : lsst::daf::persistence::Formatter(typeid(this)) {}
110 
111 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
113 
114 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
116  Persistable const* persistable, std::shared_ptr<Storage> storage,
117  std::shared_ptr<lsst::daf::base::PropertySet>) {
118  LOGL_DEBUG(_log, "MaskedImageFormatter write start");
120  dynamic_cast<MaskedImage<ImagePixelT, MaskPixelT> const*>(persistable);
121  if (ip == 0) {
122  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-MaskedImage");
123  }
124  if (typeid(*storage) == typeid(BoostStorage)) {
125  LOGL_DEBUG(_log, "MaskedImageFormatter write BoostStorage");
126  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
127  boost->getOArchive() & *ip;
128  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
129  return;
130  } else if (typeid(*storage) == typeid(FitsStorage)) {
131  LOGL_DEBUG(_log, "MaskedImageFormatter write FitsStorage");
132  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
133  ip->writeFits(fits->getPath());
134  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
135  return;
136  }
137  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for MaskedImage");
138 }
139 
140 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
142  std::shared_ptr<Storage> storage, std::shared_ptr<lsst::daf::base::PropertySet>) {
143  LOGL_DEBUG(_log, "MaskedImageFormatter read start");
144  if (typeid(*storage) == typeid(BoostStorage)) {
145  LOGL_DEBUG(_log, "MaskedImageFormatter read BoostStorage");
146  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
148  boost->getIArchive() & *ip;
149  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
150  return ip;
151  } else if (typeid(*storage) == typeid(FitsStorage)) {
152  LOGL_DEBUG(_log, "MaskedImageFormatter read FitsStorage");
153  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
155  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
156  return ip;
157  }
158  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for MaskedImage");
159 }
160 
161 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
163  Persistable*, std::shared_ptr<Storage>, std::shared_ptr<lsst::daf::base::PropertySet>) {
164  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for MaskedImage");
165 }
166 
167 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
168 template <class Archive>
170  Archive& ar, unsigned int const, Persistable* persistable) {
171  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize start");
173  dynamic_cast<MaskedImage<ImagePixelT, MaskPixelT, VariancePixelT>*>(persistable);
174  if (ip == 0) {
175  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-MaskedImage");
176  }
177  ar & ip->_image & ip->_variance & ip->_mask;
178  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize end");
179 }
180 
181 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
182 std::shared_ptr<lsst::daf::persistence::Formatter>
184  std::shared_ptr<lsst::pex::policy::Policy> policy) {
185  return std::shared_ptr<lsst::daf::persistence::Formatter>(
187 }
188 
190 #define INSTANTIATE(I, M, V) \
191  template class MaskedImageFormatter<I, M, V>; \
192  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
193  boost::archive::text_oarchive&, unsigned int const, Persistable*); \
194  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
195  boost::archive::text_iarchive&, unsigned int const, Persistable*); \
196  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
197  boost::archive::binary_oarchive&, unsigned int const, Persistable*); \
198  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
199  boost::archive::binary_iarchive&, unsigned int const, Persistable*);
200 
203 INSTANTIATE(float, MaskPixel, VariancePixel)
204 INSTANTIATE(double, MaskPixel, VariancePixel)
205 INSTANTIATE(uint64_t, MaskPixel, VariancePixel)
207 }
208 }
209 } // namespace lsst::afw::formatters
std::uint16_t MaskPixel
default type for Masks and MaskedImage Masks
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
#define INSTANTIATE(TYPE)
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)
void writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > imageMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > maskMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > varianceMetadata=std::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
Definition: MaskedImage.cc:481
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
#define __attribute__(x)
Class implementing persistence and retrieval for MaskedImages.
float VariancePixel
default type for MaskedImage variance images
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)