LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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,
44  ndarray::Array<typename ImageT::Pixel,2,2> & array,
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 {
65  ndarray::Array<OtherPixel,2,2> array;
67  //copy and convert
68  _array = ndarray::allocate(array.getShape());
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;
77  ndarray::Array<typename ImageT::Pixel,2,2> & _array;
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 ) {
93  ndarray::Array<typename ImageT::Pixel,2,2> array;
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.
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:202
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:59
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
Utilities for working with FITS files.
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
#define PTR(...)
Definition: base.h:41
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,...)
A FITS-related replacement for LSST_EXCEPT that takes an additional Fits object and uses makeErrorMes...
Definition: fits.h:91