forked from GlidingWeb/IGCWebView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseigc.js
More file actions
236 lines (211 loc) · 8.54 KB
/
parseigc.js
File metadata and controls
236 lines (211 loc) · 8.54 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
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
// 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',
'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).
var dateRecord = igcFile.match(/H[FO]DTE([\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][\d]{4})([-\d][\d]{4})/;
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);
}
var curPosition= parseLatLong(positionMatch[4]);
if((curPosition[0] !==0) && (curPosition[1] !==0)) {
return {
recordTime: positionTime,
latLong: curPosition,
pressureAltitude: parseInt(positionMatch[6], 10),
gpsAltitude: parseInt(positionMatch[7], 10)
};
}
}
}
// ---- 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: [],
taskpoints: []
};
// 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 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
var taskRegex = /^C[\d]{7}[NS][\d]{8}[EW].*/;
if (taskRegex.test(currentLine)) {
//drop the "C" and push raw data to model. Will parse later if needed using same functions as for user entered tasks
model.taskpoints.push(currentLine.substring(1).trim());
}
break;
case 'H': // Header information
headerData = parseHeader(currentLine);
if (headerData) {
model.headers.push(headerData);
}
break;
}
}
return model;
}