-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDaidalusFileWalker.cpp
More file actions
378 lines (340 loc) · 10.4 KB
/
DaidalusFileWalker.cpp
File metadata and controls
378 lines (340 loc) · 10.4 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
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
/*
* Copyright (c) 2015-2021 United States Government as represented by
* the National Aeronautics and Space Administration. No copyright
* is claimed in the United States under Title 17, U.S.Code. All Other
* Rights Reserved.
*/
/*
* DaidalusFileWalker.cpp
*
*/
#include <algorithm>
#include "DaidalusFileWalker.h"
namespace larcfm {
DaidalusFileWalker::DaidalusFileWalker(const std::string& filename) : filename_(filename) {
if (filename.empty()) {
sr_.open(&std::cin);
} else {
sr_.open(filename);
}
init();
}
// Reset input file (do nothing if filename is empty)
void DaidalusFileWalker::resetInputFile(const std::string& filename) {
if (!filename.empty()) {
filename_ = filename;
sr_ = SequenceReader(filename);
init();
}
}
void DaidalusFileWalker::init() {
sr_.setWindowSize(1);
index_ = 0;
times_ = sr_.sequenceKeys();
if (times_.size() > 0) {
sr_.setActive(times_[0]);
}
ownship_.clear();
traffic_.clear();
wind_ = Velocity::INVALID();
columns_ = sr_.getColumnList();
}
/**
* By default ownship is the first aircraft in the daa file.
* This method allows for the selection of a different aircraft as the ownship
* If aircraft with given name doesn't exist at a time step, no ownship or traffic
* is added to the Daidalus object at that particular time step.
*/
void DaidalusFileWalker::setOwnship(const std::string& name) {
ownship_ = name;
}
/**
* Reset the ownship value so that the first aircraft in the daa are considered
* the ownship.
*/
void DaidalusFileWalker::resetOwnship() {
ownship_.clear();
}
/**
* By default all aircraft that are not the ownship are considered to be traffic.
* This method add a particular aircraft to the list of selected aircraft.
* Several aircraft can be selected, but if the list of selected aircraft is non empty,
* only the aircraft in the list are considered traffic.
*/
void DaidalusFileWalker::selectTraffic(const std::string& name) {
traffic_.push_back(name);
}
/**
* By default all aircraft that are not the ownship are considered to be traffic.
* This method add a list of aircraft to the list of selected aircraft.
* Several aircraft can be selected, but if the list of selected aircraft is non empty,
* only the aircraft in the list are considered traffic.
*/
void DaidalusFileWalker::selectTraffic(const std::vector<std::string>& names) {
traffic_.insert(traffic_.end(),names.begin(),names.end());
}
/**
* Reset the list of selected aircraft so that all aircraft that are not ownship are
* considered traffic.
*/
void DaidalusFileWalker::resetSelectedTraffic() {
traffic_.clear();
}
/**
* Set wind velocity (TO direction, i.e., direction the wind blows to)
* This setting will override wind information available in the DAA file
* @param wind Wind (TO direction)
*/
void DaidalusFileWalker::setWindVelocityTo(const Velocity& wind) {
wind_ = wind;
}
/**
* Set wind velocity (FROM direction, i.e., direction the wind comes from)
* This setting will override wind information available in the DAA file
* @param wind Wind (FROM direction)
*/
void DaidalusFileWalker::setWindVelocityFrom(const Velocity& wind) {
if (wind.isInvalid()) {
wind_ = wind;
} else {
wind_ = wind.Neg();
}
}
/**
* Reset wind. Wind from DAA file will be used.
*/
void DaidalusFileWalker::resetWind() {
wind_ = Velocity::INVALID();
}
double DaidalusFileWalker::firstTime() const {
if (!times_.empty()) {
return times_[0];
}
return PINFINITY;
}
double DaidalusFileWalker::lastTime() const {
if (!times_.empty()) {
return times_[times_.size()-1];
}
return NINFINITY;
}
int DaidalusFileWalker::getIndex() const {
return index_;
}
double DaidalusFileWalker::getTime() const {
if (0 <= index_ && index_ < static_cast<int>(times_.size())) {
return times_[index_];
} else {
return NaN;
}
}
bool DaidalusFileWalker::atBeginning() const {
return index_ == 0;
}
bool DaidalusFileWalker::atEnd() const {
return index_ >=0 && index_ == static_cast<int>(times_.size());
}
bool DaidalusFileWalker::goToTime(double t) {
return goToTimeStep(indexOfTime(t));
}
bool DaidalusFileWalker::goToTimeStep(int i) {
if (0 <= i && i < static_cast<int>(times_.size())) {
index_ = i;
sr_.setActive(times_[index_]);
return true;
}
return false;
}
void DaidalusFileWalker::goToBeginning() {
goToTimeStep(0);
}
void DaidalusFileWalker::goToEnd() {
goToTimeStep(times_.size());
}
void DaidalusFileWalker::goNext() {
bool ok = goToTimeStep(index_+1);
if (!ok) {
index_ = times_.size();
}
}
void DaidalusFileWalker::goPrev() {
if (!atBeginning()) {
goToTimeStep(index_-1);
}
}
int DaidalusFileWalker::indexOfTime(double t) const {
int i = -1;
if (t >= firstTime() && t <= lastTime()) {
i = 0;
for (; i < static_cast<int>(times_.size())-1; ++i) {
if (t >= times_[i] && t < times_[i+1]) {
break;
}
}
}
return i;
}
ParameterData DaidalusFileWalker::extraColumnsToParameters(double time, const std::string& ac_name) {
ParameterData pd;
for (std::vector<std::string>::const_iterator col_ptr = columns_.begin(); col_ptr != columns_.end(); ++col_ptr) {
if (sr_.hasExtraColumnData(time, ac_name,*col_ptr)) {
std::string units = sr_.getExtraColumnUnits(*col_ptr);
if (units == "unitless" || units == "unspecified") {
pd.set(*col_ptr, sr_.getExtraColumnString(time,ac_name,*col_ptr));
} else {
pd.setInternal(*col_ptr,sr_.getExtraColumnValue(time, ac_name,*col_ptr), units);
}
}
}
return pd;
}
void DaidalusFileWalker::readExtraColumns(Daidalus& daa, int ac_idx, bool read_wind) {
ParameterData pcol = extraColumnsToParameters(daa.getCurrentTime(),daa.getAircraftStateAt(ac_idx).getId());
if (pcol.size() > 0) {
if (ac_idx == 0) {
// Note that daidalus parameters (in column format) and air velocity are ignored for traffic aircraft
// The index of the aircraft below is the index in daa
daa.setParameterData(pcol);
// Read wind unless is overriden
if (pcol.contains("heading") && pcol.contains("airspeed") && read_wind) {
double heading = pcol.getValue("heading");
double airspeed = pcol.getValue("airspeed");
daa.setOwnshipAirVelocity(heading,airspeed);
}
}
if (pcol.contains("alerter")) {
daa.setAlerterIndex(ac_idx,pcol.getInt("alerter"));
}
double s_EW_std = 0.0;
if (pcol.contains("s_EW_std")) {
s_EW_std = pcol.getValue("s_EW_std");
}
double s_NS_std = 0.0;
if (pcol.contains("s_NS_std")) {
s_NS_std = pcol.getValue("s_NS_std");
}
double s_EN_std = 0.0;
if (pcol.contains("s_EN_std")) {
s_EN_std = pcol.getValue("s_EN_std");
}
daa.setHorizontalPositionUncertainty(ac_idx,s_EW_std,s_NS_std,s_EN_std);
double sz_std = 0.0;
if (pcol.contains("sz_std")) {
sz_std = pcol.getValue("sz_std");
}
daa.setVerticalPositionUncertainty(ac_idx,sz_std);
double v_EW_std = 0.0;
if (pcol.contains("v_EW_std")) {
v_EW_std = pcol.getValue("v_EW_std");
}
double v_NS_std = 0.0;
if (pcol.contains("v_NS_std")) {
v_NS_std = pcol.getValue("v_NS_std");
}
double v_EN_std = 0.0;
if (pcol.contains("v_EN_std")) {
v_EN_std = pcol.getValue("v_EN_std");
}
daa.setHorizontalVelocityUncertainty(ac_idx,v_EW_std,v_NS_std,v_EN_std);
double vz_std = 0.0;
if (pcol.contains("vz_std")) {
vz_std = pcol.getValue("vz_std");
}
daa.setVerticalSpeedUncertainty(ac_idx,vz_std);
}
}
void DaidalusFileWalker::setDAAFormat(DAAFormatter& formatter) const {
formatter.sxyz = false;
formatter.vxyz = false;
formatter.groundvel = false;
formatter.alerter = false;
formatter.sum = false;
for (std::vector<std::string>::const_iterator col_ptr = columns_.begin(); col_ptr != columns_.end(); ++col_ptr) {
const std::string& col = *col_ptr;
if ( col == "sx" || col == "sy" ) {
formatter.sxyz = true;
} else if ( col == "vx" || col == "vy" ) {
formatter.vxyz = true;
} else if ( col == "heading" || col == "airspeed" ) {
formatter.groundvel = true;
} else if ( col == "alerter") {
formatter.alerter = true;
} else if ( col == "s_EW_std" || col == "s_NS_std" || col == "s_EN_std"
|| col == "sz_std" || col == "v_EW_std" || col == "v_NS_std"
|| col == "v_EN_std" || col == "vz_std") {
formatter.sum = true;
}
}
if (!wind_.isInvalid()) {
formatter.groundvel = true;
}
}
void DaidalusFileWalker::setDAAUnits(Daidalus& daa) const {
for (std::vector<std::string>::const_iterator col_ptr = columns_.begin(); col_ptr != columns_.end(); ++col_ptr) {
const std::string& col = *col_ptr;
std::string units = sr_.getExtraColumnUnits(col);
daa.setUnitsOf(col,units);
}
}
bool DaidalusFileWalker::readState(Daidalus& daa) {
if (p_.size() > 0) {
daa.setParameterData(p_);
daa.reset();
}
int own = -1; // By default onwship is 0
for (int ac = 0; ac < sr_.size();++ac) {
if (ownship_.empty() || sr_.getName(ac) == ownship_) {
own = ac;
break;
}
}
if (own >= 0) {
std::string ido = sr_.getName(own);
Position so = sr_.getPosition(own);
Velocity vo = sr_. getVelocity(own);
daa.setOwnshipState(ido,so,vo,getTime());
readExtraColumns(daa,0,wind_.isInvalid());
if (!wind_.isInvalid() && !wind_.isZero()) {
// Override wind
daa.setWindVelocityTo(wind_);
}
for (int ac = 0; ac < sr_.size();++ac) {
if (ac == own) {
continue;
}
std::string ida = sr_.getName(ac);
if (traffic_.empty() ||
std::find(traffic_.begin(),traffic_.end(),ida) != traffic_.end()) {
Position sa = sr_.getPosition(ac);
Velocity va = sr_. getVelocity(ac);
// Notice that idx may be different from ac because of traffic
int idx=daa.addTrafficState(ida,sa,va);
if (idx >= 0) {
readExtraColumns(daa,idx,false);
}
}
}
} else {
std::string msg = "";
if (!ownship_.empty()) {
msg = "("+ownship_+") ";
}
std::cerr << "** Warning: Ownship aircraft " << msg << "not found at time " << FmPrecision(getTime()) << std::endl;
}
goNext();
return own >= 0;
}
// ErrorReporter Interface Methods
bool DaidalusFileWalker::hasError() const {
return sr_.hasError();
}
bool DaidalusFileWalker::hasMessage() const {
return sr_.hasMessage();
}
std::string DaidalusFileWalker::getMessage() {
return sr_.getMessage();
}
std::string DaidalusFileWalker::getMessageNoClear() const {
return sr_.getMessageNoClear();
}
}