TestLeapSecs.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 David Chin, Jolien Creighton
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 
00028 INT4 lalDebugLevel = 0;
00029 
00030 NRCSID (LALTESTLEAPSECSC, "$Id: TestLeapSecs.c,v 1.4 2007/06/08 14:41:43 bema Exp $");
00031 
00032 typedef struct gps_leap_sec {
00033   time_t gps;       /* GPS time when leap sec was introduced */
00034   INT4   tai_utc;   /* GPS-UTC at GPS time gps */
00035 } gps_leap_sec_t;
00036 
00037 int main(int argc, char *argv[])
00038 {
00039   static LALStatus    status;
00040   static const gps_leap_sec_t leapsec_data[] =
00041     {
00042       {0,         19},  /* 1980-Jan-06 */
00043       {46828801,  20},  /* 1981-Jul-01 */
00044       {78364802,  21},  /* 1982-Jul-01 */
00045       {109900803, 22},  /* 1983-Jul-01 */
00046       {173059204, 23},  /* 1985-Jul-01 */
00047       {252028805, 24},  /* 1988-Jan-01 */
00048       {315187206, 25},  /* 1990-Jan-01 */
00049       {346723207, 26},  /* 1991-Jan-01 */
00050       {393984008, 27},  /* 1992-Jul-01 */
00051       {425520009, 28},  /* 1993-Jul-01 */
00052       {457056010, 29},  /* 1994-Jul-01 */
00053       {504489611, 30},  /* 1996-Jan-01 */
00054       {551750412, 31},  /* 1997-Jul-01 */
00055       {599184013, 32},  /* 1999-Jan-01 */
00056     };
00057   static const INT4 num_data = sizeof(leapsec_data)/sizeof(gps_leap_sec_t);
00058   LIGOTimeGPS         gpsTime = {0, 0};
00059   INT4                gps_utc = 0;
00060   INT4                tai_utc = 0;
00061   INT4                i = 0;
00062   LALLeapSecFormatAndAcc formatAndAcc = {LALLEAPSEC_GPSUTC, LALLEAPSEC_STRICT};
00063 
00064 
00065   if (argc > 1)
00066     lalDebugLevel = atoi(argv[1]);
00067 
00068 
00069   /*
00070    * To run the test, compute GPS-UTC just before and just after the time
00071    * that the leap second is added
00072    */
00073 
00074   if (lalDebugLevel > 0)
00075     printf("TestLeapSecs: TESTING GPS-UTC\n");
00076 
00077   gpsTime.gpsSeconds = 0;
00078   LALLeapSecs(&status, &gps_utc, &gpsTime, &formatAndAcc);
00079 
00080   if (lalDebugLevel > 0)
00081     printf("\tGPS = %9d;    GPS-UTC = %d\n", gpsTime.gpsSeconds,
00082            gps_utc);
00083 
00084   if (status.statusCode && lalDebugLevel > 0)
00085     {
00086       fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00087               __LINE__, LALTESTLEAPSECSC);
00088       REPORTSTATUS(&status);
00089       return status.statusCode;
00090     }
00091 
00092   if (gps_utc != 0)
00093     {
00094       if (lalDebugLevel > 0)
00095         {
00096           fprintf(stderr,
00097                   "TestLeapSecs: LALLeapSecs returned wrong value: expected 0, got %d; i = %d\n",
00098                   gps_utc, i);
00099           REPORTSTATUS(&status);
00100         }
00101       return 1;
00102     }
00103 
00104   gpsTime.gpsSeconds = 1;
00105   LALLeapSecs(&status, &gps_utc, &gpsTime, &formatAndAcc);
00106   if (lalDebugLevel > 0)
00107     printf("\tGPS = %9d;    GPS-UTC = %d\n", gpsTime.gpsSeconds,
00108            gps_utc);
00109 
00110   if (status.statusCode && lalDebugLevel > 0)
00111     {
00112       fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00113               __LINE__, LALTESTLEAPSECSC);
00114       REPORTSTATUS(&status);
00115       return status.statusCode;
00116     }
00117 
00118   if (gps_utc != 0)
00119     {
00120       if (lalDebugLevel > 0)
00121         {
00122           fprintf(stderr,
00123                   "TestLeapSecs: LALLeapSecs returned wrong value: expected 0, got %d; i = %d\n",
00124                   gps_utc, i);
00125           REPORTSTATUS(&status);
00126         }
00127       return 1;
00128     }
00129 
00130 
00131   
00132   for (i = 1; i < num_data; ++i)
00133     {
00134       if (lalDebugLevel > 2)
00135         {
00136           printf("TestLeapSecs: BEFORE LEAP SECOND ADDED\n");
00137         }
00138       
00139       gpsTime.gpsSeconds = leapsec_data[i].gps - 1;
00140       LALLeapSecs(&status, &gps_utc, &gpsTime, &formatAndAcc);
00141       if (lalDebugLevel > 0)
00142         printf("\tGPS = %9d;    GPS-UTC = %d\n", gpsTime.gpsSeconds,
00143                gps_utc);
00144             
00145       if (status.statusCode && lalDebugLevel > 0)
00146         {
00147           fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00148                   __LINE__, LALTESTLEAPSECSC);
00149           REPORTSTATUS(&status);
00150           return status.statusCode;
00151         }
00152 
00153       if (gps_utc != leapsec_data[i-1].tai_utc - 19)
00154         {
00155           if (lalDebugLevel > 0)
00156             {
00157               fprintf(stderr,
00158                       "TestLeapSecs: LALLeapSecs returned wrong value: expected %d, got %d; i = %d\n",
00159                       leapsec_data[i-1].tai_utc - 19, gps_utc, i);
00160               REPORTSTATUS(&status);
00161             }
00162           return 1;
00163         }
00164 
00165       if (lalDebugLevel > 2)
00166         {
00167           printf("TestLeapSecs: AFTER LEAP SECOND ADDED\n");
00168         }
00169       
00170       gpsTime.gpsSeconds += 2;
00171       LALLeapSecs(&status, &gps_utc, &gpsTime, &formatAndAcc);
00172       if (lalDebugLevel > 0)
00173         printf("\tGPS = %9d;    GPS-UTC = %d\n\n", gpsTime.gpsSeconds,
00174                gps_utc);
00175 
00176       if (status.statusCode && lalDebugLevel > 0)
00177         {
00178           fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00179                   __LINE__, LALTESTLEAPSECSC);
00180           REPORTSTATUS(&status);
00181           return status.statusCode;
00182         }
00183 
00184       if (gps_utc != leapsec_data[i].tai_utc - 19)
00185         {
00186           if (lalDebugLevel > 0)
00187             {
00188               fprintf(stderr,
00189                       "TestLeapSecs: LALLeapSecs returned wrong value: expected %d, got %d; i = %d\n",
00190                       leapsec_data[i-1].tai_utc - 19, gps_utc, i);
00191               REPORTSTATUS(&status);
00192             }
00193           return 1;
00194         }
00195     } /* for */
00196 
00197 
00198   formatAndAcc.accuracy = LALLEAPSEC_LOOSE;
00199   gpsTime.gpsSeconds = 701654353 + 4;
00200 
00201   LALLeapSecs(&status, &gps_utc, &gpsTime, &formatAndAcc);
00202   if (lalDebugLevel > 0)
00203     printf("\tGPS = %9d;    GPS-UTC = %d\n", gpsTime.gpsSeconds,
00204            gps_utc);
00205 
00206   if (status.statusCode && lalDebugLevel > 0)
00207     {
00208       fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00209               __LINE__, LALTESTLEAPSECSC);
00210       REPORTSTATUS(&status);
00211       return status.statusCode;
00212     }
00213 
00214   if (gps_utc != 13)
00215     {
00216       if (lalDebugLevel > 0)
00217         {
00218           fprintf(stderr,
00219                   "TestLeapSecs: LALLeapSecs returned wrong value: expected 13, got %d; i = %d\n",
00220                   gps_utc, i);
00221           REPORTSTATUS(&status);
00222         }
00223       return 1;
00224     }
00225 
00226   if (lalDebugLevel > 0)
00227     printf("\nTestLeapSecs: TESTING TAI-UTC\n\n");
00228 
00229   /*
00230    * Test TAI-UTC
00231    */
00232   formatAndAcc.format = LALLEAPSEC_TAIUTC;
00233   
00234   for (i = 1; i < num_data; ++i)
00235     {
00236       if (lalDebugLevel > 2)
00237         {
00238           printf("TestLeapSecs: BEFORE LEAP SECOND ADDED\n");
00239         }
00240       
00241       gpsTime.gpsSeconds = leapsec_data[i].gps - 1;
00242       LALLeapSecs(&status, &tai_utc, &gpsTime, &formatAndAcc);
00243       if (lalDebugLevel > 0)
00244         printf("\tGPS = %9d;    TAI-UTC = %d\n", gpsTime.gpsSeconds,
00245                tai_utc);
00246             
00247       if (status.statusCode && lalDebugLevel > 0)
00248         {
00249           fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00250                   __LINE__, LALTESTLEAPSECSC);
00251           REPORTSTATUS(&status);
00252           return status.statusCode;
00253         }
00254 
00255       if (tai_utc != leapsec_data[i-1].tai_utc)
00256         {
00257           if (lalDebugLevel > 0)
00258             {
00259               fprintf(stderr,
00260                       "TestLeapSecs: LALLeapSecs returned wrong value: expected %d, got %d; i = %d\n",
00261                       leapsec_data[i-1].tai_utc, tai_utc, i);
00262               REPORTSTATUS(&status);
00263             }
00264           return 1;
00265         }
00266 
00267       if (lalDebugLevel > 2)
00268         {
00269           printf("TestLeapSecs: AFTER LEAP SECOND ADDED\n");
00270         }
00271       
00272       gpsTime.gpsSeconds += 2;
00273       LALLeapSecs(&status, &tai_utc, &gpsTime, &formatAndAcc);
00274       if (lalDebugLevel > 0)
00275         printf("\tGPS = %9d;    TAI-UTC = %d\n\n", gpsTime.gpsSeconds,
00276               tai_utc);
00277 
00278       if (status.statusCode && lalDebugLevel > 0)
00279         {
00280           fprintf(stderr, "TestLeapSecs: LALLeapSecs() failed, line %i, %s\n",
00281                   __LINE__, LALTESTLEAPSECSC);
00282           REPORTSTATUS(&status);
00283           return status.statusCode;
00284         }
00285 
00286       if (tai_utc != leapsec_data[i].tai_utc)
00287         {
00288           if (lalDebugLevel > 0)
00289             {
00290               fprintf(stderr,
00291                       "TestLeapSecs: LALLeapSecs returned wrong value: expected %d, got %d; i = %d\n",
00292                       leapsec_data[i-1].tai_utc, tai_utc, i);
00293               REPORTSTATUS(&status);
00294             }
00295           return 1;
00296         }
00297     } /* for */
00298 
00299   return 0;
00300 }

Generated on Sat Sep 6 03:07:41 2008 for LAL by  doxygen 1.5.2