-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgotdr.go
1177 lines (976 loc) · 28.6 KB
/
gotdr.go
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
/*
Author: Naseredin Aramnejad naseredin.aramnejad@gmail.com
This script is designed to extract all the possible information from the
given sor file.
each sor file (Provided by OTDR Equipment) contains multiple data blocks.
Formulas and blueprint of this script are inspired by the information provided by:
Sidney Li
http://morethanfootnotes.blogspot.com/2015/07/
*/
package main
import (
"bytes"
"encoding/csv"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
)
const lightSpeed = 299.79181901 // m/µsec
func nukeIfErr(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
fmt.Printf("Failed to open browser: %v\n", err)
}
}
func mod(a, b float64) float64 {
return a - b*math.Floor(a/b)
}
func removePaths(stack []byte) []byte {
lines := bytes.Split(stack, []byte("\n"))
for i, line := range lines {
if idx := bytes.LastIndex(line, []byte("/go/")); idx != -1 {
lines[i] = line[idx+4:]
} else if idx := bytes.Index(line, []byte(":")); idx != -1 {
lines[i] = line[idx:]
}
}
return bytes.Join(lines, []byte("\n"))
}
func customPanicHandler() {
if r := recover(); r != nil {
// Get the stack trace
stack := debug.Stack()
// Remove file paths from the stack trace
sanitizedStack := removePaths(stack)
// Log or print the sanitized stack trace
fmt.Printf("Panic: %v\n%s", r, sanitizedStack)
// Optionally, exit the program
os.Exit(1)
}
}
// Reverse will reverse the hex string in every 2 bytes. Example: 0ABCD123 => 23D1BC0A.
func Reverse(s string) string {
str := ""
for ind := 0; ind < len(s); ind += 2 {
str = s[ind:ind+2] + str
}
return str
}
func dB(point int64) float64 {
return float64(point*-1000) * math.Pow(10, -6)
}
func ReadSorFile(filename string) otdrRawData {
r := otdrRawData{
Filename: filename,
}
f, err := os.Open(filename)
if err != nil {
nukeIfErr(err)
}
defer f.Close()
// Get the file size
stat, err := f.Stat()
if err != nil {
nukeIfErr(err)
}
// Read the entire file at once
buffer := make([]byte, stat.Size())
_, err = io.ReadFull(f, buffer)
if err != nil {
nukeIfErr(err)
}
//Converting the byte array into a hex String
r.HexData = hex.EncodeToString(buffer)
//Converting the HexData to a text string
chars, err := hex.DecodeString(r.HexData)
nukeIfErr(err)
r.Decodedfile = string(chars)
return r
}
// parsHexValue calls the Reverse() funcition to reverse the order of the provided HexString and then converts it's value to int64.
func parsHexValue(hexData string) int64 {
output, err := strconv.ParseInt(Reverse(hexData), 16, 64)
if err != nil {
fmt.Println(err)
return 0
}
return output
}
func (d *otdrRawData) draw() {
// Create a new line chart instance
xValues := make([]opts.LineData, len(d.DataPoints))
yValues := make([]opts.LineData, len(d.DataPoints))
// yValues := make([]opts.ScatterData, len(d.DataPoints))
for i, point := range d.DataPoints {
xValues[i] = opts.LineData{Value: point[0]}
yValues[i] = opts.LineData{Value: point[1]}
// yValues[i] = opts.ScatterData{Value: point[1]}
}
// Create a new line chart instance
line := charts.NewLine()
// line := charts.NewScatter()
// Set global options like title and legend
line.SetGlobalOptions(
charts.WithColorsOpts(opts.Colors{
"green",
}),
charts.WithToolboxOpts(opts.Toolbox{
Show: opts.Bool(true),
Feature: &opts.ToolBoxFeature{
DataZoom: &opts.ToolBoxFeatureDataZoom{
Show: opts.Bool(true),
YAxisIndex: false,
},
Restore: &opts.ToolBoxFeatureRestore{
Show: opts.Bool(true),
},
SaveAsImage: &opts.ToolBoxFeatureSaveAsImage{
Show: opts.Bool(true),
},
},
}),
charts.WithInitializationOpts(opts.Initialization{
Width: "1300px",
Height: "500px",
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "inside",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
Start: 0,
End: 100,
XAxisIndex: []int{0},
}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(true)}),
charts.WithAnimation(true),
charts.WithTitleOpts(opts.Title{
Title: "GOTDR Viewer",
}),
)
markPoints := make([]opts.MarkPointNameCoordItem, 0, len(d.DataPoints))
for _, ev := range d.Events {
loc := d.return_index(ev.EventLocM)
if loc[0] == 0 {
continue
}
c := "blue"
if strings.Contains(ev.EventType, "EXXX") || strings.Contains(ev.EventType, "E999") {
c = "red"
}
mkpoint := opts.MarkPointNameCoordItem{
Name: ev.EventType,
Value: strconv.Itoa(ev.EventNumber),
Coordinate: []interface{}{loc[0], loc[1]},
Symbol: "pin",
ItemStyle: &opts.ItemStyle{
Color: c,
Opacity: 0.5,
},
SymbolSize: 35,
}
markPoints = append(markPoints, mkpoint)
}
// Add data to the line chart
line.SetXAxis(xValues).AddSeries("Reflection", yValues, charts.WithMarkPointNameCoordItemOpts(markPoints...))
line.SetSeriesOptions(
charts.WithMarkPointStyleOpts(
opts.MarkPointStyle{Label: &opts.Label{Show: opts.Bool(true)}}),
charts.WithAreaStyleOpts(opts.AreaStyle{
Color: "yellow",
Opacity: 0.1,
}),
)
f, _ := os.Create("graph.html")
// line.Render(f)
d.generateHTML(f, line)
openBrowser("graph.html")
}
func (d *otdrRawData) generateHTML(w io.Writer, line *charts.Line) {
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OTDR Report</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
background-color: #f0f0f0;
font-size: 16px;
}
.container {
display: flex;
flex-direction: column;
background-color: #ffffff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 1600px;
margin: 0 auto;
}
.summary {
width: 100%;
background-color: #f9f9f9;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
padding: 20px;
margin-top: 20px;
}
.chart {
width: 100%;
}
table {
border-collapse: collapse;
width: 100%;
font-size: 14px;
table-layout: fixed;
}
th, td {
border: 1px solid #e0e0e0;
padding: 10px;
text-align: left;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
th {
background-color: #4a90e2;
color: white;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
/* Responsive design */
@media (max-width: 768px) {
.container {
padding: 15px;
}
th, td {
padding: 8px;
font-size: 12px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="chart">
`))
line.Render(w)
tmpl := template.Must(template.New("summary").Parse(`
</div>
<div class="summary">
<table>
<thead>
<tr>
<th>Fixed Parameters</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Date & Time</td>
<td>{{.DT}}</td>
</tr>
<tr>
<td>Scan Range</td>
<td>{{.SR}} m</td>
</tr>
<tr>
<td>Unit</td>
<td>{{.UNIT}}</td>
</tr>
<tr>
<td>Actual Wavelength</td>
<td>{{.WL}} nm</td>
</tr>
<tr>
<td>Pulse Width QTY</td>
<td>{{.PWQ}}</td>
</tr>
<tr>
<td>Pulse Width(ns)</td>
<td>{{.PW}}</td>
</tr>
<tr>
<td>Sample Quantity</td>
<td>{{.SQ}}</td>
</tr>
<tr>
<td>Fiber Light Speed</td>
<td>{{.FLS}} km/s</td>
</tr>
<tr>
<td>Fiber Length (EOF)</td>
<td>{{.FLEN}} m</td>
</tr>
<tr>
<td>Bellcore Version</td>
<td>{{.BLV}}</td>
</tr>
</tbody>
</table>
</div>
<div class="summary">
<table>
<thead>
<tr>
<th>Key events</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Key events qty</td>
<td>{{.KE}}</td>
</tr>
</tbody>
</table>
</div>
<div class="summary">
<table>
<thead>
<tr>
<th>Supplier Parameters</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>OTDR Supplier</td>
<td>{{.OS}}</td>
</tr>
<tr>
<td>OTDR Module Name</td>
<td>{{.OMN}}</td>
</tr>
<tr>
<td>OTDR Name</td>
<td>{{.ON}}</td>
</tr>
<tr>
<td>OTDR Software Version</td>
<td>{{.OSV}}</td>
</tr>
<tr>
<td>OTDR Module Serial Number Version</td>
<td>{{.OMS}}</td>
</tr>
<tr>
<td>OTDR other info</td>
<td>{{.OOI}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
`))
data := struct {
DT time.Time
UNIT string
WL float64
PWQ int64
FLS float64
PW []int64
SQ []int64
FLEN float64
BLV float64
OS string
ON string
OSV string
OMS string
OOI string
OMN string
SR []float64
KE int
}{
DT: d.FixedParams.DateTime,
UNIT: d.FixedParams.Unit,
WL: d.FixedParams.ActualWL,
PWQ: d.FixedParams.PulseWidthNo,
FLS: d.FixedParams.FiberSpeed * 1000,
PW: d.FixedParams.PulseWidth,
SQ: d.FixedParams.SampleQTY,
FLEN: d.TotalLength,
SR: d.FixedParams.Range,
BLV: d.BellCoreVersion,
ON: d.Supplier.OTDRName,
OMN: d.Supplier.OTDRModuleName,
OS: d.Supplier.OTDRSupplier,
OSV: d.Supplier.OTDRswVersion,
OMS: d.Supplier.OTDRModuleSN,
OOI: d.Supplier.OTDROtherInfo,
KE: len(d.Events),
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
log.Println("failed to create the html file")
}
htmlContent := buf.String()
w.Write([]byte(htmlContent))
}
func (d *otdrRawData) return_index(loc float64) []float64 {
closest := []float64{math.Inf(0), 0}
for ind, i := range d.DataPoints {
if i[0] == loc {
return []float64{float64(ind), i[1]}
}
if math.Abs(loc-i[0]) < math.Abs(loc-closest[0]) && i[0] < loc {
closest = []float64{float64(ind), i[1]}
} else if i[0] > loc {
break
}
}
return []float64{closest[0], closest[1]}
}
func (d *otdrRawData) mapKeyEvents(events string) map[int][2]int {
m := make(map[int][2]int)
start := 4
evnumbers := int(parsHexValue(events[:start]))
for i := 1; i <= evnumbers; i++ {
if i == evnumbers {
m[i] = [2]int{start, len(events) - 46}
} else {
end := strings.Index(events[start+84:], fmt.Sprintf("%02x00", i+1))
if end == -1 {
fmt.Println("pattern not found:", events)
return nil
}
end += start + 84
m[i] = [2]int{start, end}
start = end
}
}
return m
}
func (d *otdrRawData) GetNext(key string) string {
if _, exists := d.SecLocs[key]; !exists {
log.Println(key, "not found")
return ""
}
index := d.SecLocs[key][1]
var nextKey string
nextIndex := math.Inf(1)
for k, v := range d.SecLocs {
if k == key || len(v) < 2 {
continue
}
if float64(index) < float64(v[1]) && float64(v[1]) < nextIndex {
nextIndex = float64(v[1])
nextKey = k
}
}
return nextKey
}
func (d *otdrRawData) GetOrder() {
sections := []string{
"SupParams",
"ExfoNewProprietaryBlock",
"Map",
"FxdParams",
"YokogawaSpecial",
"SetupParams",
"DataPts",
"NokiaParams",
"KeyEvents",
"GenParams",
"WaveMTSParams",
"WavetekTwoMTS",
"WavetekThreeMTS",
"BlocOtdrPrivate",
"ActernaConfig",
"ActernaMiniCurve",
"AcqParam",
"ViewParams",
"SystemParams",
"AnalysisParams",
"MiscParams",
"JDSUEvenementsMTS",
"Cksum",
}
sectionLocations := make(map[string][]int)
for _, word := range sections {
re := regexp.MustCompile(regexp.QuoteMeta(word))
matches := re.FindAllStringIndex(d.Decodedfile, -1)
locations := make([]int, len(matches))
for i, match := range matches {
locations[i] = match[0]
}
sectionLocations[word] = locations
}
if len(sectionLocations["Cksum"]) < 2 {
fmt.Printf("%s file has no checksum\n", d.Filename)
os.Exit(1)
}
d.SecLocs = sectionLocations
}
// Under construction
func (d *otdrRawData) getSetupParams() {
// s := SetupParams{}
if len(d.SecLocs["SetupParams"]) == 0 {
// log.Println("SetupParams section is missing")
return
}
setupparams := d.HexData[(d.SecLocs["SetupParams"][1]+10)*2 : (d.SecLocs[d.GetNext("SetupParams")][1] * 2)]
log.Println("setupParams", setupparams)
}
// Under construction
func (d *otdrRawData) getMiscParams() {
m := MiscParams{}
if len(d.SecLocs["MiscParams"]) == 0 {
return
}
slicedMiscParams := strings.Split(d.Decodedfile[d.SecLocs["MiscParams"][1]+10:d.SecLocs[d.GetNext("MiscParams")][1]], "\x00")[1:]
m.Mode = d.extractData(slicedMiscParams, 0)
m.FiberType = d.extractData(slicedMiscParams, 1)
d.MiscParams = m
}
// Under construction
func (d *otdrRawData) getAcqParam() {
// a :=AcqParam{}
if len(d.SecLocs["AcqParam"]) == 0 {
// log.Println("AcqParam section is missing")
return
}
acqparam := d.HexData[(d.SecLocs["AcqParam"][1]+10)*2 : (d.SecLocs[d.GetNext("AcqParam")][1] * 2)]
log.Println("AcqParams", acqparam)
}
// Under construction
func (d *otdrRawData) getViewParams() {
// v :=ViewParams{}
if len(d.SecLocs["ViewParams"]) == 0 {
// log.Println("ViewParams section is missing")
return
}
viewparams := d.HexData[(d.SecLocs["ViewParams"][1]+10)*2 : (d.SecLocs[d.GetNext("ViewParams")][1] * 2)]
log.Println("viewParams", viewparams)
}
// Under construction
func (d *otdrRawData) getAnalysisParams() {
// a :=AnalysisParams{}
if len(d.SecLocs["AnalysisParams"]) == 0 {
// log.Println("AnalysisParams section is missing")
return
}
analyticsparams := d.HexData[(d.SecLocs["AnalysisParams"][1]+10)*2 : (d.SecLocs[d.GetNext("AnalysisParams")][1] * 2)]
log.Println("AnalyticsParams", analyticsparams)
}
// Under construction
func (d *otdrRawData) getSystemParams() {
// s :=SystemParams{}
if len(d.SecLocs["SystemParams"]) == 0 {
// log.Println("SystemParams section is missing")
return
}
systemparams := d.HexData[(d.SecLocs["SystemParams"][1]+10)*2 : (d.SecLocs[d.GetNext("SystemParams")][1] * 2)]
log.Println("SystemParams", systemparams)
}
// getFixedParams function extracts the Fixed Parameters from the sor file and stores it in FixInfos struct.
func (d *otdrRawData) getFixedParams() {
f := FixInfo{}
if len(d.SecLocs["FxdParams"]) == 0 {
log.Println("FxdParams section is missing")
return
}
fixInfo := d.HexData[(d.SecLocs["FxdParams"][1]+10)*2 : (d.SecLocs[d.GetNext("FxdParams")][1] * 2)]
p := 8
f.DateTime = time.Unix(parsHexValue(fixInfo[:p]), 0)
unit, err := hex.DecodeString(fixInfo[p : p+4])
nukeIfErr(err)
p += 4
f.ActualWL = float64(parsHexValue(fixInfo[p:p+4])) / 10.0
p += 4
f.AO = float64(parsHexValue(fixInfo[p : p+8]))
p += 8
f.AOD = float64(parsHexValue(fixInfo[p : p+8]))
p += 8
f.PulseWidthNo = parsHexValue(fixInfo[p : p+4])
p += 4
for i := 0; i < int(f.PulseWidthNo); i++ {
f.PulseWidth = append(f.PulseWidth, parsHexValue(fixInfo[p:p+4]))
p += 4
}
resolution_m_p1 := []float64{}
for i := 0; i < int(f.PulseWidthNo); i++ {
resolution_m_p1 = append(resolution_m_p1, float64(parsHexValue(fixInfo[p:p+8]))*math.Pow(10, -8))
p += 8
}
for i := 0; i < int(f.PulseWidthNo); i++ {
f.SampleQTY = append(f.SampleQTY, parsHexValue(fixInfo[p:p+8]))
p += 8
}
f.IOR = float64(parsHexValue(fixInfo[p : p+8]))
p += 8
f.RefIndex = f.IOR * math.Pow(10, -5)
f.FiberSpeed = lightSpeed / f.RefIndex
for i := 0; i < int(f.PulseWidthNo); i++ {
f.Resolution = append(f.Resolution, resolution_m_p1[i]*f.FiberSpeed)
}
f.Backscattering = float64(parsHexValue(fixInfo[p:p+4])) * -0.1
p += 4
f.Averaging = parsHexValue(fixInfo[p : p+8])
p += 8
f.AveragingTime = float64(parsHexValue(fixInfo[p:p+4])) / 600
p += 4
for i := 0; i < int(f.PulseWidthNo); i++ {
f.Range = append(f.Range, float64(f.SampleQTY[i])*f.Resolution[i])
}
f.Unit = string(unit)
d.FixedParams = f
}
func (d *otdrRawData) getDataPoints() {
if len(d.SecLocs["DataPts"]) == 0 {
log.Println("DataPts section is missing")
return
}
dtpoints := d.HexData[d.SecLocs["DataPts"][1]*2 : d.SecLocs[d.GetNext("DataPts")][1]*2][40:]
var start int64 = 0
var cumulative_length float64 = 0
for i := range d.FixedParams.SampleQTY {
qty := int64(d.FixedParams.SampleQTY[i])
resolution := d.FixedParams.Resolution[i]
var j int64
for j = 0; j < qty; j++ {
index := start + j
if index < int64(len(dtpoints)) {
hex_value := dtpoints[index*4 : index*4+4]
db_value := math.Round(dB(parsHexValue(hex_value))*1000) / 1000
passedlen := math.Round(float64(cumulative_length*1000)) / 1000
dataPoint := []float64{passedlen, db_value}
d.DataPoints = append(d.DataPoints, dataPoint)
cumulative_length += resolution
}
}
start += qty
}
}
// SupParams function extracts the Supplier Parameters from the sor file and stores it in SupParam struct.
func (d *otdrRawData) getSupParams() {
if len(d.SecLocs["SupParams"]) == 0 {
log.Println("SupParams section is missing")
return
}
supString := strings.Split(d.Decodedfile[d.SecLocs["SupParams"][1]+10:d.SecLocs[d.GetNext("SupParams")][1]], "\x00")
slicedParams := supString[:len(supString)-1]
supInfo := SupParam{
OTDRSupplier: strings.TrimSpace(slicedParams[0]),
OTDRName: strings.TrimSpace(slicedParams[1]),
OTDRsn: strings.TrimSpace(slicedParams[2]),
OTDRModuleName: strings.TrimSpace(slicedParams[3]),
OTDRModuleSN: strings.TrimSpace(slicedParams[4]),
OTDRswVersion: strings.TrimSpace(slicedParams[5]),
OTDROtherInfo: strings.TrimSpace(slicedParams[6]),
}
d.Supplier = supInfo
}
func (d *otdrRawData) extractData(data []string, item int) string {
if len(data)-1 < item {
return ""
} else {
return strings.TrimSpace(data[item])
}
}
// GenParams function extracts the General Parameters from the sor file and stores it in GenParam struct.
func (d *otdrRawData) getGenParams() {
if len(d.SecLocs["GenParams"]) == 0 {
log.Println("GenParams section is missing")
return
}
genStringBeforeSplit := strings.Split(d.Decodedfile[d.SecLocs["GenParams"][1]+10:d.SecLocs[d.GetNext("GenParams")][1]], "\x00")
genString := genStringBeforeSplit[:len(genStringBeforeSplit)-1]
genInfo := GenParam{
CableID: d.extractData(genString, 0)[2:],
Lang: d.extractData(genString, 0)[:2],
FiberID: d.extractData(genString, 1),
LocationA: d.extractData(genString, 2)[4:],
LocationB: d.extractData(genString, 3),
CableCode: d.extractData(genString, 4),
BuildCondition: d.extractData(genString, 5),
Operator: d.extractData(genString, 13),
Comment: d.extractData(genString, 14),
FiberType: "G." + strconv.FormatInt(parsHexValue(hex.EncodeToString([]byte(d.extractData(genString, 2)[:2]))), 10),
OTDRWavelength: strconv.FormatInt(parsHexValue(hex.EncodeToString([]byte(d.extractData(genString, 2)[2:4]))), 10) + " nm",
}
d.GenParams = genInfo
}
// getFiberLength calculates the fiber length and returns it.
func (d *otdrRawData) getFiberLength() {
for _, v := range d.Events {
if strings.Contains(v.EventType, "EXX") || strings.Contains(v.EventType, "E99") {
d.TotalLength = float64(v.EventLocM)
}
}
}
// getBellCoreVersion reads the bellcore version from the sor file and returns it.
func (d *otdrRawData) getBellCoreVersion() {
if len(d.SecLocs["Map"]) == 0 {
log.Println("Map section is missing")
return
}
d.BellCoreVersion = float64(parsHexValue(d.HexData[(d.SecLocs["Map"][0]+4)*2:(d.SecLocs["Map"][0]+5)*2])) / 100.0
}
// getTotalLoss reads the total loss of the fiber from the sor file and returns it.
func (d *otdrRawData) getTotalLoss() {
if len(d.SecLocs["WaveMTSParams"]) > 0 {
totallossinfo := d.HexData[(d.SecLocs["WaveMTSParams"][1]-22)*2 : (d.SecLocs["WaveMTSParams"][1]-18)*2]
d.TotalLoss = float64(parsHexValue(totallossinfo)) * 0.001
} else {
d.TotalLoss = 0
}
}
// getKeyEvents function extracts the events information from the sor file and stores it in OTDREvent struct.
func (d *otdrRawData) getKeyEvents() {
d.Events = map[int]OTDREvent{}
if len(d.SecLocs["KeyEvents"]) == 0 {
log.Println("KeyEvents section is missing")
return
}
events := d.HexData[(d.SecLocs["KeyEvents"][1]+10)*2 : d.SecLocs[d.GetNext("KeyEvents")][1]*2]
p := d.mapKeyEvents(events)
var eventhexlist []string
for _, v := range p {
eventhexlist = append(eventhexlist, events[v[0]:v[1]])
}
for _, e := range eventhexlist {
event := OTDREvent{}
eNum := int(parsHexValue(e[:4]))
event.EventNumber = eNum
event.EventLocM = float64(parsHexValue(e[4:12])) * (math.Pow(10, -4)) * float64(d.FixedParams.FiberSpeed)
stValue := mod(event.EventLocM, d.FixedParams.Resolution[0])
if stValue >= d.FixedParams.Resolution[0]/2 {
event.EventLocM = event.EventLocM + (d.FixedParams.Resolution[0] - stValue)
} else {
event.EventLocM = event.EventLocM + -stValue
}
event.EventLocM = math.Round(event.EventLocM*1000) / 1000
event.Slope = float64(parsHexValue(e[12:16])) * 0.001
event.SpliceLoss = float64(parsHexValue(e[16:20])) * 0.001
if parsHexValue(e[20:28]) > 0 {
event.RefLoss = float64((float64(parsHexValue(e[20:28])) - math.Pow(2, 32)) * 0.001)
} else {
event.RefLoss = float64(parsHexValue(e[20:28]))
}
eventType, err := hex.DecodeString(e[28:44])
nukeIfErr(err)
event.EventType = string(eventType)
event.EndOfPreviousEvent = int(parsHexValue(e[44:52]))
event.BegOfCurrentEvent = int(parsHexValue(e[52:60]))
event.EndOfCurrentEvent = int(parsHexValue(e[60:68]))
event.BegOfNextEvent = int(parsHexValue(e[68:76]))
event.PeakCurrentEvent = int(parsHexValue(e[76:84]))
if len(e) > 88 {
if len(e) < 102 {
comment, err := hex.DecodeString(e[84:])
nukeIfErr(err)
event.Comment = string(comment)
} else {
comment, err := hex.DecodeString(e[84:102])
nukeIfErr(err)
event.Comment = string(comment)
}
}
d.Events[eNum] = event
}
}
func (d *otdrRawData) export2Json() {
var exportData = struct {
Filename string `json:"File Name"`
MiscParams MiscParams `json:"Misc Params"`
FixedParams FixInfo `json:"Fixed Parameters"`
TotalLoss float64 `json:"Total Fiber Loss(dB)"`
TotalLength float64 `json:"Fiber Length(km)"`
GenParams GenParam `json:"General Information"`
Supplier SupParam `json:"Supplier Information"`
Events map[int]OTDREvent `json:"Key Events"`
BellCoreVersion float64 `json:"Bellcore Version"`
}{
Filename: d.Filename,
MiscParams: d.MiscParams,
FixedParams: d.FixedParams,
TotalLoss: d.TotalLoss,
TotalLength: d.TotalLength,
GenParams: d.GenParams,
Supplier: d.Supplier,
Events: d.Events,
BellCoreVersion: d.BellCoreVersion,
}
b, err := json.MarshalIndent(exportData, "", " ")
nukeIfErr(err)
_ = os.WriteFile("OTDR_Output.json", b, 0644)