PLplot  5.10.0
deltaT-gen.c
Go to the documentation of this file.
00001 //  Copyright (C) 2009-2014 Alan W. Irwin
00002 //
00003 //  This file is part of PLplot.
00004 //  PLplot is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU Library General Public License as published
00006 //  by the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  PLplot is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU Library General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU Library General Public License
00015 //  along with PLplot; if not, write to the Free Software
00016 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00017 //
00018 //
00019 
00020 // Program for generating spline representation (xspline, yspline,
00021 // and y2spline arrays) header from deltaT.dat.
00022 //
00023 // The program assumes that argv[1] will be the input file, and
00024 // argv[2] the output file.  This works cross-platform without
00025 // worrying about shell redirects of stdin and stdout that are
00026 // not accessible on Windows, apparently.
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <math.h>
00032 #include "dspline.h"
00033 
00034 
00035 //--------------------------------------------------------------------------
00036 //   Function-like macro definitions
00037 //--------------------------------------------------------------------------
00038 
00039 #define MemError1( a )    do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )
00040 
00041 const char header[] = ""                                                                                 \
00042                       "/*\n"                                                                             \
00043                       "  This file is part of PLplot.\n"                                                 \
00044                       "  \n"                                                                             \
00045                       "  PLplot is free software; you can redistribute it and/or modify\n"               \
00046                       "  it under the terms of the GNU Library General Public License as published\n"    \
00047                       "  by the Free Software Foundation; either version 2 of the License, or\n"         \
00048                       "  (at your option) any later version.\n"                                          \
00049                       "  \n"                                                                             \
00050                       "  PLplot is distributed in the hope that it will be useful,\n"                    \
00051                       "  but WITHOUT ANY WARRANTY; without even the implied warranty of\n"               \
00052                       "  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"                \
00053                       "  GNU Library General Public License for more details.\n"                         \
00054                       "  \n"                                                                             \
00055                       "  You should have received a copy of the GNU Library General Public License\n"    \
00056                       "  along with PLplot; if not, write to the Free Software\n"                        \
00057                       "  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
00058                       "  \n"                                                                             \
00059                       "  \n"                                                                             \
00060                       "  This header file contains spline data (xspline, yspline, and y2spline)\n"       \
00061                       "  for converting between UT1 and ephemeris time.\n"                               \
00062                       "  It is an automatically generated file, so please do\n"                          \
00063                       "  not edit it directly. Make any changes to deltaT.dat then use\n"                \
00064                       "  deltaT-gen to recreate this header file.\n"                                     \
00065                       "  \n"                                                                             \
00066                       "*/";
00067 
00068 int main( int argc, char *argv[] )
00069 {
00070     FILE   *fr, *fw;
00071     char   readbuffer[256];
00072     double *xspline        = NULL;
00073     double *yspline        = NULL;
00074     double *y2spline       = NULL;
00075     int    i               = 0;
00076     int    number_of_lines = 0;
00077 
00078     if ( ( argc < 2 ) || ( fr = fopen( argv[1], "r" ) ) == NULL )
00079     {
00080         fprintf( stderr, "Cannot open first file as readable\n" );
00081         exit( 1 );
00082     }
00083 
00084     if ( ( argc < 3 ) || ( fw = fopen( argv[2], "w" ) ) == NULL )
00085     {
00086         fprintf( stderr, "Cannot open second file as writable\n" );
00087         exit( 1 );
00088     }
00089 
00090     //
00091     //   Work out how many lines we have all up
00092     //
00093 
00094     while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
00095     {
00096         ++number_of_lines;
00097     }
00098 
00099     //
00100     //   Allocate memory to the arrays which will hold the data
00101     //
00102 
00103     if ( ( xspline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00104         MemError1( "Allocating memory to the xspline table" );
00105 
00106     if ( ( yspline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00107         MemError1( "Allocating memory to the yspline table" );
00108 
00109     if ( ( y2spline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
00110         MemError1( "Allocating memory to the y2spline table" );
00111 
00112     rewind( fr ); // Go back to the start of the file
00113 
00114     //
00115     //    Read in line by line, and copy the numbers into our arrays
00116     //
00117 
00118     while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
00119     {
00120         sscanf( readbuffer, "%lf %lf", (double *) &xspline[i], (double *) &yspline[i] );
00121         i++;
00122     }
00123 
00124     fclose( fr );
00125     // Calculate spline representation using second derivative condition
00126     // on end points that is consistent with overall average parabolic shape
00127     // of delta T curve (Morrison and Stephenson, 2004) with second
00128     // derivative = 6.4e-3 secs/year/year.
00129     dspline( xspline, yspline, number_of_lines, 2, 6.4e-3, 2, 6.4e-3, y2spline );
00130 
00131 //
00132 //   Write the data out to file ready to be included in our source
00133 //
00134 
00135 
00136     fprintf( fw, "%s\n", header );
00137 
00138     fprintf( fw, "const int number_of_entries_in_spline_tables=%d;\n\n", number_of_lines );
00139 
00140     fprintf( fw, "const double xspline[%d] = {\n", number_of_lines );
00141     for ( i = 0; i < number_of_lines; i++ )
00142     {
00143         fprintf( fw, "%10.0f,\n", xspline[i] );
00144     }
00145     fprintf( fw, "};\n" );
00146 
00147     fprintf( fw, "const double yspline[%d] = {\n", number_of_lines );
00148     for ( i = 0; i < number_of_lines; i++ )
00149     {
00150         fprintf( fw, "%10.0f,\n", yspline[i] );
00151     }
00152     fprintf( fw, "};\n" );
00153 
00154     fprintf( fw, "const double y2spline[%d] = {\n", number_of_lines );
00155     for ( i = 0; i < number_of_lines; i++ )
00156     {
00157         fprintf( fw, "%25.15e,\n", y2spline[i] );
00158     }
00159     fprintf( fw, "};\n" );
00160 
00161     fclose( fw );
00162     free( xspline );
00163     free( yspline );
00164     free( y2spline );
00165 
00166     return ( 0 );
00167 }
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines