Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALMetaIO 4.0.6.1-ea7c608
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sngl_burst.c
Go to the documentation of this file.
1/*
2 * sngl_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 SnglBurst structure.
39 */
41{
42 SnglBurst *new = XLALMalloc(sizeof(*new));
43
44 if(!new)
46
47 new->next = NULL;
48 new->process_id = new->event_id = -1;
49 memset(new->ifo, 0, sizeof(new->ifo));
50 memset(new->search, 0, sizeof(new->search));
51 memset(new->channel, 0, sizeof(new->channel));
52 XLALGPSSet(&new->start_time, 0, 0);
53 XLALGPSSet(&new->peak_time, 0, 0);
54 new->duration = XLAL_REAL4_FAIL_NAN;
55 new->central_freq = XLAL_REAL4_FAIL_NAN;
56 new->bandwidth = XLAL_REAL4_FAIL_NAN;
57 new->amplitude = XLAL_REAL4_FAIL_NAN;
58 new->snr = XLAL_REAL4_FAIL_NAN;
59 new->confidence = XLAL_REAL4_FAIL_NAN;
60 new->chisq = XLAL_REAL8_FAIL_NAN;
61 new->chisq_dof = XLAL_REAL8_FAIL_NAN;
62
63 return new;
64}
65
66
67/**
68 * Free a SnglBurst.
69 */
71{
72 SnglBurst *next = row ? row->next : NULL;
73 XLALFree(row);
74 return next;
75}
76
77
78/**
79 * Free a SnglBurst linked list.
80 */
82{
83 while(head)
84 head = XLALDestroySnglBurst(head);
85}
86
87
88/**
89 * Read the sngl_burst table from a LIGO Light Weight XML file into a
90 * linked list of SnglBurst structures.
91 */
93 const char *filename
94)
95{
96 static const char table_name[] = "sngl_burst";
97 int miostatus;
98 SnglBurst *head = NULL;
99 SnglBurst **next = &head;
100 struct MetaioParseEnvironment env;
101 struct {
102 int process_id;
103 int ifo;
104 int search;
105 int channel;
106 int start_time;
107 int start_time_ns;
108 int peak_time;
109 int peak_time_ns;
110 int duration;
111 int central_freq;
112 int bandwidth;
113 int amplitude;
114 int snr;
115 int confidence;
116 int chisq;
117 int chisq_dof;
118 int event_id;
119 } column_pos;
120
121 /* open the file and find table */
122
123 if(MetaioOpenFile(&env, filename)) {
124 XLALPrintError("%s(): error opening \"%s\": %s\n", __func__, filename, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
126 }
127 if(MetaioOpenTableOnly(&env, table_name)) {
128 MetaioAbort(&env);
129 XLALPrintError("%s(): cannot find %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
131 }
132
133 /* find columns */
134
136 column_pos.process_id = XLALLIGOLwFindColumn(&env, "process_id", METAIO_TYPE_INT_8S, 1);
137 column_pos.ifo = XLALLIGOLwFindColumn(&env, "ifo", METAIO_TYPE_LSTRING, 1);
138 column_pos.search = XLALLIGOLwFindColumn(&env, "search", METAIO_TYPE_LSTRING, 1);
139 column_pos.channel = XLALLIGOLwFindColumn(&env, "channel", METAIO_TYPE_LSTRING, 1);
140 column_pos.start_time = XLALLIGOLwFindColumn(&env, "start_time", METAIO_TYPE_INT_4S, 1);
141 column_pos.start_time_ns = XLALLIGOLwFindColumn(&env, "start_time_ns", METAIO_TYPE_INT_4S, 1);
142 column_pos.peak_time = XLALLIGOLwFindColumn(&env, "peak_time", METAIO_TYPE_INT_4S, 1);
143 column_pos.peak_time_ns = XLALLIGOLwFindColumn(&env, "peak_time_ns", METAIO_TYPE_INT_4S, 1);
144 column_pos.duration = XLALLIGOLwFindColumn(&env, "duration", METAIO_TYPE_REAL_4, 1);
145 column_pos.central_freq = XLALLIGOLwFindColumn(&env, "central_freq", METAIO_TYPE_REAL_4, 1);
146 column_pos.bandwidth = XLALLIGOLwFindColumn(&env, "bandwidth", METAIO_TYPE_REAL_4, 1);
147 column_pos.amplitude = XLALLIGOLwFindColumn(&env, "amplitude", METAIO_TYPE_REAL_4, 1);
148 column_pos.snr = XLALLIGOLwFindColumn(&env, "snr", METAIO_TYPE_REAL_4, 1);
149 column_pos.confidence = XLALLIGOLwFindColumn(&env, "confidence", METAIO_TYPE_REAL_4, 1);
150 column_pos.chisq = XLALLIGOLwFindColumn(&env, "chisq", METAIO_TYPE_REAL_8, 1);
151 column_pos.chisq_dof = XLALLIGOLwFindColumn(&env, "chisq_dof", METAIO_TYPE_REAL_8, 1);
152 column_pos.event_id = XLALLIGOLwFindColumn(&env, "event_id", METAIO_TYPE_INT_8S, 1);
153
154 /* check for failure (== a required column is missing) */
155
156 if(XLALGetBaseErrno()) {
157 MetaioAbort(&env);
158 XLALPrintError("%s(): failure reading %s table: missing required column\n", __func__, table_name);
160 }
161
162 /* loop over the rows in the file */
163
164 while((miostatus = MetaioGetRow(&env)) > 0) {
165 /* create a new row */
166
168
169 if(!row) {
171 MetaioAbort(&env);
173 }
174
175 /* append to linked list */
176
177 *next = row;
178 next = &(*next)->next;
179
180 /* populate the columns */
181
182 row->process_id = env.ligo_lw.table.elt[column_pos.process_id].data.int_8s;
183 if(strlen(env.ligo_lw.table.elt[column_pos.ifo].data.lstring.data) >= sizeof(row->ifo) ||
184 strlen(env.ligo_lw.table.elt[column_pos.search].data.lstring.data) >= sizeof(row->search) ||
185 strlen(env.ligo_lw.table.elt[column_pos.channel].data.lstring.data) >= sizeof(row->channel)) {
187 MetaioAbort(&env);
188 XLALPrintError("%s(): failure reading %s table: string too long\n", __func__, table_name);
190 }
191 strncpy(row->ifo, env.ligo_lw.table.elt[column_pos.ifo].data.lstring.data, sizeof(row->ifo) - 1);
192 strncpy(row->search, env.ligo_lw.table.elt[column_pos.search].data.lstring.data, sizeof(row->search) - 1);
193 strncpy(row->channel, env.ligo_lw.table.elt[column_pos.channel].data.lstring.data, sizeof(row->channel) - 1);
194 XLALGPSSet(&row->start_time, env.ligo_lw.table.elt[column_pos.start_time].data.int_4s, env.ligo_lw.table.elt[column_pos.start_time_ns].data.int_4s);
195 XLALGPSSet(&row->peak_time, env.ligo_lw.table.elt[column_pos.peak_time].data.int_4s, env.ligo_lw.table.elt[column_pos.peak_time_ns].data.int_4s);
196 row->duration = env.ligo_lw.table.elt[column_pos.duration].data.real_4;
197 row->central_freq = env.ligo_lw.table.elt[column_pos.central_freq].data.real_4;
198 row->bandwidth = env.ligo_lw.table.elt[column_pos.bandwidth].data.real_4;
199 row->amplitude = env.ligo_lw.table.elt[column_pos.amplitude].data.real_4;
200 row->snr = env.ligo_lw.table.elt[column_pos.snr].data.real_4;
201 row->confidence = env.ligo_lw.table.elt[column_pos.confidence].data.real_4;
202 row->chisq = env.ligo_lw.table.elt[column_pos.chisq].data.real_8;
203 row->chisq_dof = env.ligo_lw.table.elt[column_pos.chisq_dof].data.real_8;
204 row->event_id = env.ligo_lw.table.elt[column_pos.event_id].data.int_8s;
205 }
206 if(miostatus < 0) {
208 MetaioAbort(&env);
209 XLALPrintError("%s(): I/O error parsing %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
211 }
212
213 /* close file */
214
215 if(MetaioClose(&env)) {
217 XLALPrintError("%s(): error parsing document after %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
219 }
220
221 /* done */
222
223 return head;
224}
225
226
227/**
228 * Write a sngl_burst table to an XML file.
229 */
231 LIGOLwXMLStream *xml,
232 const SnglBurst *sngl_burst
233)
234{
235 const char *row_head = "\n\t\t\t";
236
237 /* table header */
238
240 XLALFilePuts("\t<Table Name=\"sngl_burst:table\">\n", xml->fp);
241 XLALFilePuts("\t\t<Column Name=\"process:process_id\" Type=\"int_8s\"/>\n", xml->fp);
242 XLALFilePuts("\t\t<Column Name=\"ifo\" Type=\"lstring\"/>\n", xml->fp);
243 XLALFilePuts("\t\t<Column Name=\"search\" Type=\"lstring\"/>\n", xml->fp);
244 XLALFilePuts("\t\t<Column Name=\"channel\" Type=\"lstring\"/>\n", xml->fp);
245 XLALFilePuts("\t\t<Column Name=\"start_time\" Type=\"int_4s\"/>\n", xml->fp);
246 XLALFilePuts("\t\t<Column Name=\"start_time_ns\" Type=\"int_4s\"/>\n", xml->fp);
247 XLALFilePuts("\t\t<Column Name=\"peak_time\" Type=\"int_4s\"/>\n", xml->fp);
248 XLALFilePuts("\t\t<Column Name=\"peak_time_ns\" Type=\"int_4s\"/>\n", xml->fp);
249 XLALFilePuts("\t\t<Column Name=\"duration\" Type=\"real_4\"/>\n", xml->fp);
250 XLALFilePuts("\t\t<Column Name=\"central_freq\" Type=\"real_4\"/>\n", xml->fp);
251 XLALFilePuts("\t\t<Column Name=\"bandwidth\" Type=\"real_4\"/>\n", xml->fp);
252 XLALFilePuts("\t\t<Column Name=\"amplitude\" Type=\"real_4\"/>\n", xml->fp);
253 XLALFilePuts("\t\t<Column Name=\"snr\" Type=\"real_4\"/>\n", xml->fp);
254 XLALFilePuts("\t\t<Column Name=\"confidence\" Type=\"real_4\"/>\n", xml->fp);
255 XLALFilePuts("\t\t<Column Name=\"chisq\" Type=\"real_8\"/>\n", xml->fp);
256 XLALFilePuts("\t\t<Column Name=\"chisq_dof\" Type=\"real_8\"/>\n", xml->fp);
257 XLALFilePuts("\t\t<Column Name=\"event_id\" Type=\"int_8s\"/>\n", xml->fp);
258 XLALFilePuts("\t\t<Stream Name=\"sngl_burst:table\" Type=\"Local\" Delimiter=\",\">", xml->fp);
259 if(XLALGetBaseErrno())
261
262 /* rows */
263
264 for(; sngl_burst; sngl_burst = sngl_burst->next) {
265 if(XLALFilePrintf(xml->fp, "%s%ld,\"%s\",\"%s\",\"%s\",%d,%d,%d,%d,%.8g,%.8g,%.8g,%.8g,%.8g,%.8g,%.16g,%.16g,%ld",
266 row_head,
267 sngl_burst->process_id,
268 sngl_burst->ifo,
269 sngl_burst->search,
270 sngl_burst->channel,
271 sngl_burst->start_time.gpsSeconds,
272 sngl_burst->start_time.gpsNanoSeconds,
273 sngl_burst->peak_time.gpsSeconds,
274 sngl_burst->peak_time.gpsNanoSeconds,
275 sngl_burst->duration,
276 sngl_burst->central_freq,
277 sngl_burst->bandwidth,
278 sngl_burst->amplitude,
279 sngl_burst->snr,
280 sngl_burst->confidence,
281 sngl_burst->chisq,
282 sngl_burst->chisq_dof,
283 sngl_burst->event_id
284 ) < 0)
286 row_head = ",\n\t\t\t";
287 }
288
289 /* table footer */
290
291 if(XLALFilePuts("\n\t\t</Stream>\n\t</Table>\n", xml->fp) < 0)
293
294 /* done */
295
296 return 0;
297}
298
299
300/**
301 * Assign event_id values to the entries in a SnglBurst linked list. All
302 * SnglBurst rows in the list will be blamed on the given process_id, and
303 * assigned sequential event_ids starting with the given event_id. The
304 * return value is the next event_id after the last one assigned to a row
305 * in the list.
306 */
308 SnglBurst *head,
309 long process_id,
310 long event_id
311)
312{
313 for(; head; head = head->next) {
314 head->process_id = process_id;
315 head->event_id = event_id++;
316 }
317 return event_id;
318}
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_REAL4_FAIL_NAN
#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)
filename
SnglBurst * XLALDestroySnglBurst(SnglBurst *row)
Free a SnglBurst.
Definition: sngl_burst.c:70
int XLALWriteLIGOLwXMLSnglBurstTable(LIGOLwXMLStream *xml, const SnglBurst *sngl_burst)
Write a sngl_burst table to an XML file.
Definition: sngl_burst.c:230
SnglBurst * XLALSnglBurstTableFromLIGOLw(const char *filename)
Read the sngl_burst table from a LIGO Light Weight XML file into a linked list of SnglBurst structure...
Definition: sngl_burst.c:92
SnglBurst * XLALCreateSnglBurst(void)
Create a SnglBurst structure.
Definition: sngl_burst.c:40
void XLALDestroySnglBurstTable(SnglBurst *head)
Free a SnglBurst linked list.
Definition: sngl_burst.c:81
long XLALSnglBurstAssignIDs(SnglBurst *head, long process_id, long event_id)
Assign event_id values to the entries in a SnglBurst linked list.
Definition: sngl_burst.c:307
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
LIGOTimeGPS start_time
CHAR channel[LIGOMETA_CHANNEL_MAX]
CHAR search[LIGOMETA_SEARCH_MAX]
CHAR ifo[LIGOMETA_IFO_MAX]
LIGOTimeGPS peak_time
struct tagSnglBurst * next