/
borealis_formats.py
2420 lines (2145 loc) · 88.3 KB
/
borealis_formats.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
# Copyright 2019 SuperDARN Canada, University of Saskatchewan
# Author: Marci Detwiller
"""
This file contains several classes with the fields that pertain to
SuperDARN Borealis HDF5 files. All formats inherit from BaseFormat.
Classes
-------
BorealisRawacf
BorealisBfiq
BorealisAntennasIq
BorealisRawrf
as well as previous versions of these classes, currently including
BorealisRawacfv0_5
BorealisBfiqv0_5
BorealisAntennasIqv0_5
BorealisRawrfv0_5
BorealisRawacfv0_4
BorealisBfiqv0_4
BorealisAntennasIqv0_4
BorealisRawrfv0_4
Globals
-------
borealis_version_dict
A lookup table for [version][filetype] that provides the appropriate class
given the version and filetype strings.
Notes
-----
- Debug data files such as Borealis stage iq data (an intermediate
product that can be generated during filtering and decimating, showing
progression from rawrf to output ptrs iq files) will not be included here.
This is a debug format only and should not be used for higher level
data products.
See Also
--------
- BaseFormat
It is critical to understand the BaseFormat methods and design concept
to understand how each of the format class' methods work, and how they
are used in restructuring from site to array structure (and vice versa)
for each format.
- The Borealis documentation on formats is at
https://borealis.readthedocs.io/en/latest/borealis_data.html
"""
import copy
import h5py
import numpy as np
from typing import List
from collections import OrderedDict
from .base_format import BaseFormat
class BorealisRawacfv0_4(BaseFormat):
"""
Class containing Borealis Rawacf data fields and their types.
See Also
--------
BaseFormat
BorealisRawacf (most up to date format)
Notes
-----
Rawacf data has been mixed, filtered, and decimated; beamformed and
combined into antenna arrays; then autocorrelated and correlated between
antenna arrays to produce matrices of num_ranges x num_lags.
See BaseFormat for description of classmethods and some staticmethods
and how they are used to verify format files and restructure Borealis
files to array and site structure.
Static Methods
--------------
find_num_ranges(OrderedDict): int
Returns num ranges in the data for use in finding dimensions
find_num_lags(OrderedDict): int
Returns the num lags in the data for use in finding dimensions
"""
@staticmethod
def find_num_ranges(records: OrderedDict) -> int:
"""
Find the number of ranges given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_ranges
The number of ranges being calculated in the acfs.
Notes
-----
Num_ranges is unique to a slice so cannot change inside file.
"""
first_key = list(records.keys())[0]
num_ranges = records[first_key]['correlation_dimensions'][1]
return num_ranges
@staticmethod
def find_num_lags(records: OrderedDict) -> int:
"""
Find the number of lags given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_lags
The number of lags being calculated in the acfs.
Notes
-----
Num_lags is unique to a slice so cannot change inside file.
"""
first_key = list(records.keys())[0]
num_lags = records[first_key]['correlation_dimensions'][2]
return num_lags
@staticmethod
def reshape_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------
records
An OrderedDict of the site style data, with the main_acfs,
intf_acfs, and xcfs fields in all records reshaped to the correct
dimensions.
Notes
-----
BorealisRawacf has the correlation fields flattened in the
site structured files, so this field is reshaped in here.
"""
# dimensions provided in correlation_dimensions field as num_beams,
# num_ranges, num_lags for the rawacf format.
new_records = copy.deepcopy(records)
for key in list(records.keys()):
record_dimensions = new_records[key]['correlation_dimensions']
for field in ['main_acfs', 'intf_acfs', 'xcfs']:
new_records[key][field] = new_records[key][field].\
reshape(record_dimensions)
return new_records
@staticmethod
def flatten_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------
records
An OrderedDict of the site style data, with the correlation
fields in all records flattened as is the convention
in site structured files.
Notes
-----
BorealisRawacf has the main_acfs, intf_acfs, and xcfs fields flattened
in the site structured files.
"""
new_records = copy.deepcopy(records)
for key in list(records.keys()):
for field in ['main_acfs', 'intf_acfs', 'xcfs']:
new_records[key][field] = new_records[key][field].flatten()
return new_records
@classmethod
def site_get_max_dims(cls, filename: str, unshared_parameters: List[str]):
"""
See BaseFormat class for description and use of this method.
Parameters
----------
filename: str
Name of the site file being checked
unshared_parameters: List[str]
List of parameter names that are not shared between all the records
in the site restructured file, i.e. may have different dimensions
between records.
Returns
-------
fields_max_dims: dict
dictionary containing field names (str) as keys with maximum
dimensions required to restructure to array file as values (tuples)
Raises
------
"""
fields_max_dims, max_num_sequences, max_num_beams = super(BorealisRawacfv0_4,
cls).site_get_max_dims(filename,
unshared_parameters)
# Now change the main_acfs, int_acfs and xcfs dicts to maximum required dims
# Get the num_ranges and num_lags fields directly from one record of the file
with h5py.File(filename, 'r') as site_file:
# hacky way to get first key with KeyView object from .keys()
record_name = [k for i, k in enumerate(site_file.keys()) if i == 0][0]
_, num_ranges, num_lags = site_file[record_name]['correlation_dimensions']
# Change the data dimensions to the multidimensional size instead of flattened size
reshaped_correlation_dims = (max_num_beams, num_ranges, num_lags)
fields_max_dims['main_acfs'] = reshaped_correlation_dims
fields_max_dims['intf_acfs'] = reshaped_correlation_dims
fields_max_dims['xcfs'] = reshaped_correlation_dims
return fields_max_dims, max_num_sequences, max_num_beams
@classmethod
def is_restructureable(cls) -> bool:
"""
See BaseFormat class for description and use of this method.
"""
return True
@classmethod
def single_element_types(cls):
"""
See BaseFormat class for description and use of this method.
Returns
-------
single_element_types
All the single-element fields in records of the
format, as a dictionary fieldname : type.
"""
return {
# Identifies the version of Borealis that made this data. Necessary
# for all versions.
"borealis_git_hash": np.unicode_,
# Number used to identify experiment.
"experiment_id": np.int64,
# Name of the experiment file.
"experiment_name": np.unicode_,
# Comment about the whole experiment
"experiment_comment": np.unicode_,
# Additional text comment that describes the slice.
"slice_comment": np.unicode_,
# Number of slices in the experiment at this integration time.
"num_slices": np.int64,
# Three letter radar identifier.
"station": np.unicode_,
# Number of sampling periods in the integration time.
"num_sequences": np.int64,
# range gate separation (equivalent distance between samples), km.
"range_sep": np.float32,
# Round trip time of flight to first range in microseconds.
"first_range_rtt": np.float32,
# Distance to first range in km.
"first_range": np.float32,
# Sampling rate of the samples being written to file in Hz.
"rx_sample_rate": np.float64,
# Designates if the record is the first in a scan.
"scan_start_marker": np.bool_,
# Integration time in seconds.
"int_time": np.float32,
# Length of the pulse in microseconds.
"tx_pulse_len": np.uint32,
# The minimum spacing between pulses, spacing between pulses is
# always a multiple of this in microseconds.
"tau_spacing": np.uint32,
# Number of main array antennas.
"main_antenna_count": np.uint32,
# Number of interferometer array antennas.
"intf_antenna_count": np.uint32,
# The frequency used for this experiment slice in kHz.
"freq": np.uint32,
# str denoting C data type of the samples included in the data
# array, such as 'complex float'.
"samples_data_type": np.unicode_,
# data normalization factor determined by the filter scaling in the
# decimation scheme.
"data_normalization_factor": np.float64,
# number of beams calculated for the integration time.
"num_beams": np.uint32
}
@classmethod
def array_dtypes(cls):
"""
See BaseFormat class for description and use of this method.
Returns
-------
array_dtypes
All the array fields in records of the
format, as a dictionary fieldname : array dtype.
"""
return {
# The pulse sequence in multiples of the tau_spacing.
"pulses": np.uint32,
# The lags created from combined pulses.
"lags": np.uint32,
# Samples that have been blanked during TR switching.
"blanked_samples": np.uint32,
# A list of GPS timestamps of the beginning of transmission for
# each sampling period in the integration time. Seconds since
# epoch.
"sqn_timestamps": np.float64,
# A list of beam numbers used in this slice.
"beam_nums": np.uint32,
# A list of the beams azimuths for each beam in degrees.
"beam_azms": np.float64,
# Noise at the receive frequency, should be an array
# (one value per sequence) (TODO units??) (TODO document
# FFT resolution bandwidth for this value, should be =
# output_sample rate?)
"noise_at_freq": np.float64,
# Denotes what each acf/xcf dimension represents. = "num_beams",
# "num_ranges", "num_lags" in site rawacf files.
"correlation_descriptors": np.unicode_,
# The dimensions in which to reshape the acf/xcf data.
"correlation_dimensions": np.uint32,
# Main array autocorrelations
"main_acfs": np.complex64,
# Interferometer array autocorrelations
"intf_acfs": np.complex64,
# Crosscorrelations between main and interferometer arrays
"xcfs": np.complex64
}
@classmethod
def shared_fields(cls):
"""
See BaseFormat class for description and use of this method.
Notes
-----
The dimension info for shared_fields is not necessary because the
dimensions will be the same for site and restructured files.
"""
return ['blanked_samples', 'borealis_git_hash',
'data_normalization_factor', 'experiment_comment',
'experiment_id', 'experiment_name', 'first_range',
'first_range_rtt', 'freq', 'intf_antenna_count', 'lags',
'main_antenna_count', 'pulses', 'range_sep',
'rx_sample_rate', 'samples_data_type',
'slice_comment', 'station', 'tau_spacing',
'tx_pulse_len']
@classmethod
def unshared_fields_dims_array(cls):
"""
See BaseFormat class for description and use of this method.
"""
return { # functions take records dictionary
'num_sequences': [],
'int_time': [],
'sqn_timestamps': [cls.find_max_sequences],
'noise_at_freq': [cls.find_max_sequences],
'main_acfs': [cls.find_max_beams, cls.find_num_ranges,
cls.find_num_lags],
'intf_acfs': [cls.find_max_beams, cls.find_num_ranges,
cls.find_num_lags],
'xcfs': [cls.find_max_beams, cls.find_num_ranges,
cls.find_num_lags],
'scan_start_marker': [],
'beam_nums': [cls.find_max_beams],
'beam_azms': [cls.find_max_beams],
'num_slices': []
}
@classmethod
def unshared_fields_dims_site(cls):
"""
See BaseFormat class for description and use of this method.
"""
return { # functions take arrays dictionary and record_num
'num_sequences': [],
'int_time': [],
'sqn_timestamps': [lambda arrays, record_num:
arrays['num_sequences'][record_num]],
'noise_at_freq': [lambda arrays, record_num:
arrays['num_sequences'][record_num]],
'main_acfs': [lambda arrays, record_num:
arrays['num_beams'][record_num],
lambda arrays, record_num:
arrays['main_acfs'].shape[2],
lambda arrays, record_num:
arrays['main_acfs'].shape[3]],
'intf_acfs': [lambda arrays, record_num:
arrays['num_beams'][record_num],
lambda arrays, record_num:
arrays['main_acfs'].shape[2],
lambda arrays, record_num:
arrays['main_acfs'].shape[3]],
'xcfs': [lambda arrays, record_num:
arrays['num_beams'][record_num],
lambda arrays, record_num:
arrays['main_acfs'].shape[2],
lambda arrays, record_num:
arrays['main_acfs'].shape[3]],
'scan_start_marker': [],
'beam_nums': [lambda arrays, record_num:
arrays['num_beams'][record_num]],
'beam_azms': [lambda arrays, record_num:
arrays['num_beams'][record_num]],
'num_slices': []
}
@classmethod
def array_specific_fields_generate(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_beams': lambda records: np.array(
[len(record['beam_nums']) for key, record in records.items()],
dtype=np.uint32),
'correlation_descriptors': lambda records: np.array(
['num_records', 'max_num_beams', 'num_ranges', 'num_lags'])
}
@classmethod
def array_specific_fields_iterative_generator(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_beams': lambda record: len(record['beam_nums'])
}
@classmethod
def site_specific_fields_generate(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'correlation_descriptors': lambda arrays, record_num: np.array(
['num_beams', 'num_ranges', 'num_lags']),
'correlation_dimensions': lambda arrays, record_num: np.array(
[arrays['num_beams'][record_num], arrays['main_acfs'].shape[2],
arrays['main_acfs'].shape[3]], dtype=np.uint32)
}
class BorealisBfiqv0_4(BaseFormat):
"""
Class containing Borealis Bfiq data fields and their types.
See Also
--------
BaseFormat
BorealisBfiq (most up to date format)
Notes
-----
Bfiq data is beamformed i and q data. It has been mixed, filtered,
decimated to the final output receive rate, and it has been beamformed
and all channels have been combined into their arrays. No correlation
or averaging has occurred.
See BaseFormat for description of classmethods and some staticmethods and
how they are used to verify format files and restructure Borealis files to
array and site structure.
Static Methods
--------------
find_num_antenna_arrays(OrderedDict): int
Returns number of arrays in the data for use in finding dimensions
find_num_samps(OrderedDict): int
Returns the number of samples in the data for use in finding dimensions
"""
@staticmethod
def find_num_antenna_arrays(records: OrderedDict) -> int:
"""
Find the number of antenna arrays given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_arrays
The number of arrays that have been beamformed and combined in
the file. Typically 2; main and one interferometer.
Notes
-----
Num_arrays is unique to a slice so cannot change inside file.
"""
first_key = list(records.keys())[0]
num_arrays = records[first_key]['data_dimensions'][0]
return num_arrays
@staticmethod
def find_num_samps(records: OrderedDict) -> int:
"""
Find the number of samples given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_samps
The number of samples that have been recorded in a sequence.
Notes
-----
The num_ranges/first_range and sampling rates that determine this
value cannot change within a slice, therefore it is one value per file.
"""
first_key = list(records.keys())[0]
num_samps = records[first_key]['data_dimensions'][3]
return num_samps
@staticmethod
def reshape_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------
records
An OrderedDict of the site style data, with the data
field in all records reshaped to the correct dimensions.
Notes
-----
BorealisBfiq has the data field flattened in the
site structured files, so this field is reshaped here to the
correct dimensions given in data_dimensions.
"""
new_records = copy.deepcopy(records)
for key in list(records.keys()):
record_dimensions = records[key]['data_dimensions']
for field in ['data']:
new_records[key][field] = new_records[key][field].\
reshape(record_dimensions)
return new_records
@staticmethod
def flatten_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------
records
An OrderedDict of the site style data, with the data
field in all records flattened as is the convention
in site structured files.
Notes
-----
BorealisBfiq has the data field flattened in the
site structured files.
"""
new_records = copy.deepcopy(records)
for key in list(records.keys()):
for field in ['data']:
new_records[key][field] = new_records[key][field].flatten()
return new_records
@classmethod
def site_get_max_dims(cls, filename: str, unshared_parameters: List[str]):
"""
See BaseFormat class for description and use of this method.
Parameters
----------
filename: str
Name of the site file being checked
unshared_parameters: List[str]
List of parameter names that are not shared between all the records
in the site restructured file, i.e. may have different dimensions
between records.
Returns
-------
fields_max_dims: dict
dictionary containing field names (str) as keys with maximum
dimensions required to restructure to array file as values (tuples)
Raises
------
"""
fields_max_dims, max_num_sequences, max_num_beams = super(BorealisBfiqv0_4,
cls).site_get_max_dims(filename,
unshared_parameters)
# Get the num_ant_arrays and num_samps fields directly from one record of the file
with h5py.File(filename, 'r') as site_file:
# hacky way to get first key with KeyView object from .keys()
record_name = [k for i, k in enumerate(site_file.keys()) if i == 0][0]
num_ant_arrays, _, _, num_samps = site_file[record_name]['data_dimensions']
# Change the data dimensions to the multidimensional size instead of flattened size
reshaped_data_dims = (num_ant_arrays, max_num_sequences, max_num_beams, num_samps)
fields_max_dims['data'] = reshaped_data_dims
return fields_max_dims, max_num_sequences, max_num_beams
@classmethod
def is_restructureable(cls) -> bool:
"""
See BaseFormat class for description and use of this method.
"""
return True
@classmethod
def single_element_types(cls):
"""
See BaseFormat class for description and use of this method.
Returns
-------
single_element_types
All the single-element fields in records of the
format, as a dictionary fieldname : type.
"""
return {
# Identifies the version of Borealis that made this data. Necessary
# for all versions.
"borealis_git_hash": np.unicode_,
# Number used to identify experiment.
"experiment_id": np.int64,
# Name of the experiment file.
"experiment_name": np.unicode_,
# Comment about the whole experiment
"experiment_comment": np.unicode_,
# Additional text comment that describes the slice.
"slice_comment": np.unicode_,
# Number of slices in the experiment at this integration time.
"num_slices": np.int64,
# Three letter radar identifier.
"station": np.unicode_,
# Number of sampling periods in the integration time.
"num_sequences": np.int64,
# Sampling rate of the samples being written to file in Hz.
"rx_sample_rate": np.float64,
# Designates if the record is the first in a scan.
"scan_start_marker": np.bool_,
# Integration time in seconds.
"int_time": np.float32,
# Length of the pulse in microseconds.
"tx_pulse_len": np.uint32,
# The minimum spacing between pulses, spacing between pulses is
# always a multiple of this. In microseconds.
"tau_spacing": np.uint32,
# Number of main array antennas.
"main_antenna_count": np.uint32,
# Number of interferometer array antennas.
"intf_antenna_count": np.uint32,
# The frequency used for this experiment slice in kHz.
"freq": np.uint32,
# str denoting C data type of the samples included in the data
# array, such as 'complex float'.
"samples_data_type": np.unicode_,
# Number of samples in the sampling period.
"num_samps": np.uint32,
# range gate separation (equivalent distance between samples), km
"range_sep": np.float32,
# Round trip time of flight to first range in microseconds.
"first_range_rtt": np.float32,
# Distance to first range in km.
"first_range": np.float32,
# Number of ranges to calculate correlations for.
"num_ranges": np.uint32,
# data normalization factor determined by the filter scaling in the
# decimation scheme.
"data_normalization_factor": np.float64,
# number of beams calculated for the integration time.
"num_beams": np.uint32
}
@classmethod
def array_dtypes(cls):
"""
See BaseFormat class for description and use of this method.
Returns
-------
array_dtypes
All the array fields in records of the
format, as a dictionary fieldname : array dtype.
"""
return {
# The pulse sequence in multiples of the tau_spacing.
"pulses": np.uint32,
# The lags created from combined pulses.
"lags": np.uint32,
# Samples that have been blanked during TR switching.
"blanked_samples": np.uint32,
# For pulse encoding phase, in degrees offset.
# Contains one phase offset per pulse in pulses.
"pulse_phase_offset": np.float32,
# A list of GPS timestamps of the beginning of transmission for
# each sampling period in the integration time. Seconds since
# epoch.
"sqn_timestamps": np.float64,
# A list of beam numbers used in this slice.
"beam_nums": np.uint32,
# A list of the beams azimuths for each beam in degrees.
"beam_azms": np.float64,
# Noise at the receive frequency, should be an array (one value per
# sequence) (TODO units??) (TODO document FFT resolution
# bandwidth for this value, should be = output_sample rate?)
"noise_at_freq": np.float64,
# States what order the data is in. Describes the data layout.
"antenna_arrays_order": np.unicode_,
# Denotes what each data dimension represents. =
# "num_antenna_arrays", "num_sequences", "num_beams", "num_samps"
# for site bfiq.
"data_descriptors": np.unicode_,
# The dimensions in which to reshape the data.
"data_dimensions": np.uint32,
# A contiguous set of samples (complex float) at given sample rate
"data": np.complex64
}
@classmethod
def shared_fields(cls):
"""
See BaseFormat class for description and use of this method.
"""
return ['antenna_arrays_order', 'blanked_samples',
'borealis_git_hash',
'data_normalization_factor',
'experiment_comment', 'experiment_id', 'experiment_name',
'first_range', 'first_range_rtt', 'freq',
'intf_antenna_count', 'lags', 'main_antenna_count',
'num_ranges', 'num_samps',
'pulse_phase_offset', 'pulses', 'range_sep',
'rx_sample_rate', 'samples_data_type',
'slice_comment', 'station', 'tau_spacing',
'tx_pulse_len']
@classmethod
def unshared_fields_dims_array(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_sequences': [],
'int_time': [],
'sqn_timestamps': [cls.find_max_sequences],
'noise_at_freq': [cls.find_max_sequences],
'data': [cls.find_num_antenna_arrays,
cls.find_max_sequences, cls.find_max_beams,
cls.find_num_samps],
'scan_start_marker': [],
'beam_nums': [cls.find_max_beams],
'beam_azms': [cls.find_max_beams],
'num_slices': []
}
@classmethod
def unshared_fields_dims_site(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_sequences': [],
'int_time': [],
'sqn_timestamps': [lambda arrays, record_num:
arrays['num_sequences'][record_num]],
'noise_at_freq': [lambda arrays, record_num:
arrays['num_sequences'][record_num]],
'data': [lambda arrays, record_num: arrays['data'].shape[1],
lambda arrays, record_num:
arrays['num_sequences'][record_num],
lambda arrays, record_num:
arrays['num_beams'][record_num],
lambda arrays, record_num: arrays['data'].shape[4]],
'scan_start_marker': [],
'beam_nums': [lambda arrays, record_num:
arrays['num_beams'][record_num]],
'beam_azms': [lambda arrays, record_num:
arrays['num_beams'][record_num]],
'num_slices': []
}
@classmethod
def array_specific_fields_generate(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_beams': lambda records: np.array(
[len(record['beam_nums']) for key, record in records.items()],
dtype=np.uint32),
'data_descriptors': lambda records: np.array(
['num_records', 'num_antenna_arrays', 'max_num_sequences',
'max_num_beams', 'num_samps'])
}
@classmethod
def array_specific_fields_iterative_generator(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'num_beams': lambda record: len(record['beam_nums'])
}
@classmethod
def site_specific_fields_generate(cls):
"""
See BaseFormat class for description and use of this method.
"""
return {
'data_descriptors': lambda arrays, record_num: np.array(
['num_antenna_arrays', 'num_sequences', 'num_beams',
'num_samps']),
'data_dimensions': lambda arrays, record_num: np.array(
[arrays['data'].shape[1], arrays['num_sequences'][record_num],
arrays['num_beams'][record_num], arrays['data'].shape[4]],
dtype=np.uint32)
}
class BorealisAntennasIqv0_4(BaseFormat):
"""
Class containing Borealis Antennas iq data fields and their types.
See Also
--------
BaseFormat
BorealisAntennasIq (most up to date format)
Notes
-----
Antennas iq data is data with all channels separated. It has been mixed
and filtered, but it has not been beamformed or combined into the
entire antenna array data product.
See BaseFormat for description of classmethods and some staticmethods and
how they are used to verify format files and restructure Borealis files to
array and site structure.
Static Methods
--------------
find_num_antennas(OrderedDict): int
Returns number of antennas in the data for use in finding dimensions
find_num_samps(OrderedDict): int
Returns the number of samples in the data for use in finding dimensions
"""
@staticmethod
def find_num_antennas(records: OrderedDict) -> int:
"""
Find the number of antennas given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_antennas
The number of antennas that have been recorded and stored in the
file.
Notes
-----
Num_antennas is unique to a slice so cannot change inside file.
"""
first_key = list(records.keys())[0]
num_antennas = records[first_key]['data_dimensions'][0]
return num_antennas
@staticmethod
def find_num_samps(records: OrderedDict) -> int:
"""
Find the number of samples given the records dictionary, for
restructuring to arrays.
Parameters
----------
records
The records dictionary from a site-style file.
Returns
-------
num_samps
The number of samples that have been recorded in a sequence.
Notes
-----
The num_ranges/first_range and sampling rates that determine this
value cannot change within a slice, therefore it is one value per file.
"""
first_key = list(records.keys())[0]
num_samps = records[first_key]['data_dimensions'][2]
return num_samps
@staticmethod
def reshape_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------
records
An OrderedDict of the site style data, with the data
field in all records reshaped to the correct dimensions.
Notes
-----
BorealisAntennasIq has the data field flattened in the
site structured files, so this field is reshaped here to the correct
data_dimensions given in the file.
"""
new_records = copy.deepcopy(records)
for key in list(records.keys()):
record_dimensions = records[key]['data_dimensions']
for field in ['data']:
new_records[key][field] = new_records[key][field].\
reshape(record_dimensions)
return new_records
@staticmethod
def flatten_site_arrays(records: OrderedDict) -> OrderedDict:
"""
See BaseFormat class for description and use of this method.
Parameters
----------
records
An OrderedDict of the site style data, organized
by record. Records are stored with timestamps
as the keys and the data for that timestamp
stored as a dictionary.
Returns
-------