53 int const SERIALIZATION_VERSION = 1;
61 double const TIGHT_FITS_TOL = 0.0001;
63 class SkyWcsPersistenceHelper {
66 table::Key<table::Array<std::uint8_t>>
wcs;
68 static SkyWcsPersistenceHelper
const&
get() {
69 static SkyWcsPersistenceHelper instance;
74 SkyWcsPersistenceHelper(
const SkyWcsPersistenceHelper&) =
delete;
75 SkyWcsPersistenceHelper& operator=(
const SkyWcsPersistenceHelper&) =
delete;
78 SkyWcsPersistenceHelper(SkyWcsPersistenceHelper&&) =
delete;
79 SkyWcsPersistenceHelper& operator=(SkyWcsPersistenceHelper&&) =
delete;
82 SkyWcsPersistenceHelper()
84 wcs(schema.addField<table::Array<std::uint8_t>>(
"wcs",
"wcs string representation",
"")) {
85 schema.getCitizen().markPersistent();
89 class SkyWcsFactory :
public table::io::PersistableFactory {
91 explicit SkyWcsFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
94 CatalogVector
const& catalogs)
const {
95 SkyWcsPersistenceHelper
const&
keys = SkyWcsPersistenceHelper::get();
99 table::BaseRecord
const& record = catalogs.front().front();
105 std::string getSkyWcsPersistenceName() {
return "SkyWcs"; }
107 SkyWcsFactory registration(getSkyWcsPersistenceName());
112 Eigen::Matrix2d cdMatrix;
113 double orientRad = orientation.
asRadians();
115 double xmult = flipX ? 1.0 : -1.0;
116 cdMatrix(0, 0) =
std::cos(orientRad) * scaleDeg * xmult;
117 cdMatrix(0, 1) =
std::sin(orientRad) * scaleDeg;
118 cdMatrix(1, 0) = -
std::sin(orientRad) * scaleDeg * xmult;
119 cdMatrix(1, 1) =
std::cos(orientRad) * scaleDeg;
124 auto const dstInverse = dst.
getTransform()->getInverse();
138 double const side = 1.0;
147 auto skyLL = skyVec[0].getVector();
148 auto skyDx = skyVec[1].getVector() - skyLL;
149 auto skyDy = skyVec[2].getVector() - skyLL;
154 double skyAreaSq = skyDx.asEigen().cross(skyDy.asEigen()).squaredNorm();
165 auto const crvalRad = skyFrame->getSkyRef();
172 return pixelToIwcTransform.getJacobian(pixel);
181 return std::make_shared<SkyWcs>(*metadata);
194 auto const iwcToSky = thisDict->getMapping(
"IWC",
"SKY");
195 auto const gridToSky = gridToPixel.
then(*pixelToIwc).
then(*iwcToSky);
200 os <<
"Encoding=FITS-WCS, CDMatrix=1, FitsAxisOrder=<copy>, FitsTol=" << TIGHT_FITS_TOL;
204 if (nObjectsWritten == 0) {
207 "Could not represent this SkyWcs using FITS-WCS metadata");
212 return tanWcs->getFitsMetadata(
true);
234 return _linearizePixelToSky(
skyToPixel(coord), coord, skyUnit);
237 return _linearizePixelToSky(pix,
pixelToSky(pix), skyUnit);
241 return _linearizeSkyToPixel(
skyToPixel(coord), coord, skyUnit);
245 return _linearizeSkyToPixel(pix,
pixelToSky(pix), skyUnit);
266 is >> shortClassName;
279 os <<
"The AST serialization was read as a " << astObjectPtr->getClassName()
280 <<
" instead of a FrameSet";
284 return std::make_shared<SkyWcs>(frameDict);
308 SkyWcsPersistenceHelper
const&
keys = SkyWcsPersistenceHelper::get();
316 : _frameDict(frameDict), _transform(),
318 _pixelScaleAtOrigin(0 *
radians) {
325 for (
auto const& domainName : domainNames) {
327 auto const frame = frameDict.
getFrame(domainName,
false);
328 if (frame->getNAxes() != 2) {
330 "Frame " + domainName +
" has " +
std::to_string(frame->getNAxes()) +
331 " axes instead of 2");
333 auto desiredClassName = domainName ==
"SKY" ?
"SkyFrame" :
"Frame";
334 if (frame->getClassName() != desiredClassName) {
336 "Frame " + domainName +
" is of type " + frame->getClassName() +
337 " instead of " + desiredClassName);
339 }
else if (domainName !=
"ACTUAL_PIXELS") {
342 "No frame with domain " + domainName +
" found");
348 if (baseDomain !=
"ACTUAL_PIXELS" && baseDomain !=
"PIXELS") {
350 "Base frame has domain " + baseDomain +
" instead of PIXELS or ACTUAL_PIXELS");
355 if (currentDomain !=
"SKY") {
357 "Current frame has domain " + currentDomain +
" instead of SKY");
360 return frameDict.
copy();
368 const double side = 1.0;
374 m(0, 0) = dsky10.first.asAngularUnits(skyUnit) / side;
375 m(0, 1) = dsky01.first.asAngularUnits(skyUnit) / side;
376 m(1, 0) = dsky10.second.asAngularUnits(skyUnit) / side;
377 m(1, 1) = dsky01.second.asAngularUnits(skyUnit) / side;
379 Eigen::Vector2d sky00v;
380 sky00v << sky00.getX(), sky00.getY();
381 Eigen::Vector2d pix00v;
382 pix00v << pix00.getX(), pix00.getY();
395 double const dx = 1000;
413 bool modifyActualPixels) {
414 auto const pixelMapping = pixelTransform.
getMapping();
416 bool const hasActualPixels = oldFrameDict->hasDomain(
"ACTUAL_PIXELS");
417 auto const pixelFrame = oldFrameDict->getFrame(
"PIXELS",
false);
418 auto const iwcFrame = oldFrameDict->getFrame(
"IWC",
false);
419 auto const skyFrame = oldFrameDict->getFrame(
"SKY",
false);
420 auto const oldPixelToIwc = oldFrameDict->getMapping(
"PIXELS",
"IWC");
421 auto const iwcToSky = oldFrameDict->getMapping(
"IWC",
"SKY");
425 if (hasActualPixels) {
426 auto const actualPixelFrame = oldFrameDict->getFrame(
"ACTUAL_PIXELS",
false);
427 auto const oldActualPixelToPixels = oldFrameDict->getMapping(
"ACTUAL_PIXELS",
"PIXELS");
429 if (modifyActualPixels) {
430 newActualPixelsToPixels = pixelMapping->then(*oldActualPixelToPixels).simplify();
431 newPixelToIwc = oldPixelToIwc;
433 newActualPixelsToPixels = oldActualPixelToPixels;
434 newPixelToIwc = pixelMapping->then(*oldPixelToIwc).simplify();
437 std::make_shared<ast::FrameDict>(*actualPixelFrame, *newActualPixelsToPixels, *pixelFrame);
438 newFrameDict->addFrame(
"PIXELS", *newPixelToIwc, *iwcFrame);
440 newPixelToIwc = pixelMapping->then(*oldPixelToIwc).simplify();
441 newFrameDict = std::make_shared<ast::FrameDict>(*pixelFrame, *newPixelToIwc, *iwcFrame);
443 newFrameDict->addFrame(
"IWC", *iwcToSky, *skyFrame);
444 return std::make_shared<SkyWcs>(*newFrameDict);
448 return std::make_shared<SkyWcs>(metadata,
strip);
452 Eigen::Matrix2d
const& cdMatrix,
std::string const& projection) {
454 return std::make_shared<SkyWcs>(*metadata);
458 Eigen::Matrix2d
const& cdMatrix, Eigen::MatrixXd
const& sipA,
459 Eigen::MatrixXd
const& sipB) {
461 return std::make_shared<SkyWcs>(*metadata);
465 Eigen::Matrix2d
const& cdMatrix, Eigen::MatrixXd
const& sipA,
466 Eigen::MatrixXd
const& sipB, Eigen::MatrixXd
const& sipAp,
467 Eigen::MatrixXd
const& sipBp) {
469 return std::make_shared<SkyWcs>(*metadata);
474 auto iwcToSky = wcs.
getFrameDict()->getMapping(
"IWC",
"SKY");
475 return std::make_shared<TransformPoint2ToSpherePoint>(*iwcToSky, simplify);
480 return std::make_shared<TransformPoint2ToPoint2>(*pixelToIwc, simplify);
Point in an unspecified spherical coordinate system.
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Extent< double, 2 > Extent2D
An object passed to Persistable::write to allow it to persist itself.
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
std::shared_ptr< daf::base::PropertyList > makeTanSipMetadata(Point2D const &crpix, SpherePoint const &crval, Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA, Eigen::MatrixXd const &sipB)
Make metadata for a TAN-SIP WCS without inverse matrices.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
AngleUnit constexpr radians
constant with units of radians
bool isFits() const
Return true getFitsMetadata(true) will succeed, false if not.
table::PointKey< double > crpix
def scale(algorithm, min, max=None, frame=None)
SeriesMap then(Mapping const &next) const
std::shared_ptr< SkyWcs > makeModifiedWcs(TransformPoint2ToPoint2 const &pixelTransform, SkyWcs const &wcs, bool modifyActualPixels)
Create a new SkyWcs whose pixels are transformed by pixelTransform, as described below.
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
table::PointKey< double > crval
Point2D getPosition(AngleUnit unit) const
Return longitude, latitude as a Point2D object.
std::shared_ptr< ast::FrameDict > readLsstSkyWcs(daf::base::PropertySet &metadata, bool strip=true)
Read an LSST celestial WCS FrameDict from a FITS header.
ItemVariant const * other
static int constexpr CURRENT
A class used to convert scalar POD types such as double to Angle.
std::shared_ptr< const ast::FrameDict > getFrameDict() const
Get the contained FrameDict.
Transform< Point2Endpoint, Point2Endpoint > TransformPoint2ToPoint2
lsst::afw::geom::SpherePoint SpherePoint
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Eigen::Matrix2d getCdMatrix() const
Get the 2x2 CD matrix at the pixel origin.
std::shared_ptr< const TransformPoint2ToSpherePoint > getTransform() const
Get a TransformPoint2ToSpherePoint that transforms pixels to sky in the forward direction and sky to ...
A class representing an angle.
table::Key< table::Array< std::uint8_t > > wcs
A base class for image defects.
bool hasFitsApproximation() const
Does this SkyWcs have an approximate SkyWcs that can be represented as standard FITS WCS...
std::shared_ptr< RecordT > src
std::shared_ptr< SkyWcs > getTanWcs(Point2D const &pixel) const
Get a local TAN WCS approximation to this WCS at the specified pixel position.
std::shared_ptr< Frame > getFrame(std::string const &domain, bool copy=true) const
std::shared_ptr< SkyWcs > makeFlippedWcs(SkyWcs const &wcs, bool flipLR, bool flipTB, geom::Point2D const ¢er)
Return a copy of a FITS-WCS with pixel positions flipped around a specified center.
Angle getPixelScale() const
Get the pixel scale at the pixel origin.
std::shared_ptr< FrameDict > copy() const
std::shared_ptr< daf::base::PropertyList > makeSimpleWcsMetadata(Point2D const &crpix, SpherePoint const &crval, Eigen::Matrix2d const &cdMatrix, std::string const &projection="TAN")
Make FITS metadata for a simple FITS WCS (one with no distortion).
int write(Object const &object)
static std::shared_ptr< SkyWcs > readStream(std::istream &is)
Deserialize a SkyWcs from an input stream.
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)
static std::string getShortClassName()
std::shared_ptr< TransformPoint2ToPoint2 > getPixelToIntermediateWorldCoords(SkyWcs const &wcs, bool simplify=true)
Return a transform from pixel coordinates to intermediate world coordinates.
std::shared_ptr< TransformPoint2ToSpherePoint > getIntermediateWorldCoordsToSky(SkyWcs const &wcs, bool simplify=true)
Return a transform from intermediate world coordinates to sky.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
AffineTransform linearizeSkyToPixel(SpherePoint const &coord, AngleUnit const &skyUnit) const
Return the local linear approximation to skyToPixel at a point given in sky coordinates.
Point2D getPixelOrigin() const
Get the pixel origin, in pixels, using the LSST convention.
std::shared_ptr< TransformPoint2ToPoint2 > makeWcsPairTransform(SkyWcs const &src, SkyWcs const &dst)
A Transform obtained by putting two SkyWcs objects "back to back".
#define LSST_EXCEPT(type,...)
std::shared_ptr< SkyWcs > copyAtShiftedPixelOrigin(Extent2D const &shift) const
Return a copy of this SkyWcs with the pixel origin shifted by the specified amount.
static std::shared_ptr< SkyWcs > readString(std::string &str)
Deserialize a SkyWcs from a string, using the same format as readStream.
SpherePoint pixelToSky(Point2D const &pixel) const
Compute sky position(s) from pixel position(s)
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Point2D skyToPixel(SpherePoint const &sky) const
Compute pixel position(s) from sky position(s)
SkyWcs(SkyWcs const &)=default
std::string writeString() const
Serialize this SkyWcs to a string, using the same format as writeStream.
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
std::shared_ptr< daf::base::PropertyList > getFitsMetadata(bool precise=false) const
Return the WCS as FITS WCS metadata.
constexpr double asRadians() const noexcept
Return an Angle's value in radians.
AffineTransform linearizePixelToSky(SpherePoint const &coord, AngleUnit const &skyUnit) const
Return the local linear approximation to pixelToSky at a point given in sky coordinates.
Eigen::Matrix2d makeCdMatrix(Angle const &scale, Angle const &orientation=0 *degrees, bool flipX=false)
Make a WCS CD matrix.
table::PointKey< int > pixel
void writeStream(std::ostream &os) const
Serialize this SkyWcs to an output stream.
static int constexpr BASE
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
bool isFlipped() const
Does the WCS follow the convention of North=Up, East=Left?
constexpr double asDegrees() const noexcept
Return an Angle's value in degrees.
std::pair< Angle, Angle > getTangentPlaneOffset(SpherePoint const &other) const
Get the offset from a tangent plane centered at this point to another point.
SpherePoint getSkyOrigin() const
Get the sky origin, the celestial fiducial point.
bool operator==(SkyWcs const &other) const
Equality is based on the string representations being equal.
std::shared_ptr< Object > read()
std::shared_ptr< SkyWcs > makeTanSipWcs(Point2D const &crpix, SpherePoint const &crval, Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA, Eigen::MatrixXd const &sipB)
Construct a TAN-SIP SkyWcs with forward SIP distortion terms and an iterative inverse.
bool hasDomain(std::string const &domain) const
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.