LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 // not used? #include <stdlib.h>
44 #include <boost/archive/binary_iarchive.hpp>
45 #include <boost/archive/binary_oarchive.hpp>
46 #include <boost/archive/text_iarchive.hpp>
47 #include <boost/archive/text_oarchive.hpp>
48 
49 //#include "boost/serialization/shared_ptr.hpp"
50 #include "wcslib/wcs.h"
51 
52 #include "lsst/daf/base.h"
53 #include "lsst/daf/persistence.h"
55 #include "lsst/pex/exceptions.h"
56 #include "lsst/pex/logging/Trace.h"
60 #include "lsst/afw/image/Wcs.h"
61 
62 #define EXEC_TRACE 20
63 static void execTrace(std::string s, int level = EXEC_TRACE) {
64  lsst::pex::logging::Trace("afw.WcsFormatter", level, s);
65 }
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  execTrace("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  execTrace("WcsFormatter write BoostStorage");
99  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
100  boost->getOArchive() & *ip;
101  execTrace("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  execTrace("WcsFormatter read start");
111  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
112  afwImg::Wcs* ip = new afwImg::Wcs;
113  execTrace("WcsFormatter read BoostStorage");
114  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
115  boost->getIArchive() & *ip;
116  execTrace("WcsFormatter read end");
117  return ip;
118  }
119  else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
120  execTrace("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  execTrace("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  wcsProps->add("EQUINOX", wcs._wcsInfo[0].equinox, "Equinox of coordinates");
148  wcsProps->add("RADESYS", std::string(wcs._wcsInfo[0].radesys), "Coordinate system for equinox");
149  wcsProps->add("CRPIX1", wcs._wcsInfo[0].crpix[0], "WCS Coordinate reference pixel");
150  wcsProps->add("CRPIX2", wcs._wcsInfo[0].crpix[1], "WCS Coordinate reference pixel");
151  wcsProps->add("CD1_1", wcs._wcsInfo[0].cd[0], "WCS Coordinate scale matrix");
152  wcsProps->add("CD1_2", wcs._wcsInfo[0].cd[1], "WCS Coordinate scale matrix");
153  wcsProps->add("CD2_1", wcs._wcsInfo[0].cd[2], "WCS Coordinate scale matrix");
154  wcsProps->add("CD2_2", wcs._wcsInfo[0].cd[3], "WCS Coordinate scale matrix");
155  wcsProps->add("CRVAL1", wcs._wcsInfo[0].crval[0], "WCS Ref value (RA in decimal degrees)");
156  wcsProps->add("CRVAL2", wcs._wcsInfo[0].crval[1], "WCS Ref value (DEC in decimal degrees)");
157  wcsProps->add("CUNIT1", std::string(wcs._wcsInfo[0].cunit[0]));
158  wcsProps->add("CUNIT2", std::string(wcs._wcsInfo[0].cunit[1]));
159  //
160  // Some projections need PVi_j keywords. Add them.
161  //
162  for (int i = 0; i != wcs._wcsInfo[0].npv; ++i) {
163  auto const pv = wcs._wcsInfo[0].pv[i];
164  int const ii = pv.i > 0 ? pv.i : (wcs._wcsInfo[0].lat + 1); // 0 => latitude axis (see wcslib/wsc.h)
165  char key[20];
166  sprintf(key, "PV%d_%d", ii, pv.m);
167  wcsProps->add(key, pv.value);
168  }
169 
170  std::string ctype1(wcs._wcsInfo[0].ctype[0]);
171  std::string ctype2(wcs._wcsInfo[0].ctype[1]);
172  wcsProps->add("CTYPE1", ctype1, "WCS Coordinate type");
173  wcsProps->add("CTYPE2", ctype2, "WCS Coordinate type");
174 
175  return wcsProps;
176 }
177 
178 template <class Archive>
180  Archive& ar, int const, dafBase::Persistable* persistable) {
181  execTrace("WcsFormatter delegateSerialize start");
182  afwImg::Wcs* ip = dynamic_cast<afwImg::Wcs*>(persistable);
183  if (ip == 0) {
184  throw LSST_EXCEPT(pexExcept::RuntimeError, "Serializing non-Wcs");
185  }
186 
187  // Serialize most fields normally
188  ar & ip->_nWcsInfo & ip->_relax;
189  ar & ip->_wcsfixCtrl & ip->_wcshdrCtrl & ip->_nReject;
190  ar & ip->_coordSystem;
191 
192 
193  // If we are loading, create the array of Wcs parameter structs
194  if (Archive::is_loading::value) {
195  ip->_wcsInfo =
196  reinterpret_cast<wcsprm*>(malloc(ip->_nWcsInfo * sizeof(wcsprm)));
197  }
198 
199 
200  for (int i = 0; i < ip->_nWcsInfo; ++i) {
201  // If we are loading, initialize the struct first
202  if (Archive::is_loading::value) {
203  ip->_wcsInfo[i].flag = -1;
204  wcsini(1, 2, &(ip->_wcsInfo[i]));
205  }
206 
207  // Serialize only critical Wcs parameters
208  //wcslib provides support for arrays of wcs', but we only
209  //implement support for one.
210  ar & ip->_wcsInfo[i].naxis;
211  ar & ip->_wcsInfo[i].equinox;
212  ar & ip->_wcsInfo[i].radesys;
213  ar & ip->_wcsInfo[i].crpix[0];
214  ar & ip->_wcsInfo[i].crpix[1];
215  ar & ip->_wcsInfo[i].cd[0];
216  ar & ip->_wcsInfo[i].cd[1];
217  ar & ip->_wcsInfo[i].cd[2];
218  ar & ip->_wcsInfo[i].cd[3];
219  ar & ip->_wcsInfo[i].crval[0];
220  ar & ip->_wcsInfo[i].crval[1];
221  ar & ip->_wcsInfo[i].cunit[0];
222  ar & ip->_wcsInfo[i].cunit[1];
223  ar & ip->_wcsInfo[i].ctype[0];
224  ar & ip->_wcsInfo[i].ctype[1];
225  ar & ip->_wcsInfo[i].altlin;
226 
227  // If we are loading, compute intermediate values given those above
228  if (Archive::is_loading::value) {
229  ip->_wcsInfo[i].flag = 0;
230  wcsset(&(ip->_wcsInfo[i]));
231  }
232  }
233  execTrace("WcsFormatter delegateSerialize end");
234 }
235 
237  boost::archive::text_oarchive & , int, dafBase::Persistable*);
239  boost::archive::text_iarchive & , int, dafBase::Persistable*);
241  boost::archive::binary_oarchive & , int, dafBase::Persistable*);
243  boost::archive::binary_iarchive & , int, dafBase::Persistable*);
244 
246  pexPolicy::Policy::Ptr policy) {
248 }
249 
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
Definition: Wcs.h:394
Interface for WcsFormatter class.
boost::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
Class implementing persistence and retrieval for Wcs objects.
Definition: WcsFormatter.h:55
coord::CoordSystem _coordSystem
Definition: Wcs.h:397
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
daf_persistence package header file
virtual boost::archive::text_iarchive & getIArchive(void)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
definition of the Trace messaging facilities
WcsFormatter(lsst::pex::policy::Policy::Ptr policy)
Definition: WcsFormatter.cc:79
static lsst::daf::persistence::FormatterRegistration registration
Definition: WcsFormatter.h:91
tbl::Key< int > wcs
static lsst::daf::base::PropertyList::Ptr generatePropertySet(lsst::afw::image::Wcs const &wcs)
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
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:1039
Interface for MaskedImageFormatter class.
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
struct wcsprm * _wcsInfo
Definition: Wcs.h:391
boost::shared_ptr< PropertyList > Ptr
Definition: PropertyList.h:84
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
Interface for PropertySetFormatter class.
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
#define __attribute__(x)
Definition: WcsFormatter.cc:39
lsst::afw::image::Wcs Wcs
Definition: Wcs.cc:60
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
Definition: Wcs.h:393
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.
boost::shared_ptr< Storage > Ptr
Definition: Storage.h:62
Class for boost::serialization storage.
Definition: BoostStorage.h:58
Base class for all persistable classes.
Definition: Persistable.h:74
static lsst::daf::persistence::Formatter::Ptr createInstance(lsst::pex::policy::Policy::Ptr policy)
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
Definition: Wcs.h:395
table::Key< std::string > ctype1
Definition: Wcs.cc:1038
#define EXEC_TRACE
Definition: WcsFormatter.cc:62
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
Include files required for standard LSST Exception handling.
Interface for ImageFormatter class.
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)