Index: trunk/rtcm3torinex/lib/rtcm3torinex.c
===================================================================
--- trunk/rtcm3torinex/lib/rtcm3torinex.c	(revision 5674)
+++ trunk/rtcm3torinex/lib/rtcm3torinex.c	(revision 6329)
@@ -446,4 +446,56 @@
         ge->TOW = 0.9999E9;
         ret = 1019;
+      }
+      break;
+    case 1043:
+      if(handle->GPSWeek)
+      {
+        struct sbasephemeris *gs;
+        int sv, i, time, tod, day;
+        gs = &handle->ephemerisSBAS;
+        memset(gs, 0, sizeof(*gs));
+
+        GETBITS(sv, 6)
+        gs->satellite = PRN_SBAS_START+sv;
+        GETBITS(gs->IODN, 8)
+        GETBITS(time, 13)
+        time <<= 4;
+        gs->GPSweek_TOE = handle->GPSWeek;
+        GETBITS(gs->URA, 4)
+        GETFLOATSIGN(gs->x_pos, 30, 0.08)
+        GETFLOATSIGN(gs->y_pos, 30, 0.08)
+        GETFLOATSIGN(gs->z_pos, 25, 0.4)
+        GETFLOATSIGN(gs->x_velocity, 17, 0.000625)
+        GETFLOATSIGN(gs->y_velocity, 17, 0.000625)
+        GETFLOATSIGN(gs->z_velocity, 18, 0.004)
+        GETFLOATSIGN(gs->x_acceleration, 10, 0.0000125)
+        GETFLOATSIGN(gs->y_acceleration, 10, 0.0000125)
+        GETFLOATSIGN(gs->z_acceleration, 10, 0.0000625)
+        GETFLOATSIGN(gs->agf0, 12, 1.0/(1<<30)/(1<<1))
+        GETFLOATSIGN(gs->agf1, 8, 1.0/(1<<30)/(1<<10))
+
+        /* calculate time */
+        tod = handle->GPSTOW%(24*60*60);
+        day = handle->GPSTOW/(24*60*60);
+        if(time > 19*60*60 && tod < 5*60*60)
+          --day;
+        else if(time < 5*60*60 && tod > 19*60*60)
+          ++day;
+        time += day*24*60*60;
+        if(time > 7*24*60*60)
+          ++gs->GPSweek_TOE;
+        else if(time < 0)
+          --gs->GPSweek_TOE;
+        gs->TOE = time;
+
+        i = (gs->GPSweek_TOE - handle->GPSWeek)*7*24*60*60
+        + (gs->TOE - handle->GPSTOW) - 2*60*60;
+        if(i > 5*60*60 && i < 8*60*60)
+        {
+          handle->GPSTOW = gs->TOE;
+          handle->GPSWeek = gs->GPSweek_TOE;
+        }
+        gs->TOW = 0.9999E9;
+        ret = 1043;
       }
       break;
@@ -2489,5 +2541,5 @@
     while((r = RTCM3Parser(Parser)))
     {
-      if(r == 1020 || r == 1019 || r == 1044)
+      if(r == 1020 || r == 1019 || r == 1044 || r == 1043)
       {
         FILE *file = 0;
@@ -2514,4 +2566,5 @@
             Parser->gpsephemeris = 0;
             Parser->glonassephemeris = 0;
+            Parser->sbasephemeris = 0;
             file = Parser->gpsfile;
           }
@@ -2559,4 +2612,24 @@
             file = Parser->gpsfile;
           }
+          else if(r == 1043)
+          {
+            if(Parser->sbasephemeris)
+            {
+              if(!(Parser->sbasfile = fopen(Parser->sbasephemeris, "w")))
+              {
+                RTCM3Error("Could not open SBAS ephemeris output file.\n");
+              }
+              else
+              {
+                char buffer[100];
+                fprintf(Parser->sbasfile,
+                "%9.2f%11sN: SBAS NAV DATA%24sRINEX VERSION / TYPE\n", 2.1, "", "");
+                HandleRunBy(buffer, sizeof(buffer), 0, Parser->rinex3);
+                fprintf(Parser->sbasfile, "%s\n%60sEND OF HEADER\n", buffer, "");
+              }
+              Parser->sbasephemeris = 0;
+            }
+            file = Parser->sbasfile;
+          }
           else if(r == 1044)
           {
@@ -2582,4 +2655,5 @@
         if(file)
         {
+          const char *sep = "   ";
           if(r == 1020)
           {
@@ -2594,17 +2668,50 @@
 
             if(Parser->rinex3)
+            {
               ConvLine(file, "R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
               e->almanac_number, cti.year, cti.month, cti.day, cti.hour, cti.minute,
               cti.second, -e->tau, e->gamma, (double) i);
+              sep = "    ";
+            }
             else
+            {
               ConvLine(file, "%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e\n",
               e->almanac_number, cti.year%100, cti.month, cti.day, cti.hour, cti.minute,
               (double) cti.second, -e->tau, e->gamma, (double) i);
-            ConvLine(file, "   %19.12e%19.12e%19.12e%19.12e\n", e->x_pos,
+            }
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->x_pos,
             e->x_velocity, e->x_acceleration, (e->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
-            ConvLine(file, "   %19.12e%19.12e%19.12e%19.12e\n", e->y_pos,
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->y_pos,
             e->y_velocity, e->y_acceleration, (double) e->frequency_number);
-            ConvLine(file, "   %19.12e%19.12e%19.12e%19.12e\n", e->z_pos,
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->z_pos,
             e->z_velocity, e->z_acceleration, (double) e->E);
+          }
+          else if(r == 1043)
+          {
+            struct sbasephemeris *e = &Parser->ephemerisSBAS;
+            struct converttimeinfo cti;
+            converttime(&cti, e->GPSweek_TOE, e->TOE);
+            if(Parser->rinex3)
+            {
+              ConvLine(file, "S%02d %04d %2d %2d %2d %2d %2d%19.12e%19.12e%19.12e\n",
+              e->satellite-100, cti.year, cti.month, cti.day, cti.hour, cti.minute,
+              cti.second, e->agf0, e->agf1, (double)e->TOW);
+              sep = "    ";
+            }
+            else
+            {
+              ConvLine(file, "%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e\n",
+              e->satellite-100, cti.year%100, cti.month, cti.day, cti.hour, cti.minute,
+              (double)cti.second, e->agf0, e->agf1, (double)e->TOW);
+            }
+            /* X, health */
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->x_pos,
+            e->x_velocity, e->x_acceleration, e->URA == 15 ? 1.0 : 0.0);
+            /* Y, accuracy */
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->y_pos,
+            e->y_velocity, e->y_acceleration, (double)e->URA);
+            /* Z */
+            ConvLine(file, "%s%19.12e%19.12e%19.12e%19.12e\n", sep, e->z_pos,
+            e->z_velocity, e->z_acceleration, (double)e->IODN);
           }
           else /* if(r == 1019 || r == 1044) */
@@ -2615,5 +2722,4 @@
             struct converttimeinfo cti;
             converttime(&cti, e->GPSweek, e->TOC);
-            const char *sep = "   ";
             int qzss = 0;
             int num = e->satellite;
@@ -3259,4 +3365,5 @@
   const char *data;
   const char *headerfile;
+  const char *sbasephemeris;
   const char *gpsephemeris;
   const char *qzssephemeris;
@@ -3280,4 +3387,5 @@
 { "qzssephemeris",    required_argument, 0, 'Q'},
 { "glonassephemeris", required_argument, 0, 'G'},
+{ "sbasephemeris",    required_argument, 0, 'B'},
 { "rinex3",           no_argument,       0, '3'},
 { "changeobs",        no_argument,       0, 'O'},
@@ -3289,5 +3397,5 @@
 {0,0,0,0}};
 #endif
-#define ARGOPT "-d:s:p:r:t:f:u:E:G:Q:M:S:R:n:h3O"
+#define ARGOPT "-d:s:p:r:t:f:u:E:G:B:Q:M:S:R:n:h3O"
 
 enum MODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, AUTO = 4, END };
@@ -3420,4 +3528,5 @@
   args->gpsephemeris = 0;
   args->qzssephemeris = 0;
+  args->sbasephemeris = 0;
   args->glonassephemeris = 0;
   args->rinex3 = 0;
@@ -3444,4 +3553,5 @@
     case 'f': args->headerfile = optarg; break;
     case 'E': args->gpsephemeris = optarg; break;
+    case 'B': args->sbasephemeris = optarg; break;
     case 'G': args->glonassephemeris = optarg; break;
     case 'Q': args->qzssephemeris = optarg; break;
@@ -3521,4 +3631,5 @@
     " -G " LONG_OPT("--glonassephemeris ") "output file for GLONASS ephemeris data\n"
     " -Q " LONG_OPT("--qzssephemeris    ") "output file for QZSS ephemeris data\n"
+    " -B " LONG_OPT("--sbasephemeris    ") "output file for SBAS ephemeris data\n"
     " -3 " LONG_OPT("--rinex3           ") "output RINEX type 3 data\n"
     " -S " LONG_OPT("--proxyhost        ") "proxy name or address\n"
@@ -3626,4 +3737,5 @@
     Parser.gpsephemeris = args.gpsephemeris;
     Parser.qzssephemeris = args.qzssephemeris;
+    Parser.sbasephemeris = args.sbasephemeris;
     Parser.rinex3 = args.rinex3;
     Parser.changeobs = args.changeobs;
Index: trunk/rtcm3torinex/lib/rtcm3torinex.h
===================================================================
--- trunk/rtcm3torinex/lib/rtcm3torinex.h	(revision 5674)
+++ trunk/rtcm3torinex/lib/rtcm3torinex.h	(revision 6329)
@@ -401,4 +401,24 @@
 };
 
+struct sbasephemeris {
+  int    satellite;
+  int    IODN;             /*          [bits  14- 21] */
+  int    GPSweek_TOE;
+  int    TOW;
+  int    TOE;              /*  [s]     [bits  22- 34] */
+  double agf0;             /*  [s]     [bits 206-217] */
+  double agf1;             /*  [s/s]   [bits 218-225] */
+  double x_pos;            /*  [km]    [bits  39- 68] */
+  double x_velocity;       /*  [km/s]  [bits 124-140] */
+  double x_acceleration;   /*  [km/s^2][bits 176-185] */
+  double y_pos;            /*  [km]    [bits  69- 98] */
+  double y_velocity;       /*  [km/s]  [bits 141-157] */
+  double y_acceleration;   /*  [km/s^2][bits 186-195] */
+  double z_pos;            /*  [km]    [bits  99-123] */
+  double z_velocity;       /*  [km/s]  [bits 158-175] */
+  double z_acceleration;   /*  [km/s^2][bits 196-205] */
+  int    URA;              /*          [bits  35- 38] */
+};
+
 struct galileoephemeris {
   int    flags;            /* GALEPHF_xxx */
@@ -451,4 +471,5 @@
   struct galileoephemeris ephemerisGALILEO;
   struct glonassephemeris ephemerisGLONASS;
+  struct sbasephemeris ephemerisSBAS;
   struct gnssdata DataNew;
   int    GLOFreq[PRN_GLONASS_NUM]; /* frequency numbers of GLONASS + 100 */
@@ -493,7 +514,9 @@
   const char * gpsephemeris;
   const char * qzssephemeris;
+  const char * sbasephemeris;
   FILE *       glonassfile;
   FILE *       gpsfile;
   FILE *       qzssfile;
+  FILE *       sbasfile;
 };
 
