lsst.afw  13.0-49-g6f23dda
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MaskFormatter.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 MaskFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <string>
35 
36 #include "boost/serialization/shared_ptr.hpp"
37 #include "boost/serialization/binary_object.hpp"
38 #include <boost/archive/binary_oarchive.hpp>
39 #include <boost/archive/binary_iarchive.hpp>
40 
42 
43 #include "lsst/daf/base.h"
44 #include "lsst/daf/persistence.h"
45 #include "lsst/log/Log.h"
46 #include "lsst/afw/image/Mask.h"
47 
49 
50 namespace {
51 LOG_LOGGER _log = LOG_GET("afw.MaskFormatter");
52 }
53 
54 using lsst::daf::base::Persistable;
55 using lsst::daf::persistence::BoostStorage;
56 using lsst::daf::persistence::FitsStorage;
57 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 = "Mask";
74  return name;
75 }
76 
77 template <typename MaskPixelT>
78 lsst::daf::persistence::FormatterRegistration MaskFormatter<MaskPixelT>::registration(
79  MaskFormatterTraits<MaskPixelT>::name(), typeid(Mask<MaskPixelT>), createInstance);
80 
81 template <typename MaskPixelT>
82 MaskFormatter<MaskPixelT>::MaskFormatter(std::shared_ptr<lsst::pex::policy::Policy>)
83  : lsst::daf::persistence::Formatter(typeid(this)) {}
84 
85 template <typename MaskPixelT>
87 
88 template <typename MaskPixelT>
89 void MaskFormatter<MaskPixelT>::write(Persistable const* persistable, std::shared_ptr<Storage> storage,
90  std::shared_ptr<lsst::daf::base::PropertySet>) {
91  LOGL_DEBUG(_log, "MaskFormatter write start");
92  Mask<MaskPixelT> const* ip = dynamic_cast<Mask<MaskPixelT> const*>(persistable);
93  if (ip == 0) {
94  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Mask");
95  }
96  if (typeid(*storage) == typeid(BoostStorage)) {
97  LOGL_DEBUG(_log, "MaskFormatter write BoostStorage");
98  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
99  boost->getOArchive() & *ip;
100  LOGL_DEBUG(_log, "MaskFormatter write end");
101  return;
102  } else if (typeid(*storage) == typeid(FitsStorage)) {
103  LOGL_DEBUG(_log, "MaskFormatter write FitsStorage");
104  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
105  // Need to cast away const because writeFits modifies the metadata.
106  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
107  vip->writeFits(fits->getPath());
108  LOGL_DEBUG(_log, "MaskFormatter write end");
109  return;
110  }
111  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
112 }
113 
114 template <typename MaskPixelT>
115 Persistable* MaskFormatter<MaskPixelT>::read(std::shared_ptr<Storage> storage,
116  std::shared_ptr<lsst::daf::base::PropertySet>) {
117  LOGL_DEBUG(_log, "MaskFormatter read start");
118  if (typeid(*storage) == typeid(BoostStorage)) {
119  LOGL_DEBUG(_log, "MaskFormatter read BoostStorage");
120  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
122  boost->getIArchive() & *ip;
123  LOGL_DEBUG(_log, "MaskFormatter read end");
124  return ip;
125  } else if (typeid(*storage) == typeid(FitsStorage)) {
126  LOGL_DEBUG(_log, "MaskFormatter read FitsStorage");
127  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
128  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
129  LOGL_DEBUG(_log, "MaskFormatter read end");
130  return ip;
131  }
132  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
133 }
134 
135 template <typename MaskPixelT>
136 void MaskFormatter<MaskPixelT>::update(Persistable*, std::shared_ptr<Storage>,
137  std::shared_ptr<lsst::daf::base::PropertySet>) {
138  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
139 }
140 
141 template <typename MaskPixelT>
142 template <class Archive>
143 void MaskFormatter<MaskPixelT>::delegateSerialize(Archive& ar, int const version, Persistable* persistable) {
144  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize start");
145  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
146  if (ip == 0) {
147  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
148  }
149  ar & ip->_offsetRows & ip->_offsetCols;
150  ar & ip->_maskPlaneDict;
151  unsigned int cols;
152  unsigned int rows;
153  unsigned int planes;
154  if (Archive::is_saving::value) {
155  cols = ip->_vwImagePtr->cols();
156  rows = ip->_vwImagePtr->rows();
157  planes = ip->_vwImagePtr->planes();
158  }
159  ar& cols& rows& planes;
160  if (Archive::is_loading::value) {
161  ip->_vwImagePtr->set_size(cols, rows, planes);
162  }
163  unsigned int pixels = cols * rows * planes;
164  MaskPixelT* data = ip->_vwImagePtr->data();
165  ar& boost::serialization::make_array(data, pixels);
166  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize end");
167 }
168 
169 template <typename MaskPixelT>
170 std::shared_ptr<lsst::daf::persistence::Formatter> MaskFormatter<MaskPixelT>::createInstance(
171  std::shared_ptr<lsst::pex::policy::Policy> policy) {
172  return std::shared_ptr<lsst::daf::persistence::Formatter>(new MaskFormatter<MaskPixelT>(policy));
173 }
174 
175 template class MaskFormatter<MaskPixel>;
176 // The followings fails
177 // because the function template `delegateSerialize' is obsolete(?)
178 // template void MaskFormatter<MaskPixel>::delegateSerialize(
179 // boost::archive::binary_oarchive&, int const, Persistable*);
180 // template void MaskFormatter<MaskPixel>::delegateSerialize(
181 // boost::archive::binary_iarchive&, int const, Persistable*);
182 }
183 }
184 } // namespace lsst::afw::formatters
std::uint16_t MaskPixel
default type for Masks and MaskedImage Masks
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
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 std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
#define __attribute__(x)
version
Definition: conf.py:12
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
table::Key< std::string > name
Definition: ApCorrMap.cc:71
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
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)
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:44
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)