69 void setMetadataFromFoundValue(daf::base::PropertyList& metadata,
std::string const&
name,
71 if (!foundValue.
found) {
72 throw LSST_EXCEPT(pex::exceptions::LogicError,
"Bug! FitsChan card \"" + name +
"\" not found");
74 if (comment.empty()) {
75 metadata.set(name, foundValue.
value);
77 metadata.set(name, foundValue.
value, comment);
91 excludeNames.
insert(moreNames.begin(), moreNames.end());
94 if (metadata.
exists(
"RADECSYS") && !metadata.
exists(
"RADESYS")) {
96 metadata.
remove(
"RADECSYS");
101 ast::FitsChan channel(stream,
"Encoding=FITS-WCS, IWC=1, SipReplace=0");
105 obj = channel.
read();
108 "The metadata does not describe an AST object");
113 "metadata describes a " + obj->getClassName() +
", not a FrameSet");
120 std::set_difference(initialNames.begin(), initialNames.end(), finalNames.begin(), finalNames.end(),
121 std::inserter(namesChannelStripped, namesChannelStripped.begin()));
129 for (
auto const& name : namesToStrip) {
151 auto const initialBaseIndex = rawFrameSet->getBase();
155 if (rawFrameSet->findFrame(
ast::Frame(2,
"Domain=GRID"))) {
156 gridIndex = rawFrameSet->getCurrent();
160 auto const baseFrame = rawFrameSet->getFrame(initialBaseIndex,
false);
161 auto const baseClassName = rawFrameSet->getClassName();
162 if (baseFrame->getClassName() !=
"Frame") {
164 "The base frame is of type " + baseFrame->getClassName() +
165 "instead of Frame; cannot read metadata as a SkyWcs");
167 if (baseFrame->getNAxes() != 2) {
170 " axes instead of 2; cannot read metadata as a SkyWcs");
172 if (baseFrame->getDomain() !=
"") {
174 "The base frame has domain \"" + baseFrame->getDomain() +
175 "\" instead of blank or GRID; cannot read metadata as a SkyWcs");
179 gridIndex = initialBaseIndex;
183 if (!rawFrameSet->findFrame(
ast::Frame(2,
"Domain=IWC"))) {
185 "No IWC frame found; cannot read metadata as a SkyWcs");
187 auto const iwcIndex = rawFrameSet->getCurrent();
188 auto const iwcFrame = rawFrameSet->getFrame(iwcIndex);
193 auto const stdSkyFrameTemplate =
ast::SkyFrame(
"System=ICRS");
200 if (!rawFrameSet->findFrame(stdSkyFrameTemplate)) {
202 "Could not find a SkyFrame; cannot read metadata as a SkyWcs");
204 auto initialSkyIndex = rawFrameSet->getCurrent();
210 rawFrameSet->setBase(initialSkyIndex);
211 auto stdSkyFrameSet = rawFrameSet->findFrame(stdSkyFrameTemplate);
212 if (!stdSkyFrameSet) {
214 "Bug: found a SkyFrame the first time, but not the second time");
221 rawFrameSet->addFrame(initialSkyIndex, *stdSkyFrameSet->getMapping()->simplify(),
223 auto const stdSkyIndex = rawFrameSet->getCurrent();
224 auto const stdSkyFrame = rawFrameSet->getFrame(stdSkyIndex,
false);
233 auto const gridToIwc = rawFrameSet->getMapping(gridIndex, iwcIndex)->simplify();
234 auto const pixelToIwc = pixelToGrid.then(*gridToIwc).simplify();
235 auto const iwcToStdSky = rawFrameSet->getMapping(iwcIndex, stdSkyIndex);
237 auto frameDict = std::make_shared<ast::FrameDict>(
ast::Frame(2,
"Domain=PIXELS"), *pixelToIwc, *iwcFrame);
238 frameDict->addFrame(
"IWC", *iwcToStdSky, *stdSkyFrame);
242 auto crpixPixels = pixelToGrid.applyInverse(crpixGrid);
243 auto crvalRad = frameDict->applyForward(crpixPixels);
248 skyFrame->setSkyRefIs(
"Ignored");
249 skyFrame->setSkyRef(crvalRad);
255 int const numCards = fitsChan.
getNCard();
256 auto metadata = std::make_shared<daf::base::PropertyList>();
257 for (
int cardNum = 1; cardNum <= numCards; ++cardNum) {
264 auto foundValue = fitsChan.
getFitsF();
265 setMetadataFromFoundValue(*metadata, cardName, foundValue, cardComment);
269 auto foundValue = fitsChan.
getFitsI();
270 setMetadataFromFoundValue(*metadata, cardName, foundValue, cardComment);
274 auto foundValue = fitsChan.
getFitsS();
275 setMetadataFromFoundValue(*metadata, cardName, foundValue, cardComment);
279 auto foundValue = fitsChan.
getFitsL();
280 setMetadataFromFoundValue(*metadata, cardName, foundValue, cardComment);
285 setMetadataFromFoundValue(*metadata, cardName, foundValue, cardComment);
296 os <<
"Card " << cardNum <<
" with name \"" << cardName <<
"\" has type " 297 <<
static_cast<int>(cardType) <<
", which is not supported by PropertyList";
303 os <<
"Bug! Card " << cardNum <<
" with name \"" << cardName
304 <<
"\" has type NOTYPE, which should not be possible";
double getAsDouble(std::string const &name) const
FoundValue< std::string > getFitsCN(std::string const &name="", std::string defval="") const
virtual void remove(std::string const &name)
CardType getCardType() const
std::shared_ptr< daf::base::PropertyList > createTrivialWcsMetadata(std::string const &wcsName, geom::Point2I const &xy0)
std::string getCardComm() const
std::vector< std::string > getAllCardNames()
T make_move_iterator(T... args)
geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
std::shared_ptr< ast::FrameDict > readLsstSkyWcs(daf::base::PropertySet &metadata, bool strip=true)
Read an LSST celestial WCS FrameDict from a FITS header.
std::string getCardName() const
static int constexpr CURRENT
void set(std::string const &name, T const &value)
FoundValue< int > getFitsI(std::string const &name="", int defval=0) const
A base class for image defects.
FoundValue< std::string > getFitsS(std::string const &name="", std::string defval="") const
static int constexpr NOFRAME
bool exists(std::string const &name) const
std::shared_ptr< daf::base::PropertyList > getPropertyListFromFitsChan(ast::FitsChan &fitsChan)
Copy values from an AST FitsChan into a PropertyList.
T dynamic_pointer_cast(T... args)
FoundValue< double > getFitsF(std::string const &name="", double defval=0) const
#define LSST_EXCEPT(type,...)
virtual char const * what(void) const
std::shared_ptr< ast::FrameSet > readFitsWcs(daf::base::PropertySet &metadata, bool strip=true)
Read a FITS convention WCS FrameSet from FITS metadata.
std::string getAsString(std::string const &name) const
T set_difference(T... args)
std::shared_ptr< Object > read()
std::string const wcsNameForXY0
std::string makeLimitedFitsHeader(lsst::daf::base::PropertySet const &metadata, std::set< std::string > const &excludeNames={})
Format a PropertySet into an FITS header string in a simplistic fashion.
FoundValue< bool > getFitsL(std::string const &name="", bool defval=false) const