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
LIGOLwXMLRead.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007 Andres C. Rodriguez, Alexander Dietz, Duncan Brown,
3 * Jolien Creighton, Kipp Cannon, Lisa M. Goggin, Patrick Brady, Robert
4 * Adam Mercer, Saikat Ray-Majumder, Anand Sengupta, Stephen Fairhurst,
5 * Xavier Siemens, Craig Robinson , Sean Seader, Thomas Cokelaer
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with with program; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23/*-----------------------------------------------------------------------
24 *
25 * File Name: LIGOLwXMLRead.c
26 *
27 * Author: Brown, D. A.
28 *
29 *-----------------------------------------------------------------------
30 */
31
32/**
33 * \author Cannon, K. C. and Brown, D. A. and Fairhurst, S.
34 * \file
35 * \ingroup lalmetaio_general
36 *
37 * \brief Routines to read tabular data from LIGO lightweight XML files.
38 *
39 * ### Description ###
40 *
41 * ### Algorithm ###
42 *
43 * None.
44 *
45 * ### Uses ###
46 *
47 * ### Notes ###
48 *
49 * %% Any relevant notes.
50 *
51 */
52
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
56#include <metaio.h>
57
58#include <lal/Date.h>
59#include <lal/LALConstants.h>
60#include <lal/LALStdio.h>
61#include <lal/LIGOLwXMLRead.h>
62#include <lal/LIGOMetadataTables.h>
63#include <lal/LIGOMetadataUtils.h>
64#include <lal/XLALError.h>
65
66/**
67 * Test a LIGO Light Weight XML file for the presence of a specific table.
68 * Returns > 0 if the document contains the table, 0 if the document does
69 * not contain the table, and < 0 on error.
70 *
71 * BUGS:
72 *
73 * - This function can't tell the difference between a missing table and an
74 * unparseable document. This is a limitation in libmetaio.
75 *
76 * - This function parses the entire file to determine if the table is
77 * present, which is slow.
78 *
79 * - This entire approach to XML I/O is the wrong way to go. What's needed
80 * is a "load document" function, and a "save document" function. DO NOT
81 * attempt to write such functions by using this function to test for
82 * every possible table one-by-one and loading the ones that are found.
83 * Put the time into writing a proper XML I/O layer!!
84 */
85int XLALLIGOLwHasTable(const char *filename, const char *table_name)
86{
87 struct MetaioParseEnvironment env;
88 int has_table;
89
90 /*
91 * open the file and find table
92 */
93
94 if(MetaioOpenFile(&env, filename)) {
95 XLALPrintError("%s(): error opening \"%s\": %s\n", __func__, filename, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
97 }
98
99 /*
100 * find table. note: parse errors are interpreted as "table is
101 * missing". metaio provides no other mechanism for testing for
102 * the presence of a table.
103 */
104
105 has_table = !MetaioOpenTableOnly(&env, table_name);
106 MetaioClearErrno(&env);
107
108 /*
109 * close
110 */
111
112 if(MetaioClose(&env)) {
113 XLALPrintError("%s(): error parsing document after %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
115 }
116
117 /*
118 * done
119 */
120
121 return has_table;
122}
123
124
125/**
126 * Convenience wrapper for MetaioFindColumn(), translating to XLAL-style
127 * error reporting and printing useful error messages on failure. Returns
128 * the integer index of the column, or a negative integer if the column is
129 * not found or has the wrong type. If required is non-zero, then an XLAL
130 * error is reported if the column is missing, but if required is zero then
131 * no error is generated for missing columns. When a column is found, it's
132 * type is checked and an XLAL error is reported if it does not match the
133 * requested type. Passing METAIO_TYPE_UNKNOWN disables the column type
134 * test.
135 */
137 struct MetaioParseEnvironment *env,
138 const char *name,
139 unsigned int type,
140 int required
141)
142{
143 int pos = MetaioFindColumn(env, name);
144 if(pos >= 0) {
145 /* column was found, check type */
146 if(type != METAIO_TYPE_UNKNOWN && env->ligo_lw.table.col[pos].data_type != type) {
147 XLALPrintError("%s(): column \"%s\" has wrong type\n", __func__, name);
149 }
150 } else if(required) {
151 /* required column is missing */
152 XLALPrintError("%s(): missing required column \"%s\"\n", __func__, name);
154 }
155 return pos;
156}
157
158
159/**
160 * Convenience function to extract the integer part of an ilwd:char ID
161 * string with some error checking. If either of ilwd_char_table_name or
162 * ilwd_char_column_name is not NULL, then the corresponding portion of the
163 * ilwd:char string must match it exactly. The return value is the
164 * recovered integer suffix or < 0 on failure.
165 */
166
167
169 const struct MetaioParseEnvironment *env,
170 int column_number,
171 const char *ilwd_char_table_name,
172 const char *ilwd_char_column_name
173)
174{
175 char *fmt;
176 const char *ilwd_char = env->ligo_lw.table.elt[column_number].data.lstring.data;
177 long long id;
178
179 /*
180 * 8 = 1 for the '\0', 2 for the ':' characters, and 5 for the
181 * "%%lld" string
182 */
183
184 fmt = malloc(strlen(ilwd_char_table_name ? ilwd_char_table_name : "%*[^:]") + strlen(ilwd_char_column_name ? ilwd_char_column_name : "%*[^:]") + 8);
185 if(!fmt)
187
188 sprintf(fmt, "%s:%s:%%lld", ilwd_char_table_name ? ilwd_char_table_name : "%*[^:]", ilwd_char_column_name ? ilwd_char_column_name : "%*[^:]");
189
190 if(sscanf(ilwd_char, fmt, &id) < 1) {
191 free(fmt);
192 XLALPrintError("%s(): invalid %s \"%s\" for %s\n", __func__, ilwd_char_column_name ? ilwd_char_column_name : "ID", ilwd_char, ilwd_char_table_name ? ilwd_char_table_name : "table");
194 }
195
196 free(fmt);
197
198 return id;
199}
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...
long long XLALLIGOLwParseIlwdChar(const struct MetaioParseEnvironment *env, int column_number, const char *ilwd_char_table_name, const char *ilwd_char_column_name)
Convenience function to extract the integer part of an ilwd:char ID string with some error checking.
int XLALLIGOLwHasTable(const char *filename, const char *table_name)
Test a LIGO Light Weight XML file for the presence of a specific table.
Definition: LIGOLwXMLRead.c:85
const char *const name
#define XLAL_ERROR(...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
XLAL_ENOMEM
XLAL_EDATA
XLAL_EIO
string id
filename