30 #define __attribute__(x)
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>
42 #include "wcslib/wcs.h"
44 #include "lsst/daf/base.h"
45 #include "lsst/daf/persistence.h"
46 #include "lsst/daf/persistence/PropertySetFormatter.h"
47 #include "lsst/pex/exceptions.h"
48 #include "lsst/log/Log.h"
56 LOG_LOGGER _log = LOG_GET(
"afw.TanWcsFormatter");
61 namespace formatters {
63 namespace dafBase = lsst::daf::base;
64 namespace dafPersist = lsst::daf::persistence;
65 namespace pexPolicy = lsst::pex::policy;
66 namespace pexExcept = lsst::pex::exceptions;
68 dafPersist::FormatterRegistration TanWcsFormatter::registration(
"TanWcs",
typeid(
image::TanWcs),
71 TanWcsFormatter::TanWcsFormatter(std::shared_ptr<pexPolicy::Policy>) : dafPersist::Formatter(typeid(this)) {}
73 TanWcsFormatter::~TanWcsFormatter(
void) {}
75 void TanWcsFormatter::write(dafBase::Persistable
const* persistable,
76 std::shared_ptr<dafPersist::Storage> storage,
77 std::shared_ptr<dafBase::PropertySet>) {
78 LOGL_DEBUG(_log,
"TamWcsFormatter write start");
81 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Persisting non-TanWcs");
83 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
84 LOGL_DEBUG(_log,
"TanWcsFormatter write BoostStorage");
85 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
86 boost->getOArchive() & *ip;
87 LOGL_DEBUG(_log,
"TanWcsFormatter write end");
90 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized Storage for TanWcs");
93 dafBase::Persistable* TanWcsFormatter::read(std::shared_ptr<dafPersist::Storage> storage,
94 std::shared_ptr<dafBase::PropertySet> additionalData) {
95 LOGL_DEBUG(_log,
"TanWcsFormatter read start");
96 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
98 LOGL_DEBUG(_log,
"TanWcsFormatter read BoostStorage");
99 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
100 boost->getIArchive() & *ip;
101 LOGL_DEBUG(_log,
"TanWcsFormatter read end");
103 }
else if (
typeid(*storage) ==
typeid(dafPersist::FitsStorage)) {
104 LOGL_DEBUG(_log,
"TanWcsFormatter read FitsStorage");
105 dafPersist::FitsStorage* fits =
dynamic_cast<dafPersist::FitsStorage*
>(storage.get());
106 int hdu = additionalData->get<
int>(
"hdu", INT_MIN);
109 LOGL_DEBUG(_log,
"TanWcsFormatter read end");
112 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized Storage for TanWcs");
115 void TanWcsFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::Storage>,
116 std::shared_ptr<dafBase::PropertySet>) {
117 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unexpected call to update for TanWcs");
121 template <
class Archive>
126 if (Archive::is_loading::value) {
127 m = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(rows, cols);
129 for (
int j = 0; j < m.cols(); ++j) {
130 for (
int i = 0; i < m.rows(); ++i) {
136 static void encodeSipHeader(std::shared_ptr<daf::base::PropertySet> wcsProps,
137 std::string
const& which,
138 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
const& m) {
139 int order = m.rows();
140 if (m.cols() != order) {
141 throw LSST_EXCEPT(pexExcept::DomainError,
"sip" + which +
" matrix is not square");
145 wcsProps->add(which +
"_ORDER", static_cast<int>(order));
146 for (
int i = 0; i <= order; ++i) {
147 for (
int j = 0; j <= order; ++j) {
148 double val = m(i, j);
150 wcsProps->add((boost::format(
"%1%_%2%_%3%") % which % i % j).str(), val);
157 std::shared_ptr<dafBase::PropertyList> TanWcsFormatter::generatePropertySet(
image::TanWcs const& wcs) {
165 wcsProps->add(
"NAXIS", wcs.
_wcsInfo[0].naxis,
"number of data axes");
168 if (strncmp(wcs.
_wcsInfo[0].radesys,
"ICRS", 4) != 0) {
169 wcsProps->add(
"EQUINOX", wcs.
_wcsInfo[0].equinox,
"Equinox of coordinates");
171 wcsProps->add(
"RADESYS", std::string(wcs.
_wcsInfo[0].radesys),
"Coordinate system for equinox");
172 wcsProps->add(
"CRPIX1", wcs.
_wcsInfo[0].crpix[0],
"WCS Coordinate reference pixel");
173 wcsProps->add(
"CRPIX2", wcs.
_wcsInfo[0].crpix[1],
"WCS Coordinate reference pixel");
174 wcsProps->add(
"CD1_1", wcs.
_wcsInfo[0].cd[0],
"WCS Coordinate scale matrix");
175 wcsProps->add(
"CD1_2", wcs.
_wcsInfo[0].cd[1],
"WCS Coordinate scale matrix");
176 wcsProps->add(
"CD2_1", wcs.
_wcsInfo[0].cd[2],
"WCS Coordinate scale matrix");
177 wcsProps->add(
"CD2_2", wcs.
_wcsInfo[0].cd[3],
"WCS Coordinate scale matrix");
178 wcsProps->add(
"CRVAL1", wcs.
_wcsInfo[0].crval[0],
"WCS Ref value (RA in decimal degrees)");
179 wcsProps->add(
"CRVAL2", wcs.
_wcsInfo[0].crval[1],
"WCS Ref value (DEC in decimal degrees)");
180 wcsProps->add(
"CUNIT1", std::string(wcs.
_wcsInfo[0].cunit[0]));
181 wcsProps->add(
"CUNIT2", std::string(wcs.
_wcsInfo[0].cunit[1]));
192 if (wcs._hasDistortion) {
193 if (ctype1.rfind(
"-SIP") == std::string::npos) {
196 if (ctype2.rfind(
"-SIP") == std::string::npos) {
199 encodeSipHeader(wcsProps,
"A", wcs._sipA);
200 encodeSipHeader(wcsProps,
"B", wcs._sipB);
201 encodeSipHeader(wcsProps,
"AP", wcs._sipAp);
202 encodeSipHeader(wcsProps,
"BP", wcs._sipBp);
204 wcsProps->add(
"CTYPE1", ctype1,
"WCS Coordinate type");
205 wcsProps->add(
"CTYPE2", ctype2,
"WCS Coordinate type");
210 template <
class Archive>
211 void TanWcsFormatter::delegateSerialize(Archive& ar,
int const, dafBase::Persistable* persistable) {
212 LOGL_DEBUG(_log,
"TanWcsFormatter delegateSerialize start");
215 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Serializing non-TanWcs");
223 ar & ip->_hasDistortion;
225 if (ip->_hasDistortion) {
233 if (Archive::is_loading::value) {
234 ip->
_wcsInfo =
reinterpret_cast<wcsprm*
>(malloc(ip->
_nWcsInfo *
sizeof(wcsprm)));
237 for (
int i = 0; i < ip->
_nWcsInfo; ++i) {
239 if (Archive::is_loading::value) {
263 if (Archive::is_loading::value) {
268 LOGL_DEBUG(_log,
"TanWcsFormatter delegateSerialize end");
273 template void TanWcsFormatter::delegateSerialize(boost::archive::text_oarchive&,
int, dafBase::Persistable*);
274 template void TanWcsFormatter::delegateSerialize(boost::archive::text_iarchive&,
int, dafBase::Persistable*);
275 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_oarchive&,
int,
276 dafBase::Persistable*);
277 template void TanWcsFormatter::delegateSerialize(boost::archive::binary_iarchive&,
int,
278 dafBase::Persistable*);
281 std::shared_ptr<dafPersist::Formatter> TanWcsFormatter::createInstance(
282 std::shared_ptr<pexPolicy::Policy> policy) {
283 return std::shared_ptr<dafPersist::Formatter>(
new TanWcsFormatter(policy));
table::Key< std::string > ctype2
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Read FITS header.
Implementation of the WCS standard for the special case of the Gnomonic (tangent plane) projection...
int _wcshdrCtrl
Controls messages to stderr from wcshdr (0 for none); see wcshdr.h for details.
int _relax
Degree of permissiveness for wcspih (0 for strict); see wcshdr.h for details.
int _wcsfixCtrl
Do potentially unsafe translations of non-standard unit strings? 0/1 = no/yes.
table::Key< std::string > ctype1
coord::CoordSystem _coordSystem
lsst::daf::base::PropertyList PropertyList