lsst.afw  14.0-25-g0e8fc950e+7
WcsFormatter.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 WcsFormatter 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 // not used? #include <stdlib.h>
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 "boost/serialization/shared_ptr.hpp"
43 #include "wcslib/wcs.h"
44 
45 #include "lsst/daf/base.h"
46 #include "lsst/daf/persistence.h"
48 #include "lsst/log/Log.h"
49 #include "lsst/pex/exceptions.h"
53 #include "lsst/afw/image/Wcs.h"
54 #include "lsst/afw/fits.h"
55 
56 namespace {
57 LOG_LOGGER _log = LOG_GET("afw.WcsFormatter");
58 }
59 
60 namespace lsst {
61 namespace afw {
62 namespace formatters {
63 
64 namespace dafBase = lsst::daf::base;
66 namespace pexPolicy = lsst::pex::policy;
68 
69 dafPersist::FormatterRegistration WcsFormatter::registration("Wcs", typeid(image::Wcs), createInstance);
70 
71 WcsFormatter::WcsFormatter(std::shared_ptr<pexPolicy::Policy>) : dafPersist::Formatter(typeid(this)) {}
72 
74 
78  LOGL_DEBUG(_log, "WcsFormatter write start");
79  image::Wcs const* ip = dynamic_cast<image::Wcs const*>(persistable);
80  if (ip == 0) {
81  throw LSST_EXCEPT(pexExcept::RuntimeError, "Persisting non-Wcs");
82  }
83  // TODO: Replace this with something better in DM-10776
85  if (boost) {
86  LOGL_DEBUG(_log, "WcsFormatter write BoostStorage");
87  boost->getOArchive() & *ip;
88  LOGL_DEBUG(_log, "WcsFormatter write end");
89  return;
90  }
91  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for Wcs");
92 }
93 
96  LOGL_DEBUG(_log, "WcsFormatter read start");
97  // TODO: Replace this with something better in DM-10776
99  if (boost) {
100  image::Wcs* ip = new image::Wcs;
101  LOGL_DEBUG(_log, "WcsFormatter read BoostStorage");
102  boost->getIArchive() & *ip;
103  LOGL_DEBUG(_log, "WcsFormatter read end");
104  return ip;
105  }
107  if (fits) {
108  LOGL_DEBUG(_log, "WcsFormatter read FitsStorage");
109  int hdu = additionalData->get<int>("hdu", INT_MIN);
111  image::Wcs* ip = new image::Wcs(md);
112  LOGL_DEBUG(_log, "WcsFormatter read end");
113  return ip;
114  }
115  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for Wcs");
116 }
117 
120  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for Wcs");
121 }
122 
124  // Only generates properties for the first wcsInfo.
126 
127  assert(wcs._wcsInfo); // default ctor is private, so an uninitialized Wcs should not exist in the wild
128 
129  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
130  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
131  // dealing with ICRS, and may confuse readers. Don't write it.
132  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
133  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
134  }
135  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
136  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
137  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
138  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
139  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
140  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
141  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
142  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
143  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
144  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
145  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
146  //
147  // Some projections need PVi_j keywords. Add them.
148  //
149  for (int i = 0; i != wcs._wcsInfo[0].npv; ++i) {
150  auto const pv = wcs._wcsInfo[0].pv[i];
151  int const ii = pv.i > 0 ? pv.i : (wcs._wcsInfo[0].lat + 1); // 0 => latitude axis (see wcslib/wsc.h)
152  char key[20];
153  sprintf(key, "PV%d_%d", ii, pv.m);
154  wcsProps->add(key, pv.value);
155  }
156 
157  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
158  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
159  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
160  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
161 
162  return wcsProps;
163 }
164 
165 template <class Archive>
167  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize start");
168  image::Wcs* ip = dynamic_cast<image::Wcs*>(persistable);
169  if (ip == 0) {
170  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-Wcs");
171  }
172 
173  // Serialize most fields normally
174  ar & ip->_nWcsInfo & ip->_relax;
175  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
176  ar & ip->_coordSystem;
177 
178  // If we are loading, create the array of Wcs parameter structs
179  if (Archive::is_loading::value) {
180  ip->_wcsInfo = reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
181  }
182 
183  for (int i = 0; i < ip->_nWcsInfo; ++i) {
184  // If we are loading, initialize the struct first
185  if (Archive::is_loading::value) {
186  ip->_wcsInfo[i].flag = -1;
187  wcsini(1, 2, &(ip->_wcsInfo[i]));
188  }
189 
190  // Serialize only critical Wcs parameters
191  // wcslib provides support for arrays of wcs', but we only
192  // implement support for one.
193  ar & ip->_wcsInfo[i].naxis;
194  ar & ip->_wcsInfo[i].equinox;
195  ar & ip->_wcsInfo[i].radesys;
196  ar & ip->_wcsInfo[i].crpix[0];
197  ar & ip->_wcsInfo[i].crpix[1];
198  ar & ip->_wcsInfo[i].cd[0];
199  ar & ip->_wcsInfo[i].cd[1];
200  ar & ip->_wcsInfo[i].cd[2];
201  ar & ip->_wcsInfo[i].cd[3];
202  ar & ip->_wcsInfo[i].crval[0];
203  ar & ip->_wcsInfo[i].crval[1];
204  ar & ip->_wcsInfo[i].cunit[0];
205  ar & ip->_wcsInfo[i].cunit[1];
206  ar & ip->_wcsInfo[i].ctype[0];
207  ar & ip->_wcsInfo[i].ctype[1];
208  ar & ip->_wcsInfo[i].altlin;
209 
210  // If we are loading, compute intermediate values given those above
211  if (Archive::is_loading::value) {
212  ip->_wcsInfo[i].flag = 0;
213  wcsset(&(ip->_wcsInfo[i]));
214  }
215  }
216  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize end");
217 }
218 
219 // Explicit template specializations confuse Doxygen, tell it to ignore them
221 template void WcsFormatter::delegateSerialize(boost::archive::text_oarchive&, int, dafBase::Persistable*);
222 template void WcsFormatter::delegateSerialize(boost::archive::text_iarchive&, int, dafBase::Persistable*);
223 template void WcsFormatter::delegateSerialize(boost::archive::binary_oarchive&, int, dafBase::Persistable*);
224 template void WcsFormatter::delegateSerialize(boost::archive::binary_iarchive&, int, dafBase::Persistable*);
226 
230 }
231 }
232 }
233 } // end lsst::afw::formatters
static std::shared_ptr< lsst::daf::base::PropertyList > generatePropertySet(lsst::afw::image::Wcs const &wcs)
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:45
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:104
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
table::Key< std::string > ctype2
Definition: Wcs.cc:933
#define LOGL_DEBUG(logger, message...)
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
STL class.
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
A base class for image defects.
T dynamic_pointer_cast(T... args)
T get(T... args)
#define __attribute__(x)
Definition: WcsFormatter.cc:30
#define LSST_EXCEPT(type,...)
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
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Definition: WcsFormatter.cc:94
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Definition: WcsFormatter.cc:75
#define LOG_GET(logger)
table::Key< std::string > ctype1
Definition: Wcs.cc:932
struct wcsprm * _wcsInfo
Definition: Wcs.h:444
coord::CoordSystem _coordSystem
Definition: Wcs.h:450