Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-3a66518
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sw_inj_frames.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Erin Macdonald, Matthew Pitkin
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20/* Code to create frames and add them to existing data files
21Input format as $ ./sw_inj_frames framefile duration epoch
22example$ ./lalpulsar_sw_inj_frames -p /Users/erinmacdonald/lsc/analyses/test_par_files -g /Users/erinmacdonald/lsc/analyses/frames -c /Users/erinmacdonald/lsc/analyses/CWINJframes -I H1 -e /Users/erinmacdonald/lsc/src/lscsoft/lalsuite/lalpulsar/test -y 09-11
23*/
24
25/* 2/3/11 v. 2 (which is not entirely accurate, but I'm starting from here): added a log file, version number, and separate channel for i(t) -- E. Macdonald */
26/* 3/3/11 v. 3: placed log comments throughout */
27/* 7/3/11 v. 4: output file to dump ascii of failed frames */
28/* 21/3/11 v. 5: fixed closing .par file L520 */
29/* 9/4/11 v.6: including proper motion calculations */
30/* 12/5/11 v.7: fix memory leak */
31/* 31/5/11 v.8: Matt Pitkin's fixes for the memory leak*/
32/* 23/6/11 v.9; Changed naming of log files to append time, sloppy but works */
33/* 29/6/11 v.10; XLAL functions memory leaks fixed */
34/* 11/7/11 v.11; Outputs surpressed and better log output */
35/* 30/1/12 v.12: Set signal generation barycenter delay lookup table step size */
36/* 27/06/13 v.13: Allow 2nd and 3rd frequency derivatives and make compatible with new frame functions */
37
38#include "config.h"
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <strings.h>
44#include <errno.h>
45#include <stdarg.h>
46#include <sys/types.h>
47#include <sys/wait.h>
48#include <time.h>
49#include <signal.h>
50#include <math.h>
51#include <dirent.h>
52#include <sys/stat.h>
53
54#include <lal/Units.h>
55#include <lal/LALFrStream.h>
56#include <lal/LALFrameIO.h>
57#include <lal/LALCache.h>
58#include <lal/TimeSeries.h>
59#include <lal/LALStdlib.h>
60#include <lal/AVFactories.h>
61#include <lal/UserInput.h>
62#include <lal/GeneratePulsarSignal.h>
63#include <lal/LALInitBarycenter.h>
64#include <lal/LALDatatypes.h>
65#include <lal/DetectorSite.h>
66#include <lal/Date.h>
67#include <lal/SkyCoordinates.h>
68#include <lal/RngMedBias.h>
69#include <lal/LALRunningMedian.h>
70#include <lal/BinaryPulsarTiming.h>
71#include <lal/LogPrintf.h>
72#include <lal/LALString.h>
73#include <lal/LALPulsarVCSInfo.h>
74
75#define STRINGLENGTH 256 /* the length of general string */
76
77/* ---------- local type definitions ---------- */
78typedef struct {
79 /* CHAR *out_chan; Channel output ie. H1_LDAS_C02_L2_CWINJ*/
80 /* CHAR *in_chan; Channel input from .gwf ie. H1:LDAS-STRAIN*/
81 REAL8 srate; /* sample rate -- default 16384*/
82 /* REAL8 duration; duration (sec) Now read from file name */
83 /* REAL8 start; epoch (GPSSeconds) Now read from file name */
84 CHAR *inputdir; /* directory for .par files*/
85 CHAR *gwfdir; /*directory for .gwf files*/
86 CHAR *ephemEarth; /**< Earth ephemeris file to use */
87 CHAR *ephemSun; /**< Sun ephemeris file to use */
88 CHAR *IFO; /*detector */
89 CHAR *logDir; /*directory for the log files */
90 CHAR *outputdir;
92
94
95/* ---------- local function prototypes ---------- */
96int InitUserVars( UserInput_t *uvar, int argc, char **argv ); /*Initiates user variables*/
97
98int XLALFrameFileName( char *fname, size_t size, const char *chname, const LIGOTimeGPS *epoch, double duration );
99
100/* ---------- Function definitions ---------- */
101
102int main( int argc, char **argv )
103{
104 const char *fn = __func__;
105
106 UserInput_t *uvar = &uvar_struct; /*structure for user-defined input variables*/
107
108 /* read all user input */
109 if ( InitUserVars( uvar, argc, argv ) != XLAL_SUCCESS ) {
111 }
112
113 char version[256];
114 sprintf( version, "v13" ); /*manually change */
115
116 /*Get current time for log */
117 time_t result;
118 result = time( NULL );
119 struct tm *btm = localtime( &result );
120
121 /*Start the log file */
122 FILE *logfile;
123 char log_file[256];
124
125 char logfiledir[6];
126 strncpy( logfiledir, ( strrchr( uvar->gwfdir, '/' ) + 1 ), 5 );
127 logfiledir[5] = '\0';
128
129 sprintf( log_file, "%s/%s/injections-%s.log", uvar->outputdir, uvar->logDir, logfiledir );
130 /*fprintf(stderr, "Your log file is here: %s\n", log_file);*/
131
132 /*Writing to .log file*/
133 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
134 fprintf( stderr, "Error opening .log file! \n" );
135 return 0;
136 } else {
137 fprintf( logfile, "\nsw_inj_frames.c version number: %s\nCurrent time: %s \n", version, asctime( btm ) );
138 fprintf( logfile, "User inputs:\n Sample rate: %f\n Pulsar file directory: %s\n Raw frame file directory: %s\n Output directory: %s\n \n Name of directory for log (w/in output dir): %s\n IFO: %s\n", uvar->srate, uvar->inputdir, uvar->gwfdir, uvar->outputdir, uvar->logDir, uvar->IFO );
139 fclose( logfile );
140 }
141 /* </log file> */
142
143 struct dirent **parnamelist;
144 struct dirent **gwfnamelist;
145 char pulin[256];
146
148
149 /*init ephemeris-data */
151 XLAL_CHECK( ( edat = XLALInitBarycenter( uvar->ephemEarth, uvar->ephemSun ) ) != NULL, XLAL_EFUNC );
152
153 /*init detector info */
155 if ( ( site = XLALGetSiteInfo( uvar -> IFO ) ) == NULL ) {
156 XLALPrintError( "%s: Failed to get site-info for detector '%s'\n", fn, uvar->IFO );
158 }
159
160
161 /* if ( (params.pulsar.spindown = XLALCreateREAL8Vector(1))== NULL ) { */
162 /* XLALPrintError("Out of memory"); */
163 /* XLAL_ERROR ( fn, XLAL_EFUNC ); */
164 /* } */
165
166 REAL8 srate; /*sample rate defaulted to 16384 */
167 srate = uvar->srate;
168
169 /*creates the .gwf channels */
170 CHAR in_chan[256];
171 sprintf( in_chan, "%s:LDAS-STRAIN", uvar->IFO );
172 /*fprintf( stderr, "\nin_chan = %s\n", in_chan);*/
173
174 CHAR out_chan[256];
175 sprintf( out_chan, "%s:CWINJ_TOT", uvar->IFO );
176 /*fprintf( stderr, "\nout_chan = %s\n", out_chan);*/
177
178 CHAR inj_chan[256];
179 sprintf( inj_chan, "%s:CWINJ_SIG", uvar->IFO );
180
181 /*Writing to .log file*/
182 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
183 fprintf( stderr, "Error opening .log file! \n" );
184 return 0;
185 } else {
186 fprintf( logfile, "Channels:\n Reading from raw: %s\n Injection+noise: %s\n Pure injection: %s\n", in_chan, out_chan, inj_chan );
187 fclose( logfile );
188 }
189 /* </log file> */
190
191 /* Major loop to run through gwf files */
192 int m;
193 UINT4 i;
194
195 char out_file[256]; /*need a different way to do this */
196 FILE *inject; /*for .par file from uvar->inputdir*/
197
198 CHAR gwf_dir[256]; /* for use with XLALFrStreamOpen */
199 sprintf( gwf_dir, "%s/.", uvar->gwfdir );
200
202 char strepoch[10];
203
204 UINT4 ndata;
205
206 /** Put the pulsar files in the log (so as not to loop every time) **/
207
208 int n = scandir( uvar->inputdir, &parnamelist, 0, alphasort );
209 if ( n < 0 ) {
210 XLALPrintError( "%s: scandir() failed for directory '%s'\n", fn, uvar->inputdir );
212 }
213
214 UINT4 numParFiles = ( UINT4 )n;
215 UINT4 h = 0;
216
217 char parname[256];
218 PulsarParameters *pulparams[numParFiles];
219
220 /*clear . and .. in directory */
221 free( parnamelist[0] );
222 free( parnamelist[1] );
223
224 for ( h = 2; h < numParFiles; h++ ) {
225 if ( strstr( parnamelist[h]->d_name, ".par" ) == NULL ) {
226
227 free( parnamelist[h] );
228
229 continue;
230 } else {
231 /*Writing to .log file*/
232 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
233 fprintf( stderr, "Error opening .log file! \n" );
234 return 0;
235 } else {
236 sprintf( parname, "%s/%s", uvar->inputdir, parnamelist[h]->d_name );
237 char t[ 100 ];
238 struct stat b;
239 if ( !stat( parname, &b ) ) {
240 strftime( t, 100, "%d/%m/%Y %H:%M:%S", localtime( &b.st_mtime ) );
241 fprintf( logfile, "%s/%s Last Modified: %s\n", uvar->inputdir, parnamelist[h]->d_name, t );
242 fclose( logfile );
243 }
244 }
245 /* </log file> */
246
247 /* Save the pulsar parameter files to memory to be called later*/
248 /*sprintf(pulin,"%s/%s", uvar->inputdir, parnamelist[h]->d_name);*/
249 if ( ( inject = fopen( parname, "r" ) ) == NULL ) {
250 fprintf( stderr, "Error opening file: %s\n", parname );
252 }
253
254 pulparams[h] = XLALReadTEMPOParFile( parname );
255
256 fclose( inject );
257 /*fprintf(stderr,"You have now read in %s, saved in index %i", parname, h);*/
258
259 } /*checking if file ends in .par*/
260
261 free( parnamelist[h] );
262
263 }/*looping through files in uvar->inputdir */
264
265 free( parnamelist );
266
267 /** Done logging .par files **/
268
269 if ( ( m = scandir( uvar->gwfdir, &gwfnamelist, 0, alphasort ) ) == -1 ) {
270 XLALPrintError( "%s: scandir('%s',...) failed.\n", fn, uvar->gwfdir );
272 }
273
274 free( gwfnamelist[0] );
275 free( gwfnamelist[1] );
276
277 UINT4 k = 0;
278 for ( k = 2; k < ( UINT4 )m; k++ ) {
279 if ( strstr( gwfnamelist[k]->d_name, ".gwf" ) == NULL ) {
280
281 free( gwfnamelist[k] );
282
283 continue; /*make sure it's a .gwf file */
284 } else {
285 LALFrStream *gwffile = NULL;
286
287 if ( ( gwffile = XLALFrStreamOpen( uvar->gwfdir, gwfnamelist[k]->d_name ) ) == NULL ) {
288 /*XLAL_ERROR ( fn, XLAL_EFUNC ); -- don't want to abort, but save elsewhere test that it's an acceptable file */
289
290 /*Writing to failed file*/
291 FILE *frames;
292 char framefilename[256];
293 sprintf( framefilename, "%s/%s/failed_frames.txt", uvar->outputdir, uvar->logDir );
294 if ( ( frames = fopen( framefilename, "a" ) ) == NULL ) {
295 fprintf( stderr, "Error opening file %s! \n", framefilename );
296 return 0;
297 } else {
298 fprintf( frames, "%s\n", gwfnamelist[k]->d_name );
299 fclose( frames );
300 }
301 /* </failed file> */
302 continue; /*don't exit program if .gwf file fails, continue through*/
303 } else {
304 /*Writing to .log file*/
305 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
306 fprintf( stderr, "Error opening .log file! \n" );
307 return 0;
308 } else {
309 fprintf( logfile, "Read file %s/%s\n", uvar->gwfdir, gwfnamelist[k]->d_name );
310 fclose( logfile );
311 }
312 /* </log file> */
313
314 REAL8TimeSeries *gwfseries = NULL;
315 REAL8TimeSeries *series = NULL;
316
317 /** extract epoch and duration from gwf file name **/
318
319 strncpy( strepoch, strchr( gwfnamelist[k]->d_name, '9' ), 9 ); /*All epochs in S6 begin with 9... potential problem in future */
320 XLAL_LAST_ELEM( strepoch ) = '\0'; /*Null terminate the string*/
321 epoch.gpsSeconds = atoi( strepoch ); /* convert to integer from string */
322 epoch.gpsNanoSeconds = 0; /* no nanosecond precision */
323 /* fprintf(stderr, "epoch = %i\n", epoch.gpsSeconds);*/
324
325 char strdur[4];
326 strncpy( strdur, ( strrchr( gwfnamelist[k]->d_name, '-' ) + 1 ), 3 ); /* duration is last number in frame file */
327 XLAL_LAST_ELEM( strdur ) = '\0';
328 /* assigns duration from .gwf frame */
329 ndata = atoi( strdur );
330
331 /*Writing to .log file*/
332 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
333 fprintf( stderr, "Error opening .log file! \n" );
334 return 0;
335 } else {
336 fprintf( logfile, "Using epoch: %i and duration: %i\n", epoch.gpsSeconds, ndata );
337 fclose( logfile );
338 }
339 /* </log file> */
340
341 /* acquire time series from frame file */
342 if ( ( gwfseries = XLALCreateREAL8TimeSeries( in_chan, &epoch, 0., 1. / srate, &lalSecondUnit, ( int )( ndata * srate ) ) ) == NULL ) {
344 }
345
346 if ( XLALFrStreamGetREAL8TimeSeries( gwfseries, gwffile ) != XLAL_SUCCESS ) {
347 /*Writing to failed file*/
348 FILE *frames;
349 char framefilename[256];
350 sprintf( framefilename, "%s/%s/failed_frames.txt", uvar->outputdir, uvar->logDir );
351 if ( ( frames = fopen( framefilename, "a" ) ) == NULL ) {
352 fprintf( stderr, "Error opening file %s! \n", framefilename );
353 return 0;
354 } else {
355 fprintf( frames, "%s\n", gwfnamelist[k]->d_name );
356 fclose( frames );
357 }
358
359 /* </failed file> */
360 XLALDestroyREAL8TimeSeries( gwfseries );
361 continue; /*don't exit program if .gwf file fails, continue through*/
362 }
363
364 int detflags;
365
366 /* define output .gwf file */
367 detflags = XLALFrameFileName( out_file, sizeof( out_file ), out_chan, &epoch, ndata );
368 //sprintf(out_file, "%c-%s-%d-%d.gwf", uvar->IFO[0], out_chan, epoch.gpsSeconds, ndata);
369
370 /*Writing to .log file*/
371 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
372 fprintf( stderr, "Error opening .log file! \n" );
373 return 0;
374 } else {
375 fprintf( logfile, "Writing to %s/%s\n", uvar->outputdir, out_file );
376 fclose( logfile );
377 }
378 /* </log file> */
379
380 /* read in and test generated frame with XLAL function*/
381
382 LALFrameH *outFrame = NULL; /* frame data structure */
383
384 if ( ( outFrame = XLALFrameNew( &epoch, ( REAL8 )ndata, "CW_INJ", 0, 0, detflags ) ) == NULL ) {
385 LogPrintf( LOG_CRITICAL, "%s : XLALFrameNew() failed with error = %d.\n", fn, xlalErrno );
387 }
388
389 series = NULL;
390 if ( ( series = XLALCreateREAL8TimeSeries( out_chan, &epoch, 0., 1. / srate, &lalSecondUnit, ( int )( ndata * srate ) ) ) == NULL ) {
392 }
393
394 FILE *filetest;
395 CHAR fffile[256];
396 snprintf( fffile, STRINGLENGTH, "%s/%s", uvar->outputdir, out_file );
397
398 if ( ( filetest = fopen( fffile, "w+" ) ) == NULL ) {
399 fprintf( stderr, "fail" );
400 }
401
402 fclose( filetest );
403
404 /*create series to be the sum, general series to add to the noise */
405 REAL8TimeSeries *total_inject = NULL;
406 if ( ( total_inject = XLALCreateREAL8TimeSeries( inj_chan, &epoch, 0., 1. / srate, &lalSecondUnit, ( UINT4 )( ndata * srate ) ) ) == NULL ) {
408 }
409
410 /* need to set all values in total_inject to zero so not pre-allocated */
411 UINT4 counter = 0;
412 for ( counter = 0; counter < total_inject->data->length; counter++ ) {
413 total_inject->data->data[counter] = 0;
414 }
415
416 /*Read in all pulsar files from inputdir*/
417 n = scandir( uvar->inputdir, &parnamelist, 0, alphasort );
418 if ( n < 0 ) {
419 XLALPrintError( "%s: scandir() failed for directory '%s'\n", fn, uvar->inputdir );
421 }
422 numParFiles = ( UINT4 )n;
423 h = 0;
424
425 for ( h = 0; h < numParFiles; h++ ) {
426 if ( strstr( parnamelist[h]->d_name, ".par" ) == NULL ) {
427 /*fprintf(stderr, "Not using file %s\n",parnamelist[h]->d_name);*/
428
429 free( parnamelist[h] );
430
431 continue;
432 } else {
433 /*sprintf(pulin, "%s/%s", uvar->inputdir, parnamelist[h]->d_name);*/
434 /*fprintf(stderr, "This is your pulsar file:%s\n", pulin);*/
435 /*if (( inject = fopen ( pulin, "r" )) == NULL ){*/
436 /*fprintf(stderr, "Error opening file: %s\n", pulin);*/
437 /*XLAL_ERROR ( fn, XLAL_EIO );*/
438 /*}*/
440 /* set signal generation barycenter delay look-up table step size */
441 params.dtDelayBy2 = 10.; /* generate table every 10 seconds */
442
443 /*BinaryPulsarParams pulparams; read from the .par file */
444 if ( ( params.pulsar.spindown = XLALCreateREAL8Vector( 3 ) ) == NULL ) {
445 XLALPrintError( "Out of memory" );
447 }
448
449 const REAL8Vector *fs = PulsarGetREAL8VectorParam( pulparams[h], "F" );
450 fprintf( stderr, "Your f0 is %f\n", fs->data[0] );
451
452 /*Convert location with proper motion */
453 INT4 dtpos = 0;
454 if ( PulsarCheckParam( pulparams[h], "POSEPOCH" ) ) {
455 dtpos = epoch.gpsSeconds - ( INT4 )PulsarGetREAL8Param( pulparams[h], "POSEPOCH" ); /*calculate time since posepoch */
456 } else {
457 dtpos = epoch.gpsSeconds - ( INT4 )PulsarGetREAL8Param( pulparams[h], "PEPOCH" ); /*calculate time since pepoch */
458 }
459
460 REAL8 ra = 0., dec = 0.;
461 if ( PulsarCheckParam( pulparams[h], "RAJ" ) ) {
462 ra = PulsarGetREAL8Param( pulparams[h], "RAJ" );
463 } else if ( PulsarCheckParam( pulparams[h], "RA" ) ) {
464 ra = PulsarGetREAL8Param( pulparams[h], "RA" );
465 } else {
466 XLALPrintError( "No right ascension found" );
468 }
469 if ( PulsarCheckParam( pulparams[h], "DECJ" ) ) {
470 dec = PulsarGetREAL8Param( pulparams[h], "DECJ" );
471 } else if ( PulsarCheckParam( pulparams[h], "DEC" ) ) {
472 dec = PulsarGetREAL8Param( pulparams[h], "DEC" );
473 } else {
474 XLALPrintError( "No declination found" );
476 }
477
478 params.pulsar.position.latitude = dec + ( REAL8 )dtpos * PulsarGetREAL8ParamOrZero( pulparams[h], "PMDEC" );
479 params.pulsar.position.longitude = ra + ( REAL8 )dtpos * PulsarGetREAL8ParamOrZero( pulparams[h], "PMRA" ) / cos( params.pulsar.position.latitude );
480 params.pulsar.position.system = COORDINATESYSTEM_EQUATORIAL;
481
482 params.pulsar.f0 = 2.*fs->data[0];
483 for ( UINT4 kk = 0; kk < 3; kk++ ) {
484 // set frequency derivaties (up to third derivative)
485 if ( fs->length > kk ) {
486 params.pulsar.spindown->data[kk] = 2.*fs->data[kk + 1];
487 } else {
488 params.pulsar.spindown->data[kk] = 0.;
489 }
490 }
491 if ( ( XLALGPSSetREAL8( &( params.pulsar.refTime ), PulsarGetREAL8Param( pulparams[h], "PEPOCH" ) ) ) == NULL ) {
493 }
494 params.pulsar.psi = PulsarGetREAL8ParamOrZero( pulparams[h], "PSI" );
495 params.pulsar.phi0 = PulsarGetREAL8ParamOrZero( pulparams[h], "PHI0" );
496 /*Conversion from h0 and cosi to plus and cross */
497 REAL8 cosiota = PulsarGetREAL8ParamOrZero( pulparams[h], "COSIOTA" );
498 REAL8 h0 = PulsarGetREAL8ParamOrZero( pulparams[h], "H0" );
499 params.pulsar.aPlus = 0.5 * h0 * ( 1. + cosiota * cosiota );
500 params.pulsar.aCross = h0 * cosiota;
501
502 /*if binary */
503 if ( PulsarCheckParam( pulparams[h], "BINARY" ) ) {
504 /*fprintf(stderr, "You have a binary! %s", pulin);*/
505 params.orbit.asini = PulsarGetREAL8ParamOrZero( pulparams[h], "A1" );
506 params.orbit.period = PulsarGetREAL8ParamOrZero( pulparams[h], "PB" ) * 86400;
507
508 /*Taking into account ELL1 model option */
509 if ( strstr( PulsarGetStringParam( pulparams[h], "BINARY" ), "ELL1" ) != NULL ) {
510 REAL8 w, e, eps1, eps2;
511 eps1 = PulsarGetREAL8ParamOrZero( pulparams[h], "EPS1" );
512 eps2 = PulsarGetREAL8ParamOrZero( pulparams[h], "EPS2" );
513 w = atan2( eps1, eps2 );
514 e = sqrt( eps1 * eps1 + eps2 * eps2 );
515 params.orbit.argp = w;
516 params.orbit.ecc = e;
517 } else {
518 params.orbit.argp = PulsarGetREAL8ParamOrZero( pulparams[h], "OM" );
519 params.orbit.ecc = PulsarGetREAL8ParamOrZero( pulparams[h], "ECC" );
520 }
521 if ( strstr( PulsarGetStringParam( pulparams[h], "BINARY" ), "ELL1" ) != NULL ) {
522 REAL8 fe, uasc, Dt, T0val = 0.;
523 fe = sqrt( ( 1.0 - params.orbit.ecc ) / ( 1.0 + params.orbit.ecc ) );
524 uasc = 2.0 * atan( fe * tan( params.orbit.argp / 2.0 ) );
525 Dt = ( params.orbit.period / LAL_TWOPI ) * ( uasc - params.orbit.ecc * sin( uasc ) );
526 T0val = PulsarGetREAL8ParamOrZero( pulparams[h], "TASC" ) + Dt;
527 PulsarAddParam( pulparams[h], "T0", &T0val, PULSARTYPE_REAL8_t );
528 }
529
530 params.orbit.tp.gpsSeconds = ( UINT4 )floor( PulsarGetREAL8ParamOrZero( pulparams[h], "T0" ) );
531 params.orbit.tp.gpsNanoSeconds = ( UINT4 )floor( ( PulsarGetREAL8ParamOrZero( pulparams[h], "T0" ) - params.orbit.tp.gpsSeconds ) * 1e9 );
532 } /* if is a BINARY */
533 else {
534 XLAL_INIT_MEM( params.orbit ); /*if not a binary pulsar*/
535 }
536 params.site = site;
537 params.ephemerides = edat;
538 params.startTimeGPS = epoch;
539 params.duration = ndata; /*maybe want to take out conversion and keep this uvar->duration*/
540 params.samplingRate = srate; /*same as above*/
541 params.fHeterodyne = 0.;
542
543 REAL4TimeSeries *TSeries = NULL;
544 /*fprintf(stderr, "status%i\n", status.statusCode);*/
545 LALGeneratePulsarSignal( &status, &TSeries, &params );
546 if ( status.statusCode ) {
547 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
548 fprintf( stderr, "Error opening .log file! \n" );
549 return 0;
550 } else {
551 fprintf( logfile, "LAL Routine failed at pulsar %s\n", pulin );
553 fclose( logfile );
554 }
555 fprintf( stderr, "LAL Routine failed\n" );
557 }
558 /* add that timeseries to a common one */
559 for ( i = 0; i < TSeries->data->length; i++ ) {
560 total_inject->data->data[i] += TSeries->data->data[i];
561 }
562 /* if ((total_inject = XLALFrameAddREAL8TimeSeriesProcData(frfile, TSeries)) == NULL)*/
563 /*fprintf(stderr, "your pulsar is %s\n", pulin);*/
565 XLALDestroyREAL8Vector( params.pulsar.spindown );
566 /*fclose( inject ); close .par file */
567 } /* for strstr .par */
568
569 free( parnamelist[h] );
570
571 } /*for k<num par files */
572
573 free( parnamelist );
574 /*add to series*/
575 for ( i = 0; i < series->data->length; i++ ) {
576 series->data->data[i] = gwfseries->data->data[i] + total_inject->data->data[i];
577 /*fprintf(stderr, "%le\n", series->data->data[i]);*/
578 }
579
580 if ( XLALFrameAddREAL8TimeSeriesProcData( outFrame, series ) ) {
581 LogPrintf( LOG_CRITICAL, "%s : XLALFrameAddINT4TimeSeries() failed with error = %d.\n", fn, xlalErrno );
583 }
584
585 if ( XLALFrameAddREAL8TimeSeriesProcData( outFrame, total_inject ) ) {
586 LogPrintf( LOG_CRITICAL, "%s : XLALFrameAddREAL8TimeSeriesProcData() failed with eerror = %d.\n", fn, xlalErrno );
588 }
589
590
591 /* write frame structure to file (opens, writes, and closes file) - last argument is compression level */
592 if ( XLALFrameWrite( outFrame, fffile ) ) {
593 LogPrintf( LOG_CRITICAL, "%s : XLALFrameWrite() failed with error = %d.\n", fn, xlalErrno );
595 }
596
597 /*free(fffile);*/
598
599 XLALFrameFree( outFrame );
600
601 XLALDestroyREAL8TimeSeries( gwfseries );
602 XLALDestroyREAL8TimeSeries( total_inject );
604
605 /* Free up the memory */
606
607 /* XLALDestroyREAL8Vector(injsig);*/
608
609 /* XLALDestroyREAL8TimeSeries( gwfseries );*/
610
611 /* XLALDestroyREAL8TimeSeries( series );*/
612
613 } /*ends loop for creating CWINJ .gwf file */
614
615
616 free( gwfnamelist[k] );
617
618 /*Writing to .log file*/
619 if ( ( logfile = fopen( log_file, "a" ) ) == NULL ) {
620 fprintf( stderr, "Error opening .log file! \n" );
621 return 0;
622 } else {
623 fprintf( logfile, "%s created successfully!\n", out_file );
624 fclose( logfile );
625 }
626 /* </log file> */
627 /*fprintf(stderr, "you created %s\n", out_file);*/
628
629 XLALFrStreamClose( gwffile );
630
631 } /*ends loop through all .gwf files in .gwf directory*/
632 }
633
634 free( gwfnamelist );
636
637 LALFree( site );
638
639 fprintf( stderr, "done\n" );
640
641 return 0;
642
643} /* main() */
644
645
646/**
647 * Function to register and read all user input
648 */
649int
650InitUserVars( UserInput_t *uvar, /**< [out] UserInput structure to be filled */
651 int argc, /**< [in] number of argv element */
652 char **argv /**< [in] array of input arguments */
653 )
654{
655 const char *fn = __func__;
656
657 /* check input consistency */
658 if ( !uvar ) {
659 XLALPrintError( "%s: invalid NULL input 'uvar'\n", fn );
661 }
662 if ( !argv ) {
663 XLALPrintError( "%s: invalid NULL input 'argv'\n", fn );
665 }
666
667 /* some defaults */
668 uvar->srate = 16384;
669 uvar->ephemEarth = XLALStringDuplicate( "earth00-40-DE405.dat.gz" );
670 uvar->ephemSun = XLALStringDuplicate( "sun00-40-DE405.dat.gz" );
671
672 /* Register User Variables*/
673 /* XLALRegisterUvarMember(out_chan, STRING, 'o', OPTIONAL, "Output channel i.e. (IFO)_LDAS_C02_L2_CWINJ");*/
674 /* XLALRegisterUvarMember(in_chan, STRING, 'i', OPTIONAL, "Input channel from .gwf file, i.e. (IFO):LDAS-STRAIN");*/
675 XLALRegisterUvarMember( srate, REAL8, 'r', OPTIONAL, "user defined sample rate, default = 16384" );
676 /* XLALRegisterUvarMember(duration, REAL8, 'd', OPTIONAL, "duration of frame (sec)"); */
677 /* XLALRegisterUvarMember(start, REAL8, 's', OPTIONAL, "epoch in GPS Seconds"); */
678 XLALRegisterUvarMember( inputdir, STRING, 'p', OPTIONAL, "directory for .par files" );
679 XLALRegisterUvarMember( gwfdir, STRING, 'g', OPTIONAL, "directory for .gwf files" );
680 XLALRegisterUvarMember( outputdir, STRING, 'c', OPTIONAL, "directory for CWINJ files" );
681 XLALRegisterUvarMember( ephemEarth, STRING, 0, OPTIONAL, "Earth ephemeris file to use" );
682 XLALRegisterUvarMember( ephemSun, STRING, 0, OPTIONAL, "Sun ephemeris file to use" );
683 XLALRegisterUvarMember( IFO, STRING, 'I', REQUIRED, "Detector: 'G1', 'L1', 'H1', 'H2', 'V1'..." );
684 XLALRegisterUvarMember( logDir, STRING, 'L', OPTIONAL, "Directory to put .log file" );
685
686 BOOLEAN should_exit = 0;
688 if ( should_exit ) {
689 exit( 1 );
690 }
691
692 return XLAL_SUCCESS;
693
694} /* InitUserVars() */
695
696/* compare to chars - taken from LALFrameIO.c */
697static int charcmp( const void *c1, const void *c2 )
698{
699 char a = *( const char * )c1;
700 char b = *( const char * )c2;
701 return ( a > b ) - ( a < b );
702}
703
704/* function to set the Frame file name from the input data - taken from LALFrameIO.c */
705int XLALFrameFileName( char *fname, size_t size, const char *chname, const LIGOTimeGPS *epoch, double duration )
706{
707 char site[LAL_NUM_DETECTORS + 1] = "";
708 char *desc;
709 const char *cs;
710 char *s;
711 int detflgs = 0;
712 int t0;
713 int dt;
714
715 /* parse chname to get identified sites and detectors */
716 /* strip out detectors from "XmYn...:"-style prefix */
717 for ( cs = chname; *cs; cs += 2 ) {
718 int d;
719 /* when you get to a colon, you're done! */
720 if ( *cs == ':' ) {
721 break;
722 }
723 /* see if this is an unexpected format */
724 if ( strlen( cs ) <= 2 || !isupper( cs[0] ) || !isdigit( cs[1] ) ) {
725 /* parse error so reset detflgs and site */
726 detflgs = 0;
727 site[0] = 0;
728 break;
729 }
730 /* try to find this detector */
731 for ( d = 0; d < LAL_NUM_DETECTORS; ++d )
732 if ( 0 == strncmp( cs, lalCachedDetectors[d].frDetector.prefix, 2 ) ) {
733 /* found it: put it in sites and detflgs */
734 detflgs |= 1 << 2 * d;
735 strncat( site, cs, 1 );
736 }
737 }
738
739 /* sort and uniqify sites */
740 qsort( site, strlen( site ), 1, charcmp );
741 cs = s = site;
742 for ( cs = s = site; *s; ++cs )
743 if ( *s != *cs ) {
744 *++s = *cs;
745 }
746
747 /* description is a modified version of chname */
748 /* replace invalid description char with '_' */
749 desc = XLALStringDuplicate( chname );
750 for ( s = desc; *s; ++s )
751 if ( !isalnum( *s ) ) {
752 *s = '_';
753 }
754
755 /* determine start time field and duration field */
756 t0 = epoch->gpsSeconds;
757 dt = ( int )ceil( XLALGPSGetREAL8( epoch ) + duration ) - t0;
758
759 /* now format the file name */
760 snprintf( fname, size, "%s-%s-%d-%d.gwf", *site ? site : "X", desc, t0,
761 dt );
762
763 LALFree( desc );
764 return detflgs;
765}
#define __func__
log an I/O error, i.e.
#define IFO
int k
#define LALFree(p)
const LALVCSInfoList lalPulsarVCSInfoList
NULL-terminated list of VCS and build information for LALPulsar and its dependencies
const double c1
const double c2
#define STRING(a)
const REAL8Vector * PulsarGetREAL8VectorParam(const PulsarParameters *pars, const CHAR *name)
Return a REAL8Vector parameter.
REAL8 PulsarGetREAL8ParamOrZero(const PulsarParameters *pars, const CHAR *name)
Return a REAL8 parameter if it exists, otherwise return zero.
int PulsarCheckParam(const PulsarParameters *pars, const CHAR *name)
Check for the existence of the parameter name in the PulsarParameters structure.
PulsarParameters * XLALReadTEMPOParFile(const CHAR *pulsarAndPath)
Read in the parameters from a TEMPO(2) parameter file into a PulsarParameters structure.
void PulsarAddParam(PulsarParameters *pars, const CHAR *name, void *value, PulsarParamType type)
Add a parameter and value to the PulsarParameters structure.
REAL8 PulsarGetREAL8Param(const PulsarParameters *pars, const CHAR *name)
Return a REAL8 parameter.
const CHAR * PulsarGetStringParam(const PulsarParameters *pars, const CHAR *name)
Return a string parameter.
@ PULSARTYPE_REAL8_t
#define fprintf
double e
const double w
const LALDetector lalCachedDetectors[LAL_NUM_DETECTORS]
void LALGeneratePulsarSignal(LALStatus *status, REAL4TimeSeries **signalvec, const PulsarSignalParams *params)
EphemerisData * XLALInitBarycenter(const CHAR *earthEphemerisFile, const CHAR *sunEphemerisFile)
XLAL interface to reading ephemeris files 'earth' and 'sun', and return ephemeris-data in old backwar...
void XLALDestroyEphemerisData(EphemerisData *edat)
Destructor for EphemerisData struct, NULL robust.
#define LAL_TWOPI
unsigned char BOOLEAN
#define XLAL_INIT_MEM(x)
double REAL8
#define XLAL_LAST_ELEM(x)
#define XLAL_INIT_DECL(var,...)
char CHAR
uint32_t UINT4
int32_t INT4
LAL_NUM_DETECTORS
int XLALFrStreamClose(LALFrStream *stream)
LALFrStream * XLALFrStreamOpen(const char *dirname, const char *pattern)
int XLALFrStreamGetREAL8TimeSeries(REAL8TimeSeries *series, LALFrStream *stream)
LALFrameUFrameH LALFrameH
int XLALFrameWrite(LALFrameH *frame, const char *fname)
LALFrameH * XLALFrameNew(const LIGOTimeGPS *epoch, double duration, const char *project, int run, int frnum, INT8 detectorFlags)
void XLALFrameFree(LALFrameH *frame)
int XLALFrameAddREAL8TimeSeriesProcData(LALFrameH *frame, const REAL8TimeSeries *series)
char char * XLALStringDuplicate(const char *s)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
LOG_CRITICAL
static const INT4 m
static const INT4 a
const LALDetector * XLALGetSiteInfo(const CHAR *name)
Find the site geometry-information 'LALDetector' for given a detector name (or prefix).
Definition: SFTnaming.c:218
COORDINATESYSTEM_EQUATORIAL
void XLALDestroyREAL8TimeSeries(REAL8TimeSeries *series)
void XLALDestroyREAL4TimeSeries(REAL4TimeSeries *series)
REAL8TimeSeries * XLALCreateREAL8TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
const LALUnit lalSecondUnit
int XLALUserVarReadAllInput(BOOLEAN *should_exit, int argc, char *argv[], const LALVCSInfoList vcs_list)
#define XLALRegisterUvarMember(name, type, option, category,...)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define xlalErrno
#define XLAL_ERROR(...)
#define XLAL_CHECK(assertion,...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
XLAL_SUCCESS
XLAL_EFUNC
XLAL_EIO
XLAL_EINVAL
XLAL_EFAILED
LIGOTimeGPS * XLALGPSSetREAL8(LIGOTimeGPS *epoch, REAL8 t)
REAL8 XLALGPSGetREAL8(const LIGOTimeGPS *epoch)
size
n
desc
double t0
This structure contains all information about the center-of-mass positions of the Earth and Sun,...
The PulsarParameters structure to contain a set of pulsar parameters.
Input parameters to GeneratePulsarSignal(), defining the source and the time-series.
REAL4Sequence * data
REAL4 * data
REAL8Sequence * data
REAL8 * data
double duration
double psi
UserInput_t uvar_struct
Definition: sw_inj_frames.c:93
int XLALFrameFileName(char *fname, size_t size, const char *chname, const LIGOTimeGPS *epoch, double duration)
int main(int argc, char **argv)
#define STRINGLENGTH
Definition: sw_inj_frames.c:75
int InitUserVars(UserInput_t *uvar, int argc, char **argv)
Function to register and read all user input.
static int charcmp(const void *c1, const void *c2)
enum @4 site
enum @1 epoch
double srate