lsst.afw  13.0-94-ga1c444047
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::FormatterStorage;
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<FormatterStorage> 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  // TODO: Replace this with something better in DM-10776
97  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
98  if (boost) {
99  LOGL_DEBUG(_log, "MaskFormatter write BoostStorage");
100  boost->getOArchive() & *ip;
101  LOGL_DEBUG(_log, "MaskFormatter write end");
102  return;
103  }
104  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
105  if (fits) {
106  LOGL_DEBUG(_log, "MaskFormatter write FitsStorage");
107  // Need to cast away const because writeFits modifies the metadata.
108  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
109  vip->writeFits(fits->getPath());
110  LOGL_DEBUG(_log, "MaskFormatter write end");
111  return;
112  }
113  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
114 }
115 
116 template <typename MaskPixelT>
117 Persistable* MaskFormatter<MaskPixelT>::read(std::shared_ptr<FormatterStorage> storage,
118  std::shared_ptr<lsst::daf::base::PropertySet>) {
119  LOGL_DEBUG(_log, "MaskFormatter read start");
120  // TODO: Replace this with something better in DM-10776
121  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
122  if (boost) {
123  LOGL_DEBUG(_log, "MaskFormatter read BoostStorage");
125  boost->getIArchive() & *ip;
126  LOGL_DEBUG(_log, "MaskFormatter read end");
127  return ip;
128  }
129  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
130  if (fits) {
131  LOGL_DEBUG(_log, "MaskFormatter read FitsStorage");
132  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
133  LOGL_DEBUG(_log, "MaskFormatter read end");
134  return ip;
135  }
136  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
137 }
138 
139 template <typename MaskPixelT>
140 void MaskFormatter<MaskPixelT>::update(Persistable*, std::shared_ptr<FormatterStorage>,
141  std::shared_ptr<lsst::daf::base::PropertySet>) {
142  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
143 }
144 
145 template <typename MaskPixelT>
146 template <class Archive>
147 void MaskFormatter<MaskPixelT>::delegateSerialize(Archive& ar, int const version, Persistable* persistable) {
148  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize start");
149  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
150  if (ip == 0) {
151  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
152  }
153  ar & ip->_offsetRows & ip->_offsetCols;
154  ar & ip->_maskPlaneDict;
155  unsigned int cols;
156  unsigned int rows;
157  unsigned int planes;
158  if (Archive::is_saving::value) {
159  cols = ip->_vwImagePtr->cols();
160  rows = ip->_vwImagePtr->rows();
161  planes = ip->_vwImagePtr->planes();
162  }
163  ar& cols& rows& planes;
164  if (Archive::is_loading::value) {
165  ip->_vwImagePtr->set_size(cols, rows, planes);
166  }
167  unsigned int pixels = cols * rows * planes;
168  MaskPixelT* data = ip->_vwImagePtr->data();
169  ar& boost::serialization::make_array(data, pixels);
170  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize end");
171 }
172 
173 template <typename MaskPixelT>
174 std::shared_ptr<lsst::daf::persistence::Formatter> MaskFormatter<MaskPixelT>::createInstance(
175  std::shared_ptr<lsst::pex::policy::Policy> policy) {
176  return std::shared_ptr<lsst::daf::persistence::Formatter>(new MaskFormatter<MaskPixelT>(policy));
177 }
178 
179 template class MaskFormatter<MaskPixel>;
180 // The followings fails
181 // because the function template `delegateSerialize' is obsolete(?)
182 // template void MaskFormatter<MaskPixel>::delegateSerialize(
183 // boost::archive::binary_oarchive&, int const, Persistable*);
184 // template void MaskFormatter<MaskPixel>::delegateSerialize(
185 // boost::archive::binary_iarchive&, int const, Persistable*);
186 }
187 }
188 } // namespace lsst::afw::formatters
Definition: Span.h:37
#define __attribute__(x)
A base class for image defects.
Definition: cameraGeom.dox:3
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
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.
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:44