📄 to-rinex.c
字号:
FILE *f1;
FILE *f2;
int i ;
if( (RinexEpoch=(RinexEpochType *)malloc( sizeof(RinexEpochType) )) == NULL )
{ printf("Error: Not enough memory allocatein b_mben\n"); return 1; }
if( (f1=fopen( SourceFile, "rb" )) == NULL )
{ sprintf( "Error: File %s not found",SourceFile ); return 1; }
if( fread(&Header, sizeof(struct navstar_bin_file_type ), 1, f1 ) != 1 )
{ printf("Error: Unable to read header from %s",SourceFile ); return 1; }
rinex_obs_header.MeasureInterval = Header.interval;
rinex_obs_header.NumFileSV=Header.max_channels;
sprintf(rinex_obs_header.SoftVersion,"%f",Header.version);
/* Read first good epoch ,write rinexe header file */
EpochNum=0;
BadEpochNum=0;
if ((f2 = fopen(RinexFile, "wt"))== NULL)
{ fprintf(stderr, "Cannot open output file.\n"); return 1; }
IsFirstEpoch=1;
do
{
flag=ReadNavstarEpoch( f1,RinexEpoch ); /* -1 - end of file */
/* 0 - success */
/* 1 - failure */
if(flag==0)
{
EpochNum++;
if(IsFirstEpoch)
{
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;
IsFirstEpoch=0;
}
WriteRinexObsEpochFile(f2,RinexEpoch);
}
else if(flag==1) BadEpochNum++;
}while(flag!= -1);
fclose(f2); fclose( f1 );
return 0;
}
int ReadNavstarEpoch(FILE *fname, RinexEpochType *RinexEpoch)
{
double P0=3.1415926535898/180.0;
int size,i;
double x,y,z;
/* structures */
struct pben_navstar_type /* position record (68 bytes) */
{
short size; /* size of structure */
char num_sats; /* number of satellites to follow */
char status; /* bit 0: position valid (1=valid)
bit 1: 0 - static mode 1 - kinematic */
float ant_ht; /* antenna height (HT) in metres */
double phi,lamda; /* position dec deg */
float ht;
double rcv_time; /* receive time in seconds since sunday */
double corr_time; /* corrected receive time */
double clock_shift; /* clock_shift in metres */
double clock_drift;
char extra[8];
}b_pben ; /* binary position record */
struct mben_navstar_type /* measurement record (44 bytes) */
{
short size; /* size in bytes */
char prn; /* prn number */
unsigned char locktime; /* seconds since last loss of lock
(255 max) 0=no loss of lock */
double ca_range; /* ca pseudo range in metres */
double phase_l1; /* phase in cycles (on ca channel) */
double phase_rate_l1; /* phase_rate in cycles */
double p1_range; /* pcode on l1 (m) */
double p2_range; /* pcode on l2 (m) */
}b_mben ;
/* Read epoch */
if( fread( &b_pben, sizeof(struct pben_navstar_type), 1, fname ) != 1 )
{ sprintf(err_str,"End of file" ); return (-1); }
/* check pben size */
if( b_pben.size != sizeof(struct pben_navstar_type ) )
{ printf("Error: Bad position record " ); return 1; }
/* read mben records */
size = b_pben.num_sats * sizeof( struct mben_navstar_type );
if( !size ) return 1;
for( i=0; i<b_pben.num_sats; ++i )
{
/* check measurement record sizes */
if( fread( &b_mben, sizeof( struct mben_navstar_type), 1, fname ) != 1 )
{ printf("Error: Bad Pre-mature \n"); return 1; }
if( b_mben.size != sizeof( struct mben_navstar_type ) )
{ printf("Error: Bad measurement record \n" ); return 1;}
RinexEpoch->PRN[i]=b_mben.prn;
RinexEpoch->ca_range[i]=b_mben.ca_range;
RinexEpoch->phase_l1[i]=b_mben.phase_l1;
}
/* setup rinex epoch data */
GetYMDHMS(WeekNo,b_pben.rcv_time,&RinexEpoch->year,&RinexEpoch->month,
&RinexEpoch->day,&RinexEpoch->hour,&RinexEpoch->minute,&RinexEpoch->second);
rinex_obs_header.EndYear=RinexEpoch->year;
rinex_obs_header.EndMonth=RinexEpoch->month;
rinex_obs_header.EndDay=RinexEpoch->day;
rinex_obs_header.EndHour=RinexEpoch->hour;
rinex_obs_header.EndMinute=RinexEpoch->minute;
rinex_obs_header.EndSecond=RinexEpoch->second;
RinexEpoch->year-=1900;
RinexEpoch->flag=!(b_pben.status&0x01);
RinexEpoch->satnum=b_pben.num_sats;
BLHtoXYZ((P0*b_pben.phi), (P0*b_pben.lamda),b_pben.ant_ht,
&x,&y,&z);
rinex_obs_header.AppX=x;
rinex_obs_header.AppY=y;
rinex_obs_header.AppZ=z;
return 0;
}
void Navsymm21Rinex(char *EPPFile,char *RinexNavFile)
{
FILE *f1, *f2;
SVText *snv;
if ((f1 = fopen(EPPFile, "rt")) == NULL)
{ fprintf(stderr, "Cannot open input file.\n"); return ; }
if ((f2 = fopen(RinexNavFile, "wt")) == NULL)
{ fprintf(stderr, "Cannot open outut file.\n"); return ; }
if( (snv=(SVText *)malloc( sizeof(SVText) )) == NULL )
{ printf("Not enough memory to allocate buffer\n"); return ; }
WriteRinexNavHeaderFile(f2);
do { if(ReadNavsymmEph(f1,snv)) break; WriteRinexNavFile(f2,snv);
}while(1);
fclose(f1); fclose(f2); free(snv);
}
int ReadNavsymmEph( FILE *f1,SVText *snv )
{
int j;
char t1[30],t2[30],t3[30],t4[30],t5[30];
if(fscanf( f1,"%d %d %ld %s %ld %ld\r\n",
&snv->prn,&snv->wn,&snv->tow,t1,&snv->aodc,&snv->toc)
==EOF) return 1;
snv->tgd=atof(t1);
WeekNo=snv->wn;
if(fscanf( f1,"%s %s %s\r\n",t1,t2,t3)
==EOF) return 1;
snv->af2=atof(t1); snv->af1=atof(t2); snv->af0=atof(t3);
if(fscanf( f1,"%ld %s %s %s\r\n",&snv->aode, t1,t2,t3)
==EOF) return 1;
snv->crs=atof(t1); snv->deltan=atof(t2); snv->m0=atof(t3);
if(fscanf( f1,"%s %s %s %s\r\n",t1,t2,t3,t4)
==EOF) return 1;
snv->cuc=atof(t1); snv->e=atof(t2); snv->cus=atof(t3); snv->roota=atof(t4);
if(fscanf( f1,"%ld %s %s %s\r\n", &snv->toe,t1,t2,t3)
==EOF) return 1;
snv->cic=atof(t1); snv->omega0=atof(t2); snv->cis=atof(t3);
if(fscanf( f1,"%s %s %s %s %s\r\n",t1,t2,t3,t4,t5)
==EOF) return 1 ;
snv->i0=atof(t1); snv->crc=atof(t2); snv->omega=atof(t3);
snv->omegadot=atof(t4); snv->idot=atof(t5);
return(0);
}
int Trimble2Rinex(char *SourceFile,char *RinexObsFile,char *RinexNavFile)
{
FILE *fs,*fo,*fn,*tfo;
unsigned int sync,ch[4], len,len1,num, type;
RinexEpochType *RinexEpoch;
SVText far *eph;
int First;
int i;
if((fs=fopen(SourceFile,"rb"))==NULL)
{
printf("can not open %s file",SourceFile);
return -1;
}
if((fn=fopen(RinexNavFile,"wa"))==NULL)
{
printf("can not open %s file",RinexNavFile);
return -1;
}
if((fo=fopen(RinexObsFile,"wa"))==NULL)
{
printf("can not open %s file",RinexObsFile);
return -1;
}
if( (eph=( SVText far *)malloc( sizeof(SVText) )) == NULL )
{
printf("Not enough memory to allocate buffer\n");
return -1; /* terminate program if out of memory */
}
if( (RinexEpoch=(RinexEpochType *)malloc( sizeof(RinexEpochType)*MAXCHAN )) == NULL )
{
printf("Error: Not enough memory allocatein RinexEpoch\n");
return -1;
}
InitRinexObsHeader();
WriteRinexNavHeaderFile(fn);
First=1; sync=0;
while(!feof(fs))
{
for(i=0;i<4;i++) ch[i]=fgetc(fs);
if(ch[0]=='t') /* 0x74 */
{
len=ch[1]-4; num=ch[2]; type=ch[3];
len1 =ch[1]*256+ch[2]-4;
sync =1;
}
if(sync)
{
if((type==0)||(type==7)||(type==17)) /* observation data record */
{
if((type==0)) for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
else if((type==7)) for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
else if((type==17))
{
ReadType17(fs, RinexEpoch);
if(First==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;
First=0;
}
if(WeekNo!=0) WriteRinexObsEpochFile(fo,RinexEpoch);
}
}
if((type==2)||(type==6)||(type==12)) /* antenna height and channnel
calibration data */
{
if((type==2)) for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
else if((type==6)) for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
else if((type==12)) for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
}
if((type==1)||(type==10)||(type==11)) /* postion and velocity */
{
if((type==1)) for(i=0;i<len;i++){ fgetc(fs); if(feof(fs)) break;}
else if((type==10)) for(i=0;i<len;i++){ fgetc(fs); if(feof(fs)) break;}
else if((type==11)) if(ReadType11(fs)!=0) break;
}
if((type==9)||(type==20)) /* meteological data */
{
if((type==9)) for(i=0;i<len;i++){ fgetc(fs); if(feof(fs)) break;}
else if((type==20)) for(i=0;i<len;i++){ fgetc(fs); if(feof(fs)) break;}
}
if((type==3)||(type==21)) /* ephmeris data */
{
if((type==3))
{
if(ReadType3(len,fs,eph,fn)!=0) break;
WriteRinexNavFile( fn ,eph);
}
else if((type==21))
{
if(ReadType21(len,fs,eph,fn)!=0) break;
WriteRinexNavFile( fn ,eph);
}
}
if(type==15) /* ionospheric /UTC */
for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
if(type==5) /* header */
for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
if(type==16) /* data collector information */
for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
if(type==19) /* event marker */
for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
if(type==8) /* kinematic mark */
for(i=0;i<len;i++) { fgetc(fs); if(feof(fs)) break;}
}
sync=0;
}
free(eph); free(RinexEpoch);
fclose(fs); fclose(fo); fclose(fn); fclose(tfo);
}
int ReadType11(FILE *fs)
{
/*length of record including preamble = 82 + 2 * SVs */
int i;
char chan[MAXCHAN];
char prn[MAXCHAN];
double a=6378245.0;
double e2=0.00669342162297;
double PAI=3.1415926535898;
double N;
struct Type11_Pos_Struct
{
double lat ; /* semi-circles */
double lon ; /* semi-circles */
double alt; /* meters */
double clock_offset; /* meters */
double freq_offset; /* Hz * -1 (at 1536 * 1.023 MHz) */
double PDOP;
double latdot; /* radians/sec */
double londot; /* radians/sec */
double altdot; /* meters/sec */
long GPStime; /* ms */
char flag; /* 0 : 0D "clock only" solution
1 : 1D "height only" solution
2 : 2D solution with fixed height and time
3 : 2D solution with fixed height
4 : 3D solution */
char prnnum;
}PosData;
if((fread(&PosData,sizeof(struct Type11_Pos_Struct),1,fs))!=1) return 1;
for(i=0; i<PosData.prnnum;i++)
{
if(feof(fs)) return 1;
chan[i]=fgetc(fs);
}
for(i=0; i<PosData.prnnum;i++)
{
if(feof(fs)) return 1;
prn[i]=fgetc(fs);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -