PLplot: deltaT-gen.c Source File

PLplot 5.10.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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