lsst.afw  15.0-29-ga12a2b06e
fits_io.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_h_INCLUDED
25 #define LSST_AFW_IMAGE_fits_io_h_INCLUDED
26 
27 #include <string>
28 
29 #include "boost/format.hpp"
30 #include "lsst/pex/exceptions.h"
31 #include "lsst/afw/fits.h"
32 #include "lsst/afw/geom.h"
33 #include "lsst/afw/geom/wcsUtils.h"
34 #include "ndarray.h"
35 
36 namespace lsst {
37 namespace afw {
38 namespace image {
39 
40 template <typename PixelT>
41 inline void fits_read_array(fits::Fits& fitsfile, ndarray::Array<PixelT, 2, 2>& array, geom::Point2I& xy0,
43  ImageOrigin origin = PARENT) {
44  fitsfile.checkCompressedImagePhu();
45  if (!fitsfile.checkImageType<PixelT>()) {
46  throw LSST_FITS_EXCEPT(fits::FitsTypeError, fitsfile, "Incorrect image type for FITS image");
47  }
48  int nAxis = fitsfile.getImageDim();
49  ndarray::Vector<ndarray::Size, 2> shape;
50  if (nAxis == 2) {
51  shape = fitsfile.getImageShape<2>();
52  } else if (nAxis == 3) {
53  ndarray::Vector<ndarray::Size, 3> shape3 = fitsfile.getImageShape<3>();
54  if (shape3[0] != 1) {
56  boost::str(boost::format("3rd dimension %d is not 1") % shape3[0]));
57  }
58  shape = shape3.last<2>();
59  }
60  /*
61  * TODO: DM-11235 We cannot call fitsfile.readMetadata(metadata, true)
62  * as it doesn't handle INHERIT correctly
63  */
64  const int hdu = fitsfile.getHdu();
66  metadata.combine(header);
67  fitsfile.setHdu(hdu);
68 
69  // Origin of part of image to read
70  xy0 = geom::Point2I();
71 
73  geom::Extent2I dimensions = geom::Extent2I(shape[1], shape[0]);
74 
75  if (!bbox.isEmpty()) {
76  if (origin == PARENT) {
77  bbox.shift(-xyOffset);
78  }
79  xy0 = bbox.getMin();
80 
81  if (bbox.getMinX() < 0 || bbox.getMinY() < 0 || bbox.getWidth() > dimensions.getX() ||
82  bbox.getHeight() > dimensions.getY()) {
84  (boost::format("BBox (%d,%d) %dx%d doesn't fit in image %dx%d") %
85  bbox.getMinX() % bbox.getMinY() % bbox.getWidth() % bbox.getHeight() %
86  dimensions.getX() % dimensions.getY())
87  .str());
88  }
89  dimensions = bbox.getDimensions();
90  }
91 
92  array = ndarray::allocate(dimensions.getY(), dimensions.getX());
93 
94  fitsfile.readImage(array, ndarray::makeVector(xy0.getY(), xy0.getX()));
95 
96  xy0 += xyOffset;
97 }
98 
99 }
100 }
101 } // namespace lsst::afw::image
102 
103 #endif // !LSST_AFW_IMAGE_fits_io_h_INCLUDED
int getImageDim()
Return the number of dimensions in the current HDU.
Definition: fits.cc:1300
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
Definition: fits.cc:1313
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=fits::DEFAULT_HDU, bool strip=false)
Return the metadata (header entries) from a FITS file.
Definition: Utils.h:64
geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
Definition: wcsUtils.cc:98
Extent< int, 2 > Extent2I
Definition: Extent.h:380
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
An integer coordinate rectangle.
Definition: Box.h:55
void readImage(ndarray::Array< T, N, N > const &array, ndarray::Vector< int, N > const &offset)
Read an array from a FITS image.
Definition: fits.h:539
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:35
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:515
A base class for image defects.
virtual void combine(ConstPtr source)
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
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:477
#define LSST_EXCEPT(type,...)
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
Definition: fits.cc:1581
An exception thrown when a FITS file has the wrong type.
Definition: fits.h:40
table::Box2IKey bbox
Point< int, 2 > Point2I
Definition: Point.h:301
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
Definition: fits.cc:471
#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
std::string const wcsNameForXY0
Definition: ImageBase.h:74