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

deltaT-gen.c

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

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