-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnormalizer.go
More file actions
96 lines (82 loc) · 2 KB
/
Copy pathnormalizer.go
File metadata and controls
96 lines (82 loc) · 2 KB
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
package main
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
)
func normalizeQSO(qso QSO) QSO {
// Normalize power
qso.POWER = normalizePower(qso.POWER)
// Calculate band from frequency
if qso.FREQ != "" {
qso.BAND = calculateBand(qso.FREQ)
}
return qso
}
func normalizePower(powerStr string) string {
if powerStr == "" {
return powerStr
}
// Remove whitespace and convert to lowercase
powerStr = strings.TrimSpace(strings.ToLower(powerStr))
// Extract numeric value
re := regexp.MustCompile(`^(\d+(?:\.\d+)?)`)
match := re.FindStringSubmatch(powerStr)
if len(match) < 2 {
return powerStr // Return original if no valid number found
}
value, err := strconv.ParseFloat(match[1], 64)
if err != nil {
return powerStr // Return original if parsing fails
}
// Convert based on unit
if strings.Contains(powerStr, "kw") {
value *= 1000 // kW to W
} else if strings.Contains(powerStr, "mw") {
value *= 0.001 // mW to W
}
// If it's just 'w' or no unit, assume it's already in watts
// Return as string without decimal if it's a whole number
if value == math.Floor(value) {
return fmt.Sprintf("%.0f", value)
}
return fmt.Sprintf("%.3f", value)
}
func calculateBand(freqStr string) string {
freq, err := strconv.ParseFloat(freqStr, 64)
if err != nil {
return ""
}
// Band definitions (frequencies in MHz)
// These are standard amateur radio bands
bandMap := []struct {
name string
lower float64
upper float64
}{
{"160M", 1.800, 2.000},
{"80M", 3.500, 4.000},
{"60M", 5.330, 5.400},
{"40M", 7.000, 7.300},
{"30M", 10.100, 10.150},
{"20M", 14.000, 14.350},
{"17M", 18.068, 18.168},
{"15M", 21.000, 21.450},
{"12M", 24.890, 24.990},
{"10M", 28.000, 29.700},
{"6M", 50.000, 54.000},
{"2M", 144.000, 148.000},
{"1.25M", 222.000, 225.000},
{"70CM", 420.000, 450.000},
{"33CM", 902.000, 928.000},
{"23CM", 1240.000, 1300.000},
}
for _, band := range bandMap {
if freq >= band.lower && freq <= band.upper {
return band.name
}
}
return ""
}