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
cut.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_cut lalfr-cut
22 * @ingroup lalframe_programs
23 *
24 * @brief Cut channels from frames in frame files
25 *
26 * ### Synopsis
27 *
28 * lalfr-cut list [file ...]
29 *
30 * ### Description
31 *
32 * The `lalfr-cut` utility cuts out selected channels (as specified by `list`)
33 * from each `file` and writes frames containing these channels only to the
34 * standard output. If no `file` argument is specified, or if a file argument
35 * is a single dash (`-`), `lalfr-cut` reads from the standard input. The
36 * channels specified by list are comma-delimited channel names.
37 *
38 * ### Example
39 *
40 * To cut the channels `chan1` and `chan2` from file `framefile.gwf` and output
41 * frames to standard output:
42 *
43 * lalfr-cut chan1,chan2 framefile.gwf
44 *
45 * @sa @ref lalfr_paste
46 */
47
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <lal/LALMalloc.h>
52#include <lal/LALString.h>
53#include <lal/LALFrameU.h>
54#include "utils.h"
55
56#define FAILURE(...) do { fprintf(stderr, __VA_ARGS__); exit(99); } while (0)
57
58char **chanvalloc(const char *str);
59void chanvfree(char **chanv);
60char *chanvstr(char **chanv, const char *str);
61
62int main(int argc, char *argv[])
63{
64 char stdio[] = "-";
65 char *defaultfilev[1] = { stdio };
66 int filec = (argc == 2 ? 1 : argc - 2);
67 char **filev = (argc == 2 ? defaultfilev : &argv[2]);
68 char **chanv;
69 int f;
70 LALFrameUFrFile *output;
71
72 if (argc == 1) {
73 fprintf(stderr, "usage: %s list [file ...]\n", argv[0]);
74 return 1;
75 }
76
77 if (argc == 2) {
78 filec = 1;
79 filev = defaultfilev;
80 } else {
81 filec = argc - 2;
82 filev = &argv[2];
83 }
84
85 /* convert list of channel names to a vector of channel names */
86 chanv = chanvalloc(argv[1]);
87
88 output = XLALFrameUFrFileOpen(NULL, "w");
89 if (!output)
90 FAILURE("could not create output file\n");
91
92 for (f = 0; f < filec; ++f) {
93 char *fname = filev[f];
94 LALFrameUFrFile *input;
95 LALFrameUFrTOC *toc;
96 size_t nframe;
97 size_t pos;
98
99 input = XLALFrameUFrFileOpen(fname, "r");
100 if (!input)
101 FAILURE("file %s not found\n", fname);
102
103 toc = XLALFrameUFrTOCRead(input);
104 if (!toc)
105 FAILURE("no TOC found in file %s\n", fname);
106
107 nframe = XLALFrameUFrTOCQueryNFrame(toc);
108 if ((int)(nframe) <= 0)
109 FAILURE("no frames found in file %s\n", fname);
110
111 /* loop over frames in input file */
112 for (pos = 0; pos < nframe; ++pos) {
113 LALFrameUFrameH *frame;
114 char **chanp;
115
116 frame = framecpy(input, pos);
117 copydetectors(frame, input);
118 for (chanp = chanv; *chanp; ++chanp)
119 copychannels(frame, input, pos, *chanp);
122 }
123
125 }
126
128 chanvfree(chanv);
129 return 0;
130}
131
132/* creates a vector of channel names from a comma-delimited channel list */
133char **chanvalloc(const char *str)
134{
135 char *s = XLALStringDuplicate(str);
136 char **chanv;
137 int chanc = 1;
138 int i;
139 for (; (str = strchr(str, ',')); ++str)
140 ++chanc;
141 chanv = calloc(chanc + 1, sizeof(*chanv));
142 if (chanc > 1) {
143 chanv[0] = XLALStringDuplicate(strtok(s, ","));
144 for (i = 1; i < chanc; ++i)
145 chanv[i] = XLALStringDuplicate(strtok(NULL, ","));
146 XLALFree(s);
147 } else
148 chanv[0] = s;
149 return chanv;
150}
151
152/* frees a vector of channel names */
153void chanvfree(char **chanv)
154{
155 if (chanv) {
156 char **p;
157 for (p = chanv; *p; ++p)
158 XLALFree(*p);
159 free(chanv);
160 }
161 return;
162}
163
164/* seeks the string str in a vector of channel names;
165 * returns NULL if not found */
166char *chanvstr(char **chanv, const char *str)
167{
168 while (*chanv && strcmp(*chanv, str))
169 ++chanv;
170 return *chanv;
171}
#define fprintf
int main(int argc, char *argv[])
Definition: cut.c:62
#define FAILURE(...)
Definition: cut.c:56
void chanvfree(char **chanv)
Definition: cut.c:153
char * chanvstr(char **chanv, const char *str)
Definition: cut.c:166
char ** chanvalloc(const char *str)
Definition: cut.c:133
size_t XLALFrameUFrTOCQueryNFrame(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrameH structures contained.
Definition: LALFrameU.c:150
int XLALFrameUFrameHWrite(LALFrameUFrFile *stream, LALFrameUFrameH *frame)
Write a FrameH structure to an output FrFile stream.
Definition: LALFrameU.c:220
void XLALFrameUFrameHFree(LALFrameUFrameH *frame)
Free a FrameH structure.
Definition: LALFrameU.c:205
struct tagLALFrameUFrameH LALFrameUFrameH
Incomplete type for a frame header FrameH structure.
Definition: LALFrameU.h:64
LALFrameUFrTOC * XLALFrameUFrTOCRead(LALFrameUFrFile *stream)
Read the table of contents FrTOC structure for a FrFile stream.
Definition: LALFrameU.c:145
LALFrameUFrFile * XLALFrameUFrFileOpen(const char *filename, const char *mode)
Open a frame file FrFile stream.
Definition: LALFrameU.c:130
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
void XLALFree(void *p)
char char * XLALStringDuplicate(const char *s)
p
void output(int gps_sec, int output_type)
LALFrameUFrameH * framecpy(LALFrameUFrFile *frfile, size_t pos)
Definition: utils.c:26
int copydetectors(LALFrameUFrameH *frame, LALFrameUFrFile *frfile)
Definition: utils.c:58
int copychannels(LALFrameUFrameH *frame, LALFrameUFrFile *frfile, size_t pos, const char *match)
Definition: utils.c:121