• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

tai-utc-gen.c

Go to the documentation of this file.
00001 //  $Id: tai-utc-gen.c 11680 2011-03-27 17:57:51Z airwin $
00002 //
00003 //  Copyright (C) 2009 Alan W. Irwin
00004 //
00005 //  This file is part of PLplot.
00006 //  PLplot is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU Library General Public License as published
00008 //  by the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  PLplot is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU Library General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU Library General Public License
00017 //  along with PLplot; if not, write to the Free Software
00018 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019 //
00020 //
00021 
00022 // Program for generating data structure used for containing tai-utc
00023 // conversion information (linear transforms and leap seconds).
00024 //
00025 // The program assumes that argv[1] will be the input file, and
00026 // argv[2] the output file.  This works cross-platform without
00027 // worrying about shell redirects of stdin and stdout that are
00028 // not accessible on Windows, apparently.
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <math.h>
00034 
00035 
00036 //--------------------------------------------------------------------------
00037 //   Function-like macro definitions
00038 //--------------------------------------------------------------------------
00039 
00040 #define MemError1( a )    do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )
00041 
00042 const char header[] = ""                                                                                 \
00043                       "/*\n"                                                                             \
00044                       "  This file is part of PLplot.\n"                                                 \
00045                       "  \n"                                                                             \
00046                       "  PLplot is free software; you can redistribute it and/or modify\n"               \
00047                       "  it under the terms of the GNU Library General Public License as published\n"    \
00048                       "  by the Free Software Foundation; either version 2 of the License, or\n"         \
00049                       "  (at your option) any later version.\n"                                          \
00050                       "  \n"                                                                             \
00051                       "  PLplot is distributed in the hope that it will be useful,\n"                    \
00052                       "  but WITHOUT ANY WARRANTY; without even the implied warranty of\n"               \
00053                       "  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"                \
00054                       "  GNU Library General Public License for more details.\n"                         \
00055                       "  \n"                                                                             \
00056                       "  You should have received a copy of the GNU Library General Public License\n"    \
00057                       "  along with PLplot; if not, write to the Free Software\n"                        \
00058                       "  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
00059                       "  \n"                                                                             \
00060                       "  \n"                                                                             \
00061                       "  This header file contains the table containing the linear transforms \n"        \
00062                       "  for converting between TAI and UTC.\n"                                          \
00063                       "  It is an automatically generated file, so please do\n"                          \
00064                       "  not edit it directly. Make any changes to tai-utc.dat then use\n"               \
00065                       "  tai-utc-gen to recreate this header file.\n"                                    \
00066                       "  \n"                                                                             \
00067                       "  tai-utc.dat contains four essential fields to represent the following\n"        \
00068                       "  formula for the linear transformation between TAI and UTC: \n"                  \
00069                       "    TAI-UTC (seconds) = offset1 + (MJD-offset2)*slope\n"                          \
00070                       "  There are four essential fields per line in tai-utc.dat to represent\n"         \
00071                       "  this formula.  They are the Julian date (UTC) where the linear\n"               \
00072                       "  transformation implied by the line is first applied;\n"                         \
00073                       "  offset1 (seconds); offset2 (days), and slope (secs/day).\n"                     \
00074                       "  \n"                                                                             \
00075                       "*/";
00076 
00077 int main( int argc, char *argv[] )
00078 {
00079     FILE   *fr, *fw;
00080     char   readbuffer[256];
00081     int    *MJDstart = NULL;
00082     double *offset1  = NULL;
00083     int    *offset2  = NULL;
00084     double *slope    = NULL;
00085     double sec, *leap_sec = NULL;
00086     int    jd;
00087     int    i = 0;
00088     int    number_of_lines = 0;
00089 
00090     if ( ( fr = fopen( argv[1], "r" ) ) == NULL )
00091     {
00092         fprintf( stderr, "Cannot open first file as readable\n" );
00093         exit( 1 );
00094     }
00095 
00096     if ( ( fw = fopen( argv[2], "w" ) ) == NULL )
00097     {
00098         fprintf( stderr, "Cannot open second file as writable\n" );
00099         exit( 1 );
00100     }
00101 
00102     //
00103     //   Work out how many lines we have all up
00104     //
00105 
00106     while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
00107     {
00108         ++number_of_lines;
00109     }
00110 
00111     //
00112     //   Allocate memory to the arrays which will hold the data
00113     //
00114 
00115     if ( ( MJDstart = (int *) calloc( number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
00116         MemError1( "Allocating memory to the MJDstart table" );
00117 
00118     if ( ( offset1 = (double *) calloc( number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00119         MemError1( "Allocating memory to the offset1 table" );
00120 
00121     if ( ( offset2 = (int *) calloc( number_of_lines, (size_t) sizeof ( int ) ) ) == NULL )
00122         MemError1( "Allocating memory to the offset2 table" );
00123 
00124     if ( ( slope = (double *) calloc( number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00125         MemError1( "Allocating memory to the slope table" );
00126 
00127     if ( ( leap_sec = (double *) calloc( number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00128         MemError1( "Allocating memory to the leap_sec table" );
00129 
00130     rewind( fr ); // Go back to the start of the file
00131 
00132     //
00133     //    Read in line by line, and copy the numbers into our arrays
00134     //
00135 
00136     while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
00137     {
00138         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] );
00139         // Should be exact since all jd's in the file are integer+0.5
00140         MJDstart[i] = jd - 2400000;
00141         i++;
00142     }
00143 
00144     fclose( fr );
00145 
00146 //
00147 //   Write the data out to file ready to be included in our source
00148 //
00149 
00150 
00151     fprintf( fw, "%s\n", header );
00152 
00153     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" );
00154 
00155     fprintf( fw, "const int number_of_entries_in_tai_utc_table=%d;\n\n", number_of_lines );
00156 
00157     fprintf( fw, "const TAI_UTC TAI_UTC_lookup_table[%d] = {\n", number_of_lines );
00158     for ( i = 0; i < number_of_lines; i++ )
00159     {
00160         sec = offset1[i] + (double) ( MJDstart[i] - offset2[i] ) * slope[i];
00161         if ( i == 0 )
00162             leap_sec[i] = 0.;
00163         else
00164             // sec is TAI-UTC in seconds calculated from UTC transformation
00165             // (equation 1 in README.tai-utc).  This calculation must be correct
00166             // for start of epoch range.  However, near end of epoch range where
00167             // ambiguities in UTC occur, must use equivalent TAI transformation
00168             // (equation 2 from same source) to calculate the UTC discontinuity
00169             // unambiguously.
00170             leap_sec[i] = sec - ( offset1[i - 1] + (double) ( MJDstart[i] + sec / 86400. - offset2[i - 1] ) * slope[i - 1] ) / ( 1. + slope[i - 1] / 86400. );
00171         if ( fabs( leap_sec[i] ) < 1.e-14 )
00172             leap_sec[i] = 0.;
00173         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] );
00174     }
00175     fprintf( fw, "};\n" );
00176 
00177     fclose( fw );
00178     free( MJDstart );
00179     free( offset1 );
00180     free( offset2 );
00181     free( slope );
00182     free( leap_sec );
00183 
00184     return ( 0 );
00185 }
00186 

Generated on Wed Oct 12 2011 20:42:23 for PLplot by  doxygen 1.7.1