sem.c

Go to the documentation of this file.
00001 /**
00002 \file    sem.c
00003 \brief   GNSS core 'c' function library: SEM format almanacs.
00004 \author  Glenn D. MacGougan (GDM)
00005 \date    2006-11-10
00006 \since   2005-08-14
00007 
00008 \b "LICENSE INFORMATION" \n
00009 Copyright (c) 2007, refer to 'author' doxygen tags \n
00010 All rights reserved. \n
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided the following conditions are met: \n
00014 
00015 - Redistributions of source code must retain the above copyright
00016   notice, this list of conditions and the following disclaimer. \n
00017 - Redistributions in binary form must reproduce the above copyright
00018   notice, this list of conditions and the following disclaimer in the
00019   documentation and/or other materials provided with the distribution. \n
00020 - The name(s) of the contributor(s) may not be used to endorse or promote 
00021   products derived from this software without specific prior written 
00022   permission. \n
00023 
00024 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00025 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00028 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00030 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00032 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00033 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00034 SUCH DAMAGE.
00035 */
00036 
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include "sem.h"
00040 #include "constants.h"
00041 
00042 #ifndef WIN32
00043 #define _CRT_SECURE_NO_DEPRECATE
00044 #endif
00045 
00046 
00047 BOOL SEM_ReadAlmanacDataFromFile(
00048   const char* semFilePath,    //!< path to the input SEM ASCII file
00049   SEM_structAlmanac* alm,     //!< pointer to an array of SEM almanac structs
00050   unsigned char  max_to_read, //!< length of the array
00051   unsigned char* number_read  //!< number of almanac items read
00052   )
00053 {
00054   FILE* in;                   // the input file pointer
00055   int n;                      // counter to indicate the number of items read by sscanf
00056   unsigned char i;            // counter
00057   unsigned utmp;              // tmp used in fscanf input
00058   unsigned number_of_records; // number of records in file
00059   unsigned week;              // gps week [week]
00060   unsigned toa;               // time of almanac applicability [s]
00061   double dtmp;                // tmp used in fscanf input
00062   char description[128];      // file descriptor
00063   
00064   // initialize
00065   *number_read = 0;  
00066 
00067   // check stupid error
00068   if( max_to_read < 1 )
00069     return FALSE;
00070 
00071 #ifndef _CRT_SECURE_NO_DEPRECATE
00072   if( fopen_s( &in, semFilePath, "r" ) != 0 )
00073     return FALSE;
00074   if( !in )
00075     return FALSE;
00076 
00077   n = fscanf_s( in, "%u", &number_of_records ); if(n != 1){return FALSE;}
00078   if( number_of_records < 1 )
00079     return FALSE;
00080 
00081   n = fscanf_s( in, "%s", &description ); if(n != 1){return FALSE;}
00082   n = fscanf_s( in, "%u", &week ); if(n != 1){return FALSE;}
00083   n = fscanf_s( in, "%u", &toa );  if(n != 1){return FALSE;}
00084 
00085 
00086   for( i = 0; i < number_of_records && i < max_to_read; i++ )
00087   {
00088     alm[i].week = (unsigned short) week;
00089     alm[i].toa  = toa;
00090     n = fscanf_s( in, "%u", &utmp );  if(n != 1){break;} alm[i].prn = (unsigned short)utmp; if(n != 1){return FALSE;}
00091     n = fscanf_s( in, "%u", &utmp );  if(n != 1){break;} alm[i].svn = (unsigned short)utmp; if(n != 1){return FALSE;}
00092     n = fscanf_s( in, "%u", &utmp );  if(n != 1){break;} alm[i].ura = (unsigned char)utmp;  if(n != 1){return FALSE;}
00093     
00094     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].ecc      = dtmp;    if(n != 1){return FALSE;}
00095     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].i0       = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00096     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omegadot = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00097     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].sqrta    = dtmp;    if(n != 1){return FALSE;}
00098     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omega0   = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00099     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].w        = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00100     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].m0       = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00101     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af0      = dtmp;    if(n != 1){return FALSE;}
00102     n = fscanf_s( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af1      = dtmp;    if(n != 1){return FALSE;}
00103 
00104     n = fscanf_s( in, "%u", &utmp );  if(n != 1){break;} alm[i].health      = (unsigned char)utmp; if(n != 1){return FALSE;}
00105     n = fscanf_s( in, "%u", &utmp );  if(n != 1){break;} alm[i].config_code = (unsigned char)utmp; if(n != 1){return FALSE;}
00106 
00107     *number_read = (unsigned char)(i+1);
00108   }
00109   
00110   fclose(in);
00111 #else
00112   in = fopen( semFilePath, "r" );
00113   if( !in )
00114     return FALSE;
00115 
00116   n = fscanf( in, "%u", &number_of_records ); if(n != 1){return FALSE;}
00117   if( number_of_records < 1 )
00118     return FALSE;
00119 
00120   n = fscanf( in, "%s", &description ); if(n != 1){return FALSE;}
00121   n = fscanf( in, "%u", &week ); if(n != 1){return FALSE;}
00122   n = fscanf( in, "%u", &toa );  if(n != 1){return FALSE;}
00123 
00124 
00125   for( i = 0; i < number_of_records && i < max_to_read; i++ )
00126   {
00127     alm[i].week = (unsigned short) week;
00128     alm[i].toa  = toa;
00129     n = fscanf( in, "%u", &utmp );  if(n != 1){break;} alm[i].prn = (unsigned short)utmp; if(n != 1){return FALSE;}
00130     n = fscanf( in, "%u", &utmp );  if(n != 1){break;} alm[i].svn = (unsigned short)utmp; if(n != 1){return FALSE;}
00131     n = fscanf( in, "%u", &utmp );  if(n != 1){break;} alm[i].ura = (unsigned char)utmp;  if(n != 1){return FALSE;}
00132     
00133     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].ecc      = dtmp;    if(n != 1){return FALSE;}
00134     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].i0       = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00135     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omegadot = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00136     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].sqrta    = dtmp;    if(n != 1){return FALSE;}
00137     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].omega0   = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00138     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].w        = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00139     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].m0       = dtmp*PI; if(n != 1){return FALSE;} // convert to radians
00140     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af0      = dtmp;    if(n != 1){return FALSE;}
00141     n = fscanf( in, "%lf", &dtmp ); if(n != 1){break;} alm[i].af1      = dtmp;    if(n != 1){return FALSE;}
00142 
00143     n = fscanf( in, "%u", &utmp );  if(n != 1){break;} alm[i].health      = (unsigned char)utmp; if(n != 1){return FALSE;}
00144     n = fscanf( in, "%u", &utmp );  if(n != 1){break;} alm[i].config_code = (unsigned char)utmp; if(n != 1){return FALSE;}
00145 
00146     *number_read = (unsigned char)(i+1);
00147   }
00148   
00149   fclose(in);
00150 #endif
00151 
00152   return TRUE;
00153 }
00154 
00155 
00156 BOOL SEM_WriteAlmanacDataToFile(
00157   const char* semFilePath,       //!< path to the output SEM ASCII file
00158   SEM_structAlmanac* alm,        //!< pointer to an array of SEM almanac structs
00159   unsigned char number_to_write  //!< length of the array
00160   )
00161 {
00162   char buffer[1024];
00163   unsigned char i;
00164 
00165   FILE* out;
00166 
00167 #ifndef _CRT_SECURE_NO_DEPRECATE
00168   if( fopen_s( &out, semFilePath, "w") != 0 )
00169     return FALSE;
00170   if( !out )
00171     return FALSE;
00172 #else
00173   out = fopen(semFilePath, "w");
00174   if( !out )
00175     return FALSE;
00176 #endif
00177 
00178   fprintf( out, "%d %s\n", number_to_write, "CURRENT.ALM" );
00179   fprintf( out, "%d %d\n\n", alm[0].week, alm[0].toa );
00180 
00181   for( i = 0; i < number_to_write; i++ )
00182   {
00183     SEM_WriteSingleAlmanacElementToBuffer( alm[i], buffer, 1024 );
00184     fprintf( out, "%s", buffer );
00185   }
00186 
00187   fclose(out);
00188   return TRUE;
00189 }
00190 
00191 
00192 BOOL SEM_WriteSingleAlmanacElementToBuffer(
00193   SEM_structAlmanac alm,     //!< SEM almanac struct
00194   char* buffer,              //!< buffer to write the SEM struct information
00195   unsigned short bufferSize  //!< size of the buffer, must be greater than 1024 bytes
00196   )
00197 {
00198   int scount = 0;
00199 
00200 #ifndef _CRT_SECURE_NO_DEPRECATE
00201   int dcount = 0;
00202   if( bufferSize < 1024 )
00203   {
00204     sprintf_s( buffer, bufferSize, "" );
00205     return FALSE;
00206   }
00207   dcount = sprintf_s( buffer+scount, bufferSize-scount, "%d\n%d\n%d\n", alm.prn, alm.svn, alm.ura );                          if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00208   dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.ecc, alm.i0/PI, alm.omegadot/PI );  if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00209   dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.sqrta, alm.omega0/PI, alm.w/PI );   if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00210   dcount = sprintf_s( buffer+scount, bufferSize-scount, "%-23.14g %23.14g %23.14g\n", alm.m0/PI, alm.af0, alm.af1 );          if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }  
00211   dcount = sprintf_s( buffer+scount, bufferSize-scount, "%d\n%d\n\n", alm.health, alm.config_code );                          if( dcount < 0 ){ return FALSE; }else{ scount += dcount; }
00212 #else
00213   if( bufferSize < 1024 )
00214   {
00215     sprintf( buffer, "" );
00216     return FALSE;
00217   }
00218   scount += sprintf( buffer+scount, "%d\n%d\n%d\n", alm.prn, alm.svn, alm.ura );
00219   scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.ecc, alm.i0/PI, alm.omegadot/PI );
00220   scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.sqrta, alm.omega0/PI, alm.w/PI );
00221   scount += sprintf( buffer+scount, "%-23.14g %23.14g %23.14g\n", alm.m0/PI, alm.af0, alm.af1 );
00222   scount += sprintf( buffer+scount, "%d\n%d\n\n", alm.health, alm.config_code );    
00223 #endif
00224 
00225   return TRUE;
00226 }
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 /*
00235 // SEM TEST CODE
00236 #include <stdio.h>
00237 #include <conio.h>
00238 #include <stdlib.h>
00239 #include <memory.h>
00240 #include "../zen/open/include/yuma.h"
00241 
00242 int main( int argc, char* argv[] )
00243 {
00244   SEM_structAlmanac alm[32];
00245   unsigned char number_read=0;
00246   unsigned char i = 0;
00247   char buffer[1024];
00248 
00249   memset( alm, 0, sizeof(SEM_structAlmanac)*32 );
00250   SEM_ReadAlmanacDataFromFile( 
00251     "SEM312.txt",
00252     alm,
00253     32,
00254     &number_read );
00255 
00256   for( i = 0; i < number_read; i++ )
00257   {
00258     SEM_WriteSingleAlmanacElementToBuffer( alm[i], buffer, 1024 );
00259     printf( "%s\n", buffer );
00260     getch();
00261   }
00262   
00263   SEM_WriteAlmanacDataToFile( "yuma.txt", alm, number_read );
00264   
00265   getch();
00266 
00267   return 0;
00268 }
00269 */