PCL
AstrometricMetadata.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/AstrometricMetadata.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_AstrometricMetadata_h
53 #define __PCL_AstrometricMetadata_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/AutoPointer.h>
60 #include <pcl/Optional.h>
61 #include <pcl/Point.h>
62 #include <pcl/ProjectionBase.h>
63 #include <pcl/Property.h>
64 #include <pcl/SphericalRotation.h>
65 #include <pcl/TimePoint.h>
66 #include <pcl/WCSKeywords.h>
68 
69 /*
70  * Based on original work contributed by AndrĂ©s del Pozo.
71  */
72 
73 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
74 namespace pi
75 {
76  class ImageWindow;
77 }
78 #endif
79 
80 namespace pcl
81 {
82 
83 // ----------------------------------------------------------------------------
84 
97 // ----------------------------------------------------------------------------
98 
99 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
100 class PCL_CLASS ImageWindow;
101 #endif
102 
103 class PCL_CLASS XISFReader;
104 class PCL_CLASS XISFWriter;
105 
112 class PCL_CLASS AstrometricMetadata
113 {
114 public:
115 
122  {
123  String referenceMatrix;
124  String wcsTransformationType;
125  String controlPoints;
126  String splineLengths;
127  String splineErrors;
128  String projectionName;
129  String projectionOrigin;
130  String resolution;
131  String rotation;
132  String referenceSystem;
133  String observationStartTime;
134  String observationEndTime;
135  String observerLocation;
136  String focalDistance;
137  String pixelSize;
138  String fieldOfView;
139  String centerCoordinates;
140  String topLeftCoordinates;
141  String topRightCoordinates;
142  String bottomLeftCoordinates;
143  String bottomRightCoordinates;
144  String referenceCatalog;
145  String creationTime;
146  String creationSoftware;
147  };
148 
153  AstrometricMetadata() = default;
154 
159  : m_projection( x.m_projection->Clone() )
160  , m_transformWI( x.m_transformWI->Clone() )
161  , m_refSys( x.m_refSys )
162  , m_width( x.m_width )
163  , m_height( x.m_height )
164  , m_pixelSize( x.m_pixelSize )
165  , m_obsStartTime( x.m_obsStartTime )
166  , m_obsEndTime( x.m_obsEndTime )
167  , m_geoLongitude( x.m_geoLongitude )
168  , m_geoLatitude( x.m_geoLatitude )
169  , m_geoHeight( x.m_geoHeight )
170  , m_resolution( x.m_resolution )
171  , m_focalLength( x.m_focalLength )
172  , m_creationTime( x.m_creationTime )
173  , m_catalog( x.m_catalog )
174  , m_creatorApp( x.m_creatorApp )
175  , m_creatorModule( x.m_creatorModule )
176  , m_creatorOS( x.m_creatorOS )
177  , m_description( x.m_description )
178  {
179  }
180 
185 
205  AstrometricMetadata( ProjectionBase* projection, WorldTransformation* worldTransformation, int width, int height );
206 
207 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
208  // Implemented in /core/Components/ImageWindow/ImageWindow.cpp
209  AstrometricMetadata( const pi::ImageWindow* );
210 #else
216 #endif
217 
223 
228  {
229  }
230 
234  AstrometricMetadata& operator =( const AstrometricMetadata& x )
235  {
236  m_projection = x.m_projection->Clone();
237  m_transformWI = x.m_transformWI->Clone();
238  m_refSys = x.m_refSys;
239  m_equinox = x.m_equinox;
240  m_width = x.m_width;
241  m_height = x.m_height;
242  m_pixelSize = x.m_pixelSize;
243  m_obsStartTime = x.m_obsStartTime;
244  m_obsEndTime = x.m_obsEndTime;
245  m_geoLongitude = x.m_geoLongitude;
246  m_geoLatitude = x.m_geoLatitude;
247  m_geoHeight = x.m_geoHeight;
248  m_resolution = x.m_resolution;
249  m_focalLength = x.m_focalLength;
250  m_creationTime = x.m_creationTime;
251  m_catalog = x.m_catalog;
252  m_creatorApp = x.m_creatorApp;
253  m_creatorModule = x.m_creatorModule;
254  m_creatorOS = x.m_creatorOS;
255  m_description = x.m_description;
256  return *this;
257  }
258 
262  AstrometricMetadata& operator =( AstrometricMetadata&& ) = default;
263 
268  bool IsValid() const
269  {
270  return !m_projection.IsNull() && !m_transformWI.IsNull();
271  }
272 
303  void Verify( DPoint& centerErrors,
304  DPoint& topLeftErrors, DPoint& topRightErrors,
305  DPoint& bottomLeftErrors, DPoint& bottomRightErrors ) const;
306 
324  void Validate( double tolerance = 0.01 ) const;
325 
332  {
333  return dynamic_cast<const SplineWorldTransformation*>( m_transformWI.Pointer() ) != nullptr;
334  }
335 
336 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
337  // Implemented in /core/Components/ImageWindow/ImageWindow.cpp
338  void Write( pi::ImageWindow* window, bool notify = true ) const;
339 #else
353  void Write( ImageWindow& window, bool notify = true ) const;
354 #endif
355 
372  void Write( XISFWriter& writer ) const;
373 
403  {
404  return m_refSys.IsEmpty() ? IsoString( "ICRS" ) : m_refSys;
405  }
406 
411  void SetReferenceSystem( const IsoString& refSys )
412  {
413  m_refSys = refSys.Trimmed();
414  }
415 
437  static IsoString ReferenceSystemFromMetadata( const PropertyArray& properties, const FITSKeywordArray& keywords );
438 
443  int Width() const
444  {
445  return m_width;
446  }
447 
452  int Height() const
453  {
454  return m_height;
455  }
456 
467  Rect Bounds() const
468  {
469  return Rect( m_width, m_height );
470  }
471 
476  const ProjectionBase* Projection() const
477  {
478  return m_projection.Pointer();
479  }
480 
486  {
487  return m_transformWI.Pointer();
488  }
489 
490 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
491  WorldTransformation* WorldTransform()
492  {
493  return m_transformWI.Pointer();
494  }
495 #endif
496 
501  double ResolutionFromFocal( double focal ) const
502  {
503  return (focal > 0) ? m_pixelSize.OrElse( 0 )/focal * 0.18/Const<double>::pi() : 0.0;
504  }
505 
510  double FocalFromResolution( double resolution ) const
511  {
512  return ResolutionFromFocal( resolution );
513  }
514 
525  double Rotation( bool& flipped ) const;
526 
533  double Resolution() const
534  {
535  return m_resolution;
536  }
537 
545  double SearchRadius() const
546  {
547  if ( !IsValid() )
548  throw Error( "Invalid call to AstrometricMetadata::SearchRadius(): No astrometric solution." );
549  DPoint cRD;
550  if ( !ImageToCelestial( cRD, DPoint( 0.5*m_width, 0.5*m_height ) ) )
551  throw Error( "Failed to perform ImageToCelestial() coordinate transformation for the image center" );
552  DPoint pRD1, pRD2, pRD3, pRD4;
553  if ( !ImageToCelestial( pRD1, DPoint( 0, 0 ) )
554  || !ImageToCelestial( pRD2, DPoint( m_width, 0 ) )
555  || !ImageToCelestial( pRD3, DPoint( 0, m_height ) )
556  || !ImageToCelestial( pRD4, DPoint( m_width, m_height ) ) )
557  {
558  return 180;
559  }
560  return Max( Max( Max( m_projection->Distance( cRD, pRD1 ), m_projection->Distance( cRD, pRD2 ) ),
561  m_projection->Distance( cRD, pRD3 ) ),
562  m_projection->Distance( cRD, pRD4 ) );
563  }
564 
570  {
571  return m_obsStartTime;
572  }
573 
579  {
580  m_obsStartTime = startTime;
581  }
582 
588  {
589  return m_obsEndTime;
590  }
591 
597  {
598  m_obsEndTime = endTime;
599  }
600 
608  {
609  if ( !m_obsStartTime.IsDefined() )
610  return Optional<TimePoint>();
611  if ( !m_obsEndTime.IsDefined() )
612  return m_obsStartTime;
613  return m_obsStartTime() + (m_obsEndTime() - m_obsStartTime())/2;
614  }
615 
622  {
623  return m_geoLongitude;
624  }
625 
634  void SetLocationLongitude( double longitude )
635  {
636  if ( longitude > 180 )
637  longitude -= 360;
638  else if ( longitude <= -180 )
639  longitude += 360;
640  if ( longitude < -180 || longitude > +180 )
641  throw Error( "AstrometricMetadata::SetLocationLongitude(): Geographic longitude out of range." );
642  m_geoLongitude = longitude;
643  }
644 
651  {
652  return m_geoLatitude;
653  }
654 
663  void SetLocationLatitude( double latitude )
664  {
665  if ( latitude < -90 || latitude > +90 )
666  throw Error( "AstrometricMetadata::SetLocationLatitude(): Geographic latitude out of range." );
667  m_geoLatitude = latitude;
668  }
669 
675  {
676  return m_geoHeight;
677  }
678 
682  void SetLocationHeight( double height )
683  {
684  m_geoHeight = height;
685  }
686 
691  {
692  return m_pixelSize;
693  }
694 
699  void SetPixelSize( double pixelSize )
700  {
701  m_pixelSize = pixelSize;
702  m_focalLength = FocalFromResolution( m_resolution );
703  }
704 
711  {
712  return m_creationTime;
713  }
714 
720  String Catalog() const
721  {
722  return m_catalog;
723  }
724 
731  {
732  return m_creatorApp;
733  }
734 
742  {
743  return m_creatorModule;
744  }
745 
753  {
754  return m_creatorOS;
755  }
756 
780  bool ImageToCelestial( DPoint& pRD, const DPoint& pI ) const
781  {
782  if ( !IsValid() )
783  throw Error( "Invalid call to AstrometricMetadata::ImageToCelestial(): No astrometric solution." );
784  if ( m_projection->Inverse( pRD, m_transformWI->Inverse( pI ) ) )
785  {
786  // Constrain right ascension to the [0,360) range.
787  if ( pRD.x < 0 )
788  pRD.x += 360;
789  else if ( pRD.x >= 360 )
790  pRD.x -= 360;
791  return true;
792  }
793  return false;
794  }
795 
826  bool RawImageToCelestial( DPoint& pRD, const DPoint& pI ) const
827  {
828  if ( !IsValid() )
829  throw Error( "Invalid call to AstrometricMetadata::RawImageToCelestial(): No astrometric solution." );
830  return m_projection->Inverse( pRD, m_transformWI->Inverse( pI ) );
831  }
832 
853  bool CelestialToImage( DPoint& pI, const DPoint& pRD ) const
854  {
855  if ( !IsValid() )
856  throw Error( "Invalid call to AstrometricMetadata::CelestialToImage(): No astrometric solution." );
857  DPoint pW;
858  if ( m_projection->Direct( pW, pRD ) )
859  {
860  pI = m_transformWI->Direct( pW );
861  return true;
862  }
863  return false;
864  }
865 
943  void Build( const PropertyArray& properties, const FITSKeywordArray& keywords, int width, int height );
944 
972  void UpdateBasicKeywords( FITSKeywordArray& keywords ) const;
973 
1017  void UpdateWCSKeywords( FITSKeywordArray& keywords, bool generate = false ) const;
1018 
1079  void UpdateProperties( PropertyArray& properties ) const;
1080 
1090  {
1091  m_obsStartTime.Undefine();
1092  m_obsEndTime.Undefine();
1093  m_geoLongitude.Undefine();
1094  m_geoLatitude.Undefine();
1095  m_geoHeight.Undefine();
1096  }
1097 
1107  {
1108  m_obsStartTime = source.m_obsStartTime;
1109  m_obsEndTime = source.m_obsEndTime;
1110  m_geoLongitude = source.m_geoLongitude;
1111  m_geoLatitude = source.m_geoLatitude;
1112  m_geoHeight = source.m_geoHeight;
1113  }
1114 
1124  {
1125  if ( wcs.dateobs.IsDefined() )
1126  m_obsStartTime = TimePoint( wcs.dateobs() );
1127  else
1128  m_obsStartTime.Undefine();
1129 
1130  if ( wcs.dateend.IsDefined() )
1131  m_obsEndTime = TimePoint( wcs.dateend() );
1132  else
1133  m_obsEndTime.Undefine();
1134 
1135  m_geoLongitude = wcs.longobs;
1136  m_geoLatitude = wcs.latobs;
1137  m_geoHeight = wcs.altobs;
1138  }
1139 
1195  static void RemoveKeywords( FITSKeywordArray& keywords, bool removeCenterKeywords = true, bool removeScaleKeywords = true );
1196 
1213  static void RescalePixelSizeKeywords( FITSKeywordArray& keywords, double scalingFactor );
1214 
1263  static void RemoveProperties( PropertyArray& properties, bool removeCenterProperties = true, bool removeScaleProperties = true );
1264 
1272  static void RemoveProperties( ImageWindow& window, bool removeCenterProperties = true, bool removeScaleProperties = true );
1273 
1289  static void RescalePixelSizeProperties( PropertyArray& properties, double scalingFactor );
1290 
1298  static void RescalePixelSizeProperties( ImageWindow& window, double scalingFactor );
1299 
1304  String Summary() const;
1305 
1316  {
1317  UpdateDescription();
1318  return m_description.IsNull() ? DescriptionItems() : *m_description;
1319  }
1320 
1324  static void RemoveSplineWorldTransformationProperties( PropertyArray& );
1325 
1326 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
1327  // Implemented in /core/Components/ImageWindow/ImageWindow.cpp
1328  static void RemoveSplineWorldTransformationProperties( pi::ImageWindow* );
1329 #else
1333  static void RemoveSplineWorldTransformationProperties( ImageWindow& );
1334 #endif
1335 
1336 private:
1337 
1338  AutoPointer<ProjectionBase> m_projection;
1339  AutoPointer<WorldTransformation> m_transformWI;
1340  IsoString m_refSys; // ICRS(default) or GCRS
1341  Optional<double> m_equinox; // years - deprecated
1342  int m_width = 0; // px
1343  int m_height = 0; // px
1344  Optional<double> m_pixelSize; // um
1345  Optional<TimePoint> m_obsStartTime; // UTC
1346  Optional<TimePoint> m_obsEndTime; // UTC
1347  Optional<double> m_geoLongitude; // deg
1348  Optional<double> m_geoLatitude; // deg
1349  Optional<double> m_geoHeight; // m
1350  double m_resolution = 0; // deg/px
1351  Optional<double> m_focalLength; // mm
1352  Optional<TimePoint> m_creationTime;
1353  String m_catalog;
1354  String m_creatorApp;
1355  String m_creatorModule;
1356  String m_creatorOS;
1357  mutable
1358  AutoPointer<DescriptionItems> m_description;
1359 
1360  WCSKeywords ComputeWCSKeywords() const;
1361  void UpdateDescription() const;
1362 };
1363 
1364 } //pcl
1365 
1366 #endif // __AstrometricMetadata_h
1367 
1368 // ----------------------------------------------------------------------------
1369 // EOF pcl/AstrometricMetadata.h - Released 2024-06-18T15:48:54Z
Generic dynamic array.
Definition: Array.h:100
Astrometric metadata.
void CopyTimeAndLocationMetadata(const WCSKeywords &wcs)
Optional< TimePoint > ObservationEndTime() const
void Verify(DPoint &centerErrors, DPoint &topLeftErrors, DPoint &topRightErrors, DPoint &bottomLeftErrors, DPoint &bottomRightErrors) const
bool HasSplineWorldTransformation() const
void UpdateProperties(PropertyArray &properties) const
double Rotation(bool &flipped) const
static void RemoveKeywords(FITSKeywordArray &keywords, bool removeCenterKeywords=true, bool removeScaleKeywords=true)
AstrometricMetadata(const ImageWindow &window)
void Build(const PropertyArray &properties, const FITSKeywordArray &keywords, int width, int height)
bool CelestialToImage(DPoint &pI, const DPoint &pRD) const
void SetLocationLatitude(double latitude)
AstrometricMetadata(ProjectionBase *projection, WorldTransformation *worldTransformation, int width, int height)
bool RawImageToCelestial(DPoint &pRD, const DPoint &pI) const
AstrometricMetadata(AstrometricMetadata &&)=default
void SetLocationLongitude(double longitude)
void Write(XISFWriter &writer) const
DescriptionItems Description() const
static IsoString ReferenceSystemFromMetadata(const PropertyArray &properties, const FITSKeywordArray &keywords)
static void RescalePixelSizeProperties(ImageWindow &window, double scalingFactor)
Optional< double > LocationLongitude() const
const ProjectionBase * Projection() const
AstrometricMetadata(XISFReader &reader)
const WorldTransformation * WorldTransform() const
static void RescalePixelSizeProperties(PropertyArray &properties, double scalingFactor)
void SetLocationHeight(double height)
Optional< double > LocationLatitude() const
bool ImageToCelestial(DPoint &pRD, const DPoint &pI) const
Optional< TimePoint > ObservationMiddleTime() const
IsoString ReferenceSystem() const
void SetReferenceSystem(const IsoString &refSys)
static void RemoveProperties(PropertyArray &properties, bool removeCenterProperties=true, bool removeScaleProperties=true)
void Validate(double tolerance=0.01) const
static void RescalePixelSizeKeywords(FITSKeywordArray &keywords, double scalingFactor)
double ResolutionFromFocal(double focal) const
void Write(ImageWindow &window, bool notify=true) const
void CopyTimeAndLocationMetadata(const AstrometricMetadata &source)
String Summary() const
AstrometricMetadata(const AstrometricMetadata &x)
void SetPixelSize(double pixelSize)
double FocalFromResolution(double resolution) const
Optional< TimePoint > ObservationStartTime() const
static void RemoveProperties(ImageWindow &window, bool removeCenterProperties=true, bool removeScaleProperties=true)
Optional< double > PixelSize() const
void UpdateBasicKeywords(FITSKeywordArray &keywords) const
void SetObservationStartTime(TimePoint startTime)
Optional< TimePoint > CreationTime() const
void SetObservationEndTime(TimePoint endTime)
void UpdateWCSKeywords(FITSKeywordArray &keywords, bool generate=false) const
Optional< double > LocationHeight() const
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
static constexpr T pi()
Definition: Constants.h:94
A simple exception with an associated error message.
Definition: Exception.h:239
A generic point in the two-dimensional space.
Definition: Point.h:100
component x
Abscissa (horizontal, or X-axis coordinate).
Definition: Point.h:111
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
bool IsEmpty() const noexcept
Definition: String.h:818
High-level interface to an image window object in the PixInsight core application.
Definition: ImageWindow.h:287
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
An object that can be in a defined or undefined state.
Definition: Optional.h:82
bool IsDefined() const
Definition: Optional.h:180
Base class of all projection systems.
Surface spline world coordinate transformation.
Unicode (UTF-16) string.
Definition: String.h:8113
An instant in any timescale.
Definition: TimePoint.h:103
A structure for management of WCS FITS header keywords and associated image properties.
Definition: WCSKeywords.h:89
Optional< double > altobs
Geodetic height of the observation location in meters.
Definition: WCSKeywords.h:100
Optional< double > longobs
Geodetic longitude of the observation location in degrees. Reckoned positive east of the reference me...
Definition: WCSKeywords.h:98
Optional< double > latobs
Geodetic latitude of the observation location in degrees. Reckoned positive north of the equator.
Definition: WCSKeywords.h:99
Optional< double > dateobs
Observation start time as a Julian date, UTC timescale.
Definition: WCSKeywords.h:96
Optional< double > dateend
Observation end time as a Julian date, UTC timescale.
Definition: WCSKeywords.h:97
Abstract base class of world coordinate transformations.
XISF input file stream
Definition: XISF.h:892
XISF output file stream
Definition: XISF.h:1281
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
PCL root namespace.
Definition: AbstractImage.h:77
A collection of strings describing the properties and parameters of an astrometric solution.