| 1 |
/** |
| 2 |
* This program parses the U.S. FAA airports data base in its textual format and |
| 3 |
* generates a valid ACM scenery. Starts the program with the --help |
| 4 |
* option for more. |
| 5 |
* |
| 6 |
* |
| 7 |
* IMPLEMENTATION NOTES |
| 8 |
* |
| 9 |
* NOTE 1. LOCATOR misalignment issue workaround. |
| 10 |
* The FAA DB reports the magnetic bearing of the locator as degrees with |
| 11 |
* apparent very high precision (one hundred of degree, ex: "123.45") and the |
| 12 |
* local magnetic deviation at a given date, but this latter with very low |
| 13 |
* precision (1 degree only, ex: "09E" which means a magnetic deviation of |
| 14 |
* +9 DEG). |
| 15 |
* The true bearing can be calculate adding the magnetic deviation to the |
| 16 |
* magnetic bearing so obtaining the nominal true bearing: |
| 17 |
* |
| 18 |
* nominal_true_bearing = nominal_magnetic_bearing + nominal_mag_dev |
| 19 |
* = 123.45 + 9 = 132 +/- 1 DEG |
| 20 |
* |
| 21 |
* This nominal true bearing has then a very poor precision of only 1 degree, |
| 22 |
* which means 32 m of error over a distance of 1 NM. Such an error is |
| 23 |
* acceptable only for non precision approaches (ILS types LDA, SDF), but we |
| 24 |
* expect at least a precision ten times better than that for precision |
| 25 |
* approaches. |
| 26 |
* To get a better precision, this program calculates the actual true bearing |
| 27 |
* of the locator as the direction of the line joining the served runway end |
| 28 |
* to the locator position. The heuristics is then the following: |
| 29 |
* |
| 30 |
* 1. For non-precision ILS (LDA, SDF) use the nominal true bearing. |
| 31 |
* |
| 32 |
* 2. For any other type of ILS, calculate the actual true bearing as stated |
| 33 |
* above; if the result differs less than a degree from the nominal true bearing |
| 34 |
* we assume our value is simply a more accurate value than the nominal one; |
| 35 |
* if the difference is greater, keeps the nominal true bearing assuming the |
| 36 |
* misalignment be a design feature of the installation to preserve but displays |
| 37 |
* a warning. |
| 38 |
* |
| 39 |
* NOTE 2. Cope with current ILS record limitations. |
| 40 |
* In the real world an ILS consists of up to 3 antennas: locator, GS and DME. |
| 41 |
* Each antenna has its own location (latitude and longitude) and its own |
| 42 |
* elevation. Unfortunately, the current format of the ACM ILS record allows to |
| 43 |
* specify the location of the locator and GS and provides only one elevation |
| 44 |
* field; distance is calculated using locator location + available elevation; |
| 45 |
* landing point is calculated using GS location + available elevation. |
| 46 |
* We remedy as follows depending on the type of ILS: |
| 47 |
* |
| 48 |
* 1. LOCATOR only ILS. No problem at all; the elevation field is set with the |
| 49 |
* elevation of the locator. |
| 50 |
* |
| 51 |
* 2. LOCATOR + GS. Elevation field is set with the GS elevation for precise |
| 52 |
* touch down point simulation; actual locator altitude is usually very close |
| 53 |
* and does not care too much anyway. |
| 54 |
* |
| 55 |
* 3. LOCATOR + DME. ACM assumes the DME antenna coincides with the locator, |
| 56 |
* which is already a source of error for precise distance measurement; |
| 57 |
* to mitigate this, the elevation field is set with the actual DME elevation; |
| 58 |
* actual locator altitude is usually very close and does not care too much |
| 59 |
* anyway. |
| 60 |
* |
| 61 |
* 4. LOCATOR + GS + DME. Just like LOCATOR + GS for precise touch down point. |
| 62 |
* |
| 63 |
* Further complication: the elevation field is blank in many cases, so we |
| 64 |
* take the value available in the following order of preference: |
| 65 |
* GS, DME, LOCATOR. If no value available at all, zero is assumed and a |
| 66 |
* warning is displayed. |
| 67 |
* |
| 68 |
* @file |
| 69 |
* @author Umberto Salsi <[email protected]> |
| 70 |
* @version $Date: 2018/09/23 13:05:36 $ |
| 71 |
*/ |
| 72 |
#include <stdio.h> |
| 73 |
#include <stdlib.h> |
| 74 |
#include <assert.h> |
| 75 |
#include <ctype.h> |
| 76 |
#include <string.h> |
| 77 |
|
| 78 |
#include "../../src/util/error.h" |
| 79 |
#include "../../src/util/units.h" |
| 80 |
#include "../../src/util/memory.h" |
| 81 |
#include "../../src/util/hashtable.h" |
| 82 |
#include "../../src/dis/dis/earth.h" |
| 83 |
|
| 84 |
/** |
| 85 |
* State of the string pattern matcher. Our custom implementation of the regex |
| 86 |
* is simple, fast an portable across Windows an Linux. |
| 87 |
*/ |
| 88 |
typedef struct { |
| 89 |
/** |
| 90 |
* Next char to parse. Each match_X() function increments this pointer |
| 91 |
* whenever the parsing of X succeeded. |
| 92 |
*/ |
| 93 |
char *c; |
| 94 |
} match_Type; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Initializes the string pattern matcher. |
| 99 |
* @param this |
| 100 |
* @param s Subject string to parse. |
| 101 |
*/ |
| 102 |
static void match_init(match_Type *this, char *s) |
| 103 |
{ |
| 104 |
this->c = s; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Matches the end of the subject string. |
| 109 |
* @param this |
| 110 |
* @return True if currently at the end of the subject string. |
| 111 |
*/ |
| 112 |
static int match_end(match_Type *this) |
| 113 |
{ |
| 114 |
return *this->c == 0; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Matches a char. |
| 119 |
* @param this |
| 120 |
* @param c |
| 121 |
* @return |
| 122 |
*/ |
| 123 |
static int match_char(match_Type *this, char c) |
| 124 |
{ |
| 125 |
if( *this->c == c ){ |
| 126 |
this->c++; |
| 127 |
return 1; |
| 128 |
} else { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Matches a set of chars. |
| 135 |
* @param this |
| 136 |
* @param set Set of chars. |
| 137 |
* @return |
| 138 |
*/ |
| 139 |
static int match_set(match_Type *this, char *set) |
| 140 |
{ |
| 141 |
while( *set != 0 && *set != *this->c ) |
| 142 |
set++; |
| 143 |
if( *set != 0 ){ |
| 144 |
this->c++; |
| 145 |
return 1; |
| 146 |
} else { |
| 147 |
return 0; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Matches a capital letter. |
| 153 |
* @param this |
| 154 |
* @return |
| 155 |
*/ |
| 156 |
static int match_capital(match_Type *this) |
| 157 |
{ |
| 158 |
if( 'A' <= *this->c && *this->c <= 'Z' ){ |
| 159 |
this->c++; |
| 160 |
return 1; |
| 161 |
} else { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Matches a digit. |
| 168 |
* @param this |
| 169 |
* @return |
| 170 |
*/ |
| 171 |
static int match_digit(match_Type *this) |
| 172 |
{ |
| 173 |
if( '0' <= *this->c && *this->c <= '9' ){ |
| 174 |
this->c++; |
| 175 |
return 1; |
| 176 |
} else { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Matches digits. |
| 183 |
* @param this |
| 184 |
* @param min Minimum number of digits to parse. |
| 185 |
* @param max Maximum number of digits to parse. |
| 186 |
* @return |
| 187 |
*/ |
| 188 |
static int match_digits(match_Type *this, int min, int max) |
| 189 |
{ |
| 190 |
char *back = this->c; |
| 191 |
int i = 0; |
| 192 |
while( i < max ){ |
| 193 |
if( ! match_digit(this) ){ |
| 194 |
if( i < min ){ |
| 195 |
this->c = back; |
| 196 |
return 0; |
| 197 |
} else { |
| 198 |
return 1; |
| 199 |
} |
| 200 |
} |
| 201 |
i++; |
| 202 |
} |
| 203 |
return 1; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Matches one or more digits and a possible decimal part. Note that the number |
| 209 |
* must start with a digit, so ".5" isn't valid. |
| 210 |
*/ |
| 211 |
static int match_decimal(match_Type *this) |
| 212 |
{ |
| 213 |
char *back = this->c; |
| 214 |
if( match_digits(this, 1, 9) |
| 215 |
&& ((match_char(this, '.') && match_digits(this, 1, 9)) || 1) ){ |
| 216 |
return 1; |
| 217 |
} else { |
| 218 |
this->c = back; |
| 219 |
return 0; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* Returns true if the string is a decimal number. Note that the number |
| 226 |
* must start with a digit, so ".5" isn't valid. |
| 227 |
*/ |
| 228 |
static int isDecimal(char *s) |
| 229 |
{ |
| 230 |
match_Type m; |
| 231 |
match_init(&m, s); |
| 232 |
return match_decimal(&m) |
| 233 |
&& match_end(&m); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns true if the string is a decimal number with possible leading sign. |
| 238 |
* Note that the number must have at least one digit in its integral part, so |
| 239 |
* "+.5" isn't valid (rationale: the standard atof() function does not support |
| 240 |
* this abbreviated format). |
| 241 |
*/ |
| 242 |
static int isSignedDecimal(char *s) |
| 243 |
{ |
| 244 |
match_Type m; |
| 245 |
match_init(&m, s); |
| 246 |
return (match_set(&m, "+-") || 1) |
| 247 |
&& match_decimal(&m) |
| 248 |
&& match_end(&m); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Returns true if the string is a valid latitude. Ex.: "12-34-56.78N". |
| 253 |
* FIXME: does not check range. |
| 254 |
*/ |
| 255 |
static int isLatitude(char *s) |
| 256 |
{ |
| 257 |
match_Type m; |
| 258 |
match_init(&m, s); |
| 259 |
return match_digits(&m, 1, 2) |
| 260 |
&& match_char(&m, '-') |
| 261 |
&& match_digits(&m, 1, 2) |
| 262 |
&& match_char(&m, '-') |
| 263 |
&& match_digits(&m, 1, 3) |
| 264 |
&& ( ! match_char(&m, '.') || match_digits(&m, 1, 4) ) |
| 265 |
&& match_set(&m, "NS") |
| 266 |
&& match_end(&m); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Returns true if the string is a valid longitude. Ex.: "12-34-56.78E". |
| 271 |
* FIXME: does not check range. |
| 272 |
*/ |
| 273 |
static int isLongitude(char *s) |
| 274 |
{ |
| 275 |
match_Type m; |
| 276 |
match_init(&m, s); |
| 277 |
return match_digits(&m, 1, 3) |
| 278 |
&& match_char(&m, '-') |
| 279 |
&& match_digits(&m, 1, 2) |
| 280 |
&& match_char(&m, '-') |
| 281 |
&& match_digits(&m, 1, 3) |
| 282 |
&& ( ! match_char(&m, '.') || match_digits(&m, 1, 4) ) |
| 283 |
&& match_set(&m, "WE") |
| 284 |
&& match_end(&m); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Matches a runway end identifier. Valid identifiers must contain 1 or 2 digits |
| 289 |
* and a possible letter among "LRCX"; the "X" letter seems used for LDA ILS only. |
| 290 |
* @param this |
| 291 |
* @return |
| 292 |
*/ |
| 293 |
static int match_runwayEndID(match_Type *this) |
| 294 |
{ |
| 295 |
return match_digit(this) |
| 296 |
&& (match_digit(this) || 1) |
| 297 |
&& (match_set(this, "LRCX") || 1); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Returns true if the string is a valid runway end identifier. |
| 303 |
* @param s |
| 304 |
* @return |
| 305 |
*/ |
| 306 |
static int isRunwayEndID(char *s) |
| 307 |
{ |
| 308 |
match_Type m; |
| 309 |
match_init(&m, s); |
| 310 |
return match_runwayEndID(&m) |
| 311 |
&& match_end(&m); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Returns true if the string is a valid runway identifier. Valid |
| 316 |
* identifiers must contain two valid runway end IDs separated by "/". |
| 317 |
* @param s |
| 318 |
* @return |
| 319 |
*/ |
| 320 |
static int isRunwayID(char *s) |
| 321 |
{ |
| 322 |
match_Type m; |
| 323 |
match_init(&m, s); |
| 324 |
return match_runwayEndID(&m) |
| 325 |
&& match_char(&m, '/') |
| 326 |
&& match_runwayEndID(&m) |
| 327 |
&& match_end(&m); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Returns true if the string contains a valid ILS identifier. |
| 332 |
*/ |
| 333 |
static int isILSID(char *s) |
| 334 |
{ |
| 335 |
match_Type m; |
| 336 |
match_init(&m, s); |
| 337 |
return match_char(&m, 'I') |
| 338 |
&& match_char(&m, '-') |
| 339 |
&& match_capital(&m) && match_capital(&m) && match_capital(&m) |
| 340 |
&& match_end(&m); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/* |
| 345 |
* Range of latitude and longitude of the items we are interested to extract. |
| 346 |
* The value must be greater or equal to the minimum, and strictly less than the |
| 347 |
* maximum. |
| 348 |
* For runways, the base end is compared. |
| 349 |
* For ILS, the position of the localized antenna is compared. |
| 350 |
* For NDB and VOR with associated DME, the location of the NDB or VOR antenna |
| 351 |
* is compared. |
| 352 |
*/ |
| 353 |
static double lat_min = units_DEGtoRAD(-90); |
| 354 |
static double lat_max = units_DEGtoRAD(91); |
| 355 |
static double lon_min = units_DEGtoRAD(-180); |
| 356 |
static double lon_max = units_DEGtoRAD(181); |
| 357 |
|
| 358 |
/* |
| 359 |
* State of the input file we are parsing. Since we parse one file at a time, |
| 360 |
* one only set of global variables make this program simpler. Moreover, |
| 361 |
* the program abort at any error, and this further simplifies things. |
| 362 |
*/ |
| 363 |
static char line[9999]; // current line to parse |
| 364 |
static int line_len; // length of the current line |
| 365 |
static int line_no; // no. of the current line, first is no. 1 |
| 366 |
static char *in_filename; // filename of the current file |
| 367 |
static FILE *in; // current file |
| 368 |
|
| 369 |
/** |
| 370 |
* Opens the file to parse. Aborts on error. |
| 371 |
* @param filename |
| 372 |
*/ |
| 373 |
static void openFile(char *filename) |
| 374 |
{ |
| 375 |
in_filename = filename; |
| 376 |
in = fopen(in_filename, "r"); |
| 377 |
if( in == NULL ) |
| 378 |
error_system("opening %s", in_filename); |
| 379 |
line_no = 0; |
| 380 |
line[0] = 0; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Reads the next line and makes it available in global variables. |
| 386 |
* Aborts on error. |
| 387 |
* @return True if a new line is available, false at end of the file. |
| 388 |
*/ |
| 389 |
static int readLine() |
| 390 |
{ |
| 391 |
if( fgets(line, sizeof(line), in) == NULL ){ |
| 392 |
if( feof(in) ){ |
| 393 |
line[0] = 0; |
| 394 |
return 0; |
| 395 |
} else { |
| 396 |
error_external("%s:%d: file read error", in_filename, line_no); |
| 397 |
return 0; // prevents gcc -Wall complain |
| 398 |
} |
| 399 |
} else { |
| 400 |
line_no++; |
| 401 |
line_len = strlen(line); |
| 402 |
// Removes trailing "\r\n" or "\n": |
| 403 |
if( line_len > 0 && line[line_len-1] == '\n' ){ |
| 404 |
line[line_len-1] = 0; |
| 405 |
line_len--; |
| 406 |
if( line_len > 0 && line[line_len-1] == '\r' ){ |
| 407 |
line[line_len-1] = 0; |
| 408 |
line_len--; |
| 409 |
} |
| 410 |
} |
| 411 |
return 1; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* Extracts a field from the current line. Aborts if the current line is too |
| 418 |
* short. Aborts if the destination field cannot contain the source field and |
| 419 |
* the trailing NUL byte even after leading and trailing white space have been |
| 420 |
* removed. |
| 421 |
* @param start Byte offset of the beginning. |
| 422 |
* @param len Length of the field. |
| 423 |
* @param field Destination of the field; leading and trailing white spaces are |
| 424 |
* removed and a trailing NUL byte is added. |
| 425 |
* @param field_capacity Capacity of the destination field, including terminating |
| 426 |
* NUL byte. |
| 427 |
*/ |
| 428 |
static void getField(int start, int len, char *field, int field_capacity) |
| 429 |
{ |
| 430 |
assert( len + 1 <= field_capacity ); |
| 431 |
if( start + len > line_len ) |
| 432 |
error_external("%s:%d: cannot parse field at offset %d length %d because line input length too short: %d", in_filename, line_no, start, len, line_len); |
| 433 |
int a = start; |
| 434 |
int b = start + len; |
| 435 |
while( a < b && isspace(line[a]) ) |
| 436 |
a++; |
| 437 |
while( b > a && isspace(line[b-1]) ) |
| 438 |
b--; |
| 439 |
int field_len = b - a; |
| 440 |
if( field_len > 0 ) |
| 441 |
memcpy(field, line + a, field_len); |
| 442 |
field[field_len] = 0; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
static int getLatitudeField(int start, int len, char *field, int field_capacity) |
| 447 |
{ |
| 448 |
getField(start, len, field, field_capacity); |
| 449 |
if( isLatitude(field) ){ |
| 450 |
return 1; |
| 451 |
} else { |
| 452 |
fprintf(stderr, "%s:%d: invalid latitude: %s\n", |
| 453 |
in_filename, line_no, field); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
static int getLongitudeField(int start, int len, char *field, int field_capacity) |
| 460 |
{ |
| 461 |
getField(start, len, field, field_capacity); |
| 462 |
if( isLongitude(field) ){ |
| 463 |
return 1; |
| 464 |
} else { |
| 465 |
fprintf(stderr, "%s:%d: invalid longitude: %s\n", |
| 466 |
in_filename, line_no, field); |
| 467 |
return 0; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
/** |
| 473 |
* Normalize angle to the canonical range [0,2*M_PI[. |
| 474 |
*/ |
| 475 |
static double normalizeAngle(double a) |
| 476 |
{ |
| 477 |
if( ! isfinite(a) ) |
| 478 |
error_internal("cannot normalize non-finite angle: %g", a); |
| 479 |
if(a <= -2*M_PI) |
| 480 |
a = a + 2*M_PI*floor(-a / (2*M_PI)); |
| 481 |
else if(a >= 2*M_PI) |
| 482 |
a = a - 2*M_PI*floor(a / (2*M_PI)); |
| 483 |
if( a < 0 ) |
| 484 |
a = a + 2*M_PI; |
| 485 |
else if( a >= 2*M_PI ) |
| 486 |
a = a - 2*M_PI; |
| 487 |
return a; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
// ------------------------ RWY ---------------------------- |
| 492 |
|
| 493 |
|
| 494 |
typedef struct { |
| 495 |
char pk[12]; // airport primary key of the data base |
| 496 |
char id[29]; |
| 497 |
char type[14]; |
| 498 |
char elev[8]; |
| 499 |
} Airport; |
| 500 |
|
| 501 |
|
| 502 |
/** |
| 503 |
* Maps airport's pk to the allocated Airport. |
| 504 |
*/ |
| 505 |
static hashtable_Type *airports; |
| 506 |
|
| 507 |
/** |
| 508 |
* Runway end. The airport and end ID are the key of the entry. |
| 509 |
* Used to calculate the actual true bearing of the locator (see implementation |
| 510 |
* note 1). |
| 511 |
*/ |
| 512 |
typedef struct { |
| 513 |
char pk[12]; // airport primary key of the data base |
| 514 |
char id[4]; // runway end ID |
| 515 |
double lat; |
| 516 |
double lon; |
| 517 |
} RunwayEnd; |
| 518 |
|
| 519 |
/** Maps airport pk + runway end ID to runway end. */ |
| 520 |
static hashtable_Type *runwayends; |
| 521 |
|
| 522 |
/** Get hash of runway end callback. */ |
| 523 |
static int getHashOfRunwayEnd(void *rwyend_) |
| 524 |
{ |
| 525 |
RunwayEnd *rwyend = (RunwayEnd *) rwyend_; |
| 526 |
return hashtable_getHashOfString(rwyend->pk)*3 |
| 527 |
+ hashtable_getHashOfString(rwyend->id); |
| 528 |
} |
| 529 |
|
| 530 |
/** Compare runway ends callback. */ |
| 531 |
static int equalRunwayEnd(void *a_, void *b_) |
| 532 |
{ |
| 533 |
RunwayEnd *a = (RunwayEnd *) a_; |
| 534 |
RunwayEnd *b = (RunwayEnd *) b_; |
| 535 |
return strcmp(a->pk, b->pk) == 0 && strcmp(a->id, b->id) == 0; |
| 536 |
} |
| 537 |
|
| 538 |
/** Add runway end to hash table. */ |
| 539 |
static void addRunwayEnd(char *airport_pk, char *id, double lat, double lon) |
| 540 |
{ |
| 541 |
RunwayEnd *rwyend = memory_allocate(sizeof(RunwayEnd), NULL); |
| 542 |
memory_strcpy(rwyend->pk, sizeof(rwyend->pk), airport_pk); |
| 543 |
memory_strcpy(rwyend->id, sizeof(rwyend->id), id); |
| 544 |
rwyend->lat = lat; |
| 545 |
rwyend->lon = lon; |
| 546 |
if( hashtable_get(runwayends, rwyend) != NULL ) |
| 547 |
fprintf(stderr, "%s:%d: ERROR: duplicated runway end\n", |
| 548 |
in_filename, line_no); |
| 549 |
hashtable_put(runwayends, rwyend, rwyend); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
/** |
| 554 |
* Parses the airports data base file "APT.txt" and send to standard output |
| 555 |
* the filtered list of the RWY records. As a side effect, the global airports |
| 556 |
* array remains available, although currently not used elsewhere here. |
| 557 |
*/ |
| 558 |
static void parseAPT(char *apt_filename) |
| 559 |
{ |
| 560 |
airports = hashtable_new(hashtable_getHashOfString, hashtable_equalStrings); |
| 561 |
|
| 562 |
runwayends = hashtable_new(getHashOfRunwayEnd, equalRunwayEnd); |
| 563 |
|
| 564 |
openFile(apt_filename); |
| 565 |
|
| 566 |
// Step 1: parse all airports. Generate airports hash table. |
| 567 |
while( readLine() ){ |
| 568 |
if( ! memory_startsWith(line, "APT") ) |
| 569 |
continue; |
| 570 |
Airport *airport = memory_allocate(sizeof(Airport), NULL); |
| 571 |
getField(3, 11, airport->pk, sizeof(airport->pk)); |
| 572 |
getField(27, 4, airport->id, sizeof(airport->id)); |
| 573 |
getField(14, 13, airport->type, sizeof(airport->type)); |
| 574 |
getField(578, 7, airport->elev, sizeof(airport->elev)); |
| 575 |
hashtable_put(airports, airport->pk, airport); |
| 576 |
} |
| 577 |
|
| 578 |
// Step 2: parse runways and runway ends. Generate runway ends hash table |
| 579 |
// and RWY output records. |
| 580 |
rewind(in); |
| 581 |
line_no = 0; |
| 582 |
while( readLine() ){ |
| 583 |
if( ! memory_startsWith(line, "RWY") ) |
| 584 |
continue; |
| 585 |
|
| 586 |
// RWY id: |
| 587 |
char id[8]; |
| 588 |
getField(16, 7, id, sizeof(id)); |
| 589 |
if( ! isRunwayID(id) ){ |
| 590 |
// Silently discards helicopter, sea, balloon, ice runways. |
| 591 |
continue; |
| 592 |
} |
| 593 |
|
| 594 |
// RWY latitude 1: |
| 595 |
char lat1_text[16]; |
| 596 |
getField(88, 15, lat1_text, sizeof(lat1_text)); |
| 597 |
if( lat1_text[0] == 0 ) |
| 598 |
continue; |
| 599 |
double lat1; |
| 600 |
if( ! earth_parseLatitude(lat1_text, &lat1) ){ |
| 601 |
fprintf(stderr, "%s:%d: failed to parse RWY %s latitude: %s - ignore\n", in_filename, line_no, id, lat1_text); |
| 602 |
continue; |
| 603 |
} |
| 604 |
|
| 605 |
// RWY longitude 1: |
| 606 |
char lon1_text[16]; |
| 607 |
getField(115, 15, lon1_text, sizeof(lon1_text)); |
| 608 |
double lon1; |
| 609 |
if( ! earth_parseLongitude(lon1_text, &lon1) ){ |
| 610 |
fprintf(stderr, "%s:%d: failed to parse RWY longitude: %s - ignore\n", in_filename, line_no, lon1_text); |
| 611 |
continue; |
| 612 |
} |
| 613 |
|
| 614 |
char pk[12]; |
| 615 |
getField(3, 11, pk, sizeof(pk)); |
| 616 |
Airport *airport = hashtable_get(airports, pk); |
| 617 |
if( airport == NULL ){ |
| 618 |
fprintf(stderr, "%s:%d: RWY record refers to unknown airport primary key: %s - ignore\n", in_filename, line_no, pk); |
| 619 |
continue; |
| 620 |
} |
| 621 |
if( !(strcmp(airport->type, "AIRPORT") == 0 || strcmp(airport->type, "ULTRALIGHT") == 0) ) |
| 622 |
continue; |
| 623 |
|
| 624 |
char len[6]; |
| 625 |
getField(23, 5, len, sizeof(len)); |
| 626 |
char width[5]; |
| 627 |
getField(28, 4, width, sizeof(width)); |
| 628 |
|
| 629 |
// RWY latitude 2: |
| 630 |
char lat2_text[16]; |
| 631 |
if( ! getLatitudeField(310, 15, lat2_text, sizeof(lat2_text)) ) |
| 632 |
continue; |
| 633 |
double lat2; |
| 634 |
if( ! earth_parseLatitude(lat2_text, &lat2) ){ |
| 635 |
fprintf(stderr, "%s:%d: failed to parse RWY %s latitude: %s - ignore\n", in_filename, line_no, id, lat2_text); |
| 636 |
continue; |
| 637 |
} |
| 638 |
|
| 639 |
// RWY longitude 2: |
| 640 |
char lon2_text[16]; |
| 641 |
getField(337, 15, lon2_text, sizeof(lon2_text)); |
| 642 |
double lon2; |
| 643 |
if( ! earth_parseLongitude(lon2_text, &lon2) ){ |
| 644 |
fprintf(stderr, "%s:%d: failed to parse RWY longitude: %s - ignore\n", in_filename, line_no, lon2_text); |
| 645 |
continue; |
| 646 |
} |
| 647 |
|
| 648 |
// Ignore runway if middle point lies outside the range: |
| 649 |
double center_lat = (lat1 + lat2)/2; |
| 650 |
double center_lon = (lon1 + lon2)/2; |
| 651 |
if( !(lat_min <= center_lat && center_lat < lat_max |
| 652 |
&& lon_min <= center_lon && center_lon < lon_max) ) |
| 653 |
continue; |
| 654 |
|
| 655 |
char id1[4]; |
| 656 |
getField(65, 3, id1, sizeof(id1)); |
| 657 |
char id2[4]; |
| 658 |
getField(287, 3, id2, sizeof(id2)); |
| 659 |
|
| 660 |
addRunwayEnd(pk, id1, lat1, lon1); |
| 661 |
addRunwayEnd(pk, id2, lat2, lon2); |
| 662 |
|
| 663 |
printf("RWY %s %s %.0f %s %s %s %s %s %s\n", |
| 664 |
airport->id, |
| 665 |
id, |
| 666 |
atof(airport->elev), // rounding to integral foot |
| 667 |
len, width, lat1_text, lon1_text, lat2_text, lon2_text); |
| 668 |
} |
| 669 |
|
| 670 |
fclose(in); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
// ------------------- ILS --------------------------- |
| 675 |
|
| 676 |
|
| 677 |
/** |
| 678 |
* Key that univocally identifies a ILS. Informations about a single ILS are |
| 679 |
* split among several records: this key allows to rejoin these records |
| 680 |
* together. |
| 681 |
*/ |
| 682 |
typedef struct { |
| 683 |
char airport_pk[12]; |
| 684 |
char runwayEndId[4]; |
| 685 |
char type[11]; |
| 686 |
} ILSKey; |
| 687 |
|
| 688 |
/** Get ILS hash callback. */ |
| 689 |
static int getILSKeyHash(void *ilskey_) |
| 690 |
{ |
| 691 |
ILSKey *ilskey = (ILSKey *) ilskey_; |
| 692 |
return (hashtable_getHashOfString(ilskey->airport_pk) * 3 |
| 693 |
+ hashtable_getHashOfString(ilskey->runwayEndId)) * 3 |
| 694 |
+ hashtable_getHashOfString(ilskey->type); |
| 695 |
} |
| 696 |
|
| 697 |
/** Compare ILS key callback. */ |
| 698 |
static int equalILSKeys(void *a_, void *b_) |
| 699 |
{ |
| 700 |
ILSKey *a = (ILSKey *) a_; |
| 701 |
ILSKey *b = (ILSKey *) b_; |
| 702 |
return strcmp(a->airport_pk, b->airport_pk) == 0 |
| 703 |
&& strcmp(a->runwayEndId, b->runwayEndId) == 0 |
| 704 |
&& strcmp(a->type, b->type) == 0; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
/** |
| 709 |
* Collected data about an ILS. Note that in the ILS record of out scenery |
| 710 |
* format there is only one elevation field available for all the antennas and |
| 711 |
* there are no fields for the location of the DME, then ACM shows the distance |
| 712 |
* from the LOCALIZER instead. Then, we collect all the elevations of all the |
| 713 |
* antennas and use the first one available among these (in order of preference): |
| 714 |
* 1. GS elevation, so the landing spot is at the planned distance from the |
| 715 |
* runway threshold. |
| 716 |
* 2. DME elevation, so the displayed distance closely matches the expected value. |
| 717 |
* 3. LOCALIZER elevation as last resort. |
| 718 |
*/ |
| 719 |
typedef struct { |
| 720 |
ILSKey key; // key shared by records referring to a specific ILS |
| 721 |
char airport_pk[12]; |
| 722 |
char id[7]; // name of the ILS, typically "I-XXX" |
| 723 |
double nominal_true_bearing; // nominal true bearing |
| 724 |
|
| 725 |
// LOCALIZER data: |
| 726 |
int loc_line_no; // line no. in the file for error messages |
| 727 |
char loc_status[23]; |
| 728 |
char loc_lat[15]; |
| 729 |
char loc_lon[15]; |
| 730 |
char loc_elev[8]; |
| 731 |
char loc_freq[8]; |
| 732 |
char loc_width[6]; |
| 733 |
|
| 734 |
// GS data: |
| 735 |
int gs_line_no; // line no. in the file for error messages |
| 736 |
char gs_status[23]; |
| 737 |
char gs_lat[15]; |
| 738 |
char gs_lon[15]; |
| 739 |
char gs_elev[8]; |
| 740 |
char gs_angle[6]; |
| 741 |
|
| 742 |
// DME data: |
| 743 |
char dme_elev[8]; |
| 744 |
} ILS; |
| 745 |
|
| 746 |
/** |
| 747 |
* Dictionary that maps our ILS key into the allocated ILS. |
| 748 |
*/ |
| 749 |
hashtable_Type *ilss; |
| 750 |
|
| 751 |
|
| 752 |
/** |
| 753 |
* Parse the ILS key fields from the current line, searches the dictionary |
| 754 |
* for a matching ILS or allocates a new one if not found. |
| 755 |
* @return Allocated ILS data structure that matches the key, or NULL if |
| 756 |
* parsing failed. |
| 757 |
*/ |
| 758 |
static ILS * parseILSKey() |
| 759 |
{ |
| 760 |
ILSKey key; |
| 761 |
|
| 762 |
getField(4, 11, key.airport_pk, sizeof(key.airport_pk)); |
| 763 |
if( key.airport_pk[0] == 0 ){ |
| 764 |
fprintf(stderr, "%s:%d: empty airport primary key\n", in_filename, line_no); |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
getField(15, 3, key.runwayEndId, sizeof(key.runwayEndId)); |
| 769 |
if( ! isRunwayEndID(key.runwayEndId) ){ |
| 770 |
fprintf(stderr, "%s:%d: invalid runway end ID: %s\n", in_filename, line_no, key.runwayEndId); |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
|
| 774 |
getField(18, 10, key.type, sizeof(key.type)); |
| 775 |
if( key.type[0] == 0 ){ |
| 776 |
fprintf(stderr, "%s:%d: empty airport type\n", in_filename, line_no); |
| 777 |
return NULL; |
| 778 |
} |
| 779 |
|
| 780 |
ILS *ils = hashtable_get(ilss, &key); |
| 781 |
if( ils == NULL ){ |
| 782 |
ils = memory_allocate(sizeof(ILS), NULL); |
| 783 |
memset(ils, 0, sizeof(*ils)); |
| 784 |
ils->key = key; |
| 785 |
hashtable_put(ilss, &ils->key, ils); |
| 786 |
} |
| 787 |
return ils; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
/** |
| 792 |
* Parse magnetic bearing and magnetic variation and returns the true bearing. |
| 793 |
* @param bearing_mag_deg Ex. "012". |
| 794 |
* @param mag_var Ex. "09E". |
| 795 |
* @return True bearing in [0,2*M_PI[, or -1 if parsing failed. |
| 796 |
*/ |
| 797 |
static double computeTrueBearing(char *bearing_mag_deg, char *mag_var) |
| 798 |
{ |
| 799 |
// Parse bearing: |
| 800 |
match_Type m; |
| 801 |
match_init(&m, bearing_mag_deg); |
| 802 |
if( !( match_decimal(&m) && match_end(&m) ) ) |
| 803 |
return -1; |
| 804 |
double bearing_true_deg = atof(bearing_mag_deg); |
| 805 |
|
| 806 |
// Parse magnetic variation: |
| 807 |
match_init(&m, mag_var); |
| 808 |
if( !(match_decimal(&m) && match_set(&m, "EW") && match_end(&m) ) ) |
| 809 |
return -1; |
| 810 |
int mag_var_len = strlen(mag_var); |
| 811 |
char sign = mag_var[mag_var_len-1]; |
| 812 |
|
| 813 |
// Calculate true bearing: |
| 814 |
mag_var[mag_var_len-1] = 0; |
| 815 |
if( sign == 'E' ) |
| 816 |
bearing_true_deg += atof(mag_var); |
| 817 |
else |
| 818 |
bearing_true_deg -= atof(mag_var); |
| 819 |
mag_var[mag_var_len-1] = sign; // restore original |
| 820 |
|
| 821 |
return normalizeAngle( units_DEGtoRAD(bearing_true_deg) ); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
/** |
| 826 |
* Returns the true bearing going from (lat1,lon1) to (lat2,lon2). |
| 827 |
* Used to calculate the actual bearing from runway end to locator. |
| 828 |
*/ |
| 829 |
static double trueBearing(double lat1, double lon1, double lat2, double lon2) |
| 830 |
{ |
| 831 |
double dlat = lat2 - lat1; |
| 832 |
double dlon = lon2 - lon1; |
| 833 |
return normalizeAngle( atan2(dlon * cos(lat1), dlat) ); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
static int isILSFreq(char *s) |
| 838 |
{ |
| 839 |
match_Type m; |
| 840 |
match_init(&m, s); |
| 841 |
if( !(match_digits(&m, 1, 3) && (!match_char(&m, '.') || match_digits(&m, 1, 2)) && match_end(&m)) ) |
| 842 |
return 0; |
| 843 |
int freq = floor(atof(s) * 100 + 0.5); // tenth of KHz |
| 844 |
return 10800 <= freq && freq <= 11795; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
static void parseILS(char *ils_filename) |
| 849 |
{ |
| 850 |
// Step 1: collect data for each specific ILS, spread among several records. |
| 851 |
ilss = hashtable_new(getILSKeyHash, equalILSKeys); |
| 852 |
openFile(ils_filename); |
| 853 |
while( readLine() ){ |
| 854 |
|
| 855 |
if( memory_startsWith(line, "ILS1") ){ |
| 856 |
ILS *ils = parseILSKey(); |
| 857 |
if( ils == NULL ) |
| 858 |
continue; |
| 859 |
|
| 860 |
getField(4, 11, ils->airport_pk, sizeof(ils->airport_pk)); |
| 861 |
|
| 862 |
getField(28, 6, ils->id, sizeof(ils->id)); |
| 863 |
if( ! isILSID(ils->id) ){ |
| 864 |
fprintf(stderr, "%s:%d: invalid ILS ID '%s', check!\n", |
| 865 |
in_filename, line_no, ils->id); |
| 866 |
} |
| 867 |
|
| 868 |
char bearing_mag_deg[7]; |
| 869 |
getField(281, 6, bearing_mag_deg, sizeof(bearing_mag_deg)); |
| 870 |
char mag_var[7]; |
| 871 |
getField(287, 3, mag_var, sizeof(mag_var)); |
| 872 |
ils->nominal_true_bearing = computeTrueBearing(bearing_mag_deg, mag_var); |
| 873 |
if( ils->nominal_true_bearing < 0 ){ |
| 874 |
fprintf(stderr, "%s:%d: invalid magnetic bearing ('%s') or magnetic variation ('%s') for ILS %s, check!\n", |
| 875 |
in_filename, line_no, bearing_mag_deg, mag_var, ils->id); |
| 876 |
ils->nominal_true_bearing = 0; |
| 877 |
} |
| 878 |
|
| 879 |
} else if( memory_startsWith(line, "ILS2") ){ // LOCALIZER |
| 880 |
ILS *ils = parseILSKey(); |
| 881 |
if( ils == NULL ) |
| 882 |
continue; |
| 883 |
ils->loc_line_no = line_no; |
| 884 |
getField(28, 22, ils->loc_status, sizeof(ils->loc_status)); |
| 885 |
if( ! getLatitudeField(60, 14, ils->loc_lat, sizeof(ils->loc_lat)) ) |
| 886 |
continue; |
| 887 |
if( ! getLongitudeField(85, 14, ils->loc_lon, sizeof(ils->loc_lon)) ) |
| 888 |
continue; |
| 889 |
|
| 890 |
getField(126, 7, ils->loc_elev, sizeof(ils->loc_elev)); |
| 891 |
if( ! isSignedDecimal(ils->loc_elev) ){ |
| 892 |
fprintf(stderr, "%s:%d: invalid localizer elevation '%s' for ILS %s -- assuming zero\n", |
| 893 |
in_filename, line_no, ils->loc_elev, ils->id); |
| 894 |
strcpy(ils->loc_elev, "0"); |
| 895 |
} |
| 896 |
|
| 897 |
getField(133, 7, ils->loc_freq, sizeof(ils->loc_freq)); |
| 898 |
if( ! isDecimal(ils->loc_freq) ){ |
| 899 |
fprintf(stderr, "%s:%d: invalid frequency '%s' for ILS %s -- assuming zero\n", |
| 900 |
in_filename, line_no, ils->loc_freq, ils->id); |
| 901 |
strcpy(ils->loc_freq, "0"); |
| 902 |
} |
| 903 |
|
| 904 |
getField(155, 5, ils->loc_width, sizeof(ils->loc_width)); |
| 905 |
if( ! isDecimal(ils->loc_width) ){ |
| 906 |
if( *(ils->loc_width) != 0 ) |
| 907 |
fprintf(stderr, "%s:%d: invalid beam width '%s' for ILS %s -- assuming 5.0\n", |
| 908 |
in_filename, line_no, ils->loc_width, ils->id); |
| 909 |
strcpy(ils->loc_width, "5.0"); |
| 910 |
} |
| 911 |
|
| 912 |
} else if( memory_startsWith(line, "ILS3") ){ // GS |
| 913 |
ILS *ils = parseILSKey(); |
| 914 |
if( ils == NULL ) |
| 915 |
continue; |
| 916 |
ils->gs_line_no = line_no; |
| 917 |
getField(28, 22, ils->gs_status, sizeof(ils->gs_status)); |
| 918 |
if( ! getLatitudeField(60, 14, ils->gs_lat, sizeof(ils->gs_lat)) ) |
| 919 |
continue; |
| 920 |
if( ! getLongitudeField(85, 14, ils->gs_lon, sizeof(ils->gs_lon)) ) |
| 921 |
continue; |
| 922 |
getField(126, 7, ils->gs_elev, sizeof(ils->gs_elev)); |
| 923 |
if( ! isSignedDecimal(ils->gs_elev) ){ |
| 924 |
fprintf(stderr, "%s:%d: invalid GS elevation '%s' for ILS %s -- assuming zero\n", |
| 925 |
in_filename, line_no, ils->gs_elev, ils->id); |
| 926 |
strcpy(ils->gs_elev, "0"); |
| 927 |
} |
| 928 |
getField(148, 5, ils->gs_angle, sizeof(ils->gs_angle)); |
| 929 |
|
| 930 |
} else if( memory_startsWith(line, "ILS4") ){ // DME |
| 931 |
ILS *ils = parseILSKey(); |
| 932 |
if( ils == NULL ) |
| 933 |
continue; |
| 934 |
getField(126, 7, ils->dme_elev, sizeof(ils->dme_elev)); |
| 935 |
if( ! isSignedDecimal(ils->dme_elev) ){ |
| 936 |
fprintf(stderr, "%s:%d: invalid DME elevation '%s' for ILS %s -- assuming zero\n", |
| 937 |
in_filename, line_no, ils->dme_elev, ils->id); |
| 938 |
strcpy(ils->dme_elev, "0"); |
| 939 |
} |
| 940 |
|
| 941 |
} else if( memory_startsWith(line, "ILS5") ){ |
| 942 |
} else if( memory_startsWith(line, "ILS6") ){ |
| 943 |
} else { |
| 944 |
fprintf(stderr, "%s:%d: not a valid record ILS1...6 line -- ignore\n", |
| 945 |
in_filename, line_no); |
| 946 |
} |
| 947 |
} |
| 948 |
fclose(in); |
| 949 |
|
| 950 |
// Step 2: generate the output by filtering the wanted ILS. |
| 951 |
ILSKey *key; |
| 952 |
for(key = hashtable_firstKey(ilss); key != NULL; key = hashtable_nextKey(ilss)){ |
| 953 |
|
| 954 |
ILS *ils = hashtable_get(ilss, key); |
| 955 |
|
| 956 |
if( ! memory_startsWith(ils->loc_status, "OPERATIONAL") ) |
| 957 |
continue; |
| 958 |
|
| 959 |
double lat; |
| 960 |
if( ! earth_parseLatitude(ils->loc_lat, &lat) ){ |
| 961 |
fprintf(stderr, "%s:%d: invalid latitude %s -- ignore\n", |
| 962 |
in_filename, ils->loc_line_no, ils->loc_lat); |
| 963 |
continue; |
| 964 |
} |
| 965 |
if( !(lat_min <= lat && lat < lat_max) ) |
| 966 |
continue; |
| 967 |
|
| 968 |
double lon; |
| 969 |
if( ! earth_parseLongitude(ils->loc_lon, &lon) ){ |
| 970 |
fprintf(stderr, "%s:%d: invalid longitude %s -- ignore\n", |
| 971 |
in_filename, ils->loc_line_no, ils->loc_lon); |
| 972 |
continue; |
| 973 |
} |
| 974 |
if( !(lon_min <= lon && lon < lon_max) ) |
| 975 |
continue; |
| 976 |
|
| 977 |
if( ! isILSFreq(ils->loc_freq) ){ |
| 978 |
fprintf(stderr, "%s:%d: %s %s: invalid frequency: %s -- ignore\n", |
| 979 |
in_filename, line_no, ils->key.type, ils->id, ils->loc_freq); |
| 980 |
continue; |
| 981 |
} |
| 982 |
|
| 983 |
/* |
| 984 |
* If this ILS claims to have an operational GS, its type must match |
| 985 |
* this fact, otherwise ACM would complain. Fix as needed. |
| 986 |
*/ |
| 987 |
int has_gs = memory_startsWith(ils->gs_status, "OPERATIONAL"); |
| 988 |
if( ! has_gs && (memory_startsWith(ils->key.type, "ILS") || strcmp(ils->key.type, "LOC/GS") == 0) ){ |
| 989 |
fprintf(stderr, "%s:%d: GS not operational for ILS %s -- ignore\n", |
| 990 |
in_filename, ils->loc_line_no, ils->id); |
| 991 |
continue; |
| 992 |
} |
| 993 |
|
| 994 |
// Remove leading "I-" from ID: |
| 995 |
char id[7]; |
| 996 |
memory_strcpy(id, sizeof(id), ils->id); |
| 997 |
if( memory_startsWith(id, "I-") ){ |
| 998 |
memmove(id + 1, id + 2, strlen(id) -1); |
| 999 |
} |
| 1000 |
|
| 1001 |
// Chose elevation (see implementation note 2): |
| 1002 |
char *elev_ptr; |
| 1003 |
if( ils->gs_elev[0] != 0 ) |
| 1004 |
elev_ptr = ils->gs_elev; |
| 1005 |
else if( ils->dme_elev[0] != 0 ) |
| 1006 |
elev_ptr = ils->dme_elev; |
| 1007 |
else |
| 1008 |
elev_ptr = ils->loc_elev; |
| 1009 |
if( *elev_ptr == 0 ){ |
| 1010 |
fprintf(stderr, "%s: no elevation available for ILS %s -- using zero\n", in_filename, ils->id); |
| 1011 |
elev_ptr = "0"; |
| 1012 |
} |
| 1013 |
char elev[9]; |
| 1014 |
snprintf(elev, sizeof(elev), "%.0f", atof(elev_ptr)); |
| 1015 |
|
| 1016 |
// Compute the actual true bearing and compare with the nominal true |
| 1017 |
// bearing and decide which one to report (see implementation note 1). |
| 1018 |
double true_bearing = ils->nominal_true_bearing; |
| 1019 |
RunwayEnd rwyendkey; |
| 1020 |
memory_strcpy(rwyendkey.pk, sizeof(rwyendkey.pk), ils->airport_pk); |
| 1021 |
memory_strcpy(rwyendkey.id, sizeof(rwyendkey.id), ils->key.runwayEndId); |
| 1022 |
RunwayEnd *rwyend = hashtable_get(runwayends, &rwyendkey); |
| 1023 |
if( rwyend == NULL ){ |
| 1024 |
fprintf(stderr, "%s:%d: Warning: ILS %s, no runway end found for airport pk %s and runway end ID %s. Cannot calculate actual true bearing of the locator, using nominal.", |
| 1025 |
in_filename, ils->loc_line_no, ils->id, ils->airport_pk, ils->key.runwayEndId); |
| 1026 |
} else { |
| 1027 |
|
| 1028 |
int precision_approach = !( memory_startsWith(ils->key.type, "LDA") |
| 1029 |
|| memory_startsWith(ils->key.type, "SDF") ); |
| 1030 |
|
| 1031 |
// Compute actual geographic bearing: |
| 1032 |
double actual_true_bearing = trueBearing(rwyend->lat, rwyend->lon, |
| 1033 |
lat, lon); |
| 1034 |
double delta = fabs( actual_true_bearing - true_bearing ); |
| 1035 |
if( delta > M_PI ) |
| 1036 |
delta = 2*M_PI - delta; |
| 1037 |
|
| 1038 |
// Which bearing to take? |
| 1039 |
if( delta < units_DEGtoRAD(1) ){ |
| 1040 |
// Very small misalignment. |
| 1041 |
if( precision_approach ) |
| 1042 |
true_bearing = actual_true_bearing; |
| 1043 |
|
| 1044 |
} else { |
| 1045 |
// Quite large misalignment. |
| 1046 |
if( precision_approach ) |
| 1047 |
fprintf(stderr, "%s:%d: Warning: ILS %s of type %s:\n" |
| 1048 |
"\t- nominal true bearing is %03.2f DEG,\n" |
| 1049 |
"\t- locator to runway end %s true bearing is %03.2f DEG,\n" |
| 1050 |
"\t- delta %.2f DEG.\n", |
| 1051 |
in_filename, ils->loc_line_no, ils->id, ils->key.type, |
| 1052 |
units_RADtoDEG(ils->nominal_true_bearing), |
| 1053 |
rwyend->id, |
| 1054 |
units_RADtoDEG(actual_true_bearing), |
| 1055 |
units_RADtoDEG(delta)); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
printf("ILS %s %s %s %s %s %s %s %s %s %s %03.2f %s\n", |
| 1060 |
ils->key.runwayEndId, |
| 1061 |
ils->key.type, |
| 1062 |
id, |
| 1063 |
ils->loc_freq, |
| 1064 |
ils->loc_lat, |
| 1065 |
ils->loc_lon, |
| 1066 |
has_gs? ils->gs_lat : "-", |
| 1067 |
has_gs? ils->gs_lon : "-", |
| 1068 |
elev, |
| 1069 |
ils->loc_width, |
| 1070 |
units_RADtoDEG(true_bearing), |
| 1071 |
has_gs? ils->gs_angle : "-" |
| 1072 |
); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
// --------------------- NAV ---------------------------------- |
| 1079 |
|
| 1080 |
|
| 1081 |
/** Channel to frequency map. */ |
| 1082 |
static char *channels[][2] = { |
| 1083 |
{"17X", "108.00"}, {"17Y", "108.05"}, {"18X", "108.10"}, {"18Y", "108.15"}, |
| 1084 |
{"19X", "108.20"}, {"19Y", "108.25"}, {"20X", "108.30"}, {"20Y", "108.35"}, |
| 1085 |
{"21X", "108.40"}, {"21Y", "108.45"}, {"22X", "108.50"}, {"22Y", "108.55"}, |
| 1086 |
{"23X", "108.60"}, {"23Y", "108.65"}, {"24X", "108.70"}, {"24Y", "108.75"}, |
| 1087 |
{"25X", "108.80"}, {"25Y", "108.85"}, {"26X", "108.90"}, {"26Y", "108.95"}, |
| 1088 |
{"27X", "109.00"}, {"27Y", "109.05"}, {"28X", "109.10"}, {"28Y", "109.15"}, |
| 1089 |
{"29X", "109.20"}, {"29Y", "109.25"}, {"30X", "109.30"}, {"30Y", "109.35"}, |
| 1090 |
{"31X", "109.40"}, {"31Y", "109.45"}, {"32X", "109.50"}, {"32Y", "109.55"}, |
| 1091 |
{"33X", "109.60"}, {"33Y", "109.65"}, {"34X", "109.70"}, {"34Y", "109.75"}, |
| 1092 |
{"35X", "109.80"}, {"35Y", "109.85"}, {"36X", "109.90"}, {"36Y", "109.95"}, |
| 1093 |
{"37X", "110.00"}, {"37Y", "110.05"}, {"38X", "110.10"}, {"38Y", "110.15"}, |
| 1094 |
{"39X", "110.20"}, {"39Y", "110.25"}, {"40X", "110.30"}, {"40Y", "110.35"}, |
| 1095 |
{"41X", "110.40"}, {"41Y", "110.45"}, {"42X", "110.50"}, {"42Y", "110.55"}, |
| 1096 |
{"43X", "110.60"}, {"43Y", "110.65"}, {"44X", "110.70"}, {"44Y", "110.75"}, |
| 1097 |
{"45X", "110.80"}, {"45Y", "110.85"}, {"46X", "110.90"}, {"46Y", "110.95"}, |
| 1098 |
{"47X", "111.00"}, {"47Y", "111.05"}, {"48X", "111.10"}, {"48Y", "111.15"}, |
| 1099 |
{"49X", "111.20"}, {"49Y", "111.25"}, {"50X", "111.30"}, {"50Y", "111.35"}, |
| 1100 |
{"51X", "111.40"}, {"51Y", "111.45"}, {"52X", "111.50"}, {"52Y", "111.55"}, |
| 1101 |
{"53X", "111.60"}, {"53Y", "111.65"}, {"54X", "111.70"}, {"54Y", "111.75"}, |
| 1102 |
{"55X", "111.80"}, {"55Y", "111.85"}, {"56X", "111.90"}, {"56Y", "111.95"}, |
| 1103 |
{"57X", "112.00"}, {"57Y", "112.05"}, {"58X", "112.10"}, {"58Y", "112.15"}, |
| 1104 |
{"59X", "112.20"}, {"59Y", "112.25"}, {"70X", "112.30"}, {"70Y", "112.35"}, |
| 1105 |
{"71X", "112.40"}, {"71Y", "112.45"}, {"72X", "112.50"}, {"72Y", "112.55"}, |
| 1106 |
{"73X", "112.60"}, {"73Y", "112.65"}, {"74X", "112.70"}, {"74Y", "112.75"}, |
| 1107 |
{"75X", "112.80"}, {"75Y", "112.85"}, {"76X", "112.90"}, {"76Y", "112.95"}, |
| 1108 |
{"77X", "113.00"}, {"77Y", "113.05"}, {"78X", "113.10"}, {"78Y", "113.15"}, |
| 1109 |
{"79X", "113.20"}, {"79Y", "113.25"}, {"80X", "113.30"}, {"80Y", "113.35"}, |
| 1110 |
{"81X", "113.40"}, {"81Y", "113.45"}, {"82X", "113.50"}, {"82Y", "113.55"}, |
| 1111 |
{"83X", "113.60"}, {"83Y", "113.65"}, {"84X", "113.70"}, {"84Y", "113.75"}, |
| 1112 |
{"85X", "113.80"}, {"85Y", "113.85"}, {"86X", "113.90"}, {"86Y", "113.95"}, |
| 1113 |
{"87X", "114.00"}, {"87Y", "114.05"}, {"88X", "114.10"}, {"88Y", "114.15"}, |
| 1114 |
{"89X", "114.20"}, {"89Y", "114.25"}, {"90X", "114.30"}, {"90Y", "114.35"}, |
| 1115 |
{"91X", "114.40"}, {"91Y", "114.45"}, {"92X", "114.50"}, {"92Y", "114.55"}, |
| 1116 |
{"93X", "114.60"}, {"93Y", "114.65"}, {"94X", "114.70"}, {"94Y", "114.75"}, |
| 1117 |
{"95X", "114.80"}, {"95Y", "114.85"}, {"96X", "114.90"}, {"96Y", "114.95"}, |
| 1118 |
{"97X", "115.00"}, {"97Y", "115.05"}, {"98X", "115.10"}, {"98Y", "115.15"}, |
| 1119 |
{"99X", "115.20"}, {"99Y", "115.25"}, {"100X", "115.30"}, {"100Y", "115.35"}, |
| 1120 |
{"101X", "115.40"}, {"101Y", "115.45"}, {"102X", "115.50"}, {"102Y", "115.55"}, |
| 1121 |
{"103X", "115.60"}, {"103Y", "115.65"}, {"104X", "115.70"}, {"104Y", "115.75"}, |
| 1122 |
{"105X", "115.80"}, {"105Y", "115.85"}, {"106X", "115.90"}, {"106Y", "115.95"}, |
| 1123 |
{"107X", "116.00"}, {"107Y", "116.05"}, {"108X", "116.10"}, {"108Y", "116.15"}, |
| 1124 |
{"109X", "116.20"}, {"109Y", "116.25"}, {"110X", "116.30"}, {"110Y", "116.35"}, |
| 1125 |
{"111X", "116.40"}, {"111Y", "116.45"}, {"112X", "116.50"}, {"112Y", "116.55"}, |
| 1126 |
{"113X", "116.60"}, {"113Y", "116.65"}, {"114X", "116.70"}, {"114Y", "116.75"}, |
| 1127 |
{"115X", "116.80"}, {"115Y", "116.85"}, {"116X", "116.90"}, {"116Y", "116.95"}, |
| 1128 |
{"117X", "117.00"}, {"117Y", "117.05"}, {"118X", "117.10"}, {"118Y", "117.15"}, |
| 1129 |
{"119X", "117.20"}, {"119Y", "117.25"}, {"120X", "117.30"}, {"120Y", "117.35"}, |
| 1130 |
{"121X", "117.40"}, {"121Y", "117.45"}, {"122X", "117.50"}, {"122Y", "117.55"}, |
| 1131 |
{"123X", "117.60"}, {"123Y", "117.65"}, {"124X", "117.70"}, {"124Y", "117.75"}, |
| 1132 |
{"125X", "117.80"}, {"125Y", "117.85"}, {"126X", "117.90"}, {"126Y", "117.95"}, |
| 1133 |
{NULL, NULL} |
| 1134 |
}; |
| 1135 |
|
| 1136 |
|
| 1137 |
static int mapChannelToFrequency(char *channel, char *freq, int freq_capacity) |
| 1138 |
{ |
| 1139 |
// Skip leading zeros: |
| 1140 |
while( *channel == '0' ) |
| 1141 |
channel++; |
| 1142 |
int i = 0; |
| 1143 |
while(channels[i][0] != NULL){ |
| 1144 |
if( strcmp(channels[i][0], channel) == 0 ){ |
| 1145 |
memory_strcpy(freq, freq_capacity, channels[i][1]); |
| 1146 |
return 1; |
| 1147 |
} |
| 1148 |
i++; |
| 1149 |
} |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
// VOR, VORTAC, DME. |
| 1155 |
static int isNAVFreq(char *s) |
| 1156 |
{ |
| 1157 |
match_Type m; |
| 1158 |
match_init(&m, s); |
| 1159 |
if( !(match_digits(&m, 1, 3) && (!match_char(&m, '.') || match_digits(&m, 1, 2)) && match_end(&m)) ) |
| 1160 |
return 0; |
| 1161 |
int freq = floor(atof(s) * 100 + 0.5); // tenth of KHz |
| 1162 |
return 10800 <= freq && freq <= 11795; |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
static int isNDBFreq(char *s) |
| 1167 |
{ |
| 1168 |
match_Type m; |
| 1169 |
match_init(&m, s); |
| 1170 |
if( !(match_digits(&m, 3, 3) && match_end(&m)) ) |
| 1171 |
return 0; |
| 1172 |
int freq = atoi(s); |
| 1173 |
return 200 <= freq && freq <= 529; |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
static void parseNAV(char *nav_filename) |
| 1178 |
{ |
| 1179 |
openFile(nav_filename); |
| 1180 |
while( readLine() ){ |
| 1181 |
if( ! memory_startsWith(line, "NAV1") ) |
| 1182 |
continue; |
| 1183 |
char type[21]; |
| 1184 |
getField(8, 20, type, sizeof(type)); |
| 1185 |
if( strcmp(type, "FAN MARKER") == 0 ) |
| 1186 |
// Unsure what they are, but no freq and no channel available; |
| 1187 |
// currently only 6 remaining, but it seems are going to be removed. |
| 1188 |
continue; |
| 1189 |
char id[5]; |
| 1190 |
getField(28, 4, id, sizeof(id)); |
| 1191 |
char lat[15]; |
| 1192 |
if( ! getLatitudeField(371, 14, lat, sizeof(lat)) ) |
| 1193 |
continue; |
| 1194 |
char lon[15]; |
| 1195 |
if( ! getLongitudeField(396, 14, lon, sizeof(lon)) ) |
| 1196 |
continue; |
| 1197 |
char elev[8]; |
| 1198 |
getField(472, 7, elev, sizeof(elev)); |
| 1199 |
char freq[7]; |
| 1200 |
getField(533, 6, freq, sizeof(freq)); |
| 1201 |
char channel[5]; |
| 1202 |
getField(529, 4, channel, sizeof(channel)); |
| 1203 |
|
| 1204 |
// True if have to add a dummy DME for some special type of NDB. |
| 1205 |
int add_dme = 0; |
| 1206 |
|
| 1207 |
if( strcmp(type, "VORTAC") == 0 |
| 1208 |
|| strcmp(type, "VOR/DME") == 0 |
| 1209 |
|| strcmp(type, "NDB") == 0 |
| 1210 |
|| strcmp(type, "TACAN") == 0 |
| 1211 |
|| strcmp(type, "VOR") == 0 |
| 1212 |
|| strcmp(type, "DME") == 0 |
| 1213 |
){ |
| 1214 |
// ok |
| 1215 |
|
| 1216 |
} else if( strcmp(type, "FAN MARKER") == 0 |
| 1217 |
|| strcmp(type, "MARINE NDB") == 0 |
| 1218 |
|| strcmp(type, "UHF/NDB") == 0){ |
| 1219 |
strcpy(type, "NDB"); |
| 1220 |
|
| 1221 |
} else if( strcmp(type, "CONSOLAN") == 0 |
| 1222 |
|| strcmp(type, "VOT") == 0 ){ |
| 1223 |
// FAA test facility. Ignore. |
| 1224 |
continue; |
| 1225 |
|
| 1226 |
} else if( strcmp(type, "MARINE NDB/DME") == 0 |
| 1227 |
|| strcmp(type, "NDB/DME") == 0 ){ |
| 1228 |
strcpy(type, "NDB"); |
| 1229 |
add_dme = 1; |
| 1230 |
|
| 1231 |
} else { |
| 1232 |
fprintf(stderr, "%s:%d: unknown type of NAVAID: %s -- ignore\n", |
| 1233 |
in_filename, line_no, type); |
| 1234 |
continue; |
| 1235 |
} |
| 1236 |
|
| 1237 |
double lat_rad; |
| 1238 |
if( ! earth_parseLatitude(lat, &lat_rad) ){ |
| 1239 |
fprintf(stderr, "%s:%d: invalid latitude -- ignore\n", in_filename, line_no); |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
if( !(lat_min <= lat_rad && lat_rad < lat_max) ) |
| 1243 |
continue; |
| 1244 |
|
| 1245 |
double lon_rad; |
| 1246 |
if( ! earth_parseLongitude(lon, &lon_rad) ){ |
| 1247 |
fprintf(stderr, "%s:%d: invalid longitude -- ignore\n", in_filename, line_no); |
| 1248 |
continue; |
| 1249 |
} |
| 1250 |
if( !(lon_min <= lon_rad && lon_rad < lon_max) ) |
| 1251 |
continue; |
| 1252 |
|
| 1253 |
// Elevation is missing on some NDB, but it does not care too much |
| 1254 |
// when there is no associated DME. |
| 1255 |
// Workaround: |
| 1256 |
if( elev[0] == 0 ){ |
| 1257 |
if( ! add_dme && strcmp(type, "NDB") == 0 ){ |
| 1258 |
strcpy(elev, "0"); |
| 1259 |
} else { |
| 1260 |
|
| 1261 |
//fprintf(stderr, "%s:%d: elevation field is empty -- ignore\n", |
| 1262 |
// in_filename, line_no); |
| 1263 |
//continue; |
| 1264 |
|
| 1265 |
// We currently do not support NDB/DME and report NDB only anyway, |
| 1266 |
// so continue: |
| 1267 |
strcpy(elev, "0"); |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
if( freq[0] == 0 ){ |
| 1272 |
if( channel[0] == 0 ){ |
| 1273 |
fprintf(stderr, "%s:%d: missing both frequency and channel -- ignore\n", in_filename, line_no); |
| 1274 |
continue; |
| 1275 |
} else { |
| 1276 |
if( ! mapChannelToFrequency(channel, freq, sizeof(freq)) ){ |
| 1277 |
fprintf(stderr, "%s:%d: invalid channel: %s -- ignore\n", |
| 1278 |
in_filename, line_no, channel); |
| 1279 |
continue; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
if( (memory_startsWith(type, "VOR") || memory_startsWith(type, "DME")) |
| 1285 |
&& ! isNAVFreq(freq) ){ |
| 1286 |
fprintf(stderr, "%s:%d: %s %s: invalid frequency: %s -- ignore\n", |
| 1287 |
in_filename, line_no, type, id, freq); |
| 1288 |
continue; |
| 1289 |
} |
| 1290 |
|
| 1291 |
if( strcmp(type, "NDB") == 0 && ! isNDBFreq(freq) ){ |
| 1292 |
fprintf(stderr, "%s:%d: %s %s: invalid frequency: %s -- ignore\n", |
| 1293 |
in_filename, line_no, type, id, freq); |
| 1294 |
continue; |
| 1295 |
} |
| 1296 |
|
| 1297 |
char elev_rounded[9]; |
| 1298 |
snprintf(elev_rounded, sizeof(elev_rounded), "%.0f", atof(elev)); |
| 1299 |
|
| 1300 |
printf("NAV %s %s %s %s %s %s %s\n", |
| 1301 |
id, type, lat, lon, elev_rounded, freq, |
| 1302 |
channel[0] == 0? "-" : channel); |
| 1303 |
// FIXME: unsure what to do with NDB+DME: they cannot share the same freq! |
| 1304 |
if( add_dme ) |
| 1305 |
fprintf(stderr, "%s:%d: Warning: unsupported DME associated to NDB -- reporting NDB only antenna instead.\n", |
| 1306 |
in_filename, line_no); |
| 1307 |
} |
| 1308 |
|
| 1309 |
fclose(in); |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
static void help() |
| 1314 |
{ |
| 1315 |
puts( |
| 1316 |
"This program parses the U.S. FAA data base in its textual format and generates\n" |
| 1317 |
"an ACM scenery. The FAA data base can be downloaded from\n" |
| 1318 |
"https://nfdc.faa.gov/xwiki/bin/view/NFDC\n" |
| 1319 |
"Download and decompress the APT.zip, ILS.zip and NAV.zip files in a folder,\n" |
| 1320 |
"then start this program with the following command line parameters:\n" |
| 1321 |
"\n" |
| 1322 |
" --lat-min DEG Minimum latitude, default 90S.\n" |
| 1323 |
" --lat-max DEG Maximum latitude, default 90N.\n" |
| 1324 |
" --lon-min DEG Minimum longitude, default 180W.\n" |
| 1325 |
" --lon-max DEG maximum longitude, default 180E.\n" |
| 1326 |
" --src-dir PATH Path to the directory of the decompressed FAA data base,\n" |
| 1327 |
" default . (current directory).\n" |
| 1328 |
"\n" |
| 1329 |
"The program collects all the runways, ILS and NAVAID in the given range of\n" |
| 1330 |
"latitude and longitude and writes the scenery on standard output; errors and\n" |
| 1331 |
"warnings are sent to standard error. Only the RWY, RWY2, ILS and NAV records\n" |
| 1332 |
"are generated. You may want to add the TEAM1_LOC, TEAM2_LOC and GROUND_COLOR\n" |
| 1333 |
"records to complete the scenery.\n" |
| 1334 |
); |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
int main(int argc, char** argv) |
| 1339 |
{ |
| 1340 |
error_init(argv[0]); |
| 1341 |
char *src_dir = "."; |
| 1342 |
|
| 1343 |
int i = 1; |
| 1344 |
while( i < argc ){ |
| 1345 |
char *a = argv[i]; |
| 1346 |
char *v = i < argc? argv[i+1] : NULL; |
| 1347 |
if( strcmp(a, "--help") == 0 || strcmp(a, "-h") == 0 ){ |
| 1348 |
help(); |
| 1349 |
return 0; |
| 1350 |
} else if( strcmp(a, "--lat-min") == 0 && v != NULL ){ |
| 1351 |
if( ! earth_parseLatitude(v, &lat_min) ) |
| 1352 |
error_external("invalid latitude: %s", v); |
| 1353 |
i += 2; |
| 1354 |
} else if( strcmp(a, "--lat-max") == 0 && v != NULL ){ |
| 1355 |
if( ! earth_parseLatitude(v, &lat_max) ) |
| 1356 |
error_external("invalid latitude: %s", v); |
| 1357 |
i += 2; |
| 1358 |
} else if( strcmp(a, "--lon-min") == 0 && v != NULL ){ |
| 1359 |
if( ! earth_parseLongitude(v, &lon_min) ) |
| 1360 |
error_external("invalid longitude: %s", v); |
| 1361 |
i += 2; |
| 1362 |
} else if( strcmp(a, "--lon-max") == 0 && v != NULL ){ |
| 1363 |
if( ! earth_parseLongitude(v, &lon_max) ) |
| 1364 |
error_external("invalid longitude: %s", v); |
| 1365 |
i += 2; |
| 1366 |
} else if( strcmp(a, "--src-dir") == 0 && v != NULL ){ |
| 1367 |
src_dir = v; |
| 1368 |
i += 2; |
| 1369 |
} else { |
| 1370 |
error_external("unknown option/value: %s", a); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
if( !(lat_min < lat_max && lon_min < lon_max) ) |
| 1375 |
error_external("invalid latitude [%g,%g[ DEG or longitude [%g,%g[ DEG ranges", units_RADtoDEG(lat_min), units_RADtoDEG(lat_max), units_RADtoDEG(lon_min), units_RADtoDEG(lon_max)); |
| 1376 |
|
| 1377 |
char path[999]; |
| 1378 |
|
| 1379 |
snprintf(path, sizeof(path), "%s/APT.txt", src_dir); |
| 1380 |
parseAPT(path); |
| 1381 |
|
| 1382 |
snprintf(path, sizeof(path), "%s/ILS.txt", src_dir); |
| 1383 |
parseILS(path); |
| 1384 |
|
| 1385 |
snprintf(path, sizeof(path), "%s/NAV.txt", src_dir); |
| 1386 |
parseNAV(path); |
| 1387 |
} |