lsst.afw  15.0-25-gacd707fca
SkyWcs.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2017 AURA/LSST.
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_GEOM_SKYWCS_H
25 #define LSST_AFW_GEOM_SKYWCS_H
26 
27 #include <memory>
28 #include <utility>
29 #include <vector>
30 
31 #include "Eigen/Core"
32 #include "astshim.h"
33 #include "ndarray.h"
34 
37 #include "lsst/afw/geom/Angle.h"
38 #include "lsst/afw/geom/Point.h"
39 #include "lsst/afw/geom/Endpoint.h"
42 
43 namespace lsst {
44 namespace afw {
45 namespace geom {
46 
60 Eigen::Matrix2d makeCdMatrix(Angle const &scale, Angle const &orientation = 0 * degrees, bool flipX = false);
61 
113 class SkyWcs final : public table::io::PersistableFacade<SkyWcs>, public table::io::Persistable {
114 public:
115  SkyWcs(SkyWcs const &) = default;
116  SkyWcs(SkyWcs &&) = default;
117  SkyWcs &operator=(SkyWcs const &) = delete;
118  SkyWcs &operator=(SkyWcs &&) = delete;
119  ~SkyWcs() = default;
120 
131  bool operator==(SkyWcs const &other) const;
132  bool operator!=(SkyWcs const &other) const { return !(*this == other); }
133 
145  explicit SkyWcs(daf::base::PropertySet &metadata, bool strip = false);
146 
159  explicit SkyWcs(ast::FrameDict const &frameDict);
160 
169 
186  std::shared_ptr<daf::base::PropertyList> getFitsMetadata(bool precise = false) const;
187 
195  Angle getPixelScale(Point2D const &pixel) const;
196 
204  Angle getPixelScale() const { return _pixelScaleAtOrigin; };
205 
211  Point2D getPixelOrigin() const { return _pixelOrigin; };
212 
218  SpherePoint getSkyOrigin() const;
219 
225  Eigen::Matrix2d getCdMatrix(Point2D const &pixel) const;
226 
232  Eigen::Matrix2d getCdMatrix() const;
233 
237  std::shared_ptr<SkyWcs> getTanWcs(Point2D const &pixel) const;
238 
248 
254 
262  bool isFlipped() const;
263 
264  bool isPersistable() const override { return true; }
265 
278  AffineTransform linearizePixelToSky(SpherePoint const &coord, AngleUnit const &skyUnit) const;
279 
292  AffineTransform linearizePixelToSky(Point2D const &pixel, AngleUnit const &skyUnit) const;
293 
306  AffineTransform linearizeSkyToPixel(SpherePoint const &coord, AngleUnit const &skyUnit) const;
307 
320  AffineTransform linearizeSkyToPixel(Point2D const &pixel, AngleUnit const &skyUnit) const;
321 
326  SpherePoint pixelToSky(Point2D const &pixel) const { return _transform->applyForward(pixel); }
327  SpherePoint pixelToSky(double x, double y) const { return pixelToSky(Point2D(x, y)); }
329  return _transform->applyForward(pixels);
330  }
332 
337  Point2D skyToPixel(SpherePoint const &sky) const { return _transform->applyInverse(sky); }
339  return _transform->applyInverse(sky);
340  }
342 
344 
350  bool hasFitsApproximation() const { return false; }
351 
357  bool isFits() const;
358 
365 
368 
386  void writeStream(std::ostream &os) const;
387 
389  std::string writeString() const;
390 
391 protected:
392  // Override methods required by afw::table::io::Persistable
393  std::string getPersistenceName() const override;
394  std::string getPythonModule() const override;
395  void write(OutputArchiveHandle &handle) const override;
396 
397 private:
398  /*
399  * Construct a SkyWcs from a shared pointer to an ast::FrameDict
400  *
401  * The frameDict may be modified.
402  */
403  explicit SkyWcs(std::shared_ptr<ast::FrameDict> frameDict);
404 
405  /*
406  * Check a FrameDict to see if it can safely be used for a SkyWcs
407  * Return a copy so that it can be used as an argument to the SkyWcs(shared_ptr<FrameDict>) constructor
408  */
409  std::shared_ptr<ast::FrameDict> _checkFrameDict(ast::FrameDict const &frameDict) const;
410 
411  // the full FrameDict, for operations that need intermediate frames
413  // cached transform from _frameDict, for fast computation of pixels<->sky
415  Point2D _pixelOrigin; // cached pixel origin
416  Angle _pixelScaleAtOrigin; // cached pixel scale at pixel origin
417 
418  /*
419  * Implementation for the overloaded public linearizePixelToSky methods, requiring both a pixel coordinate
420  * and the corresponding sky coordinate.
421  */
422  AffineTransform _linearizePixelToSky(Point2D const &pix, SpherePoint const &coord,
423  AngleUnit const &skyUnit) const;
424 
425  /*
426  * Implementation for the overloaded public linearizeSkyToPixel methods, requiring both a pixel coordinate
427  * and the corresponding sky coordinate.
428  */
429  AffineTransform _linearizeSkyToPixel(Point2D const &pix, SpherePoint const &coord,
430  AngleUnit const &skyUnit) const;
431 
433  void _computeCache() {
434  _transform = std::make_shared<TransformPoint2ToSpherePoint>(*_frameDict->getMapping(), true);
435  _pixelOrigin = skyToPixel(getSkyOrigin());
436  _pixelScaleAtOrigin = getPixelScale(_pixelOrigin);
437  }
438 };
439 
448 std::shared_ptr<SkyWcs> makeFlippedWcs(SkyWcs const &wcs, bool flipLR, bool flipTB,
449  geom::Point2D const &center);
450 
478  bool modifyActualPixels);
479 
496 
508  Eigen::Matrix2d const &cdMatrix, std::string const &projection = "TAN");
509 
521 std::shared_ptr<SkyWcs> makeTanSipWcs(Point2D const &crpix, SpherePoint const &crval,
522  Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA,
523  Eigen::MatrixXd const &sipB);
524 
538 std::shared_ptr<SkyWcs> makeTanSipWcs(Point2D const &crpix, SpherePoint const &crval,
539  Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA,
540  Eigen::MatrixXd const &sipB, Eigen::MatrixXd const &sipAp,
541  Eigen::MatrixXd const &sipBp);
554 
559  bool simplify = true);
560 
567  bool simplify = true);
568 
575 
576 } // namespace geom
577 } // namespace afw
578 } // namespace lsst
579 
580 #endif
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:61
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Definition: SkyWcs.h:113
lsst::afw::geom::Angle Angle
Definition: misc.h:33
An object passed to Persistable::write to allow it to persist itself.
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
bool isFits() const
Return true getFitsMetadata(true) will succeed, false if not.
Definition: SkyWcs.cc:221
table::PointKey< double > crpix
Definition: OldWcs.cc:131
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:113
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
std::shared_ptr< SkyWcs > makeModifiedWcs(TransformPoint2ToPoint2 const &pixelTransform, SkyWcs const &wcs, bool modifyActualPixels)
Create a new SkyWcs whose pixels are transformed by pixelTransform, as described below.
Definition: SkyWcs.cc:411
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: SkyWcs.cc:304
bool strip
Definition: fits.cc:817
table::PointKey< double > crval
Definition: OldWcs.cc:130
ItemVariant const * other
Definition: Schema.cc:55
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
STL class.
std::shared_ptr< const ast::FrameDict > getFrameDict() const
Get the contained FrameDict.
Definition: SkyWcs.cc:217
std::vector< SpherePoint > pixelToSky(std::vector< Point2D > const &pixels) const
Compute sky position(s) from pixel position(s)
Definition: SkyWcs.h:328
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: SkyWcs.cc:302
Eigen::Matrix2d getCdMatrix() const
Get the 2x2 CD matrix at the pixel origin.
Definition: SkyWcs.cc:174
std::shared_ptr< const TransformPoint2ToSpherePoint > getTransform() const
Get a TransformPoint2ToSpherePoint that transforms pixels to sky in the forward direction and sky to ...
Definition: SkyWcs.h:253
A class representing an angle.
Definition: Angle.h:102
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:66
A base class for image defects.
bool hasFitsApproximation() const
Does this SkyWcs have an approximate SkyWcs that can be represented as standard FITS WCS...
Definition: SkyWcs.h:350
Point< double, 2 > Point2D
Definition: Point.h:304
An affine coordinate transformation consisting of a linear transformation and an offset.
std::shared_ptr< RecordT > src
Definition: Match.cc:48
std::vector< Point2D > skyToPixel(std::vector< SpherePoint > const &sky) const
Compute pixel position(s) from sky position(s)
Definition: SkyWcs.h:338
std::shared_ptr< SkyWcs > getTanWcs(Point2D const &pixel) const
Get a local TAN WCS approximation to this WCS at the specified pixel position.
Definition: SkyWcs.cc:176
SpherePoint pixelToSky(double x, double y) const
Compute sky position(s) from pixel position(s)
Definition: SkyWcs.h:327
std::shared_ptr< SkyWcs > makeFlippedWcs(SkyWcs const &wcs, bool flipLR, bool flipTB, geom::Point2D const &center)
Return a copy of a FITS-WCS with pixel positions flipped around a specified center.
Definition: SkyWcs.cc:392
Angle getPixelScale() const
Get the pixel scale at the pixel origin.
Definition: SkyWcs.h:204
static std::shared_ptr< SkyWcs > readStream(std::istream &is)
Deserialize a SkyWcs from an input stream.
Definition: SkyWcs.cc:257
static std::string getShortClassName()
Definition: SkyWcs.cc:247
std::shared_ptr< TransformPoint2ToPoint2 > getPixelToIntermediateWorldCoords(SkyWcs const &wcs, bool simplify=true)
Return a transform from pixel coordinates to intermediate world coordinates.
Definition: SkyWcs.cc:477
double x
std::shared_ptr< TransformPoint2ToSpherePoint > getIntermediateWorldCoordsToSky(SkyWcs const &wcs, bool simplify=true)
Return a transform from intermediate world coordinates to sky.
Definition: SkyWcs.cc:471
AffineTransform linearizeSkyToPixel(SpherePoint const &coord, AngleUnit const &skyUnit) const
Return the local linear approximation to skyToPixel at a point given in sky coordinates.
Definition: SkyWcs.cc:239
bool operator!=(SkyWcs const &other) const
Definition: SkyWcs.h:132
Point2D getPixelOrigin() const
Get the pixel origin, in pixels, using the LSST convention.
Definition: SkyWcs.h:211
std::shared_ptr< TransformPoint2ToPoint2 > makeWcsPairTransform(SkyWcs const &src, SkyWcs const &dst)
A Transform obtained by putting two SkyWcs objects "back to back".
Definition: SkyWcs.cc:123
STL class.
std::shared_ptr< SkyWcs > copyAtShiftedPixelOrigin(Extent2D const &shift) const
Return a copy of this SkyWcs with the pixel origin shifted by the specified amount.
Definition: SkyWcs.cc:183
static std::shared_ptr< SkyWcs > readString(std::string &str)
Deserialize a SkyWcs from a string, using the same format as readStream.
Definition: SkyWcs.cc:286
SpherePoint pixelToSky(Point2D const &pixel) const
Compute sky position(s) from pixel position(s)
Definition: SkyWcs.h:326
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: SkyWcs.cc:306
Point2D skyToPixel(SpherePoint const &sky) const
Compute pixel position(s) from sky position(s)
Definition: SkyWcs.h:337
SkyWcs(SkyWcs const &)=default
std::string writeString() const
Serialize this SkyWcs to a string, using the same format as writeStream.
Definition: SkyWcs.cc:296
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition: SkyWcs.cc:446
std::shared_ptr< daf::base::PropertyList > getFitsMetadata(bool precise=false) const
Return the WCS as FITS WCS metadata.
Definition: SkyWcs.cc:188
bool isPersistable() const override
Return true if this particular object can be persisted using afw::table::io.
Definition: SkyWcs.h:264
AffineTransform linearizePixelToSky(SpherePoint const &coord, AngleUnit const &skyUnit) const
Return the local linear approximation to pixelToSky at a point given in sky coordinates.
Definition: SkyWcs.cc:232
Eigen::Matrix2d makeCdMatrix(Angle const &scale, Angle const &orientation=0 *degrees, bool flipX=false)
Make a WCS CD matrix.
Definition: SkyWcs.cc:111
table::PointKey< int > pixel
void writeStream(std::ostream &os) const
Serialize this SkyWcs to an output stream.
Definition: SkyWcs.cc:291
bool isFlipped() const
Does the WCS follow the convention of North=Up, East=Left?
Definition: SkyWcs.cc:249
SkyWcs & operator=(SkyWcs const &)=delete
std::ostream * os
Definition: Schema.cc:736
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
STL class.
SpherePoint getSkyOrigin() const
Get the sky origin, the celestial fiducial point.
Definition: SkyWcs.cc:157
bool operator==(SkyWcs const &other) const
Equality is based on the string representations being equal.
Definition: SkyWcs.cc:133
int y
Definition: SpanSet.cc:44
Transform LSST spatial data, such as Point2D and SpherePoint, using an AST mapping.
Definition: Transform.h:67
std::shared_ptr< SkyWcs > makeTanSipWcs(Point2D const &crpix, SpherePoint const &crval, Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA, Eigen::MatrixXd const &sipB)
Construct a TAN-SIP SkyWcs with forward SIP distortion terms and an iterative inverse.
Definition: SkyWcs.cc:456