lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/image/Wcs.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  if (!fitsfile.checkImageType<PixelT>()) {
45  throw LSST_FITS_EXCEPT(fits::FitsTypeError, fitsfile, "Incorrect image type for FITS image");
46  }
47  int nAxis = fitsfile.getImageDim();
48  ndarray::Vector<ndarray::Size, 2> shape;
49  if (nAxis == 2) {
50  shape = fitsfile.getImageShape<2>();
51  } else if (nAxis == 3) {
52  ndarray::Vector<ndarray::Size, 3> shape3 = fitsfile.getImageShape<3>();
53  if (shape3[0] != 1) {
54  throw LSST_EXCEPT(fits::FitsError,
55  boost::str(boost::format("3rd dimension %d is not 1") % shape3[0]));
56  }
57  shape = shape3.last<2>();
58  }
59  /*
60  * TODO: DM-11235 We cannot call fitsfile.readMetadata(metadata, true)
61  * as it doesn't handle INHERIT correctly
62  */
63  const int hdu = fitsfile.getHdu();
64  std::shared_ptr<daf::base::PropertyList> header(readMetadata(fitsfile, true));
65  metadata.combine(header);
66  fitsfile.setHdu(hdu);
67 
68  // Origin of part of image to read
69  xy0 = geom::Point2I();
70 
72  geom::Extent2I dimensions = geom::Extent2I(shape[1], shape[0]);
73 
74  if (!bbox.isEmpty()) {
75  if (origin == PARENT) {
76  bbox.shift(-xyOffset);
77  }
78  xy0 = bbox.getMin();
79 
80  if (bbox.getMinX() < 0 || bbox.getMinY() < 0 || bbox.getWidth() > dimensions.getX() ||
81  bbox.getHeight() > dimensions.getY()) {
82  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
83  (boost::format("BBox (%d,%d) %dx%d doesn't fit in image %dx%d") %
84  bbox.getMinX() % bbox.getMinY() % bbox.getWidth() % bbox.getHeight() %
85  dimensions.getX() % dimensions.getY())
86  .str());
87  }
88  dimensions = bbox.getDimensions();
89  }
90 
91  array = ndarray::allocate(dimensions.getY(), dimensions.getX());
92 
93  fitsfile.readImage(array, ndarray::makeVector(xy0.getY(), xy0.getX()));
94 
95  xy0 += xyOffset;
96 }
97 
98 template <typename ImageT>
99 inline void fits_write_image(fits::Fits& fitsfile, const ImageT& image,
100  std::shared_ptr<daf::base::PropertySet const> metadata =
101  std::shared_ptr<daf::base::PropertySet const>()) {
102  fitsfile.createImage<typename ImageT::Pixel>(image.getArray().getShape());
103  if (metadata) {
104  fitsfile.writeMetadata(*metadata);
105  }
106  fitsfile.writeImage(image.getArray());
107 }
108 }
109 }
110 } // namespace lsst::afw::image
111 
112 #endif // !LSST_AFW_IMAGE_fits_io_h_INCLUDED
int getImageDim()
Return the number of dimensions in the current HDU.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
void fits_write_image(fits::Fits &fitsfile, const ImageT &image, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >())
Definition: fits_io.h:99
bool checkImageType()
Return true if the current HDU has the given pixel type.
Extent< int, 2 > Extent2I
Definition: Extent.h:341
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:199
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:414
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:33
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:390
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.
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
afw::table::Key< afw::table::Array< ImagePixelT > > image
An exception thrown when a FITS file has the wrong type.
Definition: fits.h:38
void createImage(ndarray::Vector< ndarray::Size, N > const &shape)
Create an image with pixel type provided by the given explicit PixelT template parameter and shape de...
Definition: fits.h:347
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Return the metadata (header entries) from a FITS file.
Definition: Utils.h:64
Point< int, 2 > Point2I
Definition: Point.h:270
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:372
#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:84
std::string const wcsNameForXY0
Definition: Image.h:84
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Definition: Wcs.cc:1096