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
MaskedImageFormatter.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 MaskedImageFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <cstdint>
35 #include <iostream>
36 #include <string>
37 
38 #include "boost/serialization/shared_ptr.hpp"
39 #include <boost/archive/binary_iarchive.hpp>
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/text_iarchive.hpp>
42 #include <boost/archive/text_oarchive.hpp>
43 
44 #include "lsst/daf/base.h"
45 #include "lsst/daf/persistence.h"
46 #include "lsst/log/Log.h"
51 #include "lsst/afw/fits.h"
52 
53 namespace {
54 LOG_LOGGER _log = LOG_GET("afw.MaskedImageFormatter");
55 }
56 
64 
65 namespace lsst {
66 namespace afw {
67 namespace formatters {
68 
69 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
71 public:
72  static std::string name();
73 };
74 
75 template <>
77  static std::string name = "MaskedImageU";
78  return name;
79 }
80 template <>
82  static std::string name = "MaskedImageI";
83  return name;
84 }
85 template <>
87  static std::string name = "MaskedImageF";
88  return name;
89 }
90 template <>
92  static std::string name = "MaskedImageD";
93  return name;
94 }
95 template <>
97  static std::string name = "MaskedImageL";
98  return name;
99 }
100 
101 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
106 
107 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
110  : lsst::daf::persistence::Formatter(typeid(this)) {}
111 
112 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
114 
115 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
119  LOGL_DEBUG(_log, "MaskedImageFormatter write start");
121  dynamic_cast<MaskedImage<ImagePixelT, MaskPixelT> const*>(persistable);
122  if (ip == 0) {
123  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-MaskedImage");
124  }
125  // TODO: Replace this with something better in DM-10776
127  if (boost) {
128  LOGL_DEBUG(_log, "MaskedImageFormatter write BoostStorage");
129  boost->getOArchive() & *ip;
130  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
131  return;
132  }
133  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
134  if (fits) {
135  LOGL_DEBUG(_log, "MaskedImageFormatter write FitsStorage");
136 
137  fits::ImageWriteOptions imageOptions, maskOptions, varianceOptions;
138  if (additionalData) {
139  try {
140  imageOptions = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("image"));
141  maskOptions = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("mask"));
142  varianceOptions = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("variance"));
143  } catch (std::exception const& exc) {
144  LOGLS_WARN(_log, "Unable to construct MaskedImage write options ("
145  << exc.what() << "); writing with default options");
146  }
147  }
148  ip->writeFits(fits->getPath(), imageOptions, maskOptions, varianceOptions);
149  LOGL_DEBUG(_log, "MaskedImageFormatter write end");
150  return;
151  }
152  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for MaskedImage");
153 }
154 
155 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
158  LOGL_DEBUG(_log, "MaskedImageFormatter read start");
160  // TODO: Replace this with something better in DM-10776
161  if (boost) {
162  LOGL_DEBUG(_log, "MaskedImageFormatter read BoostStorage");
164  boost->getIArchive() & *ip;
165  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
166  return ip;
167  }
168  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
169  if (fits) {
170  LOGL_DEBUG(_log, "MaskedImageFormatter read FitsStorage");
172  LOGL_DEBUG(_log, "MaskedImageFormatter read end");
173  return ip;
174  }
175  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for MaskedImage");
176 }
177 
178 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
181  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for MaskedImage");
182 }
183 
184 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
185 template <class Archive>
187  Archive& ar, unsigned int const, Persistable* persistable) {
188  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize start");
190  dynamic_cast<MaskedImage<ImagePixelT, MaskPixelT, VariancePixelT>*>(persistable);
191  if (ip == 0) {
192  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-MaskedImage");
193  }
194  ar & ip->_image & ip->_variance & ip->_mask;
195  LOGL_DEBUG(_log, "MaskedImageFormatter delegateSerialize end");
196 }
197 
198 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
204 }
205 
207 #define INSTANTIATE(I, M, V) \
208  template class MaskedImageFormatter<I, M, V>; \
209  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
210  boost::archive::text_oarchive&, unsigned int const, Persistable*); \
211  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
212  boost::archive::text_iarchive&, unsigned int const, Persistable*); \
213  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
214  boost::archive::binary_oarchive&, unsigned int const, Persistable*); \
215  template void MaskedImageFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
216  boost::archive::binary_iarchive&, unsigned int const, Persistable*);
217 
224 } // namespace formatters
225 } // namespace afw
226 } // 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.
#define LOG_LOGGER
Definition: Log.h:712
Definition: Span.h:36
#define __attribute__(x)
Options for writing an image to FITS.
Definition: fits.h:218
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.
A base class for image defects.
Definition: cameraGeom.dox:3
T what(T... args)
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
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 writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > imageMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > maskMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > varianceMetadata=std::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
Definition: MaskedImage.cc:486
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:47
Class implementing persistence and retrieval for MaskedImages.
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
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
float VariancePixel
default type for MaskedImage variance images
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:156
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104