LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 "boost/serialization/shared_ptr.hpp"
44 #include "boost/serialization/binary_object.hpp"
45 #include <boost/archive/binary_oarchive.hpp>
46 #include <boost/archive/binary_iarchive.hpp>
47 
49 
50 #include "lsst/daf/base.h"
51 #include "lsst/daf/persistence.h"
52 #include "lsst/pex/logging/Trace.h"
53 #include "lsst/afw/image/Mask.h"
54 
56 
57 #define EXEC_TRACE 20
58 static void execTrace(std::string s, int level = EXEC_TRACE) {
59  lsst::pex::logging::Trace("afw.MaskFormatter", level, s);
60 }
61 
68 
69 namespace lsst {
70 namespace afw {
71 namespace formatters {
72 
73 template <typename imagePixelT>
75 public:
76  static std::string name();
77 };
78 
79 template<> std::string MaskFormatterTraits<MaskPixel>::name() {
80  static std::string name = "Mask";
81  return name;
82 }
83 
84 template <typename MaskPixelT>
87  typeid(Mask<MaskPixelT>),
88  createInstance);
89 
90 template <typename MaskPixelT>
93  lsst::daf::persistence::Formatter(typeid(this)) {
94 }
95 
96 template <typename MaskPixelT>
98 }
99 
100 template <typename MaskPixelT>
102  Persistable const* persistable,
103  Storage::Ptr storage,
105  execTrace("MaskFormatter write start");
106  Mask<MaskPixelT> const* ip =
107  dynamic_cast<Mask<MaskPixelT> const*>(persistable);
108  if (ip == 0) {
109  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Mask");
110  }
111  if (typeid(*storage) == typeid(BoostStorage)) {
112  execTrace("MaskFormatter write BoostStorage");
113  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
114  boost->getOArchive() & *ip;
115  execTrace("MaskFormatter write end");
116  return;
117  }
118  else if (typeid(*storage) == typeid(FitsStorage)) {
119  execTrace("MaskFormatter write FitsStorage");
120  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
121  // Need to cast away const because writeFits modifies the metadata.
122  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
123  vip->writeFits(fits->getPath());
124  execTrace("MaskFormatter write end");
125  return;
126  }
127  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
128 }
129 
130 template <typename MaskPixelT>
132  Storage::Ptr storage,
134  execTrace("MaskFormatter read start");
135  if (typeid(*storage) == typeid(BoostStorage)) {
136  execTrace("MaskFormatter read BoostStorage");
137  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
139  boost->getIArchive() & *ip;
140  execTrace("MaskFormatter read end");
141  return ip;
142  }
143  else if (typeid(*storage) == typeid(FitsStorage)) {
144  execTrace("MaskFormatter read FitsStorage");
145  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
146  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
147  execTrace("MaskFormatter read end");
148  return ip;
149  }
150  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Mask");
151 }
152 
153 template <typename MaskPixelT>
155  Persistable*,
156  Storage::Ptr,
158  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
159 }
160 
161 template <typename MaskPixelT> template <class Archive>
163  Archive& ar, int const version, Persistable* persistable) {
164  execTrace("MaskFormatter delegateSerialize start");
165  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
166  if (ip == 0) {
167  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
168  }
169  ar & ip->_offsetRows & ip->_offsetCols;
170  ar & ip->_maskPlaneDict;
171  unsigned int cols;
172  unsigned int rows;
173  unsigned int planes;
174  if (Archive::is_saving::value) {
175  cols = ip->_vwImagePtr->cols();
176  rows = ip->_vwImagePtr->rows();
177  planes = ip->_vwImagePtr->planes();
178  }
179  ar & cols & rows & planes;
180  if (Archive::is_loading::value) {
181  ip->_vwImagePtr->set_size(cols, rows, planes);
182  }
183  unsigned int pixels = cols * rows * planes;
184  MaskPixelT* data = ip->_vwImagePtr->data();
185  ar & boost::serialization::make_array(data, pixels);
186  execTrace("MaskFormatter delegateSerialize end");
187 }
188 
189 template <typename MaskPixelT>
193 }
194 
195 template class MaskFormatter<MaskPixel>;
198 //template void MaskFormatter<MaskPixel>::delegateSerialize(
199 // boost::archive::binary_oarchive&, int const, Persistable*);
200 //template void MaskFormatter<MaskPixel>::delegateSerialize(
201 // boost::archive::binary_iarchive&, int const, Persistable*);
202 
203 
204 }}} // namespace lsst::afw::formatters
static lsst::daf::persistence::FormatterRegistration registration
Definition: MaskFormatter.h:87
boost::uint16_t MaskPixel
#define EXEC_TRACE
table::Key< std::string > name
Definition: ApCorrMap.cc:71
boost::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
MaskFormatter(lsst::pex::policy::Policy::Ptr policy)
daf_persistence package header file
virtual boost::archive::text_iarchive & getIArchive(void)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
definition of the Trace messaging facilities
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
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
#define __attribute__(x)
static lsst::daf::persistence::Formatter::Ptr createInstance(lsst::pex::policy::Policy::Ptr policy)
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:93
Interface for MaskFormatter class.
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
LSST bitmasks.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
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.
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Class for boost::serialization storage.
Definition: BoostStorage.h:58
static boost::shared_ptr< detail::MaskDict > _maskPlaneDict()
Definition: Mask.cc:1246
Base class for all persistable classes.
Definition: Persistable.h:74
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