UTCTime.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: UTCTime.cpp 2457 2010-08-18 14:20:12Z coandrei $"
00002 
00008 //============================================================================
00009 //
00010 //  This file is part of GPSTk, the GPS Toolkit.
00011 //
00012 //  The GPSTk is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published
00014 //  by the Free Software Foundation; either version 2.1 of the License, or
00015 //  any later version.
00016 //
00017 //  The GPSTk is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 //
00022 //  You should have received a copy of the GNU Lesser General Public
00023 //  License along with GPSTk; if not, write to the Free Software Foundation,
00024 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 //  Wei Yan - Chinese Academy of Sciences . 2009, 2010
00027 //
00028 //============================================================================
00029 
00030 #include "UTCTime.hpp"
00031 #include "IERS.hpp"
00032 
00033 
00034 namespace gpstk
00035 {
00036 
00037    using namespace std;
00038 
00039       // Seconds per day.
00040    const double UTCTime::DAY_TO_SECOND = 86400.0; 
00041       
00042       // Constant used for conversion to Terrestrial Time.
00043    const double UTCTime::TT_TAI = 32.184;  
00044       
00045       // Constant used for conversion to GPS time.
00046    const double UTCTime::TAI_GPS = 19.0;
00047 
00048 
00049    DayTime UTCTime::asUT1()
00050    {
00051       DayTime T(this->DOYyear(),this->DOYday(),this->DOYsecond()); // UTC
00052       T += IERS::UT1mUTC(this->mjdUTC());
00053       return T;
00054    }
00055 
00056    DayTime UTCTime::asUTC()
00057    {
00058       DayTime T(this->DOYyear(),this->DOYday(),this->DOYsecond());
00059       return T;
00060    }
00061 
00062    DayTime UTCTime::asTT()
00063    {
00064       DayTime T = (*this).asTAI();
00065       T += TT_TAI;
00066       return T;
00067    }
00068 
00069    DayTime UTCTime::asTDB()
00070    {
00071        
00072       DayTime TT = this->asTT();
00073 
00074       struct MJDTime mjdTT;
00075       mjdTT.MJDint = (long)floor(TT.MJD());
00076       mjdTT.MJDfr = TT.MJD() - mjdTT.MJDint;
00077 
00078       double tdbtdt = 0.0;
00079       double tdbtdtdot = 0.0;
00080       long oldmjd = 0;
00081       long l = 0;
00082 
00083       while ( mjdTT.MJDfr >= 1.0 ) 
00084       {
00085          mjdTT.MJDint++ ;
00086          mjdTT.MJDfr-- ;
00087       }
00088       while ( mjdTT.MJDfr < 0.0 ) 
00089       {
00090          mjdTT.MJDint-- ;
00091          mjdTT.MJDfr++ ;
00092       }
00093 
00094       if ( mjdTT.MJDint != oldmjd ) 
00095       {
00096          oldmjd = mjdTT.MJDint ;
00097          l = oldmjd + 2400001 ;
00098 
00099          tdbtdt = ctatv (l, 0.0) ;
00100          tdbtdtdot = ctatv (l, 0.5) - ctatv (l, -0.5) ;
00101       }
00102 
00103       double TDB_minus_TT = ( tdbtdt + (mjdTT.MJDfr - 0.5) * tdbtdtdot );
00104 
00105       DayTime T = TT;
00106       T += TDB_minus_TT;
00107 
00108       return T;
00109    }
00110 
00111    DayTime UTCTime::asTAI()
00112    {
00113       DayTime T(this->DOYyear(),this->DOYday(),this->DOYsecond()); // UTC
00114       T += IERS::TAImUTC(this->mjdUTC());      // TAI
00115       return T;
00116    }
00117 
00118    DayTime UTCTime::asGPST()
00119    {
00120       DayTime T(this->DOYyear(),this->DOYday(),this->DOYsecond()); // UTC
00121       T += IERS::TAImUTC(this->mjdUTC());   // TAI
00122       T += -UTCTime::TAI_GPS;         // GPST
00123       return T;
00124    }
00125 
00127    DayTime UTCTime::asBDT()
00128    {
00129       DayTime bdt = this->asGPST();
00130       bdt -= 14.0;
00131 
00132       return bdt;
00133    }
00134 
00135    
00136       /*
00137        * Computes the cumulative relativistic time correction to
00138        * earth-based clocks, TDB-TDT, for a given time. Routine
00139        * furnished by the Bureau des Longitudes, modified by
00140        * removal of terms much smaller than 0.1 microsecond.
00141        * @param jdno. Julian day number of lookup
00142        * @param fjdno. Fractional part of Julian day number
00143        * @return Time difference TDB-TDT (seconds)
00144        */
00145    double UTCTime::ctatv(long jdno, double fjdno)
00146    {
00147 
00148       double t, tt, t1, t2, t3, t4, t5, t24, t25, t29, t30, t31 ;
00149 
00150       t = ((jdno-2451545) + fjdno)/(365250.0) ;
00151       tt = t*t ;
00152 
00153       t1  =     1656.674564 * std::sin(  6283.075943033*t + 6.240054195)
00154          +        22.417471 * std::sin(  5753.384970095*t + 4.296977442)
00155          +        13.839792 * std::sin( 12566.151886066*t + 6.196904410)
00156          +         4.770086 * std::sin(   529.690965095*t + 0.444401603)
00157          +         4.676740 * std::sin(  6069.776754553*t + 4.021195093)
00158          +         2.256707 * std::sin(   213.299095438*t + 5.543113262)
00159          +         1.694205 * std::sin(    -3.523118349*t + 5.025132748)
00160          +         1.554905 * std::sin( 77713.772618729*t + 5.198467090)
00161          +         1.276839 * std::sin(  7860.419392439*t + 5.988822341)
00162          +         1.193379 * std::sin(  5223.693919802*t + 3.649823730)
00163          +         1.115322 * std::sin(  3930.209696220*t + 1.422745069)
00164          +         0.794185 * std::sin( 11506.769769794*t + 2.322313077)
00165          +         0.600309 * std::sin(  1577.343542448*t + 2.678271909)
00166          +         0.496817 * std::sin(  6208.294251424*t + 5.696701824)
00167          +         0.486306 * std::sin(  5884.926846583*t + 0.520007179)
00168          +         0.468597 * std::sin(  6244.942814354*t + 5.866398759)
00169          +         0.447061 * std::sin(    26.298319800*t + 3.615796498)
00170          +         0.435206 * std::sin(  -398.149003408*t + 4.349338347)
00171          +         0.432392 * std::sin(    74.781598567*t + 2.435898309)
00172          +         0.375510 * std::sin(  5507.553238667*t + 4.103476804) ;
00173 
00174       t2  =        0.243085 * std::sin(  -775.522611324*t + 3.651837925)
00175          +         0.230685 * std::sin(  5856.477659115*t + 4.773852582)
00176          +         0.203747 * std::sin( 12036.460734888*t + 4.333987818)
00177          +         0.173435 * std::sin( 18849.227549974*t + 6.153743485)
00178          +         0.159080 * std::sin( 10977.078804699*t + 1.890075226)
00179          +         0.143935 * std::sin(  -796.298006816*t + 5.957517795)
00180          +         0.137927 * std::sin( 11790.629088659*t + 1.135934669)
00181          +         0.119979 * std::sin(    38.133035638*t + 4.551585768)
00182          +         0.118971 * std::sin(  5486.777843175*t + 1.914547226)
00183          +         0.116120 * std::sin(  1059.381930189*t + 0.873504123)
00184          +         0.101868 * std::sin( -5573.142801634*t + 5.984503847)
00185          +         0.098358 * std::sin(  2544.314419883*t + 0.092793886)
00186          +         0.080164 * std::sin(   206.185548437*t + 2.095377709)
00187          +         0.079645 * std::sin(  4694.002954708*t + 2.949233637)
00188          +         0.075019 * std::sin(  2942.463423292*t + 4.980931759)
00189          +         0.064397 * std::sin(  5746.271337896*t + 1.280308748)
00190          +         0.063814 * std::sin(  5760.498431898*t + 4.167901731)
00191          +         0.062617 * std::sin(    20.775395492*t + 2.654394814)
00192          +         0.058844 * std::sin(   426.598190876*t + 4.839650148)
00193          +         0.054139 * std::sin( 17260.154654690*t + 3.411091093) ;
00194 
00195       t3  =        0.048373 * std::sin(   155.420399434*t + 2.251573730)
00196          +         0.048042 * std::sin(  2146.165416475*t + 1.495846011)
00197          +         0.046551 * std::sin(    -0.980321068*t + 0.921573539)
00198          +         0.042732 * std::sin(   632.783739313*t + 5.720622217)
00199          +         0.042560 * std::sin(161000.685737473*t + 1.270837679)
00200          +         0.042411 * std::sin(  6275.962302991*t + 2.869567043)
00201          +         0.040759 * std::sin( 12352.852604545*t + 3.981496998)
00202          +         0.040480 * std::sin( 15720.838784878*t + 2.546610123)
00203          +         0.040184 * std::sin(    -7.113547001*t + 3.565975565)
00204          +         0.036955 * std::sin(  3154.687084896*t + 5.071801441)
00205          +         0.036564 * std::sin(  5088.628839767*t + 3.324679049)
00206          +         0.036507 * std::sin(   801.820931124*t + 6.248866009)
00207          +         0.034867 * std::sin(   522.577418094*t + 5.210064075)
00208          +         0.033529 * std::sin(  9437.762934887*t + 2.404714239)
00209          +         0.033477 * std::sin(  6062.663207553*t + 4.144987272)
00210          +         0.032438 * std::sin(  6076.890301554*t + 0.749317412)
00211          +         0.032423 * std::sin(  8827.390269875*t + 5.541473556)
00212          +         0.030215 * std::sin(  7084.896781115*t + 3.389610345)
00213          +         0.029862 * std::sin( 12139.553509107*t + 1.770181024)
00214          +         0.029247 * std::sin(-71430.695617928*t + 4.183178762) ;
00215 
00216       t4  =        0.028244 * std::sin( -6286.598968340*t + 5.069663519)
00217          +         0.027567 * std::sin(  6279.552731642*t + 5.040846034)
00218          +         0.025196 * std::sin(  1748.016413067*t + 2.901883301)
00219          +         0.024816 * std::sin( -1194.447010225*t + 1.087136918)
00220          +         0.022567 * std::sin(  6133.512652857*t + 3.307984806)
00221          +         0.022509 * std::sin( 10447.387839604*t + 1.460726241)
00222          +         0.021691 * std::sin( 14143.495242431*t + 5.952658009)
00223          +         0.020937 * std::sin(  8429.241266467*t + 0.652303414)
00224          +         0.020322 * std::sin(   419.484643875*t + 3.735430632)
00225          +         0.017673 * std::sin(  6812.766815086*t + 3.186129845)
00226          +         0.017806 * std::sin(    73.297125859*t + 3.475975097)
00227          +         0.016155 * std::sin( 10213.285546211*t + 1.331103168)
00228          +         0.015974 * std::sin( -2352.866153772*t + 6.145309371)
00229          +         0.015949 * std::sin(  -220.412642439*t + 4.005298270)
00230          +         0.015078 * std::sin( 19651.048481098*t + 3.969480770)
00231          +         0.014751 * std::sin(  1349.867409659*t + 4.308933301)
00232          +         0.014318 * std::sin( 16730.463689596*t + 3.016058075)
00233          +         0.014223 * std::sin( 17789.845619785*t + 2.104551349)
00234          +         0.013671 * std::sin(  -536.804512095*t + 5.971672571)
00235          +         0.012462 * std::sin(   103.092774219*t + 1.737438797) ;
00236 
00237       t5  =        0.012420 * std::sin(  4690.479836359*t + 4.734090399)
00238          +         0.011942 * std::sin(  8031.092263058*t + 2.053414715)
00239          +         0.011847 * std::sin(  5643.178563677*t + 5.489005403)
00240          +         0.011707 * std::sin( -4705.732307544*t + 2.654125618)
00241          +         0.011622 * std::sin(  5120.601145584*t + 4.863931876)
00242          +         0.010962 * std::sin(     3.590428652*t + 2.196567739)
00243          +         0.010825 * std::sin(   553.569402842*t + 0.842715011)
00244          +         0.010396 * std::sin(   951.718406251*t + 5.717799605)
00245          +         0.010453 * std::sin(  5863.591206116*t + 1.913704550)
00246          +         0.010099 * std::sin(   283.859318865*t + 1.942176992)
00247          +         0.009858 * std::sin(  6309.374169791*t + 1.061816410)
00248          +         0.009963 * std::sin(   149.563197135*t + 4.870690598)
00249          +         0.009370 * std::sin(149854.400135205*t + 0.673880395) ;
00250 
00251       t24 = t * (102.156724 * std::sin(  6283.075849991*t + 4.249032005)
00252          +         1.706807 * std::sin( 12566.151699983*t + 4.205904248)
00253          +         0.269668 * std::sin(   213.299095438*t + 3.400290479)
00254          +         0.265919 * std::sin(   529.690965095*t + 5.836047367)
00255          +         0.210568 * std::sin(    -3.523118349*t + 6.262738348)
00256          +         0.077996 * std::sin(  5223.693919802*t + 4.670344204) ) ;
00257 
00258       t25 = t * (  0.059146 * std::sin(    26.298319800*t + 1.083044735)
00259          +         0.054764 * std::sin(  1577.343542448*t + 4.534800170)
00260          +         0.034420 * std::sin(  -398.149003408*t + 5.980077351)
00261          +         0.033595 * std::sin(  5507.553238667*t + 5.980162321)
00262          +         0.032088 * std::sin( 18849.227549974*t + 4.162913471)
00263          +         0.029198 * std::sin(  5856.477659115*t + 0.623811863)
00264          +         0.027764 * std::sin(   155.420399434*t + 3.745318113)
00265          +         0.025190 * std::sin(  5746.271337896*t + 2.980330535)
00266          +         0.024976 * std::sin(  5760.498431898*t + 2.467913690)
00267          +         0.022997 * std::sin(  -796.298006816*t + 1.174411803)
00268          +         0.021774 * std::sin(   206.185548437*t + 3.854787540)
00269          +         0.017925 * std::sin(  -775.522611324*t + 1.092065955)
00270          +         0.013794 * std::sin(   426.598190876*t + 2.699831988)
00271          +         0.013276 * std::sin(  6062.663207553*t + 5.845801920)
00272          +         0.012869 * std::sin(  6076.890301554*t + 5.333425680)
00273          +         0.012152 * std::sin(  1059.381930189*t + 6.222874454)
00274          +         0.011774 * std::sin( 12036.460734888*t + 2.292832062)
00275          +         0.011081 * std::sin(    -7.113547001*t + 5.154724984)
00276          +         0.010143 * std::sin(  4694.002954708*t + 4.044013795)
00277          +         0.010084 * std::sin(   522.577418094*t + 0.749320262)
00278          +         0.009357 * std::sin(  5486.777843175*t + 3.416081409) ) ;
00279 
00280       t29 = tt * ( 0.370115 * std::sin(                     4.712388980)
00281          +         4.322990 * std::sin(  6283.075849991*t + 2.642893748)
00282          +         0.122605 * std::sin( 12566.151699983*t + 2.438140634)
00283          +         0.019476 * std::sin(   213.299095438*t + 1.642186981)
00284          +         0.016916 * std::sin(   529.690965095*t + 4.510959344)
00285          +         0.013374 * std::sin(    -3.523118349*t + 1.502210314) ) ;
00286 
00287       t30 = t * tt * 0.143388 * std::sin( 6283.075849991*t + 1.131453581) ;
00288 
00289       return (t1+t2+t3+t4+t5+t24+t25+t29+t30) * 1.0e-6 ;
00290 
00291    }  // End of method 'UTCTime::ctatv()'
00292 
00293 
00294    double UTCTime::xPole()
00295    {
00296       return IERS::xPole( this->mjdUTC() );
00297    }
00298 
00299    double UTCTime::yPole()
00300    {
00301       return IERS::yPole( this->mjdUTC() );
00302    }
00303 
00304    double UTCTime::UT1mUTC()
00305    {
00306       return IERS::UT1mUTC( this->mjdUTC() );
00307    }
00308 
00309    double UTCTime::TAImUTC()
00310    {
00311       return IERS::TAImUTC( this->mjdUTC() );
00312    }
00313 
00314    double UTCTime::GPSTmUTC()
00315    {
00316       return (this->asGPST()-this->asUTC());
00317    }
00318       
00319 
00320    void UTCTime::test()
00321    {
00322       
00323       cout<<"... testing UTCTime ..."<<endl;
00324 
00325       IERS::loadIERSFile("finals.data");
00326       
00327       UTCTime utc(short(2002),short(1),short(1),short(0),short(0),0.0);
00328 
00329       cout << "UTC "<< utc << endl
00330           << "UT1 "<< utc.asUT1()<< endl
00331           << "TT  "<< utc.asTT()   << endl
00332           << "TAI "<< utc.asTAI()<< endl
00333           << "TDB "<< utc.asTDB()<< endl
00334           << "GPST"<< utc.asGPST()<<endl;
00335       
00336       cout << "TAI-GPST "<<utc.asTAI()-utc.asGPST()<<endl;
00337       cout << "TT-TAI   "<<utc.asTT()-utc.asTAI()<<endl;
00338       cout << "UTC-TAI  "<<utc.asUTC()-utc.asTAI()<<endl;
00339       cout << "UT1-UTC  "<<utc.asUT1()-utc.asUTC()<<endl;
00340       cout << "TDB-TT   "<<utc.asTDB()-utc.asTT()<<endl;
00341 
00342       cout << "It's seems to be ok !" <<endl;
00343       
00344       DayTime utc2;
00345       DayTime gpst1;
00346       UTC2TT(utc,gpst1);
00347       TT2UTC(gpst1,utc2);
00348       
00349       cout<<setw(20)<<setprecision(8)<<utc2-utc<<endl;
00350       
00351       int a = 0;
00352 
00353    }
00354 
00355    
00356       // GPS time to UTC time
00357       // @param gpst    GPST as input 
00358       // @param utc     UTC as output
00359    void GPST2UTC(const DayTime& gpst, DayTime& utc)
00360    {
00361       UTCTime T;
00362 
00363       double mjdGPST = gpst.MJD();
00364       double mjdTAI = mjdGPST+ UTCTime::TAI_GPS/UTCTime::DAY_TO_SECOND;
00365 
00366          // input should be utc
00367       double dtu = IERS::TAImUTC(mjdTAI);               
00368       double mjdUTC = mjdTAI - dtu / UTCTime::DAY_TO_SECOND;
00369 
00370       dtu = IERS::TAImUTC(mjdUTC);
00371       mjdUTC = mjdTAI - dtu / UTCTime::DAY_TO_SECOND;      
00372 
00373       utc = gpst;                           // GPST
00374       utc += UTCTime::TAI_GPS;            // TAI
00375       utc += -IERS::TAImUTC(mjdUTC);      // UTC
00376 
00377    }
00378       // UTC time to GPS time 
00379       // @param utc    UTC as input 
00380       // @param gpst   GPST as output 
00381    void UTC2GPST(const DayTime& utc, DayTime& gpst)
00382    {
00383       UTCTime T(utc.DOYyear(), utc.DOYday(), utc.DOYsecond());
00384       gpst = T.asGPST();
00385       
00386    }
00387 
00388       // UT1 time to UTC time
00389       // @param ut1     UT1 as input 
00390       // @param utc     UTC as output 
00391    void UT12UTC(const DayTime& ut1, DayTime& utc)
00392    {
00393       UTCTime T(ut1.DOYyear(), ut1.DOYday(), ut1.DOYsecond());
00394       
00395       T -= IERS::UT1mUTC(ut1.MJD());   // input should be utc
00396       double mjdUTC = T.MJD();
00397 
00398       T.setYDoySod(ut1.DOYyear(), ut1.DOYday(), ut1.DOYsecond());
00399       T -= IERS::UT1mUTC(mjdUTC);
00400       
00401       utc = ut1;
00402       utc -= IERS::UT1mUTC(T.MJD());
00403    }
00404 
00405       // UTC time to UT1 time
00406       // @param utc     UTC as input 
00407       // @param ut1     UT1 as output
00408    void UTC2UT1(const DayTime& utc, DayTime& ut1)
00409    {
00410       UTCTime T(utc.DOYyear(), utc.DOYday(), utc.DOYsecond());
00411       ut1 = T.asUT1();
00412    }
00413 
00414       // TT time to UTC time
00415       // @param tt      TT as input 
00416       // @param utc     UTC as output 
00417    void TT2UTC(const DayTime& tt, DayTime& utc)
00418    {
00419       UTCTime T(tt.DOYyear(),tt.DOYday(),tt.DOYsecond());
00420       
00421       DayTime TAI  = tt;          // TT
00422       TAI -= UTCTime::TT_TAI;       // TAI
00423 
00424       utc = TAI;
00425       utc -= IERS::TAImUTC(TAI.MJD()); // input should be UTC     
00426       
00427       double mjdUTC = utc.MJD();
00428 
00429       utc = TAI;
00430       utc -= IERS::TAImUTC(mjdUTC);
00431 
00432       mjdUTC = utc.MJD();
00433 
00434       utc = TAI;
00435       utc -= IERS::TAImUTC(mjdUTC);
00436    }
00437 
00438       // UTC time to TT time
00439       // @param UTC     UTC as input 
00440       // @param tt      TT as output 
00441    void UTC2TT(const DayTime& utc, DayTime& tt)
00442    {
00443       UTCTime T(utc.DOYyear(),utc.DOYday(),utc.DOYsecond());
00444       tt = T.asTT();
00445    }
00446 
00447       // TAI time to UTC time
00448       // @param tai     TAI as input 
00449       // @param utc     UTC as output 
00450    void TAI2UTC(const DayTime& tai, DayTime& utc)
00451    {
00452       UTCTime T(tai.DOYyear(),tai.DOYday(),tai.DOYsecond());
00453       
00454       utc = tai;
00455       utc -= IERS::TAImUTC(tai.MJD()); // input should be UTC     
00456 
00457       double mjdUTC = utc.MJD();
00458 
00459       utc = tai;
00460       utc -= IERS::TAImUTC(mjdUTC);
00461 
00462       mjdUTC = utc.MJD();
00463 
00464       utc = tai;
00465       utc -= IERS::TAImUTC(mjdUTC);
00466    }
00467    
00468       // TAI time to UTC time
00469       // @param utc     UTC as input 
00470       // @param tai     TAI as output 
00471    void UTC2TAI(const DayTime& utc, DayTime& tai)
00472    {
00473       UTCTime T(utc.DOYyear(),utc.DOYday(),utc.DOYsecond());
00474       tai = T.asTAI();
00475    }
00476 
00477 
00478       // BDT time to UTC time
00479       // @param bdt     BDT as input 
00480       // @param utc     UTC as output 
00481    void BDT2UTC(const DayTime& bdt, DayTime& utc)
00482    {
00483       DayTime gpst(bdt);
00484       gpst += 14.0;
00485 
00486       GPST2UTC(gpst, utc);
00487    }
00488 
00489       // UTC time to BDT time
00490       // @param utc     UTC as input 
00491       // @param bdt     BDT as output 
00492    void UTC2BDT(const DayTime& utc, DayTime& bdt)
00493    {
00494       UTC2GPST(utc,bdt);  
00495       bdt -= 14.0;
00496    }
00497 
00498 
00499 }  // End of namespace 'gpstk'
00500 
00501 

Generated on Tue May 22 03:31:02 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1