LALApps 10.1.0.1-eeff03c
getdata.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Duncan Brown, Jolien Creighton, Lisa M. Goggin, Matt Pitkin
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#include <LALAppsVCSInfo.h>
23#include <math.h>
24#include <string.h>
25
26#include <lal/LALStdlib.h>
27#include <lal/LALStdio.h>
28#include <lal/AVFactories.h>
29#include <lal/Random.h>
30#include <lal/Date.h>
31#include <lal/ResampleTimeSeries.h>
32#include <lal/LALFrStream.h>
33#include <lal/TimeSeries.h>
34#include <lal/Units.h>
35#include <lal/LIGOMetadataRingdownUtils.h>
36
37#include "getdata.h"
38#include "errutil.h"
39
40/* create simulated data */
42 const char *channelName,
43 LIGOTimeGPS *epoch,
45 int dataType,
47 UINT4 simSeed,
48 REAL4 simScale
49 )
50{
51 RandomParams *ranpar = NULL;
53 UINT4 npoints;
54 UINT4 j;
55
56 verbose( "creating simulated white gaussian noise with random seed %u\n",
57 simSeed );
58
59 series = LALCalloc( 1, sizeof( *series ) );
60 if ( ! series )
61 return NULL;
62
63 npoints = duration * sampleRate;
64
65 /* populate data with gaussian random numbers */
66 series->data = XLALCreateREAL4Vector( npoints );
67 ranpar = XLALCreateRandomParams( simSeed );
68 XLALNormalDeviates( series->data, ranpar );
70
71 /* set metadata */
72 snprintf( series->name, sizeof( series->name ), "%s_SIM", channelName );
73 series->epoch = *epoch;
74 series->deltaT = 1.0/sampleRate;
75 if ( dataType == LALRINGDOWN_DATATYPE_SIM )
76 series->sampleUnits = lalStrainUnit;
77 else
78 series->sampleUnits = lalADCCountUnit;
79
80 /* scale series data */
81 for ( j = 0; j < series->data->length; ++j )
82 series->data->data[j] *= simScale;
83
84 return series;
85}
86
87/* create simulated data */
89 const char *channelName,
90 LIGOTimeGPS *epoch,
93 UINT4 simSeed,
95 )
96{
99 UINT4 npoints;
100 UINT4 j;
101
102 npoints = duration * sampleRate;
103
104 gsl_rng *rng;
105 gsl_rng_env_setup();
106 rng = gsl_rng_alloc(gsl_rng_default);
107 gsl_rng_set( rng, simSeed );
108
109 verbose( "creating simulated gaussian noise with random seed %u\n",
110 simSeed );
111
114 snprintf( output->name, sizeof( output->name ), "%s_SIM", channelName );
115
116 XLALSimNoise(series, 0 , psd, rng);
117
118 for ( j = 0; j < series->data->length; ++j )
119 output->data->data[j] = series->data->data[j];
120
122 LALFree( series);
123
124 return output;
125}
126
127
128
129/* create no noise data */
131 const char *channelName,
132 LIGOTimeGPS *epoch,
134 int dataType,
136 )
137{
139 UINT4 npoints;
140 UINT4 j;
141
142 verbose( "creating zero data\n");
143
144 series = LALCalloc( 1, sizeof( *series ) );
145 if ( ! series )
146 return NULL;
147
148 npoints = duration * sampleRate;
149
150 /* populate data with gaussian random numbers */
151 series->data = XLALCreateREAL4Vector( npoints );
152
153 /* set metadata */
154 snprintf( series->name, sizeof( series->name ), "%s_ZERO", channelName );
155 series->epoch = *epoch;
156 series->deltaT = 1.0/sampleRate;
157 if ( dataType == LALRINGDOWN_DATATYPE_ZERO )
158 series->sampleUnits = lalStrainUnit;
159 else
160 series->sampleUnits = lalADCCountUnit;
161
162 for ( j = 0; j < series->data->length; ++j )
163 series->data->data[j] = 0;
164
165 return series;
166}
167
168
169
170/* read frame data */
172 const char *cacheName,
173 const char *channelName,
174 LIGOTimeGPS *epoch,
176 int dataType
177 )
178{
179 LALCache *cache = NULL;
180 LALFrStream *stream = NULL;
183
184 verbose( "get data from cache file %s\n", cacheName );
185
186 /* open the data cache and use it to get a frame stream */
187 cache = XLALCacheImport( cacheName );
188 stream = XLALFrStreamCacheOpen( cache );
190
191 /* set the mode of the frame stream */
192 XLALFrStreamSetMode( stream, mode );
193
194 /* read the series */
196
197 /* close the frame stream */
198 XLALFrStreamClose( stream );
199
200 /* if this is strain data, correct the units */
201 if ( dataType == LALRINGDOWN_DATATYPE_HT_REAL4 )
202 series->sampleUnits = lalStrainUnit;
203
204 return series;
205}
206
207
208/* read double-precision frame data and convert to single-precision data */
210 const char *cacheName,
211 const char *channelName,
212 LIGOTimeGPS *epoch,
214 int dataType,
215 REAL8 dblHighPassFreq
216 )
217{
219 REAL8TimeSeries *dblser;
220 LALCache *cache = NULL;
221 LALFrStream *stream = NULL;
223 UINT4 j;
224
225 verbose( "get data from cache file %s\n", cacheName );
226
227 /* open the data cache and use it to get a frame stream */
228 cache = XLALCacheImport( cacheName );
229 stream = XLALFrStreamCacheOpen( cache );
231
232 /* set the mode of the frame stream */
233 XLALFrStreamSetMode( stream, mode );
234
235 /* read double-precision series */
236 dblser = fr_get_REAL8TimeSeries( channelName, epoch, duration, stream );
237
238 /* close the frame stream */
239 XLALFrStreamClose( stream );
240
241 /* highpass the double-precision series */
242 highpass_REAL8TimeSeries( dblser, dblHighPassFreq );
243
244 /* now create single-precision series */
245 series = LALCalloc( 1, sizeof( *series ) );
246
247 /* copy metadata */
248 if((int)sizeof(series->name) <= snprintf( series->name, sizeof( series->name ), "%s_CNVRT", dblser->name ))
249 XLAL_ERROR_NULL(XLAL_FAILURE,"String truncated");
250 series->epoch = dblser->epoch;
251 series->deltaT = dblser->deltaT;
252 series->f0 = dblser->f0;
253 series->sampleUnits = dblser->sampleUnits;
254
255 /* scale and copy data */
257 for ( j = 0; j < series->data->length; ++j )
258 series->data->data[j] = (REAL4)( 1.0 * dblser->data->data[j] );
259
260 /* if this is strain data, correct the units */
261 if ( dataType == LALRINGDOWN_DATATYPE_HT_REAL8 )
262 series->sampleUnits = lalStrainUnit;
263
264 /* destroy REAL8 time series */
265 XLALDestroyREAL8Vector( dblser->data );
266 LALFree(dblser);
267
268 return series;
269}
270
271
272/* low-level routine to read single-precision frame data */
274 LIGOTimeGPS *epoch, REAL8 duration, LALFrStream *stream )
275{
277 REAL8 srate;
278 UINT4 npoints;
279
280 series = LALCalloc( 1, sizeof( *series ) );
281 if ( ! series )
282 return NULL;
283
284 /* if epoch is not NULL then seek to the appropriate time */
285 if ( epoch )
286 {
287 verbose( "seek to gps time %d.%09d\n", epoch->gpsSeconds,
288 epoch->gpsNanoSeconds );
289 XLALFrStreamSeek( stream, epoch );
290 series->epoch = *epoch;
291 }
292
293 strncpy( series->name, channelName, sizeof( series->name ) - 1 );
294
295 /* call first time to get sample rate */
297
298 /* compute sample rate and number of points to request */
299 srate = 1.0/series->deltaT;
300 npoints = floor( duration*srate + 0.5 ); /* round to nearest integer */
301
302 /* create the data */
303 series->data = XLALCreateREAL4Vector( npoints );
304
305 /* now get the data */
306 verbose( "read %g seconds of data (%u points at sample rate %g Hz)\n",
307 duration, npoints, srate );
309
310 return series;
311}
312
313
314/* low-level routine to read double-precision frame data */
316 LIGOTimeGPS *epoch, REAL8 duration, LALFrStream *stream )
317{
319 REAL8 srate;
320 UINT4 npoints;
321
322 series = LALCalloc( 1, sizeof( *series ) );
323 if ( ! series )
324 return NULL;
325
326 /* if epoch is not NULL then seek to the appropriate time */
327 if ( epoch )
328 {
329 verbose( "seek to gps time %d.%09d\n", epoch->gpsSeconds,
330 epoch->gpsNanoSeconds );
331 XLALFrStreamSeek( stream, epoch );
332 series->epoch = *epoch;
333 }
334
335 strncpy( series->name, channelName, sizeof( series->name ) - 1 );
336
337 /* call first time to get sample rate */
339
340 /* compute sample rate and number of points to request */
341 srate = 1.0/series->deltaT;
342 npoints = floor( duration*srate + 0.5 ); /* round to nearest integer */
343
344 /* create the data */
345 series->data = XLALCreateREAL8Vector( npoints );
346
347 /* now get the data */
348 verbose( "read %g seconds of data (%u points at sample rate %g Hz)\n",
349 duration, npoints, srate );
351
352 return series;
353}
354
355
356/* resample time series */
358{
360 char name[LALNameLength];
361 if ( sampleRate > 0.0 && sampleRate * series->deltaT < 1.0 )
362 {
363 ResampleTSParams resamplepar;
364 memset( &resamplepar, 0, sizeof( resamplepar ) );
365 resamplepar.deltaT = 1.0/sampleRate;
366 resamplepar.filterType = defaultButterworth;
367 verbose( "resampling data from %g Hz to %g Hz\n", 1.0/series->deltaT,
368 sampleRate );
370 &status );
371 strncpy( name, series->name, LALNameLength * sizeof(char) );
373 "%s_RSMPL", name );
374 }
375 return 0;
376}
377
378
379/* highpass filter time series */
381{
383 char name[LALNameLength];
384 PassBandParamStruc highpasspar;
385
386 if ( frequency > 0.0 )
387 {
388 highpasspar.nMax = 8;
389 highpasspar.f1 = -1;
390 highpasspar.a1 = -1;
391 highpasspar.f2 = frequency;
392 highpasspar.a2 = 0.9; /* this means 10% attenuation at f2 */
393 verbose( "highpass filtering data at %g Hz\n", frequency );
395 &status );
396 strncpy( name, series->name, LALNameLength * sizeof(char) );
397 XLALStringPrint( series->name, sizeof( series->name ),
398 "%s_HPFLTR", name );
399
400 }
401 return 0;
402}
403
404
405/* highpass filter double-precision time series */
407{
409 char name[LALNameLength];
410 PassBandParamStruc highpasspar;
411 if ( frequency > 0.0 )
412 {
413 highpasspar.nMax = 8;
414 highpasspar.f1 = -1;
415 highpasspar.a1 = -1;
416 highpasspar.f2 = frequency;
417 highpasspar.a2 = 0.9; /* this means 10% attenuation at f2 */
418 verbose( "highpass filtering data at %g Hz\n", frequency );
420 &status );
421 strncpy( name, series->name, LALNameLength * sizeof(char) );
422 XLALStringPrint( series->name, sizeof( series->name ),
423 "%s_HPFLTR", name );
424 }
425 return 0;
426}
427
428
429/* trim padding from data */
431{
432 char name[LALNameLength];
433 UINT4 padSamples = floor( padData / series->deltaT + 0.5 );
434 UINT4 blockSamples = series->data->length - 2 * padSamples;
435
436 if ( padData > 0 )
437 {
438 series = XLALResizeREAL4TimeSeries(series, padSamples, blockSamples);
439 strncpy( name, series->name, LALNameLength * sizeof(char) );
440 XLALStringPrint( series->name, sizeof( series->name ),
441 "%s_STRIPPAD", name );
442 }
443
444 return 0;
445}
#define LAL_CALL(function, statusptr)
int j
#define LALCalloc(m, n)
#define LALFree(p)
LALRINGDOWN_DATATYPE_HT_REAL4
LALRINGDOWN_DATATYPE_ZERO
LALRINGDOWN_DATATYPE_HT_REAL8
LALRINGDOWN_DATATYPE_SIM
INT4 sampleRate
Definition: blindinj.c:124
INT4 duration
Definition: blindinj.c:123
int verbose
Definition: chirplen.c:77
REAL4TimeSeries * ring_get_frame_data(const char *cacheName, const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, int dataType)
Definition: getdata.c:171
REAL4TimeSeries * get_simulated_data(const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, int dataType, REAL8 sampleRate, UINT4 simSeed, REAL4 simScale)
Definition: getdata.c:41
REAL4TimeSeries * get_zero_data(const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, int dataType, REAL8 sampleRate)
Definition: getdata.c:130
int highpass_REAL4TimeSeries(REAL4TimeSeries *series, REAL8 frequency)
Definition: getdata.c:380
REAL4TimeSeries * fr_get_REAL4TimeSeries(const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, LALFrStream *stream)
Definition: getdata.c:273
REAL8TimeSeries * fr_get_REAL8TimeSeries(const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, LALFrStream *stream)
Definition: getdata.c:315
REAL4TimeSeries * get_simulated_data_new(const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, REAL8 sampleRate, UINT4 simSeed, REAL8FrequencySeries *psd)
Definition: getdata.c:88
int resample_REAL4TimeSeries(REAL4TimeSeries *series, REAL8 sampleRate)
Definition: getdata.c:357
REAL4TimeSeries * get_frame_data_dbl_convert(const char *cacheName, const char *channelName, LIGOTimeGPS *epoch, REAL8 duration, int dataType, REAL8 dblHighPassFreq)
Definition: getdata.c:209
int trimpad_REAL4TimeSeries(REAL4TimeSeries *series, REAL8 padData)
Definition: getdata.c:430
int highpass_REAL8TimeSeries(REAL8TimeSeries *series, REAL8 frequency)
Definition: getdata.c:406
void LALButterworthREAL8TimeSeries(LALStatus *stat, REAL8TimeSeries *series, PassBandParamStruc *params)
void LALDButterworthREAL4TimeSeries(LALStatus *stat, REAL4TimeSeries *series, PassBandParamStruc *params)
void XLALDestroyCache(LALCache *cache)
LALCache * XLALCacheImport(const char *fname)
double REAL8
#define XLAL_INIT_DECL(var,...)
uint32_t UINT4
float REAL4
LALNameLength
int XLALFrStreamSeek(LALFrStream *stream, const LIGOTimeGPS *epoch)
int XLALFrStreamClose(LALFrStream *stream)
LALFrStream * XLALFrStreamCacheOpen(LALCache *cache)
int XLALFrStreamSetMode(LALFrStream *stream, int mode)
LAL_FR_STREAM_VERBOSE_MODE
int XLALFrStreamGetREAL8TimeSeriesMetadata(REAL8TimeSeries *series, LALFrStream *stream)
int XLALFrStreamGetREAL4TimeSeriesMetadata(REAL4TimeSeries *series, LALFrStream *stream)
int XLALFrStreamGetREAL4TimeSeries(REAL4TimeSeries *series, LALFrStream *stream)
int XLALFrStreamGetREAL8TimeSeries(REAL8TimeSeries *series, LALFrStream *stream)
int XLALSimNoise(REAL8TimeSeries *s, size_t stride, REAL8FrequencySeries *psd, gsl_rng *rng)
int XLALStringPrint(char *s, size_t n, const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(3
RandomParams * XLALCreateRandomParams(INT4 seed)
void XLALDestroyRandomParams(RandomParams *params)
int XLALNormalDeviates(REAL4Vector *deviates, RandomParams *params)
void LALResampleREAL4TimeSeries(LALStatus *status, REAL4TimeSeries *ts, ResampleTSParams *params)
defaultButterworth
REAL4TimeSeries * XLALCreateREAL4TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
REAL8TimeSeries * XLALCreateREAL8TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
REAL4TimeSeries * XLALResizeREAL4TimeSeries(REAL4TimeSeries *series, int first, size_t length)
const LALUnit lalStrainUnit
const LALUnit lalADCCountUnit
REAL4Vector * XLALCreateREAL4Vector(UINT4 length)
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define XLAL_ERROR_NULL(...)
XLAL_FAILURE
char name[LIGOMETA_SOURCE_MAX]
Definition: inspinj.c:561
static LALStatus status
Definition: inspinj.c:552
psd
string mode
frequency
REAL8Sequence * data
LALUnit sampleUnits
LIGOTimeGPS epoch
CHAR name[LALNameLength]
REAL8 * data
ResampleTSFilter filterType
Definition: series.h:36
char * name
Definition: series.h:37
float f0
Definition: series.h:43
float * data
Definition: series.h:46
enum @1 epoch
CHAR * channelName
Definition: tmpltbank.c:402
INT4 padData
Definition: tmpltbank.c:394
int output(const char *outfile, int outtype, REAL4TimeSeries *series)
Definition: view.c:603
double srate