📄 almanac.c
字号:
/* The struct, oldalmstruc, is used to conserve backward compatability with
GPSBLDR-1 style almanac files. In GPSBLDR-1 almanac files the non-integer
almstruc components are type 'double'. In GPSBLDR-2 they are type 'float'
within the program but type 'double' when saved and read from file. */
typedef struct {
int vflg; /* 0=No Info, 1=Valid Almanac, 2=Nonexistent SV */
int almhlth; /* Almanac health code */
int refweek; /* Reference time, GPS week */
double toa; /* Reference time, seconds of refweek */
double ecc; /* Orbital eccentricity */
double inclin; /* Orbital inclination (radians) */
double rora; /* Rate of right ascension (radians/sec) */
double sqrta; /* SQRT(orbital semimajor axis in meters) */
double ratoa; /* Right ascension at ref time (radians) */
double argpg; /* Argument of perigee at ref time (radians) */
double manom; /* Mean anomaly at ref time (radians) */
double af0; /* Clock correction at ref time (seconds) */
double af1; /* Clock correction rate (seconds/second) */
} oldalmstruc;
#include "includes.h"
/****************************************************************************
* Function: void ReadAlmanacs(void)
*
* Open the almanac file ALM.INI and read in the alamanac, iono/UTC and
* ephemeris data.
*
* Input: None.
*
* Output: None.
*
* Return Value: None.
****************************************************************************/
void ReadAlmanacs(void)
{
char buff[90];
int almanac;
FILE *fpAlmanacFile,*fopen();
logical AlmanacFilePresent;
oldalmstruc readalm;
/* Open up default almanac file ALM.INI */
if((fpAlmanacFile=fopen("ALM.INI","rb"))==NULL)
{
printf("\n\n*** ERROR ***");
printf("\nCould not open almanac file ALM.INI.");
printf("\nPress ALT-X to terminate or any other key to continue "
"in Cold Start mode.\n");
while(!kbhit()) continue;
if(GetKey()==ALTX)
QuitGpsBuilder();
TrackMode = COLD_START;
}
else
{
/* Check for the file header "ALMANACF" */
fread(&buff,8,1,fpAlmanacFile);
if(memcmp(buff,"ALMANACF",8)==0)
{
AlmanacFilePresent = TRUE;
for(almanac=0;almanac<MAXSATELLITES;almanac++)
{
fread(&readalm,sizeof(oldalmstruc),1,fpAlmanacFile);
/* Copy the read data into the internal almanac structure,
with data type conversions as appropriate. */
alms[almanac].vflg = readalm.vflg;
alms[almanac].almhlth = readalm.almhlth;
alms[almanac].refweek = readalm.refweek;
alms[almanac].toa = (float)readalm.toa;
alms[almanac].ecc = (float)readalm.ecc;
alms[almanac].inclin = (float)readalm.inclin;
alms[almanac].rora = (float)readalm.rora;
alms[almanac].sqrta = (float)readalm.sqrta;
alms[almanac].ratoa = (float)readalm.ratoa;
alms[almanac].argpg = (float)readalm.argpg;
alms[almanac].manom = (float)readalm.manom;
alms[almanac].af0 = (float)readalm.af0;
alms[almanac].af1 = (float)readalm.af1;
}
fread(&ionoutc,sizeof(iustruc),1,fpAlmanacFile);
fread(&ephs[0],sizeof(ephstruc),MAXSATELLITES,fpAlmanacFile);
}
else
{
AlmanacFilePresent = FALSE;
printf("\n\n*** ERROR ***");
printf("\nFile ALM.INI is not a valid almanac file.");
printf("\nPress ALT-X to terminate or any other key to continue "
"in Cold Start mode.\n");
while(!kbhit()) continue;
if(GetKey()==ALTX)
QuitGpsBuilder();
TrackMode = COLD_START;
}
fclose(fpAlmanacFile);
} /* End of open almanac file. */
/* Check which alamanacs are present and validate those which are. */
if(AlmanacFilePresent)
{
ValidateAlmanacs();
ValidateEphemerides();
}
}
/****************************************************************************
* Function: void ValidateAlmanacs(void)
*
* Checks first that the almanac for the satellite in question is present and
* then validates in contents.
*
* Input: None.
*
* Output: None.
*
* Return Value: None.
****************************************************************************/
void ValidateAlmanacs(void)
{
int AlmanacCounter;
logical AlmanacValid[MAXSATELLITES];
logical AlmanacMissing[MAXSATELLITES];
logical BadAlmanac;
BadAlmanac = FALSE;
for(AlmanacCounter=0; AlmanacCounter<MAXSATELLITES;AlmanacCounter++)
{
if(alms[AlmanacCounter].vflg==TRUE)
{
AlmanacMissing[AlmanacCounter] = FALSE;
AlmanacValid[AlmanacCounter] = TRUE;
}
else
{
BadAlmanac = TRUE;
AlmanacMissing[AlmanacCounter] = TRUE;
}
}
if(BadAlmanac)
{
printf("\n\n***** WARNING *****");
printf("\nAlmanacs did not exist or the data was corrupted for "
"the following PRNs:\n\n");
for(AlmanacCounter=0; AlmanacCounter<MAXSATELLITES;AlmanacCounter++)
{
if(AlmanacMissing[AlmanacCounter]==TRUE ||
AlmanacValid[AlmanacCounter]==FALSE)
printf("%2.2d ",AlmanacCounter+1);
}
} /* End of BadAlmanac. */
}
/****************************************************************************
* Function: void SaveAlmanacs(char *fname)
*
* Save almanacs, ionospheric/UTC models, and current ephemerides to file.
*
* Input: fname - the name of the file to which to save the data.
*
* Output: None.
*
* Return Value: None.
****************************************************************************/
void SaveAlmanacs(char *fname)
{
FILE *fpAlmanacFile;
int almanac;
oldalmstruc writealm;
fpAlmanacFile = fopen(fname,"wb");
if(fpAlmanacFile!=NULL)
{
rewind(fpAlmanacFile);
fwrite("ALMANACF",8,1,fpAlmanacFile); /* Identifier */
for(almanac=0;almanac<MAXSATELLITES;almanac++)
{
/* Copy the data from the internal almanac structure into the
write data, with data type conversions as appropriate. */
writealm.vflg = alms[almanac].vflg;
writealm.almhlth = alms[almanac].almhlth;
writealm.refweek = alms[almanac].refweek;
writealm.toa = (double)alms[almanac].toa;
writealm.ecc = (double)alms[almanac].ecc;
writealm.inclin = (double)alms[almanac].inclin;
writealm.rora = (double)alms[almanac].rora;
writealm.sqrta = (double)alms[almanac].sqrta;
writealm.ratoa = (double)alms[almanac].ratoa;
writealm.argpg = (double)alms[almanac].argpg;
writealm.manom = (double)alms[almanac].manom;
writealm.af0 = (double)alms[almanac].af0;
writealm.af1 = (double)alms[almanac].af1;
fwrite(&writealm,sizeof(oldalmstruc),1,fpAlmanacFile);
}
fwrite(&ionoutc,sizeof(iustruc),1,fpAlmanacFile);
fwrite(&ephs[0],sizeof(ephstruc),MAXSATELLITES,fpAlmanacFile);
fclose(fpAlmanacFile);
}
else
WarningMessage("Can't open almanac file");
}
/****************************************************************************
* Function: void ValidateEphemerides(void)
*
* Validate ephemeris (checks if older than 4 hours).
*
* Input: None.
*
* Output: None.
*
* Return Value: None.
****************************************************************************/
void ValidateEphemerides(void)
{
int satellite;
double age;
unsigned long current_TIC;
int gps_week;
double gps_second;
CurrentTIC(¤t_TIC);
TICToGpsTime(current_TIC,&gps_week,&gps_second);
for(satellite=0;satellite<MAXSATELLITES;satellite++)
{
PROTECT++;
if(ephs[satellite].vflg==VALID)
{
age = SECONDS_IN_WEEK*(gps_week-ephs[satellite].toewk)
+ gps_second - ephs[satellite].toe;
if(fabs(age)>FOUR_HOURS) ephs[satellite].vflg = INVALID;
}
PROTECT--;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -