#include #include #include #include "mex.h" /* readrinex Reads rinex file and outputs: - observables (as a structure) - date and time of obs (as a matrix) - PRN numbers (as a vector) - a priori coordinates (from rinex header) This version reads GPS and GLONASS. WARNING: GPS PRNs are assumed to be < 50 GLONASS PRNs are PRN+50 (e.g. R3 is named 53) If Matlab crashes with segmentation violation when reading rinex file try running the rinex file through teqc first. Usage: [Observables,epochs,sv,apcoords]=readrinex(filename) Sun Nov 1 19:08:07 EST 2009 ecalais@purdue.edu */ FILE *fIn; void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *FileName; const char **Observables; char GetString[82], SatType[99]; int i,j,k,ns, LineFlag,FileNameLength,NumObs,NumSats,NumG,NumR,NumEpochs; int SatColumn[99]={0}, ColumnIndex[99]={0}, CurrentSats[99]={0}, SatCount[99]={0}; double XYZ[3], O, *pSatellites; double **pObs, *pEpoch; mxArray **mxObs; if (nrhs!=1) mexErrMsgTxt("Usage: [Observables,epochs,sv,apcoords]=readrinex(filename)."); if (mxIsChar(prhs[0])==0) mexErrMsgTxt("First argument must be a rinex file name."); FileNameLength = (mxGetM(prhs[0])*mxGetN(prhs[0]))+1; FileName =(char *)mxCalloc(FileNameLength,sizeof(char)); mxGetString(prhs[0],FileName,FileNameLength); /* Try to open rinex file. */ fIn = fopen(FileName, "r"); if (fIn==NULL) mexErrMsgTxt("Could not open file."); /* Check for valid RINEX file.*/ fgets(GetString, 82, fIn); printf("Reading %s, RINEX version %c\n",FileName,GetString[5]); if(strstr(GetString,"RINEX VERSION / TYPE") == 0) { printf("\n%s",GetString); mexErrMsgTxt("Invalid RINEX header."); } /* Determine number/type of observables. */ while (fgets(GetString, 82, fIn)) { if(GetString[62]==47) /* Look for the '/' in '# / TYPES OF OBSERV' */ { sscanf(GetString,"%d",&NumObs); /*Observables = (char **)mxCalloc(NumObs,sizeof(char *));*/ Observables = mxCalloc(NumObs,sizeof(char *)); if(NumObs>5) LineFlag=2; else LineFlag=1; for(i=0;i12) { fgets(GetString, 82, fIn); for(j=0;j0) { SatColumn[i]=NumSats; SatCount[NumSats]=i; NumSats++; if (i<50) NumG++; if (i>=50) NumR++; } printf("Found %d satellites: %d GPS, %d GLONASS\n",NumSats, NumG, NumR); /* Allocate memory to hold outputs */ mxObs = (mxArray **)mxCalloc(NumObs,sizeof(mxArray *)); pObs = (double **)mxCalloc(NumObs,sizeof(double *)); plhs[0]=mxCreateStructMatrix(1,1,NumObs,Observables); plhs[1]=mxCreateDoubleMatrix(NumEpochs,6,mxREAL); plhs[2]=mxCreateDoubleMatrix(NumSats,1,mxREAL); plhs[3]=mxCreateDoubleMatrix(3,1,mxREAL); pSatellites=mxGetPr(plhs[2]); for (i=0;i12) { fgets(GetString, 82, fIn); for(j=0;j