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
Wcs.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
26 #ifndef LSST_AFW_IMAGE_WCS_H
27 #define LSST_AFW_IMAGE_WCS_H
28 
29 #include <limits>
30 #include "Eigen/Core"
31 #include "lsst/base.h"
33 #include "lsst/daf/base/Citizen.h"
34 #include "lsst/afw/image/Image.h"
35 #include "lsst/afw/coord/Coord.h"
37 #include "lsst/afw/geom/Point.h"
38 #include "lsst/afw/geom/Extent.h"
41 
42 struct wcsprm; // defined in wcs.h
43 
44 namespace lsst {
45 namespace daf {
46  namespace base {
47  class PropertySet;
48  }
49 }
50 namespace afw {
51  namespace formatters {
52  class WcsFormatter;
53  }
54  namespace table {
55  class BaseRecord;
56  }
57 namespace image {
58 
106 
111 {
112 public:
113  typedef std::shared_ptr<Wcs> Ptr;
114  typedef std::shared_ptr<Wcs const> ConstPtr;
115 
121  friend PTR(Wcs) makeWcs(PTR(lsst::daf::base::PropertySet) const& fitsMetadata,
122  bool stripMetadata);
123 
124  Wcs(lsst::afw::geom::Point2D const & crval, lsst::afw::geom::Point2D const & crpix,
125  Eigen::Matrix2d const & CD,
126  std::string const & ctype1="RA---TAN", std::string const & ctype2="DEC--TAN",
127  double equinox=2000, std::string const & raDecSys="ICRS",
128  std::string const & cunits1="deg", std::string const & cunits2="deg"
129  );
130 
131  virtual ~Wcs();
132  virtual Ptr clone(void) const;
133 
134  bool operator==(Wcs const & other) const;
135  bool operator!=(Wcs const & other) const { return !(*this == other); }
136 
139 
141  lsst::afw::geom::Point2D getPixelOrigin() const;
142 
144  Eigen::Matrix2d getCDMatrix() const;
145 
147  virtual void flipImage(int flipLR, int flipTB, lsst::afw::geom::Extent2I dimensions) const;
148 
150  virtual void rotateImageBy90(int nQuarter, lsst::afw::geom::Extent2I dimensions) const;
151 
153  virtual PTR(lsst::daf::base::PropertyList) getFitsMetadata() const;
154 
163  bool isFlipped() const;
164 
166  double pixArea(lsst::afw::geom::Point2D pix00) const;
167 
169  geom::Angle pixelScale() const;
170 
178  PTR(coord::Coord) pixelToSky(double pix1, double pix2) const;
179 
187  PTR(coord::Coord) pixelToSky(lsst::afw::geom::Point2D const & pixel) const;
188 
189  /*
190  * @brief Convert from pixel position to sky coordinates (e.g. RA/dec)
191  *
192  * @note This routine is designed for the knowledgeable user in need of performance;
193  * it's safer to call the version that returns a PTR(Coord).
194  */
195  void pixelToSky(
196  double pixel1, double pixel2, geom::Angle& sky1, geom::Angle& sky2
197  ) const;
198 
208  geom::Point2D skyToPixel(geom::Angle sky1, geom::Angle sky2) const;
209 
211  geom::Point2D skyToPixel(coord::Coord const & coord) const;
212 
218  geom::Point2D skyToIntermediateWorldCoord(coord::Coord const & coord) const;
219 
220  virtual bool hasDistortion() const { return false;};
221 
223 
224  double getEquinox() const;
225 
233  bool isSameSkySystem(Wcs const &wcs) const;
234 
239 
256  coord::Coord const & coord,
258  ) const;
259 
276  geom::Point2D const & pix,
278  ) const;
279 
296  coord::Coord const & coord,
298  ) const;
299 
316  geom::Point2D const & pix,
318  ) const;
319 
320  // Mutators; the first one is virtual, even though it will never be overridden,
321  // to make sure subclasses use the correct version of both
322 
331  virtual void shiftReferencePixel(double dx, double dy);
332 
333  // Virtual to make sure subclasses use the correct version of both shiftReferencePixel mutators.
334  virtual void shiftReferencePixel(geom::Extent2D const & d) { shiftReferencePixel(d.getX(), d.getY()); }
335 
337  virtual bool isPersistable() const;
338 
339 private:
340  //Allow the formatter to access private goo
342 
343  void initWcsLib(geom::Point2D const & crval, geom::Point2D const & crpix,
344  Eigen::Matrix2d const & CD,
345  std::string const & ctype1, std::string const & ctype2,
346  double equinox, std::string const & raDecSys,
347  std::string const & cunits1, std::string const & cunits2
348  );
349 
350 protected:
351 
352  friend class WcsFactory;
354  bool _mayBePersistable() const;
355  // See afw::table::io::Persistable
356  virtual std::string getPersistenceName() const;
357  virtual std::string getPythonModule() const;
358  virtual void write(OutputArchiveHandle & handle) const;
359 
360  // Protected virtual implementation for operator== (must be true in both directions for equality).
361  virtual bool _isSubset(Wcs const & other) const;
362 
363  // Return true if coordinate system is ICRS or FK5 J2000
364  bool _isIcrs() const {
365  return (getCoordSystem() == afw::coord::ICRS) ||
366  ((getCoordSystem() == afw::coord::FK5) && (getEquinox() == 2000));
367  }
368 
369  // Default constructor, only used by WcsFormatter
370  Wcs();
371 
372  //If you want to create a Wcs from a FITS header, use makeWcs().
373  //This is protected because the derived classes need to be able to see it.
374  Wcs(CONST_PTR(lsst::daf::base::PropertySet) const& fitsMetadata);
375 
376  // Construct from a record; used by WcsFactory for afw::table::io persistence.
377  explicit Wcs(afw::table::BaseRecord const & record);
378 
379  Wcs(Wcs const & rhs);
380  Wcs& operator= (const Wcs &);
381 
382  virtual void pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const;
383  virtual geom::Point2D skyToPixelImpl(geom::Angle sky1, geom::Angle sky2) const;
384 
385  PTR(afw::coord::Coord) makeCorrectCoord(geom::Angle sky0, geom::Angle sky1) const;
386 
391  PTR(afw::coord::Coord) convertCoordToSky(coord::Coord const & coord) const;
392 
393  virtual geom::AffineTransform linearizePixelToSkyInternal(
394  geom::Point2D const & pix,
395  coord::Coord const & coord,
396  geom::AngleUnit skyUnit
397  ) const;
398 
399  virtual geom::AffineTransform linearizeSkyToPixelInternal(
400  geom::Point2D const & pix,
401  coord::Coord const & coord,
402  geom::AngleUnit skyUnit
403  ) const;
404 
405 
406  void initWcsLibFromFits(CONST_PTR(lsst::daf::base::PropertySet) const& fitsMetadata);
407  void _initWcs();
408  void _setWcslibParams();
409 
410  struct wcsprm* _wcsInfo;
412  int _relax;
415  int _nReject;
418 };
419 
420 namespace detail {
422  createTrivialWcsAsPropertySet(std::string const& wcsName, int const x0=0, int const y0=0);
423 
424  geom::Point2I getImageXY0FromMetadata(std::string const& wcsName, lsst::daf::base::PropertySet *metadata);
425 }
426 
427 PTR(Wcs) makeWcs(PTR(lsst::daf::base::PropertySet) const& fitsMetadata, bool stripMetadata=false);
428 
429 /*
430  Note, CD matrix elements must be in degrees/pixel.
431  */
432 PTR(Wcs) makeWcs(coord::Coord const & crval, geom::Point2D const & crpix,
433  double CD11, double CD12, double CD21, double CD22);
434 
435 namespace detail {
436  int stripWcsKeywords(PTR(lsst::daf::base::PropertySet) const& metadata,
437  CONST_PTR(Wcs) const& wcs
438  );
439 }
440 
441 
450 {
451 public:
454 
455  virtual PTR(afw::geom::XYTransform) invert() const;
456 
458  virtual PTR(afw::geom::XYTransform) clone() const;
459  virtual Point2D forwardTransform(Point2D const &pixel) const;
460  virtual Point2D reverseTransform(Point2D const &pixel) const;
461 
462 protected:
463  CONST_PTR(Wcs) _dst;
464  CONST_PTR(Wcs) _src;
465  bool const _isSameSkySystem;
466 };
467 
468 
469 }}} // lsst::afw::image
470 
471 #endif // LSST_AFW_IMAGE_WCS_H
472 
473 // LocalWords: LSST
void _setWcslibParams()
Definition: Wcs.cc:70
double getEquinox() const
Definition: Wcs.cc:841
virtual bool hasDistortion() const
Definition: Wcs.h:220
A coordinate class intended to represent absolute positions.
#define LSST_PERSIST_FORMATTER(formatter...)
Macro used to connect the persistable class with the Formatter and boost::serialization.
Definition: Persistable.h:98
lsst::afw::coord::Coord Coord
Definition: misc.h:35
bool _isIcrs() const
Definition: Wcs.h:364
geom::AffineTransform linearizePixelToSky(coord::Coord const &coord, geom::AngleUnit skyUnit=geom::degrees) const
Return the local linear approximation to Wcs::pixelToSky at a point given in sky coordinates.
Definition: Wcs.cc:929
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:55
An object passed to Persistable::write to allow it to persist itself.
virtual void pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const
Definition: Wcs.cc:859
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
geom::Point2D skyToPixel(geom::Angle sky1, geom::Angle sky2) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:796
std::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
Wcs & operator=(const Wcs &)
virtual bool _isSubset(Wcs const &other) const
Definition: Wcs.cc:534
XYTransformFromWcsPair: An XYTransform obtained by putting two Wcs objects &quot;back to back&quot;...
Definition: Wcs.h:449
boost::shared_ptr< afw::coord::Coord > makeCorrectCoord(geom::Angle sky0, geom::Angle sky1) const
Given a sky position, use the values stored in ctype and radesys to return the correct sub-class of C...
Definition: Wcs.cc:907
void initWcsLibFromFits(boost::shared_ptr< lsst::daf::base::PropertySet const > const &fitsMetadata)
Parse a fits header, extract the relevant metadata and create a Wcs object.
Definition: Wcs.cc:187
boost::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0, int const y0)
Define a trivial WCS that maps the lower left corner (LLC) pixel of an image to a given value...
Definition: Wcs.cc:1190
table::PointKey< double > crval
Definition: Wcs.cc:1030
void initWcsLib(geom::Point2D const &crval, geom::Point2D const &crpix, Eigen::Matrix2d const &CD, std::string const &ctype1, std::string const &ctype2, double equinox, std::string const &raDecSys, std::string const &cunits1, std::string const &cunits2)
Manually initialise a wcs struct using values passed by the constructor.
Definition: Wcs.cc:375
tbl::Key< int > wcs
virtual geom::AffineTransform linearizeSkyToPixelInternal(geom::Point2D const &pix, coord::Coord const &coord, geom::AngleUnit skyUnit) const
Implementation for the overloaded public linearizeSkyToPixel methods, requiring both a pixel coordina...
Definition: Wcs.cc:994
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Wcs.cc:1081
geom::Point2D skyToIntermediateWorldCoord(coord::Coord const &coord) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:800
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
afw::coord::CoordSystem getCoordSystem() const
Definition: Wcs.h:222
virtual Ptr clone(void) const
Definition: Wcs.cc:566
boost::shared_ptr< coord::Coord > pixelToSky(double pix1, double pix2) const
Convert from pixel position to sky coordinates (e.g.
Definition: Wcs.cc:886
table::Key< std::string > ctype2
Definition: Wcs.cc:1034
bool _mayBePersistable() const
Perform basic checks on whether *this might be persistable.
Definition: Wcs.cc:1099
boost::shared_ptr< afw::coord::Coord > convertCoordToSky(coord::Coord const &coord) const
Given a Coord (as a shared pointer), return the sky position in the correct coordinate system for thi...
Definition: Wcs.cc:792
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:70
int const x0
Definition: saturated.cc:45
virtual void shiftReferencePixel(double dx, double dy)
Move the pixel reference position by (dx, dy)
Definition: Wcs.cc:1167
virtual geom::Point2D skyToPixelImpl(geom::Angle sky1, geom::Angle sky2) const
Definition: Wcs.cc:746
std::shared_ptr< Wcs > Ptr
Definition: Wcs.h:113
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:72
A 2D linear coordinate transformation.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Wcs.cc:1079
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:42
AngleUnit const degrees
Definition: Angle.h:91
friend boost::shared_ptr< Wcs > makeWcs(boost::shared_ptr< lsst::daf::base::PropertySet > const &fitsMetadata, bool stripMetadata)
Create a Wcs of the correct class using a FITS header.
A class representing an Angle.
Definition: Angle.h:103
afwGeom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Return a Point2I(x0, y0) given a PropertySet containing a suitable WCS (e.g.
Definition: Wcs.cc:1213
Interface for Persistable base class.
An affine coordinate transformation consisting of a linear transformation and an offset.
virtual void rotateImageBy90(int nQuarter, lsst::afw::geom::Extent2I dimensions) const
Rotate image by nQuarter times 90 degrees.
Definition: Wcs.cc:628
table::Key< double > equinox
Definition: Wcs.cc:1035
bool isSameSkySystem(Wcs const &wcs) const
Return true if a WCS has the same coordinate system and equinox as this one.
Definition: Wcs.cc:848
Eigen::Matrix2d getCDMatrix() const
Returns the CD matrix.
Definition: Wcs.cc:587
geom::Angle pixelScale() const
Returns the pixel scale [Angle/pixel].
Definition: Wcs.cc:739
Wcs()
Construct an invalid Wcs given no arguments.
Definition: Wcs.cc:89
lsst::afw::geom::Point2D getPixelOrigin() const
Returns CRPIX (corrected to LSST convention).
Definition: Wcs.cc:579
virtual void flipImage(int flipLR, int flipTB, lsst::afw::geom::Extent2I dimensions) const
Flip CD matrix around the y-axis.
Definition: Wcs.cc:605
Support for 2-D images.
boost::shared_ptr< lsst::afw::coord::Coord > getSkyOrigin() const
Returns CRVAL. This need not be the centre of the image.
Definition: Wcs.cc:574
virtual geom::AffineTransform linearizePixelToSkyInternal(geom::Point2D const &pix, coord::Coord const &coord, geom::AngleUnit skyUnit) const
Definition: Wcs.cc:946
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Wcs.cc:1077
bool _skyAxesSwapped
if true then the sky axes are swapped
Definition: Wcs.h:417
bool isFlipped() const
Does the Wcs follow the convention of North=Up, East=Left?
Definition: Wcs.cc:686
Base class for all records.
Definition: BaseRecord.h:27
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
Definition: Wcs.h:414
geom::LinearTransform getLinearTransform() const
Return the linear part of the Wcs, the CD matrix in FITS-speak, as an AffineTransform.
Definition: Wcs.cc:1003
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
Definition: Wcs.h:412
lsst::afw::geom::Angle Angle
Definition: misc.h:38
#define PTR(...)
Definition: base.h:41
Class for storing generic metadata.
Definition: PropertySet.h:82
Virtual base class for 2D transforms.
Definition: XYTransform.h:48
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:413
virtual bool isPersistable() const
Whether the Wcs is persistable using afw::table::io archives.
Definition: Wcs.cc:1107
double pixArea(lsst::afw::geom::Point2D pix00) const
Sky area covered by a pixel at position pix00 in units of square degrees.
Definition: Wcs.cc:706
geom::AffineTransform linearizeSkyToPixel(coord::Coord const &coord, geom::AngleUnit skyUnit=geom::degrees) const
Return the local linear approximation to Wcs::skyToPixel at a point given in sky coordinates.
Definition: Wcs.cc:976
lsst::afw::image::XYTransformFromWcsPair XYTransformFromWcsPair
Definition: Wcs.cc:64
Point< double, 2 > Point2D
Definition: Point.h:288
table::PointKey< int > pixel
virtual boost::shared_ptr< lsst::daf::base::PropertyList > getFitsMetadata() const
Return a PropertyList containing FITS header keywords that can be used to save the Wcs...
Definition: Wcs.cc:682
Base class for all persistable classes.
Definition: Persistable.h:74
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
#define CONST_PTR(...)
A shared pointer to a const object.
Definition: base.h:47
int stripWcsKeywords(boost::shared_ptr< lsst::daf::base::PropertySet > const &metadata, boost::shared_ptr< Wcs const > const &wcs)
Strip keywords from the input metadata that are related to the generated Wcs.
Definition: Wcs.cc:1256
int const y0
Definition: saturated.cc:45
Basic LSST definitions.
This is the base class for spherical coordinates.
Definition: Coord.h:69
A coordinate class intended to represent offsets and dimensions.
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:180
Extent< int, 2 > Extent2I
Definition: Extent.h:358
Class representing an invertible 2D transform.
table::Key< std::string > ctype1
Definition: Wcs.cc:1033
struct wcsprm * _wcsInfo
Definition: Wcs.h:410
coord::CoordSystem _coordSystem
Definition: Wcs.h:416
virtual void shiftReferencePixel(geom::Extent2D const &d)
Definition: Wcs.h:334
table::PointKey< double > crpix
Definition: Wcs.cc:1031
Functions to handle coordinates.
std::shared_ptr< Wcs const > ConstPtr
Definition: Wcs.h:114