LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
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 { namespace afw { namespace image {
37 
38 template <typename PixelT>
39 inline void fits_read_array(
40  fits::Fits & fitsfile,
42  geom::Point2I & xy0,
44  geom::Box2I bbox=geom::Box2I(),
45  ImageOrigin origin=PARENT
46 ) {
47  if (!fitsfile.checkImageType<PixelT>()) {
48  throw LSST_FITS_EXCEPT(
49  fits::FitsTypeError,
50  fitsfile,
51  "Incorrect image type for FITS image"
52  );
53  }
54  int nAxis = fitsfile.getImageDim();
56  if (nAxis == 2) {
57  shape = fitsfile.getImageShape<2>();
58  } else if (nAxis == 3) {
59  ndarray::Vector<int,3> shape3 = fitsfile.getImageShape<3>();
60  if (shape3[0] != 1) {
61  throw LSST_EXCEPT(
62  fits::FitsError,
63  boost::str(boost::format("3rd dimension %d is not 1") % shape3[0])
64  );
65  }
66  shape = shape3.last<2>();
67  }
68 
69  fitsfile.readMetadata(metadata, true);
70 
71  // Origin of part of image to read
72  xy0 = geom::Point2I();
73 
75  geom::Extent2I dimensions = geom::Extent2I(shape[1], shape[0]);
76 
77  if (!bbox.isEmpty()) {
78  if (origin == PARENT) {
79  bbox.shift(-xyOffset);
80  }
81  xy0 = bbox.getMin();
82 
83  if (bbox.getMinX() < 0 || bbox.getMinY() < 0 ||
84  bbox.getWidth() > dimensions.getX() || bbox.getHeight() > dimensions.getY()
85  ) {
86  throw LSST_EXCEPT(
87  lsst::pex::exceptions::LengthError,
88  (boost::format("BBox (%d,%d) %dx%d doesn't fit in image %dx%d") %
89  bbox.getMinX() % bbox.getMinY() % bbox.getWidth() % bbox.getHeight() %
90  dimensions.getX() % dimensions.getY()).str()
91  );
92  }
93  dimensions = bbox.getDimensions();
94  }
95 
96  array = ndarray::allocate(dimensions.getY(), dimensions.getX());
97 
98  fitsfile.readImage(array, ndarray::makeVector(xy0.getY(), xy0.getX()));
99 
100  xy0 += xyOffset;
101 }
102 
103 template <typename ImageT>
104 inline void fits_write_image(
105  fits::Fits & fitsfile, const ImageT & image,
107 ) {
108  fitsfile.createImage<typename ImageT::Pixel>(image.getArray().getShape());
109  if (metadata) {
110  fitsfile.writeMetadata(*metadata);
111  }
112  fitsfile.writeImage(image.getArray());
113 }
114 
115 }}} // namespace lsst::afw::image
116 
117 #endif // !LSST_AFW_IMAGE_fits_io_h_INCLUDED
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Definition: Wcs.cc:1210
An include file to include the header files for lsst::afw::geom.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
#define CONST_PTR(...)
Definition: base.h:47
Point< int, 2 > Point2I
Definition: PSF.h:39
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:368
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
afw::table::PointKey< int > dimensions
A fixed-size 1D array class.
Definition: Vector.h:93
Vector< T, M > last() const
Create a new Vector from the last M elements of this.
Definition: Vector.h:166
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
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
int getImageDim()
Return the number of dimensions in the current HDU.
detail::SimpleInitializer< N > allocate(Vector< int, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A multidimensional strided array.
Definition: Array.h:47
bool checkImageType()
Return true if the current HDU has the given pixel type..
ndarray::Vector< int, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:386
Class for storing generic metadata.
Definition: PropertySet.h:82
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:410
#define LSST_FITS_EXCEPT(type, fitsObj,...)
Definition: fits.h:91
Extent< int, 2 > Extent2I
Definition: Extent.h:355
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, boost::shared_ptr< daf::base::PropertySet const > metadata=boost::shared_ptr< daf::base::PropertySet const >())
Definition: fits_io.h:104
std::string const wcsNameForXY0
Definition: Image.h:82
void createImage(ndarray::Vector< int, N > const &shape)
Create an image with pixel type provided by the given explicit PixelT template parameter and shape de...
Definition: fits.h:343
Include files required for standard LSST Exception handling.