PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
deltaT-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 spline representation (xspline, yspline,
21 // and y2spline arrays) header from deltaT.dat.
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 #include "dspline.h"
33 
34 
35 //--------------------------------------------------------------------------
36 // Function-like macro definitions
37 //--------------------------------------------------------------------------
38 
39 #define MemError1( a ) do { fprintf( stderr, "MEMORY ERROR %d\n" a "\n", __LINE__ ); exit( __LINE__ ); } while ( 0 )
40 
41 const char header[] = "" \
42  "/*\n" \
43  " This file is part of PLplot.\n" \
44  " \n" \
45  " PLplot is free software; you can redistribute it and/or modify\n" \
46  " it under the terms of the GNU Library General Public License as published\n" \
47  " by the Free Software Foundation; either version 2 of the License, or\n" \
48  " (at your option) any later version.\n" \
49  " \n" \
50  " PLplot is distributed in the hope that it will be useful,\n" \
51  " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
52  " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
53  " GNU Library General Public License for more details.\n" \
54  " \n" \
55  " You should have received a copy of the GNU Library General Public License\n" \
56  " along with PLplot; if not, write to the Free Software\n" \
57  " Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" \
58  " \n" \
59  " \n" \
60  " This header file contains spline data (xspline, yspline, and y2spline)\n" \
61  " for converting between UT1 and ephemeris time.\n" \
62  " It is an automatically generated file, so please do\n" \
63  " not edit it directly. Make any changes to deltaT.dat then use\n" \
64  " deltaT-gen to recreate this header file.\n" \
65  " \n" \
66  "*/";
67 
68 int main( int argc, char *argv[] )
69 {
70  FILE *fr, *fw;
71  char readbuffer[256];
72  double *xspline = NULL;
73  double *yspline = NULL;
74  double *y2spline = NULL;
75  int i = 0;
76  int number_of_lines = 0;
77 
78  if ( ( argc < 2 ) || ( fr = fopen( argv[1], "r" ) ) == NULL )
79  {
80  fprintf( stderr, "Cannot open first file as readable\n" );
81  exit( 1 );
82  }
83 
84  if ( ( argc < 3 ) || ( fw = fopen( argv[2], "w" ) ) == NULL )
85  {
86  fprintf( stderr, "Cannot open second file as writable\n" );
87  exit( 1 );
88  }
89 
90  //
91  // Work out how many lines we have all up
92  //
93 
94  while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
95  {
96  ++number_of_lines;
97  }
98 
99  //
100  // Allocate memory to the arrays which will hold the data
101  //
102 
103  if ( ( xspline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
104  MemError1( "Allocating memory to the xspline table" );
105 
106  if ( ( yspline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
107  MemError1( "Allocating memory to the yspline table" );
108 
109  if ( ( y2spline = (double *) calloc( (size_t) number_of_lines, (size_t) sizeof ( double ) ) ) == NULL )
110  MemError1( "Allocating memory to the y2spline table" );
111 
112  rewind( fr ); // Go back to the start of the file
113 
114  //
115  // Read in line by line, and copy the numbers into our arrays
116  //
117 
118  while ( ( fgets( readbuffer, 255, fr ) != NULL ) )
119  {
120  sscanf( readbuffer, "%lf %lf", (double *) &xspline[i], (double *) &yspline[i] );
121  i++;
122  }
123 
124  fclose( fr );
125  // Calculate spline representation using second derivative condition
126  // on end points that is consistent with overall average parabolic shape
127  // of delta T curve (Morrison and Stephenson, 2004) with second
128  // derivative = 6.4e-3 secs/year/year.
129  dspline( xspline, yspline, number_of_lines, 2, 6.4e-3, 2, 6.4e-3, y2spline );
130 
131 //
132 // Write the data out to file ready to be included in our source
133 //
134 
135 
136  fprintf( fw, "%s\n", header );
137 
138  fprintf( fw, "const int number_of_entries_in_spline_tables=%d;\n\n", number_of_lines );
139 
140  fprintf( fw, "const double xspline[%d] = {\n", number_of_lines );
141  for ( i = 0; i < number_of_lines; i++ )
142  {
143  fprintf( fw, "%10.0f,\n", xspline[i] );
144  }
145  fprintf( fw, "};\n" );
146 
147  fprintf( fw, "const double yspline[%d] = {\n", number_of_lines );
148  for ( i = 0; i < number_of_lines; i++ )
149  {
150  fprintf( fw, "%10.0f,\n", yspline[i] );
151  }
152  fprintf( fw, "};\n" );
153 
154  fprintf( fw, "const double y2spline[%d] = {\n", number_of_lines );
155  for ( i = 0; i < number_of_lines; i++ )
156  {
157  fprintf( fw, "%25.15e,\n", y2spline[i] );
158  }
159  fprintf( fw, "};\n" );
160 
161  fclose( fw );
162  free( xspline );
163  free( yspline );
164  free( y2spline );
165 
166  return ( 0 );
167 }
168