Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALFrame 3.0.7.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
print.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Duncan Brown, Jolien Creighton, Robert Adam Mercer
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 lalfr_print lalfr-print
22 * @ingroup lalframe_programs
23 *
24 * @brief Prints channel data from frame files
25 *
26 * ### Synopsis
27 *
28 * lalfr-print [file ...]
29 *
30 * ### Description
31 *
32 * The `lalfr-print` utility reads the contents of the channels from each
33 * `file` and prints the data to the standard output. If `file` is a single
34 * dash (`-`) or absent, `lalfr-print` reads from the standard input.
35 *
36 * For each channel contained in `file`, the output is written in two-column
37 * format where the first column is the GPS time of each sample and the second
38 * column contains the corresponding sample values. The columns are separated
39 * by a tab character (`\t`) and each line is separated by a newline character
40 * (`\n`).
41 *
42 * Each channel in `file` is written sequentially and are separated by a line
43 * containing a separator consisting of the character `#` followed by the name
44 * of the next channel to be written. If more than one file argument is
45 * present then the separate files are processed sequentially and separated in
46 * the output by a line containing the separator `==> file <==` where `file` is
47 * the name of the current file being processed.
48 *
49 * ### Examples
50 *
51 * The command:
52 *
53 * lalfr-print file.gwf
54 *
55 * prints to standard output all the channels contained in `file.gwf`. If
56 * there are more than one channel present in that file, they can be split into
57 * separate files, each containing a single channel's data, with the command:
58 *
59 * lalfr-print file.gwf | awk '/^#/ { fn = $4 ".txt"; print > fn; next } { print >> fn }'
60 *
61 * and the resulting files are named after each of the channels in `file.gwf`
62 * with a `.txt` extension.
63 *
64 * @sa @ref lalfr_dump, @ref lalfr_fmt
65 */
66
67#include <inttypes.h>
68#include <math.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <lal/LALFrameU.h>
73
74#define FS "\t" /* field separator */
75#define RS "\n" /* record separator */
76
77#define FAILURE(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
78
79int printchannel(LALFrameUFrChan * channel, double x0, int fdom);
80int printval(void *data, size_t i, int dtype);
81
82int main(int argc, char *argv[])
83{
84 char stdio[] = "-";
85 char *defaultfilev[1] = { stdio };
86 int filec = (argc == 1 ? 1 : argc - 1);
87 char **filev = (argc == 1 ? defaultfilev : &argv[1]);
88 int f;
89
90 for (f = 0; f < filec; ++f) {
91 char *fname = filev[f];
92 LALFrameUFrFile *frfile;
93 LALFrameUFrTOC *toc;
94 size_t nadc;
95 size_t nsim;
96 size_t nproc;
97 size_t nframe;
98 size_t chan;
99 size_t pos;
100
101 frfile = XLALFrameUFrFileOpen(fname, "r");
102 if (!frfile)
103 FAILURE("file %s not found\n", fname);
104
105 if (f > 0)
106 printf("\n");
107 if (filec > 1)
108 printf("==> %s <==\n", fname);
109
110 toc = XLALFrameUFrTOCRead(frfile);
111 if (!toc)
112 FAILURE("no TOC found\n");
113
114 nframe = XLALFrameUFrTOCQueryNFrame(toc);
115 if (!toc)
116 FAILURE("no frames found\n");
117
118 nadc = XLALFrameUFrTOCQueryAdcN(toc);
119 nsim = XLALFrameUFrTOCQuerySimN(toc);
120 nproc = XLALFrameUFrTOCQueryProcN(toc);
121
122 for (chan = 0; chan < nadc; ++chan) {
123 LALFrameUFrChan *channel;
124 const char *name;
126 printf("# time (s)\t%s", name);
127 for (pos = 0; pos < nframe; ++pos) {
128 double tip;
129 double tfp;
130 tfp = XLALFrameUFrTOCQueryGTimeModf(&tip, toc, pos);
131 channel = XLALFrameUFrChanRead(frfile, name, pos);
132 if (pos == 0) {
133 const char *unit = XLALFrameUFrChanVectorQueryUnitY(channel);
134 if (unit && strlen(unit))
135 printf(" (%s)", unit);
136 printf("\n");
137 }
138 printchannel(channel, tip + tfp, 0);
140 }
141 }
142
143 for (chan = 0; chan < nsim; ++chan) {
144 LALFrameUFrChan *channel;
145 const char *name;
147 printf("# time (s)\t%s", name);
148 for (pos = 0; pos < nframe; ++pos) {
149 double tip;
150 double tfp;
151 tfp = XLALFrameUFrTOCQueryGTimeModf(&tip, toc, pos);
152 channel = XLALFrameUFrChanRead(frfile, name, pos);
153 if (pos == 0) {
154 const char *unit = XLALFrameUFrChanVectorQueryUnitY(channel);
155 if (unit && strlen(unit))
156 printf(" (%s)", unit);
157 printf("\n");
158 }
159 printchannel(channel, tip + tfp, 0);
161 }
162 }
163
164 for (chan = 0; chan < nproc; ++chan) {
165 LALFrameUFrChan *channel;
166 const char *name;
167 int fdom = 0;
169 for (pos = 0; pos < nframe; ++pos) {
170 double tip;
171 double tfp;
172 tfp = XLALFrameUFrTOCQueryGTimeModf(&tip, toc, pos);
173 channel = XLALFrameUFrChanRead(frfile, name, pos);
174 if (pos == 0) {
175 const char *unit;
177 if (unit && strcmp(unit, "s") == 0)
178 printf("# time (s)\t%s", name);
179 else if (unit && strcmp(unit, "s^-1") == 0) {
180 printf("# freq (s^-1)\t%s", name);
181 fdom = 1;
182 }
183 else if (unit && strlen(unit)) {
184 printf("# sample (%s)\t%s", unit, name);
185 fdom = 1;
186 }
187 else {
188 printf("# sample\t%s", name);
189 fdom = 1;
190 }
192 if (unit && strlen(unit))
193 printf(" (%s)", unit);
194 printf("\n");
195 }
196 if (fdom)
197 printchannel(channel, 0.0, fdom);
198 else
199 printchannel(channel, tip + tfp, 0);
201 }
202 }
203
205 XLALFrameUFrFileClose(frfile);
206 }
207
208 return 0;
209}
210
211int printchannel(LALFrameUFrChan * channel, double x0, int fdom)
212{
213 /* const char *name; */
214 double x0ip, x0fp;
215 double dx;
216 void *data;
217 int dtype;
218 size_t ndata;
219 size_t i;
220
221 if (!channel)
222 return -1;
223
225
226 /* name = XLALFrameUFrChanQueryName(channel); */
227 if (!fdom)
229
235
236 x0fp = modf(x0, &x0ip);
237 for (i = 0; i < ndata; ++i) {
238 double xip, xfp;
239 xfp = modf(x0fp + i * dx, &xip);
240 xip += x0ip;
241 printf("%ld.%09ld", (long)xip, (long)fabs(1e9 * xfp));
242 fputs(FS, stdout);
243 printval(data, i, dtype);
244 fputs(RS, stdout);
245 }
246
247 return 0;
248}
249
250int printval(void *data, size_t i, int dtype)
251{
252 switch (dtype) {
254 return printf("%c", ((char *)data)[i]);
256 return printf("%" PRIi16, ((int16_t *) data)[i]);
258 return printf("%" PRIi32, ((int32_t *) data)[i]);
260 return printf("%" PRIi64, ((int64_t *) data)[i]);
262 return printf("%" PRIu8, ((uint8_t *) data)[i]);
264 return printf("%" PRIu16, ((uint16_t *) data)[i]);
266 return printf("%" PRIu32, ((uint32_t *) data)[i]);
268 return printf("%" PRIu64, ((uint64_t *) data)[i]);
270 return printf("%e", (double)((float *)data)[i]);
272 return printf("%e", ((double *)data)[i]);
274 return printf("(%e,%e)", (double)((float *)data)[2 * i],
275 (double)((float *)data)[2 * i + 1]);
277 return printf("(%e,%e)", ((float *)data)[2 * i],
278 ((float *)data)[2 * i + 1]);
280 return printf("%s", ((char **)data)[i]);
281 default:
282 break;
283 }
284 return -1;
285}
const char *const name
const char * XLALFrameUFrTOCQuerySimName(const LALFrameUFrTOC *toc, size_t sim)
Query FrTOC structure for the name of a FrSimData structure.
Definition: LALFrameU.c:180
size_t XLALFrameUFrTOCQueryNFrame(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrameH structures contained.
Definition: LALFrameU.c:150
void * XLALFrameUFrChanVectorQueryData(const LALFrameUFrChan *channel)
Query FrChan structure for the data pointer in its FrVect structure.
Definition: LALFrameU.c:360
int XLALFrameUFrChanVectorQueryType(const LALFrameUFrChan *channel)
Query FrChan structure for the data type of its FrVect structure.
Definition: LALFrameU.c:355
void XLALFrameUFrTOCFree(LALFrameUFrTOC *toc)
Free a FrTOC structure.
Definition: LALFrameU.c:140
LALFrameUFrChan * XLALFrameUFrChanRead(LALFrameUFrFile *stream, const char *name, size_t pos)
Read a channel FrChan structure from a FrFile stream.
Definition: LALFrameU.c:285
size_t XLALFrameUFrTOCQuerySimN(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrSimData structures.
Definition: LALFrameU.c:175
void XLALFrameUFrChanFree(LALFrameUFrChan *channel)
Free a FrChan structure.
Definition: LALFrameU.c:280
int XLALFrameUFrChanVectorExpand(LALFrameUFrChan *channel)
Expands a FrVect structure within a FrChan structure.
Definition: LALFrameU.c:340
double XLALFrameUFrChanQueryTimeOffset(const LALFrameUFrChan *channel)
Query FrChan structure for time offset for this channel.
Definition: LALFrameU.c:310
LALFrameUFrTOC * XLALFrameUFrTOCRead(LALFrameUFrFile *stream)
Read the table of contents FrTOC structure for a FrFile stream.
Definition: LALFrameU.c:145
size_t XLALFrameUFrTOCQueryProcN(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrProcData structures.
Definition: LALFrameU.c:185
double XLALFrameUFrChanVectorQueryDx(const LALFrameUFrChan *channel, size_t dim)
Query FrChan structure for the sampling interval in the dim dimension of the multi-dimensional data i...
Definition: LALFrameU.c:385
const char * XLALFrameUFrTOCQueryProcName(const LALFrameUFrTOC *toc, size_t proc)
Query FrTOC structure for the name of a FrProcData structure.
Definition: LALFrameU.c:190
size_t XLALFrameUFrChanVectorQueryNData(const LALFrameUFrChan *channel)
Query FrChan structure for the number of points of data in its FrVect structure.
Definition: LALFrameU.c:370
double XLALFrameUFrTOCQueryGTimeModf(double *iptr, const LALFrameUFrTOC *toc, size_t pos)
Query FrTOC structure for start time of a FrameH structure.
Definition: LALFrameU.c:155
const char * XLALFrameUFrChanVectorQueryUnitY(const LALFrameUFrChan *channel)
Query FrChan structure for sample units of the data in the FrVect structure.
Definition: LALFrameU.c:400
const char * XLALFrameUFrTOCQueryAdcName(const LALFrameUFrTOC *toc, size_t adc)
Query FrTOC structure for the name of a FrAdcData structure.
Definition: LALFrameU.c:170
const char * XLALFrameUFrChanVectorQueryUnitX(const LALFrameUFrChan *channel, size_t dim)
Query FrChan structure for the units of the domain of the dim dimension of the multi-dimensional data...
Definition: LALFrameU.c:395
LALFrameUFrFile * XLALFrameUFrFileOpen(const char *filename, const char *mode)
Open a frame file FrFile stream.
Definition: LALFrameU.c:130
double XLALFrameUFrChanVectorQueryStartX(const LALFrameUFrChan *channel, size_t dim)
Query FrChan structure for the starting value of the dim dimension of the multi-dimensional data in t...
Definition: LALFrameU.c:390
struct tagLALFrameUFrTOC LALFrameUFrTOC
Incomplete type for a table of contents FrTOC structure.
Definition: LALFrameU.h:78
void XLALFrameUFrFileClose(LALFrameUFrFile *stream)
Close a FrFile stream.
Definition: LALFrameU.c:125
size_t XLALFrameUFrTOCQueryAdcN(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrAdcData structures.
Definition: LALFrameU.c:165
@ LAL_FRAMEU_FR_VECT_STRING
Id for string data type.
Definition: LALFrameU.h:166
@ LAL_FRAMEU_FR_VECT_16C
Id for 128-bit double precision complex data type.
Definition: LALFrameU.h:163
@ LAL_FRAMEU_FR_VECT_C
Id for 8-bit signed char data type.
Definition: LALFrameU.h:142
@ LAL_FRAMEU_FR_VECT_4R
Id for 32-bit single precision floating point data type.
Definition: LALFrameU.h:151
@ LAL_FRAMEU_FR_VECT_2S
Id for 16-bit signed integer data type.
Definition: LALFrameU.h:145
@ LAL_FRAMEU_FR_VECT_8S
Id for 64-bit signed integer data type.
Definition: LALFrameU.h:157
@ LAL_FRAMEU_FR_VECT_8U
Id for 64-bit unsigned integer data type.
Definition: LALFrameU.h:175
@ LAL_FRAMEU_FR_VECT_4S
Id for 32-bit signed integer data type.
Definition: LALFrameU.h:154
@ LAL_FRAMEU_FR_VECT_8C
Id for 64-bit single precision complex data type.
Definition: LALFrameU.h:160
@ LAL_FRAMEU_FR_VECT_1U
Id for 8-bit unsigned char data type.
Definition: LALFrameU.h:178
@ LAL_FRAMEU_FR_VECT_8R
Id for 64-bit double precision floating point data type.
Definition: LALFrameU.h:148
@ LAL_FRAMEU_FR_VECT_4U
Id for 32-bit unsigned integer data type.
Definition: LALFrameU.h:172
@ LAL_FRAMEU_FR_VECT_2U
Id for 16-bit unsigned integer data type.
Definition: LALFrameU.h:169
int main(int argc, char *argv[])
Definition: print.c:82
#define FS
Definition: print.c:74
int printchannel(LALFrameUFrChan *channel, double x0, int fdom)
Definition: print.c:211
int printval(void *data, size_t i, int dtype)
Definition: print.c:250
#define FAILURE(...)
Definition: print.c:77
#define RS
Definition: print.c:75
char * channel
Definition: stream.c:108