summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkarsh Simha <akarsh@kde.org>2016-09-28 07:06:23 (GMT)
committerAkarsh Simha <akarsh@kde.org>2016-09-28 11:54:01 (GMT)
commit3aedbf074e715bd233c584f5c0161dc824c8c1ae (patch)
tree8d84810a069e4550c08255ceac59dfb78f48dfbf
parent90e7e72037b029f61d922ec2fc60e2f9135ec090 (diff)
Fix epoch problems in KStarsDateTime
1. Use the correct definitions of Julian epoch and Besselian epoch. 2. Create static methods to convert JD to Epoch and vice versa 3. Create static method to convert an epoch value expressed as a string into a Julian Epoch number.
-rw-r--r--kstars/time/kstarsdatetime.cpp86
-rw-r--r--kstars/time/kstarsdatetime.h51
2 files changed, 110 insertions, 27 deletions
diff --git a/kstars/time/kstarsdatetime.cpp b/kstars/time/kstarsdatetime.cpp
index 76ed422..10a1e1a 100644
--- a/kstars/time/kstarsdatetime.cpp
+++ b/kstars/time/kstarsdatetime.cpp
@@ -138,7 +138,7 @@ void KStarsDateTime::setDate( const QDate &_d ) {
setDJD( (long double)_d.toJulianDay() + jdFrac );
}
-KStarsDateTime KStarsDateTime::addSecs( double s ) const {
+KStarsDateTime KStarsDateTime::addSecs( double s ) const {
long double ds = (long double)s/86400.;
KStarsDateTime kdt( djd() + ds );
return kdt;
@@ -206,31 +206,73 @@ QTime KStarsDateTime::GSTtoUT( dms GST ) const {
return( QTime( hr, mn, sc, ms ) );
}
-bool KStarsDateTime::setFromEpoch( double epoch ) {
- if (epoch == 1950.0) {
- setDJD( 2433282.4235 );
- return true;
- } else if ( epoch == 2000.0 ) {
- setDJD( J2000 );
- return true;
- } else {
- int year = int( epoch );
- KStarsDateTime dt( QDate( year, 1, 1 ), QTime( 0, 0, 0 ) );
- double days = (double)(dt.date().daysInYear())*( epoch - (double)year );
- dt = dt.addSecs( days*86400. ); //set date and time based on the number of days into the year
-
- if ( dt.isValid() ) {
- setDJD( dt.djd() );
- return true;
- } else
- return false;
- }
+void KStarsDateTime::setFromEpoch( double epoch ) {
+ if ( epoch == 1950.0 ) // Assume Besselian
+ setFromEpoch( epoch, BESSELIAN );
+ else
+ setFromEpoch( epoch, JULIAN ); // Assume Julian
+}
+
+bool KStarsDateTime::setFromEpoch( double epoch, EpochType type ) {
+ if ( type != JULIAN && type != BESSELIAN )
+ return false;
+ else
+ setDJD( epochToJd( epoch, type ) );
+ return true;
}
bool KStarsDateTime::setFromEpoch( const QString &eName ) {
bool result;
- double epoch = eName.toDouble(&result);
+ double epoch;
+ epoch = stringToEpoch( eName, result );
+
if( !result )
return false;
- return setFromEpoch( epoch );
+ return setFromEpoch( epoch, JULIAN ); // We've already converted
+}
+
+
+long double KStarsDateTime::epochToJd(double epoch, EpochType type) {
+ switch( type ) {
+ case BESSELIAN:
+ return B1900 + ( epoch - 1900.0 ) * JD_PER_BYEAR;
+ case JULIAN:
+ return J2000 + ( epoch - 2000.0 ) * 365.25;
+ default:
+ return NaN::d;
+ }
+}
+
+
+double KStarsDateTime::jdToEpoch(long double jd, KStarsDateTime::EpochType type) {
+ // Definitions for conversion formulas are from:
+ //
+ // * http://scienceworld.wolfram.com/astronomy/BesselianEpoch.html
+ // * http://scienceworld.wolfram.com/astronomy/JulianEpoch.html
+ //
+
+ switch( type ) {
+ case KStarsDateTime::BESSELIAN:
+ return 1900.0 + ( jd - KStarsDateTime::B1900 )/KStarsDateTime::JD_PER_BYEAR;
+ case KStarsDateTime::JULIAN:
+ return 2000.0 + ( jd - J2000 )/365.24;
+ default:
+ return NaN::d;
+ }
+}
+
+
+double KStarsDateTime::stringToEpoch(const QString& eName, bool &ok) {
+ double epoch;
+ if ( eName.isEmpty() ) // By default, assume J2000
+ return J2000;
+
+ if ( eName.startsWith( 'J' ) )
+ epoch = eName.mid( 1 ).toDouble(&ok);
+ else if ( eName.startsWith( 'B' ) ) {
+ epoch = eName.mid( 1 ).toDouble(&ok);
+ epoch = jdToEpoch( epochToJd( epoch, BESSELIAN ), JULIAN ); // Convert Besselian epoch to Julian epoch
+ }
+
+ return epoch;
}
diff --git a/kstars/time/kstarsdatetime.h b/kstars/time/kstarsdatetime.h
index 3f227fe..faca3da 100644
--- a/kstars/time/kstarsdatetime.h
+++ b/kstars/time/kstarsdatetime.h
@@ -153,10 +153,18 @@ public:
*/
QTime GSTtoUT( dms GST ) const; // FIXME: Shouldn't this be static?
+ /**
+ *@enum Epoch description options
+ *@note After 1976, the IAU standard for epochs is Julian Years.
+ */
+ enum EpochType {
+ JULIAN, /**< Julian epoch (see http://scienceworld.wolfram.com/astronomy/JulianEpoch.html) */
+ BESSELIAN, /**< Besselian epoch (see http://scienceworld.wolfram.com/astronomy/BesselianEpoch.html) */
+ };
/**
- *@return the epoch value of the Date/Time.
- *@note the epoch is shorthand for the date, expressed as a floating-point year value.
+ *@return the (Julian) epoch value of the Date/Time.
+ *@short This is (approximately) the year expressed as a floating-point value
*@sa setFromEpoch()
*@note The definition of Julian Epoch used here comes from http://scienceworld.wolfram.com/astronomy/JulianEpoch.html
*/
@@ -165,10 +173,9 @@ public:
/**
*Set the Date/Time from an epoch value, represented as a double.
*@p e the epoch value
- *@return true if date set successfully
*@sa epoch()
*/
- bool setFromEpoch( double e );
+ bool setFromEpoch( double e, EpochType type );
/**
*Set the Date/Time from an epoch value, represented as a string.
@@ -178,7 +185,41 @@ public:
*/
bool setFromEpoch( const QString &e );
+ /**
+ *Set the Date/Time from an epoch value, represented as a double.
+ *@p e the epoch value
+ *@note This method assumes that the epoch 1950.0 is Besselian, otherwise assumes that the epoch is a Julian epoch. This is provided for backward compatibility, and because custom catalogs may still use 1950.0 to mean B1950.0 despite the IAU standard for epochs being Julian.
+ *@sa epoch()
+ */
+ void setFromEpoch( double e );
+
+ /**
+ *@short Takes in an epoch and returns a Julian Date
+ *@return the Julian Date (date with fraction)
+ *@param epoch A floating-point year value specifying the Epoch
+ *@param type JULIAN or BESSELIAN depending on what convention the epoch is specified in
+ */
+ static long double epochToJd( double epoch, EpochType type=JULIAN );
+ /**
+ *@short Takes in a Julian Date and returns the corresponding epoch year in the given system
+ *@return the epoch as a floating-point year value
+ *@param jd Julian date
+ *@param type Epoch system (KStarsDateTime::JULIAN or KStarsDateTime::BESSELIAN)
+ */
+ static double jdToEpoch( long double jd, EpochType type=JULIAN );
+
+ /**
+ *@short Takes in a string and returns a Julian epoch
+ */
+ static double stringToEpoch( const QString &eName, bool &ok );
+
+ /**
+ * The following values were obtained from Eric Weisstein's world of science:
+ * http://scienceworld.wolfram.com/astronomy/BesselianEpoch.html
+ */
+ constexpr static const double B1900 = 2415020.31352; // Julian date of B1900 epoch
+ constexpr static const double JD_PER_BYEAR = 365.242198781; // Julian days in a Besselian year
private:
/**
*@return the Greenwich Sidereal Time at 0h UT on this object's Date
@@ -187,7 +228,7 @@ private:
dms GSTat0hUT() const;
long double DJD;
+
};
#endif //KSTARSDATETIME_H_
-