LALInspiral 5.0.3.1-eeff03c
NRWaveIO.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006 S.Fairhurst, B. Krishnan, L.Santamaria
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/**
21 * \file NRWaveIO.c
22 * \ingroup NRWaveIO_h
23 * \author S.Fairhurst, B.Krishnan, L.Santamaria
24 *
25 * \brief Functions for reading/writing numerical relativity waveforms
26 *
27 */
28
29#include <lal/LALStdio.h>
30#include <lal/FileIO.h>
31#include <lal/NRWaveIO.h>
32#include <lal/NRWaveInject.h>
33
34/**
35 * Functionfor reading the numrel waveform -- just returns the numrel
36 * data as it is without any rescaling of time or amplitude
37 */
38
39void LALReadNRWave_raw(LALStatus *status, /**< pointer to LALStatus structure */
40 REAL4TimeVectorSeries **out, /**< [out] output time series for h+ and hx */
41 const CHAR *filename /**< [in] File containing numrel waveform */)
42{
43
44 UINT4 length, k, r;
45 REAL4TimeVectorSeries *ret=NULL;
47 REAL4Vector *timeVec=NULL;
48 LALParsedDataFile *cfgdata=NULL;
49 REAL4 tmp1, tmp2, tmp3;
50
53
54 /* some consistency checks */
55 ASSERT (filename != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
56 ASSERT ( out != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
57 ASSERT ( *out == NULL, status, NRWAVEIO_ENONULL, NRWAVEIO_MSGENONULL );
58
59 if ( XLALParseDataFile ( &cfgdata, filename) != XLAL_SUCCESS ) {
60 ABORT( status, NRWAVEIO_EFILE, NRWAVEIO_MSGEFILE );
61 }
62 length = cfgdata->lines->nTokens; /*number of data points */
63
64
65 /* allocate memory */
66 ret = LALCalloc(1, sizeof(*ret));
67 if (!ret) {
68 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
69 }
70 strcpy(ret->name,filename);
71 ret->f0 = 0;
72
74 if (!data) {
75 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
76 }
77
78 timeVec = XLALCreateREAL4Vector (length);
79 if (!timeVec) {
80 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
81 }
82
83 /* now get the data */
84 for (k = 0; k < length; k++) {
85 r = sscanf(cfgdata->lines->tokens[k], "%f%f%f", &tmp1, &tmp2, &tmp3);
86
87 /* Check the data file format */
88 if ( r != 3) {
89 /* there must be exactly 3 data entries -- time, h+, hx */
90 ABORT( status, NRWAVEIO_EFORMAT, NRWAVEIO_MSGEFORMAT );
91 }
92
93 timeVec->data[k] = tmp1;
94 data->data[k] = tmp2;
95 data->data[data->vectorLength + k] = tmp3;
96
97 }
98
99 /* scale time */
100 ret->deltaT = timeVec->data[1] - timeVec->data[0];
101
102 /* might also want to go through timeVec to make sure it is evenly spaced */
103
104
105 ret->data = data;
106 (*out) = ret;
107
108 XLALDestroyREAL4Vector (timeVec);
109 XLALDestroyParsedDataFile ( cfgdata);
110
112 RETURN(status);
113
114} /* LALReadNRWave() */
115
116
117
118void LALReadNRWave_raw_real8(LALStatus *status, /**< pointer to LALStatus structure */
119 REAL8TimeVectorSeries **out, /**< [out] output time series for h+ and hx */
120 const CHAR *filename /**< [in] File containing numrel waveform */)
121{
122
123 UINT4 length, k, r;
124 REAL8TimeVectorSeries *ret=NULL;
126 REAL8Vector *timeVec=NULL;
127 LALParsedDataFile *cfgdata=NULL;
128 REAL8 tmp1, tmp2, tmp3;
129
132
133 /* some consistency checks */
134 ASSERT (filename != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
135 ASSERT ( out != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
136 ASSERT ( *out == NULL, status, NRWAVEIO_ENONULL, NRWAVEIO_MSGENONULL );
137
138 if ( XLALParseDataFile ( &cfgdata, filename ) != XLAL_SUCCESS ) {
139 ABORT( status, NRWAVEIO_EFILE, NRWAVEIO_MSGEFILE );
140 }
141 length = cfgdata->lines->nTokens; /*number of data points */
142
143
144 /* allocate memory */
145 ret = LALCalloc(1, sizeof(*ret));
146 if (!ret) {
147 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
148 }
149 strcpy(ret->name,filename);
150 ret->f0 = 0;
151
153 if (!data) {
154 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
155 }
156
157 timeVec = XLALCreateREAL8Vector (length);
158 if (!timeVec) {
159 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
160 }
161
162 /* now get the data */
163 for (k = 0; k < length; k++) {
164 r = sscanf(cfgdata->lines->tokens[k], "%lf%lf%lf", &tmp1, &tmp2, &tmp3);
165
166 /* Check the data file format */
167 if ( r != 3) {
168 /* there must be exactly 3 data entries -- time, h+, hx */
169 ABORT( status, NRWAVEIO_EFORMAT, NRWAVEIO_MSGEFORMAT );
170 }
171
172 timeVec->data[k] = tmp1;
173 data->data[k] = tmp2;
174 data->data[data->vectorLength + k] = tmp3;
175
176 }
177
178 /* scale time */
179 ret->deltaT = timeVec->data[1] - timeVec->data[0];
180
181 /* might also want to go through timeVec to make sure it is evenly spaced */
182
183
184 ret->data = data;
185 (*out) = ret;
186
187 XLALDestroyREAL8Vector (timeVec);
189
191 RETURN(status);
192
193} /* LALReadNRWave() */
194
195
196/**
197 * Reads a numerical relativity waveform given a filename and a value of the
198 * total mass for setting the timescale. The output waveform is scaled corresponding
199 * to a distance of 1Mpc.
200 */
201void LALReadNRWave(LALStatus *status, /**< pointer to LALStatus structure */
202 REAL4TimeVectorSeries **out, /**< [out] output time series for h+ and hx */
203 const REAL4 mass, /**< [in] Value of total mass for setting time scale */
204 const CHAR *filename /**< [in] File containing numrel waveform */)
205{
206
207 UINT4 length, k, r;
208 REAL4TimeVectorSeries *ret=NULL;
210 REAL4Vector *timeVec=NULL;
211 REAL4 massMpc;
212 LALParsedDataFile *cfgdata=NULL;
213 REAL4 tmp1, tmp2, tmp3;
214
217
218 /* some consistency checks */
219 ASSERT (filename != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
220 ASSERT ( mass > 0, status, NRWAVEIO_EVAL, NRWAVEIO_MSGEVAL );
221 ASSERT ( out != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
222 ASSERT ( *out == NULL, status, NRWAVEIO_ENONULL, NRWAVEIO_MSGENONULL );
223
224 if ( XLALParseDataFile ( &cfgdata, filename ) != XLAL_SUCCESS ) {
225 ABORT( status, NRWAVEIO_EFILE, NRWAVEIO_MSGEFILE );
226 }
227 length = cfgdata->lines->nTokens; /*number of data points */
228
229
230 /* allocate memory */
231 ret = LALCalloc(1, sizeof(*ret));
232 if (!ret) {
233 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
234 }
235 strcpy(ret->name,filename);
236 ret->f0 = 0;
237
239 if (!data) {
240 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
241 }
242
243 timeVec = XLALCreateREAL4Vector (length);
244 if (!timeVec) {
245 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
246 }
247
248 /* mass in Mpc -- we multiply h(t) by this factor */
249 massMpc = mass * LAL_MRSUN_SI / ( LAL_PC_SI * 1.0e6);
250
251 /* now get the data */
252 for (k = 0; k < length; k++) {
253 r = sscanf(cfgdata->lines->tokens[k], "%f%f%f", &tmp1, &tmp2, &tmp3);
254
255 /* Check the data file format */
256 if ( r != 3) {
257 /* there must be exactly 3 data entries -- time, h+, hx */
258 ABORT( status, NRWAVEIO_EFORMAT, NRWAVEIO_MSGEFORMAT );
259 }
260
261 timeVec->data[k] = tmp1;
262 data->data[k] = massMpc * tmp2;
263 data->data[data->vectorLength + k] = massMpc * tmp3;
264
265 }
266
267 /* scale time */
268 ret->deltaT = LAL_MTSUN_SI * mass * ( timeVec->data[1] - timeVec->data[0]);
269
270 /* go through timeVec to make sure it is uniformly spaced */
271
272
273 ret->data = data;
274 (*out) = ret;
275
276 XLALDestroyREAL4Vector (timeVec);
278
280 RETURN(status);
281
282} /* LALReadNRWave() */
283
284
285
286/**
287 * Function for reading a numerical relativity metadata file.
288 * It returns a list of numrel wave parameters. It uses
289 * XLALParseDataFile() for reading the data file. This automatically
290 * takes care of removing comment lines starting with # and other details.
291 */
292void
293LALNRDataFind( LALStatus *status, /**< pointer to LALStatus structure */
294 NRWaveCatalog *out, /**< [out] list of numrel metadata */
295 const CHAR *dir, /**< [in] directory with data files */
296 const CHAR *filename /**< [in] File with metadata information */)
297{
298 LALParsedDataFile *cfgdata=NULL;
299 UINT4 k, numWaves;
300
303
304 ASSERT (filename != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
305 ASSERT ( out != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
306 ASSERT ( dir != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
307
308 if ( XLALParseDataFile ( &cfgdata, filename ) != XLAL_SUCCESS ) {
309 ABORT( status, NRWAVEIO_EFILE, NRWAVEIO_MSGEFILE );
310 }
311 numWaves = cfgdata->lines->nTokens; /*number of waves */
312
313 /* allocate memory for output catalog */
314 out->length = numWaves;
315 out->data = LALCalloc(1, out->length * sizeof(NRWaveMetaData));
316 if ( out->data == NULL) {
317 ABORT( status, NRWAVEIO_ENOMEM, NRWAVEIO_MSGENOMEM );
318 }
319
320 /* now get wave parameters from each line of data */
321 for (k = 0; k < numWaves; k++) {
322 TRY(LALGetSingleNRMetaData( status->statusPtr, out->data + k, dir, cfgdata->lines->tokens[k]), status);
323 }
324
326
328 RETURN(status);
329}
330
331
332
333/** Parse a single string to fill the NRWaveMetaData structure */
334void
335LALGetSingleNRMetaData( LALStatus *status, /**< pointer to LALStatus structure */
336 NRWaveMetaData *out, /**< [out] Meta data struct to be filled */
337 const CHAR *dir, /**< [in] data directory */
338 const CHAR *cfgstr /**< [in] config string containing the data for a single NR wave*/)
339{
340 REAL4 tmpR[7];
341 INT4 tmpI[2];
342 INT4 test;
343 CHAR tmpStr[512];
344
347
348 ASSERT (cfgstr != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
349 ASSERT (out != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
350 ASSERT (dir != NULL, status, NRWAVEIO_ENULL, NRWAVEIO_MSGENULL );
351
352 test = sscanf(cfgstr, "%f%f%f%f%f%f%f%d%d%s", tmpR, tmpR+1, tmpR+2, tmpR+3, tmpR+4, tmpR+5,
353 tmpR+6, tmpI, tmpI+1, tmpStr);
354
355 /* Check the metadata file format */
356 if ( test != 10) {
357 /* there must be exactly 10 data entries
358 -- massratio, spin1[3], spin2[3], l, m, filename */
359 ABORT( status, NRWAVEIO_EFORMAT, NRWAVEIO_MSGEFORMAT );
360 }
361
362 /* the mass ratio must be positive */
363 ASSERT (tmpR[0] >= 0, status, NRWAVEIO_EVAL, NRWAVEIO_MSGEVAL );
364
365 /*** need a few more format checks here ***/
366
367 /* copy values to out */
368 out->massRatio = tmpR[0];
369 out->spin1[0] = tmpR[1];
370 out->spin1[1] = tmpR[2];
371 out->spin1[2] = tmpR[3];
372 out->spin2[0] = tmpR[4];
373 out->spin2[1] = tmpR[5];
374 out->spin2[2] = tmpR[6];
375
376 out->mode[0] = tmpI[0];
377 out->mode[1] = tmpI[1];
378
379 strcpy(out->filename, dir);
380 strcat(out->filename, "/");
381 strcat(out->filename, tmpStr);
382
384 RETURN(status);
385}
386
387/** Put the main functionalities of nr_wave.c together */
388void
390 LALStatus *status, /**< pointer to LALStatus structure */
391 REAL4TimeVectorSeries **outStrain, /**< h+, hx data */
392 NRWaveCatalog *nrCatalog, /**< NR wave metadata struct */
393 INT4 modeLlo, /**< contains modeLlo and modeLhi */
394 INT4 modeLhi, /**< modeLhi */
395 const SimInspiralTable *thisInj /**< injection */)
396{
397 INT4 modeL, modeM;
398 REAL4TimeVectorSeries *sumStrain=NULL;
399 REAL4TimeVectorSeries *tempStrain=NULL;
400 NRWaveMetaData thisMetaData;
401
404
405 /* loop over l values */
406 for ( modeL = modeLlo; modeL <= modeLhi; modeL++ )
407 {
408 /* loop over m values */
409 for ( modeM = -modeL; modeM <= modeL; modeM++ )
410 {
411 /* find nearest matching numrel waveform */
412 XLALFindNRFile( &thisMetaData, nrCatalog, thisInj, modeL, modeM );
413
414 /* read numrel waveform */
415 TRY( LALReadNRWave( status->statusPtr, &tempStrain, thisInj->mass1 +
416 thisInj->mass2, thisMetaData.filename ), status );
417
418 /* compute the h+ and hx for given inclination and coalescence phase*/
419 XLALOrientNRWave( tempStrain, thisMetaData.mode[0],
420 thisMetaData.mode[1], thisInj->inclination, thisInj->coa_phase );
421
422 if (sumStrain == NULL) {
423
424 sumStrain = LALCalloc(1, sizeof(*sumStrain));
425
426 sumStrain->data = XLALCreateREAL4VectorSequence(2, tempStrain->data->vectorLength);
427 sumStrain->deltaT = tempStrain->deltaT;
428 sumStrain->f0 = tempStrain->f0;
429 sumStrain->sampleUnits = tempStrain->sampleUnits;
430
431 memset(sumStrain->data->data,0.0,2*tempStrain->data->vectorLength*sizeof(REAL4));
432
433 sumStrain = XLALSumStrain( sumStrain, tempStrain );
434 }
435
436 else {
437 sumStrain = XLALSumStrain( sumStrain, tempStrain );
438 }
439
440 /*fprintf(stdout, "\nInjecting NR waveform from file %s", thisMetaData.filename);*/
441
442 /* clear memory for strain */
443 XLALDestroyREAL4VectorSequence ( tempStrain->data );
444 LALFree( tempStrain );
445 tempStrain = NULL;
446
447 } /* end loop over modeM values */
448
449 } /* end loop over modeL values */
450
451 /*fprintf(stdout, "\nNR injections done\n");*/
452
453 *outStrain = sumStrain;
454
456 RETURN(status);
457
458}
459
460
461
462
463/** Main driver funtion for doing Numerical Relativity Injections */
464void LALDriveNRInject( LALStatus *status, /**< pointer to LALStatus structure */
465 REAL4TimeSeries *injData, /**< The time series to inject into */
466 SimInspiralTable *injections, /**< The list of injections to perform */
467 NumRelInjectParams *params /**< Parameters */
468 )
469{
470
471 REAL4TimeVectorSeries *sumStrain = NULL;
472 SimInspiralTable *thisInj = NULL; /* current injection */
473
476
477 /* loop over injections */
478 for ( thisInj = injections; thisInj; thisInj = thisInj->next )
479 {
480
481 TRY( LALAddStrainModes(status->statusPtr, &sumStrain, params->nrCatalog,
482 params->modeLlo, params->modeLhi, thisInj), status);
483
484 TRY( LALInjectStrainGW( status->statusPtr, injData, sumStrain, thisInj, params->ifo, params->dynRange), status);
485
487 LALFree( sumStrain );
488 sumStrain = NULL;
489
490 } /* end loop over injections */
491
492
494 RETURN(status);
495
496}
static INT4 test(REAL4, REAL4, REAL4, REAL4, REAL4, REAL4, REAL4, REAL4)
#define LALCalloc(m, n)
#define LALFree(p)
#define ABORT(statusptr, code, mesg)
#define TRY(func, statusptr)
#define ATTATCHSTATUSPTR(statusptr)
#define ASSERT(assertion, statusptr, code, mesg)
#define DETATCHSTATUSPTR(statusptr)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
REAL8 tmp3
REAL8 tmp1
sigmaKerr data[0]
REAL8VectorSequence * XLALCreateREAL8VectorSequence(UINT4 length, UINT4 veclen)
void XLALDestroyREAL4VectorSequence(REAL4VectorSequence *vecseq)
REAL4VectorSequence * XLALCreateREAL4VectorSequence(UINT4 length, UINT4 veclen)
int XLALParseDataFile(LALParsedDataFile **cfgdata, const CHAR *fname)
void XLALDestroyParsedDataFile(LALParsedDataFile *cfgdata)
#define LAL_MTSUN_SI
#define LAL_PC_SI
#define LAL_MRSUN_SI
double REAL8
char CHAR
uint32_t UINT4
int32_t INT4
float REAL4
void LALReadNRWave(LALStatus *status, REAL4TimeVectorSeries **out, const REAL4 mass, const CHAR *filename)
Reads a numerical relativity waveform given a filename and a value of the total mass for setting the ...
Definition: NRWaveIO.c:201
void LALGetSingleNRMetaData(LALStatus *status, NRWaveMetaData *out, const CHAR *dir, const CHAR *cfgstr)
Parse a single string to fill the NRWaveMetaData structure.
Definition: NRWaveIO.c:335
void LALDriveNRInject(LALStatus *status, REAL4TimeSeries *injData, SimInspiralTable *injections, NumRelInjectParams *params)
Main driver funtion for doing Numerical Relativity Injections.
Definition: NRWaveIO.c:464
void LALNRDataFind(LALStatus *status, NRWaveCatalog *out, const CHAR *dir, const CHAR *filename)
Function for reading a numerical relativity metadata file.
Definition: NRWaveIO.c:293
void LALReadNRWave_raw_real8(LALStatus *status, REAL8TimeVectorSeries **out, const CHAR *filename)
Definition: NRWaveIO.c:118
#define NRWAVEIO_EVAL
Invalid value.
Definition: NRWaveIO.h:62
#define NRWAVEIO_EFILE
Error in file-IO.
Definition: NRWaveIO.h:59
#define NRWAVEIO_EFORMAT
Meta data file format incorrect.
Definition: NRWaveIO.h:63
void LALAddStrainModes(LALStatus *status, REAL4TimeVectorSeries **outStrain, NRWaveCatalog *nrCatalog, INT4 modeLlo, INT4 modeLhi, const SimInspiralTable *thisInj)
Put the main functionalities of nr_wave.c together.
Definition: NRWaveIO.c:389
#define NRWAVEIO_ENONULL
Not a Null pointer.
Definition: NRWaveIO.h:60
#define NRWAVEIO_ENOMEM
Memory ellocation error.
Definition: NRWaveIO.h:61
#define NRWAVEIO_ENULL
Null pointer.
Definition: NRWaveIO.h:58
void LALReadNRWave_raw(LALStatus *status, REAL4TimeVectorSeries **out, const CHAR *filename)
Functionfor reading the numrel waveform – just returns the numrel data as it is without any rescaling...
Definition: NRWaveIO.c:39
INT4 XLALFindNRFile(NRWaveMetaData *out, NRWaveCatalog *nrCatalog, const SimInspiralTable *inj, INT4 modeL, INT4 modeM)
For given inspiral parameters, find nearest waveform in catalog of numerical relativity waveforms.
Definition: NRWaveInject.c:586
INT4 XLALOrientNRWave(REAL4TimeVectorSeries *strain, UINT4 modeL, INT4 modeM, REAL4 inclination, REAL4 coa_phase)
Takes a (sky averaged) numerical relativity waveform and returns the waveform appropriate for given c...
Definition: NRWaveInject.c:99
REAL4TimeVectorSeries * XLALSumStrain(REAL4TimeVectorSeries *tempstrain, REAL4TimeVectorSeries *strain)
Takes a strain of h+ and hx data and stores it in a temporal strain in order to perform the sum over ...
Definition: NRWaveInject.c:58
void LALInjectStrainGW(LALStatus *status, REAL4TimeSeries *injData, REAL4TimeVectorSeries *strain, SimInspiralTable *thisInj, CHAR *ifo, REAL8 dynRange)
Definition: NRWaveInject.c:655
static const INT4 r
REAL4Vector * XLALCreateREAL4Vector(UINT4 length)
void XLALDestroyREAL4Vector(REAL4Vector *vector)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
XLAL_SUCCESS
TokenList * lines
List of numrel waveform metadata.
Definition: NRWaveIO.h:98
NRWaveMetaData * data
List of waveforms.
Definition: NRWaveIO.h:100
UINT4 length
Number of waveforms.
Definition: NRWaveIO.h:99
Struct containing metadata information about a single numerical relativity waveform.
Definition: NRWaveIO.h:84
REAL8 spin2[3]
Spin of m2.
Definition: NRWaveIO.h:87
INT4 mode[2]
l and m values
Definition: NRWaveIO.h:88
REAL8 spin1[3]
Spin of m1.
Definition: NRWaveIO.h:86
REAL8 massRatio
Mass ratio m1/m2 where we assume m1 >= m2.
Definition: NRWaveIO.h:85
CHAR filename[LALNameLength]
filename where data is stored
Definition: NRWaveIO.h:89
CHAR name[LALNameLength]
REAL4VectorSequence * data
REAL4 * data
REAL8VectorSequence * data
CHAR name[LALNameLength]
REAL8 * data
struct tagSimInspiralTable * next
CHAR ** tokens
UINT4 nTokens