-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadif.js
143 lines (118 loc) · 3.55 KB
/
adif.js
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
/* adif.js - Parse ADIF files (https://adif.org) into JavaScript array of QSOs
*
* 2020/09/05 Stephen Houser, N1SH
*/
(function () {
function parseAdif(adifFileData) {
var qsos = [];
// remove all newlines from the file before parsing
cleanAdifData = adifFileData.replace(/(\r\n\t|\n|\r\n|\r\t)/gm, "");
//console.log(cleanAdifData);
var filePartsMatch = cleanAdifData.match(/(.*)\<eoh>(.*)/i);
if (filePartsMatch && filePartsMatch.length == 3) {
header = filePartsMatch[1];
body = filePartsMatch[2];
qsoMatches = body.match(/(.*?)\<eor\>/gi);
for (var q = 0; q < qsoMatches.length; q++) {
qso = parseRecord(qsoMatches[q]);
qsos.push(qso);
}
} else {
throw "Invalid ADIF file. Cannot find header marker <eoh>.";
}
return [header, qsos];
}
// Series of `<name:len>data` fields
/* parseRecord - parse an ADIF record into a QSO object */
function parseRecord(record) {
var qso = {};
fields = record.match(/\<(.+?):.+?\>([^\<]+)/g);
for (var f = 0; f < fields.length; f++) {
var f_v = parseField(fields[f]);
if (f_v !== null && f_v.length == 2) {
qso[f_v[0].toLowerCase()] = f_v[1];
}
}
qso = addDateTimeToQso(qso);
return qso;
}
/* addDateTimeToQso - Adds a computed `date_time` field to the QSO in memory.
*
* This is used later on when displaying the
*/
function addDateTimeToQso(qso) {
var dString = qso.qso_date.substring(0, 4) + '/'
+ qso.qso_date.substring(4, 6) + '/'
+ qso.qso_date.substring(6);
var tString = qso.time_on.substring(0, 2) + ':'
+ qso.time_on.substring(2, 4);
if (qso.time_on.length > 4) {
tString = tString + ':' + qso.time_on.substring(4);
}
newQso = qso;
newQso['date_time'] = dString + ' ' + tString;
return newQso;
}
// `<name:len>data`
// <gridsquare:4>FN43
/* parseField - parse a single ADIF field into a javascript value */
function parseField(field) {
// don't capture whitespace at end of field
var field_value = field.match(/\<(.+?):.+?\>(.*?)\s*$/);
if (field_value !== null) {
return [field_value[1].toLowerCase(), field_value[2]]; // lowercased the ADIF fields
}
return null;
}
/* parseCoordinate - parse an ADIF formatted coordinate to decimal.
*
*/
function parseCoordinate(dms) {
if (typeof dms !== "string") {
throw "parseCoordinate: Invalid (null) coordinate passed.";
}
// Ex: N040 26.719
halfDecimalMatch = dms.match(/([NSEW])([0-9]+)\s+([0-9]*\.?[0-9]+)/);
if (halfDecimalMatch != null) {
var [_, direction, degrees, minutes] = halfDecimalMatch;
var dd = parseInt(degrees) + parseFloat(minutes) / 60;
if (direction == "S" || direction == "W") {
dd = -dd;
}
return dd;
}
// Ex: 43° 41' 11'' N -70° 32' 60'' W
fullDMSMatch = dms.match(
/([+-][0-9]{1,2})°?\s+([0-9]{1,2})'?\s+([0-9]{1,2})(["']{1,2})?\s+([NSEW])/
);
if (fullDMSMatch != null) {
var [_, degrees, minutes, seconds, _, hemisphere] = coordinate.match(
/([+-][0-9]{1,2})°?\s+([0-9]{1,2})'?\s+([0-9]{1,2})(["']{1,2})?\s+([NSEW])/
);
var dd =
parseInt(degrees) +
parseFloat(minutes) / 60 +
parseFloat(seconds) / 60 / 60;
if (hemisphere === "S" || hemisphere === "W") {
dd = -1 * dd;
}
return dd;
}
return dms;
}
function testAll() {
console.log('Not implemented');
return false;
}
/* Export public functions */
var adifExports = {
parseAdif: parseAdif,
parseCoordinate: parseCoordinate,
test: testAll
};
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = adifExports;
} else {
window.Adif = adifExports;
}
})();