CppEphem
test_CEDate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * test_CEDate.cpp: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2018 JCardenzana *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 
22 #include "test_CEDate.h"
23 #include "CENamespace.h"
24 
25 
26 /**********************************************************************/
30  CETestSuite()
31 {
32  // Set the date to J2000 Julian date
33  base_date_.SetDate(2451545.000000);
35 }
36 
37 
38 /**********************************************************************/
42 {}
43 
44 
45 /**********************************************************************/
51 {
52  std::cout << "\nTesting CEDate:\n";
53 
54  // Run each of the tests
61 
62  return pass();
63 }
64 
65 
66 /**********************************************************************/
70 {
71  // Values for testing
72  std::vector<double> greg_20190101 = {2019, 1.0, 1.0, 0.5};
73  double jd_20190101 = 2458485.0;
74 
75  // Default constructor. Because the default date is given as the current
76  // date, the check is just that the value is greater than 01-01-2019
77  CEDate test1;
78  test_greaterthan(test1.JD(), jd_20190101, __func__, __LINE__);
79 
80  // Copy constructor
81  CEDate test2(base_date_);
82  test_double(test2.JD(), base_date_.JD(), __func__, __LINE__);
83 
84  // Copy assignment operator
85  CEDate test3 = base_date_;
86  test_double(test3.JD(), base_date_.JD(), __func__, __LINE__);
87 
88  // Construct from a vector
89  CEDate test4(greg_20190101);
90  test_double(test4.JD(), jd_20190101, __func__, __LINE__);
91 
92  // Construct from a Julian date
93  CEDate test5(jd_20190101);
94  test_double(test5.JD(), jd_20190101, __func__, __LINE__);
95 
96  return pass();
97 }
98 
99 
100 /**********************************************************************/
104 {
106  return test_double(test_date_.JD(), base_date_.JD(), __func__, __LINE__);
107 }
108 
109 /**********************************************************************/
113 {
115  return test_double(test_date_.MJD(), base_date_.MJD(), __func__, __LINE__);
116 }
117 
118 
119 /**********************************************************************/
123 {
125  test_double(test_date_.Gregorian(), base_date_.Gregorian(), __func__, __LINE__);
126 
127  // Test getting the year, month, day, and day fraction
128  test_int(test_date_.Year(), base_date_.Year(), __func__, __LINE__);
129  test_int(test_date_.Month(), base_date_.Month(), __func__, __LINE__);
130  test_int(test_date_.Day(), base_date_.Day(), __func__, __LINE__);
131  test_double(test_date_.DayFraction(), base_date_.DayFraction(), __func__, __LINE__);
132 
133  // Test getting the Gregorian date as a vector
134  test_vect(test_date_.GregorianVect(), base_date_.GregorianVect(), __func__, __LINE__);
135 
136  // Test setting the date as a gregorian vector
137  std::vector<double> greg_vec = base_date_.GregorianVect();
138  CEDate test1;
139  test(test1.JD() != base_date_.JD(), __func__, __LINE__);
140  test1.SetDate(greg_vec);
141  test_vect(test1.GregorianVect(), greg_vec, __func__, __LINE__);
142 
143  // Make sure that the returned errors of GregorianVect2JD are handled
144  greg_vec = base_date_.GregorianVect();
145  std::vector<double> greg_vec2 = greg_vec;
146  // Test invalid year
147  greg_vec2[0] = -50000;
148  test_double(CEDate::GregorianVect2JD(greg_vec2), 0.0, __func__, __LINE__);
149  greg_vec2 = greg_vec;
150  // Test invalid month
151  greg_vec2[1] = -1;
152  test_double(CEDate::GregorianVect2JD(greg_vec2), 0.0, __func__, __LINE__);
153  greg_vec2[1] = 13;
154  test_double(CEDate::GregorianVect2JD(greg_vec2), 0.0, __func__, __LINE__);
155  greg_vec2 = greg_vec;
156  // Test date
157  greg_vec2[2] += 100;
158  test_double(CEDate::GregorianVect2JD(greg_vec2), base_date_.JD() + 100.0, __func__, __LINE__);
159 
160  // Test conversion of gregorian vector to JD, MJD, Gregorian date
161  greg_vec = {2000, 01, 01, 0.5};
162  double test_jd = CppEphem::julian_date_J2000();
163  double test_mjd = test_jd - DJM0;
164  double test_greg = 20000101.5;
165  test_double(CEDate::GregorianVect2JD(greg_vec), test_jd, __func__, __LINE__);
166  test_double(CEDate::GregorianVect2MJD(greg_vec), test_mjd, __func__, __LINE__);
167  test_double(CEDate::GregorianVect2Gregorian(greg_vec), test_greg, __func__, __LINE__);
168 
169  return pass();
170 }
171 
172 
173 /**********************************************************************/
177 {
178  // Reset the date
180 
181  // Set the return type to JD
183  test_double(test_date_, base_date_.JD(), __func__, __LINE__);
184 
185  // Set the return type to MJD
187  test_double(test_date_, base_date_.MJD(), __func__, __LINE__);
188 
189  // Set the return type to Gregorian
191  test_double(test_date_, base_date_.Gregorian(), __func__, __LINE__);
192 
193  return pass();
194 }
195 
196 
197 /**********************************************************************/
201 {
202  // Seconds since midnight
203  double test1 = base_date_.MJD() - std::floor(base_date_.MJD());
204  test1 *= CppEphem::sec_per_day();
205  test_double(base_date_.GetSecondsSinceMidnight(0.0), test1, __func__, __LINE__);
206 
207  // Test CurrentJD (that it is in some reasonable range)
208  double test_JD = CEDate::CurrentJD();
209  test_greaterthan(test_JD, 2458485.0, __func__, __LINE__);
210  test_lessthan(test_JD, 2468485.0, __func__, __LINE__);
211 
212  // Test GetTime
213  double test_GetTime = base_date_.GetTime(2.0);
214  double test_GetTime_UTC = base_date_.GetTime_UTC();
215  test_double(test_GetTime, 140000.0, __func__, __LINE__);
216  test_double(test_GetTime_UTC, 120000.0, __func__, __LINE__);
217 
218  // Test DUT1
219  double mjd = base_date_.MJD();
220  double dut1 = CppEphem::dut1(mjd);
221  test_double(base_date_.dut1(), dut1, __func__, __LINE__);
222  test_double(CEDate::dut1(mjd, CEDateType::MJD), dut1, __func__, __LINE__);
223 
224  // Test xp,yp
225  double xpolar = CppEphem::xp(mjd);
226  double ypolar = CppEphem::yp(mjd);
227  test_double(base_date_.xpolar(), xpolar, __func__, __LINE__);
228  test_double(base_date_.ypolar(), ypolar, __func__, __LINE__);
229  test_double(CEDate::xpolar(mjd, CEDateType::MJD), xpolar, __func__, __LINE__);
230  test_double(CEDate::ypolar(mjd, CEDateType::MJD), ypolar, __func__, __LINE__);
231 
232  // UTC -> UT1
233  double test_ut1 = base_date_.MJD() + (base_date_.dut1() / CppEphem::sec_per_day());
234  double ut11, ut12;
235  CEDate::UTC2UT1(base_date_.MJD(), &ut11, &ut12);
236  test_double(ut11, CEDate::GetMJD2JDFactor(), __func__, __LINE__);
237  test_double(ut12, test_ut1, __func__, __LINE__);
238 
239  // UTC -> TT
240  // NOTE: This is not a good test without the correct corrections
241  double test_tt(test_ut1 + 63.8285/DAYSEC);
242  double tt1, tt2;
243  CEDate::UTC2TT(base_date_.MJD(), &tt1, &tt2);
244  test_double(tt1, CEDate::GetMJD2JDFactor(), __func__, __LINE__);
245  test_double(tt2, test_tt, __func__, __LINE__);
246 
247  // UTC -> TDB
248  // NOTE: This is not a good test without the correct corrections
249  double test_tdb(test_tt);
250  double tdb1, tdb2;
251  CEDate::UTC2TDB(base_date_.MJD(), &tdb1, &tdb2);
252  test_double(tdb1, CEDate::GetMJD2JDFactor(), __func__, __LINE__);
253  test_double(tdb2, test_tdb, __func__, __LINE__);
254 
255  return pass();
256 }
257 
258 
259 /**********************************************************************/
262 int main(int argc, char** argv)
263 {
264  test_CEDate tester;
265  return (!tester.runtests());
266 }
test_CEDate::test_support_methods
virtual bool test_support_methods(void)
Test the various.
Definition: test_CEDate.cpp:200
CEDate::GregorianVect2Gregorian
static double GregorianVect2Gregorian(std::vector< double > gregorian)
Helper method for converting from Gregorian vector format to the non-vector format.
Definition: CEDate.cpp:545
test_CEDate::test_constructor
virtual bool test_constructor(void)
Test ability to create a CEDate object.
Definition: test_CEDate.cpp:69
CEDate::GetTime_UTC
virtual double GetTime_UTC() const
Method for getting the current UTC time.
Definition: CEDate.cpp:621
CENamespace.h
test_CEDate::test_ReturnType
virtual bool test_ReturnType(void)
Test ability set the return type.
Definition: test_CEDate.cpp:176
CEDate
Definition: CEDate.h:43
CppEphem::yp
double yp(const double &mjd)
Polar motion (x) for a given modified julian date (radians)
Definition: CENamespace.cpp:160
test_CEDate::test_SetDate_JD
virtual bool test_SetDate_JD(void)
Test ability to set julian date.
Definition: test_CEDate.cpp:103
CEDate::DayFraction
double DayFraction()
Get the Gregorian calendar day fraction.
Definition: CEDate.h:220
test_CEDate::test_date_
CEDate test_date_
Definition: test_CEDate.h:64
test_CEDate::~test_CEDate
virtual ~test_CEDate()
Destructor.
Definition: test_CEDate.cpp:41
CEDate::GetTime
virtual double GetTime(const double &utc_offset=0.0) const
Method for getting the current time.
Definition: CEDate.cpp:607
CppEphem::sec_per_day
double sec_per_day()
Seconds per day.
Definition: CENamespace.h:71
test_CEDate::runtests
virtual bool runtests()
Run tests.
Definition: test_CEDate.cpp:50
CEDate::xpolar
double xpolar(void) const
Polar motion (x) for a given date.
Definition: CEDate.cpp:507
CEDate::UTC2TDB
static void UTC2TDB(const double &mjd, double *tdb1, double *tdb2)
Convert the UTC MJD to TDB JD (useful for planet computations)
Definition: CEDate.cpp:429
CEDate::ypolar
static double ypolar(const double &date, const CEDateType &date_type=CEDateType::JD)
Polar motion (xy for a given date.
Definition: CEDate.cpp:520
CppEphem::julian_date_J2000
double julian_date_J2000()
Julian Date corresponding to J2000.
Definition: CENamespace.h:67
CEDate::GregorianVect2JD
static double GregorianVect2JD(std::vector< double > gregorian)
Gregorian calendar vector formatted date -> Julian date converter.
Definition: CEDate.cpp:307
CEDate::UTC2UT1
static void UTC2UT1(const double &mjd, double *ut11, double *ut12)
Convert the UTC MJD to UT1 JD.
Definition: CEDate.cpp:384
JD
Julian Date.
Definition: CEDate.h:56
CEDate::ypolar
double ypolar(void) const
Polar motion (y) for a given date.
Definition: CEDate.cpp:533
CEDate::GetMJD2JDFactor
static double GetMJD2JDFactor()
Gets the stored SOFA Julian date to Mod Julian date factor 'DJM0'.
Definition: CEDate.h:236
test_CEDate.h
test_CEDate::base_date_
CEDate base_date_
Definition: test_CEDate.h:63
CEDate::GregorianVect2MJD
static double GregorianVect2MJD(std::vector< double > gregorian)
Gregorian calendar vector formatted date -> Modified Julian date converter.
Definition: CEDate.cpp:362
MJD
Modified Julian Date.
Definition: CEDate.h:57
GREGORIAN
Gregorian calendar (year, month, day)
Definition: CEDate.h:58
CEDate::CurrentJD
static double CurrentJD()
Static method for getting the current Julian date.
Definition: CEDate.cpp:632
test_CEDate::test_Gregorian
virtual bool test_Gregorian(void)
Test ability to set Gregorian date.
Definition: test_CEDate.cpp:122
CEDate::Day
int Day()
Get the Gregorian calendar day.
Definition: CEDate.h:210
test_CEDate::test_SetDate_MJD
virtual bool test_SetDate_MJD(void)
Test ability to set modified julian date.
Definition: test_CEDate.cpp:112
CEDate::SetDate
virtual void SetDate(const double &date=CurrentJD(), const CEDateType &time_format=CEDateType::JD)
Set the date based on an actual date and the desired time_format.
Definition: CEDate.cpp:107
CEDate::MJD
virtual double MJD() const
Get the Modified Julian date represented by this object.
Definition: CEDate.h:160
CEDate::xpolar
static double xpolar(const double &date, const CEDateType &date_type=CEDateType::JD)
Polar motion (x) for a given date.
Definition: CEDate.cpp:494
CEDate::GetSecondsSinceMidnight
virtual double GetSecondsSinceMidnight(const double &utc_offset=0.0)
Method for getting the number of seconds since midnight.
Definition: CEDate.cpp:591
CEDate::GregorianVect
virtual std::vector< double > GregorianVect()
Get the Gregorian calendar date formatted as a vector.
Definition: CEDate.h:180
CEDate::Gregorian
virtual double Gregorian() const
Get the Gregorian calendar date formatted as a double.
Definition: CEDate.h:170
CppEphem::dut1
double dut1(const double &mjd)
Return dut1 based on a given modified julian date (seconds)
Definition: CENamespace.cpp:111
CEDate::SetReturnType
void SetReturnType(CEDateType return_type)
Set the return type from the overloaded 'operator double'.
Definition: CEDate.h:247
CEDate::dut1
double dut1(void) const
Return dut1 based on the date represented by this object.
Definition: CEDate.cpp:462
main
int main(int argc, char **argv)
Main method that actually runs the tests.
Definition: test_CEDate.cpp:262
CEDate::UTC2TT
static void UTC2TT(const double &mjd, double *tt1, double *tt2)
Convert the UTC MJD to TT JD.
Definition: CEDate.cpp:405
CEDate::JD
virtual double JD() const
Get the Julian date represented by this object.
Definition: CEDate.h:150
test_CEDate::test_CEDate
test_CEDate()
Default constructor.
Definition: test_CEDate.cpp:29
test_CEDate
Definition: test_CEDate.h:27
CEDate::Year
int Year()
Get the Gregorian calendar year.
Definition: CEDate.h:190
CEDate::dut1
static double dut1(const double &date, const CEDateType &date_type=CEDateType::JD)
Return dut1 based on a given modified date.
Definition: CEDate.cpp:447
CppEphem::xp
double xp(const double &mjd)
Polar motion (x) for a given modified julian date (radians)
Definition: CENamespace.cpp:148
CEDate::Month
int Month()
Get the Gregorian calendar month.
Definition: CEDate.h:200