Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALMetaIO 4.0.6.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sim_burst.c
Go to the documentation of this file.
1/*
2 * sim_burst.c
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with with program; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23
24
25#include <metaio.h>
26
27
28#include <lal/Date.h>
29#include <lal/LALMalloc.h>
30#include <lal/LIGOLwXML.h>
31#include <lal/LIGOLwXMLRead.h>
32#include <lal/LIGOMetadataTables.h>
33#include <lal/LIGOMetadataUtils.h>
34#include <lal/XLALError.h>
35
36
37/**
38 * Create a SimBurst structure.
39 */
41{
42 SimBurst *new = XLALMalloc(sizeof(*new));
43
44 if(!new)
46
47 new->next = NULL;
48 new->process_id = new->time_slide_id = new->simulation_id = -1;
49 memset(new->waveform, 0, sizeof(new->waveform));
50 new->ra = XLAL_REAL8_FAIL_NAN;
51 new->dec = XLAL_REAL8_FAIL_NAN;
52 new->psi = XLAL_REAL8_FAIL_NAN;
53 XLALGPSSet(&new->time_geocent_gps, 0, 0);
54 new->time_geocent_gmst = XLAL_REAL8_FAIL_NAN;
55 new->duration = XLAL_REAL8_FAIL_NAN;
56 new->frequency = XLAL_REAL8_FAIL_NAN;
57 new->bandwidth = XLAL_REAL8_FAIL_NAN;
58 new->q = XLAL_REAL8_FAIL_NAN;
59 new->pol_ellipse_angle = XLAL_REAL8_FAIL_NAN;
60 new->pol_ellipse_e= XLAL_REAL8_FAIL_NAN;
61 new->amplitude = XLAL_REAL8_FAIL_NAN;
62 new->hrss = XLAL_REAL8_FAIL_NAN;
63 new->egw_over_rsquared = XLAL_REAL8_FAIL_NAN;
64 new->waveform_number = 0;
65
66 return new;
67}
68
69
70/**
71 * Destroy a SimBurst structure.
72 */
74{
75 SimBurst *next = row ? row->next : NULL;
76 XLALFree(row);
77 return next;
78}
79
80
81/**
82 * Destroy a SimBurst linked list.
83 */
85{
86 while(head)
87 head = XLALDestroySimBurst(head);
88}
89
90
91/**
92 * Read the sim_burst table from a LIGO Light Weight XML file into a linked
93 * list of SimBurst structures.
94 */
96 const char *filename
97)
98{
99 static const char table_name[] = "sim_burst";
100 int miostatus;
101 SimBurst *head = NULL;
102 SimBurst **next = &head;
103 struct MetaioParseEnvironment env;
104 struct {
105 int process_id;
106 int waveform;
107 int ra;
108 int dec;
109 int psi;
110 int time_geocent_gps;
111 int time_geocent_gps_ns;
112 int time_geocent_gmst;
113 int duration;
114 int frequency;
115 int bandwidth;
116 int q;
117 int pol_ellipse_angle;
118 int pol_ellipse_e;
119 int amplitude;
120 int hrss;
121 int egw_over_rsquared;
122 int waveform_number;
123 int time_slide_id;
124 int simulation_id;
125 } column_pos;
126
127 /* open the file and find table */
128
129 if(MetaioOpenFile(&env, filename)) {
130 XLALPrintError("%s(): error opening \"%s\": %s\n", __func__, filename, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
132 }
133 if(MetaioOpenTableOnly(&env, table_name)) {
134 MetaioAbort(&env);
135 XLALPrintError("%s(): cannot find %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
137 }
138
139 /* find columns */
140
142 column_pos.process_id = XLALLIGOLwFindColumn(&env, "process_id", METAIO_TYPE_INT_8S, 1);
143 column_pos.waveform = XLALLIGOLwFindColumn(&env, "waveform", METAIO_TYPE_LSTRING, 1);
144 column_pos.ra = XLALLIGOLwFindColumn(&env, "ra", METAIO_TYPE_REAL_8, 0);
145 column_pos.dec = XLALLIGOLwFindColumn(&env, "dec", METAIO_TYPE_REAL_8, 0);
146 column_pos.psi = XLALLIGOLwFindColumn(&env, "psi", METAIO_TYPE_REAL_8, 0);
147 column_pos.time_geocent_gps = XLALLIGOLwFindColumn(&env, "time_geocent_gps", METAIO_TYPE_INT_4S, 1);
148 column_pos.time_geocent_gps_ns = XLALLIGOLwFindColumn(&env, "time_geocent_gps_ns", METAIO_TYPE_INT_4S, 1);
149 column_pos.time_geocent_gmst = XLALLIGOLwFindColumn(&env, "time_geocent_gmst", METAIO_TYPE_REAL_8, 0);
150 column_pos.duration = XLALLIGOLwFindColumn(&env, "duration", METAIO_TYPE_REAL_8, 0);
151 column_pos.frequency = XLALLIGOLwFindColumn(&env, "frequency", METAIO_TYPE_REAL_8, 0);
152 column_pos.bandwidth = XLALLIGOLwFindColumn(&env, "bandwidth", METAIO_TYPE_REAL_8, 0);
153 column_pos.q = XLALLIGOLwFindColumn(&env, "q", METAIO_TYPE_REAL_8, 0);
154 column_pos.pol_ellipse_angle = XLALLIGOLwFindColumn(&env, "pol_ellipse_angle", METAIO_TYPE_REAL_8, 0);
155 column_pos.pol_ellipse_e = XLALLIGOLwFindColumn(&env, "pol_ellipse_e", METAIO_TYPE_REAL_8, 0);
156 column_pos.amplitude = XLALLIGOLwFindColumn(&env, "amplitude", METAIO_TYPE_REAL_8, 0);
157 column_pos.hrss = XLALLIGOLwFindColumn(&env, "hrss", METAIO_TYPE_REAL_8, 0);
158 column_pos.egw_over_rsquared = XLALLIGOLwFindColumn(&env, "egw_over_rsquared", METAIO_TYPE_REAL_8, 0);
159 column_pos.waveform_number = XLALLIGOLwFindColumn(&env, "waveform_number", METAIO_TYPE_INT_8U, 0);
160 column_pos.time_slide_id = XLALLIGOLwFindColumn(&env, "time_slide_id", METAIO_TYPE_INT_8S, 1);
161 column_pos.simulation_id = XLALLIGOLwFindColumn(&env, "simulation_id", METAIO_TYPE_INT_8S, 1);
162
163 /* check for failure (== a required column is missing) */
164
165 if(XLALGetBaseErrno()) {
166 MetaioAbort(&env);
167 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
169 }
170
171 /* loop over the rows in the file */
172
173 while((miostatus = MetaioGetRow(&env)) > 0) {
174 /* create a new row */
175
177
178 if(!row) {
180 MetaioAbort(&env);
182 }
183
184 /* populate the columns */
185
186 row->process_id = env.ligo_lw.table.elt[column_pos.process_id].data.int_8s;
187 if(strlen(env.ligo_lw.table.elt[column_pos.waveform].data.lstring.data) >= sizeof(row->waveform)) {
190 MetaioAbort(&env);
191 XLALPrintError("%s(): failure reading %s table: string too long\n", __func__, table_name);
193 }
194 strncpy(row->waveform, env.ligo_lw.table.elt[column_pos.waveform].data.lstring.data, sizeof(row->waveform) - 1);
195 if(column_pos.ra >= 0)
196 row->ra = env.ligo_lw.table.elt[column_pos.ra].data.real_8;
197 if(column_pos.dec >= 0)
198 row->dec = env.ligo_lw.table.elt[column_pos.dec].data.real_8;
199 if(column_pos.psi >= 0)
200 row->psi = env.ligo_lw.table.elt[column_pos.psi].data.real_8;
201 XLALGPSSet(&row->time_geocent_gps, env.ligo_lw.table.elt[column_pos.time_geocent_gps].data.int_4s, env.ligo_lw.table.elt[column_pos.time_geocent_gps_ns].data.int_4s);
202 if(column_pos.time_geocent_gmst >= 0)
203 row->time_geocent_gmst = env.ligo_lw.table.elt[column_pos.time_geocent_gmst].data.real_8;
204 row->time_slide_id = env.ligo_lw.table.elt[column_pos.time_slide_id].data.int_8s;
205 row->simulation_id = env.ligo_lw.table.elt[column_pos.simulation_id].data.int_8s;
206
207 if(!strcmp(row->waveform, "StringCusp")) {
208 if(column_pos.duration < 0 || column_pos.frequency < 0 || column_pos.amplitude < 0) {
211 MetaioAbort(&env);
212 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
214 }
215 row->duration = env.ligo_lw.table.elt[column_pos.duration].data.real_8;
216 row->frequency = env.ligo_lw.table.elt[column_pos.frequency].data.real_8;
217 row->amplitude = env.ligo_lw.table.elt[column_pos.amplitude].data.real_8;
218 } else if(!strcmp(row->waveform, "SineGaussian")||!strcmp(row->waveform, "SineGaussianF")) {
219 if(column_pos.duration < 0 || column_pos.frequency < 0 || column_pos.bandwidth < 0 || column_pos.q < 0 || column_pos.pol_ellipse_angle < 0 || column_pos.pol_ellipse_e < 0 || column_pos.hrss < 0) {
222 MetaioAbort(&env);
223 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
225 }
226 row->duration = env.ligo_lw.table.elt[column_pos.duration].data.real_8;
227 row->frequency = env.ligo_lw.table.elt[column_pos.frequency].data.real_8;
228 row->bandwidth = env.ligo_lw.table.elt[column_pos.bandwidth].data.real_8;
229 row->q = env.ligo_lw.table.elt[column_pos.q].data.real_8;
230 row->pol_ellipse_angle = env.ligo_lw.table.elt[column_pos.pol_ellipse_angle].data.real_8;
231 row->pol_ellipse_e = env.ligo_lw.table.elt[column_pos.pol_ellipse_e].data.real_8;
232 row->hrss = env.ligo_lw.table.elt[column_pos.hrss].data.real_8;
233 } else if(!strcmp(row->waveform, "Gaussian")) {
234 if(column_pos.duration < 0 || column_pos.hrss < 0) {
237 MetaioAbort(&env);
238 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
240 }
241 row->duration = env.ligo_lw.table.elt[column_pos.duration].data.real_8;
242 row->hrss = env.ligo_lw.table.elt[column_pos.hrss].data.real_8;
243 } else if(!strcmp(row->waveform, "BTLWNB")) {
244 if(column_pos.duration < 0 || column_pos.frequency < 0 || column_pos.bandwidth < 0 || column_pos.pol_ellipse_angle < 0 || column_pos.pol_ellipse_e < 0 || column_pos.egw_over_rsquared < 0 || column_pos.waveform_number < 0) {
247 MetaioAbort(&env);
248 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
250 }
251 row->duration = env.ligo_lw.table.elt[column_pos.duration].data.real_8;
252 row->frequency = env.ligo_lw.table.elt[column_pos.frequency].data.real_8;
253 row->bandwidth = env.ligo_lw.table.elt[column_pos.bandwidth].data.real_8;
254 row->pol_ellipse_angle = env.ligo_lw.table.elt[column_pos.pol_ellipse_angle].data.real_8;
255 row->pol_ellipse_e = env.ligo_lw.table.elt[column_pos.pol_ellipse_e].data.real_8;
256 row->egw_over_rsquared = env.ligo_lw.table.elt[column_pos.egw_over_rsquared].data.real_8;
257 row->waveform_number = env.ligo_lw.table.elt[column_pos.waveform_number].data.int_8u;
258 } else if(!strcmp(row->waveform, "Impulse")) {
259 if(column_pos.amplitude < 0) {
262 MetaioAbort(&env);
263 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
265 }
266 row->amplitude = env.ligo_lw.table.elt[column_pos.amplitude].data.real_8;
267 } else {
268 /* unrecognized waveform */
271 MetaioAbort(&env);
272 XLALPrintError("%s(): unrecognized waveform \"%s\" in %s table\n", __func__, row->waveform, table_name);
274 }
275
276 /* append to linked list */
277
278 *next = row;
279 next = &(*next)->next;
280 }
281 if(miostatus < 0) {
283 MetaioAbort(&env);
284 XLALPrintError("%s(): I/O error parsing %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
286 }
287
288 /* close file */
289
290 if(MetaioClose(&env)) {
292 XLALPrintError("%s(): error parsing document after %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
294 }
295
296 /* done */
297
298 return head;
299}
300
301
302/**
303 * Write a sim_burst table to an XML file.
304 */
306 LIGOLwXMLStream *xml,
307 const SimBurst *sim_burst
308)
309{
310 const char *row_head = "\n\t\t\t";
311
312 /* table header */
313
315 XLALFilePuts("\t<Table Name=\"sim_burst:table\">\n", xml->fp);
316 XLALFilePuts("\t\t<Column Name=\"process:process_id\" Type=\"int_8s\"/>\n", xml->fp);
317 XLALFilePuts("\t\t<Column Name=\"waveform\" Type=\"lstring\"/>\n", xml->fp);
318 XLALFilePuts("\t\t<Column Name=\"ra\" Type=\"real_8\"/>\n", xml->fp);
319 XLALFilePuts("\t\t<Column Name=\"dec\" Type=\"real_8\"/>\n", xml->fp);
320 XLALFilePuts("\t\t<Column Name=\"psi\" Type=\"real_8\"/>\n", xml->fp);
321 XLALFilePuts("\t\t<Column Name=\"time_geocent_gps\" Type=\"int_4s\"/>\n", xml->fp);
322 XLALFilePuts("\t\t<Column Name=\"time_geocent_gps_ns\" Type=\"int_4s\"/>\n", xml->fp);
323 XLALFilePuts("\t\t<Column Name=\"time_geocent_gmst\" Type=\"real_8\"/>\n", xml->fp);
324 XLALFilePuts("\t\t<Column Name=\"duration\" Type=\"real_8\"/>\n", xml->fp);
325 XLALFilePuts("\t\t<Column Name=\"frequency\" Type=\"real_8\"/>\n", xml->fp);
326 XLALFilePuts("\t\t<Column Name=\"bandwidth\" Type=\"real_8\"/>\n", xml->fp);
327 XLALFilePuts("\t\t<Column Name=\"q\" Type=\"real_8\"/>\n", xml->fp);
328 XLALFilePuts("\t\t<Column Name=\"pol_ellipse_angle\" Type=\"real_8\"/>\n", xml->fp);
329 XLALFilePuts("\t\t<Column Name=\"pol_ellipse_e\" Type=\"real_8\"/>\n", xml->fp);
330 XLALFilePuts("\t\t<Column Name=\"amplitude\" Type=\"real_8\"/>\n", xml->fp);
331 XLALFilePuts("\t\t<Column Name=\"hrss\" Type=\"real_8\"/>\n", xml->fp);
332 XLALFilePuts("\t\t<Column Name=\"egw_over_rsquared\" Type=\"real_8\"/>\n", xml->fp);
333 XLALFilePuts("\t\t<Column Name=\"waveform_number\" Type=\"int_8u\"/>\n", xml->fp);
334 XLALFilePuts("\t\t<Column Name=\"time_slide:time_slide_id\" Type=\"int_8s\"/>\n", xml->fp);
335 XLALFilePuts("\t\t<Column Name=\"simulation_id\" Type=\"int_8s\"/>\n", xml->fp);
336 XLALFilePuts("\t\t<Stream Name=\"sim_burst:table\" Type=\"Local\" Delimiter=\",\">", xml->fp);
337 if(XLALGetBaseErrno())
339
340 /* rows */
341
342 for(; sim_burst; sim_burst = sim_burst->next) {
343 if(XLALFilePrintf(xml->fp, "%s%ld,\"%s\",%.16g,%.16g,%.16g,%d,%d,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%.16g,%lu,%ld,%ld",
344 row_head,
345 sim_burst->process_id,
346 sim_burst->waveform,
347 sim_burst->ra,
348 sim_burst->dec,
349 sim_burst->psi,
350 sim_burst->time_geocent_gps.gpsSeconds,
352 sim_burst->time_geocent_gmst,
353 sim_burst->duration,
354 sim_burst->frequency,
355 sim_burst->bandwidth,
356 sim_burst->q,
357 sim_burst->pol_ellipse_angle,
358 sim_burst->pol_ellipse_e,
359 sim_burst->amplitude,
360 sim_burst->hrss,
361 sim_burst->egw_over_rsquared,
362 sim_burst->waveform_number,
363 sim_burst->time_slide_id,
364 sim_burst->simulation_id
365 ) < 0)
367 row_head = ",\n\t\t\t";
368 }
369
370 /* table footer */
371
372 if(XLALFilePuts("\n\t\t</Stream>\n\t</Table>\n", xml->fp) < 0)
374
375 /* done */
376
377 return 0;
378}
379
380
381/**
382 * Assign simulation_id values to the entries in a sim_burst linked list.
383 * All sim_burst rows in the list will be blamed on the given process_id,
384 * and assigned simulation_ids in order starting with the given
385 * simulation_id. The return value is the next simulation_id after the
386 * last one assigned to a row in the list.
387 */
389 SimBurst *sim_burst,
390 long process_id,
391 long time_slide_id,
392 long simulation_id)
393{
394 for(; sim_burst; sim_burst = sim_burst->next) {
395 sim_burst->process_id = process_id;
396 sim_burst->time_slide_id = time_slide_id;
397 sim_burst->simulation_id = simulation_id++;
398 }
399 return simulation_id;
400}
int XLALLIGOLwFindColumn(struct MetaioParseEnvironment *env, const char *name, unsigned int type, int required)
Convenience wrapper for MetaioFindColumn(), translating to XLAL-style error reporting and printing us...
int XLALFilePuts(const char *s, LALFILE *file)
int XLALFilePrintf(LALFILE *file, const char *fmt,...)
void * XLALMalloc(size_t n)
void XLALFree(void *p)
#define XLAL_ERROR_NULL(...)
int XLALGetBaseErrno(void)
#define XLAL_ERROR(...)
#define XLAL_REAL8_FAIL_NAN
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
int XLALClearErrno(void)
XLAL_EFUNC
XLAL_EIO
LIGOTimeGPS * XLALGPSSet(LIGOTimeGPS *epoch, INT4 gpssec, INT8 gpsnan)
list q
filename
int XLALWriteLIGOLwXMLSimBurstTable(LIGOLwXMLStream *xml, const SimBurst *sim_burst)
Write a sim_burst table to an XML file.
Definition: sim_burst.c:305
void XLALDestroySimBurstTable(SimBurst *head)
Destroy a SimBurst linked list.
Definition: sim_burst.c:84
long XLALSimBurstAssignIDs(SimBurst *sim_burst, long process_id, long time_slide_id, long simulation_id)
Assign simulation_id values to the entries in a sim_burst linked list.
Definition: sim_burst.c:388
SimBurst * XLALCreateSimBurst(void)
Create a SimBurst structure.
Definition: sim_burst.c:40
SimBurst * XLALSimBurstTableFromLIGOLw(const char *filename)
Read the sim_burst table from a LIGO Light Weight XML file into a linked list of SimBurst structures.
Definition: sim_burst.c:95
SimBurst * XLALDestroySimBurst(SimBurst *row)
Destroy a SimBurst structure.
Definition: sim_burst.c:73
This structure contains the file stream and current table type for writing to LIGO lightweight XML fi...
Definition: LIGOLwXML.h:71
LALFILE * fp
Definition: LIGOLwXML.h:72
INT4 gpsNanoSeconds
The SimBurst structure describes a burst injection.
char waveform[LIGOMETA_WAVEFORM_MAX]
struct tagSimBurst * next
LIGOTimeGPS time_geocent_gps
REAL8 pol_ellipse_angle
unsigned long waveform_number
REAL8 egw_over_rsquared
REAL8 time_geocent_gmst