LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
43 #include <string>
44 
45 #include "boost/serialization/shared_ptr.hpp"
46 #include "boost/serialization/binary_object.hpp"
47 #include <boost/archive/binary_oarchive.hpp>
48 #include <boost/archive/binary_iarchive.hpp>
49 
51 
52 #include "lsst/daf/base.h"
53 #include "lsst/daf/persistence.h"
54 #include "lsst/log/Log.h"
55 #include "lsst/afw/image/Mask.h"
56 
58 
59 namespace {
60 LOG_LOGGER _log = LOG_GET("afw.MaskFormatter");
61 }
62 
69 
70 namespace lsst {
71 namespace afw {
72 namespace formatters {
73 
74 template <typename imagePixelT>
76 public:
77  static std::string name();
78 };
79 
80 template<> std::string MaskFormatterTraits<MaskPixel>::name() {
81  static std::string name = "Mask";
82  return name;
83 }
84 
85 template <typename MaskPixelT>
88  typeid(Mask<MaskPixelT>),
89  createInstance);
90 
91 template <typename MaskPixelT>
94  lsst::daf::persistence::Formatter(typeid(this)) {
95 }
96 
97 template <typename MaskPixelT>
99 }
100 
101 template <typename MaskPixelT>
103  Persistable const* persistable,
104  Storage::Ptr storage,
106  LOGL_DEBUG(_log, "MaskFormatter write start");
107  Mask<MaskPixelT> const* ip =
108  dynamic_cast<Mask<MaskPixelT> const*>(persistable);
109  if (ip == 0) {
110  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Mask");
111  }
112  if (typeid(*storage) == typeid(BoostStorage)) {
113  LOGL_DEBUG(_log, "MaskFormatter write BoostStorage");
114  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
115  boost->getOArchive() & *ip;
116  LOGL_DEBUG(_log, "MaskFormatter write end");
117  return;
118  }
119  else if (typeid(*storage) == typeid(FitsStorage)) {
120  LOGL_DEBUG(_log, "MaskFormatter write FitsStorage");
121  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
122  // Need to cast away const because writeFits modifies the metadata.
123  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
124  vip->writeFits(fits->getPath());
125  LOGL_DEBUG(_log, "MaskFormatter write end");
126  return;
127  }
128  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
129 }
130 
131 template <typename MaskPixelT>
133  Storage::Ptr storage,
135  LOGL_DEBUG(_log, "MaskFormatter read start");
136  if (typeid(*storage) == typeid(BoostStorage)) {
137  LOGL_DEBUG(_log, "MaskFormatter read BoostStorage");
138  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
140  boost->getIArchive() & *ip;
141  LOGL_DEBUG(_log, "MaskFormatter read end");
142  return ip;
143  }
144  else if (typeid(*storage) == typeid(FitsStorage)) {
145  LOGL_DEBUG(_log, "MaskFormatter read FitsStorage");
146  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
147  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
148  LOGL_DEBUG(_log, "MaskFormatter read end");
149  return ip;
150  }
151  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
152 }
153 
154 template <typename MaskPixelT>
156  Persistable*,
157  Storage::Ptr,
159  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
160 }
161 
162 template <typename MaskPixelT> template <class Archive>
164  Archive& ar, int const version, Persistable* persistable) {
165  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize start");
166  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
167  if (ip == 0) {
168  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
169  }
170  ar & ip->_offsetRows & ip->_offsetCols;
171  ar & ip->_maskPlaneDict;
172  unsigned int cols;
173  unsigned int rows;
174  unsigned int planes;
175  if (Archive::is_saving::value) {
176  cols = ip->_vwImagePtr->cols();
177  rows = ip->_vwImagePtr->rows();
178  planes = ip->_vwImagePtr->planes();
179  }
180  ar & cols & rows & planes;
181  if (Archive::is_loading::value) {
182  ip->_vwImagePtr->set_size(cols, rows, planes);
183  }
184  unsigned int pixels = cols * rows * planes;
185  MaskPixelT* data = ip->_vwImagePtr->data();
186  ar & boost::serialization::make_array(data, pixels);
187  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize end");
188 }
189 
190 template <typename MaskPixelT>
194 }
195 
196 template class MaskFormatter<MaskPixel>;
199 //template void MaskFormatter<MaskPixel>::delegateSerialize(
200 // boost::archive::binary_oarchive&, int const, Persistable*);
201 //template void MaskFormatter<MaskPixel>::delegateSerialize(
202 // boost::archive::binary_iarchive&, int const, Persistable*);
203 
204 
205 }}} // namespace lsst::afw::formatters
virtual int getHdu(void)
Return the HDU to read from the FITS file.
Definition: FitsStorage.cc:116
std::uint16_t MaskPixel
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Read a Persistable instance from a Storage instance.
#define LOG_LOGGER
Definition: Log.h:712
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static lsst::daf::persistence::FormatterRegistration registration
Definition: MaskFormatter.h:87
void writeFits(std::string const &fileName, boost::shared_ptr< lsst::daf::base::PropertySet const > metadata=boost::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
daf_persistence package header file
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
Class for FITS file storage.
Definition: FitsStorage.h:52
virtual boost::archive::text_oarchive & getOArchive(void)
Get a boost::serialization archive suitable for output.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
LSST DM logging module built on log4cxx.
#define __attribute__(x)
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Write a Persistable instance to a Storage instance.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:92
MaskFormatter(lsst::pex::policy::Policy::Ptr policy)
Interface for MaskFormatter class.
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Update an existing Persistable instance with information from an additional Storage instance...
LSST bitmasks.
virtual boost::archive::text_iarchive & getIArchive(void)
Get a boost::serialization archive suitable for input.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
virtual std::string const & getPath(void)
Return the pathname for the FITS file.
Definition: FitsStorage.cc:109
static lsst::daf::persistence::Formatter::Ptr createInstance(lsst::pex::policy::Policy::Ptr policy)
Class for boost::serialization storage.
Definition: BoostStorage.h:59
Base class for all persistable classes.
Definition: Persistable.h:74
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:54
Definition of default types for Masks and Variance Images.
Abstract base class for storage implementations.
Definition: Storage.h:60
static boost::shared_ptr< detail::MaskDict > _maskPlaneDict()
Definition: Mask.cc:1242