#include "rinex.h"
#include "sv.h"
#define ASHTECH 1
#define TRIMBLE 2
#define NAVSYMM 3
#define MOTOROLA 4
#define NOVATEL 5
#define EG_ASCII 0
#define EG_BINARY 1
/* globe varition */
RinexObsHeaderType rinex_obs_header;
char err_str[64]; /* string containing error message */
int WeekNo;
int MeasType[9];
/* each 10 words (3 bytes per word) corresponding to words 1-10
of subframes 1-3 of the satellite nav message. */
u1 sub1[10][3];
u1 sub2[10][3];
u1 sub3[10][3];
int EphPrn;
u1 hex_ephemeris[24][3] ;
/* [word][byte] : 24 words (3 bytes per word) corresponding to words 3-10
of subframes 1-3 of the satellite nav message. */
double tphase[3][8];
double F1[3][8],F2[3][8];
int Count1 = 0, Count2 =0;
/* programm describe */
int Ashtech2Rinex(char *SourceFile,char *ObsFile);
int Ashtech21Rinex(char *SourceFile,char *NavFile);
int ReadAshEpoch(FILE *f,int numobs,RinexEpochType *RinexEpoch);
int ReadAshEph(FILE *f,SVText *eph);
void Navsymm21Rinex(char *EPPFile,char *RinexNavFile);
int ReadNavsymmEph( FILE *f1,SVText *snv );
int Navsymm2Rinex(char *SourceFile,char *RinexFile);
int ReadNavstarEpoch(FILE *fname, RinexEpochType *RinexEpoch);
int Trimble2Rinex(char *SourceFile,char *RinexObsFile,char *RinexNavFile);
int ReadType11(FILE *fs);
int ReadType17(FILE *fs, RinexEpochType *Epoch);
int ReadType3(int len,FILE *fs,SVText far *eph,FILE *fn);
int ReadType21(int len,FILE *fs, SVText *eph,FILE *fn);
int Novatel2Rinex(char *SourceFile,char *RinexFile,char *RinexNavFile);
void ReadNovatelPos(char *s, float *X,float *Y,float *Z);
void ReadNovatelEpoch(char *s,RinexEpochType *RinexEpoch);
int ReadNovatelEph(char *s,SVText *snv);
void Convert_Ephemeris(SVText *eph);
i2 Hex(i1 ASCIIvalue);
int Motorola2Rinex(char *SourceFile,char *ObsFile,char *NavFile);
void ReadMotoEpoch(unsigned char *buf,RinexEpochType *RinexEpoch);
void ReadMotoEph(u1 eg_type, char *buf,SVText *eph);
void main()
{
int Type;
char *ObsInFile,*EphInFile, str[200];
char *RinexObsFile,*RinexNavFile;
FILE *f1,*f2;
clrscr();
printf(" ****** Transfer to Rinex File ******\n");
printf(" Source File Type: 1-Ashtech 2-Trimble 4000 3-Navsymm \n");
printf(" 4-Motorola 5-Novatel \n");
do
{
printf("Please Input GPS Type:");
scanf("%i",&Type);
if((Type>5)||(Type<1))
{ printf(" Invalid Type Input,Input Again!\n");return;}
else break;
}while(1);
InitRinexObsHeader();
RinexObsFile="rinex.98o";
RinexNavFile="rinex.98n";
WeekNo=0;
/*
Type=MOTOROLA;
ObsInFile="c:\\data\\moto.dif";
*/
switch(Type)
{
case ASHTECH:
case NAVSYMM:
printf("Please Input Soure Obs File Name:");
scanf("%20s",ObsInFile);
printf("Please Input Rinex Nav File Name:");
scanf("%20s",EphInFile);
if(Type==ASHTECH) {
Ashtech21Rinex(EphInFile,RinexNavFile);
Ashtech2Rinex(ObsInFile,"temp.obs"); }
else if(Type==NAVSYMM) {
Navsymm21Rinex(EphInFile,RinexNavFile);
Navsymm2Rinex(ObsInFile,"temp.obs");}
break;
case TRIMBLE:
case MOTOROLA:
case NOVATEL:
printf("Please Input Soure Data File Name:");
scanf("%20s",ObsInFile);
if(Type==TRIMBLE) {
Trimble2Rinex(ObsInFile,"temp.obs",RinexNavFile);}
else if(Type==NOVATEL) {
Novatel2Rinex(ObsInFile,"temp.obs",RinexNavFile);}
else if(Type==MOTOROLA) {
Motorola2Rinex(ObsInFile,"temp.obs",RinexNavFile);}
break;
default:break;
}
/* format obs file */
if ((f1 = fopen(RinexObsFile, "wt+"))== NULL) {
fprintf(stderr, "Cannot open output file.\n"); return;}
if ((f2 = fopen("temp.obs", "rt"))== NULL) {
fprintf(stderr, "Cannot open temp file.\n"); return ;}
MeasType[0]=1;MeasType[1]=2;
WriteRinexObsHeaderFile(f1, &rinex_obs_header);
while(!feof(f2)) { fgets(str,2000,f2);fputs(str,f1); }
fclose(f1); fclose(f2);
}
int Ashtech2Rinex(char *SourceFile,char *ObsFile)
{
FILE *f1,*f2;
int i,flag,FirstEpoch,numobs;
RinexEpochType RinexEpoch;
struct FileHeaderStruct
{
char version[10];
unsigned char iver;
char rtype[10];
char chnver[10];
char navver[10];
int cap; /* 1=L1 only 2=L1 L2 4=range only */
long reserved;
unsigned char numobs;
char spare[42];
} Header;
if ((f1=fopen(ObsFile,"wa+"))==NULL)
{ sprintf(err_str,"can not open file \n");return 1; }
if((f2=fopen(SourceFile,"rb"))==NULL)
{ sprintf(err_str,"cannot open file \n"); return 1; }
if(fread(&Header,sizeof(struct FileHeaderStruct),1,f2)!=1)
{ sprintf(err_str,"file read error"); return 1; }
numobs=Header.numobs;
flag=0; FirstEpoch=1;
do
{
ResetRinexEpoch(&RinexEpoch);
flag = ReadAshEpoch(f2,numobs,&RinexEpoch);
if(FirstEpoch==1)
{
rinex_obs_header.StartYear=rinex_obs_header.EndYear;
rinex_obs_header.StartMonth=rinex_obs_header.EndMonth;
rinex_obs_header.StartDay=rinex_obs_header.EndDay;
rinex_obs_header.StartHour=rinex_obs_header.EndHour;
rinex_obs_header.StartMinute=rinex_obs_header.EndMinute;
rinex_obs_header.StartSecond=rinex_obs_header.EndSecond;
FirstEpoch=0;
}
WriteRinexObsEpochFile(f1,&RinexEpoch);
}while(flag==0);
fclose(f1); fclose(f2); // free(RinexEpoch);
return 0;
}
int Ashtech21Rinex(char *SourceFile,char *NavFile)
{
FILE *f, *f2;
SVText *RinexEpp;
int First;
if((f=fopen(SourceFile,"rb"))==NULL)
{ sprintf(err_str,"cannot open file \n"); return 1; }
if ((f2=fopen(NavFile,"wa"))==NULL)
{ sprintf(err_str,"can not open file \n"); return 1; }
if((RinexEpp=(SVText *)malloc(sizeof(SVText)))==NULL)
{ sprintf(err_str,"not enough mem \n"); return 1; }
WriteRinexNavHeaderFile(f2);
while(!feof(f)) {
ResetRinexEpp(RinexEpp);
ReadAshEph(f,RinexEpp);
WriteRinexNavFile( f2, RinexEpp);}
fclose(f); fclose(f2); free(RinexEpp);
return 0;
}
int ReadAshEpoch(FILE *f,int numobs,RinexEpochType *RinexEpoch)
{
long t;
int i,j;
double C = 299792458.0;
double GPStime;
struct PosStruct
{
char sitename[4];
double rcv_time,navx,navy,navz,navt,navdot;
float navxdot,navydot,navzdot;
int PDOP;
unsigned char num_sats;
}PosData;
struct SatStruct /* measurement (data) record */
{
unsigned char svprn;
unsigned char elev; /* unit degree */
unsigned char azim; /* unit 10 degree */
unsigned char chnind;
}SatData;
struct RawDataStruct
{
double raw_range;
float smth_r;
int smth_n;
unsigned char polarity;
unsigned char warning;
unsigned char goodbad;
unsigned char ireg;
unsigned char qa_phase;
long doppler;
double carphase;
}RawData[3] ;
if(fread(&PosData,sizeof(struct PosStruct),1,f)!=1){
sprintf(err_str,"file read error"); return 1; }
GPStime=PosData.rcv_time-PosData.navt/C;
rinex_obs_header.AppX=PosData.navx;
rinex_obs_header.AppY=PosData.navy;
rinex_obs_header.AppZ=PosData.navz;
for(i=0;i<PosData.num_sats;i++) {
if(fread(&SatData,sizeof(struct SatStruct),1,f)!=1) {
sprintf(err_str,"file read error"); return 1; }
for(j=0;j<numobs;j++) {
if(fread(&RawData[j],sizeof(struct RawDataStruct),1,f)!=1) {
sprintf(err_str,"file read error"); return 1; } }
RinexEpoch->satnum++;
RinexEpoch->PRN[i]=SatData.svprn;
RinexEpoch->ca_range[i]=RawData[0].raw_range *C - PosData.navt;
RinexEpoch->phase_l1[i]=RawData[0].carphase ;
RinexEpoch->P1_range[i]=RawData[1].raw_range *C - PosData.navt;
RinexEpoch->phase_l2[i]=RawData[1].carphase ;
RinexEpoch->doppler_l1[i]=RawData[0].doppler/1000