-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdf.rb
executable file
·606 lines (545 loc) · 9.69 KB
/
gdf.rb
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
#!/usr/bin/env ruby
#
require 'bindata'
require 'geoutm'
require_relative 'ecef'
module GDF
VALID_POSTFIX = "\x01\x03\x03"
T0 = Time.utc(2000, 01, 01, 0, 0, 0)
DELIM_DEFAULT = ','
LMT_OFFSET_DEFAULT = "-08:00"
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
# Records within each Fix: ###################
# Datetime
# DateTime stored as an integer
# of seconds since 20010101_00:00:00
class TimeRecord < BinData::Primitive
endian :big
int32 :raw_seconds
def get
s = raw_seconds & 0x3FFFFFFF # filter out most siginificant bit
return T0 + s
end
def set(time_in)
time = Time.at(time_in) - T0
self.raw_seconds = time.to_i
end
end
# Coord
# This is a subsection with xyz ecef coordinates
# It is used within the larger Fix Record
class Coord < BinData::Primitive
endian :big
int32 :ecef_x
int32 :ecef_y
int32 :ecef_z
def get
return ECEF.new(ecef_x, ecef_y, ecef_z)
end
def set(ecef_in)
self.ecef_x = ecef_in.x.to_i
self.ecef_y = ecef_in.y.to_i
self.ecef_z = ecef_in.z.to_i
end
end
class MortStatus < BinData::Primitive
bit3 :mort_idx
def get
return MORT_STATUS_VALUES[self.mort_idx]
end
def set(mort_in)
case mort_in
when Fixnum
self.mort_idx = mort_in.to_i % 0x7
when String
self.mort_idx = MORT_STATUS_VALUES.index(mort_in)
else
raise "Cannot set MortStatus with #{mort_in.class} object"
end
end
end
class FixType < BinData::Primitive
bit5 :fix_idx
def get
return FIX_TYPES[self.fix_idx]
end
def set(fix_in)
case fix_in
when Fixnum
self.fix_idx = fix_in.to_i % 256
when String
self.fix_idx = FIX_TYPES.index(fix_in)
else
raise "Cannot set FixType with #{fix_in.class} object"
end
end
end
class DOP < BinData::Primitive
endian :big
uint8 :dop_byte
def get
return dop_byte.to_f / 5.0
end
def set(dop_in)
self.dop_byte = (dop_in * 5).round % 256
end
end
class Sat < BinData::Record
bit5 :num
bit3 :snr_raw
virtual :snr, :value => lambda {
num.nonzero?? snr_raw * 3 + 29 : 0
}
end
class SatArray < BinData::Array
default_parameters :type => :sat,
:initial_length => 11
end
class Error < BinData::Primitive
endian :big
uint8 :error_raw
def get
if (0x01..0xFE) === self.error_raw
self.error_raw.to_f / 5.0
else
return "NA"
end
end
def set(err = 0.0)
self.error_raw = (err * 5.0).to_i % 256
end
end
class Voltage < BinData::Primitive
endian :big
uint8 :v_byte
def get
return v_byte.to_f / 50.0
end
def set(v_in)
self.v_byte = (v_in * 50).round % 256
end
end
class Temperature < BinData::Primitive
endian :big
uint8 :temp_byte
def get
return temp_byte.to_i - 103
end
def set(temp_in)
self.temp_byte = (temp_in + 103).to_i % 256
end
end
# Header
# This is the first portion with gdf metadata
# It occurs once per file, at the beginning
class Header < BinData::Record
endian :little
uint32 :collar_id
string :prefix, :read_length => 4
string :msg, :read_length => 16
string :postfix, :read_length => 3
virtual :valid, :assert => lambda { postfix == VALID_POSTFIX }
end
# Junk
# This follow the header, and appears to
# have no bearing on the data.
# Not sure of its purpose
class Junk < BinData::Record
string :raw, :read_length => 51
end
# Fix
# A single gps fix, with all associated data.
# After the Header and Junk, the rest of the gdf
# is just repeated fixes.
class Fix < BinData::Record
default_parameter :id => nil
default_parameter :lmt_offset => LMT_OFFSET_DEFAULT
virtual :collarID, :value => :id
virtual :lmt, :value => :lmt_offset
time_record :time
coord :position
mort_status :mortality
fix_type :fixType
dop :degreeOfPrecision
sat_array :sats
virtual :numSats, :value => lambda {
self.sats.find_all { |s| s.snr != 0}.length
}
error :error3D
voltage :mainV
voltage :beaconV
temperature :tempC
def to_row(delim = DELIM_DEFAULT)
s = collarID.to_s + delim +
time.utc.strftime(DATE_FORMAT) + delim +
time.utc.strftime(TIME_FORMAT) + delim +
time.localtime(lmt).strftime(DATE_FORMAT) + delim +
time.localtime(lmt).strftime(TIME_FORMAT) + delim +
position.x.to_i.to_s + delim +
position.y.to_i.to_s + delim +
position.z.to_i.to_s + delim +
position.lat.round(5).to_s + delim +
position.lon.round(5).to_s + delim +
position.alt.round(2).to_s + delim +
position.e.to_i.to_s + delim +
position.n.to_i.to_s + delim +
degreeOfPrecision.to_s + delim +
fixType + delim +
numSats.to_s + delim
sats.each { |sat| s += sat.num.to_s + delim + sat.snr.to_s + delim }
s += mainV.to_s + delim +
beaconV.to_s + delim +
tempC.to_s
end
def Fix.row_names(delim = DELIM_DEFAULT)
s = ""
ROW_NAMES.each do |name|
s += name + delim
end
s[-1] = "\n"
return s
end
ROW_NAMES =
["Collar ID",
"UTC Date",
"UTC Time",
"LMT Date",
"LMT Time",
"ECEF X",
"ECEF Y",
"ECEF Z",
"Latitude",
"Longitutde",
"Height",
"Easting",
"Northing",
"DoP",
"Fix Type",
"Sats Used",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Sat",
"C/N",
"Main Voltage",
"Beacon Voltage",
"Temp"]
end
# GDF
# Whole file
class GDF < BinData::Record
default_parameter :filename => nil
virtual :file, :value => :filename
header :head
virtual :collarID, :value => lambda { head.collar_id }
junk :preamble
array :fixes, :read_until => :eof do
fix :id => :collarID
end
def get_csv_header(delim = DELIM_DEFAULT)
rowhead = Fix::row_names(delim)
rowhead = "No" + delim + rowhead
return rowhead
end
def to_csv(delim = DELIM_DEFAULT )
csv = ""
self.fixes.to_a.each_index do |i|
row = i.to_s + delim
row += fixes[i].to_row(delim)
row += "\n"
csv += row
end
return csv
end
end
# def get_csv_header(delim = DELIM_DEFAULT)
# rowhead = Fix::row_names(delim)
# rowhead = "No" + delim + rowhead
# return rowhead
# end
MORT_STATUS_VALUES =
[ 'N/A',
'normal',
'Low Activity no radius',
'Low Activity within radius',
'Low Activity out of radius',
'Mortality no radius',
'Mortality within radius',
'Mortality out of radius'
]
FIX_TYPES =
['No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-3',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-3',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-3',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-3',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-2',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-2',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-2',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-2',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-1',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-1',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-1',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-1',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-0',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-0',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-0',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-0',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-A',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-A',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-A',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-A',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-B',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-B',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-B',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-B',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-Z',
'No Fix',
'No Fix',
'No Fix',
'GPS-1 Sat',
'GPS-2 Sat',
'GPS-2D',
'val. GPS-3D',
'Argos-Z',
'No Fix',
'No Fix']
end