lsst.afw  13.0-94-ga1c444047
TanWcsFormatter.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 /*
26  * Implementation of TanWcsFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <string>
35 
36 //#include "boost/serialization/shared_ptr.hpp"
37 #include <boost/archive/binary_iarchive.hpp>
38 #include <boost/archive/binary_oarchive.hpp>
39 #include <boost/archive/text_iarchive.hpp>
40 #include <boost/archive/text_oarchive.hpp>
41 
42 #include "wcslib/wcs.h"
43 
44 #include "lsst/daf/base.h"
45 #include "lsst/daf/persistence.h"
46 #include "lsst/daf/persistence/PropertySetFormatter.h"
47 #include "lsst/pex/exceptions.h"
48 #include "lsst/log/Log.h"
52 #include "lsst/afw/image/TanWcs.h"
53 #include "lsst/afw/fits.h"
54 
55 namespace {
56 LOG_LOGGER _log = LOG_GET("afw.TanWcsFormatter");
57 }
58 
59 namespace lsst {
60 namespace afw {
61 namespace formatters {
62 
63 namespace dafBase = lsst::daf::base;
65 namespace pexPolicy = lsst::pex::policy;
66 namespace pexExcept = lsst::pex::exceptions;
67 
68 dafPersist::FormatterRegistration TanWcsFormatter::registration("TanWcs", typeid(image::TanWcs),
69  createInstance);
70 
71 TanWcsFormatter::TanWcsFormatter(std::shared_ptr<pexPolicy::Policy>) : dafPersist::Formatter(typeid(this)) {}
72 
73 TanWcsFormatter::~TanWcsFormatter(void) {}
74 
75 void TanWcsFormatter::write(dafBase::Persistable const* persistable,
76  std::shared_ptr<dafPersist::FormatterStorage> storage,
77  std::shared_ptr<dafBase::PropertySet>) {
78  LOGL_DEBUG(_log, "TamWcsFormatter write start");
79  image::TanWcs const* ip = dynamic_cast<image::TanWcs const*>(persistable);
80  if (ip == 0) {
81  throw LSST_EXCEPT(pexExcept::RuntimeError, "Persisting non-TanWcs");
82  }
83  // TODO: Replace this with something better in DM-10776
84  auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
85  if (boost) {
86  LOGL_DEBUG(_log, "TanWcsFormatter write BoostStorage");
87  boost->getOArchive() & *ip;
88  LOGL_DEBUG(_log, "TanWcsFormatter write end");
89  return;
90  }
91  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for TanWcs");
92 }
93 
94 dafBase::Persistable* TanWcsFormatter::read(std::shared_ptr<dafPersist::FormatterStorage> storage,
95  std::shared_ptr<dafBase::PropertySet> additionalData) {
96  LOGL_DEBUG(_log, "TanWcsFormatter read start");
97  // TODO: Replace this with something better in DM-10776
98  auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
99  if (boost) {
100  image::TanWcs* ip = new image::TanWcs;
101  LOGL_DEBUG(_log, "TanWcsFormatter read BoostStorage");
102  boost->getIArchive() & *ip;
103  LOGL_DEBUG(_log, "TanWcsFormatter read end");
104  return ip;
105  }
106  auto fits = std::dynamic_pointer_cast<dafPersist::FitsStorage>(storage);
107  if (fits) {
108  LOGL_DEBUG(_log, "TanWcsFormatter read FitsStorage");
109  int hdu = additionalData->get<int>("hdu", INT_MIN);
110  std::shared_ptr<dafBase::PropertySet> md = afw::fits::readMetadata(fits->getPath(), hdu);
111  image::TanWcs* ip = new image::TanWcs(md);
112  LOGL_DEBUG(_log, "TanWcsFormatter read end");
113  return ip;
114  }
115  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for TanWcs");
116 }
117 
118 void TanWcsFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
119  std::shared_ptr<dafBase::PropertySet>) {
120  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for TanWcs");
121 }
122 
124 template <class Archive>
125 void serializeEigenArray(Archive& ar, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& m) {
126  int rows = m.rows();
127  int cols = m.cols();
128  ar& rows& cols;
129  if (Archive::is_loading::value) {
130  m = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(rows, cols);
131  }
132  for (int j = 0; j < m.cols(); ++j) {
133  for (int i = 0; i < m.rows(); ++i) {
134  ar& m(i, j);
135  }
136  }
137 }
138 
139 static void encodeSipHeader(std::shared_ptr<daf::base::PropertySet> wcsProps,
140  std::string const& which,
141  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> const& m) {
142  int order = m.rows();
143  if (m.cols() != order) {
144  throw LSST_EXCEPT(pexExcept::DomainError, "sip" + which + " matrix is not square");
145  }
146  if (order > 0) {
147  order -= 1; // match SIP convention
148  wcsProps->add(which + "_ORDER", static_cast<int>(order));
149  for (int i = 0; i <= order; ++i) {
150  for (int j = 0; j <= order; ++j) {
151  double val = m(i, j);
152  if (val != 0.0) {
153  wcsProps->add((boost::format("%1%_%2%_%3%") % which % i % j).str(), val);
154  }
155  }
156  }
157  }
158 }
159 
160 std::shared_ptr<dafBase::PropertyList> TanWcsFormatter::generatePropertySet(image::TanWcs const& wcs) {
161  // Only generates properties for the first wcsInfo.
162  std::shared_ptr<dafBase::PropertyList> wcsProps(new dafBase::PropertyList());
163 
164  if (wcs._wcsInfo == NULL) { // nothing to add
165  return wcsProps;
166  }
167 
168  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
169  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
170  // dealing with ICRS, and may confuse readers. Don't write it.
171  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
172  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
173  }
174  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
175  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
176  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
177  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
178  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
179  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
180  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
181  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
182  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
183  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
184  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
185 
186  // Hack. Because wcslib4.3 gets confused when it's passed RA---TAN-SIP,
187  // we set the value of ctypes to just RA---TAN, regardless of whether
188  // the SIP types are present. But when we persist to a file, we need to
189  // check whether the SIP polynomials were actually there and correct
190  // ctypes if necessary. Bad things will happen if someone tries to
191  // use a system other than RA---TAN and DEC--TAN
192  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
193  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
194 
195  if (wcs._hasDistortion) {
196  if (ctype1.rfind("-SIP") == std::string::npos) {
197  ctype1 += "-SIP";
198  }
199  if (ctype2.rfind("-SIP") == std::string::npos) {
200  ctype2 += "-SIP";
201  }
202  encodeSipHeader(wcsProps, "A", wcs._sipA);
203  encodeSipHeader(wcsProps, "B", wcs._sipB);
204  encodeSipHeader(wcsProps, "AP", wcs._sipAp);
205  encodeSipHeader(wcsProps, "BP", wcs._sipBp);
206  }
207  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
208  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
209 
210  return wcsProps;
211 }
212 
213 template <class Archive>
214 void TanWcsFormatter::delegateSerialize(Archive& ar, int const, dafBase::Persistable* persistable) {
215  LOGL_DEBUG(_log, "TanWcsFormatter delegateSerialize start");
216  image::TanWcs* ip = dynamic_cast<image::TanWcs*>(persistable);
217  if (ip == 0) {
218  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-TanWcs");
219  }
220 
221  // Serialize most fields normally
222  ar & ip->_nWcsInfo & ip->_relax;
223  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
224  ar & ip->_coordSystem;
225 
226  ar & ip->_hasDistortion;
227 
228  if (ip->_hasDistortion) {
229  serializeEigenArray(ar, ip->_sipA);
230  serializeEigenArray(ar, ip->_sipAp);
231  serializeEigenArray(ar, ip->_sipB);
232  serializeEigenArray(ar, ip->_sipBp);
233  }
234 
235  // If we are loading, create the array of Wcs parameter structs
236  if (Archive::is_loading::value) {
237  ip->_wcsInfo = reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
238  }
239 
240  for (int i = 0; i < ip->_nWcsInfo; ++i) {
241  // If we are loading, initialize the struct first
242  if (Archive::is_loading::value) {
243  ip->_wcsInfo[i].flag = -1;
244  wcsini(1, 2, &(ip->_wcsInfo[i]));
245  }
246 
247  // Serialize only critical Wcs parameters
248  ar & ip->_wcsInfo[i].naxis;
249  ar & ip->_wcsInfo[i].equinox;
250  ar & ip->_wcsInfo[i].radesys;
251  ar & ip->_wcsInfo[i].crpix[0];
252  ar & ip->_wcsInfo[i].crpix[1];
253  ar & ip->_wcsInfo[i].cd[0];
254  ar & ip->_wcsInfo[i].cd[1];
255  ar & ip->_wcsInfo[i].cd[2];
256  ar & ip->_wcsInfo[i].cd[3];
257  ar & ip->_wcsInfo[i].crval[0];
258  ar & ip->_wcsInfo[i].crval[1];
259  ar & ip->_wcsInfo[i].cunit[0];
260  ar & ip->_wcsInfo[i].cunit[1];
261  ar & ip->_wcsInfo[i].ctype[0];
262  ar & ip->_wcsInfo[i].ctype[1];
263  ar & ip->_wcsInfo[i].altlin;
264 
265  // If we are loading, compute intermediate values given those above
266  if (Archive::is_loading::value) {
267  ip->_wcsInfo[i].flag = 0;
268  wcsset(&(ip->_wcsInfo[i]));
269  }
270  }
271  LOGL_DEBUG(_log, "TanWcsFormatter delegateSerialize end");
272 }
273 
274 // Explicit template specializations confuse Doxygen, tell it to ignore them
276 template void TanWcsFormatter::delegateSerialize(boost::archive::text_oarchive&, int, dafBase::Persistable*);
277 template void TanWcsFormatter::delegateSerialize(boost::archive::text_iarchive&, int, dafBase::Persistable*);
278 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_oarchive&, int,
279  dafBase::Persistable*);
280 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_iarchive&, int,
281  dafBase::Persistable*);
283 
284 std::shared_ptr<dafPersist::Formatter> TanWcsFormatter::createInstance(
285  std::shared_ptr<pexPolicy::Policy> policy) {
286  return std::shared_ptr<dafPersist::Formatter>(new TanWcsFormatter(policy));
287 }
288 }
289 }
290 } // end lsst::afw::formatters
table::Key< std::string > ctype2
Definition: Wcs.cc:933
Definition: Span.h:37
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Read FITS header.
Definition: Utils.h:64
A base class for image defects.
Definition: cameraGeom.dox:3
Implementation of the WCS standard for the special case of the Gnomonic (tangent plane) projection...
Definition: TanWcs.h:68
#define __attribute__(x)
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
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:447
void serializeEigenArray(Archive &ar, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &m)
table::Key< std::string > ctype1
Definition: Wcs.cc:932
struct wcsprm * _wcsInfo
Definition: Wcs.h:444
coord::CoordSystem _coordSystem
Definition: Wcs.h:450
Class implementing persistence and retrieval for TanWcs objects.
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56