-
Notifications
You must be signed in to change notification settings - Fork 3
/
zen.py
1168 lines (978 loc) · 38.2 KB
/
zen.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 -*-
"""
====================
Zen
====================
* Tools for reading and writing files for Zen and processing software
* Tools for copying data from SD cards
* Tools for copying schedules to SD cards
Created on Tue Jun 11 10:53:23 2013
Updated August 2020 (JP)
:copyright:
Jared Peacock (jpeacock@usgs.gov)
:license:
MIT
"""
# ==============================================================================
import datetime
import struct
from pathlib import Path
import logging
import numpy as np
from mt_metadata.utils.mttime import MTime
from mt_metadata.timeseries.filters import (
ChannelResponseFilter,
FrequencyResponseTableFilter,
CoefficientFilter,
)
from mt_metadata.timeseries import Station, Run, Electric, Magnetic
from mth5.io.zen import Z3DHeader, Z3DSchedule, Z3DMetadata
from mth5.timeseries import ChannelTS
# ==============================================================================
#
# ==============================================================================
class Z3D:
"""
Deals with the raw Z3D files output by zen.
Arguments
-----------
**fn** : string
full path to .Z3D file to be read in
======================== ================================ =================
Attributes Description Default Value
======================== ================================ =================
_block_len length of data block to read in 65536
as chunks faster reading
_counts_to_mv_conversion conversion factor to convert 9.53674316406e-10
counts to mv
_gps_bytes number of bytes for a gps stamp 16
_gps_dtype data type for a gps stamp see below
_gps_epoch starting date of GPS time
format is a tuple (1980, 1, 6, 0,
0, 0, -1, -1, 0)
_gps_f0 first gps flag in raw binary
_gps_f1 second gps flag in raw binary
_gps_flag_0 first gps flag as an int32 2147483647
_gps_flag_1 second gps flag as an int32 -2147483648
_gps_stamp_length bit length of gps stamp 64
_leap_seconds leap seconds, difference 16
between UTC time and GPS
time. GPS time is ahead
by this much
_week_len week length in seconds 604800
df sampling rate of the data 256
fn Z3D file name None
gps_flag full gps flag _gps_f0+_gps_f1
gps_stamps np.ndarray of gps stamps None
header Z3DHeader object Z3DHeader
metadata Z3DMetadata Z3DMetadata
schedule Z3DSchedule Z3DSchedule
time_series np.ndarra(len_data) None
units units in which the data is in counts
zen_schedule time when zen was set to None
run
======================== ================================ =================
* gps_dtype is formated as np.dtype([('flag0', np.int32),
('flag1', np.int32),
('time', np.int32),
('lat', np.float64),
('lon', np.float64),
('num_sat', np.int32),
('gps_sens', np.int32),
('temperature', np.float32),
('voltage', np.float32),
('num_fpga', np.int32),
('num_adc', np.int32),
('pps_count', np.int32),
('dac_tune', np.int32),
('block_len', np.int32)])
:Example:
>>> import mtpy.usgs.zen as zen
>>> zt = zen.Zen3D(r"/home/mt/mt00/mt00_20150522_080000_256_EX.Z3D")
>>> zt.read_z3d()
>>> ------- Reading /home/mt/mt00/mt00_20150522_080000_256_EX.Z3D -----
--> Reading data took: 0.322 seconds
Scheduled time was 2015-05-22,08:00:16 (GPS time)
1st good stamp was 2015-05-22,08:00:18 (GPS time)
difference of 2.00 seconds
found 6418 GPS time stamps
found 1642752 data points
>>> zt.plot_time_series()
"""
def __init__(self, fn=None, **kwargs):
self.logger = logging.getLogger(f"{__name__}.{self.__class__.__name__}")
self.fn = fn
self.header = Z3DHeader(fn)
self.schedule = Z3DSchedule(fn)
self.metadata = Z3DMetadata(fn)
self._gps_stamp_length = kwargs.pop("stamp_len", 64)
self._gps_bytes = self._gps_stamp_length / 4
self.gps_stamps = None
self._gps_flag_0 = np.int32(2147483647)
self._gps_flag_1 = np.int32(-2147483648)
self._gps_f0 = self._gps_flag_0.tobytes()
self._gps_f1 = self._gps_flag_1.tobytes()
self.gps_flag = self._gps_f0 + self._gps_f1
self._gps_dtype = np.dtype(
[
("flag0", np.int32),
("flag1", np.int32),
("time", np.int32),
("lat", np.float64),
("lon", np.float64),
("gps_sens", np.int32),
("num_sat", np.int32),
("temperature", np.float32),
("voltage", np.float32),
("num_fpga", np.int32),
("num_adc", np.int32),
("pps_count", np.int32),
("dac_tune", np.int32),
("block_len", np.int32),
]
)
self._week_len = 604800
# '1980, 1, 6, 0, 0, 0, -1, -1, 0
self._gps_epoch = MTime("1980-01-06T00:00:00")
self._leap_seconds = 18
self._block_len = 2**16
# the number in the cac files is for volts, we want mV
self._counts_to_mv_conversion = 9.5367431640625e-10 * 1e3
self.num_sec_to_skip = 2
self.units = "counts"
self.sample_rate = None
self.time_series = None
self.ch_dict = {"hx": 1, "hy": 2, "hz": 3, "ex": 4, "ey": 5}
@property
def fn(self):
return self._fn
@fn.setter
def fn(self, fn):
if fn is not None:
self._fn = Path(fn)
else:
self._fn = None
@property
def file_size(self):
if self.fn is not None:
return self.fn.stat().st_size
return 0
@property
def n_samples(self):
if self.time_series is None:
if self.sample_rate:
return int(
(self.file_size - 512 * (1 + self.metadata.count)) / 4
+ 8 * self.sample_rate
)
else:
# assume just the 3 general metadata blocks
return int((self.file_size - 512 * 3) / 4)
else:
return self.time_series.size
@property
def station(self):
"""
station name
"""
return self.metadata.station
@station.setter
def station(self, station):
"""
station name
"""
self.metadata.station = station
@property
def dipole_length(self):
"""
dipole length
"""
length = 0
if self.metadata.ch_length is not None:
length = float(self.metadata.ch_length)
elif hasattr(self.metadata, "ch_offset_xyz1"):
# only ex and ey have xyz2
if hasattr(self.metadata, "ch_offset_xyz2"):
x1, y1, z1 = [
float(offset)
for offset in self.metadata.ch_offset_xyz1.split(":")
]
x2, y2, z2 = [
float(offset)
for offset in self.metadata.ch_offset_xyz2.split(":")
]
length = np.sqrt(
(x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2
)
length = np.round(length, 2)
else:
length = 0
elif self.metadata.ch_xyz1 is not None:
x1, y1 = [float(d) for d in self.metadata.ch_xyz1.split(":")]
x2, y2 = [float(d) for d in self.metadata.ch_xyz2.split(":")]
length = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) * 100.0
length = np.round(length, 2)
return length
@property
def azimuth(self):
"""
azimuth of instrument setup
"""
if self.metadata.ch_azimuth is not None:
return float(self.metadata.ch_azimuth)
elif self.metadata.rx_xazimuth is not None:
return float(self.metadata.rx_xazimuth)
else:
return None
@property
def component(self):
"""
channel
"""
return self.metadata.ch_cmp.lower()
@property
def latitude(self):
"""
latitude in decimal degrees
"""
return self.header.lat
@property
def longitude(self):
"""
longitude in decimal degrees
"""
return self.header.long
@property
def elevation(self):
"""
elevation in meters
"""
return self.header.alt
@property
def sample_rate(self):
"""
sampling rate
"""
return self.header.ad_rate
@sample_rate.setter
def sample_rate(self, sampling_rate):
"""
sampling rate
"""
if sampling_rate is not None:
self.header.ad_rate = float(sampling_rate)
@property
def start(self):
if self.gps_stamps is not None:
return self.get_UTC_date_time(
self.header.gpsweek, self.gps_stamps["time"][0]
)
return self.zen_schedule
@property
def end(self):
if self.gps_stamps is not None:
return self.get_UTC_date_time(
self.header.gpsweek, self.gps_stamps["time"][-1]
)
return self.start + (self.n_samples / self.sample_rate)
@property
def zen_schedule(self):
"""
zen schedule data and time
"""
if self.header.old_version is True:
return MTime(self.header.schedule)
return self.schedule.initial_start
@zen_schedule.setter
def zen_schedule(self, schedule_dt):
"""
on setting set schedule datetime
"""
if not isinstance(schedule_dt, MTime):
schedule_dt = MTime(schedule_dt)
raise TypeError(
"New schedule datetime must be type datetime.datetime"
)
self.schedule.initial_start = schedule_dt
@property
def coil_number(self):
"""
coil number
"""
if self.metadata.cal_ant is not None:
return self.metadata.cal_ant
elif self.metadata.ch_number is not None:
return self.metadata.ch_number
else:
return None
@property
def channel_number(self):
if self.metadata.ch_number:
ch_num = int(float(self.metadata.ch_number))
if ch_num > 6:
try:
ch_num = self.ch_dict[self.component]
except KeyError:
ch_num = 6
return ch_num
else:
try:
return self.ch_dict[self.component]
except KeyError:
return 0
@property
def channel_metadata(self):
"""Channel metadata"""
# fill the time series object
if "e" in self.component:
ch = Electric()
ch.dipole_length = self.dipole_length
ch.ac.start = (
self.time_series[0 : int(self.sample_rate)].std()
* self.header.ch_factor
)
ch.ac.end = (
self.time_series[-int(self.sample_rate) :].std()
* self.header.ch_factor
)
ch.dc.start = (
self.time_series[0 : int(self.sample_rate)].mean()
* self.header.ch_factor
)
ch.dc.end = (
self.time_series[-int(self.sample_rate) :].mean()
* self.header.ch_factor
)
elif "h" in self.component:
ch = Magnetic()
ch.sensor.id = (self.coil_number,)
ch.sensor.manufacturer = ("Geotell",)
ch.sensor.model = ("ANT-4",)
ch.sensor.type = ("induction coil",)
ch.h_field_max.start = (
self.time_series[0 : int(self.sample_rate)].max()
* self.header.ch_factor
)
ch.h_field_max.end = (
self.time_series[-int(self.sample_rate) :].max()
* self.header.ch_factor
)
ch.h_field_min.start = (
self.time_series[0 : int(self.sample_rate)].min()
* self.header.ch_factor
)
ch.h_field_min.end = (
self.time_series[-int(self.sample_rate) :].min()
* self.header.ch_factor
)
ch.time_period.start = self.start.isoformat()
ch.time_period.end = self.end.isoformat()
ch.component = self.component
ch.sample_rate = self.sample_rate
ch.measurement_azimuth = self.azimuth
ch.units = "digital counts"
ch.channel_number = self.channel_number
ch.filter.name = self.channel_response.names
ch.filter.applied = [False] * len(self.channel_response.names)
return ch
@property
def station_metadata(self):
"""station metadta"""
sm = Station()
sm.id = self.station
sm.fdsn.id = self.station
sm.location.latitude = self.latitude
sm.location.longitude = self.longitude
sm.location.elevation = self.elevation
sm.time_period.start = self.start.isoformat()
sm.time_period.end = self.end.isoformat()
sm.acquired_by.author = self.metadata.gdp_operator
return sm
@property
def run_metadata(self):
"""Run metadata"""
rm = Run()
rm.data_logger.firmware.version = self.header.version
rm.data_logger.id = self.header.data_logger
rm.data_logger.manufacturer = "Zonge International"
rm.data_logger.model = "ZEN"
rm.time_period.start = self.start.isoformat()
rm.time_period.end = self.end.isoformat()
rm.sample_rate = self.sample_rate
rm.data_type = "MTBB"
rm.time_period.start = self.start.isoformat()
rm.time_period.end = self.end.isoformat()
rm.acquired_by.author = self.metadata.gdp_operator
rm.id = f"sr{int(self.sample_rate)}_001"
return rm
@property
def counts2mv_filter(self):
"""
Create a counts2mv coefficient filter
.. note:: Needs to be 1/channel factor because we divided the
instrument response from the data.
"""
c2mv = CoefficientFilter()
c2mv.units_in = "digital counts"
c2mv.units_out = "millivolts"
c2mv.name = "zen_counts2mv"
c2mv.gain = 1.0 / self.header.ch_factor
c2mv.comments = "digital counts to millivolts"
return c2mv
@property
def coil_response(self):
"""
Make the coile response into a FAP filter
Phase must be in radians
"""
fap = None
if self.metadata.cal_ant is not None:
fap = FrequencyResponseTableFilter()
fap.units_in = "millivolts"
fap.units_out = "nanotesla"
fap.frequencies = self.metadata.coil_cal.frequency
fap.amplitudes = self.metadata.coil_cal.amplitude
fap.phases = self.metadata.coil_cal.phase / 1e3
fap.name = f"ant4_{self.coil_number}_response"
fap.comments = "induction coil response read from z3d file"
return fap
@property
def zen_response(self):
"""
Zen response, not sure the full calibration comes directly from the
Z3D file, so skipping for now. Will have to read a Zen##.cal file
to get the full calibration. This shouldn't be a big issue cause it
should roughly be the same for all channels and since the TF is
computing the ratio they will cancel out. Though we should look
more into this if just looking at calibrate time series.
"""
fap = None
find = False
if self.metadata.board_cal not in [None, []]:
if self.metadata.board_cal[0][0] == "":
return fap
sr_dict = {256: 0, 1024: 1, 4096: 4}
sr_int = sr_dict[int(self.sample_rate)]
fap_table = self.metadata.board_cal[
np.where(self.metadata.board_cal.rate == sr_int)
]
frequency = fap_table.frequency
amplitude = fap_table.amplitude
phase = fap_table.phase / 1e3
find = True
elif self.metadata.cal_board is not None:
try:
fap_dict = self.metadata.cal_board[int(self.sample_rate)]
frequency = fap_dict["frequency"]
amplitude = fap_dict["amplitude"]
phase = fap_dict["phase"]
find = True
except KeyError:
try:
fap_str = self.metadata.cal_board["cal.ch"]
for ss in fap_str.split(";"):
freq, _, resp = ss.split(",")
ff, amp, phs = [float(item) for item in resp.split(":")]
if float(freq) == self.sample_rate:
frequency = ff
amplitude = amp
phase = phs / 1e3
find = True
except KeyError:
return fap
if find:
freq = np.logspace(np.log10(6.00000e-04), np.log10(8.19200e03), 48)
freq[np.where(freq >= frequency)[0][0]] = frequency
amp = np.ones(48)
phases = np.zeros(48)
index = np.where(freq == frequency)[0][0]
amp[index] = amplitude
phases[index] = phase
fap = FrequencyResponseTableFilter()
fap.units_in = "millivolts"
fap.units_out = "millivolts"
fap.frequencies = freq
fap.amplitudes = amp
fap.phases = phases
fap.name = f"{self.header.data_logger.lower()}_{self.sample_rate:.0f}_response"
fap.comments = "data logger response read from z3d file"
return None
@property
def channel_response(self):
filter_list = [self.counts2mv_filter]
if self.zen_response:
filter_list.append(self.zen_response)
if self.coil_response:
filter_list.append(self.coil_response)
elif self.dipole_filter:
filter_list.append(self.dipole_filter)
return ChannelResponseFilter(filters_list=filter_list)
@property
def dipole_filter(self):
dipole = None
if self.dipole_length != 0:
dipole = CoefficientFilter()
dipole.units_in = "millivolts"
dipole.units_out = "millivolts per kilometer"
dipole.name = f"dipole_{self.dipole_length:.2f}m"
dipole.gain = self.dipole_length / 1000.0
dipole.comments = "convert to electric field"
return dipole
def _get_gps_stamp_type(self, old_version=False):
"""
get the correct stamp type.
Older versions the stamp length was 36 bits
New versions have a 64 bit stamp
"""
if old_version is True:
self._gps_dtype = np.dtype(
[
("gps", np.int32),
("time", np.int32),
("lat", np.float64),
("lon", np.float64),
("block_len", np.int32),
("gps_accuracy", np.int32),
("temperature", np.float32),
]
)
self._gps_stamp_length = 36
self._gps_bytes = self._gps_stamp_length / 4
self._gps_flag_0 = -1
self._block_len = int(self._gps_stamp_length + self.sample_rate * 4)
self.gps_flag = self._gps_f0
else:
return
# ======================================
def _read_header(self, fn=None, fid=None):
"""
read header information from Z3D file
Arguments
---------------
**fn** : string
full path to Z3D file to read
**fid** : file object
if the file is open give the file id object
Outputs:
----------
* fills the Zen3ZD.header object's attributes
Example with just a file name
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_header(fn)
Example with file object
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dfid = open(fn, 'rb')
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_header(fid=Z3Dfid)
"""
if fn is not None:
self.fn = fn
self.header.read_header(fn=self.fn, fid=fid)
if self.header.old_version:
if self.header.box_number is None:
self.header.box_number = "6666"
# ======================================
def _read_schedule(self, fn=None, fid=None):
"""
read schedule information from Z3D file
Arguments
---------------
**fn** : string
full path to Z3D file to read
**fid** : file object
if the file is open give the file id object
Outputs:
----------
* fills the Zen3ZD.schedule object's attributes
Example with just a file name
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_schedule(fn)
Example with file object
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dfid = open(fn, 'rb')
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_schedule(fid=Z3Dfid)
"""
if fn is not None:
self.fn = fn
self.schedule.read_schedule(fn=self.fn, fid=fid)
if self.header.old_version:
self.schedule.initial_start = MTime(
self.header.schedule, gps_time=True
)
# ======================================
def _read_metadata(self, fn=None, fid=None):
"""
read header information from Z3D file
Arguments
---------------
**fn** : string
full path to Z3D file to read
**fid** : file object
if the file is open give the file id object
Outputs:
----------
* fills the Zen3ZD.metadata object's attributes
Example with just a file name
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_metadata(fn)
Example with file object
------------
>>> import mtpy.usgs.zen as zen
>>> fn = r"/home/mt/mt01/mt01_20150522_080000_256_EX.Z3D"
>>> Z3Dfid = open(fn, 'rb')
>>> Z3Dobj = zen.Zen3D()
>>> Z3Dobj.read_metadata(fid=Z3Dfid)
"""
if fn is not None:
self.fn = fn
if self.header.old_version:
self.metadata._schedule_metadata_len = 0
self.metadata.read_metadata(fn=self.fn, fid=fid)
# =====================================
def read_all_info(self):
"""
Read header, schedule, and metadata
"""
with open(self.fn, "rb") as file_id:
self._read_header(fid=file_id)
self._read_schedule(fid=file_id)
self._read_metadata(fid=file_id)
def _read_raw_string(self, fid):
"""
read raw sting into data
:param fid: DESCRIPTION
:type fid: TYPE
:return: DESCRIPTION
:rtype: TYPE
"""
# move the read value to where the end of the metadata is
fid.seek(self.metadata.m_tell)
# initalize a data array filled with zeros, everything goes into
# this array then we parse later
data = np.zeros(self.n_samples, dtype=np.int32)
# go over a while loop until the data cound exceed the file size
data_count = 0
while True:
# need to make sure the last block read is a multiple of 32 bit
read_len = min(
[
self._block_len,
int(32 * ((self.file_size - fid.tell()) // 32)),
]
)
test_str = np.frombuffer(fid.read(read_len), dtype=np.int32)
if len(test_str) == 0:
break
data[data_count : data_count + len(test_str)] = test_str
data_count += test_str.size
return data
def _unpack_data(self, data, gps_stamp_index):
""" """
for ii, gps_find in enumerate(gps_stamp_index):
try:
data[gps_find + 1]
except IndexError:
pass
self.logger.warning(
f"Failed gps stamp {ii+1} out of {len(gps_stamp_index)}"
)
break
if (
self.header.old_version is True
or data[gps_find + 1] == self._gps_flag_1
):
gps_str = struct.pack(
"<" + "i" * int(self._gps_bytes),
*data[int(gps_find) : int(gps_find + self._gps_bytes)],
)
self.gps_stamps[ii] = np.frombuffer(
gps_str, dtype=self._gps_dtype
)
if ii > 0:
self.gps_stamps[ii]["block_len"] = (
gps_find - gps_stamp_index[ii - 1] - self._gps_bytes
)
elif ii == 0:
self.gps_stamps[ii]["block_len"] = 0
data[int(gps_find) : int(gps_find + self._gps_bytes)] = 0
return data
# ======================================
def read_z3d(self, z3d_fn=None):
"""
read in z3d file and populate attributes accordingly
1. Read in the entire file as chunks as np.int32.
2. Extract the gps stamps and convert accordingly. Check to make sure
gps time stamps are 1 second apart and incrementing as well as
checking the number of data points between stamps is the
same as the sampling rate.
3. Converts gps_stamps['time'] to seconds relative to header.gps_week
Note we skip the first two gps stamps because there is something
wrong with the data there due to some type of buffering.
Therefore the first GPS time is when the time series starts, so you
will notice that gps_stamps[0]['block_len'] = 0, this is because there
is nothing previous to this time stamp and so the 'block_len' measures
backwards from the corresponding time index.
4. Put the data chunks into Pandas data frame that is indexed by time
:Example:
>>> from mth5.io import zen
>>> z_obj = zen.Z3D(r"home/mt_data/zen/mt001.z3d")
>>> z_obj.read_z3d()
"""
if z3d_fn is not None:
self.fn = z3d_fn
self.logger.debug(f"Reading {self.fn}")
st = datetime.datetime.now()
# using the with statement works in Python versions 2.7 or higher
# the added benefit of the with statement is that it will close the
# file object upon reading completion.
with open(self.fn, "rb") as file_id:
self._read_header(fid=file_id)
self._read_schedule(fid=file_id)
self._read_metadata(fid=file_id)
if self.header.old_version is True:
self._get_gps_stamp_type(True)
data = self._read_raw_string(file_id)
self.raw_data = data.copy()
# find the gps stamps
gps_stamp_find = self.get_gps_stamp_index(data, self.header.old_version)
# skip the first two stamps and trim data
try:
data = data[gps_stamp_find[self.num_sec_to_skip] :]
except IndexError:
msg = f"Data is too short, cannot open file {self.fn}"
self.logger.error(msg)
raise ZenGPSError(msg)
# find gps stamps of the trimmed data
gps_stamp_find = self.get_gps_stamp_index(data, self.header.old_version)
# read data chunks and GPS stamps
self.gps_stamps = np.zeros(len(gps_stamp_find), dtype=self._gps_dtype)
data = self._unpack_data(data, gps_stamp_find)
# fill the time series
self.time_series = data[np.nonzero(data)]
# validate everything
self.validate_time_blocks()
self.convert_gps_time()
self.zen_schedule = self.check_start_time()
self.logger.debug(f"found {self.gps_stamps.shape[0]} GPS time stamps")
self.logger.debug(f"found {self.time_series.size} data points")
# time it
et = datetime.datetime.now()
read_time = (et - st).total_seconds()
self.logger.debug(f"Reading data took: {read_time:.3f} seconds")
# =================================================
def get_gps_stamp_index(self, ts_data, old_version=False):
"""
locate the time stamps in a given time series.
Looks for gps_flag_0 first, if the file is newer, then makes sure the
next value is gps_flag_1
:returns: list of gps stamps indicies
"""
# find the gps stamps
gps_stamp_find = np.where(ts_data == self._gps_flag_0)[0]
if old_version is False:
gps_stamp_find = [
gps_find
for gps_find in gps_stamp_find
if ts_data[gps_find + 1] == self._gps_flag_1
]
return gps_stamp_find
# =================================================
def trim_data(self):
"""
apparently need to skip the first 2 seconds of data because of
something to do with the SD buffer
This method will be deprecated after field testing
"""
# the block length is the number of data points before the time stamp
# therefore the first block length is 0. The indexing in python
# goes to the last index - 1 so we need to put in 3
ts_skip = self.gps_stamps["block_len"][0:3].sum()
self.gps_stamps = self.gps_stamps[2:]
self.gps_stamps[0]["block_len"] = 0
self.time_series = self.time_series[ts_skip:]
# =================================================
def check_start_time(self):
"""
check to make sure the scheduled start time is similar to
the first good gps stamp
"""
# make sure the time is in gps time
zen_start_utc = self.get_UTC_date_time(
self.header.gpsweek, self.gps_stamps["time"][0]
)
# estimate the time difference between the two
time_diff = zen_start_utc - self.schedule.initial_start
self.logger.debug(f"Scheduled time was {self.schedule.initial_start}")
self.logger.debug(f"1st good stamp was {zen_start_utc}")
self.logger.debug(f"difference of {time_diff:.2f} seconds")
return zen_start_utc
# ==================================================
def validate_gps_time(self):
"""
make sure each time stamp is 1 second apart
"""
t_diff = np.zeros_like(self.gps_stamps["time"])
for ii in range(len(t_diff) - 1):
t_diff[ii] = (
self.gps_stamps["time"][ii] - self.gps_stamps["time"][ii + 1]
)
bad_times = np.where(abs(t_diff) > 0.5)[0]
if len(bad_times) > 0:
self.logger.warning("BAD GPS TIMES:")
for bb in bad_times:
self.logger.warning(f"bad GPS time at index {bb} > 0.5 s")
# ===================================================
def validate_time_blocks(self):
"""
validate gps time stamps and make sure each block is the proper length
"""
# first check if the gps stamp blocks are of the correct length
bad_blocks = np.where(
self.gps_stamps["block_len"][1:] != self.header.ad_rate
)[0]
if len(bad_blocks) > 0:
if bad_blocks.max() < 5:
ts_skip = self.gps_stamps["block_len"][
0 : bad_blocks[-1] + 1