00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include "yuma.h"
00040
00041 #ifndef WIN32
00042 #define _CRT_SECURE_NO_DEPRECATE
00043 #endif
00044
00045 BOOL YUMA_ReadAlmanacDataFromFile(
00046 const char* yumaFilePath,
00047 YUMA_structAlmanac* alm,
00048 unsigned char max_to_read,
00049 unsigned char* number_read
00050 )
00051 {
00052 FILE* in;
00053 char buffer[512];
00054 char synca, syncb;
00055 int syncFound = 0;
00056 int n;
00057 int i;
00058 unsigned utmp;
00059
00060
00061 *number_read = 0;
00062
00063
00064 if( max_to_read < 1 )
00065 return FALSE;
00066
00067 #ifndef _CRT_SECURE_NO_DEPRECATE
00068 if( fopen_s( &in, yumaFilePath, "r" ) != 0 )
00069 return FALSE;
00070 if( !in )
00071 return FALSE;
00072 #else
00073 in = fopen( yumaFilePath, "r" );
00074 if( !in )
00075 return FALSE;
00076 #endif
00077
00078
00079 synca = (char)fgetc(in);
00080 while( !feof(in) )
00081 {
00082 syncb = (char)fgetc(in);
00083 if( synca == 'I' && syncb == 'D' )
00084 {
00085 syncFound = 1;
00086 break;
00087 }
00088 else
00089 {
00090 synca = syncb;
00091 }
00092 }
00093 if( !syncFound )
00094 {
00095 fclose(in);
00096 return FALSE;
00097 }
00098
00099
00100 fseek(in, -2, SEEK_CUR );
00101
00102 i = 0;
00103 while( !feof(in) && i < max_to_read )
00104 {
00105 #ifndef _CRT_SECURE_NO_DEPRECATE
00106 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "ID: %u", &utmp ); if( n != 1 ){break;} alm[i].prn = (unsigned short)utmp;
00107 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Health: %u", &utmp ); if( n != 1 ){break;} alm[i].health = (unsigned char)utmp;
00108 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Eccentricity: %lf", &(alm[i].ecc) ); if( n != 1 ){break;}
00109 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Time of Applicability(s): %lf", &(alm[i].toa) ); if( n != 1 ){break;}
00110 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Orbital Inclination(rad): %lf", &(alm[i].i0) ); if( n != 1 ){break;}
00111 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Rate of Right Ascen(r/s): %lf", &(alm[i].omegadot) ); if( n != 1 ){break;}
00112 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "SQRT(A) (m 1/2): %lf", &(alm[i].sqrta) ); if( n != 1 ){break;}
00113 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Right Ascen at Week(rad): %lf", &(alm[i].omega0) ); if( n != 1 ){break;}
00114 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Argument of Perigee(rad): %lf", &(alm[i].w) ); if( n != 1 ){break;}
00115 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Mean Anom(rad): %lf", &(alm[i].m0) ); if( n != 1 ){break;}
00116 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Af0(s): %lf", &(alm[i].af0) ); if( n != 1 ){break;}
00117 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "Af1(s/s): %lf", &(alm[i].af1) ); if( n != 1 ){break;}
00118 if( !fgets( buffer, 512, in ) ){break;} n = sscanf_s( buffer, "week: %u", &utmp ); if( n != 1 ){break;} alm[i].week = (unsigned short)utmp;
00119 #else
00120 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "ID: %u", &utmp ); if( n != 1 ){break;} alm[i].prn = (unsigned short)utmp;
00121 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Health: %u", &utmp ); if( n != 1 ){break;} alm[i].health = (unsigned char)utmp;
00122 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Eccentricity: %lf", &(alm[i].ecc) ); if( n != 1 ){break;}
00123 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Time of Applicability(s): %lf", &(alm[i].toa) ); if( n != 1 ){break;}
00124 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Orbital Inclination(rad): %lf", &(alm[i].i0) ); if( n != 1 ){break;}
00125 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Rate of Right Ascen(r/s): %lf", &(alm[i].omegadot) ); if( n != 1 ){break;}
00126 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "SQRT(A) (m 1/2): %lf", &(alm[i].sqrta) ); if( n != 1 ){break;}
00127 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Right Ascen at Week(rad): %lf", &(alm[i].omega0) ); if( n != 1 ){break;}
00128 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Argument of Perigee(rad): %lf", &(alm[i].w) ); if( n != 1 ){break;}
00129 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Mean Anom(rad): %lf", &(alm[i].m0) ); if( n != 1 ){break;}
00130 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Af0(s): %lf", &(alm[i].af0) ); if( n != 1 ){break;}
00131 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "Af1(s/s): %lf", &(alm[i].af1) ); if( n != 1 ){break;}
00132 if( !fgets( buffer, 512, in ) ){break;} n = sscanf( buffer, "week: %u", &utmp ); if( n != 1 ){break;} alm[i].week = (unsigned short)utmp;
00133 #endif
00134 alm[i].is_af0_af1_high_precision = 0;
00135 alm[i].reserved1 = 0;
00136 i++;
00137
00138
00139 syncFound = FALSE;
00140 synca = (char)fgetc(in);
00141 while( !feof(in) )
00142 {
00143 syncb = (char)fgetc(in);
00144 if( synca == 'I' && syncb == 'D' )
00145 {
00146 syncFound = 1;
00147 break;
00148 }
00149 else
00150 {
00151 synca = syncb;
00152 }
00153 }
00154 if( !syncFound )
00155 {
00156 break;
00157 }
00158
00159
00160 fseek(in, -2, SEEK_CUR );
00161 }
00162
00163 *number_read = (unsigned char)i;
00164
00165 fclose(in);
00166
00167 return TRUE;
00168 }
00169
00170
00171 BOOL YUMA_WriteAlmanacDataToFile(
00172 const char* yumaFilePath,
00173 YUMA_structAlmanac* alm,
00174 unsigned char number_to_write
00175 )
00176 {
00177 char buffer[1024];
00178 unsigned char i;
00179
00180 FILE* out;
00181 #ifndef _CRT_SECURE_NO_DEPRECATE
00182 if( fopen_s( &out, yumaFilePath, "w") != 0 )
00183 return FALSE;
00184 if( !out )
00185 return FALSE;
00186 #else
00187 out = fopen(yumaFilePath, "w");
00188 if( !out )
00189 return FALSE;
00190 #endif
00191
00192 for( i = 0; i < number_to_write; i++ )
00193 {
00194 YUMA_WriteSingleAlmanacElementToBuffer( alm[i], buffer, 1024 );
00195 fprintf( out, "%s", buffer );
00196 }
00197
00198 fclose(out);
00199 return TRUE;
00200 }
00201
00202
00203 BOOL YUMA_WriteSingleAlmanacElementToBuffer(
00204 YUMA_structAlmanac alm,
00205 char* buffer,
00206 unsigned short bufferSize
00207 )
00208 {
00209 int scount = 0;
00210
00211 #ifndef _CRT_SECURE_NO_DEPRECATE
00212 int dcount = 0;
00213 if( bufferSize < 1024 )
00214 {
00215 sprintf_s( buffer, bufferSize, "" );
00216 return FALSE;
00217 }
00218 dcount = sprintf_s( buffer+scount, bufferSize-scount, "******** Week %03d almanac for PRN-%02d ********\n", alm.week, alm.prn ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00219 dcount = sprintf_s( buffer+scount, bufferSize-scount, "ID: % 03d\n", alm.prn ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00220 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Health: % 04d\n", alm.health ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00221 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Eccentricity: % .10g\n", alm.ecc ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00222 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Time of Applicability(s): % .4f\n", alm.toa ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00223 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Orbital Inclination(rad): % .10g\n", alm.i0 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00224 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Rate of Right Ascen(r/s): % .10g\n", alm.omegadot ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00225 dcount = sprintf_s( buffer+scount, bufferSize-scount, "SQRT(A) (m 1/2): % .6f\n", alm.sqrta ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00226 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Right Ascen at Week(rad): % .10g\n", alm.omega0 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00227 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Argument of Perigee(rad): % .10g\n", alm.w ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00228 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Mean Anom(rad): % .10g\n", alm.m0 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00229 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Af0(s): % .10g\n", alm.af0 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00230 dcount = sprintf_s( buffer+scount, bufferSize-scount, "Af1(s/s): % .10g\n", alm.af1 ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00231 dcount = sprintf_s( buffer+scount, bufferSize-scount, "week: % 03d\n\n",alm.week ); if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00232 #else
00233 if( bufferSize < 1024 )
00234 {
00235 sprintf( buffer, "" );
00236 return FALSE;
00237 }
00238 scount += sprintf( buffer+scount, "******** Week %03d almanac for PRN-%02d ********\n", alm.week, alm.prn );
00239 scount += sprintf( buffer+scount, "ID: %02d\n", alm.prn );
00240 scount += sprintf( buffer+scount, "Health: %03d\n", alm.health );
00241 scount += sprintf( buffer+scount, "Eccentricity: %.10g\n", alm.ecc );
00242 scount += sprintf( buffer+scount, "Time of Applicability(s): %.4f\n", alm.toa );
00243 scount += sprintf( buffer+scount, "Orbital Inclination(rad): %.10g\n", alm.i0 );
00244 scount += sprintf( buffer+scount, "Rate of Right Ascen(r/s): %.10g\n", alm.omegadot );
00245 scount += sprintf( buffer+scount, "SQRT(A) (m 1/2): %.6f\n", alm.sqrta );
00246 scount += sprintf( buffer+scount, "Right Ascen at Week(rad): %.10g\n", alm.omega0 );
00247 scount += sprintf( buffer+scount, "Argument of Perigee(rad): %.10g\n", alm.w );
00248 scount += sprintf( buffer+scount, "Mean Anom(rad): %.10g\n", alm.m0 );
00249 scount += sprintf( buffer+scount, "Af0(s): %.10g\n", alm.af0 );
00250 scount += sprintf( buffer+scount, "Af1(s/s): %.10g\n", alm.af1 );
00251 scount += sprintf( buffer+scount, "week: %03d\n\n",alm.week );
00252 #endif
00253 return TRUE;
00254 }
00255