LSSTApplications  11.0-22-g33de520,13.0+153,14.0+52,14.0+57,14.0-1-g013352c+36,14.0-1-g13ef843+9,14.0-1-g4b114ac+14,14.0-1-g7257b6a+12,14.0-1-g8b7e855+51,14.0-13-g7a60b79+2,14.0-14-g87d16e8+10,14.0-14-gbf7a6f8a,14.0-17-g4f4ea82+5,14.0-2-g319577b+11,14.0-2-ga5af9b6+10,14.0-22-gc48c03f+3,14.0-3-g20413be+3,14.0-46-g76222d5f+3,14.0-47-g0a51fac97,14.0-5-g744ff5f+2,14.0-5-g86eb1bd+31,14.0-6-gd5b81a9+6,14.0-6-ge2c9487+42,14.0-8-g7f6dd6b+6,14.0-8-gb81b6e9+4,14.0-9-g11010eb,14.0-9-g330837b+5
LSSTDataManagementBasePackage
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 
70 
72 
77 
78 WcsFormatter::~WcsFormatter() = default;
79 
83  LOGL_DEBUG(_log, "WcsFormatter write start");
84  image::Wcs const* ip = dynamic_cast<image::Wcs const*>(persistable);
85  if (ip == 0) {
86  throw LSST_EXCEPT(pexExcept::RuntimeError, "Persisting non-Wcs");
87  }
88  // TODO: Replace this with something better in DM-10776
90  if (boost) {
91  LOGL_DEBUG(_log, "WcsFormatter write BoostStorage");
92  boost->getOArchive() & *ip;
93  LOGL_DEBUG(_log, "WcsFormatter write end");
94  return;
95  }
96  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for Wcs");
97 }
98 
100  std::shared_ptr<dafBase::PropertySet> additionalData) {
101  LOGL_DEBUG(_log, "WcsFormatter read start");
102  // TODO: Replace this with something better in DM-10776
104  if (boost) {
105  image::Wcs* ip = new image::Wcs;
106  LOGL_DEBUG(_log, "WcsFormatter read BoostStorage");
107  boost->getIArchive() & *ip;
108  LOGL_DEBUG(_log, "WcsFormatter read end");
109  return ip;
110  }
112  if (fits) {
113  LOGL_DEBUG(_log, "WcsFormatter read FitsStorage");
114  int hdu = additionalData->get<int>("hdu", fits::DEFAULT_HDU);
116  image::Wcs* ip = new image::Wcs(md);
117  LOGL_DEBUG(_log, "WcsFormatter read end");
118  return ip;
119  }
120  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized FormatterStorage for Wcs");
121 }
122 
125  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for Wcs");
126 }
127 
129  // Only generates properties for the first wcsInfo.
131 
132  assert(wcs._wcsInfo); // default ctor is private, so an uninitialized Wcs should not exist in the wild
133 
134  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
135  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
136  // dealing with ICRS, and may confuse readers. Don't write it.
137  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
138  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
139  }
140  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
141  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
142  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
143  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
144  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
145  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
146  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
147  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
148  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
149  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
150  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
151  //
152  // Some projections need PVi_j keywords. Add them.
153  //
154  for (int i = 0; i != wcs._wcsInfo[0].npv; ++i) {
155  auto const pv = wcs._wcsInfo[0].pv[i];
156  int const ii = pv.i > 0 ? pv.i : (wcs._wcsInfo[0].lat + 1); // 0 => latitude axis (see wcslib/wsc.h)
157  char key[20];
158  sprintf(key, "PV%d_%d", ii, pv.m);
159  wcsProps->add(key, pv.value);
160  }
161 
162  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
163  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
164  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
165  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
166 
167  return wcsProps;
168 }
169 
170 template <class Archive>
172  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize start");
173  image::Wcs* ip = dynamic_cast<image::Wcs*>(persistable);
174  if (ip == 0) {
175  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-Wcs");
176  }
177 
178  // Serialize most fields normally
179  ar & ip->_nWcsInfo & ip->_relax;
180  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
181  ar & ip->_coordSystem;
182 
183  // If we are loading, create the array of Wcs parameter structs
184  if (Archive::is_loading::value) {
185  ip->_wcsInfo = reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
186  }
187 
188  for (int i = 0; i < ip->_nWcsInfo; ++i) {
189  // If we are loading, initialize the struct first
190  if (Archive::is_loading::value) {
191  ip->_wcsInfo[i].flag = -1;
192  wcsini(1, 2, &(ip->_wcsInfo[i]));
193  }
194 
195  // Serialize only critical Wcs parameters
196  // wcslib provides support for arrays of wcs', but we only
197  // implement support for one.
198  ar & ip->_wcsInfo[i].naxis;
199  ar & ip->_wcsInfo[i].equinox;
200  ar & ip->_wcsInfo[i].radesys;
201  ar & ip->_wcsInfo[i].crpix[0];
202  ar & ip->_wcsInfo[i].crpix[1];
203  ar & ip->_wcsInfo[i].cd[0];
204  ar & ip->_wcsInfo[i].cd[1];
205  ar & ip->_wcsInfo[i].cd[2];
206  ar & ip->_wcsInfo[i].cd[3];
207  ar & ip->_wcsInfo[i].crval[0];
208  ar & ip->_wcsInfo[i].crval[1];
209  ar & ip->_wcsInfo[i].cunit[0];
210  ar & ip->_wcsInfo[i].cunit[1];
211  ar & ip->_wcsInfo[i].ctype[0];
212  ar & ip->_wcsInfo[i].ctype[1];
213  ar & ip->_wcsInfo[i].altlin;
214 
215  // If we are loading, compute intermediate values given those above
216  if (Archive::is_loading::value) {
217  ip->_wcsInfo[i].flag = 0;
218  wcsset(&(ip->_wcsInfo[i]));
219  }
220  }
221  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize end");
222 }
223 
224 // Explicit template specializations confuse Doxygen, tell it to ignore them
226 template void WcsFormatter::delegateSerialize(boost::archive::text_oarchive&, int, dafBase::Persistable*);
227 template void WcsFormatter::delegateSerialize(boost::archive::text_iarchive&, int, dafBase::Persistable*);
228 template void WcsFormatter::delegateSerialize(boost::archive::binary_oarchive&, int, dafBase::Persistable*);
229 template void WcsFormatter::delegateSerialize(boost::archive::binary_iarchive&, int, dafBase::Persistable*);
231 
235 }
236 }
237 }
238 } // end lsst::afw::formatters
static std::shared_ptr< lsst::daf::base::PropertyList > generatePropertySet(lsst::afw::image::Wcs const &wcs)
WcsFormatter(WcsFormatter const &)
#define LOG_LOGGER
Definition: Log.h:712
Definition: Span.h:37
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:45
Class for storing ordered metadata with comments.
Definition: PropertyList.h:73
WcsFormatter & operator=(WcsFormatter const &)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
tbl::Key< int > wcs
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)
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
Class for FITS file storage.
Definition: FitsStorage.h:52
table::Key< std::string > ctype2
Definition: Wcs.cc:938
daf_persistence package header file
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
STL class.
LSST DM logging module built on log4cxx.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: Utils.h:64
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
A base class for image defects.
Definition: cameraGeom.dox:3
Interface for PropertySetFormatter class.
Include files required for standard LSST Exception handling.
T dynamic_pointer_cast(T... args)
T get(T... args)
#define __attribute__(x)
Definition: WcsFormatter.cc:30
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
Definition: Wcs.h:450
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
Definition: Wcs.h:448
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:449
Class for boost::serialization storage.
Definition: BoostStorage.h:59
Base class for all persistable classes.
Definition: Persistable.h:74
static lsst::daf::persistence::FormatterRegistration registration
Definition: WcsFormatter.h:74
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:99
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:80
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
table::Key< std::string > ctype1
Definition: Wcs.cc:937
struct wcsprm * _wcsInfo
Definition: Wcs.h:446
coord::CoordSystem _coordSystem
Definition: Wcs.h:452
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18