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
time_slide.c
Go to the documentation of this file.
1/*
2 * time_slide.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 TimeSlide structure.
39 */
41 void
42)
43{
44 TimeSlide *new = XLALMalloc(sizeof(*new));
45
46 if(!new)
48
49 new->next = NULL;
50 new->process_id = -1;
51 new->time_slide_id = -1;
52 memset(new->instrument, 0, sizeof(new->instrument));
53 new->offset = 0;
54
55 return new;
56}
57
58
59/**
60 * Destroy a TimeSlide structure.
61 */
63 TimeSlide *row
64)
65{
66 TimeSlide *next = row ? row->next : NULL;
67 XLALFree(row);
68 return next;
69}
70
71
72/**
73 * Destroy a TimeSlide linked list.
74 */
76 TimeSlide *head
77)
78{
79 while(head)
80 head = XLALDestroyTimeSlide(head);
81}
82
83
84/**
85 * Find and return the address of the first element in the linked list of
86 * TimeSlide objects whose time_slide_id and instrument name equal the
87 * values given. TimeSlide elements whose instrument pointer is NULL are
88 * skipped. Returns NULL if no matching row is found. This version is for
89 * a linked list of const pointers, and returns a const pointer. See also
90 * XLALTimeSlideGetByIDAndInstrument().
91 */
93 const TimeSlide *time_slide,
94 long time_slide_id,
95 const char *instrument
96)
97{
98 for(; time_slide && (time_slide->time_slide_id != time_slide_id || strcmp(time_slide->instrument, instrument)); time_slide = time_slide->next);
99 return time_slide;
100}
101
102
103/**
104 * Find and return the address of the first element in the linked list of
105 * TimeSlide objects whose time_slide_id and instrument name equal the
106 * values given. TimeSlide elements whose instrument pointer is NULL are
107 * skipped. Returns NULL if no matching row is found. This version is for
108 * a linked list of non-const pointers, and returns a non-const pointer.
109 * See also XLALTimeSlideConstGetByIDAndInstrument(). NOTE: neither
110 * version modifies the TimeSlide rows; the two versions are identical,
111 * they are provided to allow the const'ness to be passed through the
112 * function.
113 */
115 TimeSlide *time_slide,
116 long time_slide_id,
117 const char *instrument
118)
119{
120 for(; time_slide && (time_slide->time_slide_id != time_slide_id || strcmp(time_slide->instrument, instrument)); time_slide = time_slide->next);
121 return time_slide;
122}
123
124
125/**
126 * Read the time_slide table from a LIGO Light Weight XML file into a
127 * linked list of TimeSlide structures.
128 */
130 const char *filename
131)
132{
133 static const char table_name[] = "time_slide";
134 int miostatus;
135 TimeSlide *head = NULL;
136 TimeSlide **next = &head;
137 struct MetaioParseEnvironment env;
138 struct {
139 int process_id;
140 int time_slide_id;
141 int instrument;
142 int offset;
143 } column_pos;
144
145 /* open the file and find table */
146
147 if(MetaioOpenFile(&env, filename)) {
148 XLALPrintError("%s(): error opening \"%s\": %s\n", __func__, filename, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
150 }
151 if(MetaioOpenTableOnly(&env, table_name)) {
152 MetaioAbort(&env);
153 XLALPrintError("%s(): cannot find %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
155 }
156
157 /* find columns */
158
160 column_pos.process_id = XLALLIGOLwFindColumn(&env, "process_id", METAIO_TYPE_INT_8S, 1);
161 column_pos.time_slide_id = XLALLIGOLwFindColumn(&env, "time_slide_id", METAIO_TYPE_INT_8S, 1);
162 column_pos.instrument = XLALLIGOLwFindColumn(&env, "instrument", METAIO_TYPE_LSTRING, 1);
163 column_pos.offset = XLALLIGOLwFindColumn(&env, "offset", METAIO_TYPE_REAL_8, 1);
164
165 /* check for failure (== a required column is missing) */
166
167 if(XLALGetBaseErrno()) {
168 MetaioAbort(&env);
169 XLALPrintError("%s(): failure reading %s table\n", __func__, table_name);
171 }
172
173 /* loop over the rows in the file */
174
175 while((miostatus = MetaioGetRow(&env)) > 0) {
176 /* create a new row */
177
179
180 if(!row) {
182 MetaioAbort(&env);
184 }
185
186 /* append to linked list */
187
188 *next = row;
189 next = &(*next)->next;
190
191 /* populate the columns */
192
193 row->process_id = env.ligo_lw.table.elt[column_pos.process_id].data.int_8s;
194 row->time_slide_id = env.ligo_lw.table.elt[column_pos.time_slide_id].data.int_8s;
195 strncpy(row->instrument, env.ligo_lw.table.elt[column_pos.instrument].data.lstring.data, sizeof(row->instrument) - 1);
196 row->offset = env.ligo_lw.table.elt[column_pos.offset].data.real_8;
197 }
198 if(miostatus < 0) {
200 MetaioAbort(&env);
201 XLALPrintError("%s(): I/O error parsing %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
203 }
204
205 /* close file */
206
207 if(MetaioClose(&env)) {
209 XLALPrintError("%s(): error parsing document after %s table: %s\n", __func__, table_name, env.mierrmsg.data ? env.mierrmsg.data : "unknown reason");
211 }
212
213 /* done */
214
215 return head;
216}
217
218
219/**
220 * Write a time_slide table to an XML file.
221 */
223 LIGOLwXMLStream *xml,
224 const TimeSlide *time_slide
225)
226{
227 const char *row_head = "\n\t\t\t";
228
229 /* table header */
230
232 XLALFilePuts("\t<Table Name=\"time_slide:table\">\n", xml->fp);
233 XLALFilePuts("\t\t<Column Name=\"process:process_id\" Type=\"int_8s\"/>\n", xml->fp);
234 XLALFilePuts("\t\t<Column Name=\"time_slide_id\" Type=\"int_8s\"/>\n", xml->fp);
235 XLALFilePuts("\t\t<Column Name=\"instrument\" Type=\"lstring\"/>\n", xml->fp);
236 XLALFilePuts("\t\t<Column Name=\"offset\" Type=\"real_8\"/>\n", xml->fp);
237 XLALFilePuts("\t\t<Stream Name=\"time_slide:table\" Type=\"Local\" Delimiter=\",\">", xml->fp);
238 if(XLALGetBaseErrno())
240
241 /* rows */
242
243 for(; time_slide; time_slide = time_slide->next) {
244 if(XLALFilePrintf(xml->fp, "%s%ld,%ld,\"%s\",%.16g", row_head, time_slide->process_id, time_slide->time_slide_id, time_slide->instrument, time_slide->offset) < 0)
246 row_head = ",\n\t\t\t";
247 }
248
249 /* table footer */
250
251 if(XLALFilePuts("\n\t\t</Stream>\n\t</Table>\n", xml->fp) < 0)
253
254 /* done */
255
256 return 0;
257}
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(...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
int XLALClearErrno(void)
XLAL_EFUNC
XLAL_EIO
filename
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
This structure corresponds to one row of a time_slide table.
CHAR instrument[LIGOMETA_STRING_MAX]
struct tagTimeSlide * next
TimeSlide * XLALCreateTimeSlide(void)
Create a TimeSlide structure.
Definition: time_slide.c:40
const TimeSlide * XLALTimeSlideConstGetByIDAndInstrument(const TimeSlide *time_slide, long time_slide_id, const char *instrument)
Find and return the address of the first element in the linked list of TimeSlide objects whose time_s...
Definition: time_slide.c:92
TimeSlide * XLALTimeSlideGetByIDAndInstrument(TimeSlide *time_slide, long time_slide_id, const char *instrument)
Find and return the address of the first element in the linked list of TimeSlide objects whose time_s...
Definition: time_slide.c:114
TimeSlide * XLALDestroyTimeSlide(TimeSlide *row)
Destroy a TimeSlide structure.
Definition: time_slide.c:62
TimeSlide * XLALTimeSlideTableFromLIGOLw(const char *filename)
Read the time_slide table from a LIGO Light Weight XML file into a linked list of TimeSlide structure...
Definition: time_slide.c:129
int XLALWriteLIGOLwXMLTimeSlideTable(LIGOLwXMLStream *xml, const TimeSlide *time_slide)
Write a time_slide table to an XML file.
Definition: time_slide.c:222
void XLALDestroyTimeSlideTable(TimeSlide *head)
Destroy a TimeSlide linked list.
Definition: time_slide.c:75