NCFS-Pack
A generic (astro)particle physics analysis framework
Loading...
Searching...
No Matches
NcTimestamp.cxx
Go to the documentation of this file.
1
31
33
79// by the introduction of Leap Seconds when abs(UT1-UTC) exceeds 0.9 sec.
80// This time synchronisation is coordinated by the International Earth Rotation and
81// Reference Systems Service (IERS) via a daily monitoring of the Earth Orientation
82// Parameters (EOP).
83// Depending on dUT=UT1-UTC, these Leap Seconds can be positive or negative.
84// The introduction of Leap Seconds into UTC started at 01-jan-1972 00:00:00 UT.
85// In 2026 the introduction of Leap Seconds in UTC was abandoned, which implies that
86// starting from 2027 the value of abs(UT1-UTC) may exceed 0.9 sec.
87//
88// An overview of the history of introduced Leap seconds (TAI-UTC) is online available at :
89// https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat
90// or http://maia.usno.navy.mil/ser7/tai-utc.dat
91//
92// The time difference dUT=UT1-UTC is monitored on a daily basis and the data are available at :
93// https://hpiers.obspm.fr/iers/series/opa/eopc04
94// or http://maia.usno.navy.mil/ser7/ser7.dat
95//
96// The accuracy of dUT=UT1-UTC is about 10 microseconds, so in case of accurate astronomical
97// timing the user is advised to specify dUT in the various Set() facilities or use SetUT().
98// An automatic setting of dUT is provided based on the IERS data files.
99// Please refer to the member function LoadUTCparameterFiles() for further details.
101// 4) In some cases Unix Time (also called POSIX Time or UNIX Epoch Time) is used.
102// Unix Time is closely releated to UTC and represents the (fractional) elapsed second count
103// since the start of the Unix Epoch, in which every Unix Time day contains exactly 86400 seconds.
104// This implies that for Unix Time the UTC leap seconds should be ignored, as explained below.
105// The UNIX Time EPOCH starts at 01-jan-1970 00:00:00 UTC which corresponds to JD=2440587.5
106// (i.e. the start of MJD=40587, as explained in the discussion on Julian Date below).
107// Synchronization with UTC is obtained by continuing the second count when a UTC leap second
108// occurs and then the Unix Time count jumps up or down 1 second at the beginning of the new
109// day after the UTC leap second has expired.
110// In case of a negative UTC leap second (which has not occurred so far) this would result
111// in a jump forward of 1 second in Unix Time, introducing a "gap" of 1 second in the timing
112// at the start of the new day.
113// In case of a positive UTC leap second this results in a jump back of 1 second in Unix Time
114// at the start of a new day.
115// In this case there exist 2 ambiguous Unix Times over a 1 second time interval,
116// namely at the beginning of the introduction of the UTC leap second and at the moment that
117// the UTC leap second expires, i.e. the start of the new day.
118// This NcTimestamp facility provides setting and retrieval of Unix Time, but for accurate
119// timing the user is advised to use one of the time scales mentioned below.
120//
121// Supported time scales :
122// ----------------------
123// UT : Universal Time.
124// 1 day is the time span between two successive solar meridian transitions.
125// This implies that UT is based on the actual rotation of the Earth and as such suited
126// for (synchronization of) astronomical observations at various locations on Earth.
127// The reference time is defined by transitions over the meridian at Greenwich.
128// ST : Siderial Time (see also the details below).
129// 1 day is the time span between two successive stellar meridian transitions.
130// This implies that ST is based on the actual rotation of the Earth and its rotation
131// around the Sun. Siderial Time is very well suited for astronomical observations.
132// The reference time is defined by transitions over the meridian at Greenwich.
133// Because of the stability of the rotation of the Earth around the Sun, the Siderial Time
134// is derived from the Universal Time and as such not treated as a separate time scale.
135// TAI : International Atomic Time (Temps Atomique International).
136// 1 day is the time span of 86400 standard atomic (SI) seconds at average sea level.
137// The standard atomic (SI) second is derived from a set of Cs atomic clocks.
138// This implies that TAI is not related to the actual rotation of the Earth and as such
139// will not run at the same pace as UT.
140// The start epoch of TAI is 01-jan-1958 00:00:00 UT, at which time UT1-UTC was about 0.
141// GPS : Global Positioning System time.
142// This satellite based timing system is based on TAI and broadcast via a satellite network.
143// The start epoch of GPS is 06-jan-1980 00:00:00 UTC, at which the number of Leap Seconds was 19.
144// This implies that at that time TAI=UTC+19 sec. and consequently we always have TAI=GPS+19 sec.
145// GPS is broadcast in two formats : (w,sow) and (w,dow,sod) with
146// w = week number after the start epoch. 1 week=7 days or 1 week=604800 seconds.
147// sow = (fractional) second count in the current week
148// dow = day number in the current week (sunday=0, monday=1 etc.)
149// sod = (fractional) second count in the current day
150// Early implementations used to reset the week count after a cycle of 1024 weeks and
151// provided the corresponding cycle count. Please refer to the SetGPS() memberfunctions
152// for further details.
153// TT : Terrestrial Time.
154// This timing system is based on TAI and provides the date/time at average sea level.
155// It has been introduced for observations from the surface of the Earth, to be consistent
156// with General Relativity. Since planetary orbits are very stable in time and not related
157// to the rotation of the Earth, TT is mainly used for observations of the Solar system.
158// TT was synchronized with TAI at 01-jan-1977 00:00:00 TAI, to indicate 00:00:32.184 in
159// order to provide a continuation of its (now obsolete) predecessor Ephemeris Time (ET).
160// This implies that always TT=TAI+32.184 sec.
161//
162// All Gregorian dates/times (e.g. 12-aug-1982 13:20:12) are treated on basis of
163// the time scales mentioned above. So, there is no need for leap second treatment
164// like this is the case for Coordinated Universal Time (UTC).
165//
166// In order to enable a precise measurement of elapsed time over (very) long periods,
167// a continuous day counting system has been introduced, called the Julian Date.
168// The Julian Date (JD) indicates the number of days since noon (12:00:00) on
169// 01 jan -4712 (i.e. noon 01 jan 4713 BC), being day 0 of the Julian calendar.
170// It is custom to couple the Julian Date to UT so that it serves astronomical observations.
171// However, this NcTimestamp facility allows to use a continuous day counting system
172// with all the supported time scales mentioned above.
173//
174// The Modified Julian Date (MJD) indicates the number of days since midnight (00:00:00)
175// on 17-nov-1858, which corresponds to 2400000.5 days after day 0 of the
176// Julian calendar.
177//
178// The Truncated Julian Date (TJD) corresponds to 2440000.5 days after day 0
179// of the Julian calendar and consequently TJD=MJD-40000.
180// This TJD date indication was used by the Vela and CGRO satellite missions in
181// view of Gamma Ray Burst investigations.
182//
183// The Julian Epoch (JE) indicates the fractional elapsed Julian year count
184// since the start of the Gregorian year count.
185// A Julian year is defined to be 365.25 days and starts at 01-jan 12:00:00.
186// As such, the integer part of JE corresponds to the usual Gregorian year count,
187// apart from 01-jan before 12:00:00.
188// So, 01-jan-1965 12:00:00 UT corresponds to JE=1965.0
189//
190// The Besselian Epoch (BE) indicates the fractional elapsed Besselian year count
191// since the start of the Gregorian year count.
192// A Besselian (or tropical) year is defined to be 365.242198781 days.
193// The date 31-dec-1949 22:09:46.862 UT corresponds to BE=1950.0
194//
195// The Besselian and Julian epochs are used in astronomical catalogs
196// to denote values of time varying observables like e.g. right ascension.
197//
198// Because of the fact that the Julian date indicators are all w.r.t. UT or TAI derived
199// time scales, they provide an absolute time scale irrespective of timezone or daylight
200// saving time (DST).
201//
202// In view of astronomical observations and positioning it is convenient
203// to have also a UT equivalent related to stellar meridian transitions.
204// This is achieved by the Greenwich Sidereal Time (GST).
205// The GST is defined as the right ascension of the objects passing
206// the meridian over Greenwich.
207// Due to the rotation of the Earth around the Sun, a sidereal day
208// lasts 86164.09 seconds (23h 56m 04.09s) compared to the mean solar
209// day of 86400 seconds (24h).
210// Furthermore, precession of the earth's spin axis results in the fact
211// that the zero point of right ascension (vernal equinox) gradually
212// moves along the celestial equator.
213// In addition, tidal friction and ocean and atmospheric effects will
214// induce seasonal variations in the earth's spin rate and polar motion
215// of the earth's spin axis.
216// Taking the above effects into account leads to what is called
217// the Greenwich Mean Sidereal Time (GMST).
218// In case also the nutation of the earth's spin axis is taken into
219// account we speak of the Greenwich Apparent Sidereal Time (GAST).
220//
221// This NcTimestamp facility allows for picosecond precision, in view
222// of time of flight analyses for particle physics experiments.
223// For normal date/time indication the standard nanosecond precision
224// will in general be sufficient.
225// Picosecond precision can be obtained by invokation of GetPs() or GetDifference().
226// Note that when the fractional JD, MJD and TJD counts are used instead
227// of the integer (days,sec,ns) specification, the nanosecond precision
228// may be lost due to computer accuracy w.r.t. floating point operations.
229//
230// The TTimeStamp EPOCH starts at 01-jan-1970 00:00:00
231// which corresponds to JD=2440587.5 or the start of MJD=40587 or TJD=587.
232// Using the corresponding MJD of this EPOCH allows construction of
233// the yy-mm-dd hh:mm:ss:ns TTimeStamp from a given input (M/T)JD and time.
234// Obviously this TTimeStamp implementation would prevent usage of values
235// smaller than JD=2440587.5 or MJD=40587 or TJD=587.
236// Furthermore, due to a limitation on the "seconds since the EPOCH start" count
237// in TTimeStamp, the latest accessible date/time is until 19-jan-2038 02:14:07.
238// However, this NcTimestamp facility provides support for the full range
239// of (M/T)JD values, but the setting of the corresponding TTimeStamp parameters
240// is restricted to the values allowed by the TTimeStamp implementation.
241// For these earlier/later (M/T)JD values, the standard TTimeStamp parameters will
242// be set corresponding to the start of the TTimeStamp EPOCH.
243// This implies that for these earlier/later (M/T)JD values the TTimeStamp parameters
244// do not match the Julian parameters of NcTimestamp.
245// As such the standard TTimeStamp parameters do not appear on the print output
246// when invoking the Date() memberfunction for these earlier/later (M/T)JD values.
247//
248// Examples :
249// ==========
250//
251// Note : All TTimeStamp functionality is available as well.
252//
253// NcTimestamp t;
254//
255// t.Date();
256//
257// // Set a specific Date/Time in Universal Time (UT1)
258// // without recording UTC nor the corresponding International Atomic Time (TAI)
259// t.SetUT("22-08-2016","14:03:29.7358",0,"U");
260//
261// // Set a specific Date/Time in Universal Time (UTC)
262// // without recording UT1 nor the corresponding International Atomic Time (TAI)
263// t.SetUT("22-08-2016","14:03:29.7358",0,"N");
264//
265// // Set a specific Date/Time in Universal Time (UTC)
266// // and also record UT1 and the corresponding International Atomic Time (TAI)
267// // using manual "leap" and "dut" settings
268// Int_t leap=34;
269// Double_t dut=-0.14;
270// t.SetUT("17-05-2011","05:43:18.31468",0,"M",leap,dut);
271//
272// // Set a specific Date/Time in Universal Time (UTC)
273// // and also record UT1 and the corresponding International Atomic Time (TAI)
274// // using automatic "leap" and "dut" settings from the IERS data files
275// t.LoadUTCparameterFiles("leap.txt","dut.txt");
276// t.SetUT("17-05-2011","05:43:18.31468",0,"A");
277//
278// // Set a specific Date/Time in Global Positioning System time (GPS)
279// // and also record the corresponding Universal Times (UTC and UT1) and TAI
280// // using automatic "leap" and "dut" settings from the IERS data files
281// t.LoadUTCparameterFiles("leap.txt","dut.txt");
282// t.SetTAI("GPS","17-05-2011","05:43:18.31468",0,"A",0,0);
283//
284// // Retrieve Julian Date to ns precision
285// Int_t jd,jsec,jns;
286// t.GetJD(jd,jsec,jns);
287// // Get the remaining ps precision
288// Int_t ps=GetPs();
289//
290// // Retrieve fractional Truncated Julian Date
291// Double_t tjd=t.GetTJD();
292//
293// // Retrieve fractional Julian Epoch
294// Double_t je=t.GetJE();
295//
296// // Set to a specific Modified Julian Date representing UTC
297// // without recording the corresponding International Atomic Time (TAI)
298// Int_t mjd=50537;
299// Int_t mjsec=1528;
300// Int_t mjns=185643;
301// Int_t mjps=35;
302// t.SetMJD(mjd,mjsec,mjns,mjps,"N");
303//
304// t.Date();
305//
306// // Set to a specific Modified Julian Date representing UTC
307// // and also record the corresponding International Atomic Time (TAI)
308// // using automatic "leap" and "dut" settings from the IERS data files
309// Int_t mjd=58457;
310// Int_t mjsec=1528;
311// Int_t mjns=185643;
312// Int_t mjps=35;
313// t.LoadUTCparameterFiles("leap.txt","dut.txt");
314// t.SetMJD(mjd,mjsec,mjns,mjps,"A");
315//
316// t.Date();
317//
318// // Set to a specific Modified Julian Date representing UT1
319// // and also record the corresponding International Atomic Time (TAI)
320// // using automatic "leap" and "dut" settings from the IERS data files
321// Int_t mjd=58457;
322// Int_t mjsec=1528;
323// Int_t mjns=185643;
324// Int_t mjps=35;
325// t.LoadUTCparameterFiles("leap.txt","dut.txt");
326// t.SetMJD(mjd,mjsec,mjns,mjps,"U");
327//
328// t.Date();
329//
330// // Time intervals for e.g. Trigger or Time Of Flight analysis
331// NcEvent evt;
332// NcTrack* tx=evt.GetTrack(5);
333// NcTimestamp* timex=tx->GetTimestamp();
334// Double_t dt=evt.GetDifference(timex,"ps");
335// NcTimestamp trig((NcTimestamp)evt);
336// trig.Add(0,0,2,173);
337// NcSignal* sx=evt.GetHit(23);
338// NcTimestamp* timex=sx->GetTimestamp();
339// Double_t dt=trig.GetDifference(timex,"ps");
340// Int_t d,s,ns,ps;
341// trig.GetDifference(timex,d,s,ns,ps);
342//
343// // Some practical conversion facilities
344// // Note : They don't influence the actual date/time settings
345// // and as such can also be invoked as NcTimestamp::Convert(...) etc...
346// Int_t y=1921;
347// Int_t m=7;
348// Int_t d=21;
349// Int_t hh=15;
350// Int_t mm=23;
351// Int_t ss=47;
352// Int_t ns=811743;
353// Double_t jdate=t.GetJD(y,m,d,hh,mm,ss,ns);
354//
355// Int_t days,secs,nsecs;
356// Double_t date=421.1949327;
357// t.Convert(date,days,secs,nsecs);
358//
359// days=875;
360// secs=23;
361// nsecs=9118483;
362// date=t.Convert(days,secs,nsecs);
363//
364// Double_t mjdate=40563.823744;
365// Double_t epoch=t.GetJE(mjdate,"mjd");
366//
367//--- Author: Nick van Eijndhoven 28-jan-2005 Utrecht University
368//- Modified: Nick van Eijndhoven, IIHE-VUB Brussel, UTC November 22, 2026 12:45
369~~~
370**/
372
373#include "NcTimestamp.h"
374#include "Riostream.h"
375
376ClassImp(NcTimestamp); // Class implementation to enable ROOT I/O
377
380{
389
390 FillJulian();
391 fJps=0;
392 fUtc=0;
393 fLeap=0;
394 fDut=0;
395 fTmjd=0;
396 fTsec=0;
397 fTns=0;
398 fTps=0;
399 fUTCdata=0;
400}
401
402NcTimestamp::NcTimestamp(TTimeStamp& t) : TTimeStamp(t)
403{
410
411 FillJulian();
412 fJps=0;
413 fUtc=0;
414 fLeap=0;
415 fDut=0;
416 fTmjd=0;
417 fTsec=0;
418 fTns=0;
419 fTps=0;
420 fUTCdata=0;
421}
422
424{
430
431 if (fUTCdata)
432 {
433 delete fUTCdata;
434 fUTCdata=0;
435 }
436}
437
438NcTimestamp::NcTimestamp(const NcTimestamp& t) : TTimeStamp(t)
439{
445
446 fMJD=t.fMJD;
447 fJsec=t.fJsec;
448 fJns=t.fJns;
449 fJps=t.fJps;
450 fCalcs=t.fCalcs;
452 fUtc=t.fUtc;
453 fLeap=t.fLeap;
454 fDut=t.fDut;
455 fTmjd=t.fTmjd;
456 fTsec=t.fTsec;
457 fTns=t.fTns;
458 fTps=t.fTps;
459 fUTCdata=0;
460 TTree* tx=t.fUTCdata;
461 if (tx) fUTCdata=(TTree*)tx->Clone();
462}
463
465{
471
472 NcTimestamp ts(t);
473
474 fUTCdata=0;
475 TTree* tx=ts.fUTCdata;
476 if (tx) fUTCdata=(TTree*)tx->Clone();
477
478 Int_t mjd=0;
479 Int_t sec=0;
480 Int_t ns=0;
481 Int_t ps=0;
482 Int_t leap=0;
483 Double_t dut=0;
484
485 ts.GetMJD(mjd,sec,ns);
486 ps=ts.fJps;
487
488 Int_t flag=ts.GetUTCparameters(leap,dut);
489
490 TString utc="N";
491 if (flag==1) utc="M";
492 if (flag==-1) utc="A";
493 if (flag==-2) utc="U";
494 if (flag==-3) utc="U";
495
496 SetMJD(mjd,sec,ns,ps,utc,leap,dut);
497
498 // Correct for double setting of dut
499 if (utc=="A" || utc=="M") AddSec(-dut);
500
501 return *this;
502}
503
504void NcTimestamp::Date(Int_t mode,Double_t offset)
505{
528
529 Int_t mjd,mjsec,mjns;
530 GetMJD(mjd,mjsec,mjns);
531
532 TString month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
533 TString day[7]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
534 UInt_t y,m,d,wd;
535 Int_t hh,mm,ss,ns,ps;
536 Double_t gat,gast;
537 Bool_t date=kFALSE;
538
539 // The UT date and time
540 if (abs(mode)==1 || abs(mode)==3)
541 {
542 if (mjd>=40587 && (mjd<65442 || (mjd==65442 && mjsec<8047)))
543 {
544 GetDate(kTRUE,0,&y,&m,&d);
545 wd=GetDayOfWeek(kTRUE,0);
546 printf(" %-s, %02d %-s %-d ",day[wd-1].Data(),d,month[m-1].Data(),y);
547 date=kTRUE;
548 }
549 else
550 {
551 cout << " Time ";
552 date=kFALSE;
553 }
554 GetUT(hh,mm,ss,ns,ps);
555 printf("%02d:%02d:%02d.%09d%03d",hh,mm,ss,ns,ps);
556 if (!fUtc)
557 {
558 cout << " (UTC) ";
559 }
560 else
561 {
562 cout << " (UT1) ";
563 }
564
565 // The GMST time information
566 GetGMST(hh,mm,ss,ns,ps);
567 printf("%02d:%02d:%02d.%09d%03d (GMST)\n",hh,mm,ss,ns,ps);
568
569 // Equation of Time and Equation of Equinoxes
570 Double_t eot;
571 Double_t eox=Almanac(0,0,0,0,"Sun",0,0,0,&eot,10);
572 // Convert to fractional hours
573 eox/=3600.;
574 eot/=3600.;
575 if (date)
576 {
577 cout << " ";
578 }
579 else
580 {
581 cout << " ";
582 }
583 if (eot>=0) cout << " ";
584 PrintTime(eot,3); cout << " (LAT-LMT)";
585 cout << " ";
586 if (eox>=0) cout << " ";
587 PrintTime(eox,3); cout << " (LAST-LMST)"; cout << endl;
588
589 // Greenwich apparent time information
590 if (mode<0)
591 {
592 gat=GetLT(eot); // Obtain GAT via the Equation of Time as offset
593 gast=GetGAST();
594 if (date)
595 {
596 cout << " ";
597 }
598 else
599 {
600 cout << " ";
601 }
602 PrintTime(gat,12); cout << " (GAT) ";
603 PrintTime(gast,12); cout << " (GAST)" << endl;
604 }
605
606 // Local time information
607 if (offset)
608 {
609 // Determine the new date by including the offset
610 NcTimestamp t2(*this);
611 t2.Add(offset);
612 Int_t mjd2,mjsec2,mjns2;
613 t2.GetMJD(mjd2,mjsec2,mjns2);
614 if (mjd2>=40587 && (mjd2<65442 || (mjd2==65442 && mjsec2<8047)))
615 {
616 t2.GetDate(kTRUE,0,&y,&m,&d);
617 wd=t2.GetDayOfWeek(kTRUE,0);
618 printf(" %-s, %02d %-s %-d ",day[wd-1].Data(),d,month[m-1].Data(),y);
619 date=kTRUE;
620 }
621 else
622 {
623 cout << " Time ";
624 date=kFALSE;
625 }
626 // Determine the local time by including the offset w.r.t. the original timestamp
627 Double_t hlt=0;
628 Double_t hlst=0;
629 if (mode>0)
630 {
631 hlt=GetLT(offset);
632 hlst=GetLMST(offset);
633 PrintTime(hlt,12); cout << " (LMT) ";
634 PrintTime(hlst,12); cout << " (LMST)" << endl;
635 }
636 else
637 {
638 hlt=GetLT(offset+eot); // Obtain LAT via the Equation of Time as extra offset
639 hlst=GetLAST(offset);
640 PrintTime(hlt,12); cout << " (LAT) ";
641 PrintTime(hlst,12); cout << " (LAST)" << endl;
642 }
643 }
644 }
645
646 // Julian parameter information
647 if (abs(mode)==2 || abs(mode)==3)
648 {
649 Int_t jd,jsec,jns;
650 GetJD(jd,jsec,jns);
651 Int_t tjd,tjsec,tjns;
652 GetTJD(tjd,tjsec,tjns);
653 printf(" Julian Epoch : %-.20f Besselian Epoch : %-.20f\n",GetJE(),GetBE());
654 printf(" JD : %7d sec : %5d ns : %9d ps : %3d Fractional : %25.17f\n",jd,jsec,jns,fJps,GetJD());
655 printf(" MJD : %7d sec : %5d ns : %9d ps : %3d Fractional : %25.17f\n",mjd,mjsec,mjns,fJps,GetMJD());
656 printf(" TJD : %7d sec : %5d ns : %9d ps : %3d Fractional : %25.17f\n",tjd,tjsec,tjns,fJps,GetTJD());
657 if (fUtc && fUtc!=-3)
658 {
659 printf(" TAI : %7d sec : %5d ns : %9d ps : %3d Fractional : %25.17f\n",fTmjd,fTsec,fTns,fTps,GetTAI());
660 }
661 }
662
663 // TAI related information
664 if (mode==4 && fUtc && fUtc!=-3)
665 {
666 printf( " Cumulated (TAI-UTC) leap seconds: %-3d UT1-UTC : %-.6f sec.",fLeap,fDut);
667 if (fUtc<0) cout << " (IERS database)" << endl;
668 if (fUtc>0) cout << " (Manual setting)" << endl;
669
670 // A dummy timestamp is used to obtain the TAI corresponding date indicator
671 NcTimestamp tx;
673
674 if (fTmjd>=40587 && (fTmjd<65442 || (fTmjd==65442 && fTsec<8047)))
675 {
676 tx.GetDate(kTRUE,0,&y,&m,&d);
677 wd=tx.GetDayOfWeek(kTRUE,0);
678 printf(" %-s, %02d %-s %-d ",day[wd-1].Data(),d,month[m-1].Data(),y);
679 date=kTRUE;
680 }
681 else
682 {
683 cout << " Time ";
684 date=kFALSE;
685 }
686
687 // Determine the TAI derived times
688 GetTAI(hh,mm,ss,ns,ps,"TAI");
689 printf("%02d:%02d:%02d.%09d%03d (TAI) ",hh,mm,ss,ns,ps);
690
691 GetTAI(hh,mm,ss,ns,ps,"UTC");
692 printf("%02d:%02d:%02d.%09d%03d (UTC)\n",hh,mm,ss,ns,ps);
693
694 GetTAI(hh,mm,ss,ns,ps,"GPS");
695 if (!date)
696 {
697 cout << " ";
698 }
699 else
700 {
701 cout << " ";
702 }
703 printf("%02d:%02d:%02d.%09d%03d (GPS) ",hh,mm,ss,ns,ps);
704
705 GetTAI(hh,mm,ss,ns,ps,"TT");
706 printf("%02d:%02d:%02d.%09d%03d (TT)\n",hh,mm,ss,ns,ps);
707 }
708}
709
710Double_t NcTimestamp::GetJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
711{
743
744 if (y<0 || m<1 || m>12 || d<1 || d>31) return -1;
745 if (hh<0 || hh>23 || mm<0 || mm>59 || ss<0 || ss>59 || ns<0 || ns>1e9) return -1;
746
747 // The UT daytime in fractional hours
748 Double_t ut=double(hh)+double(mm)/60.+(double(ss)+double(ns)*1.e-9)/3600.;
749
750 Double_t JD=0;
751
752 JD=367*y-int(7*(y+int((m+9)/12))/4)
753 -int(3*(int((y+(m-9)/7)/100)+1)/4)
754 +int(275*m/9)+d+1721028.5+ut/24.;
755
756 return JD;
757}
758
759Double_t NcTimestamp::GetMJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
760{
792
793 Double_t JD=GetJD(y,m,d,hh,mm,ss,ns);
794
795 if (JD<0) return JD;
796
797 Double_t MJD=JD-2400000.5;
798
799 return MJD;
800}
801
802Double_t NcTimestamp::GetTJD(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns) const
803{
835
836 Double_t JD=GetJD(y,m,d,hh,mm,ss,ns);
837
838 if (JD<0) return JD;
839
840 Double_t TJD=JD-2440000.5;
841
842 return TJD;
843}
844
845Double_t NcTimestamp::GetJE(Double_t date,TString mode) const
846{
872
873 if ((mode != "jd") && (mode != "mjd") && (mode != "tjd")) return -99999;
874
875 Double_t jd=date;
876 if (mode=="mjd") jd=date+2400000.5;
877 if (mode=="tjd") jd=date+2440000.5;
878
879 Double_t je=2000.+(jd-2451545.)/365.25;
880
881 return je;
882}
883
884Double_t NcTimestamp::GetBE(Double_t date,TString mode) const
885{
911
912 if ((mode != "jd") && (mode != "mjd") && (mode != "tjd")) return -99999;
913
914 Double_t jd=date;
915 if (mode=="mjd") jd=date+2400000.5;
916 if (mode=="tjd") jd=date+2440000.5;
917
918 Double_t be=1900.+(jd-2415020.31352)/365.242198781;
919
920 return be;
921}
922
923void NcTimestamp::Convert(Double_t date,Int_t& days,Int_t& secs,Int_t& ns) const
924{
948
949 days=int(date);
950 date=date-double(days);
951 Int_t daysecs=24*3600;
952 date=date*double(daysecs);
953 secs=int(date);
954 date=date-double(secs);
955 ns=int(date*1.e9);
956}
957
958Double_t NcTimestamp::Convert(Int_t days,Int_t secs,Int_t ns) const
959{
982
983 Double_t frac=double(secs)+double(ns)*1.e-9;
984 Int_t daysecs=24*3600;
985 frac=frac/double(daysecs);
986 Double_t date=double(days)+frac;
987 return date;
988}
989
990void NcTimestamp::Convert(Double_t h,Int_t& hh,Int_t& mm,Int_t& ss,Int_t& ns,Int_t& ps) const
991{
1011
1012 // Neglect sign of h
1013 h=fabs(h);
1014
1015 hh=int(h);
1016 h=h-double(hh);
1017 h=h*60.;
1018 mm=int(h);
1019 h=h-double(mm);
1020 h=h*60.;
1021 ss=int(h);
1022 h=h-double(ss);
1023 h=h*1.e9;
1024 ns=int(h);
1025 h=h-double(ns);
1026 h=h*1000.;
1027 ps=int(h);
1028}
1029
1030void NcTimestamp::Convert(Double_t h,Int_t& hh,Int_t& mm,Double_t& ss) const
1031{
1050
1051 // Neglect sign of h
1052 h=fabs(h);
1053
1054 hh=int(h);
1055 h=h-double(hh);
1056 h=h*60.;
1057 mm=int(h);
1058 h=h-double(mm);
1059 ss=h*60.;
1060}
1061
1062Double_t NcTimestamp::Convert(Int_t hh,Int_t mm,Int_t ss,Int_t ns,Int_t ps) const
1063{
1083
1084 // Neglect the sign of the input values
1085 hh=abs(hh);
1086 mm=abs(mm);
1087 ss=abs(ss);
1088 ns=abs(ns);
1089 ps=abs(ps);
1090
1091 Double_t h=hh;
1092 h+=double(mm)/60.+(double(ss)+double(ns)*1.e-9+double(ps)*1.e-12)/3600.;
1093
1094 return h;
1095}
1096
1097Double_t NcTimestamp::Convert(Int_t hh,Int_t mm,Double_t ss) const
1098{
1117
1118 // Neglect the sign of the input values
1119 hh=abs(hh);
1120 mm=abs(mm);
1121 ss=fabs(ss);
1122
1123 Double_t h=hh;
1124 h+=double(mm)/60.+ss/3600.;
1125
1126 return h;
1127}
1128
1129void NcTimestamp::PrintTime(Double_t h,Int_t ndig) const
1130{
1147
1148 Int_t hh,mm,ss;
1149 ULong64_t sfrac;
1150 Double_t s;
1151
1152 while (h<-24)
1153 {
1154 h+=24.;
1155 }
1156 while (h>24)
1157 {
1158 h-=24.;
1159 }
1160
1161 Convert(h,hh,mm,s);
1162 ss=Int_t(s);
1163 s-=Double_t(ss);
1164 s*=pow(10.,ndig);
1165 sfrac=ULong64_t(s);
1166
1167 if (h<0) printf("%-s","-");
1168 printf("%02d:%02d:%02d.%0*llu",hh,mm,ss,ndig,sfrac);
1169}
1170
1172{
1179
1180 UInt_t y,m,d,hh,mm,ss;
1181
1182 GetDate(kTRUE,0,&y,&m,&d);
1183 GetTime(kTRUE,0,&hh,&mm,&ss);
1184 Int_t ns=GetNanoSec();
1185
1186 Double_t mjd=GetMJD(y,m,d,hh,mm,ss,ns);
1187
1188 fMJD=int(mjd);
1189 fJsec=GetSec()%(24*3600); // Daytime in elapsed seconds
1190 fJns=ns; // Remaining fractional elapsed second in nanoseconds
1191
1192 // Store the TTimeStamp seconds and nanoseconds values
1193 // for which this Julian calculation was performed.
1194 fCalcs=GetSec();
1195 fCalcns=GetNanoSec();
1196}
1197
1198void NcTimestamp::GetMJD(Int_t& mjd,Int_t& sec,Int_t& ns)
1199{
1211
1212 if (fCalcs != GetSec() || fCalcns != GetNanoSec())
1213 {
1214 FillJulian();
1215 SetUTCparameters("A",0,0);
1216 }
1217
1218 mjd=fMJD;
1219 sec=fJsec;
1220 ns=fJns;
1221}
1222
1224{
1234
1235 Int_t mjd=0;
1236 Int_t sec=0;
1237 Int_t ns=0;
1238 GetMJD(mjd,sec,ns);
1239
1240 Double_t date=Convert(mjd,sec,ns);
1241
1242 return date;
1243}
1244
1245void NcTimestamp::GetTJD(Int_t& tjd,Int_t& sec,Int_t& ns)
1246{
1258
1259 Int_t mjd=0;
1260 GetMJD(mjd,sec,ns);
1261
1262 tjd=mjd-40000;
1263}
1264
1266{
1276
1277 Int_t tjd=0;
1278 Int_t sec=0;
1279 Int_t ns=0;
1280 GetTJD(tjd,sec,ns);
1281
1282 Double_t date=Convert(tjd,sec,ns);
1283
1284 return date;
1285}
1286
1287Int_t NcTimestamp::GetTAI(Int_t& d,Int_t& sec,Int_t& ns,Int_t& ps,Bool_t tmjd)
1288{
1314
1315 // Make sure to have the updated parameters
1316 GetMJD(d,sec,ns);
1317 FillTAI();
1318
1319 d=0;
1320 sec=0;
1321 ns=0;
1322 ps=0;
1323
1324 if (!fUtc) return 0;
1325
1326 d=fTmjd;
1327 sec=fTsec;
1328 ns=fTns;
1329 ps=fTps;
1330
1331 if (!tmjd) d-=36204;
1332
1333 return fUtc;
1334}
1335
1336Double_t NcTimestamp::GetTAI(Bool_t tmjd)
1337{
1359
1360 if (!fUtc) return 0;
1361
1362 Int_t d=0;
1363 Int_t s=0;
1364 Int_t ns=0;
1365 Int_t ps=0;
1366 GetTAI(d,s,ns,ps,tmjd);
1367
1368 Double_t days=Convert(d,s,ns);
1369
1370 return days;
1371}
1372
1373Int_t NcTimestamp::GetTAI(Int_t& hh,Int_t& mm,Int_t& ss,Int_t& ns,Int_t& ps,TString type)
1374{
1398
1399 hh=0;
1400 mm=0;
1401 ss=0;
1402 ns=0;
1403 ps=0;
1404
1405 if (type!="TAI" && type!="UTC" && type!="GPS" && type!="TT") return 0;
1406
1407 Int_t d,sec,nsec,psec;
1408
1409 // Use a dummy timestamp to easily correct for the various offsets
1410 NcTimestamp tx=(*this);
1411 if (type=="UTC") tx.Add(0,-fLeap,0,0);
1412 if (type=="GPS") tx.Add(0,-19,0,0);
1413 if (type=="TT") tx.Add(0,32,184000000,0);
1414
1415 // Use the UTC parameters of the actual (unmodified) timestamp
1416 tx.fLeap=fLeap;
1417 tx.fDut=fDut;
1418
1419 tx.GetTAI(d,sec,nsec,psec);
1420
1421 hh=sec/3600;
1422 sec=sec%3600;
1423 mm=sec/60;
1424 ss=sec%60;
1425 ns=nsec;
1426 ps=psec;
1427
1428 return fUtc;
1429}
1430
1432{
1463
1464 Double_t t=0;
1465
1466 Int_t days=0;
1467 Int_t secs=0;
1468 Int_t ns=0;
1469 Int_t mjd=0;
1470 GetMJD(mjd,secs,ns);
1471 Int_t ps=GetPs();
1472
1473 // Get UTC from the stored UT1 via UTC=UT1-dUT
1474 if (fUtc)
1475 {
1476 NcTimestamp tx;
1477 tx.SetMJD(mjd,secs,ns,ps);
1478 tx.AddSec(-fDut);
1479 tx.GetMJD(mjd,secs,ns);
1480 ps=tx.GetPs();
1481 }
1482
1483 days=mjd-40587;
1484
1485 t=(days*86400)+secs;
1486 t+=double(ns)*1.e-9+double(ps)*1.e-12;
1487
1488 return t;
1489}
1490
1491void NcTimestamp::GetJD(Int_t& jd,Int_t& sec,Int_t& ns)
1492{
1504
1505 Int_t mjd=0;
1506 GetMJD(mjd,sec,ns);
1507
1508 jd=mjd+2400000;
1509 sec+=12*3600;
1510 if (sec >= 24*3600)
1511 {
1512 sec-=24*3600;
1513 jd+=1;
1514 }
1515}
1516
1518{
1528
1529 Int_t jd=0;
1530 Int_t sec=0;
1531 Int_t ns=0;
1532 GetJD(jd,sec,ns);
1533
1534 Double_t date=Convert(jd,sec,ns);
1535
1536 return date;
1537}
1538
1540{
1547
1548 Double_t jd=GetJD();
1549 Double_t je=GetJE(jd);
1550 return je;
1551}
1552
1554{
1561
1562 Double_t jd=GetJD();
1563 Double_t be=GetBE(jd);
1564 return be;
1565}
1566
1567void NcTimestamp::SetMJD(Int_t mjd,Int_t sec,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
1568{
1651
1652 if (sec<0 || sec>=24*3600 || ns<0 || ns>=1e9 || ps<0 || ps>=1000)
1653 {
1654 cout << " *NcTimestamp::SetMJD* Invalid input."
1655 << " sec : " << sec << " ns : " << ns << " ps : " << ps << endl;
1656 return;
1657 }
1658
1659 fMJD=mjd;
1660 fJsec=sec;
1661 fJns=ns;
1662 fJps=ps;
1663
1664 Int_t epoch=40587; // MJD of the start of the epoch
1665 Int_t limit=65442; // MJD of the latest possible TTimeStamp date/time
1666
1667 Int_t date,time;
1668 if (mjd<epoch || mjd>limit || (mjd==limit && sec>=8047))
1669 {
1670 Set(0,kFALSE,0,kFALSE);
1671 date=GetDate();
1672 time=GetTime();
1673 Set(date,time,0,kTRUE,0);
1674 }
1675 else
1676 {
1677 // The elapsed time since start of EPOCH
1678 Int_t days=mjd-epoch;
1679 UInt_t secs=days*24*3600;
1680 secs+=sec;
1681 Set(secs,kFALSE,0,kFALSE);
1682 date=GetDate();
1683 time=GetTime();
1684 Set(date,time,ns,kTRUE,0);
1685 }
1686
1687 // Denote that the Julian and TTimeStamp parameters are synchronised,
1688 // even in the case the MJD falls outside the TTimeStamp validity range.
1689 // The latter still allows retrieval of Julian parameters for these
1690 // earlier times.
1691 fCalcs=GetSec();
1692 fCalcns=GetNanoSec();
1693
1694 // Update the UTC parameters and corresonding TAI time recording
1695 SetUTCparameters(utc,leap,dut);
1696
1697 if (utc=="A" || utc=="M") AddSec(fDut);
1698}
1699
1700void NcTimestamp::SetMJD(Double_t mjd,TString utc,Int_t leap,Double_t dut)
1701{
1784
1785 Int_t days=0;
1786 Int_t secs=0;
1787 Int_t ns=0;
1788 Convert(mjd,days,secs,ns);
1789 SetMJD(days,secs,ns,0,utc,leap,dut);
1790}
1791
1792void NcTimestamp::SetJD(Int_t jd,Int_t sec,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
1793{
1876
1877 Int_t mjd=jd-2400000;
1878 sec-=12*3600;
1879 if (sec<0)
1880 {
1881 sec+=24*3600;
1882 mjd-=1;
1883 }
1884
1885 SetMJD(mjd,sec,ns,ps,utc,leap,dut);
1886}
1887
1888void NcTimestamp::SetJD(Double_t jd,TString utc,Int_t leap,Double_t dut)
1889{
1972
1973 Int_t days=0;
1974 Int_t secs=0;
1975 Int_t ns=0;
1976 Convert(jd,days,secs,ns);
1977
1978 SetJD(days,secs,ns,0,utc,leap,dut);
1979}
1980
1981void NcTimestamp::SetTJD(Int_t tjd,Int_t sec,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
1982{
2065
2066 Int_t mjd=tjd+40000;
2067
2068 SetMJD(mjd,sec,ns,ps,utc,leap,dut);
2069}
2070
2071void NcTimestamp::SetTJD(Double_t tjd,TString utc,Int_t leap,Double_t dut)
2072{
2155
2156 Int_t days=0;
2157 Int_t secs=0;
2158 Int_t ns=0;
2159 Convert(tjd,days,secs,ns);
2160
2161 SetTJD(days,secs,ns,0,utc,leap,dut);
2162}
2163
2165{
2173
2174 if (!fUtc || fUtc==-3)
2175 {
2176 fTmjd=0;
2177 fTsec=0;
2178 fTns=0;
2179 fTps=0;
2180 return;
2181 }
2182
2183 // Use memberfunction to ensure most recent values for the stored UT1
2185 fTps=GetPs();
2186
2187 // Dummy timestamp to easily obtain TAI based day etc. counts
2188 // It is essential not to use UTC parameters here in order to prevent an infinite loop
2189 NcTimestamp tx;
2190 tx.SetMJD(fTmjd,fTsec,fTns,fTps,"N"); // This stores UT1 as if it were UTC
2191
2192 tx.AddSecCalc(-fDut,kFALSE); // Convert the stored UT1 into UTC via UTC=UT1-dUT
2193 tx.AddCalc(0,fLeap,0,0,kFALSE); // Account for the leap seconds
2194
2195 // Retrieve the corresponding TAI day etc. count
2196 tx.GetMJD(fTmjd,fTsec,fTns);
2197 fTps=tx.GetPs();
2198}
2199
2200Int_t NcTimestamp::SetTAI(TString type,TString date,TString time,Int_t mode,TString utc,Int_t leap,Double_t dut)
2201{
2261
2262 Int_t ibad=0;
2263
2264 if (type!="UTC" && type!="GPS" && type!="TAI" && type!="TT") ibad=1;
2265
2266 if (utc!="M" && utc!="A") ibad=1;
2267
2268 // In case utc="M" check the validity of the value of "dut"
2269 if (utc=="M")
2270 {
2271 NcTimestamp tx; // Dummy timestamp for easy MJD retrieval
2272 tx.SetUT(date,time,mode,utc);
2273 Int_t mjd=tx.fMJD;
2274 if (mjd>=41317 && mjd<61405) // Leap Seconds are in use
2275 {
2276 if (fabs(dut)>0.9) ibad=1;
2277 }
2278 else // Leap Seconds were not yet in use or have been abondoned
2279 {
2280 if (mjd>=61405 && fabs(dut)>3600) ibad=1;
2281 }
2282 }
2283
2284 if (utc=="A" && !fUTCdata) ibad=1;
2285
2286 // In case utc="A" check whether the corresponding IERS database info is available
2287 if (utc=="A")
2288 {
2289 NcTimestamp tx; // Dummy timestamp for easy MJD retrieval
2290 tx.SetUT(date,time,mode,utc);
2291 Int_t ien=GetUTCparameters(tx.fMJD,leap,dut);
2292 if (ien<0) ibad=1;
2293 }
2294
2295 if (ibad)
2296 {
2297 SetJD(0,"N");
2298 return fUtc;
2299 }
2300
2301 SetUT(date,time,mode,utc,leap,dut);
2302 if (type != "UTC") Add(0,-fLeap,0,0); // Account for the leap seconds
2303 if (type == "GPS") Add(0,19,0,0); // Account for TAI-GPS=19 sec.
2304 if (type == "TT") Add(0,-32,-184000000,0); // Account for TAI-TT=-32.184 sec.
2305
2306 return fUtc;
2307}
2308
2309Int_t NcTimestamp::SetTAI(Int_t d,Int_t sec,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut,Bool_t tmjd)
2310{
2381
2382 Int_t ibad=0;
2383
2384 if (sec<0 || sec>86400 || ns<0 || ns>999999999 || ps<0 || ps>999) ibad=1;
2385
2386 if (utc!="M" && utc!="A") ibad=1;
2387
2388 // Set the corresponding MJD
2389 Int_t mjd=d;
2390 if (!tmjd) mjd+=36204;
2391
2392 // In case utc="M" check the validity of the value of "dut"
2393 if (utc=="M")
2394 {
2395 if (mjd>=41317 && mjd<61405) // Leap Seconds are in use
2396 {
2397 if (fabs(dut)>0.9) ibad=1;
2398 }
2399 else // Leap Seconds were not yet in use or have been abondoned
2400 {
2401 if (mjd>=61405 && fabs(dut)>3600) ibad=1;
2402 }
2403 }
2404
2405 if (utc=="A" && !fUTCdata) ibad=1;
2406
2407 // In case utc="A" check whether the corresponding IERS database info is available
2408 if (utc=="A")
2409 {
2410 Int_t ien=GetUTCparameters(mjd,leap,dut);
2411 if (ien<0) ibad=1;
2412 }
2413
2414 if (ibad)
2415 {
2416 SetJD(0,"N");
2417 return fUtc;
2418 }
2419
2420 SetMJD(mjd,sec,ns,ps);
2421 SetUTCparameters(utc,leap,dut); // Update the UTC parameters and corresonding TAI time recording
2422 Add(0,-fLeap,0,0); // Account for the leap seconds
2423 AddSec(fDut); // Account for dUT=UT1-UTC
2424
2425 // Set the corresponding TAI day count etc.
2426 FillTAI();
2427
2428 return fUtc;
2429}
2430
2431Int_t NcTimestamp::SetTAI(Double_t tai,TString utc,Int_t leap,Double_t dut,Bool_t tmjd)
2432{
2503
2504 Int_t days=0;
2505 Int_t secs=0;
2506 Int_t ns=0;
2507 Convert(tai,days,secs,ns);
2508
2509 SetTAI(days,secs,ns,0,utc,leap,dut,tmjd);
2510
2511 return fUtc;
2512}
2513
2514Int_t NcTimestamp::SetGPS(Int_t w,Int_t sow,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut,Int_t icycle)
2515{
2575
2576 if (w<0 || sow<0 || sow>604800 || ns<0 || ns>999999999 || ps<0 || ps>999 || icycle<0)
2577 {
2578 SetJD(0,"N");
2579 return fUtc;
2580 }
2581
2582 // Correct the week count for the cycle number if needed
2583 if (icycle) w+=icycle*1024;
2584
2585 Int_t days=8040+w*7;
2586 sow+=19;
2587 Int_t daysecs=24*3600;
2588 Int_t days2=sow/daysecs;
2589 days+=days2;
2590 Int_t secs=sow%daysecs;
2591
2592 SetTAI(days,secs,ns,ps,utc,leap,dut);
2593
2594 return fUtc;
2595}
2596
2597Int_t NcTimestamp::SetGPS(Int_t w,Int_t dow,Int_t sod,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut,Int_t icycle)
2598{
2659
2660 if (w<0 || dow<0 || dow>7 || sod<0 || sod>86400 || ns<0 || ns>999999999 || ps<0 || ps>999 || icycle<0)
2661 {
2662 SetJD(0,"N");
2663 return fUtc;
2664 }
2665
2666 // Correct the week count for the cycle number if needed
2667 if (icycle) w+=icycle*1024;
2668
2669 Int_t days=8040+w*7+dow;
2670 sod+=19;
2671
2672 SetTAI(days,sod,ns,ps,utc,leap,dut);
2673
2674 return fUtc;
2675}
2676
2677Int_t NcTimestamp::SetUnixTime(Double_t sec,TString utc,Int_t leap,Double_t dut)
2678{
2763
2764 // Determine the fractional day count since the start of the Unix Epoch
2765 Double_t tday=sec/86400.;
2766
2767 Int_t days=0;
2768 Int_t s=0;
2769 Int_t ns=0;
2770 Convert(tday,days,s,ns);
2771
2772 // Determine the remaining elapsed picoseconds
2773 Int_t iword=int(sec);
2774 sec=sec-double(iword);
2775 sec*=1e9;
2776 iword=int(sec);
2777 sec=sec-double(iword);
2778 Int_t ps=int(sec*1000.);
2779
2780 // Unix time is related to UTC and not to UT1.
2781 if (utc=="U") utc="A";
2782
2783 SetMJD(40587,0,0,0); // Start of the Unix Epoch
2784 SetUTCparameters(utc,leap,dut); // Update the UTC parameters and corresonding TAI time recording
2785 Add(days,s,ns,ps); // Add the elapsed time
2786 if (fUtc) AddSec(fDut); // Correct for dUT=UT1-UTC
2787
2788 return fUtc;
2789}
2790
2792{
2804
2805 if (ns>=0 && ns<=999999999) fJns=ns;
2806}
2807
2809{
2818
2819 return fJns;
2820}
2821
2823{
2834
2835 if (ps>=0 && ps<=999) fJps=ps;
2836}
2837
2839{
2847
2848 return fJps;
2849}
2850
2851Int_t NcTimestamp::GetUTCparameters(Int_t& leap,Double_t& dut) const
2852{
2874
2875 leap=fLeap;
2876 dut=fDut;
2877
2878 return fUtc;
2879}
2880
2881Int_t NcTimestamp::GetUTCparameters(Int_t mjd,Int_t& leap,Double_t& dut) const
2882{
2893
2894 leap=0;
2895 dut=0;
2896
2897 if (!fUTCdata) return -1;
2898
2899 Int_t nen=fUTCdata->GetEntries();
2900
2901 if (!nen) return -1;
2902
2903 Int_t dbmjd=0;
2904 Int_t dbleap=0;
2905 Double_t dbdut=0;
2906
2907 fUTCdata->SetBranchAddress("mjd",&dbmjd);
2908 fUTCdata->SetBranchAddress("lsec",&dbleap);
2909 fUTCdata->SetBranchAddress("dut",&dbdut);
2910
2911 // Data of the first entry
2912 fUTCdata->GetEntry(0);
2913 Int_t ien=mjd-dbmjd;
2914
2915 if (ien<0 || ien>=nen) return -1; // Specified mjd not in range of database
2916
2917 fUTCdata->GetEntry(ien);
2918 if (dbmjd==mjd) // Specified mjd is found in database
2919 {
2920 leap=dbleap;
2921 dut=dbdut;
2922 }
2923 else
2924 {
2925 ien=-1;
2926 leap=0;
2927 dut=0;
2928 }
2929
2930 return ien;
2931}
2932
2933Int_t NcTimestamp::SetUTCparameters(TString utc,Int_t leap,Double_t dut)
2934{
2999
3000 fUtc=0;
3001 if (utc=="U") fUtc=-3;
3002 fLeap=0;
3003 fDut=0;
3004
3005 Int_t ibad=0;
3006
3007 if (utc!="N" && utc!="M" && utc!="A" && utc!="U") ibad=1;
3008
3009 if (utc=="N" || ((utc=="A" || utc=="U") && !fUTCdata)) ibad=1;
3010
3011 // In case utc="M" check the validity of the value of "dut"
3012 if (utc=="M")
3013 {
3014 if (fMJD>=41317 && fMJD<61405) // Leap Seconds are in use
3015 {
3016 if (fabs(dut)>0.9) ibad=1;
3017 }
3018 else // Leap Seconds were not yet in use or have been abondoned
3019 {
3020 if (fMJD>=61405 && fabs(dut)>3600) ibad=1;
3021 }
3022 }
3023
3024 if (ibad)
3025 {
3026 FillTAI();
3027 return fUtc;
3028 }
3029
3030 // From here only utc="M", utc="A" or utc="U"
3031
3032 if (utc=="M")
3033 {
3034 fUtc=1;
3035 fLeap=leap;
3036 fDut=dut;
3037
3038 FillTAI();
3039 return fUtc;
3040 }
3041
3042 // Automatic setting of the UTC parameters from the loaded data files
3043 Int_t nen=fUTCdata->GetEntries();
3044
3045 if (nen<=0) // No entries in the IERS data TTree
3046 {
3047 FillTAI();
3048 return fUtc;
3049 }
3050
3051 Int_t mjd=0;
3052
3053 fUTCdata->SetBranchAddress("mjd",&mjd);
3054 fUTCdata->SetBranchAddress("lsec",&leap);
3055 fUTCdata->SetBranchAddress("dut",&dut);
3056
3057 // Get the starting mjd of the IERS daily data
3058 // and determine the entry for the current MJD info
3059 fUTCdata->GetEntry(0);
3060 Int_t ien=fMJD-mjd;
3061 if (ien>=0 && ien<nen)
3062 {
3063 fUTCdata->GetEntry(ien);
3064 if (mjd==fMJD)
3065 {
3066 fUtc=-1;
3067 if (utc=="U") fUtc=-2;
3068 fLeap=leap;
3069 fDut=dut;
3070 }
3071 }
3072
3073 FillTAI();
3074
3075 return fUtc;
3076}
3077
3078TTree* NcTimestamp::LoadUTCparameterFiles(TString leapfile,TString dutfile)
3079{
3128
3129 // Expand the input file pathnames
3130 leapfile=gSystem->ExpandPathName(leapfile.Data());
3131 dutfile=gSystem->ExpandPathName(dutfile.Data());
3132
3133 if (fUTCdata)
3134 {
3135 delete fUTCdata;
3136 fUTCdata=0;
3137 }
3138
3139 // The Leap Second input data file
3140 ifstream fleap;
3141 fleap.clear();
3142 fleap.open(leapfile.Data());
3143 if (!fleap.good())
3144 {
3145 cout << " *NcTimestamp::LoadUTCparameterFiles* Data file for Leap Seconds not found ***" << endl;
3146 cout << " File name provided was : " << leapfile.Data() << endl;
3147 return 0;
3148 }
3149
3150 // The dUT=UT1-UTC input data file
3151 ifstream fdut;
3152 fdut.clear();
3153 fdut.open(dutfile.Data());
3154 if (!fdut.good())
3155 {
3156 cout << " *NcTimestamp::LoadUTCparameterFiles* Data file for dUT=UT-UTC not found ***" << endl;
3157 cout << " File name provided was : " << dutfile.Data() << endl;
3158 return 0;
3159 }
3160
3161 // Determine the number of characters in the Leap Second input file
3162 // to reserve sufficient array storage of all Leap Second entries
3163 fleap.seekg(0,fleap.end); // Position at end of file
3164 Int_t ndim=fleap.tellg();
3165
3166 // The storage arrays for the Leap second data
3167 Int_t* lmjd=new Int_t[ndim];
3168 Int_t* leap=new Int_t[ndim];
3169
3171 // Read the Leap Second data //
3173
3174 fleap.seekg(0); // Position at begin of file
3175
3176 // Read title lines until the first data line is found
3177 string line;
3178 Int_t i=0;
3179 while (getline(fleap,line))
3180 {
3181 if (line.find("1972")!=line.npos) break;
3182 i++;
3183 }
3184
3185 // Go to the beginning of the file and skip the title lines preceding the data lines
3186 fleap.seekg(0);
3187 for (Int_t j=0; j<i; j++)
3188 {
3189 getline(fleap,line);
3190 }
3191
3192 // Read the data
3193 Float_t rmjd=0;
3194 Int_t lsec=0;
3195 Float_t x; // Dummy variable for skipping non-requested data columns
3196 i=0;
3197 while (fleap >> rmjd >> x >> x >> x >> lsec)
3198 {
3199 lmjd[i]=int(rmjd);
3200 leap[i]=lsec;
3201 i++;
3202 }
3203
3204 // The number of actual Leap Second entries
3205 Int_t nleap=i;
3206
3208 // Read the dUT=UT1-UTC //
3210
3211 fdut.seekg(0); // Position at begin of file
3212
3213 // Read title lines until the first data line is found
3214 i=0;
3215 while (getline(fdut,line))
3216 {
3217 if (line.find("1962")!=line.npos) break;
3218 i++;
3219 }
3220
3221 // Go to the beginning of the file and skip the title lines preceding the data lines
3222 fdut.seekg(0);
3223 for (Int_t j=0; j<i; j++)
3224 {
3225 getline(fdut,line);
3226 }
3227
3228 // Read the dUT daily data and fill the TTree structure
3229 Int_t mjd=0;
3230 Double_t dut=0;
3231
3232 // The produced output structure
3233 fUTCdata=new TTree("UTCdata","Daily UTC leap second and dUT=UT-UTC parameter data");
3234
3235 // Prevent this Tree to be automatically coupled to a user defined output file
3236 fUTCdata->SetDirectory(0);
3237
3238 // The output variables for the Tree
3239 fUTCdata->Branch("mjd",&mjd,"mjd/I");
3240 fUTCdata->Branch("lsec",&lsec,"lsec/I");
3241 fUTCdata->Branch("dut",&dut,"dut/D");
3242
3243 while (fdut >> x >> x >> x >> mjd >> x >> x >> dut >> x >> x >> x >> x >> x >> x >> x >> x >> x)
3244 {
3245 lsec=0;
3246 // Retrieve the corresponding Leap Second info
3247 for (Int_t j=nleap-1; j>=0; j--)
3248 {
3249 if (mjd>=lmjd[j])
3250 {
3251 lsec=leap[j];
3252 break;
3253 }
3254 }
3255 fUTCdata->Fill();
3256 }
3257
3258 delete[] lmjd;
3259 delete[] leap;
3260
3261 // Retrieve the UTC parameters for the current timestamp
3262 if (!fUtc) // Reference time is UTC
3263 {
3264 SetUTCparameters("A",0,0);
3265 AddSec(fDut);
3266 }
3267 else // Reference time is UT1
3268 {
3269 SetUTCparameters("U",0,0);
3270 }
3271
3272 return fUTCdata;
3273}
3274
3276{
3284
3285 return fUTCdata;
3286}
3287
3288void NcTimestamp::Add(Int_t d,Int_t s,Int_t ns,Int_t ps)
3289{
3315
3316 AddCalc(d,s,ns,ps);
3317}
3318
3319void NcTimestamp::Add(Double_t hours)
3320{
3333
3334 AddCalc(hours);
3335}
3336
3337void NcTimestamp::AddSec(Double_t seconds)
3338{
3351
3352 AddSecCalc(seconds);
3353}
3354
3355void NcTimestamp::AddCalc(Int_t d,Int_t s,Int_t ns,Int_t ps,Bool_t utcpar)
3356{
3386
3387 Int_t days=0;
3388 Int_t secs=0;
3389 Int_t nsec=0;
3390 // Use Get functions to ensure updated Julian parameters.
3391 GetMJD(days,secs,nsec);
3392 Int_t psec=GetPs();
3393
3394 psec+=ps%1000;
3395 nsec+=ps/1000;
3396 while (psec<0)
3397 {
3398 nsec-=1;
3399 psec+=1000;
3400 }
3401 while (psec>999)
3402 {
3403 nsec+=1;
3404 psec-=1000;
3405 }
3406
3407 nsec+=ns%1000000000;
3408 secs+=ns/1000000000;
3409 while (nsec<0)
3410 {
3411 secs-=1;
3412 nsec+=1000000000;
3413 }
3414 while (nsec>999999999)
3415 {
3416 secs+=1;
3417 nsec-=1000000000;
3418 }
3419
3420 secs+=s%(24*3600);
3421 days+=s/(24*3600);
3422 while (secs<0)
3423 {
3424 days-=1;
3425 secs+=24*3600;
3426 }
3427 while (secs>=24*3600)
3428 {
3429 days+=1;
3430 secs-=24*3600;
3431 }
3432
3433 days+=d;
3434
3435 TString utc="N";
3436 if (fUtc==1) utc="M";
3437 if (fUtc==-1) utc="A";
3438 if (fUtc==-2) utc="U";
3439 if (fUtc==-3) utc="U";
3440 Int_t leap=fLeap;
3441 Double_t dut=fDut;
3442 SetMJD(days,secs,nsec,psec);
3443
3444 if (utcpar) SetUTCparameters(utc,leap,dut);
3445}
3446
3447void NcTimestamp::AddCalc(Double_t hours,Bool_t utcpar)
3448{
3467
3468 Int_t d,s,ns,ps;
3469 Double_t h=fabs(hours);
3470 d=int(h/24.);
3471 h-=double(d)*24.;
3472 h*=3600.;
3473 s=int(h);
3474 h-=double(s);
3475 h*=1.e9;
3476 ns=int(h);
3477 h-=double(ns);
3478 ps=int(h*1000.);
3479 if (hours>0) AddCalc(d,s,ns,ps,utcpar);
3480 if (hours<0) AddCalc(-d,-s,-ns,-ps,utcpar);
3481}
3482
3483void NcTimestamp::AddSecCalc(Double_t seconds,Bool_t utcpar)
3484{
3503
3504 Int_t s,ns,ps;
3505 Double_t a=fabs(seconds);
3506 s=int(a);
3507 a-=double(s);
3508 a*=1.e9;
3509 ns=int(a);
3510 a-=double(ns);
3511 ps=int(a*1000.);
3512 if (seconds>0) AddCalc(0,s,ns,ps,utcpar);
3513 if (seconds<0) AddCalc(0,-s,-ns,-ps,utcpar);
3514}
3515
3516Int_t NcTimestamp::GetDifference(NcTimestamp* t,Int_t& d,Int_t& s,Int_t& ns,Int_t& ps,TString type)
3517{
3564
3565 d=0;
3566 s=0;
3567 ns=0;
3568 ps=0;
3569
3570 if (!t || (type!="UT" && type!="TAI")) return 0;
3571
3572 Int_t tUtc=t->fUtc;
3573 if (type=="TAI" && (!fUtc || !tUtc || fUtc==-3 || tUtc==-3)) return 0;
3574
3575 Int_t d1=0;
3576 Int_t s1=0;
3577 Int_t ns1=0;
3578 Int_t ps1=0;
3579
3580 Int_t d2=0;
3581 Int_t s2=0;
3582 Int_t ns2=0;
3583 Int_t ps2=0;
3584
3585 NcTimestamp ts1(*t);
3586 NcTimestamp ts2(*this);
3587
3588 // Check for a mixed use of UT1 and UTC
3589 if (type=="UT")
3590 {
3591 Double_t dut=0;
3592 if (!fUtc && tUtc) // This timestamp is in UTC and the other in UT1
3593 {
3594 dut=t->fDut;
3595 ts1.AddSec(-dut);
3596 }
3597 if (fUtc && !tUtc) // This timestamp is in UT1 and the other in UTC
3598 {
3599 ts2.AddSec(-fDut);
3600 }
3601 }
3602
3603 // Use Get functions to ensure updated Julian and TAI parameters.
3604 if (type=="UT")
3605 {
3606 ts1.GetMJD(d1,s1,ns1);
3607 ps1=ts1.GetPs();
3608 ts2.GetMJD(d2,s2,ns2);
3609 ps2=ts2.GetPs();
3610 }
3611 if (type=="TAI")
3612 {
3613 t->GetTAI(d1,s1,ns1,ps1);
3614 GetTAI(d2,s2,ns2,ps2);
3615 }
3616
3617 d=d1-d2;
3618 s=s1-s2;
3619 ns=ns1-ns2;
3620 ps=ps1-ps2;
3621
3622 if (!d && !s && !ns && !ps) return 0;
3623
3624 Int_t sign=0;
3625
3626 if (d>0) sign=1;
3627 if (d<0) sign=-1;
3628
3629 if (!sign && s>0) sign=1;
3630 if (!sign && s<0) sign=-1;
3631
3632 if (!sign && ns>0) sign=1;
3633 if (!sign && ns<0) sign=-1;
3634
3635 if (!sign && ps>0) sign=1;
3636 if (!sign && ps<0) sign=-1;
3637
3638 // In case the input stamp was earlier, take the reverse difference
3639 // to simplify the algebra.
3640 if (sign<0)
3641 {
3642 d=-d;
3643 s=-s;
3644 ns=-ns;
3645 ps=-ps;
3646 }
3647
3648 // Here we always have a positive time difference
3649 // and can now unambiguously correct for other negative values.
3650 if (ps<0)
3651 {
3652 ns-=1;
3653 ps+=1000;
3654 }
3655
3656 if (ns<0)
3657 {
3658 s-=1;
3659 ns+=1000000000;
3660 }
3661
3662 if (s<0)
3663 {
3664 d-=1;
3665 s+=24*3600;
3666 }
3667
3668 return sign;
3669}
3670
3671Int_t NcTimestamp::GetDifference(NcTimestamp& t,Int_t& d,Int_t& s,Int_t& ns,Int_t& ps,TString type)
3672{
3719
3720 return GetDifference(&t,d,s,ns,ps,type);
3721}
3722
3723Double_t NcTimestamp::GetDifference(NcTimestamp* t,TString u,Int_t mode,TString type)
3724{
3791
3792 if (!t || mode<1 || mode>3 || (type!="UT" && type!="TAI")) return 0;
3793
3794 if (u!="d" && u!="s" && u!="ns" && u!="ps") return 0;
3795
3796
3797 Int_t tUtc=t->fUtc;
3798 if (type=="TAI" && (!fUtc || !tUtc || fUtc==-3 || tUtc==-3)) return 0;
3799
3800 Double_t dt=0;
3801
3802 Int_t d1=0;
3803 Int_t s1=0;
3804 Int_t ns1=0;
3805 Int_t ps1=0;
3806
3807 Int_t d2=0;
3808 Int_t s2=0;
3809 Int_t ns2=0;
3810 Int_t ps2=0;
3811
3812 NcTimestamp ts1(*t);
3813 NcTimestamp ts2(*this);
3814
3815 // Check for a mixed use of UT1 and UTC
3816 if (type=="UT")
3817 {
3818 Double_t dut=0;
3819 if (!fUtc && tUtc) // This timestamp is in UTC and the other in UT1
3820 {
3821 dut=t->fDut;
3822 ts1.AddSec(-dut);
3823 }
3824 if (fUtc && !tUtc) // This timestamp is in UT1 and the other in UTC
3825 {
3826 ts2.AddSec(-fDut);
3827 }
3828 }
3829
3830 // Use Get functions to ensure updated Julian and TAI parameters.
3831 if (type=="UT")
3832 {
3833 ts1.GetMJD(d1,s1,ns1);
3834 ps1=ts1.GetPs();
3835 ts2.GetMJD(d2,s2,ns2);
3836 ps2=ts2.GetPs();
3837 }
3838 if (type=="TAI")
3839 {
3840 t->GetTAI(d1,s1,ns1,ps1);
3841 GetTAI(d2,s2,ns2,ps2);
3842 }
3843
3844 Long_t dd=d1-d2;
3845 Long_t ds=s1-s2;
3846 Long_t dns=ns1-ns2;
3847 Long_t dps=ps1-ps2;
3848
3849 // Time difference for the specified units only
3850 if (mode==3)
3851 {
3852 if (u=="d") dt=dd;
3853 if (u=="s") dt=ds;
3854 if (u=="ns") dt=dns;
3855 if (u=="ps") dt=dps;
3856 return dt;
3857 }
3858
3859 // Suppress elapsed time for the larger units than specified
3860 if (mode==2)
3861 {
3862 if (u=="s") dd=0;
3863 if (u=="ns")
3864 {
3865 dd=0;
3866 ds=0;
3867 }
3868 if (u=="ps")
3869 {
3870 dd=0;
3871 ds=0;
3872 dns=0;
3873 }
3874 }
3875
3876 // Compute the time difference as requested
3877 if (u=="s" || u=="d")
3878 {
3879 // The time difference in (fractional) seconds
3880 dt=double(dd*24*3600+ds)+(double(dns)*1e-9)+(double(dps)*1e-12);
3881 if (u=="d") dt=dt/double(24*3600);
3882 }
3883 if (u=="ns") dt=(double(dd*24*3600+ds)*1e9)+double(dns)+(double(dps)*1e-3);
3884 if (u=="ps") dt=(double(dd*24*3600+ds)*1e12)+(double(dns)*1e3)+double(dps);
3885
3886 return dt;
3887}
3888
3889Double_t NcTimestamp::GetDifference(NcTimestamp& t,TString u,Int_t mode,TString type)
3890{
3957
3958 return GetDifference(&t,u,mode,type);
3959}
3960
3961void NcTimestamp::SetUT(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
3962{
4043
4044 if (d<1 || d>31 || m<1 || m>12 || hh<0 || hh>23 || mm<0 || mm>59 || ss<0 || ss>59 || ns<0 || ns>999999999 || ps<0 || ps>999)
4045 {
4046 cout << " *NcTimestamp::SetUT* Incompatible argument(s) Day=" << d << " Month=" << m << " Year=" << y
4047 << " hour=" << hh << " min=" << mm << " sec=" << ss << " ns=" << ns << " ps=" << ps << endl;
4048 cout << " ==> TAI related time recording is disabled and JD=0 has been set." << endl;
4049 SetJD(0,"N");
4050 return;
4051 }
4052
4053 Int_t day=GetDayOfYear(d,m,y);
4054 Int_t secs=hh*3600+mm*60+ss;
4055 SetUT(y,day-1,secs,ns,ps,utc,leap,dut);
4056}
4057
4058void NcTimestamp::SetUT(Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Double_t s,TString utc,Int_t leap,Double_t dut)
4059{
4127
4128 Int_t ss=int(s);
4129 s-=double(ss);
4130 Int_t ns=s*1.e9;
4131 s-=double(ns)*1.e-9;
4132 Int_t ps=s*1.e12;
4133 SetUT(y,m,d,hh,mm,ss,ns,ps,utc,leap,dut);
4134}
4135
4136void NcTimestamp::SetUT(Int_t y,Int_t m,Int_t d,TString time,TString utc,Int_t leap,Double_t dut)
4137{
4203
4204 Long64_t iword=0;
4205 Int_t hh=0;
4206 Int_t mm=0;
4207 Int_t ss=0;
4208 Int_t ns=0;
4209 Int_t ps=0;
4210 time.ReplaceAll(":","");
4211
4212 // Unpack the hhmmss integer part
4213 iword=time.Atoll();
4214 hh=iword/10000;
4215 iword=iword%10000;
4216 mm=iword/100;
4217 iword=iword%100;
4218 ss=iword;
4219
4220 // Unpack the fractional part in ns and ps
4221 time.Remove(0,7); // Remove the integer part, including the decimal "."
4222 Int_t length=time.Length();
4223 time.Append('0',12-length); // Increase the string to represent the integer number of ps
4224 iword=time.Atoll();
4225 ns=iword/1000;
4226 ps=iword%1000;
4227 SetUT(y,m,d,hh,mm,ss,ns,ps,utc,leap,dut);
4228}
4229
4230void NcTimestamp::SetUT(TString date,TString time,Int_t mode,TString utc,Int_t leap,Double_t dut)
4231{
4301
4302 Int_t iword=0;
4303 Int_t utdate=0;
4304 Int_t year=0;
4305 Int_t month=0;
4306 Int_t day=0;
4307 TString datex=date;
4308 datex.ReplaceAll("-","");
4309 datex.ReplaceAll("/","");
4310 utdate=datex.Atoi();
4311 iword=utdate;
4312 if (mode==0)
4313 {
4314 day=iword/1000000;
4315 iword=iword%1000000;
4316 month=iword/10000;
4317 iword=iword%10000;
4318 year=iword;
4319 }
4320 if (mode==1)
4321 {
4322 year=iword/10000;
4323 iword=iword%10000;
4324 month=iword/100;
4325 iword=iword%100;
4326 day=iword;
4327 }
4328 if (mode==2)
4329 {
4330 month=iword/1000000;
4331 iword=iword%1000000;
4332 day=iword/10000;
4333 iword=iword%10000;
4334 year=iword;
4335 }
4336 if (mode==3)
4337 {
4338 year=iword/10000;
4339 iword=iword%10000;
4340 day=iword/100;
4341 iword=iword%100;
4342 month=iword;
4343 }
4344
4345 if (day<1 || day>31 || month<1 || month>12)
4346 {
4347 cout << " *NcTimestamp::SetUT* Incompatible argument(s) Date: " << date << " Time: " << time << " mode: " << mode << endl;
4348 cout << " ==> TAI related time recording is disabled and JD=0 has been set." << endl;
4349 SetJD(0,"N");
4350 }
4351 else
4352 {
4353 SetUT(year,month,day,time,utc,leap,dut);
4354 }
4355}
4356
4357void NcTimestamp::SetUT(Int_t y,Int_t d,Int_t s,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
4358{
4444
4445 Double_t jd=GetJD(y,1,1,0,0,0,0);
4446 SetJD(jd);
4447 Int_t mjd,sec,nsec;
4448 GetMJD(mjd,sec,nsec);
4449 SetMJD(mjd,0,0,0);
4450 SetUTCparameters(utc,leap,dut); // Update the UTC parameters and corresonding TAI time recording
4451 Add(d,s,ns,ps);
4452
4453 // Determine UT1 using dut=UT1-UTC if UTC was provided as input
4454 if (utc!="U") AddSec(fDut);
4455}
4456
4457void NcTimestamp::GetUT(Int_t& hh,Int_t& mm,Int_t& ss,Int_t& ns,Int_t& ps)
4458{
4466
4467 Int_t mjd,sec,nsec,psec;
4468
4469 GetMJD(mjd,sec,nsec);
4470 psec=GetPs();
4471
4472 hh=sec/3600;
4473 sec=sec%3600;
4474 mm=sec/60;
4475 ss=sec%60;
4476 ns=nsec;
4477 ps=psec;
4478}
4479
4481{
4489
4490 Int_t hh,mm,ss,ns,ps;
4491
4492 GetUT(hh,mm,ss,ns,ps);
4493
4494 Double_t ut=Convert(hh,mm,ss,ns,ps);
4495
4496 return ut;
4497}
4498
4499void NcTimestamp::GetGMST(Int_t& hh,Int_t& mm,Int_t& ss,Int_t& ns,Int_t& ps)
4500{
4510
4511 Int_t mjd,sec,nsec,psec;
4512
4513 // The current UT based timestamp data
4514 GetMJD(mjd,sec,nsec);
4515 psec=fJps;
4516
4517 // The basis for the daily corrections in units of Julian centuries w.r.t. J2000.
4518 // Note : Epoch J2000 starts at 01-jan-2000 12:00:00 UT.
4519 Double_t tau=(GetJD()-2451545.)/36525.;
4520
4521 // Syncronise sidereal time with current timestamp
4522 NcTimestamp sid;
4523 sid.SetMJD(mjd,sec,nsec,psec);
4524
4525 // Add offset for GMST start value defined as 06:41:50.54841 at 01-jan 00:00:00 UT
4526 sec=6*3600+41*60+50;
4527 nsec=548410000;
4528 psec=0;
4529 sid.Add(0,sec,nsec,psec);
4530
4531 // Daily correction for precession and polar motion
4532 Double_t addsec=8640184.812866*tau+0.093104*pow(tau,2)-6.2e-6*pow(tau,3);
4533 sec=int(addsec);
4534 addsec-=double(sec);
4535 nsec=int(addsec*1.e9);
4536 addsec-=double(nsec)*1.e-9;
4537 psec=int(addsec*1.e12);
4538 sid.Add(0,sec,nsec,psec);
4539
4540 sid.GetMJD(mjd,sec,nsec);
4541 psec=sid.GetPs();
4542
4543 hh=sec/3600;
4544 sec=sec%3600;
4545 mm=sec/60;
4546 ss=sec%60;
4547 ns=nsec;
4548 ps=psec;
4549}
4550
4552{
4561
4562 Int_t hh,mm,ss,ns,ps;
4563
4564 GetGMST(hh,mm,ss,ns,ps);
4565
4566 Double_t gst=Convert(hh,mm,ss,ns,ps);
4567
4568 return gst;
4569}
4570
4572{
4592
4593 Double_t da=Almanac();
4594
4595 // Convert to fractional hours
4596 da/=3600.;
4597
4598 Double_t gast=GetGMST()+da;
4599
4600 while (gast<0)
4601 {
4602 gast+=24.;
4603 }
4604 while (gast>24.)
4605 {
4606 gast-=24.;
4607 }
4608
4609 return gast;
4610}
4611
4612Double_t NcTimestamp::GetLT(Double_t offset)
4613{
4626
4627 // Current UT time in fractional hours
4628 Double_t h=GetUT();
4629
4630 h+=offset;
4631
4632 while (h<0)
4633 {
4634 h+=24.;
4635 }
4636 while (h>24)
4637 {
4638 h-=24.;
4639 }
4640
4641 return h;
4642}
4643
4644Double_t NcTimestamp::GetLAT(Double_t offset)
4645{
4665
4666 // Current UT time in fractional hours
4667 Double_t h=GetUT();
4668
4669 // Equation of Time
4670 Double_t eot; // LAT-LMT
4671 Almanac(0,0,0,0,"Sun",0,0,0,&eot,10);
4672 eot/=3600.; // Convert to fractional hours
4673
4674 h+=offset+eot;
4675
4676 while (h<0)
4677 {
4678 h+=24.;
4679 }
4680 while (h>24)
4681 {
4682 h-=24.;
4683 }
4684
4685 return h;
4686}
4687
4688Double_t NcTimestamp::GetLMST(Double_t offset)
4689{
4702
4703 // Current GMST time in fractional hours
4704 Double_t h=GetGMST();
4705
4706 h+=offset;
4707
4708 while (h<0)
4709 {
4710 h+=24.;
4711 }
4712 while (h>24)
4713 {
4714 h-=24.;
4715 }
4716
4717 return h;
4718}
4719
4720Double_t NcTimestamp::GetLAST(Double_t offset)
4721{
4734
4735 // Current GAST time in fractional hours
4736 Double_t h=GetGAST();
4737
4738 h+=offset;
4739
4740 while (h<0)
4741 {
4742 h+=24.;
4743 }
4744 while (h>24)
4745 {
4746 h-=24.;
4747 }
4748
4749 return h;
4750}
4751
4752void NcTimestamp::SetLT(Double_t dt,Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Int_t ss,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
4753{
4822
4823 SetUT(y,m,d,hh,mm,ss,ns,ps,utc,leap,dut);
4824 Add(-dt);
4825}
4826
4827void NcTimestamp::SetLT(Double_t dt,Int_t y,Int_t m,Int_t d,Int_t hh,Int_t mm,Double_t s,TString utc,Int_t leap,Double_t dut)
4828{
4889
4890 SetUT(y,m,d,hh,mm,s,utc,leap,dut);
4891 Add(-dt);
4892}
4893
4894void NcTimestamp::SetLT(Double_t dt,Int_t y,Int_t m,Int_t d,TString time,TString utc,Int_t leap,Double_t dut)
4895{
4954
4955 SetUT(y,m,d,time,utc,leap,dut);
4956 Add(-dt);
4957}
4958
4959void NcTimestamp::SetLT(Double_t dt,TString date,TString time,Int_t mode,TString utc,Int_t leap,Double_t dut)
4960{
5021
5022 SetUT(date,time,mode,utc,leap,dut);
5023 Add(-dt);
5024}
5025
5026void NcTimestamp::SetLT(Double_t dt,Int_t y,Int_t d,Int_t s,Int_t ns,Int_t ps,TString utc,Int_t leap,Double_t dut)
5027{
5100
5101 SetUT(y,d,s,ns,ps,utc,leap,dut);
5102 Add(-dt);
5103}
5104
5105
5105Double_t NcTimestamp::GetJD(Double_t e,TString mode) const
5106{
5118
5119 Double_t jd=0;
5120
5121 if (mode=="J" || mode=="j") jd=(e-2000.0)*365.25+2451545.0;
5122
5123 if (mode=="B" || mode=="b") jd=(e-1900.0)*365.242198781+2415020.31352;
5124
5125 return jd;
5126}
5127
5128Double_t NcTimestamp::GetMJD(Double_t e,TString mode) const
5129{
5141
5142 Double_t mjd=GetJD(e,mode)-2400000.5;
5143
5144 return mjd;
5145}
5146
5147Double_t NcTimestamp::GetTJD(Double_t e,TString mode) const
5148{
5160
5161 Double_t tjd=GetJD(e,mode)-2440000.5;
5162
5163 return tjd;
5164}
5165
5166Double_t NcTimestamp::Almanac(Double_t* dpsi,Double_t* deps,Double_t* eps,Double_t* dl,TString name,Double_t* el,Double_t* eb,Double_t* dr,Double_t* value,Int_t j)
5167{
5276
5277 Double_t pi=acos(-1.);
5278
5279 Double_t td; // Time difference in fractional Julian days w.r.t. the start of J2000.
5280 Double_t tc; // Time difference in fractional Julian centuries w.r.t. the start of J2000.
5281 Double_t tm; // Time difference in fractional Julian millennia w.r.t. the start of J2000.
5282 const Int_t nvals=11;
5283 Double_t val[nvals]; // Array to hold the additional (orbital) parameters
5284
5285 // Initialize the solar system body related values
5286 if (el) *el=720;
5287 if (eb) *eb=720;
5288 if (dr) *dr=-1;
5289 for (Int_t i=0; i<nvals; i++)
5290 {
5291 val[i]=720;
5292 }
5293 val[0]=-1; // Semi major axis of the orbit
5294 val[1]=-1; // Eccentricity of the orbit
5295 val[10]=0; // Equation of Time, this will always be calculated correctly if requested
5296
5297 // Initialize the requested additional observable value
5298 if (value)
5299 {
5300 *value=0;
5301 if (j>=0 && j<nvals) *value=val[j];
5302 }
5303
5304 td=GetJD()-2451545.0;
5305 tc=td/36525.;
5306 tm=tc/10.;
5307
5308 // Fundamental solar system variables (in arcseconds) w.r.t. the J2000.0 equinox.
5309 // The expressions are taken from the USNO circular 179.
5310 // epsilon : Mean obliquity of the ecliptic
5311 // l : Mean anomaly of the Moon
5312 // lp : Mean anomaly of the Sun
5313 // f : Mean argument of latitude of the moon
5314 // d : Mean elongation of the Moon from the Sun
5315 // om : Mean longitude of the Moon's mean ascending node
5316 Double_t epsilon=84381.406-46.836769*tc-0.0001831*pow(tc,2)+0.00200340*pow(tc,3)-0.000000576*pow(tc,4)-0.0000000434*pow(tc,5);
5317 Double_t l=485868.249036+1717915923.2178*tc+31.8792*pow(tc,2)+0.051635*pow(tc,3)-0.00024470*pow(tc,4);
5318 Double_t lp=1287104.79305+129596581.0481*tc-0.5532*pow(tc,2)+0.000136*pow(tc,3)-0.00001149*pow(tc,4);
5319 Double_t f=335779.526232+1739527262.8478*tc-12.7512*pow(tc,2)-0.001037*pow(tc,3)+0.00000417*pow(tc,4);
5320 Double_t d=1072260.70369+1602961601.2090*tc-6.3706*pow(tc,2)+0.006593*pow(tc,3)-0.00003169*pow(tc,4);
5321 Double_t om=450160.398036-6962890.5431*tc+7.4722*pow(tc,2)+0.007702*pow(tc,3)-0.00005939*pow(tc,4);
5322
5323 // General precession in longitude (in arcseconds) w.r.t. J2000.0
5324 // according to Jean Meeus Ch.21 (page 136)
5325 Double_t prec=5029.0966*tc+1.11113*pow(tc,2)-0.000006*pow(tc,3);
5326
5327 if (eps) *eps=epsilon;
5328 if (dl) *dl=prec;
5329
5330 // Convert to radians for use with goniometric functions
5331 Double_t fac=pi/(180.*3600.);
5332 epsilon*=fac;
5333 l*=fac;
5334 lp*=fac;
5335 f*=fac;
5336 d*=fac;
5337 om*=fac;
5338
5339 //The IAU 2000A nutation series expansion.
5340 Double_t phi[28]={om,2.*(f-d+om),2.*(f+om),2.*om,lp,lp+2.*(f-d+om),l,
5341 2.*f+om,l+2.*(f+om),2.*(f-d+om)-lp,2.*(f-d)+om,2.*(f+om)-l,2.*d-l,l+om,
5342 om-l,2.*(f+d+om)-l,l+2.*f+om,2.*(f-l)+om,2.*d,2.*(f+d+om),2.*(f-d+om-lp),
5343 2.*(d-l),2.*(l+d+om),l+2.*(f-d+om),2.*f+om-l,2.*l,2.*f,lp+om};
5344 Double_t s[28]={-17.2064161,-1.3170907,-0.2276413, 0.2074554, 0.1475877,-0.0516821, 0.0711159,
5345 -0.0387298,-0.0301461, 0.0215829, 0.0128227, 0.0123457, 0.0156994, 0.0063110,
5346 -0.0057976,-0.0059641,-0.0051613, 0.0045893, 0.0063384,-0.0038571, 0.0032481,
5347 -0.0047722,-0.0031046, 0.0028593, 0.0020441, 0.0029243, 0.0025887,-0.0014053};
5348 Double_t sd[28]={-0.0174666,-0.0001675,-0.0000234, 0.0000207,-0.0003633, 0.0001226, 0.0000073,
5349 -0.0000367,-0.0000036,-0.0000494, 0.0000137, 0.0000011, 0.0000010, 0.0000063,
5350 -0.0000063,-0.0000011,-0.0000042, 0.0000050, 0.0000011,-0.0000001, 0.0000000,
5351 0.0000000,-0.0000001, 0.0000000, 0.0000021, 0.0000000, 0.0000000,-0.0000025};
5352 Double_t cp[28]={ 0.0033386,-0.0013696, 0.0002796,-0.0000698, 0.0011817,-0.0000524,-0.0000872,
5353 0.0000380, 0.0000816, 0.0000111, 0.0000181, 0.0000019,-0.0000168, 0.0000027,
5354 -0.0000189, 0.0000149, 0.0000129, 0.0000031,-0.0000150, 0.0000158, 0.0000000,
5355 -0.0000018, 0.0000131,-0.0000001, 0.0000010,-0.0000074,-0.0000066, 0.0000079};
5356 Double_t c[28]= { 9.2052331, 0.5730336, 0.0978459,-0.0897492, 0.0073871, 0.0224386,-0.0006750,
5357 0.0200728, 0.0129025,-0.0095929,-0.0068982,-0.0053311,-0.0001235,-0.0033228,
5358 0.0031429, 0.0025543, 0.0026366,-0.0024236,-0.0001220, 0.0016452,-0.0013870,
5359 0.0000477, 0.0013238,-0.0012338,-0.0010758,-0.0000609,-0.0000550, 0.0008551};
5360 Double_t cd[28]={ 0.0009086,-0.0003015,-0.0000485, 0.0000470,-0.0000184,-0.0000677, 0.0000000,
5361 0.0000018,-0.0000063, 0.0000299,-0.0000009, 0.0000032, 0.0000000, 0.0000000,
5362 0.0000000,-0.0000011, 0.0000000,-0.0000010, 0.0000000,-0.0000011, 0.0000000,
5363 0.0000000,-0.0000011, 0.0000010, 0.0000000, 0.0000000, 0.0000000,-0.0000002};
5364 Double_t sp[28]={ 0.0015377,-0.0004587, 0.0001374,-0.0000291,-0.0001924,-0.0000174, 0.0000358,
5365 0.0000318, 0.0000367, 0.0000132, 0.0000039,-0.0000004, 0.0000082,-0.0000009,
5366 -0.0000075, 0.0000066, 0.0000078, 0.0000020, 0.0000029, 0.0000068, 0.0000000,
5367 -0.0000025, 0.0000059,-0.0000003,-0.0000003, 0.0000013, 0.0000011,-0.0000045};
5368
5369 Double_t dp=0,de=0,da=0;
5370 for (Int_t i=0; i<28; i++)
5371 {
5372 dp+=(s[i]+sd[i]*tc)*sin(phi[i])+cp[i]*cos(phi[i]);
5373 de+=(c[i]+cd[i]*tc)*cos(phi[i])+sp[i]*sin(phi[i]);
5374 }
5375
5376 da=dp*cos(epsilon)+0.00264096*sin(om)+0.00006352*sin(2.*om)
5377 +0.00001175*sin(2.*f-2.*d+3.*om)+0.00001121*sin(2.*f-2.*d+om)
5378 -0.00000455*sin(2.*f-2.*d+2.*om)+0.00000202*sin(2.*f+3.*om)+0.00000198*sin(2.*f+om)
5379 -0.00000172*sin(3.*om)-0.00000087*tc*sin(om);
5380
5381 if (dpsi) *dpsi=dp;
5382 if (deps) *deps=de;
5383
5384 // Convert to seconds
5385 da/=15.;
5386
5388 // Determination of the Equation of Time via a recursive invokation //
5390 if (j==10)
5391 {
5392 Double_t xdpsi,xdeps,xeps,xlambda,xbeta;
5393 Almanac(&xdpsi,&xdeps,&xeps,0,"Sun",&xlambda,&xbeta);
5394
5395 // Convert from arcsec to degrees
5396 xdpsi/=3600.;
5397 xdeps/=3600.;
5398 xeps/=3600.;
5399
5400 // Correct for nutation to get the true values
5401 xeps+=xdeps;
5402 xlambda+=xdpsi;
5403
5404 while (xlambda<0) { xlambda+=360.; }
5405 while (xlambda>360) { xlambda-=360.; }
5406
5407 // Convert to radians for gonio
5408 Double_t epsr=xeps*pi/180.;
5409 Double_t lambdar=xlambda*pi/180.;
5410 Double_t betar=xbeta*pi/180.;
5411
5412 // True Right Ascension of the Sun (see Ch.13 p.93 of J. Meeus)
5413 Double_t x=sin(lambdar)*cos(epsr)-tan(betar)*sin(epsr);
5414 Double_t y=cos(lambdar);
5415 Double_t alpha=0;
5416 if (x || y) alpha=atan2(x,y)*180./pi;
5417
5418 while (alpha<0) { alpha+=360.; }
5419 while (alpha>360) { alpha-=360.; }
5420
5421 // Mean longitude of the Sun (see Ch.28 p.183 of J. Meeus)
5422 Double_t L0=280.4664567+360007.6982779*tm+0.03032028*pow(tm,2)+pow(tm,3)/49931.-pow(tm,4)/15300.-pow(tm,5)/2000000.;
5423
5424 while (L0<0) { L0+=360.; }
5425 while (L0>360) { L0-=360.; }
5426
5427 // Determine the Equation of Time in degrees (see Ch.28 p.183 of J. Meeus)
5428 Double_t eot=L0-0.0057183-alpha+xdpsi*cos(xeps*pi/180.);
5429
5430 // The Equation of Time never exceeds 20 minutes (= 5 degrees)
5431 while (eot<-5) { eot+=360.; }
5432 while (eot>5) { eot-=360.; }
5433
5434 // Convert the Equation of Time from degrees to seconds
5435 eot*=240.;
5436
5437 val[10]=eot;
5438 if (value) *value=eot;
5439 }
5440
5442 // Determination of the mean orbital elements and true ecliptic coordinates //
5443 // of a requested solar system body, for the mean equinox of the date. //
5445
5446 // The definitions and expressions are the ones used in the book of Jean Meeus
5447 // "Astronomical Algorithms" (2nd edition of August 2009), esp. chapters 31-33.
5448
5449 // The various observables are :
5450 // a : Semi major axis (in AU) of the orbit
5451 // e : Eccentricity of the orbit
5452 // inc : Inclination (in degrees) of the orbit with the ecliptic
5453 // omega : Mean ecliptic longitude (in degrees) of the ascending node
5454 // lp : Mean orbital longitude (in degrees) of the perihelion
5455 // l : Mean orbital longitude (in degrees) of the body
5456 // omega2 : Orbital argument (in degrees) of the perihelion
5457 // m : Mean orbital anomaly (in degrees) of the body
5458 // ec : Equation of the center (in degrees)
5459 // nu : True anomaly (in degrees) of the body
5460 // ltrue : Heliocentric longitude of the body
5461 // btrue : Heliocentric latitude of the body
5462 // r : Distance between the body and the sun
5463 // lambda : Geocentric ecliptic longitude
5464 // beta : Geocentric ecliptic latitude
5465
5466 Double_t a=0;
5467 Double_t e=0;
5468 Double_t inc=0;
5469 Double_t omega=0;
5470 Double_t omega2=0;
5471 Double_t m=0;
5472 Double_t ec=0;
5473 Double_t nu=0;
5474 Double_t ltrue=0;
5475 Double_t btrue=0;
5476 Double_t r=0;
5477 Double_t lambda=0;
5478 Double_t beta=0;
5479
5480 // Polynomial coefficients for a (in AU)of the 8 major planets
5481 Double_t aa0[8]={0.387098310,0.723329820,1.000001018,1.523679342,5.202603209,9.554909192,19.218446062,30.110386869};
5482 Double_t aa1[8]={0,0,0,0,0.0000001913,-0.0000021390,-0.0000000372,-0.0000001663};
5483 Double_t aa2[8]={0,0,0,0,0,0.000000004,0.00000000098,0.00000000069};
5484 Double_t aa3[8]={0,0,0,0,0,0,0,0};
5485 // Polynomial coefficients for e of the 8 major planets
5486 Double_t ea0[8]={0.20563175,0.00677192,0.01670863,0.09340065,0.04849793,0.05554814,0.04638122,0.00945575};
5487 Double_t ea1[8]={0.000020407,-0.000047765,-0.000042037,0.000090484,0.000163225,-0.000346641,-0.000027293,0.000006033};
5488 Double_t ea2[8]={-0.0000000283,0.0000000981,-0.0000001267,-0.0000000806,-0.0000004714,-0.0000006436,0.0000000789,0};
5489 Double_t ea3[8]={-0.00000000018,0.00000000046,0.00000000014,-0.00000000025,-0.00000000201,0.00000000340,0.00000000024,-0.00000000005};
5490 // Polynomial coefficients for inc (in degrees) of the 8 major planets
5491 Double_t ia0[8]={7.004986,3.394662,0,1.849726,1.303267,2.488879,0.773197,1.769953};
5492 Double_t ia1[8]={0.0018215,0.0010037,0,-0.0006011,-0.0054965,-0.0037362,0.0007744,-0.0093082};
5493 Double_t ia2[8]={-0.00001810,-0.00000088,0,0.00001276,0.00000466,-0.00001519,0.00003749,-0.00000708};
5494 Double_t ia3[8]={0.000000056,-0.000000007,0,-0.000000007,-0.000000002,0.000000087,-0.000000092,0.000000027};
5495 // Polynomial coefficients for omega (in degrees) of the 8 major planets
5496 Double_t oa0[8]={48.330893,76.679920,0,49.558093,100.464407,113.665503,74.005957,131.784057};
5497 Double_t oa1[8]={1.1861883,0.9011206,0,0.7720959,1.0209774,0.8770880,0.5211278,1.1022039};
5498 Double_t oa2[8]={0.00017542,0.00040618,0,0.00001557,0.00040315,-0.00012176,0.00133947,0.00025952};
5499 Double_t oa3[8]={0.000000215,-0.000000093,0,0.000002267,0.000000404,-0.000002249,0.000018484,-0.000000637};
5500 // Polynomial coefficients for lp (in degrees) of the 8 major planets
5501 Double_t pa0[8]={77.456119,131.563703,102.937348,336.060234,14.331207,93.057237,173.005291,48.120276};
5502 Double_t pa1[8]={1.5564776,1.4022288,1.7195366,1.8410449,1.6126352,1.9637613,1.4863790,1.4262957};
5503 Double_t pa2[8]={0.00029544,-0.00107618,0.00045688,0.00013477,0.00103042,0.00083753,0.00021406,0.00038434};
5504 Double_t pa3[8]={0.000000009,-0.000005678,-0.000000018,0.000000536,-0.000004464,0.000004928,0.000000434,0.000000020};
5505 // Polynomial coefficients for l (in degrees) of the 8 major planets
5506 Double_t la0[8]={252.250906,181.979801,100.466457,355.433000,34.351519,50.077444,314.055005,304.348665};
5507 Double_t la1[8]={149474.0722491,58519.2130302,36000.7698278,19141.6964471,3036.3027748,1223.5110686,429.8640561,219.8833092};
5508 Double_t la2[8]={0.00030350,0.00031014,0.00030322,0.00031052,0.00022330,0.00051908,0.00030390,0.00030882};
5509 Double_t la3[8]={0.000000018,0.000000015,0.000000020,0.000000016,0.000000037,-0.000000030,0.000000026,0.000000018};
5510
5511 TString names[10]={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun","Moon"};
5512
5513 Int_t k=-1;
5514 Int_t geo=1;
5515 for (Int_t jbody=0; jbody<10; jbody++)
5516 {
5517 if (name.Contains("*")) geo=0;
5518 if (name.Contains(names[jbody].Data()))
5519 {
5520 k=jbody;
5521 break;
5522 }
5523 }
5524
5525 if (k<0) return da; // Non-supported solar system body
5526
5527 if (!geo && k==8) return da; // Request for heliocentric data of the Sun itself
5528
5529 if (geo && k==2) return da; // Request for geocentric data of the Earth itself
5530
5531 // In case geocentric data for the Sun are requested, the heliocentric data
5532 // of the Earth are used to construct the corresponding data for the Sun.
5533 Int_t sun=0;
5534 if (k==8)
5535 {
5536 k=2;
5537 sun=1;
5538 }
5539
5540 // In case heliocentric data for the Moon are requested, the corresponding data
5541 // for the Earth are provided in view of negligible differences within the
5542 // accuracy of the algorithms used here.
5543 Int_t moon=0;
5544 if (k==9) moon=1;
5545 if (!geo && k==9) k=2;
5546
5547 lambda=0;
5548 beta=0;
5549
5551 // Determination of the geocentric data for the Moon //
5553
5554 if (geo && k==9)
5555 {
5556 // Low-precision geocentric ecliptic coordinates (in degrees) of the Moon.
5557 // Source : Astronomical Alamanac 2012 page D22.
5558 // Maximal errors : 0.3 degr. in lambda, 0.2 degr. in beta, 0.003 degr. in plax and 0.2 R_Earth in r
5559 // A more accurate method is the series expansion given in the book of Jean Meeus.
5560 lambda=218.32+481267.881*tc
5561 +6.29*sin((135.+477198.87*tc)*pi/180.)-1.27*sin((259.3-413335.36*tc)*pi/180.)
5562 +0.66*sin((235.7+890534.22*tc)*pi/180.)+0.21*sin((269.9+954397.74*tc)*pi/180.)
5563 -0.19*sin((357.5+35999.05*tc)*pi/180.)-0.11*sin((186.5+966404.03*tc)*pi/180.);
5564 beta=5.13*sin((93.3+483202.02*tc)*pi/180.)+0.28*sin((228.2+960400.89*tc)*pi/180.)
5565 -0.28*sin((318.3+6003.15*tc)*pi/180.)-0.17*sin((217.6-407332.21*tc)*pi/180.);
5566 Double_t plax=0.9508
5567 +0.0518*cos((135.+477198.87*tc)*pi/180.)+0.0095*cos((259.3-413335.36*tc)*pi/180.)
5568 +0.0078*cos((235.7+890534.22*tc)*pi/180.)+0.0028*cos((269.9+954397.74*tc)*pi/180.);
5569 r=1./sin(plax*pi/180.);
5570
5571 // Convert r into km using an average Earth radius of 6367.45 km
5572 r*=6367.45;
5573
5574 while (lambda<0) { lambda+=360.; }
5575 while (lambda>360) { lambda-=360.; }
5576
5577 if (el) *el=lambda;
5578 if (eb) *eb=beta;
5579 if (dr) *dr=r;
5580
5581 return da;
5582 }
5583
5585 // Determination of the heliocentric data for the requested solar system body //
5587
5588 a=0;
5589 e=0;
5590 inc=0;
5591 omega=0;
5592 l=0;
5593 lp=0;
5594
5595 a=aa0[k]+aa1[k]*tc+aa2[k]*pow(tc,2)+aa3[k]*pow(tc,3);
5596 e=ea0[k]+ea1[k]*tc+ea2[k]*pow(tc,2)+ea3[k]*pow(tc,3);
5597 inc=ia0[k]+ia1[k]*tc+ia2[k]*pow(tc,2)+ia3[k]*pow(tc,3);
5598 omega=oa0[k]+oa1[k]*tc+oa2[k]*pow(tc,2)+oa3[k]*pow(tc,3);
5599 lp=pa0[k]+pa1[k]*tc+pa2[k]*pow(tc,2)+pa3[k]*pow(tc,3);
5600 l=la0[k]+la1[k]*tc+la2[k]*pow(tc,2)+la3[k]*pow(tc,3);
5601
5602 while (omega<0) { omega+=360.; }
5603 while (omega>360) { omega-=360.; }
5604
5605 m=l-lp;
5606
5607 while (m<0) { m+=360.; }
5608 while (m>360) { m-=360.; }
5609
5610 ec=(2.*e-(pow(e,3)/4.)+(5.*pow(e,5)/96.))*sin(m*pi/180.)+((5.*pow(e,2)/4.)-(11.*pow(e,4)/24.))*sin(2.*m*pi/180.)
5611 +((13.*pow(e,3)/12.)-(43.*pow(e,5)/64.))*sin(3.*m*pi/180.)+(103.*pow(e,4)*sin(4.*m*pi/180.)/96.)
5612 +(1097.*pow(e,5)*sin(5.*m*pi/180.)/960.);
5613 ec*=180./pi;
5614
5615 omega2=lp-omega;
5616
5617 while (omega2<0) { omega2+=360.; }
5618 while (omega2>360) { omega2-=360.; }
5619
5620 while (lp<0) { lp+=360.; }
5621 while (lp>360) { lp-=360.; }
5622
5623 while (l<0) { l+=360.; }
5624 while (l>360) { l-=360.; }
5625
5626 nu=m+ec;
5627
5628 while (nu<0) { nu+=360.; }
5629 while (nu>360) { nu-=360.; }
5630
5631 // Store the orbital parameters in the additional values array
5632 if (!sun && !moon)
5633 {
5634 val[0]=a;
5635 val[1]=e;
5636 val[2]=inc;
5637 val[3]=omega;
5638 val[4]=lp;
5639 val[5]=l;
5640 val[6]=omega2;
5641 val[7]=m;
5642 val[8]=ec;
5643 val[9]=nu;
5644 }
5645
5646 // Make requested value available
5647 if (value)
5648 {
5649 *value=0;
5650 if (j>=0 && j<nvals) *value=val[j];
5651 }
5652
5653 r=a*(1.-e*e)/(1.+e*cos(nu*pi/180.));
5654
5655 // Use sine rule to obtain the latitude in radians
5656 Double_t sinb=sin(inc*pi/180.)*sin((l-omega+ec)*pi/180.);
5657 btrue=asin(sinb);
5658
5659 ltrue=omega;
5660 // Use Neper's rule to obtain the extra term of the longitude
5661 Double_t arg=l-omega+ec;
5662 while (arg<0) { arg+=360.; }
5663 while (arg>360) { arg-=360.; }
5664 Double_t extra=0;
5665 if (cos(btrue))
5666 {
5667 Double_t cosl=cos(arg*pi/180.)/cos(btrue);
5668 extra=acos(cosl)*180./pi;
5669 if (arg>180) extra=-extra;
5670 }
5671
5672 btrue*=180./pi;
5673 ltrue+=extra;
5674
5675 // Convert heliocentric Earth data into geocentric Sun data if requested
5676 if (sun)
5677 {
5678 btrue=-btrue;
5679 ltrue+=180.;
5680 }
5681
5682 while (ltrue<0) { ltrue+=360.; }
5683 while (ltrue>360) { ltrue-=360.; }
5684
5685 if (el) *el=ltrue;
5686 if (eb) *eb=btrue;
5687 if (dr) *dr=r;
5688
5689 if (!geo || sun) return da; // Heliocentric (or geocentric Sun) coordinates were requested
5690
5692 // Convert into geocentric ecliptic coordinates //
5694
5695 // The algorithm used here is the one outlined in Ch. 33 of the book of Jean Meeus.
5696 // In view of the accuracy of the current algorithm, the effects of light-time
5697 // and aberration are not taken into account here.
5698
5699 // Determine the heliocentric coordinates of the Earth
5700 // via recursive invokation of this memberfunction
5701 Double_t l0,b0,r0;
5702 Almanac(0,0,0,0,"Earth*",&l0,&b0,&r0);
5703
5704 Double_t x=r*cos(btrue*pi/180.)*cos(ltrue*pi/180.)-r0*cos(b0*pi/180.)*cos(l0*pi/180.);
5705 Double_t y=r*cos(btrue*pi/180.)*sin(ltrue*pi/180.)-r0*cos(b0*pi/180.)*sin(l0*pi/180.);
5706 Double_t z=r*sin(btrue*pi/180.)-r0*sin(b0*pi/180.);
5707
5708 lambda=atan2(y,x)*180./pi;
5709 beta=atan2(z,sqrt(x*x+y*y))*180./pi;
5710 r=sqrt(x*x+y*y+z*z);
5711
5712 while (lambda<0) { lambda+=360.; }
5713 while (lambda>360) { lambda-=360.; }
5714
5715 if (el) *el=lambda;
5716 if (eb) *eb=beta;
5717 if (dr) *dr=r;
5718
5719 return da;
5720}
5721
5722void NcTimestamp::SetEpoch(Double_t e,TString mode,TString utc,Int_t leap,Double_t dut)
5723{
5783
5784 Double_t jd=GetJD(e,mode);
5785 SetJD(jd,utc,leap,dut);
5786}
5787
5788Double_t NcTimestamp::GetEpoch(TString mode)
5789{
5799
5800 Double_t e=0;
5801 if (mode=="B" || mode=="b") e=GetBE();
5802 if (mode=="J" || mode=="j") e=GetJE();
5803 return e;
5804}
5805
5806TString NcTimestamp::GetDayTimeString(TString mode,Int_t ndig,Double_t offset,TString* date,TString* time,Bool_t full)
5807{
5851
5852 Bool_t set=kFALSE;
5853
5854 TString sdate=mode;
5855 sdate+=" information unavailable";
5856
5857 TString stime=mode;
5858 stime+=" information unavailable";
5859
5860 TString daytime=mode;
5861 daytime+=" information unavailable";
5862
5863 TString month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
5864 TString day[7]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
5865
5866 UInt_t y=0;
5867 UInt_t m=0;
5868 UInt_t d=0;
5869 UInt_t wd=0;
5870 Int_t hh,mm,ss,ns,ps;
5871 Double_t s=0;
5872 ULong64_t sfrac=0;
5873 Double_t gat=0;
5874 Double_t gast=0;
5875 Bool_t bdate=kFALSE;
5876
5877 Int_t mjd,mjsec,mjns;
5878 GetMJD(mjd,mjsec,mjns);
5879
5880 // Check whether UT1 is the main reference
5881 if (mode=="UT1" && fUtc) mode="UT";
5882
5883 // Check whether UTC is the main reference
5884 if (mode=="UTC" && !fUtc) mode="UT";
5885
5886 // UT related information : UT, GAT, GMST and GAST date and time
5887 if (mode=="UT" || mode=="GAT" || mode=="GMST" || mode=="GAST")
5888 {
5889 if (mjd>=40587 && (mjd<65442 || (mjd==65442 && mjsec<8047)))
5890 {
5891 GetDate(kTRUE,0,&y,&m,&d);
5892 wd=GetDayOfWeek(kTRUE,0);
5893 bdate=kTRUE;
5894 }
5895 if (mode=="UT") GetUT(hh,mm,ss,ns,ps);
5896 if (mode=="GMST") GetGMST(hh,mm,ss,ns,ps);
5897 if (mode=="GAT")
5898 {
5899 // Obtain GAT as LAT without offset
5900 gat=GetLAT(0);
5901 Convert(gat,hh,mm,ss,ns,ps);
5902 }
5903 if (mode=="GAST")
5904 {
5905 gast=GetGAST();
5906 Convert(gast,hh,mm,ss,ns,ps);
5907 }
5908
5909 set=kTRUE;
5910 }
5911
5912 // TAI related information : TAI, UTC, GPS and TT date and time
5913 if (mode=="TAI" || mode=="UTC" || mode=="GPS" || mode=="TT")
5914 {
5915 if (fUtc && fUtc!=-3)
5916 {
5917 // A dummy timestamp is used to obtain the TAI corresponding date indicator
5918 NcTimestamp tx;
5920
5921 // Convert to the corresponding UTC, GPS or TT timestamps
5922 if (mode=="UTC") tx.AddSec(-fLeap);
5923 if (mode=="GPS") tx.AddSec(-19);
5924 if (mode=="TT") tx.AddSec(32.184);
5925
5926 Int_t tmjd,tsec,tns;
5927 tx.GetMJD(tmjd,tsec,tns);
5928
5929 if (tmjd>=40587 && (tmjd<65442 || (tmjd==65442 && tsec<8047)))
5930 {
5931 tx.GetDate(kTRUE,0,&y,&m,&d);
5932 wd=tx.GetDayOfWeek(kTRUE,0);
5933 bdate=kTRUE;
5934 }
5935 GetTAI(hh,mm,ss,ns,ps,mode);
5936
5937 set=kTRUE;
5938 }
5939 }
5940
5941 // Local time information
5942 if (mode=="LMT" || mode=="LAT" || mode=="LMST" || mode=="LAST")
5943 {
5944 // Determine the new date by including the offset
5945 NcTimestamp t2(*this);
5946 t2.Add(offset);
5947 Int_t mjd2,mjsec2,mjns2;
5948 t2.GetMJD(mjd2,mjsec2,mjns2);
5949 if (mjd2>=40587 && (mjd2<65442 || (mjd2==65442 && mjsec2<8047)))
5950 {
5951 t2.GetDate(kTRUE,0,&y,&m,&d);
5952 wd=t2.GetDayOfWeek(kTRUE,0);
5953 bdate=kTRUE;
5954 }
5955 // Determine the local time by including the offset w.r.t. the original timestamp
5956 Double_t hlt=0;
5957 if (mode=="LMT") hlt=GetLT(offset);
5958 if (mode=="LAT") hlt=GetLAT(offset);
5959 if (mode=="LMST") hlt=GetLMST(offset);
5960 if (mode=="LAST") hlt=GetLAST(offset);
5961 Convert(hlt,hh,mm,ss,ns,ps);
5962
5963 set=kTRUE;
5964 }
5965
5966 // No match found with requested date/time system
5967 if (!set)
5968 {
5969 if (date) *date=sdate;
5970 if (time) *time=stime;
5971 return daytime;
5972 }
5973
5974 // Create the date and time info string
5975 sdate="";
5976 stime="";
5977
5978 // The date string
5979 if (bdate)
5980 {
5981 if (full)
5982 {
5983 sdate.Form("%-s %02i-%-s-%-i",day[wd-1].Data(),d,month[m-1].Data(),y);
5984 }
5985 else
5986 {
5987 sdate.Form("%02i-%02i-%-i",d,m,y);
5988 }
5989 }
5990
5991 // The time string
5992 if (hh<10) stime+="0";
5993 stime+=hh;
5994 stime+=":";
5995 if (mm<10) stime+="0";
5996 stime+=mm;
5997 stime+=":";
5998 if (ss<10) stime+="0";
5999 stime+=ss;
6000 if (ndig>0)
6001 {
6002 stime+=".";
6003 s=double(ns)*1e-9+double(ps)*1e-12;
6004 s*=pow(10.,ndig);
6005 sfrac=ULong64_t(s);
6006 ULong64_t isfrac=pow(10,ndig-1);
6007 while (sfrac<isfrac)
6008 {
6009 stime+="0";
6010 isfrac/=10;
6011 }
6012 if (sfrac) stime+=sfrac;
6013 }
6014
6015 // Construct the combined date/time string
6016 daytime=sdate;
6017 daytime+=" ";
6018 daytime+=stime;
6019
6020 // Add the time system indicator
6021 if (mode=="UT")
6022 {
6023 mode="UT1";
6024 if (!fUtc) mode="UTC";
6025 }
6026 if (sdate=="") sdate="---";
6027 if (full)
6028 {
6029 sdate+=" ";
6030 sdate+=mode;
6031 stime+=" ";
6032 stime+=mode;
6033 daytime+=" ";
6034 daytime+=mode;
6035 }
6036
6037 if (date) *date=sdate;
6038 if (time) *time=stime;
6039
6040 return daytime;
6041}
6042
6044{
6053
6054 // Retrieve the current system clock time
6055 Set();
6056
6057 // Store this time as UTC
6058 FillJulian();
6059 Int_t ret=SetUTCparameters("A",0,0);
6060
6061 // Use the UTC parameters (if any) to determine UT1
6062 if (ret && ret!=-3)
6063 {
6064 AddSec(fDut); // Correct UTC for dUT=UT1-UTC to obtain UT1 as main reference
6065 }
6066 else
6067 {
6068 SetUTCparameters("N",0,0); // Keep the system clock time as main (UTC) reference
6069 }
6070 FillTAI();
6071}
6072
6074{
6080
6081 Bool_t ut1=kTRUE;
6082 if (!fUtc) ut1=kFALSE;
6083
6084 return ut1;
6085}
6086
ClassImp(NcTimestamp)
Handling of timestamps for (astro)particle physics research.
Definition NcTimestamp.h:20
virtual ~NcTimestamp()
Double_t GetEpoch(TString mode)
Double_t GetTJD()
Int_t SetUnixTime(Double_t sec, TString utc="A", Int_t leap=0, Double_t dut=0)
Int_t SetTAI(TString type, TString date, TString time, Int_t mode, TString utc, Int_t leap, Double_t dut=0)
Double_t GetMJD(Int_t y, Int_t m, Int_t d, Int_t hh, Int_t mm, Int_t ss, Int_t ns) const
Int_t SetGPS(Int_t w, Int_t sow, Int_t ns, Int_t ps, TString utc, Int_t leap, Double_t dut=0, Int_t icycle=0)
void SetLT(Double_t dt, Int_t y, Int_t m, Int_t d, Int_t hh, Int_t mm, Int_t ss, Int_t ns=0, Int_t ps=0, TString utc="A", Int_t leap=0, Double_t dut=0)
void SetSystemTime()
TString GetDayTimeString(TString mode, Int_t ndig=0, Double_t offset=0, TString *date=0, TString *time=0, Bool_t full=kTRUE)
Double_t Almanac(Double_t *dpsi=0, Double_t *deps=0, Double_t *eps=0, Double_t *dl=0, TString name="", Double_t *el=0, Double_t *eb=0, Double_t *dr=0, Double_t *value=0, Int_t j=0)
NcTimestamp & operator=(const NcTimestamp &t)
Double_t GetLAT(Double_t offset)
TTree * GetIERSdatabase() const
Double_t GetGMST()
Double_t GetLMST(Double_t offset)
Double_t GetUT()
Double_t GetMJD()
Bool_t IsUT1() const
Int_t GetPs() const
Double_t GetLT(Double_t offset)
void AddSecCalc(Double_t seconds, Bool_t utcpar=kTRUE)
Double_t GetBE()
Int_t SetUTCparameters(TString utc, Int_t leap, Double_t dut)
void SetEpoch(Double_t e, TString mode, TString utc="U", Int_t leap=0, Double_t dut=0)
Double_t GetGAST()
void SetUT(Int_t y, Int_t m, Int_t d, Int_t hh, Int_t mm, Int_t ss, Int_t ns=0, Int_t ps=0, TString utc="A", Int_t leap=0, Double_t dut=0)
void PrintTime(Double_t h, Int_t ndig=1) const
Double_t GetLAST(Double_t offset)
TTree * fUTCdata
TTree * LoadUTCparameterFiles(TString leapfile="$(NCFS)/IERS/leap.txt", TString dutfile="$(NCFS)/IERS/dut.txt")
void SetTJD(Int_t tjd, Int_t sec, Int_t ns, Int_t ps=0, TString utc="U", Int_t leap=0, Double_t dut=0)
void SetMJD(Int_t mjd, Int_t sec, Int_t ns, Int_t ps=0, TString utc="U", Int_t leap=0, Double_t dut=0)
Int_t GetNs() const
Double_t fDut
void SetJD(Int_t jd, Int_t sec, Int_t ns, Int_t ps=0, TString utc="U", Int_t leap=0, Double_t dut=0)
Double_t GetJE()
Double_t GetUnixTime()
void Add(Int_t d, Int_t s, Int_t ns, Int_t ps=0)
void Convert(Double_t date, Int_t &days, Int_t &secs, Int_t &ns) const
Int_t GetUTCparameters(Int_t &leap, Double_t &dut) const
Int_t GetDifference(NcTimestamp *t, Int_t &days, Int_t &sec, Int_t &ns, Int_t &ps, TString type="UT")
void AddSec(Double_t seconds)
void Date(Int_t mode=3, Double_t offset=0)
void AddCalc(Int_t d, Int_t s, Int_t ns, Int_t ps=0, Bool_t utcpar=kTRUE)
Int_t GetTAI(Int_t &d, Int_t &sec, Int_t &ns, Int_t &ps, Bool_t tmjd=kTRUE)
Double_t GetJD()
void SetPs(Int_t ps)
void SetNs(Int_t ns)