DataBufferTest.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="DataBufferTestCV">
00023 $Id: DataBufferTest.c,v 1.11 2007/06/08 14:41:46 bema Exp $
00024 </lalVerbatim>
00025 
00026 <lalLaTeX>
00027 
00028 \subsection{Program \texttt{DataBufferTest.c}}
00029 \label{ss:DataBufferTest.c}
00030 
00031 Tests the routines in \verb+DataBuffer.h+.
00032 
00033 \subsection*{Usage}
00034 \begin{verbatim}
00035 DataBufferTest [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{DataBufferTestCV}}
00064 
00065 </lalLaTeX>
00066 
00067 #endif /* autodoc block */
00068 
00069 
00070 #include <stdio.h>
00071 #include <string.h>
00072 #include <stdlib.h>
00073 #include <math.h>
00074 #include <lal/LALConfig.h>
00075 
00076 #ifdef HAVE_UNISTD_H
00077 #include <unistd.h>
00078 #endif
00079 
00080 #ifdef HAVE_GETOPT_H
00081 #include <getopt.h>
00082 #endif
00083 
00084 #include <lal/LALStdlib.h>
00085 #include <lal/AVFactories.h>
00086 #include <lal/DataBuffer.h>
00087 
00088 #define CODES_(x) #x
00089 #define CODES(x) CODES_(x)
00090 
00091 NRCSID (MAIN, "$Id: DataBufferTest.c,v 1.11 2007/06/08 14:41:46 bema Exp $");
00092 
00093 extern char *optarg;
00094 extern int   optind;
00095 
00096 int   lalDebugLevel = 1;
00097 int   verbose    = 0;
00098 int   output     = 0;
00099 char *framePath  = NULL;
00100 
00101 static void
00102 Usage (const char *program, int exitflag);
00103 
00104 static void
00105 ParseOptions (int argc, char *argv[]);
00106 
00107 static void
00108 TestStatus (LALStatus *status, const char *expectedCodes, int exitCode);
00109 
00110 static void
00111 ClearStatus (LALStatus *status);
00112 
00113 int
00114 main (int argc, char *argv[])
00115 {
00116   const INT4 numPoints = 65536;
00117   const INT4 numSpec   = 8;
00118   const INT4 numSegs   = 10;
00119 
00120   static LALStatus            status;
00121   DataBuffer              *buffer = NULL;
00122   DataBufferPar            bufferPar;
00123   DataSegment              dataout;
00124   INT2TimeSeries           data;
00125   REAL4FrequencySeries     spec;
00126   COMPLEX8FrequencySeries  resp;
00127   INT4                     seg;
00128 
00129   ParseOptions (argc, argv);
00130 
00131   if (!framePath)
00132   {
00133     framePath = getenv ("LAL_FRAME_PATH");
00134     if (!framePath)
00135     {
00136       fprintf (stderr, "error: environment LAL_FRAME_PATH undefined\n");
00137       return 77;
00138     }
00139   }
00140 
00141   data.data = NULL;
00142   spec.data = NULL;
00143   resp.data = NULL;
00144 
00145   LALI2CreateVector (&status, &data.data, numPoints);
00146   TestStatus (&status, "0", 1);
00147 
00148   LALCreateVector (&status, &spec.data, numPoints/2 + 1);
00149   TestStatus (&status, "0", 1);
00150 
00151   LALCCreateVector (&status, &resp.data, numPoints/2 + 1);
00152   TestStatus (&status, "0", 1);
00153 
00154   dataout.data = &data;
00155   dataout.spec = &spec;
00156   dataout.resp = &resp;
00157 
00158   bufferPar.numSpec    = numSpec;
00159   bufferPar.numPoints  = numPoints;
00160   bufferPar.windowType = Welch;
00161   bufferPar.plan       = NULL;
00162   bufferPar.framePath  = framePath;
00163   LALCreateForwardRealFFTPlan (&status, &bufferPar.plan, numPoints, 0);
00164   TestStatus (&status, "0", 1);
00165 
00166   LALCreateDataBuffer (&status, &buffer, &bufferPar);
00167   TestStatus (&status, "-1 0", 1);
00168   ClearStatus (&status);
00169 
00170   for (seg = 0; seg < numSegs; ++seg)
00171   {
00172     fprintf (stderr, "Segment %2d", seg);
00173 
00174     LALGetData (&status, &dataout, 3*numPoints/4, buffer);
00175     TestStatus (&status, "-1 0", 1);
00176     ClearStatus (&status);
00177 
00178     if (dataout.endOfData)
00179     {
00180       fprintf (stderr, "... end of data\n");
00181       goto exit;
00182     }
00183 
00184     /* print data vector */
00185     if (output)
00186     {
00187       FILE  *fp;
00188       CHAR   fname[64];
00189       UINT4  i;
00190 
00191       sprintf (fname, "Segment.%03d", seg);
00192       fp = fopen (fname, "w");
00193 
00194       for (i = 0; i < dataout.data->data->length; ++i)
00195         fprintf (fp, "%u\t%d\n", i, dataout.data->data->data[i]);
00196 
00197       fclose (fp);
00198     }
00199 
00200     /* print spectrum */
00201     if (output)
00202     {
00203       FILE  *fp;
00204       CHAR   fname[64];
00205       UINT4  i;
00206 
00207       sprintf (fname, "Spectrum.%03d", seg);
00208       fp = fopen (fname, "w");
00209 
00210       for (i = 0; i < dataout.spec->data->length; ++i)
00211         fprintf (fp, "%u\t%e\n", i, dataout.spec->data->data[i]);
00212 
00213       fclose (fp);
00214     }
00215 
00216     /* print calibration info */
00217     if (output)
00218     {
00219       FILE  *fp;
00220       CHAR   fname[64];
00221       UINT4  i;
00222 
00223       sprintf (fname, "Response.%03d", seg);
00224       fp = fopen (fname, "w");
00225 
00226       for (i = 0; i < dataout.resp->data->length; ++i)
00227       {
00228         REAL8 re  = dataout.resp->data->data[i].re;
00229         REAL8 im  = dataout.resp->data->data[i].im;
00230         REAL8 mod = sqrt (re*re + im*im);
00231         REAL8 arg = atan2 (im, re);
00232         fprintf (fp, "%u\t%e\t%e\n", i, mod, arg);
00233       }
00234 
00235       fclose (fp);
00236     }
00237 
00238     fprintf (stderr, "\n");
00239 
00240   }
00241 
00242 exit:
00243 
00244   LALDestroyRealFFTPlan (&status, &bufferPar.plan);
00245   TestStatus (&status, "0", 1);
00246   LALDestroyDataBuffer (&status, &buffer);
00247   TestStatus (&status, "0", 1);
00248   LALI2DestroyVector (&status, &data.data);
00249   TestStatus (&status, "0", 1);
00250   LALDestroyVector (&status, &spec.data);
00251   TestStatus (&status, "0", 1);
00252   LALCDestroyVector (&status, &resp.data);
00253   TestStatus (&status, "0", 1);
00254 
00255   LALCheckMemoryLeaks ();
00256   return 0;
00257 }
00258 
00259 
00260 
00261 /*
00262  * TestStatus ()
00263  *
00264  * Routine to check that the status code status->statusCode agrees with one of
00265  * the codes specified in the space-delimited string ignored; if not,
00266  * exit to the system with code exitcode.
00267  *
00268  */
00269 static void
00270 TestStatus (LALStatus *status, const char *ignored, int exitcode)
00271 {
00272   char  str[64];
00273   char *tok;
00274 
00275   if (verbose)
00276   {
00277     REPORTSTATUS (status);
00278   }
00279 
00280   if (strncpy (str, ignored, sizeof (str)))
00281   {
00282     if ((tok = strtok (str, " ")))
00283     {
00284       do
00285       {
00286         if (status->statusCode == atoi (tok))
00287         {
00288           return;
00289         }
00290       }
00291       while ((tok = strtok (NULL, " ")));
00292     }
00293     else
00294     {
00295       if (status->statusCode == atoi (tok))
00296       {
00297         return;
00298       }
00299     }
00300   }
00301 
00302   fprintf (stderr, "\nExiting to system with code %d\n", exitcode);
00303   exit (exitcode);
00304 }
00305 
00306 
00307 /*
00308  *
00309  * ClearStatus ()
00310  *
00311  * Recursively applies DETATCHSTATUSPTR() to status structure to destroy
00312  * linked list of statuses.
00313  *
00314  */
00315 void
00316 ClearStatus (LALStatus *status)
00317 {
00318   if (status->statusPtr)
00319   {
00320     ClearStatus      (status->statusPtr);
00321     DETATCHSTATUSPTR (status);
00322   }
00323 }
00324 
00325 
00326 /*
00327  * Usage ()
00328  *
00329  * Prints a usage message for program program and exits with code exitcode.
00330  *
00331  */
00332 static void
00333 Usage (const char *program, int exitcode)
00334 {
00335   fprintf (stderr, "Usage: %s [options]\n", program);
00336   fprintf (stderr, "Options:\n");
00337   fprintf (stderr, "  -h         print this message\n");
00338   fprintf (stderr, "  -q         quiet: run silently\n");
00339   fprintf (stderr, "  -v         verbose: print extra information\n");
00340   fprintf (stderr, "  -d level   set lalDebugLevel to level\n");
00341   fprintf (stderr, "  -o         output framedata to files\n");
00342   fprintf (stderr, "  -f dir     set frame data path to dir\n");
00343   fprintf (stderr, "             "
00344                    "(otherwise use path in environment LAL_FRAME_PATH)\n");
00345   exit (exitcode);
00346 }
00347 
00348 
00349 /*
00350  * ParseOptions ()
00351  *
00352  * Parses the argc - 1 option strings in argv[].
00353  *
00354  */
00355 static void
00356 ParseOptions (int argc, char *argv[])
00357 {
00358   while (1)
00359   {
00360     int c = -1;
00361 
00362     c = getopt (argc, argv, "hqvd:""of:");
00363     if (c == -1)
00364     {
00365       break;
00366     }
00367 
00368     switch (c)
00369     {
00370       case 'f': /* sets frame path */
00371         framePath = optarg;
00372         break;
00373 
00374       case 'o': /* sets flag to write output files */
00375         output = 1;
00376         break;
00377 
00378       case 'd': /* set debug level */
00379         lalDebugLevel = atoi (optarg);
00380         break;
00381 
00382       case 'v': /* verbose */
00383         ++verbose;
00384         break;
00385 
00386       case 'q': /* quiet: run silently (ignore error messages) */
00387         freopen ("/dev/null", "w", stderr);
00388         break;
00389 
00390       case 'h':
00391         Usage (argv[0], 0);
00392         break;
00393 
00394       default:
00395         Usage (argv[0], 1);
00396     }
00397 
00398   }
00399 
00400   if (optind < argc)
00401   {
00402     Usage (argv[0], 1);
00403   }
00404 
00405   return;
00406 }
00407 

Generated on Sat Sep 6 03:06:48 2008 for LAL by  doxygen 1.5.2