LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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 
64 namespace {
65 LOG_LOGGER _log = LOG_GET("afw.WcsFormatter");
66 }
67 
68 namespace afwForm = lsst::afw::formatters;
69 namespace afwImg = lsst::afw::image;
70 namespace dafBase = lsst::daf::base;
71 namespace dafPersist = lsst::daf::persistence;
72 namespace pexPolicy = lsst::pex::policy;
73 namespace pexExcept = lsst::pex::exceptions;
74 
75 
77  "Wcs", typeid(afwImg::Wcs), createInstance);
78 
81  dafPersist::Formatter(typeid(this)) {
82 }
83 
85 }
86 
88  dafBase::Persistable const* persistable,
91  LOGL_DEBUG(_log, "WcsFormatter write start");
92  afwImg::Wcs const* ip =
93  dynamic_cast<afwImg::Wcs const*>(persistable);
94  if (ip == 0) {
95  throw LSST_EXCEPT(pexExcept::RuntimeError, "Persisting non-Wcs");
96  }
97  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
98  LOGL_DEBUG(_log, "WcsFormatter write BoostStorage");
99  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
100  boost->getOArchive() & *ip;
101  LOGL_DEBUG(_log, "WcsFormatter write end");
102  return;
103  }
104  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for Wcs");
105 }
106 
108  dafPersist::Storage::Ptr storage,
109  dafBase::PropertySet::Ptr additionalData) {
110  LOGL_DEBUG(_log, "WcsFormatter read start");
111  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
112  afwImg::Wcs* ip = new afwImg::Wcs;
113  LOGL_DEBUG(_log, "WcsFormatter read BoostStorage");
114  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
115  boost->getIArchive() & *ip;
116  LOGL_DEBUG(_log, "WcsFormatter read end");
117  return ip;
118  }
119  else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
120  LOGL_DEBUG(_log, "WcsFormatter read FitsStorage");
121  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
122  int hdu = additionalData->get<int>("hdu", 0);
124  afwImg::readMetadata(fits->getPath(), hdu);
125  afwImg::Wcs* ip = new afwImg::Wcs(md);
126  LOGL_DEBUG(_log, "WcsFormatter read end");
127  return ip;
128  }
129  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unrecognized Storage for Wcs");
130 }
131 
136  throw LSST_EXCEPT(pexExcept::RuntimeError, "Unexpected call to update for Wcs");
137 }
138 
141  // Only generates properties for the first wcsInfo.
143 
144  assert(wcs._wcsInfo); // default ctor is private, so an uninitialized Wcs should not exist in the wild
145 
146  wcsProps->add("NAXIS", wcs._wcsInfo[0].naxis, "number of data axes");
147  // EQUINOX is "not relevant" (FITS definition, version 3.0, page 30) when
148  // dealing with ICRS, and may confuse readers. Don't write it.
149  if (strncmp(wcs._wcsInfo[0].radesys, "ICRS", 4) != 0) {
150  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
151  }
152  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
153  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
154  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
155  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
156  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
157  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
158  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
159  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
160  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
161  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
162  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
163  //
164  // Some projections need PVi_j keywords. Add them.
165  //
166  for (int i = 0; i != wcs._wcsInfo[0].npv; ++i) {
167  auto const pv = wcs._wcsInfo[0].pv[i];
168  int const ii = pv.i > 0 ? pv.i : (wcs._wcsInfo[0].lat + 1); // 0 => latitude axis (see wcslib/wsc.h)
169  char key[20];
170  sprintf(key, "PV%d_%d", ii, pv.m);
171  wcsProps->add(key, pv.value);
172  }
173 
174  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
175  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
176  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
177  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
178 
179  return wcsProps;
180 }
181 
182 template <class Archive>
184  Archive& ar, int const, dafBase::Persistable* persistable) {
185  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize start");
186  afwImg::Wcs* ip = dynamic_cast<afwImg::Wcs*>(persistable);
187  if (ip == 0) {
188  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-Wcs");
189  }
190 
191  // Serialize most fields normally
192  ar & ip->_nWcsInfo & ip->_relax;
193  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
194  ar & ip->_coordSystem;
195 
196 
197  // If we are loading, create the array of Wcs parameter structs
198  if (Archive::is_loading::value) {
199  ip->_wcsInfo =
200  reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
201  }
202 
203 
204  for (int i = 0; i < ip->_nWcsInfo; ++i) {
205  // If we are loading, initialize the struct first
206  if (Archive::is_loading::value) {
207  ip->_wcsInfo[i].flag = -1;
208  wcsini(1, 2, &(ip->_wcsInfo[i]));
209  }
210 
211  // Serialize only critical Wcs parameters
212  //wcslib provides support for arrays of wcs', but we only
213  //implement support for one.
214  ar & ip->_wcsInfo[i].naxis;
215  ar & ip->_wcsInfo[i].equinox;
216  ar & ip->_wcsInfo[i].radesys;
217  ar & ip->_wcsInfo[i].crpix[0];
218  ar & ip->_wcsInfo[i].crpix[1];
219  ar & ip->_wcsInfo[i].cd[0];
220  ar & ip->_wcsInfo[i].cd[1];
221  ar & ip->_wcsInfo[i].cd[2];
222  ar & ip->_wcsInfo[i].cd[3];
223  ar & ip->_wcsInfo[i].crval[0];
224  ar & ip->_wcsInfo[i].crval[1];
225  ar & ip->_wcsInfo[i].cunit[0];
226  ar & ip->_wcsInfo[i].cunit[1];
227  ar & ip->_wcsInfo[i].ctype[0];
228  ar & ip->_wcsInfo[i].ctype[1];
229  ar & ip->_wcsInfo[i].altlin;
230 
231  // If we are loading, compute intermediate values given those above
232  if (Archive::is_loading::value) {
233  ip->_wcsInfo[i].flag = 0;
234  wcsset(&(ip->_wcsInfo[i]));
235  }
236  }
237  LOGL_DEBUG(_log, "WcsFormatter delegateSerialize end");
238 }
239 
241  boost::archive::text_oarchive & , int, dafBase::Persistable*);
243  boost::archive::text_iarchive & , int, dafBase::Persistable*);
245  boost::archive::binary_oarchive & , int, dafBase::Persistable*);
247  boost::archive::binary_iarchive & , int, dafBase::Persistable*);
248 
250  pexPolicy::Policy::Ptr policy) {
252 }
253 
Interface for WcsFormatter class.
std::shared_ptr< Storage > Ptr
Definition: Storage.h:62
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
#define LOGL_DEBUG(logger, message...)
Definition: Log.h:513
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)
Definition: WcsFormatter.cc:87
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
daf_persistence package header file
virtual boost::archive::text_iarchive & getIArchive(void)
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
static lsst::daf::base::PropertyList::Ptr generatePropertySet(lsst::afw::image::Wcs const &wcs)
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
Interface for MaskedImageFormatter class.
#define LOG_GET(logger)
Definition: Log.h:83
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
std::shared_ptr< PropertyList > Ptr
Definition: PropertyList.h:85
Interface for PropertySetFormatter class.
#define __attribute__(x)
Definition: WcsFormatter.cc:39
lsst::afw::image::Wcs Wcs
Definition: Wcs.cc:61
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
boost::shared_ptr< daf::base::PropertySet > readMetadata(std::string const &fileName, int hdu=0, bool strip=false)
Return the metadata (header entries) from a FITS file.
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
Definition: Wcs.h:414
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)
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)
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:79
Include files required for standard LSST Exception handling.
table::Key< std::string > ctype1
Definition: Wcs.cc:1033
struct wcsprm * _wcsInfo
Definition: Wcs.h:410
coord::CoordSystem _coordSystem
Definition: Wcs.h:416
#define LOG_LOGGER
Definition: Log.h:712
Interface for ImageFormatter class.