LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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/afw/geom.h"
31 #include "lsst/afw/fits.h"
33 #include "lsst/afw/image/Image.h"
34 
35 namespace lsst { namespace afw { namespace image {
36 
37 namespace {
38 struct found_type : public std::exception { }; // type to throw when we've read our data
39 
40 template<typename ImageT, typename ExceptionT>
41 class try_fits_read_array {
42 public:
43  try_fits_read_array(fits::Fits & fitsfile,
45  geom::Point2I & xy0,
46  daf::base::PropertySet & metadata,
47  geom::Box2I const& bbox,
48  ImageOrigin const origin
49  ) : _fitsfile(&fitsfile), _array(array), _xy0(xy0),
50  _metadata(metadata), _bbox(bbox), _origin(origin) { }
51 
52  // read directly into the desired type if the file's the same type
53  void operator()(typename ImageT::Pixel) {
54  try {
56  throw ExceptionT(); // signal that we've succeeded
57  } catch(fits::FitsTypeError const&) {
58  // ah well. We'll try another image type
59  }
60  }
61 
62  template <typename OtherPixel>
63  void operator()(OtherPixel) { // read and convert into the desired type
64  try {
67  //copy and convert
69  _array.deep() = array;
70  throw ExceptionT(); // signal that we've succeeded
71  } catch(fits::FitsTypeError const&) {
72  // pass
73  }
74  }
75 private:
76  fits::Fits * _fitsfile;
82 };
83 
84 } // anonymous
85 
86 template<typename supported_fits_types, typename ImageT>
88  fits::Fits & fitsfile, ImageT& img,
90  geom::Box2I const& bbox=geom::Box2I(),
91  ImageOrigin const origin=PARENT
92 ) {
94  geom::Point2I xy0;
95  try {
96  boost::mpl::for_each<supported_fits_types>(
97  try_fits_read_array<ImageT, found_type>(
98  fitsfile, array, xy0, metadata, bbox, origin
99  )
100  );
101  } catch (found_type &) {
102  img = ImageT(array, false, xy0);
103  return;
104  }
105  throw LSST_FITS_EXCEPT(
106  fits::FitsError,
107  fitsfile,
108  "FITS file does not have one of the expected types"
109  );
110 }
111 
112 template<typename supported_fits_types, typename ImageT>
114  fits::Fits & fitsfile, ImageT& img,
116  geom::Box2I const& bbox=geom::Box2I(),
117  ImageOrigin const origin=PARENT
118 ) {
119  lsst::daf::base::PropertySet metadata_s;
120  fits_read_image<supported_fits_types, ImageT>(fitsfile, img, (metadata ? *metadata : metadata_s),
121  bbox, origin);
122 }
123 
124 }}} // lsst::afw::image
125 #endif // !LSST_AFW_IMAGE_fits_io_mpl_h_INCLUDED
An include file to include the header files for lsst::afw::geom.
#define PTR(...)
Definition: base.h:41
fits::Fits * _fitsfile
Definition: fits_io_mpl.h:76
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:58
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition: ArrayBase.h:136
geom::Box2I const & _bbox
Definition: fits_io_mpl.h:80
Support for 2-D images.
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:39
detail::SimpleInitializer< N > allocate(Vector< int, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
A multidimensional strided array.
Definition: Array.h:47
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:87
Class for storing generic metadata.
Definition: PropertySet.h:82
ndarray::Array< typename ImageT::Pixel, 2, 2 > & _array
Definition: fits_io_mpl.h:77
ImageOrigin _origin
Definition: fits_io_mpl.h:81
geom::Point2I & _xy0
Definition: fits_io_mpl.h:78
daf::base::PropertySet & _metadata
Definition: fits_io_mpl.h:79
#define LSST_FITS_EXCEPT(type, fitsObj,...)
Definition: fits.h:91