lsst.afw  14.0-40-g0271be365
Wcs.cc
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 #include <iostream>
26 #include <sstream>
27 #include <cmath>
28 #include <cstring>
29 #include <limits>
30 
31 #include "boost/format.hpp"
32 
33 #include "wcslib/wcs.h"
34 #include "wcslib/wcsfix.h"
35 #include "wcslib/wcshdr.h"
36 
37 #include "lsst/daf/base.h"
38 #include "lsst/daf/base/Citizen.h"
41 #include "lsst/pex/exceptions.h"
43 #include "lsst/afw/image/Wcs.h"
44 #include "lsst/afw/coord/Coord.h"
45 #include "lsst/afw/geom/Angle.h"
50 
51 namespace except = lsst::pex::exceptions;
52 
53 using namespace std;
54 
59 
60 // The amount of space allocated to strings in wcslib
61 const int STRLEN = 72;
62 
63 namespace lsst {
64 namespace afw {
65 namespace image {
66 
67 // Set internal params for wcslib
68 void Wcs::_setWcslibParams() {
69  _wcsfixCtrl = // ctrl for wcsfix
70  2; // Translate "H" to "h"
71  _wcshdrCtrl = // ctrl for wcspih
72  2; // Report each rejected keyrecord and the reason why it was rejected
73  _relax = // relax parameter for wcspih;
74  WCSHDR_all; // Accept all extensions recognized by the parser
75 }
76 
77 const int lsstToFitsPixels = +1;
78 const int fitsToLsstPixels = -1;
79 
80 //
81 // Constructors
82 //
83 
84 Wcs::Wcs()
85  : daf::base::Citizen(typeid(this)),
86  _wcsInfo(NULL),
87  _nWcsInfo(0),
88  _relax(0),
89  _wcsfixCtrl(0),
90  _wcshdrCtrl(0),
91  _nReject(0),
92  _coordSystem(coord::UNKNOWN) // set by _initWcs
93 {
95  _initWcs();
96 }
97 
99  : daf::base::Citizen(typeid(this)),
100  _wcsInfo(NULL),
101  _nWcsInfo(0),
102  _relax(0),
103  _wcsfixCtrl(0),
104  _wcshdrCtrl(0),
105  _nReject(0),
106  _coordSystem(coord::UNKNOWN) // set by _initWcs
107 {
109 
110  initWcsLibFromFits(fitsMetadata);
111  _initWcs();
112 }
113 
115  // first four characters of CTYPE1 (name of first axis)
116  std::string ctype1 = std::string(_wcsInfo->ctype[0]).substr(0, 4);
117 
118  if (_wcsInfo) {
119  if (ctype1[0] == 'G') {
121  _skyAxesSwapped = (ctype1[2] == 'A'); // GLAT instead of GLON
122  } else if (ctype1[0] == 'E') {
124  _skyAxesSwapped = (ctype1[2] == 'A'); // ELAT instead of ELON
125  } else {
127  _skyAxesSwapped = (ctype1[0] == 'D'); // DEC instead of RA
128  }
129 
130  // tell WCSlib that values have been updated
131  _wcsInfo->flag = 0;
132  // and then tell it to do its internal magic.
133  int status = wcsset(_wcsInfo);
134  if (status != 0) {
136  (boost::format("Failed to setup wcs structure with wcsset. Status %d: %s") %
137  status % wcs_errmsg[status])
138  .str());
139  }
140  }
141 }
142 
143 Wcs::Wcs(GeomPoint const& crval, GeomPoint const& crpix, Eigen::Matrix2d const& CD, std::string const& ctype1,
144  std::string const& ctype2, double equinox, std::string const& raDecSys, std::string const& cunits1,
145  std::string const& cunits2)
146  : daf::base::Citizen(typeid(this)),
147  _wcsInfo(NULL),
148  _nWcsInfo(0),
149  _relax(0),
150  _wcsfixCtrl(0),
151  _wcshdrCtrl(0),
152  _nReject(0),
153  _coordSystem(coord::UNKNOWN) // set by _initWcs
154 {
156  initWcsLib(crval, crpix, CD, ctype1, ctype2, equinox, raDecSys, cunits1, cunits2);
157  _initWcs();
158 }
159 
161  /*
162  * Access control for the input header
163  *
164  * We want to hack up the input, and in order to do so we need to do a deep copy on it.
165  * We only want to do that copy once, and would like to avoid doing it altogether.
166  */
167  class HeaderAccess {
168  public:
169  // Return a readable version of the metadata
170  std::shared_ptr<daf::base::PropertySet const> const& toRead() { return _constHeader; }
171  // Return a writable version of the metadata
172  std::shared_ptr<daf::base::PropertySet> const& toWrite() {
173  if (!_hackHeader) {
174  _hackHeader = _constHeader->deepCopy();
175  _constHeader = _hackHeader;
176  }
177  return _hackHeader;
178  }
179 
180  // Ctor
181  HeaderAccess(std::shared_ptr<daf::base::PropertySet const> const& header)
182  : _constHeader(header), _hackHeader() {}
183 
184  private:
187  };
188 
189  HeaderAccess access(header);
190 
191  // Some headers (e.g. SDSS ones from FNAL) have EQUINOX as a string. Fix this,
192  // as wcslib 4.4.4 refuses to handle it. Furthermore, some headers (e.g. Skymapper)
193  // use a string but prepend 'J': "J2000.0" -- if we don't handle this, we deduce
194  // an equinox of 0.0
195  {
196  std::string const& key = "EQUINOX";
197  if (access.toRead()->exists(key) && access.toRead()->typeOf(key) == typeid(std::string)) {
198  auto equinoxStr = access.toRead()->getAsString(key).c_str();
199  double equinox = ::atof(equinoxStr[0] == 'J' ? equinoxStr + 1 : equinoxStr);
200  access.toWrite()->set(key, equinox);
201  }
202  }
203 
204  //Check header isn't empty
205  int nCards = lsst::afw::formatters::countFitsHeaderCards(*(access.toRead()));
206  if (nCards <= 0) {
207  string msg = "Could not parse FITS WCS: no header cards found";
209  }
210 
211  // While the standard does not insist on CRVAL and CRPIX being present, it
212  // is almost certain their absence indicates a problem.
213  // Check for CRPIX
214  if (!access.toRead()->exists("CRPIX1") && !access.toRead()->exists("CRPIX1a")) {
215  string msg = "Neither CRPIX1 not CRPIX1a found";
217  }
218 
219  if (!access.toRead()->exists("CRPIX2") && !access.toRead()->exists("CRPIX2a")) {
220  string msg = "Neither CRPIX2 not CRPIX2a found";
222  }
223 
224  // And the same for CRVAL
225  if (!access.toRead()->exists("CRVAL1") && !access.toRead()->exists("CRVAL1a")) {
226  string msg = "Neither CRVAL1 not CRVAL1a found";
228  }
229 
230  if (!access.toRead()->exists("CRVAL2") && !access.toRead()->exists("CRVAL2a")) {
231  string msg = "Neither CRVAL2 not CRVAL2a found";
233  }
234  /*
235  * According to Greisen and Calabretta (A&A 395, 1061–1075 (2002)) it's illegal to mix PCi_j and CDi_j
236  * headers; unfortunately Subaru puts both in its headers. It actually uses PC001002 instead of PC1_2
237  * (dating to a proposed FITS standard from 1996) and at least sometimes fails to include CDELT[12],
238  * so the CD and PC matrices are inconsistent
239  *
240  * If we detect any part of a CD matrix, delete all PC matrices
241  */
242  if (access.toRead()->exists("CD1_1") || access.toRead()->exists("CD1_2") ||
243  access.toRead()->exists("CD2_1") || access.toRead()->exists("CD2_2")) {
244  for (int i = 1; i <= 2; ++i) {
245  for (int j = 1; j <= 2; ++j) {
246  std::string key = (boost::format("PC%i_%i") % j % i).str();
247  if (access.toRead()->exists(key)) {
248  double const val = access.toRead()->getAsDouble(key);
249  access.toWrite()->remove(key);
250  access.toWrite()->add("X_" + key, val);
251  }
252 
253  key = (boost::format("PC%03d%03d") % j % i).str();
254  if (access.toRead()->exists(key)) {
255  double const val = access.toRead()->getAsDouble(key);
256  access.toWrite()->remove(key);
257  access.toWrite()->add("X_" + key, val);
258  }
259  }
260  }
261  }
262 
263  //Pass the header into wcslib's formatter to extract & setup the Wcs. First need
264  //to convert to a C style string, so the compile doesn't complain about constness
265  std::string metadataStr = formatters::formatFitsProperties(*(access.toRead()));
266  // We own the data, and wcslib is slack about constness, so no qualms with casting away const
267  char* hdrString = const_cast<char*>(metadataStr.c_str());
268  // printf("wcspih string:\n%s\n", hdrString);
269 
270  nCards = formatters::countFitsHeaderCards(*(access.toRead())); // we may have dropped some
271  int pihStatus = wcspih(hdrString, nCards, _relax, _wcshdrCtrl, &_nReject, &_nWcsInfo, &_wcsInfo);
272 
273  if (pihStatus != 0) {
275  (boost::format("Could not parse FITS WCS: wcspih status = %d (%s)") % pihStatus %
276  wcs_errmsg[pihStatus])
277  .str());
278  }
279 
280  // Run wcsfix on _wcsInfo to try and fix any problems it knows about.
281  const int* naxes = NULL; // should be {NAXIS1, NAXIS2, ...} to check cylindrical projections
282  int stats[NWCSFIX]; // status returns from wcsfix
283  int fixStatus = wcsfix(_wcsfixCtrl, naxes, _wcsInfo, stats);
284  if (fixStatus != 0) {
285  std::stringstream errStream;
286  errStream << "Could not parse FITS WCS: wcsfix failed " << std::endl;
287  for (int ii = 0; ii < NWCSFIX; ++ii) {
288  if (stats[ii] >= 0) {
289  errStream << "\t" << ii << ": " << stats[ii] << " " << wcsfix_errmsg[stats[ii]] << std::endl;
290  } else {
291  errStream << "\t" << ii << ": " << stats[ii] << std::endl;
292  }
293  }
294  }
295 
296  // The Wcs standard requires a default value for RADESYS if the keyword
297  // doesn't exist in header, but wcslib doesn't set it. So we do so here. This code
298  // conforms to Calabretta & Greisen 2002 \S 3.1
299  if (!(access.toRead()->exists("RADESYS") || access.toRead()->exists("RADESYSa"))) {
300  // If RADECSYS exists, use that (counter to Calabretta & Greisen 2002 \S 3.1, but commonly used).
301  // If equinox exist and < 1984, use FK4. If >= 1984, use FK5
302  if (access.toRead()->exists("RADECSYS")) {
303  strncpy(_wcsInfo->radesys, access.toRead()->getAsString("RADECSYS").c_str(), STRLEN);
304  } else if (access.toRead()->exists("EQUINOX") || access.toRead()->exists("EQUINOXa")) {
305  std::string const EQUINOX = access.toRead()->exists("EQUINOX") ? "EQUINOX" : "EQUINOXa";
306  double const equinox = access.toRead()->getAsDouble(EQUINOX);
307  if (equinox < 1984) {
308  strncpy(_wcsInfo->radesys, "FK4", STRLEN);
309  } else {
310  strncpy(_wcsInfo->radesys, "FK5", STRLEN);
311  }
312  } else {
313  // If Equinox doesn't exist, default to ICRS
314  strncpy(_wcsInfo->radesys, "ICRS", STRLEN);
315  }
316  }
317  // strip trailing whitespace
318  {
319  for (int i = strlen(_wcsInfo->radesys) - 1; i >= 0; i--) {
320  if (isspace(_wcsInfo->radesys[i])) {
321  _wcsInfo->radesys[i] = '\0';
322  }
323  }
324  }
325  //
326  // If there are no CDi_j cards in the header, set CDi_j from PCi_j
327  // CDi_j == CDELTi*PCi_j
328  //
329  if ((_wcsInfo->altlin & 2) == 0) { // no CDi_j cards were found in the header
330  double const* cdelt = _wcsInfo->cdelt;
331  double const* pc = _wcsInfo->pc;
332  double* cd = _wcsInfo->cd;
333 
334  cd[0] = cdelt[0] * pc[0]; // 1_1
335  cd[1] = cdelt[0] * pc[1]; // 1_2
336  cd[2] = cdelt[1] * pc[2]; // 2_1
337  cd[3] = cdelt[1] * pc[3]; // 2_2
338  }
339 }
340 
341 void Wcs::initWcsLib(GeomPoint const& crval, GeomPoint const& crpix, Eigen::Matrix2d const& CD,
342  std::string const& ctype1, std::string const& ctype2, double equinox,
343  std::string const& raDecSys, std::string const& cunits1, std::string const& cunits2) {
344  // Check CD is a valid size
345  if ((CD.rows() != 2) || (CD.cols() != 2)) {
346  string msg = "CD is not a 2x2 matrix";
348  }
349 
350  // Check that cunits are legitimate values
351  bool isValid = (cunits1 == "deg");
352  isValid |= (cunits1 == "arcmin");
353  isValid |= (cunits1 == "arcsec");
354  isValid |= (cunits1 == "mas");
355 
356  if (!isValid) {
357  string msg = "CUNITS1 must be one of {deg|arcmin|arcsec|mas}";
359  }
360 
361  isValid = (cunits2 == "deg");
362  isValid |= (cunits2 == "arcmin");
363  isValid |= (cunits2 == "arcsec");
364  isValid |= (cunits2 == "mas");
365 
366  if (!isValid) {
367  string msg = "CUNITS2 must be one of {deg|arcmin|arcsec|mas}";
369  }
370 
371  // Initialise the wcs struct
372  _wcsInfo = static_cast<struct wcsprm*>(malloc(sizeof(struct wcsprm)));
373  if (_wcsInfo == NULL) {
374  throw LSST_EXCEPT(except::MemoryError, "Cannot allocate WCS info");
375  }
376 
377  _wcsInfo->flag = -1;
378  int status = wcsini(true, 2, _wcsInfo); // 2 indicates a naxis==2, a two dimensional image
379  if (status != 0) {
381  (boost::format("Failed to allocate memory with wcsini. Status %d: %s") % status %
382  wcs_errmsg[status])
383  .str());
384  }
385 
386  // Set crval, crpix and CD. Internally to the class, we use fits units for consistency with
387  // wcslib.
388  _wcsInfo->crval[0] = crval.getX();
389  _wcsInfo->crval[1] = crval.getY();
390  _wcsInfo->crpix[0] = crpix.getX() + lsstToFitsPixels;
391  _wcsInfo->crpix[1] = crpix.getY() + lsstToFitsPixels;
392 
393  // Set the CD matrix
394  for (int i = 0; i < 2; ++i) {
395  for (int j = 0; j < 2; ++j) {
396  _wcsInfo->cd[(2 * i) + j] = CD(i, j);
397  }
398  }
399 
400  // Specify that we have a CD matrix, but no PC or CROTA
401  _wcsInfo->altlin = 2;
402  _wcsInfo->flag = 0; // values have been updated
403 
404  // This is a work around for what I think is a bug in wcslib. ->types is neither
405  // initialised or set to NULL by default, so if I try to delete a Wcs object,
406  // wcslib then attempts to free non-existent space, and the code can crash.
407  _wcsInfo->types = NULL;
408 
409  // Set the coordinate system
410  strncpy(_wcsInfo->ctype[0], ctype1.c_str(), STRLEN);
411  strncpy(_wcsInfo->ctype[1], ctype2.c_str(), STRLEN);
412  strncpy(_wcsInfo->radesys, raDecSys.c_str(), STRLEN);
413  _wcsInfo->equinox = equinox;
414 
415  // Set the units
416  strncpy(_wcsInfo->cunit[0], cunits1.c_str(), STRLEN);
417  strncpy(_wcsInfo->cunit[1], cunits2.c_str(), STRLEN);
418 
419  _nWcsInfo = 1; // Specify that we have only one coordinate representation
420 
421  // Tell wcslib that we are need to set up internal values
422  status = wcsset(_wcsInfo);
423  if (status != 0) {
425  (boost::format("Failed to setup wcs structure with wcsset. Status %d: %s") %
426  status % wcs_errmsg[status])
427  .str());
428  }
429 }
430 
431 Wcs::Wcs(Wcs const& rhs)
432  : daf::base::Citizen(typeid(this)),
433  _wcsInfo(NULL),
434  _nWcsInfo(rhs._nWcsInfo),
435  _relax(rhs._relax),
438  _nReject(rhs._nReject),
439  _coordSystem(coord::UNKNOWN) // set by _initWcs
440 {
441  if (rhs._nWcsInfo > 0) {
442  _wcsInfo = static_cast<struct wcsprm*>(calloc(rhs._nWcsInfo, sizeof(struct wcsprm)));
443  if (_wcsInfo == NULL) {
444  throw LSST_EXCEPT(pex::exceptions::MemoryError, "Cannot allocate WCS info");
445  }
446 
447  int alloc = 1; // Unconditionally allocate memory when calling
448  for (int i = 0; i != rhs._nWcsInfo; ++i) {
449  _wcsInfo[i].flag = -1;
450  int status = wcscopy(alloc, &rhs._wcsInfo[i], &_wcsInfo[i]);
451  if (status != 0) {
452  wcsvfree(&i, &_wcsInfo);
454  (boost::format("Could not copy WCS: wcscopy status = %d : %s") % status %
455  wcs_errmsg[status])
456  .str());
457  }
458  }
459  }
460  _initWcs();
461 }
462 
463 bool Wcs::operator==(Wcs const& other) const {
464  if (&other == this) return true;
465  // We do a bidirectional test with a virtual member function in case one of us is a derived
466  // class with members we don't know about here.
467  // This is not the most efficient possible implementation, but I think it's the easiest one
468  // with which to ensure correctness, and I think that's more important in this case.
469  return this->_isSubset(other) && other._isSubset(*this);
470 }
471 
472 // convenience functions and a macro for implementing isSubset
473 namespace {
474 
475 inline bool compareArrays(double const* a, double const* b, int n) {
476  for (int i = 0; i < n; ++i)
477  if (a[i] != b[i]) return false;
478  return true;
479 }
480 
481 template <typename T>
482 inline bool compareStringArrays(T a, T b, int n) {
483  for (int i = 0; i < n; ++i)
484  if (strcmp(a[i], b[i]) != 0) return false;
485  return true;
486 }
487 
488 #define CHECK_NULLS(a, b) \
489  do { \
490  if ((a) == NULL) { \
491  if ((b) == NULL) return true; \
492  return false; \
493  } \
494  if ((b) == NULL) return false; \
495  } while (false)
496 
497 } // anonymous
498 
499 bool Wcs::_isSubset(Wcs const& rhs) const {
501  CHECK_NULLS(_wcsInfo->crval, rhs._wcsInfo->crval);
502  CHECK_NULLS(_wcsInfo->cd, rhs._wcsInfo->cd);
503  CHECK_NULLS(_wcsInfo->crpix, rhs._wcsInfo->crpix);
504  CHECK_NULLS(_wcsInfo->cunit, rhs._wcsInfo->cunit);
505  CHECK_NULLS(_wcsInfo->ctype, rhs._wcsInfo->ctype);
506  return _nWcsInfo == rhs._nWcsInfo && _coordSystem == rhs._coordSystem &&
507  _wcsInfo->naxis == rhs._wcsInfo->naxis && _wcsInfo->equinox == rhs._wcsInfo->equinox &&
508  _wcsInfo->altlin == rhs._wcsInfo->altlin &&
509  compareArrays(_wcsInfo->crval, rhs._wcsInfo->crval, 2) &&
510  compareArrays(_wcsInfo->crpix, rhs._wcsInfo->crpix, 2) &&
511  compareArrays(_wcsInfo->cd, rhs._wcsInfo->cd, 4) &&
512  compareStringArrays(_wcsInfo->cunit, rhs._wcsInfo->cunit, 2) &&
513  compareStringArrays(_wcsInfo->ctype, rhs._wcsInfo->ctype, 2) &&
514  skyToPixel(_wcsInfo->crval[0] * geom::degrees, _wcsInfo->crval[1] * geom::degrees) ==
515  rhs.skyToPixel(_wcsInfo->crval[0] * geom::degrees, _wcsInfo->crval[1] * geom::degrees) &&
516  *pixelToSky(_wcsInfo->crpix[0], _wcsInfo->crpix[1]) ==
517  *rhs.pixelToSky(_wcsInfo->crpix[0], _wcsInfo->crpix[1]);
518 }
519 
521  if (_wcsInfo != NULL) {
522  wcsvfree(&_nWcsInfo, &_wcsInfo);
523  }
524 }
525 
526 std::shared_ptr<Wcs> Wcs::clone(void) const { return std::shared_ptr<Wcs>(new Wcs(*this)); }
527 
528 //
529 // Accessors
530 //
531 
533  assert(_wcsInfo);
534  return makeCorrectCoord(_wcsInfo->crval[0] * geom::degrees, _wcsInfo->crval[1] * geom::degrees);
535 }
536 
538  assert(_wcsInfo);
539  // Convert from fits units back to lsst units
540  double p1 = _wcsInfo->crpix[0] + fitsToLsstPixels;
541  double p2 = _wcsInfo->crpix[1] + fitsToLsstPixels;
542  return geom::Point2D(p1, p2);
543 }
544 
545 Eigen::Matrix2d Wcs::getCDMatrix() const {
546  assert(_wcsInfo);
547  int const naxis = _wcsInfo->naxis;
548 
549  // If naxis != 2, I'm not sure if any of what follows is correct
550  assert(naxis == 2);
551 
552  Eigen::Matrix2d C;
553 
554  for (int i = 0; i < naxis; ++i) {
555  for (int j = 0; j < naxis; ++j) {
556  C(i, j) = _wcsInfo->cd[(i * naxis) + j];
557  }
558  }
559 
560  return C;
561 }
562 
563 void Wcs::flipImage(int flipLR, int flipTB, geom::Extent2I dimensions) const {
564  assert(_wcsInfo);
565 
566  int const naxis = _wcsInfo->naxis;
567 
568  // If naxis != 2, I'm not sure if any of what follows is correct
569  assert(naxis == 2);
570  // Origin is at (1,1). Adjust to avoid off by one.
571  if (flipLR) {
572  _wcsInfo->cd[0] = -_wcsInfo->cd[0];
573  _wcsInfo->cd[2] = -_wcsInfo->cd[2];
574  _wcsInfo->crpix[0] = -_wcsInfo->crpix[0] + dimensions.getX() + 1;
575  }
576  if (flipTB) {
577  _wcsInfo->cd[1] = -_wcsInfo->cd[1];
578  _wcsInfo->cd[3] = -_wcsInfo->cd[3];
579  _wcsInfo->crpix[1] = -_wcsInfo->crpix[1] + dimensions.getY() + 1;
580  }
581 
582  // tells libwcs to invalidate cached data, since transformation has been modified
583  _wcsInfo->flag = 0;
584 }
585 
586 void Wcs::rotateImageBy90(int nQuarter, geom::Extent2I dimensions) const {
587  assert(_wcsInfo);
588 
589  while (nQuarter < 0) {
590  nQuarter += 4;
591  }
592 
593  int const naxis = _wcsInfo->naxis;
594 
595  // If naxis != 2, I'm not sure if any of what follows is correct
596  assert(naxis == 2);
597  double a = _wcsInfo->cd[0];
598  double b = _wcsInfo->cd[1];
599  double c = _wcsInfo->cd[2];
600  double d = _wcsInfo->cd[3];
601  double crpx = _wcsInfo->crpix[0];
602  double crpy = _wcsInfo->crpix[1];
603  // Origin is at (1,1). Adjust to avoid off by one.
604  // E.g. CRPIX one pixel off the UR of the grid should go to
605  //(0,0) for nQuarter=2
606  switch (nQuarter % 4) {
607  case 0:
608  break;
609  case 1:
610  _wcsInfo->cd[0] = -b;
611  _wcsInfo->cd[1] = a;
612  _wcsInfo->cd[2] = -d;
613  _wcsInfo->cd[3] = c;
614  _wcsInfo->crpix[0] = -crpy + dimensions.getY() + 1;
615  _wcsInfo->crpix[1] = crpx;
616  break;
617  case 2:
618  _wcsInfo->cd[0] = -a;
619  _wcsInfo->cd[1] = -b;
620  _wcsInfo->cd[2] = -c;
621  _wcsInfo->cd[3] = -d;
622  _wcsInfo->crpix[0] = -crpx + dimensions.getX() + 1;
623  _wcsInfo->crpix[1] = -crpy + dimensions.getY() + 1;
624  break;
625  case 3:
626  _wcsInfo->cd[0] = b;
627  _wcsInfo->cd[1] = -a;
628  _wcsInfo->cd[2] = d;
629  _wcsInfo->cd[3] = -c;
630  _wcsInfo->crpix[0] = crpy;
631  _wcsInfo->crpix[1] = -crpx + dimensions.getX() + 1;
632  break;
633  }
634 
635  // tells libwcs to invalidate cached data, since transformation has been modified
636  _wcsInfo->flag = 0;
637 }
638 
641 }
642 
643 bool Wcs::isFlipped() const {
644  // We calculate the determinant of the CD (i.e the rotation and scaling)
645  // matrix. If this determinant is positive, then the image can be rotated
646  // to a position where increasing the right ascension and declination
647  // increases the horizontal and vertical pixel position. In this case the
648  // image is flipped.
649  assert(_wcsInfo);
650  double det = (_wcsInfo->cd[0] * _wcsInfo->cd[3]) - (_wcsInfo->cd[1] * _wcsInfo->cd[2]);
651 
652  if (det == 0) {
653  throw(LSST_EXCEPT(except::RuntimeError, "Wcs CD matrix is singular"));
654  }
655 
656  return (det > 0);
657 }
658 
659 static double square(double x) { return x * x; }
660 
661 double Wcs::pixArea(GeomPoint pix00) const {
662  //
663  // Figure out the (0, 0), (0, 1), and (1, 0) ra/dec coordinates of the corners of a square drawn in pixel
664  // It'd be better to centre the square at sky00, but that would involve another conversion between sky and
665  // pixel coordinates so I didn't bother
666  //
667  const double side = 1; // length of the square's sides in pixels
668 
669  // Work in 3-space to avoid RA wrapping and pole issues.
670  geom::Point3D v0 = pixelToSky(pix00)->getVector();
671 
672  // Step by "side" in x and y pixel directions...
673  GeomPoint px(pix00);
674  GeomPoint py(pix00);
675  px.shift(geom::Extent2D(side, 0));
676  py.shift(geom::Extent2D(0, side));
677  // Push the points through the WCS, and find difference in 3-space.
678  geom::Extent3D dx = pixelToSky(px)->getVector() - v0;
679  geom::Extent3D dy = pixelToSky(py)->getVector() - v0;
680 
681  // Compute |cross product| = area of parallelogram with sides dx,dy
682  // FIXME -- this is slightly incorrect -- it's making the small-angle
683  // approximation, taking the distance *through* the unit sphere
684  // rather than over its surface.
685  // This is in units of ~radians^2
686  double area = sqrt(square(dx[1] * dy[2] - dx[2] * dy[1]) + square(dx[2] * dy[0] - dx[0] * dy[2]) +
687  square(dx[0] * dy[1] - dx[1] * dy[0]));
688 
689  return area / square(side) * square(180. / geom::PI);
690 }
691 
693 
695  assert(_wcsInfo);
696 
697  double skyTmp[2];
698  double imgcrd[2];
699  double phi, theta;
700  double pixTmp[2];
701  /*
702  printf("_skyCoordsReversed: %c\n", (_skyCoordsReversed ? 'T' : 'F'));
703  printf("wcsinfo.lat: %i, lng: %i\n", _wcsInfo->lat, _wcsInfo->lng);
704  */
705  // WCSLib is smart enough to notice and handle crazy SDSS CTYPE1 = DEC--TAN,
706  // by recording the indices of the long and lat coordinates.
707  skyTmp[_wcsInfo->lng] = sky1.asDegrees();
708  skyTmp[_wcsInfo->lat] = sky2.asDegrees();
709 
710  int stat[1];
711  int status = 0;
712  status = wcss2p(_wcsInfo, 1, 2, skyTmp, &phi, &theta, imgcrd, pixTmp, stat);
713  if (status == 9) {
715  (boost::format("sky coordinates %s, %s degrees is not valid for this WCS") %
716  sky1.asDegrees() % sky2.asDegrees())
717  .str());
718  }
719  if (status > 0) {
721  (boost::format("Error: wcslib returned a status code of %d at sky %s, %s deg: %s") %
722  status % sky1.asDegrees() % sky2.asDegrees() % wcs_errmsg[status])
723  .str());
724  }
725 
726  // wcslib assumes 1-indexed coords
727  return geom::Point2D(pixTmp[0] + PixelZeroPos + fitsToLsstPixels,
728  pixTmp[1] + PixelZeroPos + fitsToLsstPixels);
729 }
730 
733  return skyToPixelImpl(sky->getLongitude(), sky->getLatitude());
734 }
735 
737  return coord.convert(_coordSystem, _wcsInfo->equinox);
738 }
739 
740 GeomPoint Wcs::skyToPixel(geom::Angle sky1, geom::Angle sky2) const { return skyToPixelImpl(sky1, sky2); }
741 
743  assert(_wcsInfo);
744 
746  double skyTmp[2];
747  double imgcrd[2];
748  double phi, theta;
749  double pixTmp[2];
750 
751  /*
752  printf("skyToIWC: _coordSystem = %i\n", (int)_coordSystem);
753  printf("coord (%.3f, %.3f)\n", coord->getLongitude().asDegrees(), coord->getLatitude().asDegrees());
754  printf("->sky (%.3f, %.3f)\n", sky->getLongitude().asDegrees(), sky->getLatitude().asDegrees());
755  */
756 
757  skyTmp[_wcsInfo->lng] = sky->getLongitude().asDegrees();
758  skyTmp[_wcsInfo->lat] = sky->getLatitude().asDegrees();
759 
760  // Estimate pixel coordinates
761  int stat[1];
762  int status = 0;
763  imgcrd[0] = imgcrd[1] = -1e6;
764  /*
765  printf(" skyTmp[] = (%.3f, %.3f)\n", skyTmp[0], skyTmp[1]);
766  printf(" _wcsInfo->lng,lat = %i, %i\n", _wcsInfo->lng, _wcsInfo->lat);
767  */
768  status = wcss2p(_wcsInfo, 1, 2, skyTmp, &phi, &theta, imgcrd, pixTmp, stat);
769  if (status > 0) {
771  (boost::format("Error: wcslib returned a status code of %d at sky %s, %s deg: %s") %
772  status % skyTmp[0] % skyTmp[1] % wcs_errmsg[status])
773  .str());
774  }
775  /*
776  printf("->iwc (%.3f, %.3f)\n", imgcrd[0], imgcrd[1]);
777  printf("-> pix (%.2f, %.2f)\n", pixTmp[0], pixTmp[1]);
778  std::shared_ptr<coord::Coord> crval = getSkyOrigin();
779  printf("(crval is (%.3f, %.3f))\n", crval->getLongitude().asDegrees(), crval->getLatitude().asDegrees());
780  */
781  return GeomPoint(imgcrd[0], imgcrd[1]);
782 }
783 
784 double Wcs::getEquinox() const {
785  if (_wcsInfo == nullptr) {
787  }
788  return _wcsInfo->equinox;
789 }
790 
791 bool Wcs::isSameSkySystem(Wcs const& wcs) const {
792  if (_isIcrs() && wcs._isIcrs()) {
793  return true;
794  }
795  return (getCoordSystem() == wcs.getCoordSystem()) && (getEquinox() == wcs.getEquinox());
796 }
797 
798 void Wcs::pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const {
799  assert(_wcsInfo);
800 
801  // wcslib assumes 1-indexed coordinates
802  double pixTmp[2] = {pixel1 - PixelZeroPos + lsstToFitsPixels, pixel2 - PixelZeroPos + lsstToFitsPixels};
803  double imgcrd[2];
804  double phi, theta;
805 
806  double sky[2];
807  int status = 0;
808  status = wcsp2s(_wcsInfo, 1, 2, pixTmp, imgcrd, &phi, &theta, sky, &status);
809  if (status > 0) {
811  (boost::format("Error: wcslib returned a status code of %d at pixel %s, %s: %s") %
812  status % pixel1 % pixel2 % wcs_errmsg[status])
813  .str());
814  }
815  // FIXME -- _wcsInfo.lat, _wcsInfo.lng ?
816  skyTmp[0] = sky[0] * geom::degrees;
817  skyTmp[1] = sky[1] * geom::degrees;
818 }
819 
820 CoordPtr Wcs::pixelToSky(GeomPoint const& pixel) const { return pixelToSky(pixel.getX(), pixel.getY()); }
821 
822 CoordPtr Wcs::pixelToSky(double pixel1, double pixel2) const {
823  assert(_wcsInfo);
824 
825  geom::Angle skyTmp[2];
826  pixelToSkyImpl(pixel1, pixel2, skyTmp);
827  return makeCorrectCoord(skyTmp[0], skyTmp[1]);
828 }
829 
830 void Wcs::pixelToSky(double pixel1, double pixel2, geom::Angle& sky1, geom::Angle& sky2) const {
831  geom::Angle skyTmp[2];
832  // HACK -- we shouldn't need to initialize these -- pixelToSkyImpl() sets them unless an
833  // exception is thrown -- but be safe.
834  skyTmp[0] = 0. * geom::radians;
835  skyTmp[1] = 0. * geom::radians;
836  pixelToSkyImpl(pixel1, pixel2, skyTmp);
837  sky1 = skyTmp[0];
838  sky2 = skyTmp[1];
839 }
840 
842  auto coordSystem = getCoordSystem();
843  if ((coordSystem == coord::ICRS) || (coordSystem == coord::GALACTIC)) {
844  // equinox not relevant
845  if (_skyAxesSwapped) {
846  return coord::makeCoord(coordSystem, sky1, sky0);
847  } else {
848  return coord::makeCoord(coordSystem, sky0, sky1);
849  }
850  } else if ((coordSystem == coord::FK5) || (coordSystem == coord::ECLIPTIC)) {
851  if (_skyAxesSwapped) {
852  return coord::makeCoord(coordSystem, sky1, sky0, _wcsInfo->equinox);
853  } else {
854  return coord::makeCoord(coordSystem, sky0, sky1, _wcsInfo->equinox);
855  }
856  }
857  throw LSST_EXCEPT(
859  (boost::format("Can't create Coord object: Unrecognised coordinate system %s") % coordSystem)
860  .str());
861 }
862 
864  return linearizePixelToSkyInternal(skyToPixel(coord), coord, skyUnit);
865 }
867  return linearizePixelToSkyInternal(pix, *pixelToSky(pix), skyUnit);
868 }
869 
871  geom::AngleUnit skyUnit) const {
872  //
873  // Figure out the (0, 0), (0, 1), and (1, 0) ra/dec coordinates of the corners of a square drawn in pixel
874  // It'd be better to centre the square at sky00, but that would involve another conversion between sky and
875  // pixel coordinates so I didn't bother
876  //
877  const double side = 10; // length of the square's sides in pixels
878  GeomPoint const sky00 = coord.getPosition(skyUnit);
879  typedef std::pair<geom::Angle, geom::Angle> AngleAngle;
880  AngleAngle const dsky10 = coord.getTangentPlaneOffset(*pixelToSky(pix00 + geom::Extent2D(side, 0)));
881  AngleAngle const dsky01 = coord.getTangentPlaneOffset(*pixelToSky(pix00 + geom::Extent2D(0, side)));
882 
883  Eigen::Matrix2d m;
884  m(0, 0) = dsky10.first.asAngularUnits(skyUnit) / side;
885  m(0, 1) = dsky01.first.asAngularUnits(skyUnit) / side;
886  m(1, 0) = dsky10.second.asAngularUnits(skyUnit) / side;
887  m(1, 1) = dsky01.second.asAngularUnits(skyUnit) / side;
888 
889  Eigen::Vector2d sky00v;
890  sky00v << sky00.getX(), sky00.getY();
891  Eigen::Vector2d pix00v;
892  pix00v << pix00.getX(), pix00.getY();
893  // return geom::AffineTransform(m, geom::Extent2D(sky00v - m * pix00v));
894  return geom::AffineTransform(m, (sky00v - m * pix00v));
895 }
896 
898  return linearizeSkyToPixelInternal(skyToPixel(coord), coord, skyUnit);
899 }
900 
902  return linearizeSkyToPixelInternal(pix, *pixelToSky(pix), skyUnit);
903 }
904 
906  geom::AngleUnit skyUnit) const {
907  geom::AffineTransform inverse = linearizePixelToSkyInternal(pix00, coord, skyUnit);
908  return inverse.invert();
909 }
910 
912 
913 // -------- table-based persistence -------------------------------------------------------------------------
914 
916 public:
917  explicit WcsFactory(std::string const& name) : table::io::PersistableFactory(name) {}
918 
919  virtual std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
920  CatalogVector const& catalogs) const;
921 };
922 
923 namespace {
924 
925 // Read-only singleton struct containing the schema and keys that a simple Wcs is mapped
926 // to in record persistence.
927 struct WcsPersistenceHelper {
928  table::Schema schema;
929  table::PointKey<double> crval;
930  table::PointKey<double> crpix;
931  table::Key<table::Array<double> > cd;
932  table::Key<std::string> ctype1;
933  table::Key<std::string> ctype2;
934  table::Key<double> equinox;
935  table::Key<std::string> radesys;
936  table::Key<std::string> cunit1;
937  table::Key<std::string> cunit2;
938 
939  static WcsPersistenceHelper const& get() {
940  static WcsPersistenceHelper instance;
941  return instance;
942  };
943 
944  // No copying
945  WcsPersistenceHelper(const WcsPersistenceHelper&) = delete;
946  WcsPersistenceHelper& operator=(const WcsPersistenceHelper&) = delete;
947 
948  // No moving
949  WcsPersistenceHelper(WcsPersistenceHelper&&) = delete;
950  WcsPersistenceHelper& operator=(WcsPersistenceHelper&&) = delete;
951 
952 private:
953  WcsPersistenceHelper()
954  : schema(),
955  crval(table::PointKey<double>::addFields(schema, "crval", "celestial reference point", "deg")),
956  crpix(table::PointKey<double>::addFields(schema, "crpix", "pixel reference point", "pixel")),
957  cd(schema.addField<table::Array<double> >(
958  "cd", "linear transform matrix, ordered (1_1, 2_1, 1_2, 2_2)", 4)),
959  ctype1(schema.addField<std::string>("ctype1", "coordinate type", 72)),
960  ctype2(schema.addField<std::string>("ctype2", "coordinate type", 72)),
961  equinox(schema.addField<double>("equinox", "equinox of coordinates")),
962  radesys(schema.addField<std::string>("radesys", "coordinate system for equinox", 72)),
963  cunit1(schema.addField<std::string>("cunit1", "coordinate units", 72)),
964  cunit2(schema.addField<std::string>("cunit2", "coordinate units", 72)) {
965  schema.getCitizen().markPersistent();
966  }
967 };
968 
969 std::string getWcsPersistenceName() { return "Wcs"; }
970 
971 WcsFactory registration(getWcsPersistenceName());
972 
973 } // anonymous
974 
975 std::string Wcs::getPersistenceName() const { return getWcsPersistenceName(); }
976 
977 std::string Wcs::getPythonModule() const { return "lsst.afw.image"; }
978 
979 void Wcs::write(OutputArchiveHandle& handle) const {
980  WcsPersistenceHelper const& keys = WcsPersistenceHelper::get();
981  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
983  record->set(keys.crval.getX(), _wcsInfo[0].crval[0]);
984  record->set(keys.crval.getY(), _wcsInfo[0].crval[1]);
985  record->set(keys.crpix, getPixelOrigin());
986  Eigen::Matrix2d cdIn = getCDMatrix();
987  Eigen::Map<Eigen::Matrix2d> cdOut((*record)[keys.cd].getData());
988  cdOut = cdIn;
989  record->set(keys.ctype1, std::string(_wcsInfo[0].ctype[0]));
990  record->set(keys.ctype2, std::string(_wcsInfo[0].ctype[1]));
991  record->set(keys.equinox, _wcsInfo[0].equinox);
992  record->set(keys.radesys, std::string(_wcsInfo[0].radesys));
993  record->set(keys.cunit1, std::string(_wcsInfo[0].cunit[0]));
994  record->set(keys.cunit2, std::string(_wcsInfo[0].cunit[1]));
995  handle.saveCatalog(catalog);
996 }
997 
999  if (_wcsInfo[0].naxis != 2) return false;
1000  if (std::strcmp(_wcsInfo[0].cunit[0], "deg") != 0) return false;
1001  if (std::strcmp(_wcsInfo[0].cunit[1], "deg") != 0) return false;
1002 
1003  return true;
1004 }
1005 
1006 bool Wcs::isPersistable() const {
1007  if (!_mayBePersistable()) {
1008  return false;
1009  }
1010  // The current table persistence only works for TAN and TAN-SIP projections
1011  return false;
1012 }
1013 
1015  : daf::base::Citizen(typeid(this)),
1016  _wcsInfo(NULL),
1017  _nWcsInfo(0),
1018  _relax(0),
1019  _wcsfixCtrl(0),
1020  _wcshdrCtrl(0),
1021  _nReject(0),
1022  _coordSystem(static_cast<afw::coord::CoordSystem>(-1)) // set by _initWcs
1023 {
1024  WcsPersistenceHelper const& keys = WcsPersistenceHelper::get();
1025  if (!record.getSchema().contains(keys.schema)) {
1026  throw LSST_EXCEPT(afw::table::io::MalformedArchiveError, "Incorrect schema for Wcs persistence");
1027  }
1028  _setWcslibParams();
1029  Eigen::Matrix2d cd = Eigen::Map<Eigen::Matrix2d const>(record[keys.cd].getData());
1030  initWcsLib(record.get(keys.crval), record.get(keys.crpix), cd, record.get(keys.ctype1),
1031  record.get(keys.ctype2), record.get(keys.equinox), record.get(keys.radesys),
1032  record.get(keys.cunit1), record.get(keys.cunit2));
1033  _initWcs();
1034 }
1035 
1037  CatalogVector const& catalogs) const {
1038  WcsPersistenceHelper const& keys = WcsPersistenceHelper::get();
1039  LSST_ARCHIVE_ASSERT(catalogs.size() >= 1u);
1040  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
1041  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
1042  std::shared_ptr<Wcs> result(new Wcs(catalogs.front().front()));
1043  return result;
1044 }
1045 
1046 // ----------------------------------------------------------------------------------------------------------
1047 
1048 //
1049 // Mutators
1050 //
1051 
1052 void Wcs::shiftReferencePixel(double dx, double dy) {
1053  assert(_wcsInfo);
1054  _wcsInfo->crpix[0] += dx;
1055  _wcsInfo->crpix[1] += dy;
1056 
1057  // tells libwcs to invalidate cached data, since transformation has been modified
1058  _wcsInfo->flag = 0;
1059 }
1060 
1061 /*
1062  * Now WCSA, pixel coordinates, but allowing for X0 and Y0
1063  */
1064 namespace detail {
1065 
1070  std::string const& wcsName,
1071  int const x0,
1072  int const y0
1073  ) {
1075 
1076  wcsMetaData->set("CRVAL1" + wcsName, x0, "Column pixel of Reference Pixel");
1077  wcsMetaData->set("CRVAL2" + wcsName, y0, "Row pixel of Reference Pixel");
1078  wcsMetaData->set("CRPIX1" + wcsName, 1, "Column Pixel Coordinate of Reference");
1079  wcsMetaData->set("CRPIX2" + wcsName, 1, "Row Pixel Coordinate of Reference");
1080  wcsMetaData->set("CTYPE1" + wcsName, "LINEAR", "Type of projection");
1081  wcsMetaData->set("CTYPE2" + wcsName, "LINEAR", "Type of projection");
1082  wcsMetaData->set("CUNIT1" + wcsName, "PIXEL", "Column unit");
1083  wcsMetaData->set("CUNIT2" + wcsName, "PIXEL", "Row unit");
1084 
1085  return wcsMetaData;
1086 }
1097  int x0 = 0; // Our value of X0
1098  int y0 = 0; // Our value of Y0
1099 
1100  try {
1101  //
1102  // Only use WCS if CRPIX[12] == 1 and CRVAL[12] is present
1103  //
1104  if (metadata->getAsDouble("CRPIX1" + wcsName) == 1 &&
1105  metadata->getAsDouble("CRPIX2" + wcsName) == 1) {
1106  x0 = metadata->getAsInt("CRVAL1" + wcsName);
1107  y0 = metadata->getAsInt("CRVAL2" + wcsName);
1108  //
1109  // OK, we've got it. Remove it from the header
1110  //
1111  metadata->remove("CRVAL1" + wcsName);
1112  metadata->remove("CRVAL2" + wcsName);
1113  metadata->remove("CRPIX1" + wcsName);
1114  metadata->remove("CRPIX2" + wcsName);
1115  metadata->remove("CTYPE1" + wcsName);
1116  metadata->remove("CTYPE1" + wcsName);
1117  metadata->remove("CUNIT1" + wcsName);
1118  metadata->remove("CUNIT2" + wcsName);
1119  }
1120  } catch (pex::exceptions::NotFoundError&) {
1121  ; // OK, not present
1122  }
1123 
1124  return geom::Point2I(x0, y0);
1125 }
1126 
1139  std::shared_ptr<Wcs const> const& wcs) {
1140  std::shared_ptr<daf::base::PropertySet> wcsMetadata = wcs->getFitsMetadata();
1141  std::vector<std::string> paramNames = wcsMetadata->paramNames();
1142  paramNames.push_back("CDELT1");
1143  paramNames.push_back("CDELT2");
1144  paramNames.push_back("LTV1");
1145  paramNames.push_back("LTV2");
1146  paramNames.push_back("PC001001");
1147  paramNames.push_back("PC001002");
1148  paramNames.push_back("PC002001");
1149  paramNames.push_back("PC002002");
1150  for (std::vector<std::string>::const_iterator ptr = paramNames.begin(); ptr != paramNames.end(); ++ptr) {
1151  metadata->remove(*ptr);
1152  }
1153 
1154  return 0; // would be ncard if remove returned a status
1155 }
1156 }
1157 
1158 // -------------------------------------------------------------------------------------------------
1159 //
1160 // XYTransformFromWcsPair
1161 
1163  : XYTransform(), _dst(dst), _src(src), _isSameSkySystem(dst->isSameSkySystem(*src)) {}
1164 
1166  return std::make_shared<XYTransformFromWcsPair>(_dst->clone(), _src->clone());
1167 }
1168 
1170  if (_isSameSkySystem) {
1171  // high performance branch; no coordinate conversion required
1172  afw::geom::Angle sky0, sky1;
1173  _src->pixelToSky(pixel[0], pixel[1], sky0, sky1);
1174  return _dst->skyToPixel(sky0, sky1);
1175  } else {
1176  std::shared_ptr<afw::coord::Coord const> coordPtr{_src->pixelToSky(pixel)};
1177  return _dst->skyToPixel(*coordPtr);
1178  }
1179 }
1180 
1182  if (_isSameSkySystem) {
1183  // high performance branch; no coordinate conversion required
1184  afw::geom::Angle sky0, sky1;
1185  _dst->pixelToSky(pixel[0], pixel[1], sky0, sky1);
1186  return _src->skyToPixel(sky0, sky1);
1187  } else {
1188  std::shared_ptr<afw::coord::Coord const> coordPtr{_dst->pixelToSky(pixel)};
1189  return _src->skyToPixel(*coordPtr);
1190  }
1191 }
1192 
1194  // just swap src, dst
1195  return std::make_shared<XYTransformFromWcsPair>(_src, _dst);
1196 }
1197 }
1198 }
1199 } // end image
double getAsDouble(std::string const &name) const
std::shared_ptr< Coord > makeCoord(CoordSystem const system, lsst::afw::geom::Angle const ra, lsst::afw::geom::Angle const dec, double const epoch)
Factory function to create a Coord of arbitrary type with decimal RA,Dec.
Definition: Coord.cc:840
virtual ~Wcs()
Definition: Wcs.cc:520
void _setWcslibParams()
Definition: Wcs.cc:68
bool operator==(Wcs const &other) const
Definition: Wcs.cc:463
static std::shared_ptr< lsst::daf::base::PropertyList > generatePropertySet(lsst::afw::image::Wcs const &wcs)
virtual bool isPersistable() const
Whether the Wcs is persistable using afw::table::io archives.
Definition: Wcs.cc:1006
bool _isIcrs() const
Definition: Wcs.h:371
geom::Angle pixelScale() const
Returns the pixel scale [Angle/pixel].
Definition: Wcs.cc:692
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
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add a pair of _x, _y fields to a Schema, and return a PointKey that points to them.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::coord::CoordSystem getCoordSystem() const
Definition: Wcs.h:229
WcsFactory(std::string const &name)
Definition: Wcs.cc:917
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 std::shared_ptr< afw::geom::XYTransform > invert() const
returns a "deep inverse" in this sense that the forward+inverse transforms do not share state default...
Definition: Wcs.cc:1193
An object passed to Persistable::write to allow it to persist itself.
virtual void remove(std::string const &name)
T front(T... args)
geom::Point2D skyToPixel(geom::Angle sky1, geom::Angle sky2) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:740
A custom container class for records, based on std::vector.
Definition: Catalog.h:97
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
bool _mayBePersistable() const
Perform basic checks on whether *this might be persistable.
Definition: Wcs.cc:998
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:215
std::string formatFitsProperties(lsst::daf::base::PropertySet const &prop, std::set< std::string > const &excludeNames={})
Format a PropertySet into a single FITS header string.
Definition: Utils.cc:267
int getAsInt(std::string const &name) const
int stripWcsKeywords(std::shared_ptr< lsst::daf::base::PropertySet > const &metadata, std::shared_ptr< Wcs const > const &wcs)
Definition: Wcs.cc:1138
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
T calloc(T... args)
T endl(T... args)
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
Wcs & operator=(const Wcs &)
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 Point2D forwardTransform(Point2D const &pixel) const
virtuals for forward and reverse transforms
Definition: Wcs.cc:1169
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
lsst::afw::geom::Point2D GeomPoint
Definition: Wcs.cc:57
table::PointKey< double > crval
Definition: Wcs.cc:929
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
STL namespace.
geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
table::Key< std::string > radesys
Definition: Wcs.cc:935
T end(T... args)
std::pair< lsst::afw::geom::Angle, lsst::afw::geom::Angle > getTangentPlaneOffset(Coord const &c) const
Get the offset on the tangent plane.
Definition: Coord.cc:537
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:104
friend class WcsFactory
Definition: Wcs.h:359
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56
table::Key< std::string > ctype2
Definition: Wcs.cc:933
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:123
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
Rotation angle is unknown.
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
lsst::afw::geom::Point2D getPosition(lsst::afw::geom::AngleUnit unit=lsst::afw::geom::degrees) const
Return our contents in a Point2D object.
Definition: Coord.cc:340
STL class.
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:21
A 2D linear coordinate transformation.
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
bool isFlipped() const
Does the Wcs follow the convention of North=Up, East=Left?
Definition: Wcs.cc:643
T push_back(T... args)
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
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
A class representing an angle.
Definition: Angle.h:102
T strncpy(T... args)
A base class for image defects.
std::shared_ptr< coord::Coord > pixelToSky(double pix1, double pix2) const
Convert from pixel position to sky coordinates (e.g.
Definition: Wcs.cc:822
virtual bool _isSubset(Wcs const &other) const
Definition: Wcs.cc:499
T strlen(T... args)
geom::LinearTransform getLinearTransform() const
Return the linear part of the Wcs, the CD matrix in FITS-speak, as an AffineTransform.
Definition: Wcs.cc:911
Point< double, 2 > Point2D
Definition: Point.h:273
std::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0=0, int const y0=0)
Definition: Wcs.cc:1069
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:116
An affine coordinate transformation consisting of a linear transformation and an offset.
table::Key< double > equinox
Definition: Wcs.cc:934
T strcmp(T... args)
const int fitsToLsstPixels
Definition: Wcs.cc:78
#define CHECK_NULLS(a, b)
Definition: Wcs.cc:488
virtual void shiftReferencePixel(double dx, double dy)
Move the pixel reference position by (dx, dy)
Definition: Wcs.cc:1052
const int STRLEN
Definition: Wcs.cc:61
Wcs()
Construct an invalid Wcs given no arguments.
Definition: Wcs.cc:84
table::Key< int > b
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:279
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:52
lsst::afw::geom::Point2D getPixelOrigin() const
Returns CRPIX (corrected to LSST convention).
Definition: Wcs.cc:537
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
void _initWcs()
Set some internal variables that we need to refer to.
Definition: Wcs.cc:114
double x
geom::Point2D skyToIntermediateWorldCoord(coord::Coord const &coord) const
Convert from sky coordinates (e.g.
Definition: Wcs.cc:742
std::shared_ptr< lsst::afw::coord::Coord > getSkyOrigin() const
Returns CRVAL. This need not be the centre of the image.
Definition: Wcs.cc:532
virtual Point2D reverseTransform(Point2D const &pixel) const
Definition: Wcs.cc:1181
virtual void rotateImageBy90(int nQuarter, lsst::afw::geom::Extent2I dimensions) const
Rotate image by nQuarter times 90 degrees.
Definition: Wcs.cc:586
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Schema schema
Definition: Wcs.cc:928
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
std::shared_ptr< Coord > convert(CoordSystem system, double epoch=2000) const
Convert to a specified Coord type at a specified epoch.
Definition: Coord.cc:465
T size(T... args)
XYTransformFromWcsPair(std::shared_ptr< Wcs const > dst, std::shared_ptr< Wcs const > src)
Definition: Wcs.cc:1162
bool _skyAxesSwapped
if true then the sky axes are swapped
Definition: Wcs.h:451
#define LSST_EXCEPT(type,...)
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
T malloc(T... args)
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
Citizen(const std::type_info &)
T begin(T... args)
virtual void pixelToSkyImpl(double pixel1, double pixel2, geom::Angle skyTmp[2]) const
Worker routine for pixelToSky.
Definition: Wcs.cc:798
virtual std::shared_ptr< afw::geom::XYTransform > clone() const
The following methods are needed to devirtualize the XYTransform parent class.
Definition: Wcs.cc:1165
std::shared_ptr< lsst::afw::coord::Coord > CoordPtr
Definition: Wcs.cc:58
T c_str(T... args)
std::shared_ptr< Wcs const > _dst
Definition: Wcs.h:519
virtual geom::Point2D skyToPixelImpl(geom::Angle sky1, geom::Angle sky2) const
Worker routine for skyToPixel.
Definition: Wcs.cc:694
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:447
T substr(T... args)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Wcs.cc:979
table::Key< std::string > cunit1
Definition: Wcs.cc:936
CoordSystem makeCoordEnum(std::string const system)
A utility function to get the enum value of a coordinate system from a string name.
Definition: Coord.cc:112
table::Key< int > a
table::PointKey< int > pixel
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
table::Key< table::Array< double > > cd
Definition: Wcs.cc:931
virtual std::shared_ptr< table::io::Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const
Construct a new object from the given InputArchive and vector of catalogs.
Definition: Wcs.cc:1036
double getEquinox() const
Definition: Wcs.cc:784
const int lsstToFitsPixels
Definition: Wcs.cc:77
T quiet_NaN(T... args)
T sqrt(T... args)
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Point< int, 2 > Point2I
Definition: Point.h:270
m
Eigen::Matrix2d getCDMatrix() const
Returns the CD matrix.
Definition: Wcs.cc:545
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:142
This is the base class for spherical coordinates.
Definition: Coord.h:63
const double PixelZeroPos
position of center of pixel 0
Definition: ImageUtils.h:44
virtual std::shared_ptr< Wcs > clone(void) const
Definition: Wcs.cc:526
T isspace(T... args)
AffineTransform const invert() const
Return the inverse transform.
table::Key< std::string > ctype1
Definition: Wcs.cc:932
std::shared_ptr< Wcs const > _src
Definition: Wcs.h:520
struct wcsprm * _wcsInfo
Definition: Wcs.h:444
coord::CoordSystem _coordSystem
Definition: Wcs.h:450
virtual void flipImage(int flipLR, int flipTB, lsst::afw::geom::Extent2I dimensions) const
Flip CD matrix around the y-axis.
Definition: Wcs.cc:563
An exception thrown when an InputArchive&#39;s contents do not make sense.
Definition: Persistable.h:39
table::Key< std::string > cunit2
Definition: Wcs.cc:937
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:466
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Wcs.cc:975
table::PointKey< double > crpix
Definition: Wcs.cc:930