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");
83 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
84 LOGL_DEBUG(_log,
"WcsFormatter write BoostStorage");
85 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
86 boost->getOArchive() & *ip;
87 LOGL_DEBUG(_log,
"WcsFormatter write end");
90 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized FormatterStorage for Wcs");
93 dafBase::Persistable* WcsFormatter::read(std::shared_ptr<dafPersist::FormatterStorage> storage,
94 std::shared_ptr<dafBase::PropertySet> additionalData) {
95 LOGL_DEBUG(_log,
"WcsFormatter read start");
96 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
98 LOGL_DEBUG(_log,
"WcsFormatter read BoostStorage");
99 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
100 boost->getIArchive() & *ip;
101 LOGL_DEBUG(_log,
"WcsFormatter read end");
103 }
else if (
typeid(*storage) ==
typeid(dafPersist::FitsStorage)) {
104 LOGL_DEBUG(_log,
"WcsFormatter 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,
"WcsFormatter read end");
112 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unrecognized FormatterStorage for Wcs");
115 void WcsFormatter::update(dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
116 std::shared_ptr<dafBase::PropertySet>) {
117 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Unexpected call to update for Wcs");
120 std::shared_ptr<dafBase::PropertyList> WcsFormatter::generatePropertySet(
image::Wcs const& wcs) {
126 wcsProps->add(
"NAXIS", wcs.
_wcsInfo[0].naxis,
"number of data axes");
129 if (strncmp(wcs.
_wcsInfo[0].radesys,
"ICRS", 4) != 0) {
130 wcsProps->add(
"EQUINOX", wcs.
_wcsInfo[0].equinox,
"Equinox of coordinates");
132 wcsProps->add(
"RADESYS", std::string(wcs.
_wcsInfo[0].radesys),
"Coordinate system for equinox");
133 wcsProps->add(
"CRPIX1", wcs.
_wcsInfo[0].crpix[0],
"WCS Coordinate reference pixel");
134 wcsProps->add(
"CRPIX2", wcs.
_wcsInfo[0].crpix[1],
"WCS Coordinate reference pixel");
135 wcsProps->add(
"CD1_1", wcs.
_wcsInfo[0].cd[0],
"WCS Coordinate scale matrix");
136 wcsProps->add(
"CD1_2", wcs.
_wcsInfo[0].cd[1],
"WCS Coordinate scale matrix");
137 wcsProps->add(
"CD2_1", wcs.
_wcsInfo[0].cd[2],
"WCS Coordinate scale matrix");
138 wcsProps->add(
"CD2_2", wcs.
_wcsInfo[0].cd[3],
"WCS Coordinate scale matrix");
139 wcsProps->add(
"CRVAL1", wcs.
_wcsInfo[0].crval[0],
"WCS Ref value (RA in decimal degrees)");
140 wcsProps->add(
"CRVAL2", wcs.
_wcsInfo[0].crval[1],
"WCS Ref value (DEC in decimal degrees)");
141 wcsProps->add(
"CUNIT1", std::string(wcs.
_wcsInfo[0].cunit[0]));
142 wcsProps->add(
"CUNIT2", std::string(wcs.
_wcsInfo[0].cunit[1]));
146 for (
int i = 0; i != wcs.
_wcsInfo[0].npv; ++i) {
147 auto const pv = wcs.
_wcsInfo[0].pv[i];
148 int const ii = pv.i > 0 ? pv.i : (wcs.
_wcsInfo[0].lat + 1);
150 sprintf(key,
"PV%d_%d", ii, pv.m);
151 wcsProps->add(key, pv.value);
156 wcsProps->add(
"CTYPE1", ctype1,
"WCS Coordinate type");
157 wcsProps->add(
"CTYPE2", ctype2,
"WCS Coordinate type");
162 template <
class Archive>
163 void WcsFormatter::delegateSerialize(Archive& ar,
int const, dafBase::Persistable* persistable) {
164 LOGL_DEBUG(_log,
"WcsFormatter delegateSerialize start");
167 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Serializing non-Wcs");
176 if (Archive::is_loading::value) {
177 ip->
_wcsInfo =
reinterpret_cast<wcsprm*
>(malloc(ip->
_nWcsInfo *
sizeof(wcsprm)));
180 for (
int i = 0; i < ip->
_nWcsInfo; ++i) {
182 if (Archive::is_loading::value) {
208 if (Archive::is_loading::value) {
213 LOGL_DEBUG(_log,
"WcsFormatter delegateSerialize end");
218 template void WcsFormatter::delegateSerialize(boost::archive::text_oarchive&,
int, dafBase::Persistable*);
219 template void WcsFormatter::delegateSerialize(boost::archive::text_iarchive&,
int, dafBase::Persistable*);
220 template void WcsFormatter::delegateSerialize(boost::archive::binary_oarchive&,
int, dafBase::Persistable*);
221 template void WcsFormatter::delegateSerialize(boost::archive::binary_iarchive&,
int, dafBase::Persistable*);
224 std::shared_ptr<dafPersist::Formatter> WcsFormatter::createInstance(
225 std::shared_ptr<pexPolicy::Policy> policy) {
226 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