LSSTApplications  16.0-1-gce273f5+17,16.0-1-gf77f410+12,16.0-10-g5a5abec+8,16.0-10-gc1446dd+12,16.0-12-g1dc09ba+6,16.0-12-g569485f,16.0-12-ga22ed6e+1,16.0-13-g4c33ca5+12,16.0-13-gb122224+3,16.0-13-gd9b1b71+12,16.0-14-g22e2ff2,16.0-14-g71e547a+8,16.0-17-g0bdc215+4,16.0-17-g6a7bfb3b+12,16.0-2-g0febb12+14,16.0-2-g839ba83+50,16.0-2-g9d5294e+39,16.0-20-ga7ad2685,16.0-3-g404ea43+9,16.0-3-gbc759ec+10,16.0-3-gcfd6c53+37,16.0-4-g03cf288+28,16.0-4-g13a27c5+14,16.0-4-g5f3a788+13,16.0-4-g8a0f11a+34,16.0-4-ga3eb747+3,16.0-45-g4805a823c,16.0-5-g1991253+12,16.0-5-g1e9226d+1,16.0-5-g865efd9+12,16.0-5-gb3f8a4b+44,16.0-5-gd0f1235+6,16.0-6-gf0acd13+31,16.0-6-gf9cb114+13,16.0-7-g6043bfc,16.0-7-ga8e1655+8,16.0-8-g23bbf3f+3,16.0-8-g4dec96c+25,16.0-8-gfd407c0+2,master-g965b868a3d+1,master-gdc6be1965f+1,w.2018.39
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/geom.h"
32 #include "lsst/afw/fits.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,
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 = lsst::geom::Point2I();
71 
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 } // namespace image
100 } // namespace afw
101 } // namespace lsst
102 
103 #endif // !LSST_AFW_IMAGE_fits_io_h_INCLUDED
int getImageDim()
Return the number of dimensions in the current HDU.
Definition: fits.cc:1296
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
virtual void combine(ConstPtr source)
Append all value vectors from the source to their corresponding properties.
table::Box2IKey bbox
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
Definition: fits.cc:1309
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
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
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:535
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:35
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:511
A base class for image defects.
Definition: cameraGeom.dox:3
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
Point< int, 2 > Point2I
Definition: Point.h:321
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:478
void fits_read_array(fits::Fits &fitsfile, ndarray::Array< PixelT, 2, 2 > &array, lsst::geom::Point2I &xy0, lsst::daf::base::PropertySet &metadata, lsst::geom::Box2I bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Definition: fits_io.h:41
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Extent< int, 2 > Extent2I
Definition: Extent.h:393
lsst::geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
Definition: wcsUtils.cc:98
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
Definition: fits.cc:1568
Class for storing generic metadata.
Definition: PropertySet.h:73
An exception thrown when a FITS file has the wrong type.
Definition: fits.h:40
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
Definition: fits.cc:472
#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
An integer coordinate rectangle.
Definition: Box.h:54
std::string const wcsNameForXY0
Definition: ImageBase.h:71