LSSTApplications  15.0+21,16.0+1,16.0+3,16.0+4,16.0+8,16.0-1-g2115a9e+2,16.0-1-g4515a79+6,16.0-1-g5c6f5ee+4,16.0-1-g7bb14cc,16.0-1-g80120d7+4,16.0-1-g98efed3+4,16.0-1-gb7f560d+1,16.0-14-gb4f0cd2fa,16.0-2-g1ad129e+1,16.0-2-g2ed7261+1,16.0-2-g311bfd2,16.0-2-g568a347+3,16.0-2-g852da13+6,16.0-2-gd4c87cb+3,16.0-3-g099ede0,16.0-3-g150e024+3,16.0-3-g1f513a6,16.0-3-g958ce35,16.0-4-g08dccf71+4,16.0-4-g128aaef,16.0-4-g84f75fb+5,16.0-4-gcfd1396+4,16.0-4-gde8cee2,16.0-4-gdfb0d14+1,16.0-5-g7bc0afb+3,16.0-5-g86fb31a+3,16.0-6-g2dd73041+4,16.0-7-g95fb7bf,16.0-7-gc37dbc2+4,w.2018.28
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
Options for writing an image to FITS.
Definition: fits.h:218
tuple options
Definition: lsstimport.py:53
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
Fits * fits
Definition: FitsWriter.cc:90
STL class.
LSST DM logging module built on log4cxx.
#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.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:47
Class for boost::serialization storage.
Definition: BoostStorage.h:59
Base class for all persistable classes.
Definition: Persistable.h:73
void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
Template function that serializes a Persistable using boost::serialization.
Definition: FormatterImpl.h:58
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