/
test_mseed_util.py
1309 lines (1188 loc) · 57.7 KB
/
test_mseed_util.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import copy
import io
import os
import random
import shutil
import sys
from datetime import datetime
from struct import pack, unpack
import warnings
import numpy as np
from obspy import UTCDateTime
from obspy.core import Stream, Trace
from obspy.core.util import NamedTemporaryFile
from obspy.io.mseed import util
from obspy.io.mseed.core import _read_mseed
from obspy.io.mseed.headers import (FIXED_HEADER_ACTIVITY_FLAGS,
FIXED_HEADER_DATA_QUAL_FLAGS,
FIXED_HEADER_IO_CLOCK_FLAGS)
from obspy.io.mseed.util import set_flags_in_fixed_headers
import pytest
def _create_mseed_file(filename, record_count, sampling_rate=1.0,
starttime=UTCDateTime(0), flags=None, seed=None,
skiprecords=0):
"""
Helper function to create MiniSEED files with certain flags set for
testing purposes.
"""
all_flags = {
"data_quality_flags": {"offset": 38, "flags": [
"amplifier_saturation", "digitizer_clipping",
"spikes", "glitches", "missing_padded_data",
"telemetry_sync_error", "digital_filter_charging",
"suspect_time_tag"]},
"activity_flags": {"offset": 36, "flags": [
"calibration_signal", "time_correction_applied",
"event_begin", "event_end", "positive_leap", "negative_leap",
"event_in_progress"]},
"io_and_clock_flags": {"offset": 37, "flags": [
"station_volume", "long_record_read",
"short_record_read", "start_time_series", "end_time_series",
"clock_locked"]}
}
# With uncompressed float32 it is 50 samples per record.
data = np.arange(50 * record_count, dtype=np.float32)
tr = Trace(data=data,
header={"sampling_rate": sampling_rate, "starttime": starttime})
tr.write(filename, format="mseed", reclen=256, encoding="FLOAT32")
if not flags:
return
# Reproducibility!
if seed is not None:
np.random.seed(seed)
data = np.fromfile(filename, dtype=np.uint8)
# Modify the flags to get the required statistics.
for key, value in all_flags.items():
if key not in flags:
continue
_f = value["flags"]
_flag_values = np.zeros(record_count - skiprecords, dtype=np.uint8)
for _i, name in enumerate(_f):
if name not in flags[key]:
continue
# Create array with the correct number of flags set.
arr = np.zeros(record_count - skiprecords, dtype=np.uint8)
arr[:flags[key][name]] = 1 << _i
np.random.shuffle(arr)
_flag_values |= arr
data[value["offset"]:: 256] = np.concatenate([
np.zeros(skiprecords, dtype=np.uint8), _flag_values])
# Write again.
data.tofile(filename)
class TestMSEEDUtil():
"""
Tests suite for util module of obspy.io.mseed.
"""
@classmethod
def setup_class(cls):
cls.maxDiff = None
# mseed steim compression is big endian
if sys.byteorder == 'little':
cls.swap = 1
else:
cls.swap = 0
def test_convert_datetime(self):
"""
Tests all time conversion methods.
"""
# These values are created using the Linux "date -u -d @TIMESTRING"
# command. These values are assumed to be correct.
timesdict = {
1234567890: UTCDateTime(2009, 2, 13, 23, 31, 30),
1111111111: UTCDateTime(2005, 3, 18, 1, 58, 31),
1212121212: UTCDateTime(2008, 5, 30, 4, 20, 12),
1313131313: UTCDateTime(2011, 8, 12, 6, 41, 53),
100000: UTCDateTime(1970, 1, 2, 3, 46, 40),
100000.111112: UTCDateTime(1970, 1, 2, 3, 46, 40, 111112),
200000000: UTCDateTime(1976, 5, 3, 19, 33, 20),
# test rounding of 7th digit of microseconds
1388479508.871572: UTCDateTime(1388479508.8715718),
}
# Loop over timesdict.
for ts, dt in timesdict.items():
assert dt == util._convert_mstime_to_datetime(ts * 1000000)
assert ts * 1000000 == util._convert_datetime_to_mstime(dt)
# Additional sanity tests.
# Random date that previously failed.
dt = UTCDateTime(2017, 3, 6, 4, 12, 16, 260696)
assert dt == util._convert_mstime_to_datetime(
util._convert_datetime_to_mstime(dt))
# Some random date.
random.seed(815) # make test reproducible
timestring = random.randint(0, 2000000) * 1e6
assert timestring == util._convert_datetime_to_mstime(
util._convert_mstime_to_datetime(timestring))
def test_convert_datetime2(self):
"""
Some failing test discovered in #1670
"""
# 1
dt = UTCDateTime(ns=1487021451935737333)
assert str(dt) == "2017-02-13T21:30:51.935737Z"
assert util._convert_datetime_to_mstime(dt) == 1487021451935737
assert dt == util._convert_mstime_to_datetime(
util._convert_datetime_to_mstime(dt))
# 2
dt = UTCDateTime(ns=1487021451935736449)
assert str(dt) == "2017-02-13T21:30:51.935736Z"
assert util._convert_datetime_to_mstime(dt) == 1487021451935736
assert dt == util._convert_mstime_to_datetime(
util._convert_datetime_to_mstime(dt))
# 3
dt = UTCDateTime(ns=1487021451935736501)
assert str(dt) == "2017-02-13T21:30:51.935737Z"
assert util._convert_datetime_to_mstime(dt) == 1487021451935737
assert dt == util._convert_mstime_to_datetime(
util._convert_datetime_to_mstime(dt))
def test_get_record_information(self, testdata):
"""
Tests the util._get_ms_file_info method with known values.
"""
filename = testdata['BW.BGLD.__.EHE.D.2008.001.first_10_records']
# Simply reading the file.
info = util.get_record_information(filename)
assert info['filesize'] == 5120
assert info['record_length'] == 512
assert info['number_of_records'] == 10
assert info['excess_bytes'] == 0
# Now with an open file. This should work regardless of the current
# value of the file pointer and it should also not change the file
# pointer.
with open(filename, 'rb') as open_file:
open_file.seek(1234)
info = util.get_record_information(open_file)
assert info['filesize'] == 5120 - 1234
assert info['record_length'] == 512
assert info['number_of_records'] == 7
assert info['excess_bytes'] == 302
assert open_file.tell() == 1234
# Now test with a BytesIO with the first ten percent.
with open(filename, 'rb') as open_file:
open_file_string = io.BytesIO(open_file.read())
open_file_string.seek(111)
info = util.get_record_information(open_file_string)
assert info['filesize'] == 5120 - 111
assert info['record_length'] == 512
assert info['number_of_records'] == 9
assert info['excess_bytes'] == 401
assert open_file_string.tell() == 111
# One more file containing two records.
filename = testdata['test.mseed']
info = util.get_record_information(filename)
assert info['filesize'] == 8192
assert info['record_length'] == 4096
assert info['number_of_records'] == 2
assert info['excess_bytes'] == 0
def test_get_record_information_negative_sr_rate_and_mult(self, testdata):
"""
Tests the method for negative sampling rate factors and multipliers.
"""
filename = testdata['single_record_negative_sr_fact_and_mult.mseed']
info = util.get_record_information(filename)
assert info == {
'activity_flags': 0,
'byteorder': '>',
'channel': 'VHZ',
'data_quality_flags': 0,
'encoding': 10,
'endtime': UTCDateTime(1991, 2, 21, 23, 59, 50, 430000),
'excess_bytes': 0,
'filesize': 4096,
'io_and_clock_flags': 0,
'location': '',
'network': 'MN',
'npts': 60,
'number_of_records': 1,
'record_length': 4096,
'samp_rate': 0.1,
'starttime': UTCDateTime(1991, 2, 21, 23, 50, 0, 430000),
'station': 'TNV',
'time_correction': 0
}
def test_issue2069(self, testdata):
"""
Tests the util._get_ms_file_info method with sample rate of 0.
Reads a datafile and sets sr factor and multipier to 0 and and mseed
ASCII LOG file with a sample rate of 0.
"""
# Test with a file by setting sr = 0
filename = testdata['BW.BGLD.__.EHE.D.2008.001.first_10_records']
fmt = '>HHBBBxHHhhBBBxlxxH'
with open(filename, "rb") as fh:
with io.BytesIO(fh.read()) as buf:
buf.seek(20, 0)
data = buf.read(28)
values = list(unpack(fmt, data))
values[7] = 0
values[8] = 0
data = pack(fmt, *values)
buf.seek(20, 0)
buf.write(data)
buf.seek(0, 0)
info = util.get_record_information(buf)
assert info['samp_rate'] == 0
# Test with an actual sr = 0 file
filename = testdata['rt130_sr0_cropped.mseed']
info = util.get_record_information(filename)
assert info['samp_rate'] == 0
def test_get_data_quality(self, testdata):
"""
This test reads a self-made Mini-SEED file with set Data Quality Bits.
A real test file would be better as this test tests a file that was
created by the inverse method that reads the bits.
The method used to be called getTimingAndDataQuality() but has in
the meanwhile been replaced by a more general get_flags() method.
This test uses the general method but is otherwise not altered.
"""
filename = testdata['qualityflags.mseed']
# Read quality flags.
result = util.get_flags(filename, timing_quality=False,
io_flags=False, activity_flags=False,
data_quality_flags=True)
# The test file contains 18 records. The first record has no set bit,
# bit 0 of the second record is set, bit 1 of the third, ..., bit 7 of
# the 9th record is set. The last nine records have 0 to 8 set bits,
# starting with 0 bits, bit 0 is set, bits 0 and 1 are set...
# Altogether the file contains 44 set bits.
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 9,
"digitizer_clipping": 8,
"spikes": 7,
"glitches": 6,
"missing_padded_data": 5,
"telemetry_sync_error": 4,
"digital_filter_charging": 3,
"suspect_time_tag": 2}
# No set quality flags should result in a list of zeros.
filename = testdata['test.mseed']
result = util.get_flags(filename, timing_quality=False,
io_flags=False, activity_flags=False,
data_quality_flags=True)
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 0,
"digitizer_clipping": 0,
"spikes": 0,
"glitches": 0,
"missing_padded_data": 0,
"telemetry_sync_error": 0,
"digital_filter_charging": 0,
"suspect_time_tag": 0}
def test_get_flags(self):
"""
This test reads a real Mini-SEED file which has been
modified ad hoc.
"""
with NamedTemporaryFile() as tf:
_create_mseed_file(
tf.name, record_count=112, seed=3412,
starttime="2015-10-16T00:00:00", skiprecords=5, flags={
'data_quality_flags': {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8},
'activity_flags': {
"calibration_signal": 1,
"time_correction_applied": 2,
"event_begin": 3,
"event_end": 53,
"positive_leap": 4,
"negative_leap": 11,
"event_in_progress": 5},
'io_and_clock_flags': {
"station_volume": 1,
"long_record_read": 33,
"short_record_read": 2,
"start_time_series": 3,
"end_time_series": 4,
"clock_locked": 32}})
result = util.get_flags(tf.name, timing_quality=False)
assert result["record_count"] == 112
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8}
assert result['activity_flags_counts'] == {
"calibration_signal": 1,
"time_correction_applied": 2,
"event_begin": 3,
"event_end": 53,
"positive_leap": 4,
"negative_leap": 11,
"event_in_progress": 5}
assert result['io_and_clock_flags_counts'] == {
"station_volume": 1,
"long_record_read": 33,
"short_record_read": 2,
"start_time_series": 3,
"end_time_series": 4,
"clock_locked": 32}
# Test time settings. Everything is still present.
starttime = '2015-10-16T00:00:00'
result = util.get_flags(tf.name, starttime=starttime,
io_flags=False, activity_flags=False,
timing_quality=False)
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8}
# Nothing is present.
starttime = '2015-10-17T00:00:00'
result = util.get_flags(tf.name, starttime=starttime,
io_flags=False, activity_flags=False,
timing_quality=False)
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 0,
"digitizer_clipping": 0,
"spikes": 0,
"glitches": 0,
"missing_padded_data": 0,
"telemetry_sync_error": 0,
"digital_filter_charging": 0,
"suspect_time_tag": 0}
# There are exactly 10 records at the front which have nothing.
# Thus reading until that point should yield nothing.
endtime = UTCDateTime('2015-10-15T00:00:00') + 50 * 10
result = util.get_flags(tf.name, endtime=endtime,
io_flags=False, activity_flags=False,
timing_quality=False)
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 0,
"digitizer_clipping": 0,
"spikes": 0,
"glitches": 0,
"missing_padded_data": 0,
"telemetry_sync_error": 0,
"digital_filter_charging": 0,
"suspect_time_tag": 0}
# Reading after that point should yield everything.
result = util.get_flags(tf.name, starttime=endtime,
io_flags=False, activity_flags=False,
timing_quality=False)
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8}
def test_get_flags_from_files_and_file_like_objects(self):
"""
Similar to test_get_flags() but with file-like objects.
"""
with NamedTemporaryFile() as tf:
_create_mseed_file(
tf.name, record_count=112, seed=3412,
starttime="2015-10-16T00:00:00", skiprecords=5, flags={
'data_quality_flags': {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8},
'activity_flags': {
"calibration_signal": 1,
"time_correction_applied": 2,
"event_begin": 3,
"event_end": 53,
"positive_leap": 4,
"negative_leap": 11,
"event_in_progress": 5},
'io_and_clock_flags': {
"station_volume": 1,
"long_record_read": 33,
"short_record_read": 2,
"start_time_series": 3,
"end_time_series": 4,
"clock_locked": 32}})
tf.seek(0, 0)
# Directly from the file.
result = util.get_flags(tf, timing_quality=False)
assert result["record_count"] == 112
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8}
assert result['activity_flags_counts'] == {
"calibration_signal": 1,
"time_correction_applied": 2,
"event_begin": 3,
"event_end": 53,
"positive_leap": 4,
"negative_leap": 11,
"event_in_progress": 5}
assert result['io_and_clock_flags_counts'] == {
"station_volume": 1,
"long_record_read": 33,
"short_record_read": 2,
"start_time_series": 3,
"end_time_series": 4,
"clock_locked": 32}
# From a BytesIO objects.
tf.seek(0, 0)
with io.BytesIO(tf.read()) as buf:
result = util.get_flags(buf, timing_quality=False)
assert result["record_count"] == 112
assert result['data_quality_flags_counts'] == {
"amplifier_saturation": 55,
"digitizer_clipping": 72,
"spikes": 55,
"glitches": 63,
"missing_padded_data": 55,
"telemetry_sync_error": 63,
"digital_filter_charging": 4,
"suspect_time_tag": 8}
assert result['activity_flags_counts'] == {
"calibration_signal": 1,
"time_correction_applied": 2,
"event_begin": 3,
"event_end": 53,
"positive_leap": 4,
"negative_leap": 11,
"event_in_progress": 5}
assert result['io_and_clock_flags_counts'] == {
"station_volume": 1,
"long_record_read": 33,
"short_record_read": 2,
"start_time_series": 3,
"end_time_series": 4,
"clock_locked": 32}
def test_get_start_and_end_time(self, testdata):
"""
Tests getting the start- and endtime of a file.
The values are compared with the results of reading the full files.
"""
mseed_filenames = ['BW.BGLD.__.EHE.D.2008.001.first_10_records',
'test.mseed', 'timingquality.mseed']
for _i in mseed_filenames:
filename = testdata[_i]
# Get the start- and end time.
(start, end) = util.get_start_and_end_time(filename)
# Parse the whole file.
stream = _read_mseed(filename)
assert start == stream[0].stats.starttime
assert end == stream[0].stats.endtime
def test_get_timing_quality(self, testdata):
"""
This test reads a self-made Mini-SEED file with Timing Quality
information in Blockette 1001. A real test file would be better.
The test file contains 101 records with the timing quality ranging from
0 to 100 in steps of 1.
The result is compared to the result from the following R command:
V <- 0:100; min(V); max(V); mean(V); median(V); quantile(V, 0.75,
type = 3); quantile(V, 0.25, type = 3)
The method used to be called getTimingAndDataQuality() but has in
the meanwhile been replaced by a more general get_flags() method.
This test uses the general method but is otherwise not altered.
"""
filename = testdata['timingquality.mseed']
result = util.get_flags(filename, timing_quality=True,
io_flags=False, activity_flags=False,
data_quality_flags=False)
# Test all values separately.
np.testing.assert_allclose(
sorted(result["timing_quality"]["all_values"]),
np.arange(0, 101))
del result["timing_quality"]["all_values"]
assert result["timing_quality"] == {
'upper_quartile': 75.0,
'min': 0.0,
'lower_quartile': 25.0,
'mean': 50.0,
'median': 50.0,
'max': 100.0}
# No timing quality set should result in an empty dictionary.
filename = testdata['BW.BGLD.__.EHE.D.2008.001.first_10_records']
result = util.get_flags(filename, timing_quality=True,
io_flags=False, activity_flags=False,
data_quality_flags=False)
assert result["timing_quality"] == {}
def test_unpack_steim_1(self, testdata):
"""
Test decompression of Steim1 strings. Remove 64 Bytes of header
by hand, see SEEDManual_V2.4.pdf page 100.
"""
steim1_file = testdata['BW.BGLD.__.EHE.D.2008.001.first_record']
# 64 Bytes header.
d = np.fromfile(steim1_file, dtype=np.uint8)[64:]
data = util._unpack_steim_1(d, 412, swapflag=self.swap,
verbose=0)
data_record = _read_mseed(steim1_file)[0].data
np.testing.assert_array_equal(data, data_record)
def test_unpack_steim_2(self, testdata):
"""
Test decompression of Steim2 strings. Remove 128 Bytes of header
by hand, see SEEDManual_V2.4.pdf page 100.
"""
steim2_file = testdata['steim2.mseed']
# 128 Bytes header.
d = np.fromfile(steim2_file, dtype=np.uint8)[128:]
data = util._unpack_steim_2(d, 5980, swapflag=self.swap,
verbose=0)
data_record = _read_mseed(steim2_file)[0].data
np.testing.assert_array_equal(data, data_record)
def test_time_shifting(self, testdata):
"""
Tests the shift_time_of_file() function.
"""
with NamedTemporaryFile() as tf:
output_filename = tf.name
# Test a normal file first.
filename = testdata['BW.BGLD.__.EHE.D.2008.001.first_10_records']
# Shift by one second.
util.shift_time_of_file(filename, output_filename, 10000)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime += 1
assert st_before == st_after
# Shift by 22 seconds in the other direction.
util.shift_time_of_file(filename, output_filename, -220000)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime -= 22
assert st_before == st_after
# Shift by 11.33 seconds.
util.shift_time_of_file(filename, output_filename, 113300)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime += 11.33
assert st_before == st_after
# Test a special case with the time correction applied flag set but
# no actual time correction in the field.
filename = testdata[
'one_record_time_corr_applied_but_time_corr_is_zero.mseed']
# Positive shift.
util.shift_time_of_file(filename, output_filename, 22000)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime += 2.2
assert st_before == st_after
# Negative shift.
util.shift_time_of_file(filename, output_filename, -333000)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime -= 33.3
assert st_before == st_after
def test_time_spreading(self, testdata):
"""
Tests spread_time_over_file() function.
"""
with NamedTemporaryFile() as tf:
output_filename = tf.name
filename = testdata['BW.BGLD.__.EHE.D.2008.001.first_10_records']
# Shift by 0.9 seconds (0.1 s gap between each blockette in file)
util.spread_time_over_file(filename, output_filename, 9000)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before_et = st_before[0].stats.endtime + 0.9
st_after_et = st_after[-1].stats.endtime
assert st_before_et - st_after_et == 0
# Check individual data blockette offsets
info = util.get_record_information(filename)
rl = info["record_length"]
for i in range(0, 10):
# Each blockette should be progressively shifted by 0.1 s
o = i*rl
i_bef = util.get_record_information(filename, offset=o)
i_aft = util.get_record_information(output_filename, offset=o)
diff = int((i_aft['starttime'] - i_bef['starttime'])*10 + 0.5)
assert diff == i
def test_time_shifting_special_case(self, testdata):
"""
Sometimes actually changing the time value is necessary. This works but
is considered experimental and thus emits a warning. Therefore Python
>= 2.6 only.
"""
with NamedTemporaryFile() as tf:
output_filename = tf.name
# This file was created only for testing purposes.
filename = testdata[
'one_record_already_applied_time_correction.mseed']
with warnings.catch_warnings(record=True):
warnings.simplefilter('error', UserWarning)
with pytest.raises(UserWarning):
util.shift_time_of_file(
input_file=filename, output_file=output_filename,
timeshift=123400)
# Now ignore the warnings and test the default values.
warnings.simplefilter('ignore', UserWarning)
util.shift_time_of_file(input_file=filename,
output_file=output_filename,
timeshift=123400)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime += 12.34
assert st_before == st_after
# Test negative shifts.
with warnings.catch_warnings(record=True):
warnings.simplefilter('ignore', UserWarning)
util.shift_time_of_file(input_file=filename,
output_file=output_filename,
timeshift=-22222)
st_before = _read_mseed(filename)
st_after = _read_mseed(output_filename)
st_before[0].stats.starttime -= 2.2222
assert st_before == st_after
def test_check_flag_value(self):
"""
Test case for obspy.io.mseed.util._check_flag_value
"""
# Valid value for a boolean flag
corrected_flag = util._check_flag_value(True)
assert isinstance(corrected_flag, bool)
assert corrected_flag
corrected_flag = util._check_flag_value(False)
assert isinstance(corrected_flag, bool)
assert not corrected_flag
# Valid value for an instant flag #1
flag_value = {"INSTANT": UTCDateTime("2009-12-23T06:00:00.0")}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-23T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-23T06:00:00.0")
# Valid value for an instant flag #2
flag_value = {"INSTANT": [UTCDateTime("2009-12-24T06:00:00.0")]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-24T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-24T06:00:00.0")
# Valid value for several instant flags
flag_value = {"INSTANT": [UTCDateTime("2009-12-24T06:00:00.0"),
UTCDateTime("2009-12-24T06:01:00.0")]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-24T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-24T06:00:00.0")
assert corrected_flag[1][0] == UTCDateTime("2009-12-24T06:01:00.0")
assert corrected_flag[1][1] == UTCDateTime("2009-12-24T06:01:00.0")
# Valid value for a duration #1
flag_value = {"DURATION": [UTCDateTime("2009-12-25T06:00:00.0"),
UTCDateTime("2009-12-25T06:10:00.0")]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-25T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-25T06:10:00.0")
# Valid value for a duration #2
flag_value = {"DURATION": [(UTCDateTime("2009-12-25T16:00:00.0"),
UTCDateTime("2009-12-25T16:10:00.0"))]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-25T16:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-25T16:10:00.0")
# Valid value for several durations #1
flag_value = {"DURATION": [UTCDateTime("2009-12-24T06:00:00.0"),
UTCDateTime("2009-12-24T06:10:00.0"),
UTCDateTime("2009-12-24T07:00:00.0"),
UTCDateTime("2009-12-24T07:10:00.0")]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-24T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-24T06:10:00.0")
assert corrected_flag[1][0] == UTCDateTime("2009-12-24T07:00:00.0")
assert corrected_flag[1][1] == UTCDateTime("2009-12-24T07:10:00.0")
# Valid value for several durations #2
flag_value = {"DURATION": [(UTCDateTime("2009-12-25T06:00:00.0"),
UTCDateTime("2009-12-25T06:10:00.0")),
(UTCDateTime("2009-12-25T07:00:00.0"),
UTCDateTime("2009-12-25T07:10:00.0"))]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert corrected_flag[0][0] == UTCDateTime("2009-12-25T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-25T06:10:00.0")
assert corrected_flag[1][0] == UTCDateTime("2009-12-25T07:00:00.0")
assert corrected_flag[1][1] == UTCDateTime("2009-12-25T07:10:00.0")
# Test of the (valid) example 1 written in set_flags_in_fixed_headers's
# docstring
date1 = UTCDateTime("2009-12-23T06:00:00.0")
date2 = UTCDateTime("2009-12-23T06:30:00.0")
date3 = UTCDateTime("2009-12-24T10:00:00.0")
date4 = UTCDateTime("2009-12-24T10:30:00.0")
date5 = UTCDateTime("2009-12-26T18:00:00.0")
date6 = UTCDateTime("2009-12-26T18:04:00.0")
flag_value = {"INSTANT": [date5, date6],
"DURATION": [(date1, date2), (date3, date4)]}
corrected_flag = util._check_flag_value(flag_value)
assert isinstance(corrected_flag, list)
assert len(corrected_flag) == 4
# Sort by start date to ensure uniqueness of the list
corrected_flag.sort(key=lambda val: val[0])
assert len(corrected_flag) == 4
assert corrected_flag[0][0] == UTCDateTime("2009-12-23T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-23T06:30:00.0")
assert corrected_flag[1][0] == UTCDateTime("2009-12-24T10:00:00.0")
assert corrected_flag[1][1] == UTCDateTime("2009-12-24T10:30:00.0")
assert corrected_flag[2][0] == UTCDateTime("2009-12-26T18:00:00.0")
assert corrected_flag[2][1] == UTCDateTime("2009-12-26T18:00:00.0")
assert corrected_flag[3][0] == UTCDateTime("2009-12-26T18:04:00.0")
assert corrected_flag[3][1] == UTCDateTime("2009-12-26T18:04:00.0")
# Test of the (valid) example 2 written in set_flags_in_fixed_headers's
# docstring
flag_value = {"INSTANT": [date5, date6],
"DURATION": [date1, date2, date3, date4]}
assert len(corrected_flag) == 4
# Sort by start date to ensure uniqueness of the list
corrected_flag.sort(key=lambda val: val[0])
assert len(corrected_flag) == 4
assert corrected_flag[0][0] == UTCDateTime("2009-12-23T06:00:00.0")
assert corrected_flag[0][1] == UTCDateTime("2009-12-23T06:30:00.0")
assert corrected_flag[1][0] == UTCDateTime("2009-12-24T10:00:00.0")
assert corrected_flag[1][1] == UTCDateTime("2009-12-24T10:30:00.0")
assert corrected_flag[2][0] == UTCDateTime("2009-12-26T18:00:00.0")
assert corrected_flag[2][1] == UTCDateTime("2009-12-26T18:00:00.0")
assert corrected_flag[3][0] == UTCDateTime("2009-12-26T18:04:00.0")
assert corrected_flag[3][1] == UTCDateTime("2009-12-26T18:04:00.0")
# Invalid type for datation flag
flag_value = "invalid because str"
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Invalid key (neither "INSTANT" nor "DURATION") for datation dict
flag_value = {"INVALID_KEY": [UTCDateTime("2009-12-25T07:10:00.0")]}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Invalid value type for key "INSTANT"
flag_value = {"INSTANT": "invalid because str"}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Invalid value type for key "DURATION"
flag_value = {"DURATION": "invalid because str"}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Invalid len for key DURATION #1
flag_value = {"DURATION": [UTCDateTime("2009-12-25T06:00:00.0"),
UTCDateTime("2009-12-25T06:10:00.0"),
UTCDateTime("2009-12-25T06:20:00.0")]}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Invalid type in DURATION
flag_value = {"DURATION": [UTCDateTime("2009-12-25T06:00:00.0"),
UTCDateTime("2009-12-25T06:10:00.0"),
UTCDateTime("2009-12-25T06:20:00.0"),
"invalid because str"]}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
# Start after end in DURATION
flag_value = {"DURATION": [UTCDateTime("2010-12-27T19:50:59.0"),
UTCDateTime("2009-12-25T06:00:00.0")]}
with pytest.raises(ValueError):
util._check_flag_value(flag_value)
def test_search_flag_in_blockette(self):
"""
Test case for obspy.io.mseed.util._search_flag_in_blockette
"""
# Write dummy file
npts = 2000
np.random.seed(42) # make test reproducible
data = np.random.randint(-1000, 1000, npts).astype(np.int32)
# This header ensures presence of blockettes 1000 and 1001
stat_header = {'network': 'NE', 'station': 'STATI', 'location': 'LO',
'channel': 'CHA', 'npts': len(data), 'sampling_rate': 1,
'mseed': {'dataquality': 'D',
'blkt1001': {'timing_quality': 63}}}
stat_header['starttime'] = UTCDateTime(datetime(2012, 8, 1,
12, 0, 0, 42))
trace1 = Trace(data=data, header=stat_header)
st = Stream([trace1])
with NamedTemporaryFile() as tf:
st.write(tf, format="mseed", encoding=11, reclen=512)
tf.seek(0, os.SEEK_SET)
file_name = tf.name
with open(file_name, "rb") as file_desc:
file_desc.seek(0, os.SEEK_SET)
# Test from file start
read_bytes = util._search_flag_in_blockette(
file_desc, 48, 1001, 4, 1)
assert not (read_bytes is None)
assert unpack(">B", read_bytes)[0] == 63
# Test from middle of a record header
file_desc.seek(14, os.SEEK_CUR)
file_pos = file_desc.tell()
read_bytes = util._search_flag_in_blockette(
file_desc, 34, 1000, 6, 1)
assert not (read_bytes is None)
assert unpack(">B", read_bytes)[0] == 9
# Check that file_desc position has not changed
assert file_desc.tell() == file_pos
# Test from middle of a record data
file_desc.seek(60, os.SEEK_CUR)
read_bytes = util._search_flag_in_blockette(
file_desc, -26, 1001, 5, 1)
assert not (read_bytes is None)
assert unpack(">B", read_bytes)[0] == 42
# Test another record. There is at least 3 records in a
# mseed with 2000 data points and 512 bytes record length
file_desc.seek(1040, os.SEEK_SET)
read_bytes = util._search_flag_in_blockette(file_desc,
32, 1001, 4, 1)
assert unpack(">B", read_bytes)[0] == 63
# Test missing blockette
read_bytes = util._search_flag_in_blockette(file_desc,
32, 201, 4, 4)
assert read_bytes is None
def test_convert_flags_to_raw_bytes(self):
"""
Test case for obspy.io.mseed.util._convert_flags_to_raw_byte
"""
recstart = UTCDateTime("2009-12-25T06:00:00.0")
recend = UTCDateTime("2009-12-26T06:00:00.0")
user_flags = {
# boolean flags
'calib_signal': True,
'time_correction': False,
# instant value
'begin_event': [(UTCDateTime("2009-12-25T07:00:00.0"),
UTCDateTime("2009-12-25T07:00:00.0"))],
# duration value (inside record)
'end_event': [(UTCDateTime("2009-12-25T07:00:00.0"),
UTCDateTime("2009-12-25T07:04:00.0"))],
# instant value at the end of the record
'positive_leap': [(UTCDateTime("2009-12-26T06:00:00.0"),
UTCDateTime("2009-12-26T06:00:00.0"))],
# value before record start
'negative_leap': [(UTCDateTime("2001-12-25T06:00:00.0"),
UTCDateTime("2001-12-25T06:00:00.0"))],
# value after record end
'event_in_progress': [(UTCDateTime("2020-12-25T06:00:00.0"),
UTCDateTime("2020-12-25T06:00:00.0"))]}
act_flags = util._convert_flags_to_raw_byte(
FIXED_HEADER_ACTIVITY_FLAGS, user_flags, recstart, recend)
assert act_flags == 13
user_flags = {
# duration value (across record start)
'sta_vol_parity_error_possible':
[(UTCDateTime("2009-12-25T00:00:00.0"),
UTCDateTime("2009-12-26T00:00:00.0"))],
# duration value (across record end)
'long_record_read': [(UTCDateTime("2009-12-26T00:00:00.0"),
UTCDateTime("2009-12-27T00:00:00.0"))],
# duration value (record inside duration)
'short_record_read': [(UTCDateTime("2009-12-25T00:00:00.0"),
UTCDateTime("2009-12-27T00:00:00.0"))],
# several datation periods, one matching the record
'start_of_time_series': [(UTCDateTime("2008-12-25T00:00:00.0"),
UTCDateTime("2008-12-27T00:00:00.0")),
(UTCDateTime("2009-12-26T00:00:00.0"),
UTCDateTime("2009-12-26T00:00:00.0")),
(UTCDateTime("2010-12-25T00:00:00.0"),
UTCDateTime("2010-12-27T00:00:00.0"))]}
io_clock = util._convert_flags_to_raw_byte(
FIXED_HEADER_IO_CLOCK_FLAGS, user_flags, recstart, recend)
assert io_clock == 15
# Quick check of data quality flags
user_flags = {'amplifier_sat_detected': True,
'digitizer_clipping_detected': False,
'spikes_detected': True,
'glitches_detected': False,
'missing_padded_data_present': True,
'telemetry_sync_error': False,
'digital_filter_maybe_charging': True,
'time_tag_questionable': False}
data_qual = util._convert_flags_to_raw_byte(
FIXED_HEADER_DATA_QUAL_FLAGS, user_flags, recstart, recend)
assert data_qual == 85
def test_set_flags_in_fixed_header(self):
"""
Test case for obspy.io.mseed.util.set_flags_in_fixed_headers
"""