-
Notifications
You must be signed in to change notification settings - Fork 11
/
parseigc.js
300 lines (267 loc) · 10.3 KB
/
parseigc.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
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
// Constructor for an exception which is thrown if the file
// being parsed is not in a valid IGC format.
function IGCException(message) {
'use strict';
this.message = message;
this.name = "IGCException";
}
// Parses an IGC logger file.
function parseIGC(igcFile) {
'use strict';
// Looks up the manufacturer name corresponding to
// the three letter code in the first line of the IGC file
// (the 'A' record).
function parseManufacturer(aRecord) {
var manufacturers = {
'GCS': 'Garrecht',
'CAM': 'Cambridge Aero Instruments',
'DSX': 'Data Swan',
'EWA': 'EW Avionics',
'FIL': 'Filser',
'FLA': 'FLARM',
'SCH': 'Scheffel',
'ACT': 'Aircotec',
'NKL': 'Nielsen Kellerman',
'LXN': 'LX Navigation',
'IMI': 'IMI Gliding Equipment',
'NTE': 'New Technologies s.r.l.',
'PES': 'Peschges',
'PRT': 'Print Technik',
'SDI': 'Streamline Data Instruments',
'TRI': 'Triadis Engineering GmbH',
'LXV': 'LXNAV d.o.o.',
'WES': 'Westerboer',
'XCS': 'XCSoar',
'XCT': 'XCTrack',
'ZAN': 'Zander'
};
var manufacturerInfo = {
manufacturer: 'Unknown',
serial: aRecord.substring(4, 7)
};
var manufacturerCode = aRecord.substring(1, 4);
if (manufacturers[manufacturerCode]) {
manufacturerInfo.manufacturer = manufacturers[manufacturerCode];
}
return manufacturerInfo;
}
// Extracts the flight date from the IGC file.
function extractDate(igcFile) {
// Date is recorded as: HFDTEddmmyy (where HFDTE is a literal and dddmmyy are digits),
// OR in the case of XCTrack the format is:
// HFDTEDATE:150522,01
var dateRecord = igcFile.match(/H[FO]DTE(?:DATE\:)?([\d]{2})([\d]{2})([\d]{2})/);
if (dateRecord === null) {
throw new IGCException('The file does not contain a date header.');
}
var day = parseInt(dateRecord[1], 10);
// Javascript numbers months from zero, not 1!
var month = parseInt(dateRecord[2], 10) - 1;
// The IGC specification has a built-in Millennium Bug (2-digit year).
// I will arbitrarily assume that any year before "80" is in the 21st century.
var year = parseInt(dateRecord[3], 10);
if (year < 80) {
year += 2000;
} else {
year += 1900;
}
return new Date(Date.UTC(year, month, day));
}
function parseHeader(headerRecord) {
var headerSubtypes = {
'PLT': 'Pilot',
'CM2': 'Crew member 2',
'GTY': 'Glider type',
'GID': 'Glider ID',
'DTM': 'GPS Datum',
'RFW': 'Firmware version',
'RHW': 'Hardware version',
'FTY': 'Flight recorder type',
'GPS': 'GPS',
'PRS': 'Pressure sensor',
'FRS': 'Security suspect, use validation program',
'CID': 'Competition ID',
'CCL': 'Competition class'
};
var headerName = headerSubtypes[headerRecord.substring(2, 5)];
if (headerName !== undefined) {
var colonIndex = headerRecord.indexOf(':');
if (colonIndex !== -1) {
var headerValue = headerRecord.substring(colonIndex + 1);
if (headerValue.length > 0 && /([^\s]+)/.test(headerValue)) {
return {
name: headerName,
value: headerValue
};
}
}
}
}
// Parses a latitude and longitude in the form:
// DDMMmmmNDDDMMmmmE
// where M = minutes and m = decimal places of minutes.
function parseLatLong(latLongString) {
var latitude = parseFloat(latLongString.substring(0, 2)) +
parseFloat(latLongString.substring(2, 7)) / 60000.0;
if (latLongString.charAt(7) === 'S') {
latitude = -latitude;
}
var longitude = parseFloat(latLongString.substring(8, 11)) +
parseFloat(latLongString.substring(11, 16)) / 60000.0;
if (latLongString.charAt(16) === 'W') {
longitude = -longitude;
}
return [latitude, longitude];
}
function parsePosition(positionRecord, model, flightDate) {
// Regex to match position records:
// Hours, minutes, seconds, latitude, N or S, longitude, E or W,
// Fix validity ('A' = 3D fix, 'V' = 2D or no fix),
// pressure altitude, GPS altitude.
// Latitude and longitude are in degrees and minutes, with the minutes
// value multiplied by 1000 so that no decimal point is needed.
// hours minutes seconds latitude longitude press alt gps alt
var positionRegex = /^B([\d]{2})([\d]{2})([\d]{2})([\d]{7}[NS][\d]{8}[EW])([AV])([\d]{5})([\d]{5})/;
var positionMatch = positionRecord.match(positionRegex);
if (positionMatch) {
// Convert the time to a date and time. Start by making a clone of the date
// object that represents the date given in the headers:
var positionTime = new Date(flightDate.getTime());
positionTime.setUTCHours(parseInt(positionMatch[1], 10), parseInt(positionMatch[2], 10), parseInt(positionMatch[3], 10));
// If the flight crosses midnight (UTC) then we now have a time that is 24 hours out.
// We know that this is the case if the time is earlier than the first position fix.
if (model.recordTime.length > 0 &&
model.recordTime[0] > positionTime) {
positionTime.setDate(flightDate.getDate() + 1);
}
return {
recordTime: positionTime,
latLong: parseLatLong(positionMatch[4]),
pressureAltitude: parseInt(positionMatch[6], 10),
gpsAltitude: parseInt(positionMatch[7], 10)
};
}
}
function parseTask(taskRecord) {
var taskRegex = /^C([\d]{7}[NS][\d]{8}[EW])(.*)/;
var taskMatch = taskRecord.match(taskRegex);
var degreeSymbol = '\u00B0';
if (taskMatch) {
var name = taskMatch[2];
// If the turnpoint name is blank, use the latitude and longitude.
if (name.trim().length === 0) {
name = taskRecord.substring(1, 3) +
degreeSymbol +
taskRecord.substring(3, 5) +
'.' +
taskRecord.substring(5, 8) +
"' " +
taskRecord.charAt(8) +
', ' +
taskRecord.substring(9, 12) +
degreeSymbol +
taskRecord.substring(12, 14) +
'.' +
taskRecord.substring(14, 17) +
"' " +
taskRecord.charAt(17);
}
return {
latLong: parseLatLong(taskMatch[1]),
name: name
};
}
}
// ---- Start of IGC parser code ----
var invalidFileMessage = 'This does not appear to be an IGC file.';
var igcLines = igcFile.split('\n');
if (igcLines.length < 2) {
throw new IGCException(invalidFileMessage);
}
// Declare the model object that is to be returned;
// this contains the position and altitude data and the header
// values.
var model = {
headers: [],
recordTime: [],
latLong: [],
pressureAltitude: [],
gpsAltitude: [],
task: {
coordinates: [],
names: [],
takeoff: "",
landing: ""
}
};
// The first line should begin with 'A' followed by
// a 3-character manufacturer Id and a 3-character serial number.
if (!(/^A[\w]{6}/).test(igcLines[0])) {
throw new IGCException(invalidFileMessage);
}
var manufacturerInfo = parseManufacturer(igcLines[0]);
model.headers.push({
name: 'Logger manufacturer',
value: manufacturerInfo.manufacturer
});
model.headers.push({
name: 'Logger serial number',
value: manufacturerInfo.serial
});
var flightDate = extractDate(igcFile);
var lineIndex;
var positionData;
var recordType;
var currentLine;
var turnpoint; // for task declaration lines
var headerData;
for (lineIndex = 0; lineIndex < igcLines.length; lineIndex++) {
currentLine = igcLines[lineIndex];
recordType = currentLine.charAt(0);
switch (recordType) {
case 'B': // Position fix
positionData = parsePosition(currentLine, model, flightDate);
if (positionData) {
model.recordTime.push(positionData.recordTime);
model.latLong.push(positionData.latLong);
model.pressureAltitude.push(positionData.pressureAltitude);
model.gpsAltitude.push(positionData.gpsAltitude);
}
break;
case 'C': // Task declaration
turnpoint = parseTask(currentLine);
if (turnpoint) {
model.task.coordinates.push(turnpoint.latLong);
model.task.names.push(turnpoint.name);
}
break;
case 'H': // Header information
headerData = parseHeader(currentLine);
if (headerData) {
model.headers.push(headerData);
}
break;
}
}
// Extract takeoff and landing names from model.task and reduce model.task.coordinates to what we want to plot
// Throw away takeoff and landing coordinates as we won't be using them
if(model.task.names.length > 0) {
var takeoffname=model.task.takeoff=model.task.names.shift();
if (model.task.coordinates[0][0]!==0) {
model.task.takeoff=takeoffname;
}
else {
model.task.takeoff="";
}
model.task.coordinates.shift();
var landingname=model.task.names.pop();
if (model.task.coordinates[model.task.coordinates.length-1][0]!==0) {
model.task.landing=landingname;
}
else {
model.task.landing="";
}
model.task.coordinates.pop();
}
return model;
}