SpecBufferTest.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="SpecBufferTestCV">
00023 $Id: SpecBufferTest.c,v 1.11 2007/06/08 14:41:46 bema Exp $
00024 </lalVerbatim>
00025 
00026 <lalLaTeX>
00027 
00028 \subsection{Program \texttt{SpecBufferTest.c}}
00029 \label{ss:SpecBufferTest.c}
00030 
00031 Tests the routines in \verb+SpecBuffer.h+.
00032 
00033 \subsection*{Usage}
00034 \begin{verbatim}
00035 SpecBufferTest [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{SpecBufferTestCV}}
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/FrameData.h>
00087 #include <lal/SpecBuffer.h>
00088 
00089 #define CODES_(x) #x
00090 #define CODES(x) CODES_(x)
00091 
00092 NRCSID (MAIN, "$Id: SpecBufferTest.c,v 1.11 2007/06/08 14:41:46 bema Exp $");
00093 
00094 extern char *optarg;
00095 extern int   optind;
00096 
00097 int   lalDebugLevel = 0;
00098 int   verbose    = 0;
00099 int   output     = 0;
00100 char *framePath  = NULL;
00101 
00102 static void
00103 Usage (const char *program, int exitflag);
00104 
00105 static void
00106 ParseOptions (int argc, char *argv[]);
00107 
00108 static void
00109 TestStatus (LALStatus *status, const char *expectedCodes, int exitCode);
00110 
00111 static void
00112 ClearStatus (LALStatus *status);
00113 
00114 int
00115 main (int argc, char *argv[])
00116 {
00117   const INT4 numPoints = 65536;
00118   const INT4 numSpec   = 8;
00119   const INT4 numSegs   = 10;
00120 
00121   static LALStatus            status;
00122   FrameData               *frameData = NULL;
00123   INT2TimeSeries           data;
00124   COMPLEX8FrequencySeries  response;
00125   REAL4FrequencySeries     spectrum;
00126   SpectrumBuffer          *specBuff  = NULL;
00127   SpectrumBufferPar        specParm;
00128   INT4                     newLock   = 0;
00129   INT4                     seg;
00130 
00131   ParseOptions (argc, argv);
00132 
00133   if (!framePath)
00134   {
00135     framePath = getenv ("LAL_FRAME_PATH");
00136     if (!framePath)
00137     {
00138       fprintf (stderr, "error: environment LAL_FRAME_PATH undefined\n");
00139       return 77;
00140     }
00141   }
00142 
00143   data.data = NULL;
00144   LALI2CreateVector (&status, &data.data, numPoints);
00145   TestStatus (&status, "0", 1);
00146 
00147   response.data = NULL;
00148   LALCCreateVector (&status, &response.data, numPoints/2 + 1);
00149   TestStatus (&status, "0", 1);
00150 
00151   LALInitializeFrameData (&status, &frameData, framePath);
00152   TestStatus (&status, CODES(0 FRAMEDATAH_EREAD), 1);
00153 
00154   spectrum.data = NULL;
00155   LALCreateVector (&status, &spectrum.data, numPoints/2 + 1);
00156   TestStatus (&status, "0", 1);
00157 
00158   specParm.numSpec    = numSpec;
00159   specParm.numPoints  = numPoints;
00160   specParm.windowType = Welch;
00161   specParm.plan       = NULL;
00162 
00163   LALCreateForwardRealFFTPlan (&status, &specParm.plan, numPoints, 0);
00164   TestStatus (&status, "0", 1);
00165 
00166   LALCreateSpectrumBuffer (&status, &specBuff, &specParm);
00167   TestStatus (&status, "0", 1);
00168 
00169   for (seg = 0; seg < numSegs; ++seg)
00170   {
00171     INT4 newCal = 0;
00172 
00173     fprintf (stderr, "Segment %2d", seg);
00174 
00175     /* get next segment of locked data */
00176     do
00177     {
00178       while (newLock)
00179       {
00180         INT2TimeSeries dummy;
00181         INT2Vector     dumvec;
00182 
00183         dumvec.length   = 3*60/data.deltaT; /* 3 minutes */
00184         dumvec.data     = NULL;             /* seek mode */
00185         dummy.data      = &dumvec;
00186 
00187         LALGetFrameData (&status, &dummy, frameData);
00188         TestStatus (&status, "0", 1);
00189         if (frameData->endOfData)
00190         {
00191           fprintf (stderr, "... end of data\n");
00192           goto exit;
00193         }
00194         fprintf (stderr, "... skipping 3 minutes into new lock");
00195         newLock = frameData->newLock;
00196         if (frameData->newCalibration)
00197         {
00198           newCal = 1;
00199         }
00200       }
00201 
00202       LALGetFrameData (&status, &data, frameData);
00203       TestStatus (&status, "0", 1);
00204       if (frameData->endOfData)
00205       {
00206         fprintf (stderr, "... end of data\n");
00207         goto exit;
00208       }
00209       newLock = frameData->newLock;
00210       if (frameData->newCalibration)
00211       {
00212         newCal = 1;
00213       }
00214     }
00215     while (newLock);
00216 
00217     if (newCal)
00218     {
00219       LALGetFrameDataResponse (&status, &response, frameData);
00220       TestStatus (&status, "0", 1);
00221 
00222       /* print calibration info */
00223       if (output)
00224       {
00225         FILE  *fp;
00226         CHAR   fname[64];
00227         UINT4  i;
00228 
00229         sprintf (fname, "Response.%03d", seg);
00230         fp = fopen (fname, "w");
00231 
00232         for (i = 0; i < response.data->length; ++i)
00233         {
00234           REAL4 re  = response.data->data[i].re;
00235           REAL4 im  = response.data->data[i].im;
00236           REAL4 mod = sqrt (re*re + im*im);
00237           REAL4 arg = atan2 (im, re);
00238           fprintf (fp, "%u\t%e\t%e\n", i, mod, arg);
00239         }
00240 
00241         fclose (fp);
00242       }
00243 
00244       fprintf (stderr, "... new calibration data");
00245 
00246     }
00247 
00248 
00249     /* print data vector */
00250     if (output)
00251     {
00252       FILE  *fp;
00253       CHAR   fname[64];
00254       UINT4  i;
00255 
00256       sprintf (fname, "Segment.%03d", seg);
00257       fp = fopen (fname, "w");
00258 
00259       for (i = 0; i < data.data->length; ++i)
00260       {
00261         fprintf (fp, "%u\t%d\n", i, data.data->data[i]);
00262       }
00263 
00264       fclose (fp);
00265     }
00266 
00267     LALAddSpectrum (&status, specBuff, &data);
00268     TestStatus (&status, "0", 1);
00269 
00270     fprintf (stderr, "\n");
00271   }
00272 
00273 exit:
00274 
00275   LALAverageSpectrum (&status, &spectrum, specBuff);
00276   TestStatus (&status, CODES(0 SPECBUFFERH_ENONE), 1);
00277 
00278   if (output)
00279   {
00280     FILE  *fp;
00281     UINT4  i;
00282 
00283     fp = fopen ("Spectrum.avg", "w");
00284 
00285     for (i = 0; i < spectrum.data->length; ++i)
00286       fprintf (fp, "%u\t%e\n", i, spectrum.data->data[i]);
00287 
00288     fclose (fp);
00289   }
00290 
00291   LALDestroyVector (&status, &spectrum.data);
00292   TestStatus (&status, "0", 1);
00293 
00294   LALDestroySpectrumBuffer (&status, &specBuff);
00295   TestStatus (&status, "0", 1);
00296 
00297   LALDestroyRealFFTPlan (&status, &specParm.plan);
00298   TestStatus (&status, "0", 1);
00299 
00300   LALFinalizeFrameData (&status, &frameData);
00301   TestStatus (&status, "0", 1);
00302 
00303   LALI2DestroyVector (&status, &data.data);
00304   TestStatus (&status, "0", 1);
00305 
00306   LALCDestroyVector (&status, &response.data);
00307   TestStatus (&status, "0", 1);
00308 
00309   LALCheckMemoryLeaks ();
00310 
00311   return 0;
00312 }
00313 
00314 
00315 
00316 /*
00317  * TestStatus ()
00318  *
00319  * Routine to check that the status code status->statusCode agrees with one of
00320  * the codes specified in the space-delimited string ignored; if not,
00321  * exit to the system with code exitcode.
00322  *
00323  */
00324 static void
00325 TestStatus (LALStatus *status, const char *ignored, int exitcode)
00326 {
00327   char  str[64];
00328   char *tok;
00329 
00330   if (verbose)
00331   {
00332     REPORTSTATUS (status);
00333   }
00334 
00335   if (strncpy (str, ignored, sizeof (str)))
00336   {
00337     if ((tok = strtok (str, " ")))
00338     {
00339       do
00340       {
00341         if (status->statusCode == atoi (tok))
00342         {
00343           return;
00344         }
00345       }
00346       while ((tok = strtok (NULL, " ")));
00347     }
00348     else
00349     {
00350       if (status->statusCode == atoi (tok))
00351       {
00352         return;
00353       }
00354     }
00355   }
00356 
00357   fprintf (stderr, "\nExiting to system with code %d\n", exitcode);
00358   exit (exitcode);
00359 }
00360 
00361 
00362 /*
00363  *
00364  * ClearStatus ()
00365  *
00366  * Recursively applies DETATCHSTATUSPTR() to status structure to destroy
00367  * linked list of statuses.
00368  *
00369  */
00370 void
00371 ClearStatus (LALStatus *status)
00372 {
00373   if (status->statusPtr)
00374   {
00375     ClearStatus      (status->statusPtr);
00376     DETATCHSTATUSPTR (status);
00377   }
00378 }
00379 
00380 
00381 /*
00382  * Usage ()
00383  *
00384  * Prints a usage message for program program and exits with code exitcode.
00385  *
00386  */
00387 static void
00388 Usage (const char *program, int exitcode)
00389 {
00390   fprintf (stderr, "Usage: %s [options]\n", program);
00391   fprintf (stderr, "Options:\n");
00392   fprintf (stderr, "  -h         print this message\n");
00393   fprintf (stderr, "  -q         quiet: run silently\n");
00394   fprintf (stderr, "  -v         verbose: print extra information\n");
00395   fprintf (stderr, "  -d level   set lalDebugLevel to level\n");
00396   fprintf (stderr, "  -o         output framedata to files\n");
00397   fprintf (stderr, "  -f dir     set frame data path to dir\n");
00398   fprintf (stderr, "             "
00399                    "(otherwise use path in environment LAL_FRAME_PATH)\n");
00400   exit (exitcode);
00401 }
00402 
00403 
00404 /*
00405  * ParseOptions ()
00406  *
00407  * Parses the argc - 1 option strings in argv[].
00408  *
00409  */
00410 static void
00411 ParseOptions (int argc, char *argv[])
00412 {
00413   while (1)
00414   {
00415     int c = -1;
00416 
00417     c = getopt (argc, argv, "hqvd:""of:");
00418     if (c == -1)
00419     {
00420       break;
00421     }
00422 
00423     switch (c)
00424     {
00425       case 'f': /* sets frame path */
00426         framePath = optarg;
00427         break;
00428 
00429       case 'o': /* sets flag to write output files */
00430         output = 1;
00431         break;
00432 
00433       case 'd': /* set debug level */
00434         lalDebugLevel = atoi (optarg);
00435         break;
00436 
00437       case 'v': /* verbose */
00438         ++verbose;
00439         break;
00440 
00441       case 'q': /* quiet: run silently (ignore error messages) */
00442         freopen ("/dev/null", "w", stderr);
00443         break;
00444 
00445       case 'h':
00446         Usage (argv[0], 0);
00447         break;
00448 
00449       default:
00450         Usage (argv[0], 1);
00451     }
00452 
00453   }
00454 
00455   if (optind < argc)
00456   {
00457     Usage (argv[0], 1);
00458   }
00459 
00460   return;
00461 }
00462 

Generated on Tue Oct 7 02:40:28 2008 for LAL by  doxygen 1.5.2