LSSTApplications  16.0+42,16.0-1-gce273f5+8,16.0-10-g230e10e+1,16.0-11-g9fe0e56+17,16.0-11-gce733cf+17,16.0-12-g5ad1ebf+9,16.0-12-gc85596e+2,16.0-13-gde155d7+2,16.0-14-g9428de4d,16.0-14-gc1cf4a94+2,16.0-15-g8e16a51+14,16.0-2-g0febb12+7,16.0-2-g839ba83+32,16.0-2-g9d5294e+22,16.0-2-gab3db49+7,16.0-2-gf41ba6b+6,16.0-2-gf4e7cdd+5,16.0-3-g6923fb6+15,16.0-3-g8e51203+2,16.0-3-g9645794+6,16.0-3-gcfd6c53+20,16.0-35-g34c7dfe62+1,16.0-4-g03cf288+11,16.0-4-g32d12de,16.0-4-g5f3a788+7,16.0-4-g7690030+30,16.0-4-g8a0f11a+16,16.0-4-ga5d8928+16,16.0-5-g0da18be+7,16.0-5-g4940a70,16.0-5-g563880a+2,16.0-5-g7742071+2,16.0-5-gb3f8a4b+26,16.0-6-g3610b4f+5,16.0-6-gf0acd13+14,16.0-8-g4dec96c+7,16.0-8-gc315727+16,16.0-9-g1de645c+7,16.0-9-gcc4efb7+6,w.2018.36
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 /*
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>
93  LOGL_DEBUG(_log, "MaskFormatter write start");
94  Mask<MaskPixelT> const* ip = dynamic_cast<Mask<MaskPixelT> const*>(persistable);
95  if (ip == 0) {
96  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Mask");
97  }
98  // TODO: Replace this with something better in DM-10776
100  if (boost) {
101  LOGL_DEBUG(_log, "MaskFormatter write BoostStorage");
102  boost->getOArchive() & *ip;
103  LOGL_DEBUG(_log, "MaskFormatter write end");
104  return;
105  }
106  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
107  if (fits) {
108  LOGL_DEBUG(_log, "MaskFormatter write FitsStorage");
109  // Need to cast away const because writeFits modifies the metadata.
110 
112  if (additionalData) {
113  try {
114  options = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("mask"));
115  } catch (std::exception const& exc) {
116  LOGLS_WARN(_log, "Unable to construct mask write options ("
117  << exc.what() << "); writing with default options");
118  }
119  }
120 
121  Mask<MaskPixelT>* vip = const_cast<Mask<MaskPixelT>*>(ip);
122  vip->writeFits(fits->getPath(), options);
123  LOGL_DEBUG(_log, "MaskFormatter write end");
124  return;
125  }
126  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
127 }
128 
129 template <typename MaskPixelT>
132  LOGL_DEBUG(_log, "MaskFormatter read start");
133  // TODO: Replace this with something better in DM-10776
135  if (boost) {
136  LOGL_DEBUG(_log, "MaskFormatter read BoostStorage");
138  boost->getIArchive() & *ip;
139  LOGL_DEBUG(_log, "MaskFormatter read end");
140  return ip;
141  }
142  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
143  if (fits) {
144  LOGL_DEBUG(_log, "MaskFormatter read FitsStorage");
145  Mask<MaskPixelT>* ip = new Mask<MaskPixelT>(fits->getPath(), fits->getHdu());
146  LOGL_DEBUG(_log, "MaskFormatter read end");
147  return ip;
148  }
149  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Mask");
150 }
151 
152 template <typename MaskPixelT>
155  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Mask");
156 }
157 
158 template <typename MaskPixelT>
159 template <class Archive>
161  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize start");
162  Mask<MaskPixelT>* ip = dynamic_cast<Mask<MaskPixelT>*>(persistable);
163  if (ip == 0) {
164  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Mask");
165  }
166  ar & ip->_offsetRows & ip->_offsetCols;
167  ar & ip->_maskPlaneDict;
168  unsigned int cols;
169  unsigned int rows;
170  unsigned int planes;
171  if (Archive::is_saving::value) {
172  cols = ip->_vwImagePtr->cols();
173  rows = ip->_vwImagePtr->rows();
174  planes = ip->_vwImagePtr->planes();
175  }
176  ar& cols& rows& planes;
177  if (Archive::is_loading::value) {
178  ip->_vwImagePtr->set_size(cols, rows, planes);
179  }
180  unsigned int pixels = cols * rows * planes;
181  MaskPixelT* data = ip->_vwImagePtr->data();
182  ar& boost::serialization::make_array(data, pixels);
183  LOGL_DEBUG(_log, "MaskFormatter delegateSerialize end");
184 }
185 
186 template <typename MaskPixelT>
190 }
191 
192 template class MaskFormatter<MaskPixel>;
193 // The followings fails
194 // because the function template `delegateSerialize' is obsolete(?)
195 // template void MaskFormatter<MaskPixel>::delegateSerialize(
196 // boost::archive::binary_oarchive&, int const, Persistable*);
197 // template void MaskFormatter<MaskPixel>::delegateSerialize(
198 // boost::archive::binary_iarchive&, int const, Persistable*);
199 } // namespace formatters
200 } // namespace afw
201 } // namespace lsst
#define LOGLS_WARN(logger, message)
Log a warn-level message using an iostream-based interface.
Definition: Log.h:657
Abstract base class for FormatterStorage implementations.
char * data
Definition: BaseTable.cc:205
#define LOG_LOGGER
Definition: Log.h:712
Definition: Span.h:36
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Options for writing an image to FITS.
Definition: fits.h:218
tuple options
Definition: lsstimport.py:47
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
daf_persistence package header file
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
MaskFormatter(MaskFormatter const &)=default
Fits * fits
Definition: FitsWriter.cc:90
STL class.
LSST DM logging module built on log4cxx.
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData) override
#define __attribute__(x)
A base class for image defects.
Definition: cameraGeom.dox:3
T what(T... args)
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:84
T dynamic_pointer_cast(T... args)
Abstract base class for all formatters.
Definition: Formatter.h:79
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
STL class.
void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData) override
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:47
void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData) override
Class for boost::serialization storage.
Definition: BoostStorage.h:59
Base class for all persistable classes.
Definition: Persistable.h:73
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)
Returns a Log object associated with logger.
Definition: Log.h:83
Class implementing persistence and retrieval for Masks.
Definition: MaskFormatter.h:44
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104