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
LALFrameIO.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2013 Jolien Creighton
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#include <config.h>
21
22#ifdef HAVE_UNISTD_H
23#define _GNU_SOURCE /* for gethostname() */
24#include <unistd.h>
25#endif
26
27#include <ctype.h>
28#include <math.h>
29#include <stdio.h>
30#include <string.h>
31
32#include <lal/LALDatatypes.h>
33#include <lal/LALDetectors.h>
34#include <lal/LALString.h>
35#include <lal/TimeSeries.h>
36#include <lal/FrequencySeries.h>
37#include <lal/Units.h>
38#include <lal/Date.h>
39
40#include <lal/LALFrameU.h>
41#include <lal/LALFrameIO.h>
42
43#ifndef HAVE_LOCALTIME_R
44#define localtime_r(timep, result) memcpy((result), localtime(timep), sizeof(struct tm))
45#endif
46
47/** @cond */
48struct tagLALFrFile {
49 LALFrameUFrFile *file;
50 LALFrameUFrTOC *toc;
51};
52/** @endcond */
53
55{
56 if (frfile) {
57 if (frfile->file) {
58 XLALFrameUFrFileClose(frfile->file);
59 frfile->file = NULL;
60 }
61 if (frfile->toc) {
62 XLALFrameUFrTOCFree(frfile->toc);
63 frfile->toc = NULL;
64 }
65 LALFree(frfile);
66 }
67 return 0;
68}
69
71{
72 LALFrFile *frfile;
73 char prot[FILENAME_MAX] = "";
74 char host[FILENAME_MAX] = "";
75 char path[FILENAME_MAX] = "";
76 int n;
77
79 XLAL_CHECK_NULL(strlen(url) < FILENAME_MAX, XLAL_EBADLEN,
80 "url %s is too long", url);
81
82 n = sscanf(url, "%[^:]://%[^/]%[^\t\n]", prot, host, path);
83 if (n != 3) { /* perhaps the hostname has been omitted */
84 XLALStringCopy(host, "localhost", sizeof(host));
85 if (n != 2) { /* assume the whole thing is a file path */
86 XLALStringCopy(prot, "file", sizeof(prot));
87 XLALStringCopy(path, url, sizeof(path));
88 }
89 }
90
91 /* only file urls are supported at this time */
92 if (strcmp(prot, "file")) /* not a file url */
93 XLAL_ERROR_NULL(XLAL_EINVAL, "Unsupported protocol %s", prot);
94
95 /* only files on localhost are supported at this time */
96 if (strcmp(host, "localhost")) { /* not explicitly localhost */
97 /* make sure the host *is* the localhost */
98 char localhost[FILENAME_MAX];
99 gethostname(localhost, FILENAME_MAX - 1);
100 if (strcmp(host, localhost)) /* not localhost */
101 XLAL_ERROR_NULL(XLAL_EINVAL, "Cannot read files from host %s",
102 host);
103 }
104
105 /* open frame file in read mode */
106 /*
107 * frfile = XLALFrameUFrFileOpen(path, "r");
108 * if (!frfile)
109 * XLAL_ERROR_NULL(XLAL_EIO, "Could not open frame file %s", path);
110 */
111 frfile = LALMalloc(sizeof(*frfile));
112 if (!frfile)
114 frfile->file = XLALFrameUFrFileOpen(path, "r");
115 if (!frfile->file) {
116 LALFree(frfile);
117 XLAL_ERROR_NULL(XLAL_EIO, "Could not open frame file %s", path);
118 }
119 frfile->toc = XLALFrameUFrTOCRead(frfile->file);
120 if (!frfile->toc) {
121 XLALFrameUFrFileClose(frfile->file);
122 LALFree(frfile);
123 XLAL_ERROR_NULL(XLAL_EIO, "Could not open TOC for frame file %s",
124 path);
125 }
126
127 return frfile;
128}
129
130size_t XLALFrFileQueryNFrame(const LALFrFile * frfile)
131{
132 return XLALFrameUFrTOCQueryNFrame(frfile->toc);
133}
134
136 const LALFrFile * frfile, size_t pos)
137{
138 double ip, fp; /* integer part and fraction part */
139 fp = XLALFrameUFrTOCQueryGTimeModf(&ip, frfile->toc, pos);
140 return XLALGPSSet(start, ip, XLAL_BILLION_REAL8 * fp);
141}
142
143double XLALFrFileQueryDt(const LALFrFile * frfile, size_t pos)
144{
145 return XLALFrameUFrTOCQueryDt(frfile->toc, pos);
146}
147
149 const char *chname, size_t pos)
150{
151 LALFrameUFrChan *channel;
152 int type;
153 channel = XLALFrameUFrChanRead(frfile->file, chname, pos);
154 if (!channel)
158 switch (type) {
160 return LAL_CHAR_TYPE_CODE;
162 return LAL_I2_TYPE_CODE;
164 return LAL_D_TYPE_CODE;
166 return LAL_S_TYPE_CODE;
168 return LAL_I4_TYPE_CODE;
170 return LAL_I8_TYPE_CODE;
172 return LAL_C_TYPE_CODE;
174 return LAL_Z_TYPE_CODE;
176 return LAL_U2_TYPE_CODE;
178 return LAL_U4_TYPE_CODE;
180 return LAL_U8_TYPE_CODE;
182 return LAL_UCHAR_TYPE_CODE;
185 "No LAL typecode equivalent to FrVect type STRING");
186 default:
187 XLAL_ERROR(XLAL_ETYPE, "Unrecognized FrVect type %d", type);
188 }
189 return -1; /* never get here anyway... */
190}
191
193 const char *chname, size_t pos)
194{
195 LALFrameUFrChan *channel;
196 size_t length;
197 channel = XLALFrameUFrChanRead(frfile->file, chname, pos);
198 if (!channel)
202 return length;
203}
204
206{
207 int result;
208 /* this process might mess up the TOC so need to reread it afterwards */
209 XLALFrameUFrTOCFree(frfile->toc);
210 result = XLALFrameUFileCksumValid(frfile->file);
211 frfile->toc = XLALFrameUFrTOCRead(frfile->file);
212 return result;
213}
214
215#define TDOM 1
216#define FDOM 2
217
218#define DOM TDOM
219
220#define TYPE INT2
221#define VEXT 2S
222#include "LALFrameIO_source.c"
223#undef VEXT
224#undef TYPE
225
226#define TYPE INT4
227#define VEXT 4S
228#include "LALFrameIO_source.c"
229#undef VEXT
230#undef TYPE
231
232#define TYPE INT8
233#define VEXT 8S
234#include "LALFrameIO_source.c"
235#undef VEXT
236#undef TYPE
237
238#define TYPE UINT2
239#define VEXT 2U
240#include "LALFrameIO_source.c"
241#undef VEXT
242#undef TYPE
243
244#define TYPE UINT4
245#define VEXT 4U
246#include "LALFrameIO_source.c"
247#undef VEXT
248#undef TYPE
249
250#define TYPE UINT8
251#define VEXT 8U
252#include "LALFrameIO_source.c"
253#undef VEXT
254#undef TYPE
255
256#define TYPE REAL4
257#define VEXT 4R
258#include "LALFrameIO_source.c"
259#undef VEXT
260#undef TYPE
261
262#define TYPE REAL8
263#define VEXT 8R
264#include "LALFrameIO_source.c"
265#undef VEXT
266#undef TYPE
267
268#define TYPE COMPLEX8
269#define VEXT 8C
270#include "LALFrameIO_source.c"
271#undef VEXT
272#undef TYPE
273
274#define TYPE COMPLEX16
275#define VEXT 16C
276#include "LALFrameIO_source.c"
277#undef VEXT
278#undef TYPE
279
280#undef DOM
281#define DOM FDOM
282
283#define TYPE REAL4
284#define VEXT 4R
285#include "LALFrameIO_source.c"
286#undef VEXT
287#undef TYPE
288
289#define TYPE REAL8
290#define VEXT 8R
291#include "LALFrameIO_source.c"
292#undef VEXT
293#undef TYPE
294
295#define TYPE COMPLEX8
296#define VEXT 8C
297#include "LALFrameIO_source.c"
298#undef VEXT
299#undef TYPE
300
301#define TYPE COMPLEX16
302#define VEXT 16C
303#include "LALFrameIO_source.c"
304#undef VEXT
305#undef TYPE
306
307#undef DOM
308#undef TDOM
309#undef FDOM
310
311int XLALFrameAddFrHistory(LALFrameH * frame, const char *name,
312 const char *comment)
313{
314 LALFrameUFrHistory *history = NULL;
316
317 /* get current time */
318 if (!XLALGPSTimeNow(&now))
320
321 history = XLALFrameUFrHistoryAlloc(name, now.gpsSeconds, comment);
322 if (!history)
324
325 if (XLALFrameUFrameHFrHistoryAdd(frame, history)) {
328 }
329
331 return 0;
332}
333
334/* determine local seasonal time - UTC (seconds) */
335static int XLALLocalTime(char site, int gpssec)
336{
337 struct tm utc;
338 struct tm loc;
339 time_t tutc;
340 time_t tloc;
341 const char *zone;
342 char *orig = NULL;
343
344 XLALGPSToUTC(&utc, gpssec);
345 tutc = XLALSecondsSinceUnixEpoch(&utc);
346
347 switch (site) {
348 case 'G':
349 zone = "Europe/Berlin";
350 break;
351 case 'H':
352 zone = "PST8PDT";
353 break;
354 case 'L':
355 zone = "CST6CDT";
356 break;
357 case 'T':
358 zone = "Japan";
359 break;
360 case 'V':
361 zone = "Europe/Rome";
362 break;
363 default: /* use UTC */
364 return 0;
365 }
366 orig = getenv("TZ");
367 if (orig)
368 orig = strdup(orig);
369 setenv("TZ", zone, 1);
370 tzset();
371
372 localtime_r(&tutc, &loc);
373 tloc = XLALSecondsSinceUnixEpoch(&loc);
374
375 if (orig) {
376 setenv("TZ", orig, 1);
377 tzset();
378 free(orig);
379 }
380
381 return (int)floor(0.5 + difftime(tloc, tutc));
382}
383
384int XLALFrameAddFrDetector(LALFrameH * frame, const LALFrDetector * detector)
385{
386 LALFrameUFrDetector *d;
387 double gpssec;
388 int localTime;
389 XLALFrameUFrameHQueryGTimeModf(&gpssec, frame);
390 localTime = XLALLocalTime(detector->prefix[0], gpssec);
391 d = XLALFrameUFrDetectorAlloc(detector->name, detector->prefix,
392 detector->vertexLatitudeRadians,
393 detector->vertexLongitudeRadians,
394 detector->vertexElevation,
395 detector->xArmAzimuthRadians,
396 detector->yArmAzimuthRadians,
397 detector->xArmAltitudeRadians,
398 detector->yArmAltitudeRadians,
399 detector->xArmMidpoint, detector->yArmMidpoint, localTime);
402 return 0;
403}
404
406{
408 return;
409}
410
411LALFrameH *XLALFrameNew(const LIGOTimeGPS * epoch, double duration,
412 const char *project, int run, int frnum, INT8 detectorFlags)
413{
414 LALFrameH *frame = NULL;
415 int detind;
416
417 /* allocate frame and set run */
418 frame = XLALFrameUFrameHAlloc(project, epoch->gpsSeconds, 1e-9 * epoch->gpsNanoSeconds, duration, frnum);
419 if (!frame)
421 if (XLALFrameUFrameHSetRun(frame, run) < 0) {
422 XLALFrameFree(frame);
424 }
425
426 /* add detectors */
427 for (detind = 0; detind < LAL_NUM_DETECTORS; ++detind) {
428 INT8 detflg = 1 << 2 * detind;
429 if ((detflg & detectorFlags)) /* yes, one ampersand! */
431 &lalCachedDetectors[detind].frDetector);
432 }
433
434 return frame;
435}
436
438 const LALFrameH * frame)
439{
440 double ip, fp; /* integer part and fraction part */
442 return XLALGPSSet(start, ip, XLAL_BILLION_REAL8 * fp);
443}
444
445
446#define DEFINE_FR_CHAN_ADD_TS_FUNCTION(chantype, laltype, vectype, compress) \
447 int XLALFrameAdd ## laltype ## TimeSeries ## chantype ## Data(LALFrameH *frame, const laltype ## TimeSeries *series) \
448 { \
449 LIGOTimeGPS frameStart; \
450 double timeOffset; \
451 const char unitX[] = "s"; \
452 char unitY[LALUnitTextSize]; \
453 LALFrameUFrChan *channel = NULL; \
454 void *data = NULL; \
455 XLALUnitAsString(unitY, sizeof(unitY), &series->sampleUnits); \
456 XLALFrameQueryGTime(&frameStart, frame); \
457 timeOffset = XLALGPSDiff(&series->epoch, &frameStart); \
458 if (timeOffset < 0) \
459 XLAL_ERROR(XLAL_EINVAL, "Series start time %d.%09d " \
460 "is earlier than frame start time %d.%09d", \
461 series->epoch.gpsSeconds, \
462 series->epoch.gpsNanoSeconds, \
463 frameStart.gpsSeconds, \
464 frameStart.gpsNanoSeconds); \
465 channel = XLALFrameUFr ## chantype ## ChanAlloc(series->name, LAL_FRAMEU_FR_VECT_ ## vectype, series->data->length); \
466 if (!channel) \
467 goto failure; \
468 data = XLALFrameUFrChanVectorQueryData(channel); \
469 if (!data) \
470 goto failure; \
471 memcpy(data, series->data->data, series->data->length * sizeof(*series->data->data)); \
472 XLALFrameUFrChanSetSampleRate(channel, 1.0/series->deltaT); \
473 XLALFrameUFrChanSetTimeOffset(channel, timeOffset); \
474 XLALFrameUFrChanVectorSetName(channel, series->name); \
475 XLALFrameUFrChanVectorSetDx(channel, series->deltaT); \
476 XLALFrameUFrChanVectorSetStartX(channel, 0.0); \
477 XLALFrameUFrChanVectorSetUnitX(channel, unitX); \
478 XLALFrameUFrChanVectorSetUnitY(channel, unitY); \
479 XLALFrameUFrChanVectorCompress(channel, LAL_FRAMEU_FR_VECT_COMPRESS_ ## compress); \
480 XLALFrameUFrameHFrChanAdd(frame, channel); \
481 XLALFrameUFrChanFree(channel); \
482 return 0; \
483 failure: /* unsuccessful exit */ \
484 XLALFrameUFrChanFree(channel); \
485 XLAL_ERROR(XLAL_EFUNC); \
486 }
487
488
489#define DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(laltype, vectype, compress) \
490int XLALFrameAdd ## laltype ## TimeSeriesProcData(LALFrameH *frame, const laltype ## TimeSeries *series) \
491 { \
492 LIGOTimeGPS frameStart; \
493 double timeOffset; \
494 double tRange; \
495 const char unitX[] = "s"; \
496 char unitY[LALUnitTextSize]; \
497 LALFrameUFrChan *channel = NULL; \
498 void *data = NULL; \
499 XLALUnitAsString(unitY, sizeof(unitY), &series->sampleUnits); \
500 XLALFrameQueryGTime(&frameStart, frame); \
501 tRange = series->deltaT * series->data->length; \
502 timeOffset = XLALGPSDiff(&series->epoch, &frameStart); \
503 if (timeOffset < 0) \
504 XLAL_ERROR(XLAL_EINVAL, "Series start time %d.%09d " \
505 "is earlier than frame start time %d.%09d", \
506 series->epoch.gpsSeconds, \
507 series->epoch.gpsNanoSeconds, \
508 frameStart.gpsSeconds, \
509 frameStart.gpsNanoSeconds); \
510 channel = XLALFrameUFrProcChanAlloc(series->name, LAL_FRAMEU_FR_PROC_TYPE_TIME_SERIES, LAL_FRAMEU_FR_PROC_SUB_TYPE_UNKNOWN, LAL_FRAMEU_FR_VECT_ ## vectype, series->data->length); \
511 if (!channel) \
512 goto failure; \
513 data = XLALFrameUFrChanVectorQueryData(channel); \
514 if (!data) \
515 goto failure; \
516 memcpy(data, series->data->data, series->data->length * sizeof(*series->data->data)); \
517 XLALFrameUFrChanSetTimeOffset(channel, timeOffset); \
518 XLALFrameUFrChanSetTRange(channel, tRange); \
519 XLALFrameUFrChanVectorSetName(channel, series->name); \
520 XLALFrameUFrChanVectorSetDx(channel, series->deltaT); \
521 XLALFrameUFrChanVectorSetStartX(channel, 0.0); \
522 XLALFrameUFrChanVectorSetUnitX(channel, unitX); \
523 XLALFrameUFrChanVectorSetUnitY(channel, unitY); \
524 XLALFrameUFrChanVectorCompress(channel, LAL_FRAMEU_FR_VECT_COMPRESS_ ## compress); \
525 XLALFrameUFrameHFrChanAdd(frame, channel); \
526 XLALFrameUFrChanFree(channel); \
527 return 0; \
528 failure: /* unsuccessful exit */ \
529 XLALFrameUFrChanFree(channel); \
530 XLAL_ERROR(XLAL_EFUNC); \
531 }
532
533
534#define DEFINE_FR_PROC_CHAN_ADD_FS_FUNCTION(laltype, vectype, compress) \
535int XLALFrameAdd ## laltype ## FrequencySeriesProcData(LALFrameH *frame, const laltype ## FrequencySeries *series, int subtype) \
536 { \
537 LIGOTimeGPS frameStart; \
538 double timeOffset; \
539 const char unitX[] = "s^-1"; \
540 char unitY[LALUnitTextSize]; \
541 LALFrameUFrChan *channel = NULL; \
542 void *data = NULL; \
543 XLALUnitAsString(unitY, sizeof(unitY), &series->sampleUnits); \
544 XLALFrameQueryGTime(&frameStart, frame); \
545 timeOffset = XLALGPSDiff(&series->epoch, &frameStart); \
546 if (timeOffset < 0) \
547 XLAL_ERROR(XLAL_EINVAL, "Series start time %d.%09d " \
548 "is earlier than frame start time %d.%09d", \
549 series->epoch.gpsSeconds, \
550 series->epoch.gpsNanoSeconds, \
551 frameStart.gpsSeconds, \
552 frameStart.gpsNanoSeconds); \
553 channel = XLALFrameUFrProcChanAlloc(series->name, LAL_FRAMEU_FR_PROC_TYPE_FREQUENCY_SERIES, subtype, LAL_FRAMEU_FR_VECT_ ## vectype, series->data->length); \
554 if (!channel) \
555 goto failure; \
556 data = XLALFrameUFrChanVectorQueryData(channel); \
557 if (!data) \
558 goto failure; \
559 memcpy(data, series->data->data, series->data->length * sizeof(*series->data->data)); \
560 XLALFrameUFrChanSetTimeOffset(channel, timeOffset); \
561 XLALFrameUFrChanVectorSetName(channel, series->name); \
562 XLALFrameUFrChanVectorSetDx(channel, series->deltaF); \
563 XLALFrameUFrChanVectorSetStartX(channel, series->f0); \
564 XLALFrameUFrChanVectorSetUnitX(channel, unitX); \
565 XLALFrameUFrChanVectorSetUnitY(channel, unitY); \
566 XLALFrameUFrChanVectorCompress(channel, LAL_FRAMEU_FR_VECT_COMPRESS_ ## compress); \
567 XLALFrameUFrameHFrChanAdd(frame, channel); \
568 XLALFrameUFrChanFree(channel); \
569 return 0; \
570 failure: /* unsuccessful exit */ \
571 XLALFrameUFrChanFree(channel); \
572 XLAL_ERROR(XLAL_EFUNC); \
573 }
574
575/* *INDENT-OFF* */
576DEFINE_FR_CHAN_ADD_TS_FUNCTION(Adc, INT2, 2S, ZERO_SUPPRESS_WORD_2)
577DEFINE_FR_CHAN_ADD_TS_FUNCTION(Adc, INT4, 4S, ZERO_SUPPRESS_WORD_4)
578DEFINE_FR_CHAN_ADD_TS_FUNCTION(Adc, REAL4, 4R, DIFF_GZIP)
579DEFINE_FR_CHAN_ADD_TS_FUNCTION(Adc, REAL8, 8R, DIFF_GZIP)
580
581DEFINE_FR_CHAN_ADD_TS_FUNCTION(Sim, INT2, 2S, ZERO_SUPPRESS_WORD_2)
582DEFINE_FR_CHAN_ADD_TS_FUNCTION(Sim, INT4, 4S, ZERO_SUPPRESS_WORD_4)
583DEFINE_FR_CHAN_ADD_TS_FUNCTION(Sim, REAL4, 4R, DIFF_GZIP)
584DEFINE_FR_CHAN_ADD_TS_FUNCTION(Sim, REAL8, 8R, DIFF_GZIP)
585
586DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(INT2, 2S, ZERO_SUPPRESS_WORD_2)
587DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(INT4, 4S, ZERO_SUPPRESS_WORD_4)
589DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(UINT2, 2U, ZERO_SUPPRESS_WORD_2)
590DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(UINT4, 4U, ZERO_SUPPRESS_WORD_4)
596
601/* *INDENT-ON* */
602
603int XLALFrameWrite(LALFrameH * frame, const char *fname)
604{
605 // LALFrFile *frfile = NULL;
606 LALFrameUFrFile *frfile = NULL;
607 char tmpfname[FILENAME_MAX];
608
609 /* open temporary file */
610 int n = snprintf(tmpfname, sizeof(tmpfname), "%s.tmp", fname);
611 if (n < 0 || n >= (int)sizeof(tmpfname))
612 goto failure;
613 frfile = XLALFrameUFrFileOpen(tmpfname, "w");
614 if (!frfile)
615 goto failure;
616
617 /* write frame */
618 XLALFrameUFrameHWrite(frfile, frame);
619
620 /* close temporary file */
621 XLALFrameUFrFileClose(frfile);
622
623 /* rename file */
624 rename(tmpfname, fname);
625 return 0;
626
627 failure: /* unsuccessful exit */
628 XLALFrameUFrFileClose(frfile);
629 /* TODO: remove tempfile */
630 return -1;
631}
632
633static int charcmp(const void *c1, const void *c2)
634{
635 char a = *(const char *)c1;
636 char b = *(const char *)c2;
637 return (a > b) - (a < b);
638}
639
640/*
641 * Based on a channel name, format a standard frame filename;
642 * in the process, determine recognized detectors and sites;
643 * returns detector flags, if recognized detectors are found.
644 */
645static int XLALFrameFileName(char *fname, size_t size, const char *chname,
646 const LIGOTimeGPS * epoch, double duration)
647{
648 char site[LAL_NUM_DETECTORS + 1] = "";
649 char *desc;
650 const char *cs;
651 char *s;
652 int detflgs = 0;
653 int t0;
654 int dt;
655
656 /* parse chname to get identified sites and detectors */
657 /* strip out detectors from "XmYn...:"-style prefix */
658 for (cs = chname; *cs; cs += 2) {
659 int d;
660 /* when you get to a colon, you're done! */
661 if (*cs == ':')
662 break;
663 /* see if this is an unexpected format */
664 if (strlen(cs) <= 2 || !isupper(cs[0]) || !isdigit(cs[1])) {
665 /* parse error so reset detflgs and site */
666 detflgs = 0;
667 site[0] = 0;
668 break;
669 }
670 /* try to find this detector */
671 for (d = 0; d < LAL_NUM_DETECTORS; ++d)
672 if (0 == strncmp(cs, lalCachedDetectors[d].frDetector.prefix, 2)) {
673 /* found it: put it in sites and detflgs */
674 detflgs |= 1 << 2 * d;
675 strncat(site, cs, 1);
676 }
677 }
678
679 /* sort and uniqify sites */
680 qsort(site, strlen(site), 1, charcmp);
681 cs = s = site;
682 for (cs = s = site; *s; ++cs)
683 if (*s != *cs)
684 *++s = *cs;
685
686 /* description is a modified version of chname */
687 /* replace invalid description char with '_' */
688 desc = XLALStringDuplicate(chname);
689 for (s = desc; *s; ++s)
690 if (!isalnum(*s))
691 *s = '_';
692
693 /* determine start time field and duration field */
694 t0 = epoch->gpsSeconds;
695 dt = (int)ceil(XLALGPSGetREAL8(epoch) + duration) - t0;
696
697 /* now format the file name */
698 snprintf(fname, size, "%s-%s-%d-%d.gwf", *site ? site : "X", desc, t0,
699 dt);
700
701 LALFree(desc);
702 return detflgs;
703}
704
705#define DEFINE_FR_WRITE_TS_FUNCTION(laltype) \
706 int XLALFrWrite ## laltype ## TimeSeries(const laltype ## TimeSeries *series, int frnum) \
707 { \
708 LALFrameH *frame; \
709 double duration; \
710 char fname[FILENAME_MAX]; \
711 int detflgs; \
712 duration = series->deltaT * series->data->length; \
713 detflgs = XLALFrameFileName(fname, sizeof(fname), series->name, &series->epoch, duration); \
714 frame = XLALFrameNew(&series->epoch, duration, "LAL", 0, frnum, detflgs); \
715 XLALFrameAdd ## laltype ## TimeSeriesProcData(frame, series); \
716 XLALFrameWrite(frame, fname); \
717 XLALFrameFree(frame); \
718 return 0; \
719 }
720
721/* *INDENT-OFF* */
729/* *INDENT-ON* */
730#define DEFINE_FR_WRITE_FS_FUNCTION(laltype) \
731 int XLALFrWrite ## laltype ## FrequencySeries(const laltype ## FrequencySeries *series, int frnum, int subtype) \
732 { \
733 LALFrameH *frame; \
734 double duration; \
735 char fname[FILENAME_MAX]; \
736 int detflgs; \
737 duration = series->deltaF > 0.0 ? 1.0 / series->deltaF : 1.0; \
738 detflgs = XLALFrameFileName(fname, sizeof(fname), series->name, &series->epoch, duration); \
739 frame = XLALFrameNew(&series->epoch, duration, "LAL", 0, frnum, detflgs); \
740 XLALFrameAdd ## laltype ## FrequencySeriesProcData(frame, series, subtype); \
741 XLALFrameWrite(frame, fname); \
742 return 0; \
743 }
744/* *INDENT-OFF* */
749/* *INDENT-ON* */
750
751#if 0
752int main(void)
753{
754 const LIGOTimeGPS epoch = { 1000000000, 0 };
755 const double duration = 16.0;
756 const char *project = "LIGO";
757 const int run = 0;
758 const int frnum = 0;
759 const int detectorFlags =
761 const double srate = 16384.0;
762 double deltaT = 1.0 / srate;
763 size_t length = duration * srate;
764 size_t j;
765
766 LALFrameH *frame;
767 REAL4TimeSeries *series;
768
769 series =
770 XLALCreateREAL4TimeSeries("dummy", &epoch, 0.0, deltaT,
771 &lalStrainUnit, length);
772 for (j = 0; j < length; ++j)
773 series->data->data[j] = j % 256;
774
775 frame =
776 XLALFrameNew(&epoch, duration, project, run, frnum, detectorFlags);
777
779
781
782 XLALFrameWrite(frame, "dummy.gwf");
783
784 XLALFrameFree(frame);
785
786 return 0;
787}
788#endif
#define XLAL_BILLION_REAL8
#define DEFINE_FR_WRITE_FS_FUNCTION(laltype)
Definition: LALFrameIO.c:730
static int XLALFrameFileName(char *fname, size_t size, const char *chname, const LIGOTimeGPS *epoch, double duration)
Definition: LALFrameIO.c:645
static int XLALLocalTime(char site, int gpssec)
Definition: LALFrameIO.c:335
#define DEFINE_FR_WRITE_TS_FUNCTION(laltype)
Definition: LALFrameIO.c:705
#define DEFINE_FR_PROC_CHAN_ADD_FS_FUNCTION(laltype, vectype, compress)
Definition: LALFrameIO.c:534
#define DEFINE_FR_PROC_CHAN_ADD_TS_FUNCTION(laltype, vectype, compress)
Definition: LALFrameIO.c:489
static LIGOTimeGPS * XLALFrameQueryGTime(LIGOTimeGPS *start, const LALFrameH *frame)
Definition: LALFrameIO.c:437
#define localtime_r(timep, result)
Definition: LALFrameIO.c:44
static int charcmp(const void *c1, const void *c2)
Definition: LALFrameIO.c:633
#define DEFINE_FR_CHAN_ADD_TS_FUNCTION(chantype, laltype, vectype, compress)
Definition: LALFrameIO.c:446
#define LALMalloc(n)
#define LALFree(p)
const char *const name
int main(int argc, char *argv[])
Definition: cat.c:63
const LALDetector lalCachedDetectors[LAL_NUM_DETECTORS]
LIGOTimeGPS * XLALGPSTimeNow(LIGOTimeGPS *gpstime)
LALTYPECODE
uint64_t UINT8
double complex COMPLEX16
double REAL8
int16_t INT2
int64_t INT8
uint16_t UINT2
uint32_t UINT4
float complex COMPLEX8
int32_t INT4
float REAL4
LAL_C_TYPE_CODE
LAL_CHAR_TYPE_CODE
LAL_U2_TYPE_CODE
LAL_Z_TYPE_CODE
LAL_S_TYPE_CODE
LAL_I2_TYPE_CODE
LAL_I8_TYPE_CODE
LAL_D_TYPE_CODE
LAL_I4_TYPE_CODE
LAL_UCHAR_TYPE_CODE
LAL_U8_TYPE_CODE
LAL_U4_TYPE_CODE
#define LAL_LLO_4K_DETECTOR_BIT
#define LAL_LHO_4K_DETECTOR_BIT
LAL_NUM_DETECTORS
int gethostname(char *name, int len)
double XLALFrFileQueryDt(const LALFrFile *frfile, size_t pos)
Query a frame file for the duration of a particular frame.
Definition: LALFrameIO.c:143
struct tagLALFrFile LALFrFile
Incomplete type for a frame file structure.
Definition: LALFrameIO.h:64
LALTYPECODE XLALFrFileQueryChanType(const LALFrFile *frfile, const char *chname, size_t pos)
Query a frame file for the data type of a channel in a frame.
Definition: LALFrameIO.c:148
int XLALFrFileCksumValid(LALFrFile *frfile)
Use checksum to determine if a frame file is valid.
Definition: LALFrameIO.c:205
size_t XLALFrFileQueryNFrame(const LALFrFile *frfile)
Query a frame file for the number of frames contained in the file.
Definition: LALFrameIO.c:130
LIGOTimeGPS * XLALFrFileQueryGTime(LIGOTimeGPS *start, const LALFrFile *frfile, size_t pos)
Query a frame file for the start time of a particular frame.
Definition: LALFrameIO.c:135
LALFrFile * XLALFrFileOpenURL(const char *url)
Open frame file for reading and return a LALFrFile structure.
Definition: LALFrameIO.c:70
int XLALFrFileClose(LALFrFile *frfile)
Close a frame file described by a LALFrFile structure.
Definition: LALFrameIO.c:54
size_t XLALFrFileQueryChanVectorLength(const LALFrFile *frfile, const char *chname, size_t pos)
Query a frame file for the number of data points in a channel in a frame.
Definition: LALFrameIO.c:192
int XLALFrameAddFrDetector(LALFrameH *frame, const LALFrDetector *detector)
Adds a detector structure to a frame.
Definition: LALFrameIO.c:384
LALFrameUFrameH LALFrameH
Incomplete type for a frame header structure.
Definition: LALFrameIO.h:522
int XLALFrameAddFrHistory(LALFrameH *frame, const char *name, const char *comment)
Adds a history structure to a frame.
Definition: LALFrameIO.c:311
int XLALFrameWrite(LALFrameH *frame, const char *fname)
Write a LALFrameH frame structure to a frame file.
Definition: LALFrameIO.c:603
LALFrameH * XLALFrameNew(const LIGOTimeGPS *epoch, double duration, const char *project, int run, int frnum, INT8 detectorFlags)
Creates a new frame structure.
Definition: LALFrameIO.c:411
int XLALFrameAddREAL4TimeSeriesProcData(LALFrameH *frame, const REAL4TimeSeries *series)
Adds an REAL4TimeSeries to a frame as a FrProcData channel.
void XLALFrameFree(LALFrameH *frame)
Frees a frame structure.
Definition: LALFrameIO.c:405
size_t XLALFrameUFrTOCQueryNFrame(const LALFrameUFrTOC *toc)
Query FrTOC structure for number of FrameH structures contained.
Definition: LALFrameU.c:150
LALFrameUFrameH * XLALFrameUFrameHAlloc(const char *name, double start1, double start2, double dt, int frnum)
Allocate memory for a new frame header FrameH structure.
Definition: LALFrameU.c:210
LALFrameUFrDetector * XLALFrameUFrDetectorAlloc(const char *name, const char *prefix, double latitude, double longitude, double elevation, double azimuthX, double azimuthY, double altitudeX, double altitudeY, double midpointX, double midpointY, int localTime)
Allocate memory for a new detector FrDetector structure.
Definition: LALFrameU.c:440
int XLALFrameUFrameHWrite(LALFrameUFrFile *stream, LALFrameUFrameH *frame)
Write a FrameH structure to an output FrFile stream.
Definition: LALFrameU.c:220
int XLALFrameUFrChanVectorQueryType(const LALFrameUFrChan *channel)
Query FrChan structure for the data type of its FrVect structure.
Definition: LALFrameU.c:355
int XLALFrameUFrameHFrDetectorAdd(LALFrameUFrameH *frame, LALFrameUFrDetector *detector)
Add a FrDetector structure to a FrameH structure.
Definition: LALFrameU.c:230
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
void XLALFrameUFrameHFree(LALFrameUFrameH *frame)
Free a FrameH structure.
Definition: LALFrameU.c:205
void XLALFrameUFrChanFree(LALFrameUFrChan *channel)
Free a FrChan structure.
Definition: LALFrameU.c:280
void XLALFrameUFrHistoryFree(LALFrameUFrHistory *history)
Free a FrHistory structure.
Definition: LALFrameU.c:508
LALFrameUFrHistory * XLALFrameUFrHistoryAlloc(const char *name, double gpssec, const char *comment)
Allocate memory for a new detector FrHistory structure.
Definition: LALFrameU.c:513
LALFrameUFrTOC * XLALFrameUFrTOCRead(LALFrameUFrFile *stream)
Read the table of contents FrTOC structure for a FrFile stream.
Definition: LALFrameU.c:145
int XLALFrameUFrameHFrHistoryAdd(LALFrameUFrameH *frame, LALFrameUFrHistory *history)
Add a FrHistory structure to a FrameH structure.
Definition: LALFrameU.c:235
size_t XLALFrameUFrChanVectorQueryNData(const LALFrameUFrChan *channel)
Query FrChan structure for the number of points of data in its FrVect structure.
Definition: LALFrameU.c:370
int XLALFrameUFrameHSetRun(LALFrameUFrameH *frame, int run)
Set the run number in a FrameH structure.
Definition: LALFrameU.c:275
struct tagLALFrameUFrHistory LALFrameUFrHistory
Incomplete type for a history data FrHistory structure.
Definition: LALFrameU.h:105
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
LALFrameUFrFile * XLALFrameUFrFileOpen(const char *filename, const char *mode)
Open a frame file FrFile stream.
Definition: LALFrameU.c:130
double XLALFrameUFrameHQueryGTimeModf(double *iptr, const LALFrameUFrameH *frame)
Query FrameH structure for the start time.
Definition: LALFrameU.c:260
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
int XLALFrameUFileCksumValid(LALFrameUFrFile *stream)
Use checksum to determine if FrFile stream is valid.
Definition: LALFrameU.c:135
@ 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
char char * XLALStringDuplicate(const char *s)
size_t XLALStringCopy(char *dst, const char *src, size_t size)
static const INT4 a
REAL4TimeSeries * XLALCreateREAL4TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
void XLALDestroyREAL4TimeSeries(REAL4TimeSeries *series)
const LALUnit lalStrainUnit
time_t XLALSecondsSinceUnixEpoch(const struct tm *utc)
struct tm * XLALGPSToUTC(struct tm *utc, INT4 gpssec)
#define XLAL_ERROR_NULL(...)
#define XLAL_ERROR(...)
#define XLAL_CHECK_NULL(assertion,...)
XLAL_EBADLEN
XLAL_ENOMEM
XLAL_EFAULT
XLAL_ENAME
XLAL_EFUNC
XLAL_ETYPE
XLAL_EIO
XLAL_EINVAL
LIGOTimeGPS * XLALGPSSet(LIGOTimeGPS *epoch, INT4 gpssec, INT8 gpsnan)
REAL8 XLALGPSGetREAL8(const LIGOTimeGPS *epoch)
def now
path
string url
double dt
Definition: stream.c:110
double t0
Definition: stream.c:109
char * channel
Definition: stream.c:108
REAL8 vertexLongitudeRadians
REAL8 vertexLatitudeRadians
REAL4 xArmMidpoint
REAL4 vertexElevation
REAL4 xArmAzimuthRadians
REAL4 yArmMidpoint
CHAR prefix[3]
REAL4 yArmAltitudeRadians
REAL4 yArmAzimuthRadians
REAL4 xArmAltitudeRadians
CHAR name[LALNameLength]
REAL4Sequence * data
REAL4 * data
enum @4 site
FILE * fp
enum @1 epoch
double srate
Definition: vis.c:237