lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifndef LSST_AFW_IMAGE_WCS_H
26 #define LSST_AFW_IMAGE_WCS_H
27 
28 #include <limits>
29 #include "Eigen/Core"
30 #include "lsst/base.h"
31 #include "lsst/daf/base/Persistable.h"
32 #include "lsst/daf/base/Citizen.h"
33 #include "lsst/afw/image/Image.h"
34 #include "lsst/afw/coord/Coord.h"
36 #include "lsst/afw/geom/Point.h"
37 #include "lsst/afw/geom/Extent.h"
40 
41 struct wcsprm; // defined in wcs.h
42 
43 namespace lsst {
44 namespace daf {
45 namespace base {
46 class PropertySet;
47 }
48 }
49 namespace afw {
50 namespace formatters {
51 class WcsFormatter;
52 }
53 namespace table {
54 class BaseRecord;
55 }
56 namespace image {
57 
103 
104 class Wcs : public lsst::daf::base::Persistable,
105  public lsst::daf::base::Citizen,
108 public:
114  friend std::shared_ptr<Wcs> makeWcs(std::shared_ptr<lsst::daf::base::PropertySet> const& fitsMetadata,
115  bool stripMetadata);
116 
133  Eigen::Matrix2d const& CD, std::string const& ctype1 = "RA---TAN",
134  std::string const& ctype2 = "DEC--TAN", double equinox = 2000, std::string const& raDecSys = "ICRS",
135  std::string const& cunits1 = "deg", std::string const& cunits2 = "deg");
136 
137  virtual ~Wcs();
138  virtual std::shared_ptr<Wcs> clone(void) const;
139 
140  bool operator==(Wcs const& other) const;
141  bool operator!=(Wcs const& other) const { return !(*this == other); }
142 
144  std::shared_ptr<lsst::afw::coord::Coord> getSkyOrigin() const;
145 
148 
150  Eigen::Matrix2d getCDMatrix() const;
151 
153  virtual void flipImage(int flipLR, int flipTB, lsst::afw::geom::Extent2I dimensions) const;
154 
156  virtual void rotateImageBy90(int nQuarter, lsst::afw::geom::Extent2I dimensions) const;
157 
159  virtual std::shared_ptr<lsst::daf::base::PropertyList> getFitsMetadata() const;
160 
169  bool isFlipped() const;
170 
175  double pixArea(lsst::afw::geom::Point2D pix00) const;
176 
178  geom::Angle pixelScale() const;
179 
187  std::shared_ptr<coord::Coord> pixelToSky(double pix1, double pix2) const;
188 
196  std::shared_ptr<coord::Coord> pixelToSky(lsst::afw::geom::Point2D const& pixel) const;
197 
204  void pixelToSky(double pixel1, double pixel2, geom::Angle& sky1, geom::Angle& sky2) const;
205 
216 
218  geom::Point2D skyToPixel(coord::Coord const& coord) const;
219 
226 
227  virtual bool hasDistortion() const { return false; };
228 
230 
231  double getEquinox() const;
232 
240  bool isSameSkySystem(Wcs const& wcs) const;
241 
246 
263  geom::AngleUnit skyUnit = geom::degrees) const;
264 
281  geom::AngleUnit skyUnit = geom::degrees) const;
282 
299  geom::AngleUnit skyUnit = geom::degrees) const;
300 
317  geom::AngleUnit skyUnit = geom::degrees) const;
318 
319  // Mutators; the first one is virtual, even though it will never be overridden,
320  // to make sure subclasses use the correct version of both
321 
330  virtual void shiftReferencePixel(double dx, double dy);
331 
332  // Virtual to make sure subclasses use the correct version of both shiftReferencePixel mutators.
333  virtual void shiftReferencePixel(geom::Extent2D const& d) { shiftReferencePixel(d.getX(), d.getY()); }
334 
336  virtual bool isPersistable() const;
337 
338 private:
339  // Allow the formatter to access private goo
340  LSST_PERSIST_FORMATTER(lsst::afw::formatters::WcsFormatter)
341 
342 
354  void initWcsLib(geom::Point2D const& crval, geom::Point2D const& crpix, Eigen::Matrix2d const& CD,
355  std::string const& ctype1, std::string const& ctype2, double equinox,
356  std::string const& raDecSys, std::string const& cunits1, std::string const& cunits2);
357 
358 protected:
359  friend class WcsFactory;
361  bool _mayBePersistable() const;
362  // See afw::table::io::Persistable
363  virtual std::string getPersistenceName() const;
364  virtual std::string getPythonModule() const;
365  virtual void write(OutputArchiveHandle& handle) const;
366 
367  // Protected virtual implementation for operator== (must be true in both directions for equality).
368  virtual bool _isSubset(Wcs const& other) const;
369 
370  // Return true if coordinate system is ICRS or FK5 J2000
371  bool _isIcrs() const {
372  return (getCoordSystem() == afw::coord::ICRS) ||
373  ((getCoordSystem() == afw::coord::FK5) && (getEquinox() == 2000));
374  }
375 
376  // Default constructor, only used by WcsFormatter
378  Wcs();
379 
380  // If you want to create a Wcs from a FITS header, use makeWcs().
381  // This is protected because the derived classes need to be able to see it.
387  Wcs(std::shared_ptr<daf::base::PropertySet const> const& fitsMetadata);
388 
389  // Construct from a record; used by WcsFactory for afw::table::io persistence.
390  explicit Wcs(afw::table::BaseRecord const& record);
391 
393  Wcs(Wcs const& rhs);
394  Wcs& operator=(const Wcs&);
395 
399  virtual void pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const;
406  virtual geom::Point2D skyToPixelImpl(geom::Angle sky1, geom::Angle sky2) const;
407 
412  std::shared_ptr<afw::coord::Coord> makeCorrectCoord(geom::Angle sky0, geom::Angle sky1) const;
413 
418  std::shared_ptr<afw::coord::Coord> convertCoordToSky(coord::Coord const& coord) const;
419 
425  coord::Coord const& coord,
426  geom::AngleUnit skyUnit) const;
427 
433  coord::Coord const& coord,
434  geom::AngleUnit skyUnit) const;
435 
437  void initWcsLibFromFits(std::shared_ptr<lsst::daf::base::PropertySet const> const& fitsMetadata);
441  void _initWcs();
442  void _setWcslibParams();
443 
444  struct wcsprm* _wcsInfo;
446  int _relax;
449  int _nReject;
452 };
453 
454 namespace detail {
455 std::shared_ptr<lsst::daf::base::PropertyList> createTrivialWcsAsPropertySet(std::string const& wcsName,
456  int const x0 = 0,
457  int const y0 = 0);
458 
459 geom::Point2I getImageXY0FromMetadata(std::string const& wcsName, lsst::daf::base::PropertySet* metadata);
460 }
461 
471 std::shared_ptr<Wcs> makeWcs(std::shared_ptr<lsst::daf::base::PropertySet> const& fitsMetadata,
472  bool stripMetadata = false);
473 
486 std::shared_ptr<Wcs> makeWcs(coord::Coord const& crval, geom::Point2D const& crpix, double CD11, double CD12,
487  double CD21, double CD22);
488 
489 namespace detail {
490 int stripWcsKeywords(
491  std::shared_ptr<lsst::daf::base::PropertySet> const& metadata,
492  std::shared_ptr<Wcs const> const& wcs
493  );
494 }
495 
504 public:
505  XYTransformFromWcsPair(std::shared_ptr<Wcs const> dst, std::shared_ptr<Wcs const> src);
507 
508  virtual std::shared_ptr<afw::geom::XYTransform> invert() const;
509 
511  virtual std::shared_ptr<afw::geom::XYTransform> clone() const;
512  virtual Point2D forwardTransform(Point2D const& pixel) const;
513  virtual Point2D reverseTransform(Point2D const& pixel) const;
514 
515 protected:
516  std::shared_ptr<Wcs const> _dst;
517  std::shared_ptr<Wcs const> _src;
518  bool const _isSameSkySystem;
519 };
520 }
521 }
522 } // lsst::afw::image
523 
524 #endif // LSST_AFW_IMAGE_WCS_H
525 
526 // LocalWords: LSST
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:905
virtual ~Wcs()
Definition: Wcs.cc:520
table::Key< std::string > ctype2
Definition: Wcs.cc:933
void _setWcslibParams()
Definition: Wcs.cc:68
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
virtual bool hasDistortion() const
Definition: Wcs.h:227
bool operator==(Wcs const &other) const
Definition: Wcs.cc:463
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:863
virtual geom::Point2D skyToPixelImpl(geom::Angle sky1, geom::Angle sky2) const
Worker routine for skyToPixel.
Definition: Wcs.cc:694
bool _isIcrs() const
Definition: Wcs.h:371
virtual std::shared_ptr< Wcs > clone(void) const
Definition: Wcs.cc:526
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:45
An object passed to Persistable::write to allow it to persist itself.
virtual geom::AffineTransform linearizePixelToSkyInternal(geom::Point2D const &pix, coord::Coord const &coord, geom::AngleUnit skyUnit) const
Implementation for the overloaded public linearizePixelToSky methods, requiring both a pixel coordina...
Definition: Wcs.cc:870
virtual std::shared_ptr< afw::geom::XYTransform > invert() const
returns a &quot;deep inverse&quot; in this sense that the forward+inverse transforms do not share state default...
Definition: Wcs.cc:1193
std::shared_ptr< coord::Coord > pixelToSky(double pix1, double pix2) const
Convert from pixel position to sky coordinates (e.g.
Definition: Wcs.cc:822
friend std::shared_ptr< Wcs > makeWcs(std::shared_ptr< lsst::daf::base::PropertySet > const &fitsMetadata, bool stripMetadata)
Create a Wcs of the correct class using a FITS header.
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
Wcs & operator=(const Wcs &)
XYTransformFromWcsPair: An XYTransform obtained by putting two Wcs objects &quot;back to back&quot;...
Definition: Wcs.h:503
virtual bool _isSubset(Wcs const &other) const
Definition: Wcs.cc:499
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Wcs.cc:979
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:104
afw::coord::CoordSystem getCoordSystem() const
Definition: Wcs.h:229
geom::Point2D skyToIntermediateWorldCoord(coord::Coord const &coord) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:742
virtual std::shared_ptr< afw::geom::XYTransform > clone() const
The following methods are needed to devirtualize the XYTransform parent class.
Definition: Wcs.cc:1165
void initWcsLibFromFits(std::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:160
std::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:736
bool _mayBePersistable() const
Perform basic checks on whether *this might be persistable.
Definition: Wcs.cc:998
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
virtual void rotateImageBy90(int nQuarter, lsst::afw::geom::Extent2I dimensions) const
Rotate image by nQuarter times 90 degrees.
Definition: Wcs.cc:586
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:661
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
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:977
A class representing an angle.
Definition: Angle.h:102
int stripWcsKeywords(std::shared_ptr< lsst::daf::base::PropertySet > const &metadata, std::shared_ptr< Wcs const > const &wcs)
Point< double, 2 > Point2D
Definition: Point.h:273
An affine coordinate transformation consisting of a linear transformation and an offset.
virtual std::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:639
geom::Point2D skyToPixel(geom::Angle sky1, geom::Angle sky2) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:740
Eigen::Matrix2d getCDMatrix() const
Returns the CD matrix.
Definition: Wcs.cc:545
virtual void shiftReferencePixel(double dx, double dy)
Move the pixel reference position by (dx, dy)
Definition: Wcs.cc:1052
Wcs()
Construct an invalid Wcs given no arguments.
Definition: Wcs.cc:84
virtual void flipImage(int flipLR, int flipTB, lsst::afw::geom::Extent2I dimensions) const
Flip CD matrix around the y-axis.
Definition: Wcs.cc:563
void _initWcs()
Set some internal variables that we need to refer to.
Definition: Wcs.cc:114
XYTransformFromWcsPair(std::shared_ptr< Wcs const > dst, std::shared_ptr< Wcs const > src)
Definition: Wcs.cc:1162
bool isSameSkySystem(Wcs const &wcs) const
Return true if a WCS has the same coordinate system and equinox as this one.
Definition: Wcs.cc:791
geom::Angle pixelScale() const
Returns the pixel scale [Angle/pixel].
Definition: Wcs.cc:692
std::shared_ptr< lsst::afw::coord::Coord > getSkyOrigin() const
Returns CRVAL. This need not be the centre of the image.
Definition: Wcs.cc:532
bool operator!=(Wcs const &other) const
Definition: Wcs.h:141
std::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:841
virtual Point2D reverseTransform(Point2D const &pixel) const
Definition: Wcs.cc:1181
lsst::afw::geom::Point2D getPixelOrigin() const
Returns CRPIX (corrected to LSST convention).
Definition: Wcs.cc:537
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Wcs.cc:975
bool _skyAxesSwapped
if true then the sky axes are swapped
Definition: Wcs.h:451
Base class for all records.
Definition: BaseRecord.h:29
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
Definition: Wcs.h:448
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
Definition: Wcs.h:446
virtual void pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const
Worker routine for pixelToSky.
Definition: Wcs.cc:798
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
table::Key< double > equinox
Definition: Wcs.cc:934
std::shared_ptr< Wcs const > _dst
Definition: Wcs.h:516
Virtual base class for 2D transforms.
Definition: XYTransform.h:46
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:447
afw::table::Key< afw::table::Array< ImagePixelT > > image
virtual bool isPersistable() const
Whether the Wcs is persistable using afw::table::io archives.
Definition: Wcs.cc:1006
bool isFlipped() const
Does the Wcs follow the convention of North=Up, East=Left?
Definition: Wcs.cc:643
table::PointKey< int > pixel
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:897
std::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0=0, int const y0=0)
Definition: Wcs.cc:1069
table::PointKey< double > crval
Definition: Wcs.cc:929
table::Key< std::string > ctype1
Definition: Wcs.cc:932
double getEquinox() const
Definition: Wcs.cc:784
This is the base class for spherical coordinates.
Definition: Coord.h:63
geom::LinearTransform getLinearTransform() const
Return the linear part of the Wcs, the CD matrix in FITS-speak, as an AffineTransform.
Definition: Wcs.cc:911
std::shared_ptr< Wcs > makeWcs(std::shared_ptr< lsst::daf::base::PropertySet > const &fitsMetadata, bool stripMetadata=false)
Create a Wcs object from a fits header.
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:173
virtual Point2D forwardTransform(Point2D const &pixel) const
virtuals for forward and reverse transforms
Definition: Wcs.cc:1169
std::shared_ptr< Wcs const > _src
Definition: Wcs.h:517
struct wcsprm * _wcsInfo
Definition: Wcs.h:444
coord::CoordSystem _coordSystem
Definition: Wcs.h:450
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Definition: Wcs.cc:1096
afw::geom::Point2D Point2D
Definition: XYTransform.h:48
table::PointKey< double > crpix
Definition: Wcs.cc:930
virtual void shiftReferencePixel(geom::Extent2D const &d)
Definition: Wcs.h:333