LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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
std::uint16_t MaskPixel
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
table::Key< std::string > name
Definition: ApCorrMap.cc:71
#define LOGL_DEBUG(logger, message...)
Definition: Log.h:513
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.
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
daf_persistence package header file
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
virtual boost::archive::text_iarchive & getIArchive(void)
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
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 LOG_GET(logger)
Definition: Log.h:83
#define __attribute__(x)
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
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)
LSST bitmasks.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
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
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:54
Definition of default types for Masks and Variance Images.
#define LOG_LOGGER
Definition: Log.h:712
Abstract base class for storage implementations.
Definition: Storage.h:60
static boost::shared_ptr< detail::MaskDict > _maskPlaneDict()
Definition: Mask.cc:1242