FreqFiducialTime.c

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2007  Holger Pletsch
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 /********************************************************************************************/
00021 /*      FreqFiducialTime - shifting frequency parameters of Einstein at Home result files   */
00022 /*              to a fixed fiducial time for post-processing coincidence analysis           */
00023 /*                                                                                          */
00024 /*                              Holger Pletsch, UWM - March 2006                            */ 
00025 /********************************************************************************************/
00026 
00027 /* This code simply shifts all frequency parameters of a combined result file generated
00028    by combiner_v2.py, to a fixed fiducial GPS time for later coincidence analysis. 
00029    Note: The code makes use of the current Einstein at Home setup file.
00030    (i.e. 'CFS_S4R2_setup.h')  
00031 */
00032 
00033 
00034 /***************************************************************************************************************************/
00035 /*   INSTRUCTIONS for building "FreqFiducialTime" under LALApps                                                            */
00036 /*                                                                                                                         */
00037 /*   Files from Einstein@Home Workunit Generator that are needed from:                                                     */
00038 /*   http://www.lsc-group.phys.uwm.edu/cgi-bin/cvs/viewcvs.cgi/einsteinathome/CFS/workunit_generator/?cvsroot=lscsoft      */
00039 /*                                                                                                                         */
00040 /*   - CFS_S4R2_setup.C (version 1.4, 2006/07/21)                                                                          */
00041 /*   - WU_generator_misc.C (version 1.2, 2006/06/08)                                                                       */
00042 /*   - WU_generator_daemon.h (version 1.6, 2006/07/21)                                                                     */
00043 /*                                                                                                                         */
00044 /*   Add the following lines to Makefile.am:                                                                               */
00045 /*                                                                                                                         */
00046 /*   INSTANCE = a                                                                                                          */
00047 /*   warnLevel = -Wall -W -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Waggregate-return -fno-common */
00048 /*   WUDIR = ${LALAPPS_SRC}/pulsar/FDS_isolated                                                                            */
00049 /*   CFLAGS := $(CFLAGS) $(CXXFLAGS) -g -O3 $(warnLevel)                                                                   */
00050 /*                                                                                                                         */
00051 /*   bin_PROGRAMS = ... FreqFiducialTime                                                                                   */
00052 /*                                                                                                                         */
00053 /*   CFS_S4R2_setup_CFLAGS := $(CFLAGS) -DDEFAULT_INSTANCE=\"$(INSTANCE)\"                                                 */
00054 /*   CFS_S4R2_setup_SOURCES = CFS_S4R2_setup.C WU_generator_daemon.h                                                       */
00055 /*   WU_generator_misc_CFLAGS := $(CFLAGS) $(CXXFLAGS) -g $(warnLevel)                                                     */
00056 /*   WU_generator_misc_SOURCES = WU_generator_misc.C WU_generator_daemon.h                                                 */
00057 /*   FreqFiducialTime_IFLAGS = -I$(WUDIR)                                                                                  */
00058 /*   FreqFiducialTime_CFLAGS = $(CXXFLAGS) -Wall -g                                                                        */
00059 /*   FreqFiducialTime_SOURCES = FreqFiducialTime.c CFS_S4R2_setup.C WU_generator_misc.C WU_generator_daemon.h              */
00060 /*                                                                                                                         */
00061 /***************************************************************************************************************************/
00062 /* $Id: FreqFiducialTime.c,v 1.18 2008/07/25 16:10:49 hpletsch Exp $  */
00063 
00064 /* ----------------------------------------------------------------------------- */
00065 /* defines */
00066 #ifndef FALSE
00067 #define FALSE (1==0)
00068 #endif
00069 #ifndef TRUE
00070 #define TRUE  (1==1)
00071 #endif
00072 
00073 #define DONE_MARKER "%DONE\n"
00074 /* maximum depth of a linked structure. */
00075 #define LINKEDSTR_MAX_DEPTH 1024 
00076 
00077 
00078 /* ----------------------------------------------------------------------------- */
00079 /* file includes */
00080 #include "config.h"
00081 #include <stdio.h>
00082 #include <string.h>
00083 #include <stdlib.h>
00084 #include "getopt.h"
00085 #include <math.h>
00086 
00087 
00088 #include <unistd.h>
00089 
00090 #include <lal/LALDatatypes.h>
00091 #include <lal/LALMalloc.h>
00092 #include <lal/LALConstants.h>
00093 #include <lal/LALStatusMacros.h>
00094 #include <lal/ConfigFile.h>
00095 #include <lal/UserInput.h>
00096 
00097 #include <lalapps.h>
00098 
00099 #include "WU_generator_daemon.h"  /* current Einstein at Home setup */
00100 
00101 
00102 /* this is defined in C99 and *should* be in math.h.  Long term
00103    protect this with a HAVE_FINITE */
00104 #ifdef _MSC_VER
00105 #include <float.h>
00106 #define finite _finite
00107 #else
00108 int finite(double);
00109 #endif
00110 
00111 
00112 
00113 
00114 /* ----------------------------------------------------------------------------- */
00115 /* some error codes and messages */
00116 #define FIDUCIALC_ENULL            1
00117 #define FIDUCIALC_ENONULL          2
00118 #define FIDUCIALC_ESYS             3
00119 #define FIDUCIALC_EINVALIDFSTATS   4
00120 #define FIDUCIALC_EMEM             5
00121 #define FIDUCIALC_ENORMAL          6
00122 
00123 
00124 #define FIDUCIALC_MSGENULL         "Arguments contained an unexpected null pointer"
00125 #define FIDUCIALC_MSGENONULL       "Input pointer was not NULL"
00126 #define FIDUCIALC_MSGESYS          "System call failed (probably file IO"
00127 #define FIDUCIALC_MSGEINVALIDFSTATS "Invalid Fstats file"
00128 #define FIDUCIALC_MSGEMEM          "Sorry, ran out of memory... bye."
00129 
00130 
00131 #define FIDUCIAL_EXIT_OK 0
00132 #define FIDUCIAL_EXIT_ERR     31
00133 #define FIDUCIAL_EXIT_READCND  32
00134 #define FIDUCIAL_EXIT_FCTEST   33
00135 #define FIDUCIAL_EXIT_OUTFAIL  34
00136 
00137 
00138 /* ----------------------------------------------------------------------------- */
00139 /* structures */
00140 
00141 typedef struct FiducialTimeConfigVarsTag 
00142 {
00143   REAL8 ThrTwoF;
00144   INT4 InNumLines;
00145   INT4 FiducialTime; /*  The fiducial GPS time */
00146   CHAR *OutputFile;  /*  Name of output file */
00147   CHAR *InputFile;   /*  Name of input file (combined result file produced by combiner_v2.py */
00148 } FiducialTimeConfigVars;
00149 
00150 typedef struct CandidateListTag
00151 {
00152 
00153   REAL8 f;           /*  Frequency of the candidate */
00154   REAL8 Alpha;       /*  right ascension of the candidate */
00155   REAL8 Delta;       /*  declination  of the candidate */
00156   REAL8 F1dot;       /*  spindown (d/dt f) of the candidate */
00157   REAL8 TwoF;        /*  Maximum value of F for the cluster */
00158   INT4 FileID;       /*  File ID to specify from which file the candidate under consideration originally came. */
00159   INT4 DataStretch;
00160   CHAR resultfname[256];  /*  Name of the particular result file where values originally came from. */
00161 } CandidateList;     
00162 
00163 
00164 
00165 
00166 /* ----------------------------------------------------------------------------- */
00167 /* Function declarelations */
00168 void ReadCommandLineArgs( LALStatus *, INT4 argc, CHAR *argv[], FiducialTimeConfigVars *CLA ); 
00169 void ReadCombinedFile( LALStatus *lalStatus, CandidateList **CList, FiducialTimeConfigVars *CLA, long *candlen );
00170 
00171 void ComputeFiducialTimeFrequency( LALStatus *, FiducialTimeConfigVars *CLA, CandidateList *CList, INT4 candlen );
00172 
00173 void PrintResultFile( LALStatus *, const FiducialTimeConfigVars *CLA, CandidateList *CList, long candlen );
00174 
00175 void FreeMemory( LALStatus *, FiducialTimeConfigVars *CLA, CandidateList *CList, const UINT4 datalen );
00176 void FreeConfigVars( LALStatus *, FiducialTimeConfigVars *CLA );
00177 
00178 
00179 
00180 
00181 /* ----------------------------------------------------------------------------- */
00182 /* Global Variables */
00183 /*! @param global_status LALStatus Used to initialize LALStatus lalStatus. */
00184 LALStatus global_status;
00185 /*! @param lalDebugLevel INT4 Control debugging behaviours. Defined in lalapps.h */
00186 extern INT4 lalDebugLevel;
00187 /*! @param vrbflg        INT4 Control debugging messages. Defined in lalapps.h */
00188 extern INT4 vrbflg;
00189 
00190 const REAL8 FIXED_FIDUCIAL_TIME = 793555944; /* here e.g. GPS startTime of first data stretch in S4 is chosen */
00191 
00192 
00193 RCSID ("$Id: FreqFiducialTime.c,v 1.18 2008/07/25 16:10:49 hpletsch Exp $");
00194 
00195 
00196 
00197 /* ------------------------------------------------------------------------------------------*/
00198 /* Code starts here.                                                                         */
00199 /* ------------------------------------------------------------------------------------------*/
00200 /* ########################################################################################## */
00201 /*!
00202   Main function  
00203 */
00204 
00205 int main(INT4 argc,CHAR *argv[]) 
00206 {
00207   LALStatus *lalStatus = &global_status;
00208   long CLength=0;
00209   CandidateList *AllmyC = NULL;
00210   FiducialTimeConfigVars FTCV;
00211 
00212 
00213   lalDebugLevel = 0 ;  
00214   vrbflg = 1;   /* verbose error-messages */
00215 
00216   /* Get the debuglevel from command line arg, then set laldebuglevel. */
00217   LAL_CALL (LALGetDebugLevel(lalStatus, argc, argv, 'v'), lalStatus);
00218 
00219   /* Reads command line arguments */
00220   LAL_CALL( ReadCommandLineArgs( lalStatus, argc, argv, &FTCV ), lalStatus); 
00221 
00222   /* Reads in combined candidare file, set CLength */
00223   LAL_CALL( ReadCombinedFile(lalStatus, &AllmyC, &FTCV, &CLength), lalStatus);
00224 
00225   /* -----------------------------------------------------------------------------------------*/      
00226   /* Compute shifting of frequency parameters */
00227   LAL_CALL( ComputeFiducialTimeFrequency( lalStatus, &FTCV, AllmyC, CLength),lalStatus );
00228  
00229   /* -----------------------------------------------------------------------------------------*/      
00230   /* Output result file */
00231   LAL_CALL( PrintResultFile( lalStatus, &FTCV, AllmyC, CLength),lalStatus );
00232 
00233   /* -----------------------------------------------------------------------------------------*/      
00234   /* Clean-up */
00235   LAL_CALL( FreeMemory(lalStatus, &FTCV, AllmyC, CLength), lalStatus );
00236 
00237   LALCheckMemoryLeaks(); 
00238 
00239   return(FIDUCIAL_EXIT_OK);
00240  
00241 } /* main() */
00242 
00243 
00244 /* ########################################################################################## */
00245 
00246 void PrintResultFile(LALStatus *lalStatus, const FiducialTimeConfigVars *CLA, CandidateList *CList, long candlen)
00247 {
00248   
00249   INT4 iindex;
00250   FILE *fp = NULL;
00251   INT4 *count;
00252   INT4 nmax = 0;
00253   const CHAR *fname;
00254   
00255   INITSTATUS( lalStatus, "PrintResultFile", rcsid );
00256   ATTATCHSTATUSPTR (lalStatus);
00257 
00258   ASSERT( CLA != NULL, lalStatus, FIDUCIALC_ENULL, FIDUCIALC_MSGENULL);
00259   ASSERT( CList != NULL, lalStatus, FIDUCIALC_ENULL, FIDUCIALC_MSGENULL);
00260   
00261   if( (count = (INT4 *) LALCalloc( (size_t) (nmax + 1), sizeof(INT4))) == NULL ) {
00262     LALPrintError("Could not allocate Memory! \n");
00263     ABORT (lalStatus, FIDUCIALC_EMEM, FIDUCIALC_MSGEMEM);
00264   }
00265   
00266   fname=CLA->OutputFile;  
00267 
00268 
00269   /* ------------------------------------------------------------- */
00270   /* Print out to the user-specified output file.*/
00271   if(strcmp(fname,"-")==0){
00272     fp=stdout;
00273   }
00274   else {
00275     if( (fp = fopen(fname,"w")) == NULL ) 
00276       {
00277         LALPrintError("\n Cannot open output file %s\n",CLA->OutputFile); 
00278         ABORT (lalStatus, FIDUCIALC_EMEM, FIDUCIALC_MSGEMEM);
00279       }
00280   }
00281 
00282   /* output lines */
00283   /*INITSTATUS( lalStatus, "print_output", rcsid ); */
00284  
00285   iindex=0;
00286   
00287   while(iindex < candlen) 
00288     {
00289       fprintf(fp,"%" LAL_INT4_FORMAT " %.13g %.7g %.7g %.5g %.6g\n",
00290               CList[iindex].DataStretch, 
00291               CList[iindex].f, 
00292               CList[iindex].Alpha, 
00293               CList[iindex].Delta, 
00294               CList[iindex].F1dot, 
00295               CList[iindex].TwoF );
00296       iindex++;
00297     }
00298 
00299   fprintf(fp, "%s", DONE_MARKER);
00300   
00301   BEGINFAIL(lalStatus) {fclose(fp);} ENDFAIL(lalStatus);
00302 
00303   LALFree( count );
00304 
00305   DETATCHSTATUSPTR (lalStatus);
00306   RETURN (lalStatus);
00307 } /* PrintResult() */
00308 
00309 
00310 
00311 /* ########################################################################################## */
00312 
00313 void FreeMemory( LALStatus *lalStatus, 
00314             FiducialTimeConfigVars *CLA, 
00315             CandidateList *CList, 
00316             const UINT4 CLength)
00317 {
00318   INITSTATUS( lalStatus, "FreeMemory", rcsid );
00319   ATTATCHSTATUSPTR (lalStatus);
00320 
00321   FreeConfigVars( lalStatus->statusPtr, CLA );
00322 
00323   if( CList != NULL ) LALFree(CList);
00324 
00325   DETATCHSTATUSPTR (lalStatus);
00326   RETURN (lalStatus);
00327 } /* FreeMemory */
00328 
00329 
00330 /* ########################################################################################## */
00331 
00332 void FreeConfigVars(LALStatus *lalStatus, FiducialTimeConfigVars *CLA )
00333 {
00334   INITSTATUS( lalStatus, "FreeConfigVars", rcsid );
00335 
00336   if( CLA->OutputFile != NULL ) LALFree(CLA->OutputFile);
00337   if( CLA->InputFile != NULL ) LALFree(CLA->InputFile);
00338 
00339   RETURN (lalStatus);
00340 } /* FreeCOnfigVars */
00341 
00342 
00343 /* ########################################################################################## */
00344 
00345 void ReadCombinedFile( LALStatus *lalStatus, 
00346                       CandidateList **CList, 
00347                       FiducialTimeConfigVars *CLA, 
00348                       long *candlen )
00349 {
00350   long i,jj;
00351   INT4  numlines;
00352   REAL8 epsilon=1e-5;
00353   CHAR line1[256];
00354   FILE *fp;
00355   long nread;
00356   UINT4 checksum=0;
00357   UINT4 bytecount=0;
00358   INT4 sizelist=16384;
00359   const CHAR *fname;
00360         
00361   fname = CLA->InputFile;
00362  
00363   INITSTATUS( lalStatus, "ReadCombinedFile", rcsid );
00364   ATTATCHSTATUSPTR (lalStatus);
00365   ASSERT( fname != NULL, lalStatus, FIDUCIALC_ENULL, FIDUCIALC_MSGENULL);
00366   ASSERT( *CList == NULL, lalStatus, FIDUCIALC_ENONULL, FIDUCIALC_MSGENONULL);
00367 
00368   /* ------ Open and count candidates file ------ */
00369   i=0;
00370 
00371   if(strcmp(fname,"-")==0){
00372     fp=stdin;
00373     }
00374   else {
00375     fp=fopen(fname,"rb");
00376     if (fp==NULL) 
00377       {
00378         LALPrintError("File %s doesn't exist!\n",fname);
00379         ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00380       }
00381   }
00382 
00383   
00384   if(strcmp(fname,"-")==0){
00385     numlines = CLA->InNumLines;
00386 #if 0
00387     while(gets(line1)) {
00388       UINT4 k;
00389       size_t len=strlen(line1);
00390       
00391       /* increment line counter */
00392       i++;
00393       
00394       /* maintain a running checksum and byte count */
00395       bytecount+=len;
00396       for (k=0; k<len; k++)
00397         checksum+=(INT4)line1[k];
00398     }
00399 #endif
00400   }
00401   
00402   else{
00403     while( fgets(line1,sizeof(line1),fp) ) {
00404       UINT4 k;
00405       size_t len=strlen(line1);
00406       
00407       /* check that each line ends with a newline char (no overflow of
00408          line1 or null chars read) */
00409       if (!len || line1[len-1] != '\n') {
00410         LALPrintError(
00411                       "Line %d of file %s is too long or has no NEWLINE.  First 255 chars are:\n%s\n",
00412                       i+1, fname, line1);
00413         fclose(fp);
00414         ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00415       }
00416       
00417       /* increment line counter */
00418       i++;
00419       
00420       /* maintain a running checksum and byte count */
00421       bytecount+=len;
00422       for (k=0; k<len; k++)
00423         checksum+=(INT4)line1[k];
00424     }
00425     /* -- close candidate file -- */
00426     fclose(fp);  
00427     numlines=i;
00428   }
00429     
00430  
00431   
00432   if ( numlines == 0) 
00433     {
00434       LALPrintError ("ERROR: File '%s' has no lines so is not properly terminated by: %s", fname, DONE_MARKER);
00435       ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00436     }
00437 
00438   if(strcmp(fname,"-") != 0){
00439     /* output a record of the running checksun amd byte count */
00440     LALPrintError( "%% %s: bytecount %" LAL_UINT4_FORMAT " checksum %" LAL_UINT4_FORMAT "\n", fname, bytecount, checksum);
00441     
00442     /* check validity of this Fstats-file */
00443     if ( strcmp(strcat(line1,"\n"), DONE_MARKER ) ) 
00444       {
00445         LALPrintError ("ERROR: File '%s' is not properly terminated by: %sbut has %s instead", fname, DONE_MARKER, line1);
00446         ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00447       }
00448     else
00449       numlines --;        /* avoid stepping on DONE-marker */
00450 
00451     *candlen=numlines;
00452   }
00453 
00454 
00455 #if 0 /* Do we need to check this? */
00456   if (*candlen <= 0  )
00457     {
00458       LALPrintError("candidate length = %ud!\n",*candlen);
00459       exit(FIDUCIAL_EXIT_ERR);;
00460     }/* check that we have candidates. */
00461 #endif
00462 
00463   
00464   /* start reserving memory for fstats-file contents */
00465   if (numlines > 0) 
00466     { 
00467       *CList = (CandidateList *)LALMalloc (sizelist*sizeof(CandidateList));
00468       if ( !CList ) 
00469         { 
00470           LALPrintError ("Could not allocate memory for candidate file %s\n\n", fname);
00471           ABORT (lalStatus, FIDUCIALC_EMEM, FIDUCIALC_MSGEMEM);
00472         }
00473     }
00474 
00475   /* ------ Open and count candidates file ------ */
00476   i=0;
00477   if(strcmp(fname,"-")==0){
00478     fp=stdin;
00479   }
00480   else {
00481     fp=fopen(fname,"rb");
00482     if (fp==NULL) 
00483       {
00484         LALPrintError("fopen(%s) failed!\n", fname);
00485         LALFree ((*CList));
00486         ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00487       }
00488   }
00489 
00490   
00491   jj=0;
00492 
00493   /* printf("Number of lines to read: %d\n", numlines); */
00494     
00495   if(strcmp(fname,"-")==0){
00496     while(i < numlines && gets(line1) )
00497       {
00498         CHAR newline='\0';
00499         
00500         if (jj >= sizelist) {
00501           sizelist = sizelist + 16384;
00502           CandidateList *tmp;
00503           tmp = (CandidateList *)LALRealloc(*CList, (sizelist * sizeof(CandidateList)) );
00504           if ( !tmp ){
00505             LALPrintError("couldnot re-allocate memory for candidate list \n\n");
00506             ABORT (lalStatus, FIDUCIALC_EMEM, FIDUCIALC_MSGEMEM);
00507           }
00508           *CList = tmp;
00509         }
00510         
00511         CandidateList *cl=&(*CList)[jj];
00512         
00513         nread = sscanf (line1,
00514                         "%s %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT, 
00515                         &(cl->resultfname), &(cl->f), &(cl->Alpha), &(cl->Delta), &(cl->F1dot), &(cl->TwoF) );
00516 
00517         if(cl->TwoF >= CLA->ThrTwoF) {
00518           /* check that values that are read in are sensible, 
00519              (result file names will be checked later, when getting 
00520              the search parameters from Einstein at Home  setup library) */
00521           if (
00522               cl->f < 0.0                        ||
00523               cl->TwoF < 0.0                     ||
00524               cl->Alpha <         0.0 - epsilon  ||
00525               cl->Alpha >   LAL_TWOPI + epsilon  ||
00526               cl->Delta < -0.5*LAL_PI - epsilon  ||
00527               cl->Delta >  0.5*LAL_PI + epsilon  ||
00528               !finite(cl->FileID)                ||                                                                 
00529               !finite(cl->f)                     ||
00530               !finite(cl->Alpha)                 ||
00531               !finite(cl->Delta)                 ||
00532               !finite(cl->F1dot)                 ||
00533               !finite(cl->TwoF)
00534               ) {
00535             LALPrintError(
00536                           "Line %d of file %s has invalid values.\n"
00537                           "First 255 chars are:\n"
00538                           "%s\n"
00539                           "1st and 4th field should be positive.\n" 
00540                           "2nd field should lie between 0 and %1.15f.\n" 
00541                           "3rd field should lie between %1.15f and %1.15f.\n"
00542                           "All fields should be finite\n",
00543                           i+1, fname, line1, (double)LAL_TWOPI, (double)-LAL_PI/2.0, (double)LAL_PI/2.0);
00544             LALFree ((*CList));
00545             fclose(fp);
00546             ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00547           }
00548           
00549           
00550 
00551           /* check that we read 6 quantities with exactly the right format */
00552           if ( nread != 6 )
00553             {
00554               LALPrintError ("Found %d not %d values on line %d in file '%s'\n"
00555                              "Line in question is\n%s",
00556                              nread, 6, i+1, fname, line1);               
00557               LALFree ((*CList));
00558               ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00559             }
00560           
00561           jj++;
00562           
00563         } /* if(cl->TwoF >= CLA->ThrTwoF) */
00564         
00565         i++;
00566       } /*  end of main while loop */
00567   }
00568   else{
00569     while(i < numlines && fgets(line1,sizeof(line1),fp) )
00570       {
00571         CHAR newline='\0';
00572         
00573         if (jj >= sizelist) {
00574           sizelist = sizelist + 16384;
00575           CandidateList *tmp;
00576           tmp = (CandidateList *)LALRealloc(*CList, (sizelist * sizeof(CandidateList)) );
00577           if ( !tmp ){
00578             LALPrintError("couldnot re-allocate memory for candidate list \n\n");
00579             ABORT (lalStatus, FIDUCIALC_EMEM, FIDUCIALC_MSGEMEM);
00580           }
00581           *CList = tmp;
00582         }
00583         
00584         CandidateList *cl=&(*CList)[jj];
00585         
00586         if (strlen(line1)==0 || line1[strlen(line1)-1] != '\n') {
00587           LALPrintError(
00588                         "Line %d of file %s is too long or has no NEWLINE.  First 255 chars are:\n%s\n",
00589                         i+1, fname, line1);
00590           LALFree ((*CList));
00591           fclose(fp);
00592           ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00593         }
00594         
00595         nread = sscanf (line1,
00596                         "%s %" LAL_INT4_FORMAT " %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT " %" 
00597                         LAL_REAL8_FORMAT " %" LAL_REAL8_FORMAT "%c", 
00598                         &(cl->resultfname), &(cl->FileID), &(cl->f), &(cl->Alpha), &(cl->Delta), &(cl->F1dot), &(cl->TwoF), &newline );
00599         if(cl->TwoF >= CLA->ThrTwoF) {
00600           /* check that values that are read in are sensible, 
00601              (result file names will be checked later, when getting 
00602              the search parameters from Einstein at Home  setup library) */
00603           if (
00604               cl->FileID < 0                     ||
00605               cl->f < 0.0                        ||
00606               cl->TwoF < 0.0                     ||
00607               cl->Alpha <         0.0 - epsilon  ||
00608               cl->Alpha >   LAL_TWOPI + epsilon  ||
00609               cl->Delta < -0.5*LAL_PI - epsilon  ||
00610               cl->Delta >  0.5*LAL_PI + epsilon  ||
00611               !finite(cl->FileID)                ||                                                                 
00612               !finite(cl->f)                     ||
00613               !finite(cl->Alpha)                 ||
00614               !finite(cl->Delta)                 ||
00615               !finite(cl->F1dot)                 ||
00616               !finite(cl->TwoF)
00617               ) {
00618             LALPrintError(
00619                           "Line %d of file %s has invalid values.\n"
00620                           "First 255 chars are:\n"
00621                           "%s\n"
00622                           "1st and 4th field should be positive.\n" 
00623                           "2nd field should lie between 0 and %1.15f.\n" 
00624                           "3rd field should lie between %1.15f and %1.15f.\n"
00625                           "All fields should be finite\n",
00626                           i+1, fname, line1, (double)LAL_TWOPI, (double)-LAL_PI/2.0, (double)LAL_PI/2.0);
00627             LALFree ((*CList));
00628             fclose(fp);
00629             ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00630           }
00631           
00632           
00633 
00634           /* check that the FIRST character following the Fstat value is a
00635              newline.  Note deliberate LACK OF WHITE SPACE char before %c
00636              above */
00637           if (newline != '\n') {
00638             LALPrintError(
00639                           "Line %d of file %s had extra chars after F value and before newline.\n"
00640                           "First 255 chars are:\n"
00641                           "%s\n",
00642                           i+1, fname, line1);
00643             LALFree ((*CList));
00644             fclose(fp);
00645             ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00646           }
00647           
00648           /* check that we read 8 quantities with exactly the right format */
00649           if ( nread != 8 )
00650             {
00651               LALPrintError ("Found %d not %d values on line %d in file '%s'\n"
00652                              "Line in question is\n%s",
00653                              nread, 8, i+1, fname, line1);               
00654               LALFree ((*CList));
00655               fclose(fp);
00656               ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00657             }
00658           
00659           jj++;
00660           
00661         } /* if(cl->TwoF >= CLA->ThrTwoF) */
00662         
00663         i++;
00664       } /*  end of main while loop */
00665   }
00666 
00667 
00668   *candlen=jj-1;
00669 
00670   /* check that we read ALL lines! */
00671   if (i != numlines) {
00672     LALPrintError(
00673             "Reading of file %s terminated after %d line but numlines=%d\n",
00674             fname, i, numlines);
00675     LALFree((*CList));
00676     fclose(fp);
00677     ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00678   }
00679 
00680   /* read final line with %DONE\n marker */
00681   if (!fgets(line1, sizeof(line1), fp)) {
00682     LALPrintError(
00683             "Failed to find marker line of file %s\n",
00684             fname);
00685     LALFree((*CList));
00686     fclose(fp);
00687     ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00688   }
00689 
00690   /* check for %DONE\n marker */
00691   if (strcmp(line1, DONE_MARKER)) {
00692     LALPrintError(
00693             "Failed to parse marker: 'final' line of file %s contained %s not %s",
00694             fname, line1, DONE_MARKER);
00695     LALFree ((*CList));
00696     fclose(fp);
00697     ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00698   }
00699 
00700   /* check that we are now at the end-of-file */
00701   if (fgetc(fp) != EOF) {
00702     LALPrintError(
00703             "File %s did not terminate after %s",
00704             fname, DONE_MARKER);
00705     LALFree ((*CList));
00706     fclose(fp);
00707     ABORT (lalStatus, FIDUCIALC_EINVALIDFSTATS, FIDUCIALC_MSGEINVALIDFSTATS);
00708   }
00709 
00710   /* -- close candidate file -- */
00711   fclose(fp);     
00712 
00713   DETATCHSTATUSPTR (lalStatus);
00714   RETURN (lalStatus);
00715 
00716 } /* ReadCombinedFile() */
00717 
00718 
00719 
00720 
00721 /* ########################################################################################## */
00722 
00723 void ReadCommandLineArgs( LALStatus *lalStatus, 
00724                      INT4 argc, 
00725                      CHAR *argv[], 
00726                      FiducialTimeConfigVars *CLA ) 
00727 {
00728 
00729   CHAR* uvar_InputData;
00730   CHAR* uvar_OutputData;
00731   BOOLEAN uvar_help;
00732   REAL8 uvar_ThrTwoF;
00733   INT4 uvar_InNumLines;
00734   INT4 uvar_FiducialTime;
00735 
00736   INITSTATUS( lalStatus, "ReadCommandLineArgs", rcsid );
00737   ATTATCHSTATUSPTR (lalStatus);
00738 
00739   ASSERT( CLA != NULL, lalStatus, FIDUCIALC_ENULL, FIDUCIALC_MSGENULL);
00740 
00741   uvar_help = 0;
00742   uvar_InputData = NULL;
00743   uvar_OutputData = NULL;
00744   uvar_ThrTwoF = -1.0;
00745   uvar_FiducialTime = FIXED_FIDUCIAL_TIME;
00746   
00747   /* register all our user-variables */
00748   LALregBOOLUserVar(lalStatus,       help,           'h', UVAR_HELP,     "Print this message"); 
00749   
00750   LALregINTUserVar(lalStatus,        FiducialTime,   'f', UVAR_OPTIONAL, "Fiducial GPS time");
00751   LALregSTRINGUserVar(lalStatus,     OutputData,     'o', UVAR_OPTIONAL, "Ouput file name");
00752   LALregSTRINGUserVar(lalStatus,     InputData,      'i', UVAR_OPTIONAL, "Input file name");
00753   LALregREALUserVar(lalStatus,       ThrTwoF,        't', UVAR_OPTIONAL, "Threshold on values of 2F");
00754   LALregINTUserVar(lalStatus,        InNumLines,     'l', UVAR_OPTIONAL, "Number of lines of input");
00755 
00756 
00757   TRY (LALUserVarReadAllInput(lalStatus->statusPtr,argc,argv),lalStatus); 
00758 
00759 
00760   if (uvar_help) {      /* if help was requested, we're done here */
00761     LALPrintError("%s\n",rcsid);
00762     fflush(stderr);
00763     LALDestroyUserVars(lalStatus->statusPtr);
00764     exit(FIDUCIAL_EXIT_OK);
00765   }
00766 
00767   CLA->FiducialTime = 0;
00768   CLA->OutputFile = NULL;
00769   CLA->InputFile = NULL;
00770   CLA->ThrTwoF = -1.0;
00771   CLA->InNumLines = 0;
00772 
00773 
00774   if(uvar_OutputData != ""){
00775     if(uvar_OutputData !="-"){
00776       CLA->OutputFile = (CHAR *) LALMalloc(strlen(uvar_OutputData)+1);
00777       if(CLA->OutputFile == NULL) {
00778         TRY( FreeConfigVars( lalStatus->statusPtr, CLA ), lalStatus);
00779         LALPrintError("Output file '%s' is incorrect. \n\n",uvar_OutputData);
00780         exit(FIDUCIAL_EXIT_ERR);
00781       }
00782     }
00783     strcpy(CLA->OutputFile,uvar_OutputData);
00784   }
00785   
00786   else 
00787     { 
00788       TRY( FreeConfigVars( lalStatus->statusPtr, CLA ), lalStatus);
00789       LALPrintError("Output file '%s' is incorrect. \n\n",uvar_OutputData);
00790       exit(FIDUCIAL_EXIT_ERR);
00791     }
00792 
00793   
00794   if(uvar_InputData != ""){
00795     if(uvar_InputData !="-"){
00796       CLA->InputFile = (CHAR *) LALMalloc(strlen(uvar_InputData)+1);
00797       if(CLA->InputFile == NULL){
00798         TRY( FreeConfigVars( lalStatus->statusPtr, CLA ), lalStatus);
00799         LALPrintError("Input file '%s' is incorrect. \n\n",uvar_InputData);
00800         exit(FIDUCIAL_EXIT_ERR);
00801       }
00802     }
00803     strcpy(CLA->InputFile,uvar_InputData);
00804   }
00805   else
00806     {
00807       TRY( FreeConfigVars( lalStatus->statusPtr, CLA ), lalStatus);
00808       LALPrintError("Input file '%s' is incorrect. \n\n",uvar_InputData);
00809       exit(FIDUCIAL_EXIT_ERR);
00810     }
00811 
00812   if(uvar_FiducialTime > 0){
00813     CLA->FiducialTime = uvar_FiducialTime;
00814   }
00815   else    {
00816     TRY( FreeConfigVars( lalStatus->statusPtr, CLA ), lalStatus);
00817     LALPrintError("The fiducial GPS time %d is incorrect. \n\n",uvar_FiducialTime);
00818     exit(FIDUCIAL_EXIT_ERR);
00819   }
00820   CLA->InNumLines = uvar_InNumLines;
00821   CLA->ThrTwoF = uvar_ThrTwoF;
00822 
00823   LALDestroyUserVars(lalStatus->statusPtr);
00824   BEGINFAIL(lalStatus) {
00825     LALFree(CLA->InputFile);
00826     LALFree(CLA->OutputFile);
00827   } ENDFAIL(lalStatus);
00828 
00829   DETATCHSTATUSPTR (lalStatus);
00830   RETURN (lalStatus);
00831 } /* void ReadCommandLineArgs()  */
00832 
00833 
00834 
00835 /* ########################################################################################## */
00836 
00837 
00838 /*----------------------------------------------------- */
00839 void ComputeFiducialTimeFrequency( LALStatus *lalStatus,
00840                                    FiducialTimeConfigVars *CLA,
00841                                    CandidateList *CList, 
00842                                    INT4 candlen)
00843 {
00844   REAL8 f_CFS;
00845   REAL8 F1dot_CFS;
00846   REAL8 f_fiducial;
00847   REAL8 deltaT;
00848   INT4 iindex;
00849   INT4 segtmp;
00850   WU_search_params_t wparams;
00851 
00852 
00853   INITSTATUS( lalStatus, "ComputeFiducialTimeFrequency", rcsid );
00854   ATTATCHSTATUSPTR (lalStatus);
00855 
00856   f_CFS=0;
00857   f_fiducial=0;
00858   F1dot_CFS=0;
00859   iindex=0;
00860   deltaT=0;
00861   
00862   /* Find data segment from resultname */
00863   findSearchParams4Result( CList[0].resultfname, &wparams );
00864 
00865   /* Compute Data-StretchID */
00866   segtmp = (INT4)(wparams.endTime / (wparams.endTime - wparams.startTime));
00867   
00868   switch( segtmp )
00869     {
00870       /* H1 ----------- */
00871 
00872     case 6537:
00873       segtmp=0;
00874       break;
00875       
00876     case 6497:
00877       segtmp=1;
00878       break;
00879       
00880     case 5828:
00881       segtmp=2;
00882       break;
00883       
00884     case 6120:
00885       segtmp=3;
00886       break;
00887       
00888     case 5955:
00889       segtmp=4;
00890       break;
00891       
00892     case 5613:
00893       segtmp=5;
00894       break;
00895 
00896     case 6126:
00897       segtmp=6;
00898       break;
00899       
00900     case 5946:
00901       segtmp=7;
00902       break;
00903       
00904     case 6130:
00905       segtmp=8;
00906       break;
00907 
00908     case 5515:
00909       segtmp=9;
00910       break;
00911 
00912       /* L1 ----------- */
00913       
00914     case 6341:
00915       segtmp=10;
00916       break;
00917 
00918     case 6102:
00919       segtmp=11;
00920       break;
00921 
00922     case 5813:
00923       segtmp=12;
00924       break;
00925 
00926     case 5783:
00927       segtmp=13;
00928       break;
00929 
00930     case 5538:
00931       segtmp=14;
00932       break;
00933 
00934     case 6514:
00935       segtmp=15;
00936       break;
00937 
00938     case 5653:
00939       segtmp=16;
00940       break;
00941 
00942     }
00943   
00944   while( iindex < candlen )
00945     {
00946       f_CFS = CList[iindex].f;
00947       F1dot_CFS = CList[iindex].F1dot;
00948       
00949       /* Get search parameters from Einstein at Home setup library */
00950       findSearchParams4Result( CList[iindex].resultfname, &wparams );
00951 
00952       /* Compute Data-StretchID */
00953       CList[iindex].DataStretch = segtmp;
00954       
00955       /* Fixed fiducial time = e.g. GPS time of first SFT in S4 */
00956       deltaT = wparams.startTime - CLA->FiducialTime;
00957 
00958       /* Compute new frequency values at fixed fiducial time */
00959       f_fiducial = f_CFS - (F1dot_CFS * deltaT);
00960      
00961       /* Replace f values by the new ones, that all refer to the same */
00962       CList[iindex].f = f_fiducial;
00963  
00964       f_CFS = 0;
00965       f_fiducial = 0;
00966       F1dot_CFS = 0;
00967       deltaT=0;
00968 
00969       iindex++;
00970 
00971     } /* while( iindex < candlen)  */
00972