FrameDataTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 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 #if 0 /* autodoc block */
00021 
00022 <lalVerbatim file="FrameDataTestCV">
00023 $Id: FrameDataTest.c,v 1.10 2007/06/08 14:41:46 bema Exp $
00024 </lalVerbatim>
00025 
00026 <lalLaTeX>
00027 
00028 \subsection{Program \texttt{FrameDataTest.c}}
00029 \label{ss:FrameDataTest.c}
00030 
00031 Tests the routines in \verb+FrameData.h+.
00032 
00033 \subsection*{Usage}
00034 \begin{verbatim}
00035 FrameDataTest [options]
00036 Options:
00037   -h         print this message
00038   -q         quiet: run silently
00039   -v         verbose: print extra information
00040   -d level   set lalDebugLevel to level
00041   -o         output framedata to files
00042   -f dir     set frame data path to dir
00043 \end{verbatim}
00044 
00045 Unless the \verb+-f+ option is used, the environment variable
00046 \verb+LAL_FRAME_PATH+ must be set to the directory containing the frame files.
00047 
00048 \subsubsection*{Description}
00049 \subsubsection*{Exit codes}
00050 \begin{tabular}{|c|l|}
00051 \hline
00052  Code & Explanation                   \00053 \hline
00054 \tt 0 & Success, normal exit.         \00055 \tt 1 & Subroutine failed.            \00056 \tt77 & Ignored failure: {\tt LAL\_FRAME\_PATH} not set. \00057 \hline
00058 \end{tabular}
00059 
00060 \subsubsection*{Uses}
00061 \subsubsection*{Notes}
00062 
00063 \vfill{\footnotesize\input{FrameDataTestCV}}
00064 
00065 </lalLaTeX>
00066 
00067 #endif /* autodoc block */
00068 
00069 
00070 #include <stdio.h>
00071 #include <string.h>
00072 #include <stdlib.h>
00073 #include <lal/LALConfig.h>
00074 
00075 #ifdef HAVE_UNISTD_H
00076 #include <unistd.h>
00077 #endif
00078 
00079 #ifdef HAVE_GETOPT_H
00080 #include <getopt.h>
00081 #endif
00082 
00083 #include <lal/LALStdlib.h>
00084 #include <lal/AVFactories.h>
00085 #include <lal/FrameData.h>
00086 
00087 #define CODES_(x) #x
00088 #define CODES(x) CODES_(x)
00089 
00090 NRCSID (MAIN, "$Id: FrameDataTest.c,v 1.10 2007/06/08 14:41:46 bema Exp $");
00091 
00092 extern char *optarg;
00093 extern int   optind;
00094 
00095 int   lalDebugLevel = 0;
00096 int   verbose    = 0;
00097 int   output     = 0;
00098 char *framePath  = NULL;
00099 
00100 static void
00101 Usage (const char *program, int exitflag);
00102 
00103 static void
00104 ParseOptions (int argc, char *argv[]);
00105 
00106 static void
00107 TestStatus (LALStatus *status, const char *expectedCodes, int exitCode);
00108 
00109 static void
00110 ClearStatus (LALStatus *status);
00111 
00112 int
00113 main (int argc, char *argv[])
00114 {
00115   const INT4               numPoints = 262144;
00116   static LALStatus            status;
00117   FrameData               *frameData = NULL;
00118   INT2TimeSeries           data;
00119   COMPLEX8FrequencySeries  response;
00120   INT4                     seg;
00121 
00122   ParseOptions (argc, argv);
00123 
00124   if (!framePath)
00125   {
00126     framePath = getenv ("LAL_FRAME_PATH");
00127     if (!framePath)
00128     {
00129       fprintf (stderr, "error: environment LAL_FRAME_PATH undefined\n");
00130       return 77;
00131     }
00132   }
00133 
00134   data.data = NULL;
00135   LALI2CreateVector (&status, &data.data, numPoints);
00136   TestStatus (&status, "0", 1);
00137 
00138   response.data = NULL;
00139   LALCCreateVector (&status, &response.data, numPoints/2 + 1);
00140   TestStatus (&status, "0", 1);
00141 
00142   LALInitializeFrameData (&status, &frameData, framePath);
00143   TestStatus (&status, CODES(0 FRAMEDATAH_EREAD), 1);
00144 
00145   for (seg = 0; seg < 99; ++seg)
00146   {
00147     LALGetFrameData (&status, &data, frameData);
00148     TestStatus (&status, "0", 1);
00149 
00150     fprintf (stderr, "Segment %2d... ", seg);
00151 
00152     if (frameData->endOfData)
00153     {
00154       fprintf (stderr, "end of data\n");
00155       break;
00156     }
00157 
00158     if (frameData->newLock)
00159     {
00160       fprintf (stderr, "starting new locked section, ");
00161     }
00162     else
00163     {
00164       fprintf (stderr, "continuing locked section, ");
00165     }
00166 
00167     if (frameData->newCalibration)
00168     {
00169       fprintf (stderr, "new calibration data, ");
00170 
00171       LALGetFrameDataResponse (&status, &response, frameData);
00172       TestStatus (&status, "0", 1);
00173 
00174       if (output)
00175       {
00176         FILE  *fp;
00177         CHAR   fname[64];
00178         UINT4  i;
00179 
00180         sprintf (fname, "Response.%03d", seg);
00181         fp = fopen (fname, "w");
00182 
00183         for (i = 0; i < response.data->length; ++i)
00184         {
00185           fprintf (fp, "%d\t%e\t%e\n", i, response.data->data[i].re,
00186                    response.data->data[i].im);
00187         }
00188 
00189         fclose (fp);
00190       }
00191     }
00192 
00193     fprintf (stderr, "gps time %d.%09d\n", data.epoch.gpsSeconds,
00194              data.epoch.gpsNanoSeconds);
00195 
00196     if (output)
00197     {
00198       FILE  *fp;
00199       CHAR   fname[64];
00200       UINT4  i;
00201 
00202       sprintf (fname, "Segment.%03d", seg);
00203       fp = fopen (fname, "w");
00204 
00205       for (i = 0; i < data.data->length; ++i)
00206       {
00207         fprintf (fp, "%d\t%d\n", i, data.data->data[i]);
00208       }
00209 
00210       fclose (fp);
00211     }
00212 
00213   }
00214 
00215 
00216   /* again... just to be annoying if end of data has happened */
00217   ++seg;
00218 
00219   LALGetFrameData (&status, &data, frameData);
00220   TestStatus (&status, "0", 1);
00221 
00222   fprintf (stderr, "Segment %2d... ", seg);
00223   if (frameData->endOfData)
00224   {
00225     fprintf (stderr, "end of data\n");
00226   }
00227 
00228   /* clean up */
00229 
00230   LALFinalizeFrameData (&status, &frameData);
00231   TestStatus (&status, "0", 1);
00232 
00233   LALCDestroyVector    (&status, &response.data);
00234   TestStatus (&status, "0", 1);
00235 
00236   LALI2DestroyVector   (&status, &data.data);
00237   TestStatus (&status, "0", 1);
00238 
00239   LALCheckMemoryLeaks ();
00240   return 0;
00241 }
00242 
00243 
00244 
00245 /*
00246  * TestStatus ()
00247  *
00248  * Routine to check that the status code status->statusCode agrees with one of
00249  * the codes specified in the space-delimited string ignored; if not,
00250  * exit to the system with code exitcode.
00251  *
00252  */
00253 static void
00254 TestStatus (LALStatus *status, const char *ignored, int exitcode)
00255 {
00256   char  str[64];
00257   char *tok;
00258 
00259   if (verbose)
00260   {
00261     REPORTSTATUS (status);
00262   }
00263 
00264   if (strncpy (str, ignored, sizeof (str)))
00265   {
00266     if ((tok = strtok (str, " ")))
00267     {
00268       do
00269       {
00270         if (status->statusCode == atoi (tok))
00271         {
00272           return;
00273         }
00274       }
00275       while ((tok = strtok (NULL, " ")));
00276     }
00277     else
00278     {
00279       if (status->statusCode == atoi (tok))
00280       {
00281         return;
00282       }
00283     }
00284   }
00285 
00286   fprintf (stderr, "\nExiting to system with code %d\n", exitcode);
00287   exit (exitcode);
00288 }
00289 
00290 
00291 /*
00292  *
00293  * ClearStatus ()
00294  *
00295  * Recursively applies DETATCHSTATUSPTR() to status structure to destroy
00296  * linked list of statuses.
00297  *
00298  */
00299 void
00300 ClearStatus (LALStatus *status)
00301 {
00302   if (status->statusPtr)
00303   {
00304     ClearStatus      (status->statusPtr);
00305     DETATCHSTATUSPTR (status);
00306   }
00307 }
00308 
00309 
00310 /*
00311  * Usage ()
00312  *
00313  * Prints a usage message for program program and exits with code exitcode.
00314  *
00315  */
00316 static void
00317 Usage (const char *program, int exitcode)
00318 {
00319   fprintf (stderr, "Usage: %s [options]\n", program);
00320   fprintf (stderr, "Options:\n");
00321   fprintf (stderr, "  -h         print this message\n");
00322   fprintf (stderr, "  -q         quiet: run silently\n");
00323   fprintf (stderr, "  -v         verbose: print extra information\n");
00324   fprintf (stderr, "  -d level   set lalDebugLevel to level\n");
00325   fprintf (stderr, "  -o         output framedata to files\n");
00326   fprintf (stderr, "  -f dir     set frame data path to dir\n");
00327   fprintf (stderr, "             "
00328                    "(otherwise use path in environment LAL_FRAME_PATH)\n");
00329   exit (exitcode);
00330 }
00331 
00332 
00333 /*
00334  * ParseOptions ()
00335  *
00336  * Parses the argc - 1 option strings in argv[].
00337  *
00338  */
00339 static void
00340 ParseOptions (int argc, char *argv[])
00341 {
00342   while (1)
00343   {
00344     int c = -1;
00345 
00346     c = getopt (argc, argv, "hqvd:""of:");
00347     if (c == -1)
00348     {
00349       break;
00350     }
00351 
00352     switch (c)
00353     {
00354       case 'f': /* sets frame path */
00355         framePath = optarg;
00356         break;
00357 
00358       case 'o': /* sets flag to write output files */
00359         output = 1;
00360         break;
00361 
00362       case 'd': /* set debug level */
00363         lalDebugLevel = atoi (optarg);
00364         break;
00365 
00366       case 'v': /* verbose */
00367         ++verbose;
00368         break;
00369 
00370       case 'q': /* quiet: run silently (ignore error messages) */
00371         freopen ("/dev/null", "w", stderr);
00372         break;
00373 
00374       case 'h':
00375         Usage (argv[0], 0);
00376         break;
00377 
00378       default:
00379         Usage (argv[0], 1);
00380     }
00381 
00382   }
00383 
00384   if (optind < argc)
00385   {
00386     Usage (argv[0], 1);
00387   }
00388 
00389   return;
00390 }
00391 

Generated on Fri Sep 5 03:06:52 2008 for LAL by  doxygen 1.5.2