lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
64 namespace dafPersist = lsst::daf::persistence;
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::Storage> 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  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
84  LOGL_DEBUG(_log, "TanWcsFormatter write BoostStorage");
85  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
86  boost->getOArchive() & *ip;
87  LOGL_DEBUG(_log, "TanWcsFormatter write end");
88  return;
89  }
90  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for TanWcs");
91 }
92 
93 dafBase::Persistable* TanWcsFormatter::read(std::shared_ptr<dafPersist::Storage> storage,
94  std::shared_ptr<dafBase::PropertySet> additionalData) {
95  LOGL_DEBUG(_log, "TanWcsFormatter read start");
96  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
97  image::TanWcs* ip = new image::TanWcs;
98  LOGL_DEBUG(_log, "TanWcsFormatter read BoostStorage");
99  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
100  boost->getIArchive() & *ip;
101  LOGL_DEBUG(_log, "TanWcsFormatter read end");
102  return ip;
103  } else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
104  LOGL_DEBUG(_log, "TanWcsFormatter read FitsStorage");
105  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
106  int hdu = additionalData->get<int>("hdu", INT_MIN);
107  std::shared_ptr<dafBase::PropertySet> md = afw::fits::readMetadata(fits->getPath(), hdu);
108  image::TanWcs* ip = new image::TanWcs(md);
109  LOGL_DEBUG(_log, "TanWcsFormatter read end");
110  return ip;
111  }
112  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for TanWcs");
113 }
114 
115 void TanWcsFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::Storage>,
116  std::shared_ptr<dafBase::PropertySet>) {
117  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for TanWcs");
118 }
119 
121 template <class Archive>
122 void serializeEigenArray(Archive& ar, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& m) {
123  int rows = m.rows();
124  int cols = m.cols();
125  ar& rows& cols;
126  if (Archive::is_loading::value) {
127  m = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(rows, cols);
128  }
129  for (int j = 0; j < m.cols(); ++j) {
130  for (int i = 0; i < m.rows(); ++i) {
131  ar& m(i, j);
132  }
133  }
134 }
135 
136 static void encodeSipHeader(std::shared_ptr<daf::base::PropertySet> wcsProps,
137  std::string const& which,
138  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> const& m) {
139  int order = m.rows();
140  if (m.cols() != order) {
141  throw LSST_EXCEPT(pexExcept::DomainError, "sip" + which + " matrix is not square");
142  }
143  if (order > 0) {
144  order -= 1; // match SIP convention
145  wcsProps->add(which + "_ORDER", static_cast<int>(order));
146  for (int i = 0; i <= order; ++i) {
147  for (int j = 0; j <= order; ++j) {
148  double val = m(i, j);
149  if (val != 0.0) {
150  wcsProps->add((boost::format("%1%_%2%_%3%") % which % i % j).str(), val);
151  }
152  }
153  }
154  }
155 }
156 
157 std::shared_ptr<dafBase::PropertyList> TanWcsFormatter::generatePropertySet(image::TanWcs const& wcs) {
158  // Only generates properties for the first wcsInfo.
159  std::shared_ptr<dafBase::PropertyList> wcsProps(new dafBase::PropertyList());
160 
161  if (wcs._wcsInfo == NULL) { // nothing to add
162  return wcsProps;
163  }
164 
165  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
166  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
167  // dealing with ICRS, and may confuse readers. Don't write it.
168  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
169  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
170  }
171  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
172  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
173  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
174  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
175  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
176  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
177  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
178  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
179  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
180  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
181  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
182 
183  // Hack. Because wcslib4.3 gets confused when it's passed RA---TAN-SIP,
184  // we set the value of ctypes to just RA---TAN, regardless of whether
185  // the SIP types are present. But when we persist to a file, we need to
186  // check whether the SIP polynomials were actually there and correct
187  // ctypes if necessary. Bad things will happen if someone tries to
188  // use a system other than RA---TAN and DEC--TAN
189  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
190  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
191 
192  if (wcs._hasDistortion) {
193  if (ctype1.rfind("-SIP") == std::string::npos) {
194  ctype1 += "-SIP";
195  }
196  if (ctype2.rfind("-SIP") == std::string::npos) {
197  ctype2 += "-SIP";
198  }
199  encodeSipHeader(wcsProps, "A", wcs._sipA);
200  encodeSipHeader(wcsProps, "B", wcs._sipB);
201  encodeSipHeader(wcsProps, "AP", wcs._sipAp);
202  encodeSipHeader(wcsProps, "BP", wcs._sipBp);
203  }
204  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
205  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
206 
207  return wcsProps;
208 }
209 
210 template <class Archive>
211 void TanWcsFormatter::delegateSerialize(Archive& ar, int const, dafBase::Persistable* persistable) {
212  LOGL_DEBUG(_log, "TanWcsFormatter delegateSerialize start");
213  image::TanWcs* ip = dynamic_cast<image::TanWcs*>(persistable);
214  if (ip == 0) {
215  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-TanWcs");
216  }
217 
218  // Serialize most fields normally
219  ar & ip->_nWcsInfo & ip->_relax;
220  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
221  ar & ip->_coordSystem;
222 
223  ar & ip->_hasDistortion;
224 
225  if (ip->_hasDistortion) {
226  serializeEigenArray(ar, ip->_sipA);
227  serializeEigenArray(ar, ip->_sipAp);
228  serializeEigenArray(ar, ip->_sipB);
229  serializeEigenArray(ar, ip->_sipBp);
230  }
231 
232  // If we are loading, create the array of Wcs parameter structs
233  if (Archive::is_loading::value) {
234  ip->_wcsInfo = reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
235  }
236 
237  for (int i = 0; i < ip->_nWcsInfo; ++i) {
238  // If we are loading, initialize the struct first
239  if (Archive::is_loading::value) {
240  ip->_wcsInfo[i].flag = -1;
241  wcsini(1, 2, &(ip->_wcsInfo[i]));
242  }
243 
244  // Serialize only critical Wcs parameters
245  ar & ip->_wcsInfo[i].naxis;
246  ar & ip->_wcsInfo[i].equinox;
247  ar & ip->_wcsInfo[i].radesys;
248  ar & ip->_wcsInfo[i].crpix[0];
249  ar & ip->_wcsInfo[i].crpix[1];
250  ar & ip->_wcsInfo[i].cd[0];
251  ar & ip->_wcsInfo[i].cd[1];
252  ar & ip->_wcsInfo[i].cd[2];
253  ar & ip->_wcsInfo[i].cd[3];
254  ar & ip->_wcsInfo[i].crval[0];
255  ar & ip->_wcsInfo[i].crval[1];
256  ar & ip->_wcsInfo[i].cunit[0];
257  ar & ip->_wcsInfo[i].cunit[1];
258  ar & ip->_wcsInfo[i].ctype[0];
259  ar & ip->_wcsInfo[i].ctype[1];
260  ar & ip->_wcsInfo[i].altlin;
261 
262  // If we are loading, compute intermediate values given those above
263  if (Archive::is_loading::value) {
264  ip->_wcsInfo[i].flag = 0;
265  wcsset(&(ip->_wcsInfo[i]));
266  }
267  }
268  LOGL_DEBUG(_log, "TanWcsFormatter delegateSerialize end");
269 }
270 
271 // Explicit template specializations confuse Doxygen, tell it to ignore them
273 template void TanWcsFormatter::delegateSerialize(boost::archive::text_oarchive&, int, dafBase::Persistable*);
274 template void TanWcsFormatter::delegateSerialize(boost::archive::text_iarchive&, int, dafBase::Persistable*);
275 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_oarchive&, int,
276  dafBase::Persistable*);
277 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_iarchive&, int,
278  dafBase::Persistable*);
280 
281 std::shared_ptr<dafPersist::Formatter> TanWcsFormatter::createInstance(
282  std::shared_ptr<pexPolicy::Policy> policy) {
283  return std::shared_ptr<dafPersist::Formatter>(new TanWcsFormatter(policy));
284 }
285 }
286 }
287 } // end lsst::afw::formatters
table::Key< std::string > ctype2
Definition: Wcs.cc:933
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
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