⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 to-rinex.c

📁 针对GPS系统的全面的源程序 希望能够对学习GPS技术的朋友有所帮助
💻 C
📖 第 1 页 / 共 4 页
字号:
  N = a / sqrt(1.0-e2*sin(PAI*(PosData.lat)) * sin(PAI*PosData.lat));
  rinex_obs_header.AppX
     = (N + (PosData.alt)) * cos(PAI*PosData.lat) * cos(PAI*PosData.lon);
  rinex_obs_header.AppY
     = (N + PosData.alt) * cos(PAI*PosData.lat) * sin(PAI*PosData.lon);
  rinex_obs_header.AppZ
     = (N * (1.0-e2) + PosData.alt ) * sin(PAI* PosData.lat);
  return 0;
}

int ReadType17(FILE *fs, RinexEpochType *Epoch)
{
  int i,j;
  char satnum;
  double gpstime,clockoffset;
  char flag;

  struct Type17_L1_Struct
  {
    char prn;
    char flag;
    char res[2];
    int  elev;
    int  azi;
    double SNratio;
    double range;
    double phase_l1;
    double doppler;
    double Res;
  } L1_data;

  struct Type17_L2_Struct
  {
    double SNratio;
    double phase_l2;
    double Pcode;
  }L2_data;

  ResetRinexEpoch(Epoch);
  if((fread(&gpstime,sizeof(double),1,fs))!=1) return 1;;
  if((fread(&clockoffset,sizeof(double),1,fs))!=1) return 1;
//  gpstime-=clockoffset;
  gpstime/=1000.0;
  GetYMDHMS(WeekNo,gpstime,&Epoch->year,&Epoch->month, &Epoch->day,
	   &Epoch->hour,&Epoch->minute,&Epoch->second);
  rinex_obs_header.EndYear=Epoch->year;
  rinex_obs_header.EndMonth=Epoch->month;
  rinex_obs_header.EndDay=Epoch->day;
  rinex_obs_header.EndHour=Epoch->hour;
  rinex_obs_header.EndMinute=Epoch->minute;
  rinex_obs_header.EndSecond=Epoch->second;

  Epoch->year-=1900;

  if((fread(&satnum,sizeof(char),1,fs))!=1) return 1;
  Epoch->satnum=satnum;

  j=0;
  for(i=0;i<satnum;i++)
  {
    if((fread(&L1_data,sizeof(struct Type17_L1_Struct),1,fs))!=1) return 1;
    flag=L1_data.flag&0x01;
    if(flag==1)
      if((fread(&L2_data,sizeof(struct Type17_L2_Struct),1,fs))!=1) return 1;
    Epoch->PRN[j]=L1_data.prn;
    Epoch->phase_l1[j] =(-1.0)* L1_data.phase_l1;
    Epoch->ca_range[j]= L1_data.range;
    if(L1_data.prn>0) j++;
  }
  return 0;
}



int ReadType3(int len,FILE *fs,SVText far *eph,FILE *fn)
{
  char ptr[500],*p;
  int i;

  ResetRinexEpp(eph);
  for(i=0;i<len;i++)
  {
    ptr[i]=fgetc(fs);
    if(feof(fs)) return 1;
  }
  p=ptr;

   WeekNo=eph->prn=(char )(*p); p++;
   p+=1;
   eph->wn = readdouble(p);        p+=8;
   eph->tgd= readdouble(p);        p+=8;
   eph->tow= readdouble(p);        p+=8;
   eph->aodc=readdouble(p);        p+=8;
   eph->toc= readdouble(p);        p+=8;
   eph->af2= readdouble(p);        p+=8;
   eph->af1= readdouble(p);        p+=8;
   eph->af0= readdouble(p);        p+=8;
   eph->aode=readdouble(p);        p+=8;
   eph->deltan=readdouble(p) * PI; p+=8;
   eph->m0=readdouble(p)* PI;      p+=8;
   eph->e= readdouble(p);          p+=8;
   eph->roota= readdouble(p);      p+=8;
   eph->toe= readdouble(p);        p+=8;
   eph->cic= readdouble(p)*PI;     p+=8;
   eph->crc= readdouble(p);        p+=8;
   eph->cis= readdouble(p)*PI;     p+=8;
   eph->crs= readdouble(p);        p+=8;
   eph->cuc= readdouble(p)*PI;     p+=8;
   eph->cus= readdouble(p)*PI;     p+=8;
   eph->omega0 =readdouble(p)*PI;  p+=8;
   eph->omega=readdouble(p)* PI;   p+=8;
   eph->i0 =readdouble(p) * PI;    p+=8;
   eph->omegadot =readdouble(p) * PI; p+=8;
   eph->idot =readdouble(p) * PI;     p+=8;

   WriteRinexNavFile( fn ,eph);
   return 0;
}

int ReadType21(int len,FILE *fs, SVText *eph,FILE *fn)
{
  char ptr[500],*p;
  int i;

  ResetRinexEpp(eph);
  for(i=0;i<len;i++)
  {
    ptr[i]=fgetc(fs);
    if(feof(fs)) return 1;
  }
  p=ptr;

   eph->prn=readchar(p);           p+=1;
   p+=1;   /*  1 byte res */
   WeekNo= eph->wn = readint(p);       p+=2;
   eph->aodc=readint(p);           p+=2;
   p+=1;  /*  1 byte res */
   eph->aode=readchar(p);          p+=1;
   eph->tow= readlong(p);          p+=4;
   eph->toc= readlong(p);          p+=4;
   eph->toe= readlong(p);          p+=4;
   eph->tgd= readdouble(p);         p+=8;
   eph->af2= readdouble(p);         p+=8;
   eph->af1= readdouble(p);         p+=8;
   eph->af0= readdouble(p);         p+=8;
   eph->crs= readdouble(p);         p+=8;
   eph->deltan=readdouble(p) * PI;  p+=8;
   eph->m0=readdouble(p)* PI;       p+=8;
   eph->cuc= readdouble(p) * PI;    p+=8;
   eph->e= readdouble(p);           p+=8;
   eph->cus= readdouble(p) * PI;    p+=8;
   eph->roota= readdouble(p);       p+=8;
   eph->cic= readdouble(p) * PI;    p+=8;
   eph->omega0 =readdouble(p) * PI; p+=8;
   eph->cis= readdouble(p) * PI;    p+=8;
   eph->i0 =readdouble(p) * PI;     p+=8;
   eph->crc= readdouble(p);         p+=8;
   eph->omega=readdouble(p)* PI;    p+=8;
   eph->omegadot =readdouble(p) * PI; p+=8;
   eph->idot =readdouble(p) * PI;     p+=8;
   return 0;
}


int Novatel2Rinex(char *SourceFile,char *RinexFile,char *RinexNavFile)
{
  FILE *fs,*fo,*fn;
  char p[2000],ch;
  RinexEpochType  *RinexEpoch;
  SVText *snv;
  int First;

  if((fs=fopen(SourceFile,"ra"))==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(RinexFile,"wa"))==NULL)
    { printf("can not open obs file"); return -1;  }
  if( (snv=(SVText *)malloc( sizeof(SVText) )) == NULL )
    { printf("Not enough memory to allocate buffer\n"); return -1;}
  if( (RinexEpoch=(RinexEpochType *)malloc( sizeof(RinexEpochType) )) == NULL )
    { printf("Error: Not enough memory allocatein RinexEpoch\n"); return -1;}

  First=1;
  WriteRinexNavHeaderFile(fn);
  while(!feof(fs))
  {
    fgets(p,2000,fs);
    if(p[0]=='$')
    {
      if((p[1]=='P')&&(p[2]=='O')&&(p[3]=='S')&&(p[4]=='A'))
	  ReadNovatelPos(p,&rinex_obs_header.AppX,
			   &rinex_obs_header.AppY,
			   &rinex_obs_header.AppZ);
      else if((p[1]=='R')&&(p[2]=='E')&&(p[3]=='P')&&(p[4]=='A'))
      {
	 ReadNovatelEph(p,snv);
	 WriteRinexNavFile(fn,snv);
      }
      else if((p[1]=='R')&&(p[2]=='G')&&(p[3]=='E')&&(p[4]=='A'))
      {
	if(feof(fs)) break;
	ReadNovatelEpoch(p,RinexEpoch);
	if(First)
	{
	  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;
	}
	WriteRinexObsEpochFile(fo,RinexEpoch);
      }
    }
  }
  fclose(fo);
  free(snv); free(RinexEpoch);
  fclose(fs);  fclose(fo);    fclose(fn);
}

void ReadNovatelPos(char *s,  float *X,float *Y,float *Z)
{
  char *ptr;
  double sec,lat,lon,hgt,N;
  double a=6378245.0;
  double e2=0.00669342162297;
  double P0=3.1415926535898/180.0;

  ptr=strtok(s,",*");
  while(ptr!=NULL)
  {
    ptr=strtok(NULL,",*");  WeekNo=atoi(ptr);
    ptr=strtok(NULL,",*");  sec=atof(ptr);
    ptr=strtok(NULL,",*");  lat=atof(ptr);
    ptr=strtok(NULL,",*");  lon=atof(ptr);
    ptr=strtok(NULL,",*");  hgt=atof(ptr);
    break;
  }
   N = a / sqrt(1.0-e2*sin(P0*lat) * sin(P0*lat));
  *X = (N + hgt) * cos(P0*lat) * cos(P0*lon);
  *Y = (N + hgt) * cos(P0*lat) * sin(P0*lon);
  *Z = (N * (1.0-e2) + hgt) * sin(P0* lat);

}

int ReadNovatelEph(char *s,SVText *snv)
{
  char *ptr;
  u1 temp[60];
  int i,base;

  ptr=strtok(s,",*");
  while(ptr!=NULL)
  {
    ptr=strtok(NULL,",*"); snv->prn=atoi(ptr);
    ptr=strtok(NULL,",*");
    for(i=0;i<60;i++) temp[i]=*ptr++;
    for(i=0;i<10;i++)  {
       base=6*i;
       sub1[i][0]=(i1)(((Hex(temp[base])) << 4) | Hex(temp[base+1]));
       sub1[i][1]=(i1)(((Hex(temp[base+2])) << 4) | Hex(temp[base+3]));
       sub1[i][2]=(i1)(((Hex(temp[base+4])) << 4) | Hex(temp[base+5])); }
    ptr=strtok(NULL,",*");
    for(i=0;i<60;i++) temp[i]=*ptr++;
    for(i=0;i<10;i++) {
       base=6*i;
       sub2[i][0]=(i1)(((Hex(temp[base])) << 4) | Hex(temp[base+1]));
       sub2[i][1]=(i1)(((Hex(temp[base+2])) << 4) | Hex(temp[base+3]));
       sub2[i][2]=(i1)(((Hex(temp[base+4])) << 4) | Hex(temp[base+5])); }
    ptr=strtok(NULL,",*");
    for(i=0;i<60;i++) temp[i]=*ptr++;
    for(i=0;i<10;i++) {
       base=6*i;
       sub3[i][0]=(i1)(((Hex(temp[base])) << 4) | Hex(temp[base+1]));
       sub3[i][1]=(i1)(((Hex(temp[base+2])) << 4) | Hex(temp[base+3]));
       sub3[i][2]=(i1)(((Hex(temp[base+4])) << 4) | Hex(temp[base+5])); }
    break;  }
  Convert_Ephemeris(snv);
}

void Convert_Ephemeris(SVText *eph)
{
/* this MACRO sign extends a negative valued hex byte into its   i2 (integer) equivalent */
#define MACRO_SET_UPPER(word_to_set) \
   if ( word_to_set & 0x0080 )  word_to_set |= 0xFF00 ;
/* this MACRO takes two hex bytes and merges them into a single  i2 (integer) value */
#define MACRO_MAKE_EPH_WORD(e_wd,sub, w1, b1, w2, b2) \
   e_wd = ( ( ( u2 ) sub[w1][b1] << 8 ) | \
	      ( u2 ) sub[w2][b2] );
   u2 word1, word2;  /* Temporary storage for words built. */
   /*  ionospheric parameter(second) */
   word1 = ( u2 ) sub1[4][0];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->alph0 = ( i2 ) word1 * TWOM30;
   /*  ionospheric parameter(sec/semi-circles) */
   word1 = ( u2 ) sub1[4][1];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->alph1 = ( i2 ) word1 * TWOM27 ;
   /*  ionospheric parameter(s/semi-circles2)  */
   word1 = ( u2 ) sub1[4][2];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->alph2 = ( i2 ) word1 * TWOM24;
   /*  ionospheric parameter(s/semi-circles3)  */
    word1 = ( u2 ) sub1[5][0];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->alph3 = ( i2 ) word1 * TWOM24;
   /*  ionospheric parameter(s)     */
   word1 = ( u2 ) sub1[5][1];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->belta0 = ( i2 ) word1 * TWOP11;
   /*  ionospheric parameter(s/semi-circles)  */
   word1 = ( u2 ) sub1[5][2];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->belta1 = ( i2 ) word1 * TWOP14;
   /*  ionospheric parameter(s/semi-circles2) */
   word1 = ( u2 ) sub1[6][0];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->belta2 = ( i2 ) word1 * TWOP16;
   /*  ionospheric parameter(s/semi-circles3) */
   word1 = ( u2 ) sub1[6][1];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->belta3 = ( i2 ) word1 * TWOP16;
   /* Estimated group delay differential ( tgd ). */
   word1 = ( u2 ) sub1[6][2];
   MACRO_SET_UPPER(word1) ;   /* Set upper bits if negative. */
   eph->tgd = ( i2 ) word1 * TWOM31;
   /*  clock date issue */
   word1 = ( u2 ) sub1[9][0];
   eph->aodc = ( i2 ) word1;
   /* Clock data reference time ( toc ). */
   MACRO_MAKE_EPH_WORD(word1,sub1,  7, 1, 7, 2 );
   eph->toc = word1 * 16.0;
   /* Time of ephemeris */
   MACRO_MAKE_EPH_WORD(word1, sub2, 9, 0, 9, 1 );
   eph->toe = (double) ( ((u4) word1) * 16 ) ;
   /*  orbit date issue  in subframes 2 and 3 */
   word1 = ( u2 ) sub2[2][0];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -