Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
create_pulsar_signal_frame.c
Go to the documentation of this file.
1/*
2 * create_pulsar_signal_frame.c: GW pulsar injection code
3 *
4 * Copyright (C) 2012 Erin Macdonald, Matthew Pitkin
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with with program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22#include "config.h"
23
24#ifndef _GNU_SOURCE
25#define _GNU_SOURCE /* for alphasort() and scandir() */
26#endif
27#include <dirent.h>
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <strings.h>
33#include <errno.h>
34#include <stdarg.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <time.h>
38#include <signal.h>
39#include <math.h>
40#include <sys/stat.h>
41
42#include <lal/Units.h>
43#include <lal/LALgetopt.h>
44#include <lal/LALFrStream.h>
45#include <lal/LALFrameIO.h>
46#include <lal/LALCache.h>
47#include <lal/TimeSeries.h>
48#include <lal/LALStdlib.h>
49#include <lal/AVFactories.h>
50#include <lal/UserInput.h>
51#include <lal/GeneratePulsarSignal.h>
52#include <lal/LALInitBarycenter.h>
53#include <lal/LALDatatypes.h>
54#include <lal/DetectorSite.h>
55#include <lal/Date.h>
56#include <lal/SkyCoordinates.h>
57#include <lal/RngMedBias.h>
58#include <lal/LALRunningMedian.h>
59#include <lal/BinaryPulsarTiming.h>
60#include <lal/LogPrintf.h>
61#include <lal/LALString.h>
62#include <lal/LALPulsarVCSInfo.h>
63
64#define STRINGLENGTH 256 /* the length of general string */
65
66/* create a detector at the GEO centre based on Virgo's arms */
67//LALFrDetector FrGEOcentre = {
68// "C1", // the detector name
69// "C1", // detector prefix
70// 0.0, // the vertex longitude (rads)
71// LAL_PI_2, // the vertex latitude (rads)
72// -LAL_BWGS84_SI, // the vertex elevation (m) - set to Earth centre
73// 0.0, // the x-arm altitude (rads)
74// 0.33916285222, // the x-arm azimuth (rads) - same as Virgo
75// 0.0, // y-arm altitude (rads)
76// 5.05155183261, // y-arm azimuth (rads) - same as Virgo
77// 1500.0, // x-arm midpoint (m)
78// 1500.0}; // y-arm midpoint (m)
79
80#define USAGE \
81"Usage: %s [options]\n\n"\
82" --help, -h display this message\n"\
83" --detector, -i the detector for which to create a frame (e.g. H1)\n"\
84" --channel, -c the channel name into which the signals will be added\n"\
85" --epoch, -e (int) the start epoch of the created frame\n"\
86" --geocentre, g set the detector to the geocentre\n"\
87" --duration, -d (int) the duration (in seconds) of the frame\n"\
88" --pulsar-dir, -p the directory containing pulsar (.par) files of signals\n\
89 to be added to the frame\n"\
90" --output-dir, -o the directory in which to output the frame\n"\
91" --output-str, -s a string to start the frame file name\n"\
92" --ephem-dir, -m the directory containing ephemeris files\n"\
93" --ephem-type, -y the ephemeris file type to use (e.g. DE405 [default])\n"\
94" --dbg-lvl, -l (int) the code debug level\n"\
95"\n"
96
97/*function to read ephemeris files*/
98EphemerisData *InitEphemeris( const CHAR *ephemType, const CHAR *ephemDir );
99
100typedef struct tagInputParams {
101 CHAR *ephemDir; /* ephemeris directory */
102 CHAR *ephemType; /* ephemeric year */
103
104 CHAR *pulsarDir; /* directory containing par files */
105
106 CHAR *det; /* detector */
107
108 CHAR *channel; /* channel name */
109
110 CHAR *outDir; /* output directory */
111 CHAR *outStr; /* string for output file name */
112
113 UINT4 frDur; /* duration of a frame */
114 UINT4 epoch; /* start epoch of a frame */
115
116 UINT4 geocentre; /* a flag to set the detector to the geocentre */
118
119void ReadInput( InputParams *inputParams, int argc, char *argv[] );
120
121/*--------------main function---------------*/
122int main( int argc, char **argv )
123{
124 const CHAR *fn = __func__;
125
126 InputParams XLAL_INIT_DECL( inputs );
127
128 REAL8 srate = 16384.0; /*sample rate defaulted to 16384 */
129
130 /* read in command line input args */
131 ReadInput( &inputs, argc, argv );
132
134
136 if ( ( edat = InitEphemeris( inputs.ephemType, inputs.ephemDir ) ) == NULL ) {
137 XLALPrintError( "%s: Failed to init ephemeris data\n", fn );
139 }
140
141 /*init detector info */
142 const LALDetector *p_site;
143 if ( ( p_site = XLALGetSiteInfo( inputs.det ) ) == NULL ) {
144 XLALPrintError( "%s: Failed to get site-info for detector '%s'\n", fn,
145 inputs.det );
147 }
148
149 LALDetector site = *p_site;
150 if ( inputs.geocentre ) { /* set site to the geocentre */
151 site.location[0] = 0.0;
152 site.location[1] = 0.0;
153 site.location[2] = 0.0;
154 }
155
156 struct dirent **pulsars;
157 INT4 n = scandir( inputs.pulsarDir, &pulsars, 0, alphasort );
158 if ( n < 0 ) {
159 XLALPrintError( "scandir failed\n" );
161 }
162
163 UINT4 numpulsars = ( UINT4 )n;
164 UINT4 h = 0;
165
166 CHAR parname[256];
167 PulsarParameters *pulparams[numpulsars];
168
169 for ( h = 2; h < numpulsars; h++ ) {
170 if ( strstr( pulsars[h]->d_name, ".par" ) == NULL ) {
171 free( pulsars[h] );
172 continue;
173 } else {
174 if ( ( int )sizeof( parname ) <= snprintf( parname, sizeof( parname ), "%s/%s", inputs.pulsarDir, pulsars[h]->d_name ) ) {
175 XLAL_ERROR( XLAL_FAILURE, "String truncated" );
176 }
177 fprintf( stderr, "%s\n", parname );
178 FILE *inject;
179
180 if ( ( inject = fopen( parname, "r" ) ) == NULL ) {
181 fprintf( stderr, "Error opening file: %s\n", parname );
183 }
184
185 pulparams[h] = XLALReadTEMPOParFile( parname );
186
187 fclose( inject );
188 }
189 }
191
192 UINT4 ndata;
193
194 epoch.gpsSeconds = inputs.epoch;
195 epoch.gpsNanoSeconds = 0;
196
197 ndata = inputs.frDur;
198
199 REAL8TimeSeries *series = NULL;
200
201 CHAR out_file[256];
202 sprintf( out_file, "%s-%s-%d-%d.gwf", inputs.det, inputs.outStr,
203 epoch.gpsSeconds, ndata );
204
205 LALFrameH *outFrame = NULL;
206
207 if ( ( outFrame = XLALFrameNew( &epoch, ( REAL8 )ndata, inputs.channel, 1, 0,
208 0 ) ) == NULL ) {
209 LogPrintf( LOG_CRITICAL, "%s : XLALFrameNew() filed with error = %d.\n", fn, xlalErrno );
211 }
212
213 if ( ( series = XLALCreateREAL8TimeSeries( inputs.channel, &epoch, 0.,
214 1. / srate, &lalSecondUnit, ( int )( ndata * srate ) ) ) == NULL ) {
216 }
217
218 UINT4 counter = 0;
219 for ( counter = 0; counter < series->data->length; counter++ ) {
220 series->data->data[counter] = 0;
221 }
222
223 /*** Read Pulsar Data ***/
224 for ( h = 0; h < numpulsars; h++ ) {
225 if ( strstr( pulsars[h]->d_name, ".par" ) == NULL ) {
226 free( pulsars[h] );
227 continue;
228 } else {
230
231 /* set signal generation barycenter delay look-up table step size */
232 params.dtDelayBy2 = 10.; /* generate table every 10 seconds */
233
234 if ( ( params.pulsar.spindown = XLALCreateREAL8Vector( 1 ) ) == NULL ) {
235 XLALPrintError( "Out of memory" );
237 }
238
239 INT4 dtpos = 0;
240 if ( PulsarCheckParam( pulparams[h], "POSEPOCH" ) ) {
241 dtpos = epoch.gpsSeconds - ( INT4 )PulsarGetREAL8Param( pulparams[h], "POSEPOCH" );
242 } else {
243 dtpos = epoch.gpsSeconds - ( INT4 )PulsarGetREAL8Param( pulparams[h], "PEPOCH" );
244 }
245
246 REAL8 ra = 0., dec = 0.;
247 if ( PulsarCheckParam( pulparams[h], "RAJ" ) ) {
248 ra = PulsarGetREAL8Param( pulparams[h], "RAJ" );
249 } else if ( PulsarCheckParam( pulparams[h], "RA" ) ) {
250 ra = PulsarGetREAL8Param( pulparams[h], "RA" );
251 } else {
252 XLALPrintError( "No right ascension found" );
254 }
255 if ( PulsarCheckParam( pulparams[h], "DECJ" ) ) {
256 dec = PulsarGetREAL8Param( pulparams[h], "DECJ" );
257 } else if ( PulsarCheckParam( pulparams[h], "DEC" ) ) {
258 dec = PulsarGetREAL8Param( pulparams[h], "DEC" );
259 } else {
260 XLALPrintError( "No declination found" );
262 }
263
264 params.pulsar.position.latitude = dec + ( REAL8 )dtpos * PulsarGetREAL8ParamOrZero( pulparams[h], "PMDEC" );
265 params.pulsar.position.longitude = ra + ( REAL8 )dtpos * PulsarGetREAL8ParamOrZero( pulparams[h], "PMRA" ) / cos( params.pulsar.position.latitude );
266 params.pulsar.position.system = COORDINATESYSTEM_EQUATORIAL;
267
268 const REAL8Vector *fs = PulsarGetREAL8VectorParam( pulparams[h], "F" );
269 if ( fs->length == 0 ) {
270 XLALPrintError( "No frequencies found" );
272 }
273
274 params.pulsar.f0 = 2.*fs->data[0];
275 if ( fs->length > 1 ) {
276 params.pulsar.spindown->data[0] = 2.*fs->data[1];
277 }
278 if ( ( XLALGPSSetREAL8( &( params.pulsar.refTime ), PulsarGetREAL8Param( pulparams[h], "PEPOCH" ) ) ) == NULL ) {
280 }
281 params.pulsar.psi = PulsarGetREAL8ParamOrZero( pulparams[h], "PSI" );
282 params.pulsar.phi0 = PulsarGetREAL8ParamOrZero( pulparams[h], "PHI0" );
283 REAL8 cosiota = PulsarGetREAL8ParamOrZero( pulparams[h], "COSIOTA" );
284 REAL8 h0 = PulsarGetREAL8ParamOrZero( pulparams[h], "H0" );
285 params.pulsar.aPlus = 0.5 * h0 * ( 1. + cosiota * cosiota );
286 params.pulsar.aCross = h0 * cosiota;
287
288 /*Add binary later if needed!*/
289
290 params.site = &site;
291 params.ephemerides = edat;
292 params.startTimeGPS = epoch;
293 params.duration = ndata;
294 params.samplingRate = srate;
295 params.fHeterodyne = 0.;
296
297 REAL4TimeSeries *TSeries = NULL;
298
299 LALGeneratePulsarSignal( &status, &TSeries, &params );
300
301 if ( status.statusCode ) {
302 fprintf( stderr, "LAL Routine failed!\n" );
304 }
305 UINT4 i;
306 for ( i = 0; i < TSeries->data->length; i++ ) {
307 series->data->data[i] += TSeries->data->data[i];
308 }
309
311 XLALDestroyREAL8Vector( params.pulsar.spindown );
312 }
313 }
314
315 if ( XLALFrameAddREAL8TimeSeriesProcData( outFrame, series ) ) {
316 LogPrintf( LOG_CRITICAL, "%s : XLALFrameAddREAL8TimeSeries() failed with error = %d.\n", fn, xlalErrno );
318 }
319
320 CHAR OUTFILE[512];
321 snprintf( OUTFILE, sizeof( OUTFILE ), "%s/%s", inputs.outDir, out_file );
322
323 if ( XLALFrameWrite( outFrame, OUTFILE ) ) {
324 LogPrintf( LOG_CRITICAL, "%s : XLALFrameWrite() failed with error = %d.\n", fn, xlalErrno );
326 }
327
328 XLALFrameFree( outFrame );
330
331 return 0;
332}
333
334EphemerisData *InitEphemeris( const CHAR *ephemType, const CHAR *ephemDir )
335{
336 const CHAR *fn = __func__;
337#define FNAME_LENGTH 1024
338 CHAR EphemEarth[FNAME_LENGTH]; /* filename of earth-ephemeris data */
339 CHAR EphemSun[FNAME_LENGTH]; /* filename of sun-ephemeris data */
340
341 /* check input consistency */
342 if ( !ephemType ) {
343 XLALPrintError( "%s: invalid NULL input for 'ephemType'\n", fn );
345 }
346
347 snprintf( EphemEarth, FNAME_LENGTH, "%s/earth00-40-%s.dat.gz", ephemDir, ephemType );
348 snprintf( EphemSun, FNAME_LENGTH, "%s/sun00-40-%s.dat.gz", ephemDir, ephemType );
349
350 EphemEarth[FNAME_LENGTH - 1] = 0;
351 EphemSun[FNAME_LENGTH - 1] = 0;
352
354 if ( ( edat = XLALInitBarycenter( EphemEarth, EphemSun ) ) == NULL ) {
355 XLALPrintError( "%s: XLALInitBarycenter() failed.\n", fn );
357 }
358
359 /* return ephemeris */
360 return edat;
361} /* InitEphemeris() */
362
363void ReadInput( InputParams *inputParams, int argc, char *argv[] )
364{
365 struct LALoption long_options[] = {
366 { "help", no_argument, 0, 'h' },
367 { "detector", required_argument, 0, 'i' },
368 { "channel", required_argument, 0, 'c' },
369 { "epoch", required_argument, 0, 'e' },
370 { "geocentre", no_argument, 0, 'g' },
371 { "duration", required_argument, 0, 'd' },
372 { "pulsar-dir", required_argument, 0, 'p' },
373 { "output-dir", required_argument, 0, 'o' },
374 { "output-str", required_argument, 0, 's' },
375 { "ephem-dir", required_argument, 0, 'm' },
376 { "ephem-type", required_argument, 0, 'y' },
377 { "dbg-lvl", required_argument, 0, 'l' },
378 { 0, 0, 0, 0 }
379 };
380
381 const CHAR *fn = __func__;
382 CHAR args[] = "hi:c:e:gd:p:o:s:m:y:l:";
383 CHAR *program = argv[0];
384
385 /* default the debug level to 1 */
386
387 /* default to no set the detector to the geocentre */
388 inputParams->geocentre = 0;
389
390 /* default ephemeris to use DE405 */
391 inputParams->ephemType = XLALStringDuplicate( "DE405" );
392
393 /* get input arguments */
394 while ( 1 ) {
395 INT4 option_index = 0;
396 INT4 c;
397
398 c = LALgetopt_long( argc, argv, args, long_options, &option_index );
399 if ( c == -1 ) { /* end of options */
400 break;
401 }
402
403 switch ( c ) {
404 case 0: /* if option set a flag, nothing else to do */
405 if ( long_options[option_index].flag ) {
406 break;
407 } else
408 fprintf( stderr, "Error parsing option %s with argument %s\n",
409 long_options[option_index].name, LALoptarg );
410 break;
411 case 'h': /* help message */
412 fprintf( stderr, USAGE, program );
413 exit( 0 );
414 case 'l': /* debug level */
415 break;
416 case 'i': /* interferometer/detector */
417 inputParams->det = XLALStringDuplicate( LALoptarg );
418 break;
419 case 'c': /* channel name */
420 inputParams->channel = XLALStringDuplicate( LALoptarg );
421 break;
422 case 'e': /* frame epoch */
423 inputParams->epoch = atoi( LALoptarg );
424 break;
425 case 'g': /* geocentre flag */
426 inputParams->geocentre = 1;
427 break;
428 case 'd': /* frame duration */
429 inputParams->frDur = atoi( LALoptarg );
430 break;
431 case 'p': /* pulsar par file directory */
432 inputParams->pulsarDir = XLALStringDuplicate( LALoptarg );
433 break;
434 case 'o': /* output directory */
435 inputParams->outDir = XLALStringDuplicate( LALoptarg );
436 break;
437 case 's': /* output name string */
438 inputParams->outStr = XLALStringDuplicate( LALoptarg );
439 break;
440 case 'm': /* ephemeris file directory */
441 inputParams->ephemDir = XLALStringDuplicate( LALoptarg );
442 break;
443 case 'y': /* ephemeris file year */
444 inputParams->ephemType = XLALStringDuplicate( LALoptarg );
445 break;
446 case '?':
447 fprintf( stderr, "unknown error while parsing options\n" );
448 break;
449 default:
450 fprintf( stderr, "unknown error while parsing options\n" );
451 break;
452 }
453 }
454
455 if ( inputParams->epoch == 0 || inputParams->frDur == 0 ) {
456 XLALPrintError( "%s: Frame epoch or duration are 0!\n", fn );
458 }
459}
const char * program
#define __func__
log an I/O error, i.e.
#define c
int LALgetopt_long(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
char * LALoptarg
#define no_argument
#define required_argument
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.
REAL8 PulsarGetREAL8Param(const PulsarParameters *pars, const CHAR *name)
Return a REAL8 parameter.
const char * name
Definition: SearchTiming.c:93
#define fprintf
int main(int argc, char **argv)
#define USAGE
void ReadInput(InputParams *inputParams, int argc, char *argv[])
#define FNAME_LENGTH
EphemerisData * InitEphemeris(const CHAR *ephemType, const CHAR *ephemDir)
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...
double REAL8
#define XLAL_INIT_DECL(var,...)
char CHAR
uint32_t UINT4
int32_t INT4
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
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
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define XLAL_ERROR_VOID(...)
#define XLAL_ERROR_NULL(...)
#define xlalErrno
#define XLAL_ERROR(...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
XLAL_EFUNC
XLAL_EIO
XLAL_EINVAL
XLAL_EFAILED
XLAL_FAILURE
LIGOTimeGPS * XLALGPSSetREAL8(LIGOTimeGPS *epoch, REAL8 t)
n
This structure contains all information about the center-of-mass positions of the Earth and Sun,...
int * flag
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
enum @4 site
enum @1 epoch
double srate