Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALFrame 3.0.7.1-8a6b96f
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
split.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_split lalfr-split
22 * @ingroup lalframe_programs
23 *
24 * @brief Split a frame file into component frames
25 *
26 * ### Synopsis
27 *
28 * lalfr-split [file [name]]
29 *
30 * ### Description
31 *
32 * The `lalfr-split` utility reads the given `file` containing multiple frames
33 * and breaks it up into files each containing one frame. If `file` is a
34 * single dash (`-`) or absent, `lalfr-split` reads from the standard input.
35 * If additional arguments are specified, the first is used as the name of the
36 * input file which is to be split. If a second additional argument is
37 * specified, it is used as a prefix for the names of the files into which
38 * the file is split. In this case, each file into which the file is split is
39 * named by the prefix followed by a lexically ordered suffix using two
40 * characters in the range `a-z` followed by the extension `.gwf`. If the name
41 * argument is not specified, the file is split into lexically ordered files
42 * named with the prefix `x` and with suffixes as above.
43 *
44 * ### Examples
45 *
46 * If `file.gwf` contains three frames then the command:
47 *
48 * lalfr-split file.gwf
49 *
50 * produces three files, `xaa.gwf`, `xab.gwf`, and `xac.gwf`, each containing
51 * one of the three frames.
52 *
53 * @sa @ref lalfr_cat
54 */
55
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <lal/LALFrameU.h>
60#include "utils.h"
61
62#define FAILURE(...) do { fprintf(stderr, __VA_ARGS__); exit(99); } while (0)
63
64char *mkfname(const char *name, size_t pos);
65
66int main(int argc, char *argv[])
67{
68 char stdio[] = "-";
69 char *fname = NULL;
70 char *basename = NULL;
71 LALFrameUFrFile *input;
72 LALFrameUFrTOC *toc;
73 size_t nframe;
74 size_t pos;
75
76 if (argc == 1) /* read from stdin */
77 fname = stdio;
78 if (argc == 2) /* read from argv[1] */
79 fname = argv[1];
80 if (argc == 3) { /* read from argv[1] with output prefix argv[2] */
81 fname = argv[1];
82 basename = argv[2];
83 }
84 if (argc > 3) {
85 fprintf(stderr, "usage: %s [file [name]]\n", argv[0]);
86 return 1;
87 }
88
89 input = XLALFrameUFrFileOpen(fname, "r");
90 if (!input)
91 FAILURE("file %s not found\n", fname);
92
93 toc = XLALFrameUFrTOCRead(input);
94 if (!toc)
95 FAILURE("no TOC found in file %s\n", fname);
96
97 nframe = XLALFrameUFrTOCQueryNFrame(toc);
98 if ((int)(nframe) <= 0)
99 FAILURE("no frames found in file %s\n", fname);
100
101 /* loop over frames in input file */
102 for (pos = 0; pos < nframe; ++pos) {
103 LALFrameUFrFile *output;
104 LALFrameUFrameH *frame;
105
106 fname = mkfname(basename, pos);
107 output = XLALFrameUFrFileOpen(fname, "w");
108 frame = framecpy(input, pos);
109
110 copydetectors(frame, input);
111 copychannels(frame, input, pos, NULL);
112
116 }
117
119 return 0;
120}
121
122char *mkfname(const char *name, size_t pos)
123{
124 static char fname[FILENAME_MAX];
125 const char *ext = "gwf";
126 char hi = 'a' + pos / 26;
127 char lo = 'a' + pos % 26;
128 snprintf(fname, sizeof(fname), "%s%c%c.%s", name ? name : "x", hi, lo,
129 ext);
130 return fname;
131}
#define fprintf
const char *const name
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
ext
int main(int argc, char *argv[])
Definition: split.c:66
char * mkfname(const char *name, size_t pos)
Definition: split.c:122
#define FAILURE(...)
Definition: split.c:62
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