LSSTApplications  16.0-1-gce273f5+17,16.0-1-gf77f410+12,16.0-10-g5a5abec+8,16.0-10-gc1446dd+12,16.0-12-g1dc09ba+6,16.0-12-g569485f,16.0-12-ga22ed6e+1,16.0-13-g4c33ca5+12,16.0-13-gb122224+3,16.0-13-gd9b1b71+12,16.0-14-g22e2ff2,16.0-14-g71e547a+8,16.0-17-g0bdc215+4,16.0-17-g6a7bfb3b+12,16.0-2-g0febb12+14,16.0-2-g839ba83+50,16.0-2-g9d5294e+39,16.0-20-ga7ad2685,16.0-3-g404ea43+9,16.0-3-gbc759ec+10,16.0-3-gcfd6c53+37,16.0-4-g03cf288+28,16.0-4-g13a27c5+14,16.0-4-g5f3a788+13,16.0-4-g8a0f11a+34,16.0-4-ga3eb747+3,16.0-45-g4805a823c,16.0-5-g1991253+12,16.0-5-g1e9226d+1,16.0-5-g865efd9+12,16.0-5-gb3f8a4b+44,16.0-5-gd0f1235+6,16.0-6-gf0acd13+31,16.0-6-gf9cb114+13,16.0-7-g6043bfc,16.0-7-ga8e1655+8,16.0-8-g23bbf3f+3,16.0-8-g4dec96c+25,16.0-8-gfd407c0+2,master-g965b868a3d+1,master-gdc6be1965f+1,w.2018.39
LSSTDataManagementBasePackage
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/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  lsst::geom::Point2I& xy0, daf::base::PropertySet& metadata,
47  lsst::geom::Box2I const& bbox, 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 {
58  fits_read_array(*_fitsfile, _array, _xy0, _metadata, _bbox, _origin);
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;
69  fits_read_array(*_fitsfile, array, _xy0, _metadata, _bbox, _origin);
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;
82  lsst::geom::Point2I& _xy0;
83  daf::base::PropertySet& _metadata;
84  lsst::geom::Box2I const& _bbox;
85  ImageOrigin _origin;
86 };
87 
88 } // namespace
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  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin const origin = PARENT) {
93  ndarray::Array<typename ImageT::Pixel, 2, 2> array;
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,
111  lsst::geom::Box2I const& bbox = lsst::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 } // namespace image
117 } // namespace afw
118 } // namespace lsst
119 #endif // !LSST_AFW_IMAGE_fits_io_mpl_h_INCLUDED
void fits_read_image(fits::Fits &fitsfile, ImageT &img, lsst::daf::base::PropertySet &metadata, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin const origin=PARENT)
Definition: fits_io_mpl.h:91
float Pixel
Typedefs to be used for pixel values.
Definition: common.h:37
table::Box2IKey bbox
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:35
A base class for image defects.
Definition: cameraGeom.dox:3
STL class.
void fits_read_array(fits::Fits &fitsfile, ndarray::Array< PixelT, 2, 2 > &array, lsst::geom::Point2I &xy0, lsst::daf::base::PropertySet &metadata, lsst::geom::Box2I bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Definition: fits_io.h:41
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
Definition: fits.cc:1568
Class for storing generic metadata.
Definition: PropertySet.h:73
#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:104
An integer coordinate rectangle.
Definition: Box.h:54