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
stat.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_stat lalfr-stat
22 * @ingroup lalframe_programs
23 *
24 * @brief Briefly describe frame files
25 *
26 * ### Synopsis
27 *
28 * lalfr-stat [file ...]
29 *
30 * ### Description
31 *
32 * The `lalfr-stat` utility displays information about the file pointed to by
33 * `file`. If `file` is a single dash (`-`) or absent, `lalfr-stat` reads from
34 * the standard input.
35 *
36 * For each `file`, `lalfr-stat` writes to standard output a line containing
37 * five columns. The first column contains the detector site information for
38 * all detector structures contained in `file`. The second column contains a
39 * file description that is parsed from the file name. The third column
40 * contains the GPS second that is equal to or just before the beginning of the
41 * data contained in `file`. The fourth column contains an integer duration in
42 * seconds such that the sum of columns three and four gives a GPS time after
43 * the data contained in the file. The fifth column contains the URL of the
44 * file.
45 *
46 * If any of the above data is missing or unknown, the value in the column is
47 * given by `-`.
48 *
49 * ### Example
50 *
51 * The command:
52 *
53 * lalfr-stat *.gwf > cache
54 *
55 * will produce a standard LAL cache file `cache` describing all the frames in
56 * the current directory.
57 */
58
59#define _GNU_SOURCE /* for realpath() */
60#include <config.h>
61#include <stdlib.h>
62
63#include <ctype.h>
64#include <math.h>
65#include <stdio.h>
66#include <string.h>
67#include <lal/LALFrameU.h>
68
69#ifndef PATH_MAX
70#define PATH_MAX FILENAME_MAX
71#endif
72
73#define H0 2.3e-18 /* Hubble constant, s */
74#define SITE_MAX 26 /* in our dreams... */
75
76#define FAILURE(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
77
78int charcmp(const void *c1, const void *c2);
79char *fnametodsc(const char *fname);
80
81int main(int argc, char *argv[])
82{
83 char stdio[] = "-";
84 char *defaultfilev[1] = { stdio };
85 int filec = (argc == 1 ? 1 : argc - 1);
86 char **filev = (argc == 1 ? defaultfilev : &argv[1]);
87 int f;
88
89 for (f = 0; f < filec; ++f) {
90 char *fname = filev[f];
91 char *dsc;
92 LALFrameUFrFile *frfile;
93 LALFrameUFrTOC *toc;
94 char sites[SITE_MAX + 1] = "";
95 char path[PATH_MAX + 1] = "";
96 size_t ndet;
97 size_t nadc;
98 size_t nsim;
99 size_t nproc;
100 size_t nframe;
101 size_t pos;
102 size_t det;
103 double t0min = +1.0 / H0; /* ridiculously far in future */
104 double t1max = -1.0 / H0; /* ridiculously far in past */
105 int dt;
106
107 frfile = XLALFrameUFrFileOpen(fname, "r");
108 if (!frfile)
109 FAILURE("file %s not found\n", fname);
110
111 toc = XLALFrameUFrTOCRead(frfile);
112 if (!toc)
113 FAILURE("no TOC found\n");
114
115 nframe = XLALFrameUFrTOCQueryNFrame(toc);
116 if ((int)(nframe) <= 0)
117 FAILURE("no frames found\n");
118
120 nadc = XLALFrameUFrTOCQueryAdcN(toc);
121 nsim = XLALFrameUFrTOCQuerySimN(toc);
122 nproc = XLALFrameUFrTOCQueryProcN(toc);
123
124 for (det = 0; det < ndet; ++det) {
125 LALFrameUFrDetector *detector;
126 const char *prefix;
127 const char *name;
129 detector = XLALFrameUFrDetectorRead(frfile, name);
130 prefix = XLALFrameUFrDetectorQueryPrefix(detector);
131 /* add site if it is new */
132 if (prefix && isupper(*prefix))
133 if (strchr(sites, *prefix) == NULL)
134 strncat(sites, prefix, 1);
135 XLALFrameUFrDetectorFree(detector);
136 }
137 /* sort letters in site string */
138 if (*sites)
139 qsort(sites, strlen(sites), sizeof(*sites), charcmp);
140 else
141 strcpy(sites, "-");
142
143 for (pos = 0; pos < nframe; ++pos) {
144 LALFrameUFrChan *channel;
145 double tip;
146 double tfp;
147 double t0;
148 double t1;
149 size_t chan;
150 tfp = XLALFrameUFrTOCQueryGTimeModf(&tip, toc, pos);
151 t0 = tip + tfp;
152 t1 = t0 + XLALFrameUFrTOCQueryDt(toc, pos);
153 if (t0 < t0min)
154 t0min = t0;
155 if (t1 > t1max)
156 t1max = t1;
157
158 for (chan = 0; chan < nadc; ++chan) {
159 const char *name;
160 double toff;
162 channel = XLALFrameUFrChanRead(frfile, name, pos);
163 if (!channel)
164 continue;
166 if (t0 + toff < t0min)
167 t0min = t0 + toff;
168 if (t1 + toff > t1max)
169 t1max = t1 + toff;
171 }
172
173 for (chan = 0; chan < nsim; ++chan) {
174 const char *name;
175 double toff;
177 channel = XLALFrameUFrChanRead(frfile, name, pos);
178 if (!channel)
179 continue;
181 if (t0 + toff < t0min)
182 t0min = t0 + toff;
183 if (t1 + toff > t1max)
184 t1max = t1 + toff;
186 }
187
188 for (chan = 0; chan < nproc; ++chan) {
189 const char *name;
190 double toff;
192 channel = XLALFrameUFrChanRead(frfile, name, pos);
193 if (!channel)
194 continue;
196 if (t0 + toff < t0min)
197 t0min = t0 + toff;
198 if (t1 + toff > t1max)
199 t1max = t1 + toff;
201 }
202 }
203
204 dsc = fnametodsc(fname);
205 if (!realpath(fname, path))
206 strcpy(path, "-");
207 dt = ceil(t1max) - floor(t0min);
208 printf("%s", sites);
209 printf("\t%s", dsc);
210 printf("\t%d", (int)floor(t0min));
211 printf("\t%d", dt > 0 ? dt : 1);
212 if (strcmp(path, "-") == 0)
213 printf("\t-");
214 else
215 printf("\tfile://%s", path);
216 printf("\n");
217
218 free(dsc);
220 XLALFrameUFrFileClose(frfile);
221 }
222
223 return 0;
224}
225
226int charcmp(const void *c1, const void *c2)
227{
228 char a = *((const char *)c1);
229 char b = *((const char *)c2);
230 return (a > b) - (a < b);
231}
232
233char *fnametodsc(const char *fname)
234{
235 if (fname && *fname && strcmp(fname, "-") && strlen(fname) < FILENAME_MAX) {
236 char src[FILENAME_MAX];
237 char dsc[FILENAME_MAX];
238 const char *base;
239 char *s;
240 unsigned int t0;
241 unsigned int dt;
242 int n;
243
244 /* get basename */
245 base = strrchr(fname, '/');
246 base = base ? base + 1 : fname;
247
248 /* see if basename is in canonical form */
249 n = sscanf(base, "%[A-Z]-%[a-zA-Z0-9_+#]-%u-%u.gwf", src, dsc, &t0,
250 &dt);
251 if (n == 4) /* correct number of conversions */
252 return strdup(dsc); /* return description field */
253
254 /* try to convert the full basename */
255 base = strrchr(fname, '/');
256 base = base ? base + 1 : fname;
257
258 /* translate spaces & hyphens to octothorpes & underscores */
259 s = dsc;
260 while (*base && *base != '.')
261 switch (*base) {
262 case ' ':
263 *s++ = '#';
264 ++base;
265 break;
266 case '-':
267 *s++ = '_';
268 ++base;
269 break;
270 default:
271 *s++ = *base++;
272 break;
273 }
274 *s = '\0';
275 if (strlen(dsc))
276 return strdup(dsc);
277 }
278
279 /* all else fails... */
280 return strdup("-");
281}
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 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
const char * XLALFrameUFrDetectorQueryPrefix(const LALFrameUFrDetector *detector)
Query FrDetector structure for the detector prefix.
Definition: LALFrameU.c:453
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
const char * XLALFrameUFrTOCQueryProcName(const LALFrameUFrTOC *toc, size_t proc)
Query FrTOC structure for the name of a FrProcData structure.
Definition: LALFrameU.c:190
double XLALFrameUFrTOCQueryGTimeModf(double *iptr, const LALFrameUFrTOC *toc, size_t pos)
Query FrTOC structure for start time of a FrameH structure.
Definition: LALFrameU.c:155
void XLALFrameUFrDetectorFree(LALFrameUFrDetector *detector)
Free a FrDetector structure.
Definition: LALFrameU.c:430
const char * XLALFrameUFrTOCQueryAdcName(const LALFrameUFrTOC *toc, size_t adc)
Query FrTOC structure for the name of a FrAdcData structure.
Definition: LALFrameU.c:170
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
double XLALFrameUFrTOCQueryDt(const LALFrameUFrTOC *toc, size_t pos)
Query FrTOC structure for duration of a FrameH structure.
Definition: LALFrameU.c:160
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
size_t XLALFrameUFrTOCQueryDetectorN(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrDetector structures.
Definition: LALFrameU.c:195
LALFrameUFrDetector * XLALFrameUFrDetectorRead(LALFrameUFrFile *stream, const char *name)
Read a detector FrDetector structure from a FrFile stream.
Definition: LALFrameU.c:435
const char * XLALFrameUFrTOCQueryDetectorName(const LALFrameUFrTOC *toc, size_t det)
Query FrTOC structure for the name of a FrDetector structure.
Definition: LALFrameU.c:200
static const INT4 a
path
src
int main(int argc, char *argv[])
Definition: stat.c:81
#define H0
Definition: stat.c:73
#define SITE_MAX
Definition: stat.c:74
#define FAILURE(...)
Definition: stat.c:76
char * fnametodsc(const char *fname)
Definition: stat.c:233
int charcmp(const void *c1, const void *c2)
Definition: stat.c:226
#define PATH_MAX
Definition: stat.c:70
double dt
Definition: stream.c:110
double t0
Definition: stream.c:109
char * channel
Definition: stream.c:108