LAL  7.5.0.1-b72065a
TranslateMJD.c
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004, 2005, 2015 Reinhard Prix
3 // Copyright (C) 2013 Matt Pitkin
4 // Copyright (C) 2007 Chris Messenger
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with with program; see the file COPYING. If not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 // MA 02110-1301 USA
20 //
21 
22 #include <math.h>
23 #include <ctype.h>
24 #include <errno.h>
25 
26 #include <lal/Date.h>
27 #include <lal/StringInput.h>
28 #include <lal/LALConstants.h>
29 #include <lal/LALString.h>
30 
31 #include <lal/UserInputParse.h>
32 
33 #include <lal/TranslateMJD.h>
34 
35 // ---------- global variables ----------
36 const REAL8 TT_minus_TAI = 32.184; ///< constant offset between TT and TAI epochs (see e.g. Table 1 in \cite SeidelmannFukushima1992,)
37 const REAL8 TAI_minus_UTC_at_GPS0 = 19.0; ///< offset between TAI and UTC at GPS epoch, ie number of leap seconds at GPS epoch
38 const REAL8 TT_minus_UTC_at_GPS0 = 51.184; ///< = TT_minus_TAI + TAI_minus_UTC_at_GPS0, ie offset between TT and UTC at GPS epoch
39 const REAL8 GPS0_in_MJDUTC = 44244.0; ///< GPS epoch [1980 JAN 6 0h UTC] expressed in MJD(UTC)
40 
41 // ==================== function definitions ====================
42 
43 ///
44 /// convert given MJD(TT) time, mjd = mjdDays + mjdFracDays into LIGOTimeGPS format, preserving full (ns) accuracy.
45 ///
46 /// returns gps input pointer on success, NULL on error.
47 ///
49 XLALTranslateMJDTTtoGPS ( LIGOTimeGPS *gps, ///< [out] returned GPS time
50  INT4 mjdDays, ///< [in] input MJD integer days, must be >= 0
51  REAL8 mjdFracDays ///< [in] input MJD fractional days, must be in [0, 1)
52  )
53 {
54  XLAL_CHECK_NULL ( gps != NULL, XLAL_EINVAL );
55  XLAL_CHECK_NULL ( mjdDays >= 0, XLAL_EDOM, "mjdDays = %d must be positive\n", mjdDays );
56  XLAL_CHECK_NULL ( (mjdFracDays < 1) && (mjdFracDays >= 0), XLAL_EDOM, "mjdFracDays = %g must be within [0, 1) days\n", mjdFracDays );
57 
58  INT4 gpsSeconds = (INT4) round ( (mjdDays - GPS0_in_MJDUTC) * 86400.0 ); // use gps0 epoch in mjd(utc) here, [round() just for safety, should be int anyway]
59  REAL8 gpsSeconds2 = mjdFracDays * 86400.0 - TT_minus_UTC_at_GPS0; // correct gps0 epoch from mjd(utc) to mjd(tt)
60 
61  REAL8 int2, frac2;
62  frac2 = modf ( gpsSeconds2, &int2 ); // get integer and fractional parts
63 
64  gpsSeconds += (INT4) int2;
65  INT4 gpsNanoSeconds = (INT4) round ( frac2 * XLAL_BILLION_REAL8 );
67  {
68  gpsNanoSeconds = 0;
69  gpsSeconds ++;
70  }
71  if ( gpsNanoSeconds < 0 )
72  {
73  gpsSeconds --;
75  }
76 
77  gps->gpsSeconds = gpsSeconds;
79 
80  return gps;
81 
82 } // XLALTranslateMJDTTtoGPS()
83 
84 
85 ///
86 /// Parse and convert given string representing MJD(TT) time into LIGOTimeGPS gps time, without loss of (ns) accuracy
87 ///
88 /// returns gps input pointer on success, NULL on error.
89 ///
91 XLALTranslateStringMJDTTtoGPS ( LIGOTimeGPS *gps, ///< [out] returned GPS time
92  const char *mjdString ///< [in] input string representing MJD(TT) time
93  )
94 {
95  XLAL_CHECK_NULL ( (gps != NULL) && (mjdString != NULL), XLAL_EINVAL );
96 
97  INT4 mjdDays;
98  REAL8 mjdFracDays;
99  XLAL_CHECK_NULL ( XLALParseStringValueAsINT4PlusFrac ( &mjdDays, &mjdFracDays, mjdString ) == XLAL_SUCCESS, XLAL_EFUNC );
100 
101  XLAL_CHECK_NULL ( XLALTranslateMJDTTtoGPS ( gps, mjdDays, mjdFracDays ) != NULL, XLAL_EFUNC );
102 
103  return gps;
104 
105 } // XLALTranslateStringMJDTTtoGPS()
#define XLAL_BILLION_INT4
Definition: Date.h:39
#define XLAL_BILLION_REAL8
Definition: Date.h:41
const REAL8 TAI_minus_UTC_at_GPS0
offset between TAI and UTC at GPS epoch, ie number of leap seconds at GPS epoch
Definition: TranslateMJD.c:37
const REAL8 GPS0_in_MJDUTC
GPS epoch [1980 JAN 6 0h UTC] expressed in MJD(UTC)
Definition: TranslateMJD.c:39
const REAL8 TT_minus_TAI
constant offset between TT and TAI epochs (see e.g. Table 1 in ,)
Definition: TranslateMJD.c:36
const REAL8 TT_minus_UTC_at_GPS0
= TT_minus_TAI + TAI_minus_UTC_at_GPS0, ie offset between TT and UTC at GPS epoch
Definition: TranslateMJD.c:38
double REAL8
Double precision real floating-point number (8 bytes).
int32_t INT4
Four-byte signed integer.
LIGOTimeGPS * XLALTranslateMJDTTtoGPS(LIGOTimeGPS *gps, INT4 mjdDays, REAL8 mjdFracDays)
convert given MJD(TT) time, mjd = mjdDays + mjdFracDays into LIGOTimeGPS format, preserving full (ns)...
Definition: TranslateMJD.c:49
LIGOTimeGPS * XLALTranslateStringMJDTTtoGPS(LIGOTimeGPS *gps, const char *mjdString)
Parse and convert given string representing MJD(TT) time into LIGOTimeGPS gps time,...
Definition: TranslateMJD.c:91
int XLALParseStringValueAsINT4PlusFrac(INT4 *valINT4, REAL8 *valFrac, const char *valString)
Parse a string containing a floating-point number into integer and fractional part,...
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
Definition: XLALError.h:825
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EDOM
Input domain error.
Definition: XLALError.h:410
@ XLAL_EINVAL
Invalid argument.
Definition: XLALError.h:409
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
INT4 gpsSeconds
Seconds since 0h UTC 6 Jan 1980.
Definition: LALDatatypes.h:459
INT4 gpsNanoSeconds
Residual nanoseconds.
Definition: LALDatatypes.h:460