LSSTApplications  11.0-24-g0a022a1,15.0+8,15.0+9,15.0-1-g19261fa+1,15.0-1-g1eca518+10,15.0-1-g60afb23+8,15.0-1-g615e0bb,15.0-1-g6668b0b+5,15.0-1-g788a293+8,15.0-1-ga91101e+8,15.0-1-gae1598d+7,15.0-1-gc45031d+10,15.0-1-gd076f1f+8,15.0-1-gdf18595+1,15.0-1-gf4f1c34+7,15.0-18-g5f205baaa,15.0-2-g100d730+1,15.0-2-g18f3f21,15.0-2-g35685a8+1,15.0-2-g947dc0d+10,15.0-2-ge3d7f4b+1,15.0-2-gf38729e,15.0-3-g150fc43+9,15.0-3-g6f085af+1,15.0-3-g9103c06+7,15.0-3-ga03b4ca+10,15.0-3-gaec6799+5,15.0-3-gb7a597c+8,15.0-3-ge6a6747,15.0-4-g45f767a+7,15.0-4-g654b129+6,15.0-4-gff20472+11,15.0-5-g389937dc+6,15.0-5-ga70c291+1,15.0-6-gbad5ef12+1,15.0-6-ge2d9597+10
LSSTDataManagementBasePackage
SipToGtransfo.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
3 #include "Eigen/Core"
5 #include "lsst/afw/geom/Angle.h"
6 #include "lsst/afw/geom/SkyWcs.h"
9 #include "lsst/jointcal/Point.h"
10 #include "lsst/jointcal/Frame.h"
12 
13 namespace jointcal = lsst::jointcal;
14 namespace afwImg = lsst::afw::image;
15 namespace afwGeom = lsst::afw::geom;
16 
17 namespace lsst {
18 namespace jointcal {
19 
21 
22 /* The inverse transformation i.e. convert from the fit result to the SIP
23  convention. */
25  const jointcal::Frame &ccdFrame,
26  const bool noLowOrderSipTerms) {
27  GtransfoLin linPart = wcsTransfo.getLinPart();
28  afwGeom::Point2D crpix_lsst; // in LSST "frame"
29  /* In order to remove the low order sip terms, one has to
30  define the linear WCS transformation as the expansion of
31  the total pix-to-tangent plane (or focal plane) at the
32  tangent point. In order to do that, we first have to find
33  which pixel transforms to the tangent point, and then expand there */
34 
35  /* compute crpix as the point that is transformed by the linear part
36  into (0,0) */
37  linPart.invert().apply(0., 0., crpix_lsst[0], crpix_lsst[1]);
38 
39  // This is what we have to respect:
40  jointcal::GtransfoPoly pix2TP = wcsTransfo.getPix2TangentPlane();
41 
42  if (noLowOrderSipTerms) {
43  Point ctmp = Point(crpix_lsst[0], crpix_lsst[1]);
44  // cookup a large Frame
45  jointcal::Frame f(ctmp.x - 10000, ctmp.y - 10000, ctmp.x + 10000, ctmp.y + 10000);
46  auto r = pix2TP.inverseTransfo(1e-6, f);
47  // overwrite crpix ...
48  r->apply(0, 0, crpix_lsst[0], crpix_lsst[1]);
49  // and the "linpart"
50  linPart = pix2TP.linearApproximation(Point(crpix_lsst[0], crpix_lsst[1]));
51  }
52 
53  /* At this stage, crpix should not be shifted from "LSST units" to
54  "FITS units" yet because the SkyWcs constructors expect it in LSST
55  units */
56 
58  wcsTransfo.getTangentPoint().y * afwGeom::degrees);
59 
60  // CD matrix:
61  Eigen::Matrix2d cdMat;
62  cdMat(0, 0) = linPart.coeff(1, 0, 0); // CD1_1
63  cdMat(0, 1) = linPart.coeff(0, 1, 0); // CD1_2
64  cdMat(1, 0) = linPart.coeff(1, 0, 1); // CD2_1
65  cdMat(1, 1) = linPart.coeff(0, 1, 1); // CD2_2
66 
67  if (!wcsTransfo.getCorr()) { // the WCS has no distortions
68  return afw::geom::makeSkyWcs(crpix_lsst, crval, cdMat);
69  }
70 
71  /* We are now given:
72  - CRPIX
73  - The CD matrix
74  - the lin part (i.e. the combination of CRPIX and CD)
75  - and pix2TP, the total transformation from pixels to tangent plane
76  and we want to extract the SIP polynomials. The algebra is detailed
77  in the appendix of the documentation */
78 
79  // This is (the opposite of) the crpix that will go into the fits header:
80  jointcal::GtransfoLinShift s2(-crpix_lsst[0], -crpix_lsst[1]);
81 
82  // for SIP, pix2TP = linpart*sipStuff, so
84  // then the sip transform reads ST = (ID+PA*S2)
85  // PA*S2 = ST -ID, PA = (ST-Id)*S2^-1
86  jointcal::GtransfoLin id; // default constructor = identity
87  jointcal::GtransfoPoly sipPoly = (sipTransform - id) * s2.invert();
88 
89  // coockup the inverse sip polynomials
90  // last argument : precision in pixels.
91  auto tp2Pix = inversePolyTransfo(pix2TP, ccdFrame, 1e-4);
92  if (!tp2Pix) {
94  "GtransfoToSip: could not invert the input wcs ");
95  }
96  jointcal::GtransfoPoly invSipStuff = (*tp2Pix) * linPart;
97  jointcal::GtransfoPoly sipPolyInv = (invSipStuff - id) * s2.invert();
98 
99  // now extract sip coefficients. First forward ones:
100  int sipOrder = sipPoly.getDegree();
101  Eigen::MatrixXd sipA(Eigen::MatrixXd::Zero(sipOrder + 1, sipOrder + 1));
102  Eigen::MatrixXd sipB(Eigen::MatrixXd::Zero(sipOrder + 1, sipOrder + 1));
103  for (int i = 0; i <= sipOrder; ++i) {
104  for (int j = 0; j <= sipOrder - i; ++j) {
105  sipA(i, j) = sipPoly.coeff(i, j, 0);
106  sipB(i, j) = sipPoly.coeff(i, j, 1);
107  }
108  }
109 
110  // now backwards coefficients
111  sipOrder = sipPolyInv.getDegree();
112  Eigen::MatrixXd sipAp(Eigen::MatrixXd::Zero(sipOrder + 1, sipOrder + 1));
113  Eigen::MatrixXd sipBp(Eigen::MatrixXd::Zero(sipOrder + 1, sipOrder + 1));
114  for (int i = 0; i <= sipOrder; ++i) {
115  for (int j = 0; j <= sipOrder - i; ++j) {
116  sipAp(i, j) = sipPolyInv.coeff(i, j, 0);
117  sipBp(i, j) = sipPolyInv.coeff(i, j, 1);
118  }
119  }
120 
121  return makeTanSipWcs(crpix_lsst, crval, cdMat, sipA, sipB, sipAp, sipBp);
122 }
123 
124 } // namespace jointcal
125 } // namespace lsst
Implements the (forward) SIP distorsion scheme.
Definition: Gtransfo.h:520
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:294
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:59
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Definition: Gtransfo.cc:486
A point in a plane.
Definition: Point.h:13
GtransfoLin invert() const
returns the inverse: T1 = T2.invert();
Definition: Gtransfo.cc:1090
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
table::PointKey< double > crval
Definition: OldWcs.cc:130
Polynomial transformation class.
Definition: Gtransfo.h:195
std::shared_ptr< lsst::afw::geom::SkyWcs > gtransfoToTanWcs(const lsst::jointcal::TanSipPix2RaDec wcsTransfo, const lsst::jointcal::Frame &ccdFrame, const bool noLowOrderSipTerms=false)
Transform the other way around.
GtransfoPoly getPix2TangentPlane() const
the transformation from pixels to tangent plane (degrees)
Definition: Gtransfo.cc:1415
double x
coordinate
Definition: Point.h:18
std::unique_ptr< GtransfoPoly > inversePolyTransfo(const Gtransfo &Direct, const Frame &frame, const double Prec)
approximates the inverse by a polynomial, up to required precision.
Definition: Gtransfo.cc:1013
rectangle with sides parallel to axes.
Definition: Frame.h:19
A base class for image defects.
Definition: cameraGeom.dox:3
GtransfoLin getLinPart() const
The Linear part (corresponding to CD&#39;s and CRPIX&#39;s)
Definition: Gtransfo.cc:1268
const GtransfoPoly * getCorr() const
Get a non-owning pointer to the correction transform polynomial.
Definition: Gtransfo.h:452
unsigned getDegree() const
returns degree
Definition: Gtransfo.h:218
int id
Definition: CR.cc:155
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
virtual GtransfoLin linearApproximation(const Point &where, const double step=0.01) const
linear (local) approximation.
Definition: Gtransfo.cc:93
just here to provide a specialized constructor, and fit.
Definition: Gtransfo.h:358
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition: SkyWcs.cc:447
afw::table::Key< afw::table::Array< ImagePixelT > > image
Point getTangentPoint() const
Get the sky origin (CRVAL in FITS WCS terminology) in degrees.
Definition: Gtransfo.cc:1266
double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
access to coefficients (read only)
Definition: Gtransfo.cc:651
std::shared_ptr< jointcal::GtransfoPoly > GtPoly_Ptr
virtual std::unique_ptr< Gtransfo > inverseTransfo(const double precision, const Frame &region) const
returns an inverse transfo. Numerical if not overloaded.
Definition: Gtransfo.cc:252
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:457