Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALSimulation 6.2.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
inject.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2015 Jolien Creighton
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 * @defgroup lalsim_inject lalsim-inject
22 * @ingroup lalsimulation_programs
23 *
24 * @brief Injects an induced gravitational wave strain into detector data
25 *
26 * ### Synopsis
27 *
28 * lalsim-inject [options] targetfile injectfile1 [injectfile2 ...]
29 *
30 * ### Description
31 *
32 * The `lalsim-inject` utility takes gravitational wave detector data
33 * contained in `targetfile` and adds to it the gravitational wave
34 * induced strain data in `injectfile1` .... The result is written
35 * to standard output. All input and output is two-column ascii format
36 * data where the first column contains the GPS timestamp of each sample
37 * and the second column contains the detector strain value.
38 *
39 * ### Options
40 *
41 * <DL>
42 * <DT>`-h`, `--help` <DD>print a help message and exit</DD>
43 * <DT>`-v`, `--verbose` <DD>verbose output</DD>
44 * </DL>
45 *
46 * ### Environment
47 *
48 * The `LAL_DEBUG_LEVEL` can used to control the error and warning reporting of
49 * `lalsim-inject`. Common values are: `LAL_DEBUG_LEVEL=0` which suppresses
50 * error messages, `LAL_DEBUG_LEVEL=1` which prints error messages alone,
51 * `LAL_DEBUG_LEVEL=3` which prints both error messages and warning messages,
52 * and `LAL_DEBUG_LEVEL=7` which additionally prints informational messages.
53 *
54 * ### Exit Status
55 *
56 * The `lalsim-inject` utility exits 0 on success, and >0 if an error occurs.
57 *
58 * ### Example
59 *
60 * The following set of commands produces 16 seconds of simulated detector
61 * noise for the LHO detector starting at GPS time 1000000000; produces a
62 * synthetic binary neutron star signal in the LHO detector that has a
63 * geocentric end time of 1000000008; and adds the signal to the noise:
64 *
65 *
66 * lalsim-detector-noise --aligo-zerodet-highpower -s 1000000000 -t 16 > noise
67 * lalsim-inspiral | lalsim-detector-strain -D H1 -a 1:23:45 -d 45.0 -p 30.0 -t 1000000008 > signal
68 * lalsim-inject noise signal > output
69 *
70 * The resulting file `output` contains the simulated signal contained in file
71 * `signal` injected into the simulated noise contained in file `noise`.
72 */
73
74
75#include <math.h>
76#include <limits.h>
77#include <stdio.h>
78#include <stdlib.h>
79#include <string.h>
80
81#include <lal/LALDatatypes.h>
82#include <lal/Units.h>
83#include <lal/Date.h>
84#include <lal/TimeSeries.h>
85#include <lal/LALSimulation.h>
86
87char *program;
88int usage(void);
89int isoption(char *arg, const char *opt);
90REAL8TimeSeries *readdata(FILE * fp);
91
92int main(int argc, char *argv[])
93{
94 char tstr[32]; // string to hold GPS time -- 31 characters is enough
96 int verbose = 0;
97 int first_stdin = -1;
98 int n;
99 int c;
100 size_t j;
101
102 program = argv[0];
103
104 /* parse options */
105 for (c = 1; c < argc; ++c) {
106 char *arg = argv[c];
107 if (*arg != '-' || strcmp(arg, "-") == 0) /* end of options */
108 break;
109 if (strcmp(arg, "--") == 0) { /* stop option parsing */
110 ++c; /* go on to next argument */
111 break;
112 }
113 if (isoption(arg, "-help") || isoption(arg, "--help")) {
114 usage();
115 return 0;
116 }
117 if (isoption(arg, "-verbose") || isoption(arg, "--verbose"))
118 verbose = 1;
119 }
120
121 /* parse remaining arguments, reading the various time series */
122
123 if (argc - c < 2) {
124 fprintf(stderr, "%s: must provide one target file and at least one injection file\n", program);
125 usage();
126 return 1;
127 }
128
129 h = calloc(argc - c, sizeof(*h));
130 for (n = 0; c < argc; ++c, ++n) {
131 char *fname = argv[c];
132 FILE *fp = stdin;
133 if (verbose)
134 fprintf(stderr, "%s: reading %s data ", program, n ? "injection" : "target");
135 if (strcmp(fname, "-") != 0) {
136 if (verbose)
137 fprintf(stderr, "from file %s\n", fname);
138 if (!(fp = fopen(fname, "r"))) {
139 fprintf(stderr, "%s: could not open file %s\n", program, fname);
140 return 1;
141 }
142 } else if (verbose)
143 fprintf(stderr, "from <stdin>\n");
144
145 if (fp == stdin) {
146 if (first_stdin < 0) {
147 h[n] = readdata(fp);
148 first_stdin = n;
149 } else /* already read stdin: copy data */
150 h[n] = XLALCutREAL8TimeSeries(h[first_stdin], 0, h[first_stdin]->data->length);
151 } else
152 h[n] = readdata(fp);
153
154 if (!h[n]) {
155 fprintf(stderr, "%s: failed to read data from file %s\n", program, fname);
156 }
157 if (verbose)
158 fprintf(stderr, "%s: %d points of strain data read\n", program, (int)h[n]->data->length);
159 }
160
161 /* add injections to target */
162 for (c = 1; c < n; ++c) {
163 /* sample rates deduced from file might have roundoff
164 * errors: if an injection series' deltaT is close enough
165 * the target series' deltaT, make it equal */
166 if (fabs(1.0 / h[c]->deltaT - 1.0 / h[0]->deltaT) < 0.1)
167 h[c]->deltaT = h[0]->deltaT;
168 else {
169 fprintf(stderr, "%s: incorrect sample rate for injection %d -- must match target\n", program, c);
170 return 1;
171 }
172
173 /* now add */
174 if (verbose)
175 fprintf(stderr, "%s: adding injection %d to target\n", program, c);
176 if (XLALSimAddInjectionREAL8TimeSeries(h[0], h[c], NULL) < 0) {
177 fprintf(stderr, "%s: failed to add injection %d to target\n", program, c);
178 return 1;
179 }
180 }
181
182 /* output results */
183 fprintf(stdout, "# time (s)\tSTRAIN (strain)\n");
184 for (j = 0; j < h[0]->data->length; ++j) {
185 LIGOTimeGPS t = h[0]->epoch;
186 fprintf(stdout, "%s\t%.18e\n", XLALGPSToStr(tstr, XLALGPSAdd(&t, j * h[0]->deltaT)), h[0]->data->data[j]);
187 }
188
189 /* cleanup and exit */
190 while (n--)
192 free(h);
194 return 0;
195}
196
197/* determine if argument matches option name */
198int isoption(char *arg, const char *opt)
199{
200 return strstr(opt, arg) == opt;
201}
202
203/* read a file containing timeseries data */
205{
207 const size_t block = 1024;
208 LIGOTimeGPS start;
210 double dt;
211 double *data = NULL;
212 size_t bufsz = 0;
213 size_t n, l;
214 char line[LINE_MAX];
215 char t0[LINE_MAX];
216 char t1[LINE_MAX];
217
218 for (l = 0, n = 0; fgets(line, sizeof(line), fp); ++l) {
219 int c;
220 if (*line == '#')
221 continue;
222 if (n == bufsz) { /* allocate more memory */
223 bufsz += block;
224 data = realloc(data, bufsz * sizeof(*data));
225 }
226 c = sscanf(line, "%s %le", n ? t1 : t0, data + n);
227 if (c != 2) {
228 fprintf(stderr, "%s: format error on line %zd: %s\n", program, l, line);
229 exit(1);
230 }
231 ++n;
232 }
233 data = realloc(data, n * sizeof(*data));
234
235 XLALStrToGPS(&start, t0, NULL);
236 XLALStrToGPS(&end, t1, NULL);
237 dt = XLALGPSDiff(&end, &start) / (n - 1);
238 h = XLALCreateREAL8TimeSeries("strain", &start, 0.0, dt, &lalStrainUnit, n);
239 memcpy(h->data->data, data, n * sizeof(*data));
240
241 free(data);
242 return h;
243}
244
245int usage(void)
246{
247 /* *INDENT-OFF* */
248 fprintf(stderr, "\
249usage: %s [options] targetfile injectfile1 [injectfile2 ...]\n\
250options:\n\
251 -h, --help print this message and exit\n\
252 -v, --verbose verbose output\n\
253", program);
254 /* *INDENT-ON* */
255 return 0;
256}
int XLALStrToGPS(LIGOTimeGPS *t, const char *nptr, char **endptr)
void LALCheckMemoryLeaks(void)
#define c
#define LINE_MAX
#define fprintf
int l
Definition: bh_qnmode.c:135
double dt
Definition: bh_ringdown.c:113
static int verbose
Definition: burst.c:166
sigmaKerr data[0]
char * XLALGPSToStr(char *s, const LIGOTimeGPS *t)
int XLALSimAddInjectionREAL8TimeSeries(REAL8TimeSeries *target, REAL8TimeSeries *h, const COMPLEX16FrequencySeries *response)
Adds a detector strain time series to detector data.
void XLALDestroyREAL8TimeSeries(REAL8TimeSeries *series)
REAL8TimeSeries * XLALCutREAL8TimeSeries(const REAL8TimeSeries *series, size_t first, size_t length)
REAL8TimeSeries * XLALCreateREAL8TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
const LALUnit lalStrainUnit
LIGOTimeGPS * XLALGPSAdd(LIGOTimeGPS *epoch, REAL8 dt)
REAL8 XLALGPSDiff(const LIGOTimeGPS *t1, const LIGOTimeGPS *t0)
int main(int argc, char *argv[])
Definition: inject.c:92
char * program
Definition: inject.c:87
REAL8TimeSeries * readdata(FILE *fp)
Definition: inject.c:204
int usage(void)
Definition: inject.c:245
int isoption(char *arg, const char *opt)
Definition: inject.c:198
end
REAL8Sequence * data
LIGOTimeGPS epoch
REAL8 * data
FILE * fp
double deltaT
Definition: unicorn.c:24