LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
43 #include <string>
44 
45 // not used? #include <stdlib.h>
46 #include <boost/archive/binary_iarchive.hpp>
47 #include <boost/archive/binary_oarchive.hpp>
48 #include <boost/archive/text_iarchive.hpp>
49 #include <boost/archive/text_oarchive.hpp>
50 
51 //#include "boost/serialization/shared_ptr.hpp"
52 #include "wcslib/wcs.h"
53 
54 #include "lsst/daf/base.h"
55 #include "lsst/daf/persistence.h"
57 #include "lsst/log/Log.h"
58 #include "lsst/pex/exceptions.h"
62 #include "lsst/afw/image/Wcs.h"
63 #include "lsst/afw/fits.h"
64 
65 namespace {
66 LOG_LOGGER _log = LOG_GET("afw.WcsFormatter");
67 }
68 
69 namespace afwForm = lsst::afw::formatters;
70 namespace afwImg = lsst::afw::image;
71 namespace dafBase = lsst::daf::base;
72 namespace dafPersist = lsst::daf::persistence;
73 namespace pexPolicy = lsst::pex::policy;
74 namespace pexExcept = lsst::pex::exceptions;
75 
76 
78  "Wcs", typeid(afwImg::Wcs), createInstance);
79 
82  dafPersist::Formatter(typeid(this)) {
83 }
84 
86 }
87 
89  dafBase::Persistable const* persistable,
92  LOGL_DEBUG(_log, "WcsFormatter write start");
93  afwImg::Wcs const* ip =
94  dynamic_cast<afwImg::Wcs const*>(persistable);
95  if (ip == 0) {
96  throw LSST_EXCEPT(pexExcept::RuntimeError, "Persisting non-Wcs");
97  }
98  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
99  LOGL_DEBUG(_log, "WcsFormatter write BoostStorage");
100  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
101  boost->getOArchive() & *ip;
102  LOGL_DEBUG(_log, "WcsFormatter write end");
103  return;
104  }
105  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for Wcs");
106 }
107 
109  dafPersist::Storage::Ptr storage,
110  dafBase::PropertySet::Ptr additionalData) {
111  LOGL_DEBUG(_log, "WcsFormatter read start");
112  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
113  afwImg::Wcs* ip = new afwImg::Wcs;
114  LOGL_DEBUG(_log, "WcsFormatter read BoostStorage");
115  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
116  boost->getIArchive() & *ip;
117  LOGL_DEBUG(_log, "WcsFormatter read end");
118  return ip;
119  }
120  else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
121  LOGL_DEBUG(_log, "WcsFormatter read FitsStorage");
122  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
123  int hdu = additionalData->get<int>("hdu", 0);
125  afw::fits::readMetadata(fits->getPath(), hdu);
126  afwImg::Wcs* ip = new afwImg::Wcs(md);
127  LOGL_DEBUG(_log, "WcsFormatter read end");
128  return ip;
129  }
130  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for Wcs");
131 }
132 
137  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for Wcs");
138 }
139 
142  // Only generates properties for the first wcsInfo.
144 
145  assert(wcs._wcsInfo); // default ctor is private, so an uninitialized Wcs should not exist in the wild
146 
147  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
148  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
149  // dealing with ICRS, and may confuse readers. Don't write it.
150  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
151  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
152  }
153  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
154  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
155  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
156  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
157  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
158  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
159  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
160  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
161  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
162  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
163  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
164  //
165  // Some projections need PVi_j keywords. Add them.
166  //
167  for (int i = 0; i != wcs._wcsInfo[0].npv; ++i) {
168  auto const pv = wcs._wcsInfo[0].pv[i];
169  int const ii = pv.i > 0 ? pv.i : (wcs._wcsInfo[0].lat + 1); // 0 => latitude axis (see wcslib/wsc.h)
170  char key[20];
171  sprintf(key, "PV%d_%d", ii, pv.m);
172  wcsProps->add(key, pv.value);
173  }
174 
175  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
176  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
177  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
178  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
179 
180  return wcsProps;
181 }
182 
183 template <class Archive>
185  Archive& ar, int const, dafBase::Persistable* persistable) {
186  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize start");
187  afwImg::Wcs* ip = dynamic_cast<afwImg::Wcs*>(persistable);
188  if (ip == 0) {
189  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-Wcs");
190  }
191 
192  // Serialize most fields normally
193  ar & ip->_nWcsInfo & ip->_relax;
194  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
195  ar & ip->_coordSystem;
196 
197 
198  // If we are loading, create the array of Wcs parameter structs
199  if (Archive::is_loading::value) {
200  ip->_wcsInfo =
201  reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
202  }
203 
204 
205  for (int i = 0; i < ip->_nWcsInfo; ++i) {
206  // If we are loading, initialize the struct first
207  if (Archive::is_loading::value) {
208  ip->_wcsInfo[i].flag = -1;
209  wcsini(1, 2, &(ip->_wcsInfo[i]));
210  }
211 
212  // Serialize only critical Wcs parameters
213  //wcslib provides support for arrays of wcs', but we only
214  //implement support for one.
215  ar & ip->_wcsInfo[i].naxis;
216  ar & ip->_wcsInfo[i].equinox;
217  ar & ip->_wcsInfo[i].radesys;
218  ar & ip->_wcsInfo[i].crpix[0];
219  ar & ip->_wcsInfo[i].crpix[1];
220  ar & ip->_wcsInfo[i].cd[0];
221  ar & ip->_wcsInfo[i].cd[1];
222  ar & ip->_wcsInfo[i].cd[2];
223  ar & ip->_wcsInfo[i].cd[3];
224  ar & ip->_wcsInfo[i].crval[0];
225  ar & ip->_wcsInfo[i].crval[1];
226  ar & ip->_wcsInfo[i].cunit[0];
227  ar & ip->_wcsInfo[i].cunit[1];
228  ar & ip->_wcsInfo[i].ctype[0];
229  ar & ip->_wcsInfo[i].ctype[1];
230  ar & ip->_wcsInfo[i].altlin;
231 
232  // If we are loading, compute intermediate values given those above
233  if (Archive::is_loading::value) {
234  ip->_wcsInfo[i].flag = 0;
235  wcsset(&(ip->_wcsInfo[i]));
236  }
237  }
238  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize end");
239 }
240 
242  boost::archive::text_oarchive & , int, dafBase::Persistable*);
244  boost::archive::text_iarchive & , int, dafBase::Persistable*);
246  boost::archive::binary_oarchive & , int, dafBase::Persistable*);
248  boost::archive::binary_iarchive & , int, dafBase::Persistable*);
249 
251  pexPolicy::Policy::Ptr policy) {
253 }
254 
Interface for WcsFormatter class.
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
#define LOG_LOGGER
Definition: Log.h:712
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:55
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Write a Persistable instance to a Storage instance.
Definition: WcsFormatter.cc:88
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
daf_persistence package header file
Include files required for standard LSST Exception handling.
static lsst::daf::base::PropertyList::Ptr generatePropertySet(lsst::afw::image::Wcs const &wcs)
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
tbl::Key< int > wcs
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
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:1034
virtual boost::archive::text_oarchive & getOArchive(void)
Get a boost::serialization archive suitable for output.
boost::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=0, bool strip=false)
Read FITS header.
Definition: Utils.h:62
Interface for MaskedImageFormatter class.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
LSST DM logging module built on log4cxx.
std::shared_ptr< PropertyList > Ptr
Definition: PropertyList.h:85
std::shared_ptr< Storage > Ptr
Definition: Storage.h:62
Interface for PropertySetFormatter class.
Utilities for working with FITS files.
#define __attribute__(x)
Definition: WcsFormatter.cc:39
virtual boost::archive::text_iarchive & getIArchive(void)
Get a boost::serialization archive suitable for input.
lsst::afw::image::Wcs Wcs
Definition: Wcs.cc:61
#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:414
virtual std::string const & getPath(void)
Return the pathname for the FITS file.
Definition: FitsStorage.cc:109
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
Definition: Wcs.h:412
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:413
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Update an existing Persistable instance with information from an additional Storage instance...
Class for boost::serialization storage.
Definition: BoostStorage.h:59
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Read a Persistable instance from a Storage instance.
static lsst::daf::persistence::Formatter::Ptr createInstance(lsst::pex::policy::Policy::Ptr policy)
Base class for all persistable classes.
Definition: Persistable.h:74
static lsst::daf::persistence::FormatterRegistration registration
Definition: WcsFormatter.h:91
WcsFormatter(lsst::pex::policy::Policy::Ptr policy)
Definition: WcsFormatter.cc:80
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
table::Key< std::string > ctype1
Definition: Wcs.cc:1033
struct wcsprm * _wcsInfo
Definition: Wcs.h:410
coord::CoordSystem _coordSystem
Definition: Wcs.h:416
Interface for ImageFormatter class.