TestGPStoUTC.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 David Chin
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <stdlib.h>
00023 #include <time.h>
00024 
00025 #include <lal/LALStdlib.h>
00026 #include <lal/Date.h>
00027 #include <lal/AVFactories.h>
00028 
00029 INT4 lalDebugLevel = 0;
00030 
00031 NRCSID (LALTESTGPSTOUTCC, "$Id: TestGPStoUTC.c,v 1.15 2007/06/08 14:41:43 bema Exp $");
00032 
00033 int main(int argc, char *argv[])
00034 {
00035   static LALStatus    status;
00036   LIGOTimeGPS         gpsTime = {0, 0};
00037   LIGOTimeGPS         tmpGps  = {0, 0};
00038   LALDate             utcDate;
00039   LALLeapSecAccuracy  accuracy = LALLEAPSEC_LOOSE; /* prevent ABORT */
00040   CHARVector         *timestamp = NULL;
00041   char                refstamp[128];
00042   /* char                infostr[256]; */
00043 
00044   if (argc > 1)
00045       lalDebugLevel = atoi(argv[1]);
00046 
00047   LALCHARCreateVector(&status, &timestamp, (UINT4)128);
00048   if (status.statusCode && lalDebugLevel > 0)
00049     REPORTSTATUS(&status);
00050 
00051   /*
00052    * GPS 0 == 1980-01-06 00:00:00 UTC Sun
00053    */
00054   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00055   if (status.statusCode && lalDebugLevel > 0)
00056     {
00057       fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00058               __LINE__, LALTESTGPSTOUTCC);
00059       REPORTSTATUS(&status);
00060       return status.statusCode;
00061     }
00062 
00063   LALDateString(&status, timestamp, &utcDate);
00064   if (status.statusCode && lalDebugLevel > 0)
00065     {
00066       fprintf(stderr, "TestGPStoUTC: LALDateString() failed, line %i, %s\n",
00067               __LINE__, LALTESTGPSTOUTCC);
00068       REPORTSTATUS(&status);
00069       return status.statusCode;
00070     }
00071 
00072   sprintf(refstamp, "1980-01-06 00:00:00 UTC Sun");
00073   
00074   if (lalDebugLevel > 0)
00075     {
00076       fprintf(stderr, "refstamp  = %s\n", refstamp);
00077       fprintf(stderr, "timestamp = %s\n", timestamp->data);
00078     }
00079 
00080   if (strcmp(refstamp, timestamp->data) != 0)
00081     {
00082       LALInfo(&status, "GPStoUTC conversion failed: wrong UTC result");
00083       fprintf(stderr, "TestGPStoUTC: date strings do not match, line %i, %s\n",
00084               __LINE__, LALTESTGPSTOUTCC);
00085       LALCHARDestroyVector(&status, &timestamp);
00086       if (status.statusCode && lalDebugLevel > 0)
00087         {
00088           fprintf(stderr,
00089                   "TestGPStoUTC: LALCHARDestroyVector() failed, line %i, %s\n",
00090                   __LINE__, LALTESTGPSTOUTCC);
00091           REPORTSTATUS(&status);
00092           return status.statusCode;
00093         }
00094       REPORTSTATUS(&status);
00095       LALCheckMemoryLeaks();
00096       return 1;
00097     }
00098 
00099   /*
00100    * GPS 457574400 == 1994-07-06 23:59:50 UTC Wed
00101    */
00102   gpsTime.gpsSeconds = 457574400;
00103   gpsTime.gpsNanoSeconds = 0;
00104   
00105   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00106   if (status.statusCode && lalDebugLevel > 0)
00107     {
00108       fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00109               __LINE__, LALTESTGPSTOUTCC);
00110       REPORTSTATUS(&status);
00111       return status.statusCode;
00112     }
00113 
00114   LALDateString(&status, timestamp, &utcDate);
00115   if (status.statusCode && lalDebugLevel > 0)
00116     {
00117       fprintf(stderr, "TestGPStoUTC: LALDateString() failed, line %i, %s\n",
00118               __LINE__, LALTESTGPSTOUTCC);
00119       REPORTSTATUS(&status);
00120       return status.statusCode;
00121     }
00122 
00123   sprintf(refstamp, "1994-07-06 23:59:50 UTC Wed");
00124   
00125   if (lalDebugLevel > 0)
00126     {
00127       fprintf(stderr, "refstamp  = %s\n", refstamp);
00128       fprintf(stderr, "timestamp = %s\n", timestamp->data);
00129     }
00130 
00131   if (strcmp(refstamp, timestamp->data) != 0)
00132     {
00133       LALInfo(&status, "GPStoUTC conversion failed: wrong UTC result");
00134       LALCHARDestroyVector(&status, &timestamp);
00135       if (status.statusCode && lalDebugLevel > 0)
00136         {
00137           fprintf(stderr,
00138                   "TestGPStoUTC: LALCHARDestroyVector() failed, line %i, %s\n",
00139                   __LINE__, LALTESTGPSTOUTCC);
00140           REPORTSTATUS(&status);
00141           return status.statusCode;
00142         }
00143       REPORTSTATUS(&status);
00144       LALCheckMemoryLeaks();
00145       return 1;
00146     }
00147 
00148   /*
00149    * GPS 599184012 == 1998-12-31 23:59:60 UTC Thu (leap second introduced)
00150    */
00151   gpsTime.gpsSeconds = 599184012;
00152   gpsTime.gpsNanoSeconds = 0;
00153   
00154   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00155   if (status.statusCode && lalDebugLevel > 0)
00156     {
00157       fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00158               __LINE__, LALTESTGPSTOUTCC);
00159       REPORTSTATUS(&status);
00160       return status.statusCode;
00161     }
00162 
00163   LALDateString(&status, timestamp, &utcDate);
00164   if (status.statusCode && lalDebugLevel > 0)
00165     {
00166       fprintf(stderr, "TestGPStoUTC: LALDateString() failed, line %i, %s\n",
00167               __LINE__, LALTESTGPSTOUTCC);
00168       REPORTSTATUS(&status);
00169       return status.statusCode;
00170     }
00171 
00172   sprintf(refstamp, "1998-12-31 23:59:60 UTC Thu");
00173   
00174   if (lalDebugLevel > 0)
00175     {
00176       fprintf(stderr, "refstamp  = %s\n", refstamp);
00177       fprintf(stderr, "timestamp = %s\n", timestamp->data);
00178     }
00179 
00180   if (strcmp(refstamp, timestamp->data) != 0)
00181     {
00182       LALInfo(&status, "GPStoUTC conversion failed: wrong UTC result");
00183       LALCHARDestroyVector(&status, &timestamp);
00184       if (status.statusCode && lalDebugLevel > 0)
00185         {
00186           fprintf(stderr,
00187                   "TestGPStoUTC: LALCHARDestroyVector() failed, line %i, %s\n",
00188                   __LINE__, LALTESTGPSTOUTCC);
00189           REPORTSTATUS(&status);
00190           return status.statusCode;
00191         }
00192       REPORTSTATUS(&status);
00193       LALCheckMemoryLeaks();
00194       return 1;
00195     }
00196 
00197   /*
00198    * UPDATEME
00199    * GPS 835747214 == 2006-07-01 00:00:00 (one second past expiry)
00200    * Expect to fail with status code 5
00201    * (see the static constant maxtestedGPS in src/GPStoUTC.c)
00202    */
00203   gpsTime.gpsSeconds     = 835747214; /* use maxtestedGPS + 1 */
00204   gpsTime.gpsNanoSeconds = 0;
00205   
00206   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00207   if (status.statusCode && lalDebugLevel > 0)
00208     {
00209       fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00210               __LINE__, LALTESTGPSTOUTCC);
00211       REPORTSTATUS(&status);
00212       return status.statusCode;
00213     }
00214 
00215   LALDateString(&status, timestamp, &utcDate);
00216   if (status.statusCode && lalDebugLevel > 0)
00217     {
00218       fprintf(stderr, "TestGPStoUTC: LALDateString() failed, line %i, %s\n",
00219               __LINE__, LALTESTGPSTOUTCC);
00220       REPORTSTATUS(&status);
00221       LALCHARDestroyVector(&status, &timestamp);
00222       LALCheckMemoryLeaks();
00223       return status.statusCode;
00224     }
00225 
00226   /* UPDATEME */
00227   /* the date here should be one second after maxtestedGPS */
00228   sprintf(refstamp, "2006-07-01 00:00:00 UTC Sat");
00229   
00230   if (lalDebugLevel > 0)
00231     {
00232       fprintf(stderr, "refstamp  = %s\n", refstamp);
00233       fprintf(stderr, "timestamp = %s\n", timestamp->data);
00234     }
00235 
00236   if (strcmp(refstamp, timestamp->data) != 0)
00237     {
00238       LALInfo(&status, "GPStoUTC conversion failed: wrong UTC result");
00239       LALCHARDestroyVector(&status, &timestamp);
00240       if (status.statusCode && lalDebugLevel > 0)
00241         {
00242           fprintf(stderr,
00243                   "TestGPStoUTC: LALCHARDestroyVector() failed, line %i, %s\n",
00244                   __LINE__, LALTESTGPSTOUTCC);
00245           REPORTSTATUS(&status);
00246           return status.statusCode;
00247         }
00248       REPORTSTATUS(&status);
00249       LALCheckMemoryLeaks();
00250       return 1;
00251     }
00252   
00253 
00254   /*
00255    * GPS -100 : should fail
00256    */
00257   gpsTime.gpsSeconds     = -100;
00258   gpsTime.gpsNanoSeconds = 0;
00259   
00260   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00261   if (status.statusCode > 0)
00262     {
00263       if (status.statusCode == DATEH_ERANGEGPSABS) /* expected error */
00264         {
00265           if (lalDebugLevel > 0)
00266             {
00267               fprintf(stderr, "failed with status code %d as expected",
00268                       DATEH_ERANGEGPSABS);
00269               REPORTSTATUS(&status);
00270             }
00271         }
00272       else /* some other error */
00273         {
00274           fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00275                   __LINE__, LALTESTGPSTOUTCC);
00276           REPORTSTATUS(&status);
00277           return status.statusCode;
00278         }
00279     }
00280 
00281 
00282   /*
00283    * GPS 701654413 == 2002-04-01 00:00:00 UTC
00284    * should fail.  No leap seconds, yet.
00285    */
00286   gpsTime.gpsSeconds     = -100;
00287   gpsTime.gpsNanoSeconds = 0;
00288   
00289   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00290   if (status.statusCode > 0)
00291     {
00292       if (status.statusCode == DATEH_ERANGEGPSABS) /* expected error */
00293         {
00294           if (lalDebugLevel > 0)
00295             {
00296               fprintf(stderr, "failed with status code %d as expected",
00297                       DATEH_ERANGEGPSABS);
00298               REPORTSTATUS(&status);
00299             }
00300         }
00301       else /* some other error */
00302         {
00303           fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00304                   __LINE__, LALTESTGPSTOUTCC);
00305           REPORTSTATUS(&status);
00306           return status.statusCode;
00307         }
00308     }
00309 
00310   /*
00311    * Now, let's try converting GPS to UTC and back
00312    */
00313   gpsTime.gpsSeconds     = 701654354;
00314   gpsTime.gpsNanoSeconds = 0;
00315 
00316   LALGPStoUTC(&status, &utcDate, &gpsTime, &accuracy);
00317   if (status.statusCode && lalDebugLevel > 0)
00318     {
00319       fprintf(stderr, "TestGPStoUTC: LALGPStoUTC() failed, line %i, %s\n",
00320               __LINE__, LALTESTGPSTOUTCC);
00321       REPORTSTATUS(&status);
00322       return status.statusCode;
00323     }
00324 
00325   LALUTCtoGPS(&status, &tmpGps, &utcDate, &accuracy);
00326   if (status.statusCode && lalDebugLevel > 0)
00327     {
00328       fprintf(stderr,
00329               "TestUTCtoGPS: error in LALUTCtoGPS, line %i, %s\n",
00330               __LINE__, LALTESTGPSTOUTCC);
00331       REPORTSTATUS(&status);
00332       return status.statusCode;
00333     }
00334 
00335   if (lalDebugLevel > 0)
00336     {
00337       fprintf(stderr, "\tgpsTime = {%d, %d}\n", gpsTime.gpsSeconds,
00338               gpsTime.gpsNanoSeconds);
00339       fprintf(stderr, "\ttmpGps  = {%d, %d}\n", tmpGps.gpsSeconds,
00340               tmpGps.gpsNanoSeconds);
00341     }
00342 
00343 
00344   if (tmpGps.gpsSeconds     != gpsTime.gpsSeconds ||
00345       tmpGps.gpsNanoSeconds != gpsTime.gpsNanoSeconds)
00346     {
00347       fprintf(stderr,
00348               "TestGPStoUTC: conversion from GPS to UTC and back to GPS failed, line %i, %s\n", __LINE__, LALTESTGPSTOUTCC);
00349       REPORTSTATUS(&status);
00350       return 1;
00351     }
00352   
00353 
00354   
00355   /*
00356    * Cleanup and exit
00357    */
00358   LALCHARDestroyVector(&status, &timestamp);
00359   if (status.statusCode && lalDebugLevel > 0)
00360     {
00361       fprintf(stderr,
00362               "TestGPStoUTC: LALCHARDestroyVector() failed, line %i, %s\n",
00363               __LINE__, LALTESTGPSTOUTCC);
00364       REPORTSTATUS(&status);
00365       return status.statusCode;
00366     }
00367   if (lalDebugLevel > 0)
00368     REPORTSTATUS(&status);
00369   LALCheckMemoryLeaks();
00370   return 0;
00371 }

Generated on Fri Aug 29 02:49:29 2008 for LAL by  doxygen 1.5.2