lsst.afw  13.0-92-g8ecf155+1
 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::FormatterStorage;
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<FormatterStorage> 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  // TODO: Replace this with something better in DM-10776
125  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
126  if (boost) {
127  LOGL_DEBUG(_log, "MaskedImageFormatter write BoostStorage");
128  boost->getOArchive() & *ip;
129  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
130  return;
131  }
132  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
133  if (fits) {
134  LOGL_DEBUG(_log, "MaskedImageFormatter write FitsStorage");
135  ip->writeFits(fits->getPath());
136  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
137  return;
138  }
139  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for MaskedImage");
140 }
141 
142 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
144  std::shared_ptr<FormatterStorage> storage, std::shared_ptr<lsst::daf::base::PropertySet>) {
145  LOGL_DEBUG(_log, "MaskedImageFormatter read start");
146  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
147  // TODO: Replace this with something better in DM-10776
148  if (boost) {
149  LOGL_DEBUG(_log, "MaskedImageFormatter read BoostStorage");
151  boost->getIArchive() & *ip;
152  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
153  return ip;
154  }
155  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
156  if (fits) {
157  LOGL_DEBUG(_log, "MaskedImageFormatter read FitsStorage");
159  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
160  return ip;
161  }
162  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for MaskedImage");
163 }
164 
165 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
167  Persistable*, std::shared_ptr<FormatterStorage>, std::shared_ptr<lsst::daf::base::PropertySet>) {
168  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for MaskedImage");
169 }
170 
171 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
172 template <class Archive>
174  Archive& ar, unsigned int const, Persistable* persistable) {
175  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize start");
177  dynamic_cast<MaskedImage<ImagePixelT, MaskPixelT, VariancePixelT>*>(persistable);
178  if (ip == 0) {
179  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-MaskedImage");
180  }
181  ar & ip->_image & ip->_variance & ip->_mask;
182  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize end");
183 }
184 
185 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
186 std::shared_ptr<lsst::daf::persistence::Formatter>
188  std::shared_ptr<lsst::pex::policy::Policy> policy) {
189  return std::shared_ptr<lsst::daf::persistence::Formatter>(
191 }
192 
194 #define INSTANTIATE(I, M, V) \
195  template class MaskedImageFormatter<I, M, V>; \
196  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
197  boost::archive::text_oarchive&, unsigned int const, Persistable*); \
198  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
199  boost::archive::text_iarchive&, unsigned int const, Persistable*); \
200  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
201  boost::archive::binary_oarchive&, unsigned int const, Persistable*); \
202  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
203  boost::archive::binary_iarchive&, unsigned int const, Persistable*);
204 
207 INSTANTIATE(float, MaskPixel, VariancePixel)
208 INSTANTIATE(double, MaskPixel, VariancePixel)
209 INSTANTIATE(uint64_t, MaskPixel, VariancePixel)
211 }
212 }
213 } // namespace lsst::afw::formatters
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
#define INSTANTIATE(TYPE)
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
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
#define __attribute__(x)
Class implementing persistence and retrieval for MaskedImages.
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
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)
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)