lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fits_io_mpl.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef LSST_AFW_IMAGE_fits_io_mpl_h_INCLUDED
25 #define LSST_AFW_IMAGE_fits_io_mpl_h_INCLUDED
26 
27 #include "boost/mpl/for_each.hpp"
28 #include "boost/mpl/vector.hpp"
29 
30 #include "lsst/afw/geom.h"
31 #include "lsst/afw/fits.h"
33 #include "lsst/afw/image/Image.h"
34 
35 namespace lsst {
36 namespace afw {
37 namespace image {
38 
39 namespace {
40 struct found_type : public std::exception {}; // type to throw when we've read our data
41 
42 template <typename ImageT, typename ExceptionT>
43 class try_fits_read_array {
44 public:
45  try_fits_read_array(fits::Fits& fitsfile, ndarray::Array<typename ImageT::Pixel, 2, 2>& array,
46  geom::Point2I& xy0, daf::base::PropertySet& metadata, geom::Box2I const& bbox,
47  ImageOrigin const origin)
48  : _fitsfile(&fitsfile),
49  _array(array),
50  _xy0(xy0),
51  _metadata(metadata),
52  _bbox(bbox),
53  _origin(origin) {}
54 
55  // read directly into the desired type if the file's the same type
56  void operator()(typename ImageT::Pixel) {
57  try {
59  throw ExceptionT(); // signal that we've succeeded
60  } catch (fits::FitsTypeError const&) {
61  // ah well. We'll try another image type
62  }
63  }
64 
65  template <typename OtherPixel>
66  void operator()(OtherPixel) { // read and convert into the desired type
67  try {
68  ndarray::Array<OtherPixel, 2, 2> array;
70  // copy and convert
71  _array = ndarray::allocate(array.getShape());
72  _array.deep() = array;
73  throw ExceptionT(); // signal that we've succeeded
74  } catch (fits::FitsTypeError const&) {
75  // pass
76  }
77  }
78 
79 private:
80  fits::Fits* _fitsfile;
81  ndarray::Array<typename ImageT::Pixel, 2, 2>& _array;
84  geom::Box2I const& _bbox;
86 };
87 
88 } // anonymous
89 
90 template <typename supported_fits_types, typename ImageT>
91 void fits_read_image(fits::Fits& fitsfile, ImageT& img, lsst::daf::base::PropertySet& metadata,
92  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin const origin = PARENT) {
93  ndarray::Array<typename ImageT::Pixel, 2, 2> array;
94  geom::Point2I xy0;
95  fitsfile.checkCompressedImagePhu();
96  try {
97  try_fits_read_array<ImageT, found_type> reader{fitsfile, array, xy0, metadata, bbox, origin};
98  reader.operator()(typename ImageT::Pixel()); // attempt first the type we were explicitly asked for
99  boost::mpl::for_each<supported_fits_types>(reader);
100  } catch (found_type&) {
101  img = ImageT(array, false, xy0);
102  return;
103  }
104  throw LSST_FITS_EXCEPT(fits::FitsError, fitsfile, "FITS file does not have one of the expected types");
105 }
106 
107 template <typename supported_fits_types, typename ImageT>
108 void fits_read_image(fits::Fits& fitsfile, ImageT& img,
109  std::shared_ptr<lsst::daf::base::PropertySet> metadata =
110  std::shared_ptr<lsst::daf::base::PropertySet>(),
111  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin const origin = PARENT) {
112  lsst::daf::base::PropertySet metadata_s;
113  fits_read_image<supported_fits_types, ImageT>(fitsfile, img, (metadata ? *metadata : metadata_s), bbox,
114  origin);
115 }
116 }
117 }
118 } // lsst::afw::image
119 #endif // !LSST_AFW_IMAGE_fits_io_mpl_h_INCLUDED
fits::Fits * _fitsfile
Definition: fits_io_mpl.h:80
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
An integer coordinate rectangle.
Definition: Box.h:55
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:34
ndarray::Array< typename ImageT::Pixel, 2, 2 > & _array
Definition: fits_io_mpl.h:81
geom::Box2I const & _bbox
Definition: fits_io_mpl.h:84
void fits_read_array(fits::Fits &fitsfile, ndarray::Array< PixelT, 2, 2 > &array, geom::Point2I &xy0, lsst::daf::base::PropertySet &metadata, geom::Box2I bbox=geom::Box2I(), ImageOrigin origin=PARENT)
Definition: fits_io.h:41
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
void fits_read_image(fits::Fits &fitsfile, ImageT &img, lsst::daf::base::PropertySet &metadata, geom::Box2I const &bbox=geom::Box2I(), ImageOrigin const origin=PARENT)
Definition: fits_io_mpl.h:91
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
afw::table::Key< afw::table::Array< ImagePixelT > > image
ImageOrigin _origin
Definition: fits_io_mpl.h:85
Point< int, 2 > Point2I
Definition: Point.h:270
geom::Point2I & _xy0
Definition: fits_io_mpl.h:82
daf::base::PropertySet & _metadata
Definition: fits_io_mpl.h:83
#define LSST_FITS_EXCEPT(type, fitsObj,...)
A FITS-related replacement for LSST_EXCEPT that takes an additional Fits object and uses makeErrorMes...
Definition: fits.h:85