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>
43 #include "wcslib/wcs.h"
45 #include "lsst/daf/base.h"
46 #include "lsst/daf/persistence.h"
47 #include "lsst/daf/persistence/PropertySetFormatter.h"
48 #include "lsst/log/Log.h"
49 #include "lsst/pex/exceptions.h"
57 LOG_LOGGER _log = LOG_GET(
"afw.WcsFormatter");
62 namespace formatters {
64 namespace dafBase = lsst::daf::base;
65 namespace dafPersist = lsst::daf::persistence;
66 namespace pexPolicy = lsst::pex::policy;
67 namespace pexExcept = lsst::pex::exceptions;
69 dafPersist::FormatterRegistration WcsFormatter::registration(
"Wcs",
typeid(
image::Wcs), createInstance);
71 WcsFormatter::WcsFormatter(std::shared_ptr<pexPolicy::Policy>) : dafPersist::Formatter(typeid(this)) {}
73 WcsFormatter::~WcsFormatter(
void) {}
75 void WcsFormatter::write(dafBase::Persistable
const* persistable,
76 std::shared_ptr<dafPersist::FormatterStorage> storage,
77 std::shared_ptr<dafBase::PropertySet>) {
78 LOGL_DEBUG(_log,
"WcsFormatter write start");
81 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Persisting non-Wcs");
84 auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
86 LOGL_DEBUG(_log,
"WcsFormatter write BoostStorage");
87 boost->getOArchive() & *ip;
88 LOGL_DEBUG(_log,
"WcsFormatter write end");
91 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized FormatterStorage for Wcs");
94 dafBase::Persistable* WcsFormatter::read(std::shared_ptr<dafPersist::FormatterStorage> storage,
95 std::shared_ptr<dafBase::PropertySet> additionalData) {
96 LOGL_DEBUG(_log,
"WcsFormatter read start");
98 auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
101 LOGL_DEBUG(_log,
"WcsFormatter read BoostStorage");
102 boost->getIArchive() & *ip;
103 LOGL_DEBUG(_log,
"WcsFormatter read end");
106 auto fits = std::dynamic_pointer_cast<dafPersist::FitsStorage>(storage);
108 LOGL_DEBUG(_log,
"WcsFormatter read FitsStorage");
109 int hdu = additionalData->get<
int>(
"hdu", INT_MIN);
112 LOGL_DEBUG(_log,
"WcsFormatter read end");
115 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized FormatterStorage for Wcs");
118 void WcsFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
119 std::shared_ptr<dafBase::PropertySet>) {
120 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unexpected call to update for Wcs");
123 std::shared_ptr<dafBase::PropertyList> WcsFormatter::generatePropertySet(
image::Wcs const& wcs) {
129 wcsProps->add(
"NAXIS", wcs.
_wcsInfo[0].naxis,
"number of data axes");
132 if (strncmp(wcs.
_wcsInfo[0].radesys,
"ICRS", 4) != 0) {
133 wcsProps->add(
"EQUINOX", wcs.
_wcsInfo[0].equinox,
"Equinox of coordinates");
135 wcsProps->add(
"RADESYS", std::string(wcs.
_wcsInfo[0].radesys),
"Coordinate system for equinox");
136 wcsProps->add(
"CRPIX1", wcs.
_wcsInfo[0].crpix[0],
"WCS Coordinate reference pixel");
137 wcsProps->add(
"CRPIX2", wcs.
_wcsInfo[0].crpix[1],
"WCS Coordinate reference pixel");
138 wcsProps->add(
"CD1_1", wcs.
_wcsInfo[0].cd[0],
"WCS Coordinate scale matrix");
139 wcsProps->add(
"CD1_2", wcs.
_wcsInfo[0].cd[1],
"WCS Coordinate scale matrix");
140 wcsProps->add(
"CD2_1", wcs.
_wcsInfo[0].cd[2],
"WCS Coordinate scale matrix");
141 wcsProps->add(
"CD2_2", wcs.
_wcsInfo[0].cd[3],
"WCS Coordinate scale matrix");
142 wcsProps->add(
"CRVAL1", wcs.
_wcsInfo[0].crval[0],
"WCS Ref value (RA in decimal degrees)");
143 wcsProps->add(
"CRVAL2", wcs.
_wcsInfo[0].crval[1],
"WCS Ref value (DEC in decimal degrees)");
144 wcsProps->add(
"CUNIT1", std::string(wcs.
_wcsInfo[0].cunit[0]));
145 wcsProps->add(
"CUNIT2", std::string(wcs.
_wcsInfo[0].cunit[1]));
149 for (
int i = 0; i != wcs.
_wcsInfo[0].npv; ++i) {
150 auto const pv = wcs.
_wcsInfo[0].pv[i];
151 int const ii = pv.i > 0 ? pv.i : (wcs.
_wcsInfo[0].lat + 1);
153 sprintf(key,
"PV%d_%d", ii, pv.m);
154 wcsProps->add(key, pv.value);
159 wcsProps->add(
"CTYPE1", ctype1,
"WCS Coordinate type");
160 wcsProps->add(
"CTYPE2", ctype2,
"WCS Coordinate type");
165 template <
class Archive>
166 void WcsFormatter::delegateSerialize(Archive& ar,
int const, dafBase::Persistable* persistable) {
167 LOGL_DEBUG(_log,
"WcsFormatter delegateSerialize start");
170 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Serializing non-Wcs");
179 if (Archive::is_loading::value) {
180 ip->
_wcsInfo =
reinterpret_cast<wcsprm*
>(malloc(ip->
_nWcsInfo *
sizeof(wcsprm)));
183 for (
int i = 0; i < ip->
_nWcsInfo; ++i) {
185 if (Archive::is_loading::value) {
211 if (Archive::is_loading::value) {
216 LOGL_DEBUG(_log,
"WcsFormatter delegateSerialize end");
221 template void WcsFormatter::delegateSerialize(boost::archive::text_oarchive&,
int, dafBase::Persistable*);
222 template void WcsFormatter::delegateSerialize(boost::archive::text_iarchive&,
int, dafBase::Persistable*);
223 template void WcsFormatter::delegateSerialize(boost::archive::binary_oarchive&,
int, dafBase::Persistable*);
224 template void WcsFormatter::delegateSerialize(boost::archive::binary_iarchive&,
int, dafBase::Persistable*);
227 std::shared_ptr<dafPersist::Formatter> WcsFormatter::createInstance(
228 std::shared_ptr<pexPolicy::Policy> policy) {
229 return std::shared_ptr<dafPersist::Formatter>(
new WcsFormatter(policy));
table::Key< std::string > ctype2
Implementation of the WCS standard for a any projection.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Read FITS header.
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