PLplot  5.15.0
tai-utc-gen.c
Go to the documentation of this file.
1 // Copyright (C) 2009-2014 Alan W. Irwin
2 //
3 // This file is part of PLplot.
4 // PLplot is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU Library General Public License as published
6 // by the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // PLplot is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Library General Public License for more details.
13 //
14 // You should have received a copy of the GNU Library General Public License
15 // along with PLplot; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 //
19 
20 // Program for generating data structure used for containing tai-utc
21 // conversion information (linear transforms and leap seconds).
22 //
23 // The program assumes that argv[1] will be the input file, and
24 // argv[2] the output file. This works cross-platform without
25 // worrying about shell redirects of stdin and stdout that are
26 // not accessible on Windows, apparently.
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 
33 
34 //--------------------------------------------------------------------------
35 // Function-like macro definitions
36 //--------------------------------------------------------------------------
37 
38 #define MemError1( a ) do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )
39 
40 const char header[] = "" \
41  "/*\n" \
42  " This file is part of PLplot.\n" \
43  " \n" \
44  " PLplot is free software; you can redistribute it and/or modify\n" \
45  " it under the terms of the GNU Library General Public License as published\n" \
46  " by the Free Software Foundation; either version 2 of the License, or\n" \
47  " (at your option) any later version.\n" \
48  " \n" \
49  " PLplot is distributed in the hope that it will be useful,\n" \
50  " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
51  " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
52  " GNU Library General Public License for more details.\n" \
53  " \n" \
54  " You should have received a copy of the GNU Library General Public License\n" \
55  " along with PLplot; if not, write to the Free Software\n" \
56  " Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
57  " \n" \
58  " \n" \
59  " This header file contains the table containing the linear transforms \n" \
60  " for converting between TAI and UTC.\n" \
61  " It is an automatically generated file, so please do\n" \
62  " not edit it directly. Make any changes to tai-utc.dat then use\n" \
63  " tai-utc-gen to recreate this header file.\n" \
64  " \n" \
65  " tai-utc.dat contains four essential fields to represent the following\n" \
66  " formula for the linear transformation between TAI and UTC: \n" \
67  " TAI-UTC (seconds) = offset1 + (MJD-offset2)*slope\n" \
68  " There are four essential fields per line in tai-utc.dat to represent\n" \
69  " this formula. They are the Julian date (UTC) where the linear\n" \
70  " transformation implied by the line is first applied;\n" \
71  " offset1 (seconds); offset2 (days), and slope (secs/day).\n" \
72  " \n" \
73  "*/";
74 
75 int main( int argc, char *argv[] )
76 {
77  FILE *fr, *fw;
78  char readbuffer[256];
79  int *MJDstart = NULL;
80  double *offset1 = NULL;
81  int *offset2 = NULL;
82  double *slope = NULL;
83  double sec, *leap_sec = NULL;
84  int jd;
85  int i = 0;
86  int number_of_lines = 0;
87 
88  if ( ( argc < 2 ) || ( fr = fopen( argv[1], "r" ) ) == NULL )
89  {
90  fprintf( stderr, "Cannot open first file as readable\n" );
91  exit( 1 );
92  }
93 
94  if ( ( argc < 3 ) || ( fw = fopen( argv[2], "w" ) ) == NULL )
95  {
96  fprintf( stderr, "Cannot open second file as writable\n" );
97  exit( 1 );
98  }
99 
100  //
101  // Work out how many lines we have all up
102  //
103 
104  while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
105  {
106  ++number_of_lines;
107  }
108 
109  //
110  // Allocate memory to the arrays which will hold the data
111  //
112 
113  if ( ( MJDstart = (int *) calloc( (size_t) number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
114  MemError1( "Allocating memory to the MJDstart table" );
115 
116  if ( ( offset1 = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
117  MemError1( "Allocating memory to the offset1 table" );
118 
119  if ( ( offset2 = (int *) calloc( (size_t) number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
120  MemError1( "Allocating memory to the offset2 table" );
121 
122  if ( ( slope = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
123  MemError1( "Allocating memory to the slope table" );
124 
125  if ( ( leap_sec = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
126  MemError1( "Allocating memory to the leap_sec table" );
127 
128  rewind( fr ); // Go back to the start of the file
129 
130  //
131  // Read in line by line, and copy the numbers into our arrays
132  //
133 
134  while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
135  {
136  sscanf( readbuffer, "%*s %*s %*s %*s %d.5 %*s %lf %*s %*s %*s %*s %d.) X %lf S", (int *) &jd, (double *) &offset1[i], (int *) &offset2[i], (double *) &slope[i] );
137  // Should be exact since all jd's in the file are integer+0.5
138  MJDstart[i] = jd - 2400000;
139  i++;
140  }
141 
142  fclose( fr );
143 
144 //
145 // Write the data out to file ready to be included in our source
146 //
147 
148 
149  fprintf( fw, "%s\n", header );
150 
151  fprintf( fw, "typedef struct {\n\tint base_day;\n\tdouble time_sec_tai;\n\tdouble time_sec_utc;\n\tdouble size_prev_leap_sec;\n\tdouble offset1;\n\tint offset2;\n\tdouble slope;\n} TAI_UTC;\n\n" );
152 
153  fprintf( fw, "const int number_of_entries_in_tai_utc_table=%d;\n\n", number_of_lines );
154 
155  fprintf( fw, "const TAI_UTC TAI_UTC_lookup_table[%d] = {\n", number_of_lines );
156  for ( i = 0; i < number_of_lines; i++ )
157  {
158  sec = offset1[i] + (double) ( MJDstart[i] - offset2[i] ) * slope[i];
159  if ( i == 0 )
160  leap_sec[i] = 0.;
161  else
162  // sec is TAI-UTC in seconds calculated from UTC transformation
163  // (equation 1 in README.tai-utc). This calculation must be correct
164  // for start of epoch range. However, near end of epoch range where
165  // ambiguities in UTC occur, must use equivalent TAI transformation
166  // (equation 2 from same source) to calculate the UTC discontinuity
167  // unambiguously.
168  leap_sec[i] = sec - ( offset1[i - 1] + (double) ( MJDstart[i] + sec / 86400. - offset2[i - 1] ) * slope[i - 1] ) / ( 1. + slope[i - 1] / 86400. );
169  if ( fabs( leap_sec[i] ) < 1.e-14 )
170  leap_sec[i] = 0.;
171  fprintf( fw, "{%d, %15.8f, 0., %20.14f, %15.8f, %d, %15.8f},\n", MJDstart[i], sec, leap_sec[i], offset1[i], offset2[i], slope[i] );
172  }
173  fprintf( fw, "};\n" );
174 
175  fclose( fw );
176  free( MJDstart );
177  free( offset1 );
178  free( offset2 );
179  free( slope );
180  free( leap_sec );
181 
182  return ( 0 );
183 }
184 
const char header[]
Definition: tai-utc-gen.c:40
static char ** argv
Definition: qt.cpp:49
static int argc
Definition: qt.cpp:48
int main(int argc, char *argv[])
Definition: tai-utc-gen.c:75
#define MemError1(a)
Definition: tai-utc-gen.c:38