lsst.afw  15.0-17-g0b88e3da8
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 #include "lsst/afw/fits.h"
48 
50 
51 namespace {
52 LOG_LOGGER _log = LOG_GET("afw.MaskFormatter");
53 }
54 
61 
62 namespace lsst {
63 namespace afw {
64 namespace formatters {
65 
66 template <typename imagePixelT>
68 public:
69  static std::string name();
70 };
71 
72 template <>
74  static std::string name = "Mask";
75  return name;
76 }
77 
78 template <typename MaskPixelT>
80  MaskFormatterTraits<MaskPixelT>::name(), typeid(Mask<MaskPixelT>), createInstance);
81 
82 template <typename MaskPixelT>
84  : lsst::daf::persistence::Formatter(typeid(this)) {}
85 
86 template <typename MaskPixelT>
88 
89 template <typename MaskPixelT>
92  LOGL_DEBUG(_log, "MaskFormatter write start");
93  Mask<MaskPixelT> const* ip = dynamic_cast<Mask<MaskPixelT> const*>(persistable);
94  if (ip == 0) {
95  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Mask");
96  }
97  // TODO: Replace this with something better in DM-10776
99  if (boost) {
100  LOGL_DEBUG(_log, "MaskFormatter write BoostStorage");
101  boost->getOArchive() & *ip;
102  LOGL_DEBUG(_log, "MaskFormatter write end");
103  return;
104  }
105  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
106  if (fits) {
107  LOGL_DEBUG(_log, "MaskFormatter write FitsStorage");
108  // Need to cast away const because writeFits modifies the metadata.
109 
111  if (additionalData) {
112  try {
113  options = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("mask"));
114  } catch (std::exception const& exc) {
115  LOGLS_WARN(_log, "Unable to construct mask write options (" << exc.what() <<
116  "); writing with default options");
117  }
118  }
119 
120  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
121  vip->writeFits(fits->getPath(), options);
122  LOGL_DEBUG(_log, "MaskFormatter write end");
123  return;
124  }
125  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
126 }
127 
128 template <typename MaskPixelT>
131  LOGL_DEBUG(_log, "MaskFormatter read start");
132  // TODO: Replace this with something better in DM-10776
134  if (boost) {
135  LOGL_DEBUG(_log, "MaskFormatter read BoostStorage");
137  boost->getIArchive() & *ip;
138  LOGL_DEBUG(_log, "MaskFormatter read end");
139  return ip;
140  }
141  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
142  if (fits) {
143  LOGL_DEBUG(_log, "MaskFormatter read FitsStorage");
144  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
145  LOGL_DEBUG(_log, "MaskFormatter read end");
146  return ip;
147  }
148  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
149 }
150 
151 template <typename MaskPixelT>
154  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
155 }
156 
157 template <typename MaskPixelT>
158 template <class Archive>
160  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize start");
161  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
162  if (ip == 0) {
163  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
164  }
165  ar & ip->_offsetRows & ip->_offsetCols;
166  ar & ip->_maskPlaneDict;
167  unsigned int cols;
168  unsigned int rows;
169  unsigned int planes;
170  if (Archive::is_saving::value) {
171  cols = ip->_vwImagePtr->cols();
172  rows = ip->_vwImagePtr->rows();
173  planes = ip->_vwImagePtr->planes();
174  }
175  ar& cols& rows& planes;
176  if (Archive::is_loading::value) {
177  ip->_vwImagePtr->set_size(cols, rows, planes);
178  }
179  unsigned int pixels = cols * rows * planes;
180  MaskPixelT* data = ip->_vwImagePtr->data();
181  ar& boost::serialization::make_array(data, pixels);
182  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize end");
183 }
184 
185 template <typename MaskPixelT>
189 }
190 
191 template class MaskFormatter<MaskPixel>;
192 // The followings fails
193 // because the function template `delegateSerialize' is obsolete(?)
194 // template void MaskFormatter<MaskPixel>::delegateSerialize(
195 // boost::archive::binary_oarchive&, int const, Persistable*);
196 // template void MaskFormatter<MaskPixel>::delegateSerialize(
197 // boost::archive::binary_iarchive&, int const, Persistable*);
198 }
199 }
200 } // namespace lsst::afw::formatters
#define LOGLS_WARN(logger, message)
char * data
Definition: BaseTable.cc:204
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Options for writing an image to FITS.
Definition: fits.h:220
tuple options
#define LOGL_DEBUG(logger, message...)
MaskFormatter(MaskFormatter const &)=default
Fits * fits
Definition: FitsWriter.cc:90
STL class.
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
#define __attribute__(x)
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)
A base class for image defects.
T what(T... args)
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:84
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
T dynamic_pointer_cast(T... args)
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
STL class.
#define LSST_EXCEPT(type,...)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
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.
#define LOG_GET(logger)
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:44