-
Notifications
You must be signed in to change notification settings - Fork 34
/
msrutils.c
418 lines (361 loc) · 12.9 KB
/
msrutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/***************************************************************************
* Generic routines to operate on miniSEED records.
*
* This file is part of the miniSEED Library.
*
* Copyright (c) 2024 Chad Trabant, EarthScope Data Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
/**********************************************************************/ /**
* @brief Initialize and return an ::MS3Record
*
* Memory is allocated if for a new ::MS3Record if \a msr is NULL.
*
* If memory for the \c datasamples field has been allocated the pointer
* will be retained for reuse. If memory for extra headers has been
* allocated it will be released.
*
* @param[in] msr A ::MS3Record to re-initialize
*
* @returns a pointer to a ::MS3Record struct on success or NULL on error.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
MS3Record *
msr3_init (MS3Record *msr)
{
void *datasamples = NULL;
size_t datasize = 0;
if (!msr)
{
msr = (MS3Record *)libmseed_memory.malloc (sizeof (MS3Record));
}
else
{
datasamples = msr->datasamples;
datasize = msr->datasize;
if (msr->extra)
libmseed_memory.free (msr->extra);
}
if (msr == NULL)
{
ms_log (2, "Cannot allocate memory\n");
return NULL;
}
memset (msr, 0, sizeof (MS3Record));
msr->datasamples = datasamples;
msr->datasize = datasize;
msr->reclen = -1;
msr->samplecnt = -1;
msr->encoding = -1;
return msr;
} /* End of msr3_init() */
/**********************************************************************/ /**
* @brief Free all memory associated with a ::MS3Record
*
* Free all memory associated with a ::MS3Record, including extra
* header and data samples if present.
*
* @param[in] ppmsr Pointer to point of the ::MS3Record to free
***************************************************************************/
void
msr3_free (MS3Record **ppmsr)
{
if (ppmsr != NULL && *ppmsr != 0)
{
if ((*ppmsr)->extra)
libmseed_memory.free ((*ppmsr)->extra);
if ((*ppmsr)->datasamples)
libmseed_memory.free ((*ppmsr)->datasamples);
libmseed_memory.free (*ppmsr);
*ppmsr = NULL;
}
} /* End of msr3_free() */
/**********************************************************************/ /**
* @brief Duplicate a ::MS3Record
*
* Extra headers are duplicated as well.
*
* If the \a datadup flag is true (non-zero) and the source
* ::MS3Record has associated data samples copy them as well.
*
* @param[in] msr ::MS3Record to duplicate
* @param[in] datadup Flag to control duplication of data samples
*
* @returns Pointer to a new ::MS3Record on success and NULL on error
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
MS3Record *
msr3_duplicate (const MS3Record *msr, int8_t datadup)
{
MS3Record *dupmsr = 0;
if (!msr)
{
ms_log (2, "%s(): Required input not defined: 'msr'\n", __func__);
return NULL;
}
/* Allocate target MS3Record structure */
if ((dupmsr = msr3_init (NULL)) == NULL)
return NULL;
/* Copy MS3Record structure */
memcpy (dupmsr, msr, sizeof (MS3Record));
/* Disconnect pointers from the source structure and reference values */
dupmsr->extra = NULL;
dupmsr->extralength = 0;
dupmsr->datasamples = NULL;
dupmsr->datasize = 0;
dupmsr->numsamples = 0;
/* Copy extra headers */
if (msr->extralength > 0 && msr->extra)
{
/* Allocate memory for new FSDH structure */
if ((dupmsr->extra = (char *)libmseed_memory.malloc (msr->extralength)) == NULL)
{
ms_log (2, "Error allocating memory\n");
msr3_free (&dupmsr);
return NULL;
}
memcpy (dupmsr->extra, msr->extra, msr->extralength);
if (dupmsr->extra)
dupmsr->extralength = msr->extralength;
}
/* Copy data samples if requested and available */
if (datadup && msr->numsamples > 0 && msr->datasize > 0 && msr->datasamples)
{
/* Allocate memory for new data array */
if ((dupmsr->datasamples = libmseed_memory.malloc ((size_t) (msr->datasize))) == NULL)
{
ms_log (2, "Error allocating memory\n");
msr3_free (&dupmsr);
return NULL;
}
memcpy (dupmsr->datasamples, msr->datasamples, msr->datasize);
if (dupmsr->datasamples)
{
dupmsr->datasize = msr->datasize;
dupmsr->numsamples = msr->numsamples;
}
}
return dupmsr;
} /* End of msr3_duplicate() */
/**********************************************************************/ /**
* @brief Calculate time of the last sample in a record
*
* If leap seconds have been loaded into the internal library list:
* when a record completely contains a leap second, starts before and
* ends after, the calculated end time will be adjusted (reduced) by
* one second.
* @note On the epoch time scale the value of a leap second is the
* same as the second following the leap second, without external
* information the values are ambiguous.
* \sa ms_readleapsecondfile()
*
* @param[in] msr ::MS3Record to calculate end time of
*
* @returns Time of the last sample on success and NSTERROR on error.
***************************************************************************/
nstime_t
msr3_endtime (const MS3Record *msr)
{
int64_t sampleoffset = 0;
if (!msr)
return NSTERROR;
if (msr->samplecnt > 0)
sampleoffset = msr->samplecnt - 1;
return ms_sampletime (msr->starttime, sampleoffset, msr->samprate);
} /* End of msr3_endtime() */
/**********************************************************************/ /**
* @brief Print header values of an MS3Record
*
* @param[in] msr ::MS3Record to print
* @param[in] details Flags to control the level of details:
* @parblock
* - \c 0 - print a single summary line
* - \c 1 - print most details of header
* - \c >1 - print all details of header and extra headers if present
* @endparblock
***************************************************************************/
void
msr3_print (const MS3Record *msr, int8_t details)
{
char time[40];
char b;
if (!msr)
return;
/* Generate a start time string */
ms_nstime2timestr (msr->starttime, time, ISOMONTHDAY_DOY_Z, NANO_MICRO);
/* Report information in the fixed header */
if (details > 0)
{
ms_log (0, "%s, version %d, %d bytes (format: %d)\n",
msr->sid, msr->pubversion, msr->reclen, msr->formatversion);
ms_log (0, " start time: %s\n", time);
ms_log (0, " number of samples: %" PRId64 "\n", msr->samplecnt);
ms_log (0, " sample rate (Hz): %.10g\n", msr3_sampratehz(msr));
if (details > 1)
{
b = msr->flags;
ms_log (0, " flags: [%d%d%d%d%d%d%d%d] 8 bits\n",
bit (b, 0x80), bit (b, 0x40), bit (b, 0x20), bit (b, 0x10),
bit (b, 0x08), bit (b, 0x04), bit (b, 0x02), bit (b, 0x01));
if (b & 0x01)
ms_log (0, " [Bit 0] Calibration signals present\n");
if (b & 0x02)
ms_log (0, " [Bit 1] Time tag is questionable\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Clock locked\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Undefined bit set\n");
if (b & 0x10)
ms_log (0, " [Bit 4] Undefined bit set\n");
if (b & 0x20)
ms_log (0, " [Bit 5] Undefined bit set\n");
if (b & 0x40)
ms_log (0, " [Bit 6] Undefined bit set\n");
if (b & 0x80)
ms_log (0, " [Bit 7] Undefined bit set\n");
}
ms_log (0, " CRC: 0x%0X\n", msr->crc);
ms_log (0, " extra header length: %d bytes\n", msr->extralength);
ms_log (0, " data payload length: %d bytes\n", msr->datalength);
ms_log (0, " payload encoding: %s (val: %d)\n",
(char *)ms_encodingstr ((uint8_t)msr->encoding), msr->encoding);
if (details > 1 && msr->extralength > 0 && msr->extra)
{
ms_log (0, " extra headers:\n");
mseh_print (msr, 16);
}
}
else
{
ms_log (0, "%s, %d, %d, %" PRId64 " samples, %-.10g Hz, %s\n",
msr->sid, msr->pubversion, msr->reclen,
msr->samplecnt, msr3_sampratehz(msr), time);
}
} /* End of msr3_print() */
/**********************************************************************/ /**
* @brief Resize data sample buffer of ::MS3Record to what is needed
*
* This routine should only be used if pre-allocation of memory, via
* ::libmseed_prealloc_block_size, was enabled to allocate the buffer.
*
* @param[in] msr ::MS3Record to resize buffer
*
* @returns Return 0 on success, otherwise returns a libmseed error code.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int
msr3_resize_buffer (MS3Record *msr)
{
uint8_t samplesize = 0;
size_t datasize;
if (!msr)
{
ms_log (2, "%s(): Required input not defined: 'msr'\n", __func__);
return MS_GENERROR;
}
samplesize = ms_samplesize(msr->sampletype);
if (samplesize && msr->datasamples && msr->numsamples > 0)
{
datasize = (size_t) msr->numsamples * samplesize;
if (msr->datasize > datasize)
{
msr->datasamples = libmseed_memory.realloc (msr->datasamples, datasize);
if (msr->datasamples == NULL)
{
ms_log (2, "%s: Cannot (re)allocate memory\n", msr->sid);
return MS_GENERROR;
}
msr->datasize = datasize;
}
}
return 0;
} /* End of msr3_resize_buffer() */
/**********************************************************************/ /**
* @brief Calculate sample rate in samples/second (Hertz) for a given ::MS3Record
*
* @param[in] msr ::MS3Record to calculate sample rate for
*
* @returns Return sample rate in Hertz (samples per second)
***************************************************************************/
inline double
msr3_sampratehz (const MS3Record *msr)
{
if (!msr)
return 0.0;
if (msr->samprate < 0.0)
return (-1.0 / msr->samprate);
else
return msr->samprate;
} /* End of msr3_sampratehz() */
/**********************************************************************/ /**
* @brief Calculate sample period in nanoseconds/sample for a given ::MS3Record
*
* @param[in] msr ::MS3Record to calculate sample period for
*
* @returns Return sample period in nanoseconds (nanoseconds per sample)
***************************************************************************/
inline nstime_t
msr3_nsperiod (const MS3Record *msr)
{
if (!msr)
return 0;
/* Calculate sample period from sample rate or period.
* The value is rounded by adding 0.5 before truncation to an integer. */
if (msr->samprate > 0.0)
return (nstime_t)(NSTMODULUS / msr->samprate + 0.5); /* samples/second */
else if (msr->samprate < 0.0)
return (nstime_t)(NSTMODULUS * -msr->samprate + 0.5); /* period or seconds/sample */
return 0;
} /* End of msr3_nsperiod() */
/**********************************************************************/ /**
* @brief Calculate data latency based on the host time
*
* Calculation is based on the time of the last sample in the record; in
* other words, the difference between the host time and the time of
* the last sample in the record.
*
* Double precision is returned, but the true precision is dependent
* on the accuracy of the host system clock among other things.
*
* @param[in] msr ::MS3Record to calculate latency for
*
* @returns seconds of latency or 0.0 on error (indistinguishable from
* 0.0 latency).
***************************************************************************/
double
msr3_host_latency (const MS3Record *msr)
{
double span = 0.0; /* Time covered by the samples */
double epoch; /* Current epoch time */
double latency = 0.0;
time_t tv;
if (msr == NULL)
return 0.0;
/* Calculate the time covered by the samples */
if (msr->samprate > 0.0 && msr->samplecnt > 0)
span = (1.0 / msr->samprate) * (msr->samplecnt - 1);
/* Grab UTC time according to the system clock */
epoch = (double)time (&tv);
/* Now calculate the latency */
latency = epoch - ((double)msr->starttime / NSTMODULUS) - span;
return latency;
} /* End of msr3_host_latency() */