LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
makeWcs.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 <memory>
26 #include "Eigen/Core"
27 #include "lsst/log/Log.h"
28 #include "lsst/afw/image/Wcs.h"
29 #include "lsst/afw/image/TanWcs.h"
30 
31 namespace except = lsst::pex::exceptions;
32 namespace afwImg = lsst::afw::image;
33 
42  bool stripMetadata
43  )
44 {
45  //
46  // _metadata is not const (it is probably meant to be), but we don't want to modify it.
47  //
48  auto metadata = _metadata; // we'll make a copy and modify metadata if needs be
49  auto modifyable = false; // ... and set this variable to say that we did
50 
51  std::string ctype1, ctype2;
52  if (metadata->exists("CTYPE1") && metadata->exists("CTYPE2")) {
53  ctype1 = metadata->getAsString("CTYPE1");
54  ctype2 = metadata->getAsString("CTYPE2");
55  } else {
56  return PTR(Wcs)();
57  }
58  //
59  // SCAMP used to use PVi_j keys with a CTYPE of TAN to specify a "TPV" projection
60  // (cf. https://github.com/astropy/astropy/issues/299
61  // and the discussion from Dave Berry in https://jira.lsstcorp.org/browse/DM-2883)
62  //
63  // Follow Dave's AST and switch TAN to TPV
64  //
65  if (ctype1.substr(5, 3) == "TAN" &&
66  (metadata->exists("PV1_5") || metadata->exists("PV2_1"))) {
67  LOGL_INFO("makeWcs", "Interpreting %s/%s + PVi_j as TPV", ctype1.c_str(), ctype2.c_str());
68 
69  if (!modifyable) {
70  metadata = _metadata->deepCopy();
71  modifyable = true;
72  }
73 
74  ctype1.replace(5, 3, "TPV");
75  metadata->set<std::string>("CTYPE1", ctype1);
76 
77  ctype2.replace(5, 3, "TPV");
78  metadata->set<std::string>("CTYPE2", ctype2);
79  }
80 
81  afwImg::Wcs::Ptr wcs; // we can't use make_shared as ctor is private
82  if (ctype1.substr(5, 3) == "TAN") {
83  wcs = afwImg::Wcs::Ptr(new afwImg::TanWcs(metadata));
84  } else if (ctype1.substr(5, 3) == "TPV") { // unfortunately we don't support TPV
85  if (!modifyable) {
86  metadata = _metadata->deepCopy();
87  modifyable = true;
88  }
89 
90  LOGL_WARN("makeWcs", "Stripping PVi_j keys from projection %s/%s", ctype1.c_str(), ctype2.c_str());
91 
92  metadata->set<std::string>("CTYPE1", "RA---TAN");
93  metadata->set<std::string>("CTYPE2", "DEC--TAN");
94  metadata->set<bool>("TPV_WCS", true);
95  // PV1_[1-4] are in principle legal, although Swarp reuses them as part of the TPV parameterisation.
96  // It turns out that leaving PV1_[1-4] in the header breaks wcslib 4.14's ability to read
97  // DECam headers (DM-3196) so we'll delete them all for now.
98  //
99  // John Swinbank points out the maximum value of j in TVi_j is 39;
100  // http://fits.gsfc.nasa.gov/registry/tpvwcs/tpv.html
101  for (int i = 0; i != 2; ++i) {
102  for (int j = 1; j <= 39; ++j) { // 39's the max in the TPV standard
103  char pvName[8];
104  sprintf(pvName, "PV%d_%d", i, j);
105  if (metadata->exists(pvName)) {
106  metadata->remove(pvName);
107  }
108  }
109  }
110 
111  wcs = afwImg::Wcs::Ptr(new afwImg::TanWcs(metadata));
112  } else {
113  wcs = afwImg::Wcs::Ptr(new afwImg::Wcs(metadata));
114  }
115 
116  //If keywords LTV[1,2] are present, the image on disk is already a subimage, so
117  //we should shift the wcs to allow for this.
118  std::string key = "LTV1";
119  if (metadata->exists(key)) {
120  wcs->shiftReferencePixel(-metadata->getAsDouble(key), 0);
121  }
122 
123  key = "LTV2";
124  if (metadata->exists(key) ) {
125  wcs->shiftReferencePixel(0, -metadata->getAsDouble(key));
126  }
127 
128  if (stripMetadata) {
129  afwImg::detail::stripWcsKeywords(metadata, wcs);
130  }
131 
132  return wcs;
133 }
134 
141  double CD11,
142  double CD12,
143  double CD21,
144  double CD22
145  ) {
146  Eigen::Matrix2d CD;
147  CD << CD11, CD12, CD21, CD22;
148  lsst::afw::geom::Point2D crvalTmp;
149  crvalTmp[0] = crval.toIcrs().getLongitude().asDegrees();
150  crvalTmp[1] = crval.toIcrs().getLatitude().asDegrees();
151  return afwImg::Wcs::Ptr(new lsst::afw::image::TanWcs(crvalTmp, crpix, CD));
152 }
lsst::afw::geom::Angle getLatitude() const
The main access method for the latitudinal coordinate.
Definition: Coord.h:119
table::PointKey< double > crval
Definition: Wcs.cc:1030
tbl::Key< int > wcs
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
table::Key< std::string > ctype2
Definition: Wcs.cc:1034
double asDegrees() const
Definition: Angle.h:123
lsst::afw::geom::Angle getLongitude() const
The main access method for the longitudinal coordinate.
Definition: Coord.h:112
std::shared_ptr< Wcs > Ptr
Definition: Wcs.h:113
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
boost::shared_ptr< Wcs > makeWcs(boost::shared_ptr< lsst::daf::base::PropertySet > const &fitsMetadata, bool stripMetadata=false)
Definition: makeWcs.cc:40
Implementation of the WCS standard for the special case of the Gnomonic (tangent plane) projection...
Definition: TanWcs.h:68
#define LOGL_WARN(logger, message...)
Definition: Log.h:545
virtual IcrsCoord toIcrs() const
Convert ourself to ICRS: RA, Dec (basically J2000)
Definition: Coord.cc:801
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Definition: PropertySet.cc:70
#define PTR(...)
Definition: base.h:41
int stripWcsKeywords(boost::shared_ptr< lsst::daf::base::PropertySet > const &metadata, boost::shared_ptr< Wcs const > const &wcs)
Definition: Wcs.cc:1256
daf::base::PropertySet & _metadata
Definition: fits_io_mpl.h:79
table::Key< std::string > ctype1
Definition: Wcs.cc:1033
#define LOGL_INFO(logger, message...)
Definition: Log.h:529
table::PointKey< double > crpix
Definition: Wcs.cc:1031