1 | /* datain.c |
---|
2 | * Reads in survey files, dealing with special characters, keywords & data |
---|
3 | * Copyright (C) 1991-2024 Olly Betts |
---|
4 | * Copyright (C) 2004 Simeon Warner |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifdef HAVE_CONFIG_H |
---|
22 | #include <config.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <limits.h> |
---|
26 | #include <stdarg.h> |
---|
27 | |
---|
28 | #include "debug.h" |
---|
29 | #include "cavern.h" |
---|
30 | #include "date.h" |
---|
31 | #include "img.h" |
---|
32 | #include "filename.h" |
---|
33 | #include "message.h" |
---|
34 | #include "filelist.h" |
---|
35 | #include "netbits.h" |
---|
36 | #include "netskel.h" |
---|
37 | #include "readval.h" |
---|
38 | #include "datain.h" |
---|
39 | #include "commands.h" |
---|
40 | #include "out.h" |
---|
41 | #include "str.h" |
---|
42 | #include "thgeomag.h" |
---|
43 | #if PROJ_VERSION_MAJOR < 8 || \ |
---|
44 | (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR < 2) |
---|
45 | /* Needed for proj_factors workaround */ |
---|
46 | # include <proj_experimental.h> |
---|
47 | #endif |
---|
48 | |
---|
49 | #define EPSILON (REAL_EPSILON * 1000) |
---|
50 | |
---|
51 | #define var(I) (pcs->Var[(I)]) |
---|
52 | |
---|
53 | /* Test for a not-a-number value in Compass data (999.0 or -999.0). |
---|
54 | * |
---|
55 | * Compass itself uses -999.0 but reportedly understands Karst data which used |
---|
56 | * 999.0 (information from Larry Fish via Simeon Warner in 2004). However |
---|
57 | * testing with Compass in early 2024 it seems 999.0 is treated like any other |
---|
58 | * reading. |
---|
59 | * |
---|
60 | * When "corrected" backsights are specified in FORMAT, Compass seems to write |
---|
61 | * out -999 with the correction applied to the CLP file. |
---|
62 | * |
---|
63 | * Valid readings should be 0 to 360 for the compass and -90 to 90 for the |
---|
64 | * clino, and the correction should have absolute value < 360, so we test for |
---|
65 | * any reading with an absolute value greater than 999 - 360 = 639, which is |
---|
66 | * well outside the valid range. |
---|
67 | */ |
---|
68 | #define is_compass_NaN(x) (fabs(x) > (999.0 - 360.0)) |
---|
69 | |
---|
70 | static int |
---|
71 | read_compass_date_as_days_since_1900(void) |
---|
72 | { |
---|
73 | /* NB order is *month* *day* year */ |
---|
74 | int month = read_uint(); |
---|
75 | int day = read_uint(); |
---|
76 | int year = read_uint(); |
---|
77 | /* Note: Larry says a 2 digit year is always 19XX */ |
---|
78 | if (year < 100) year += 1900; |
---|
79 | |
---|
80 | /* Compass uses 1901-01-01 when no date was specified. */ |
---|
81 | if (year == 1901 && day == 1 && month == 1) return -1; |
---|
82 | |
---|
83 | return days_since_1900(year, month, day); |
---|
84 | } |
---|
85 | int ch; |
---|
86 | |
---|
87 | typedef enum { |
---|
88 | // Clino omitted. VAL() should be set to 0.0. |
---|
89 | CTYPE_OMIT, |
---|
90 | // An actual clino reading. |
---|
91 | CTYPE_READING, |
---|
92 | // An explicit plumb (U/D/UP/DOWN/+V/-V for reading). |
---|
93 | CTYPE_PLUMB, |
---|
94 | // An inferred plumb (+90 or -90 and *infer plumbs). |
---|
95 | CTYPE_INFERPLUMB, |
---|
96 | // An explicit horizontal leg (H/LEVEL for reading). |
---|
97 | CTYPE_HORIZ |
---|
98 | } clino_type; |
---|
99 | |
---|
100 | /* Don't explicitly initialise as we can't set the jmp_buf - this has |
---|
101 | * static scope so will be initialised like this anyway */ |
---|
102 | parse file /* = { NULL, NULL, 0, false, NULL } */ ; |
---|
103 | |
---|
104 | bool f_export_ok; |
---|
105 | |
---|
106 | static real value[Fr - 1]; |
---|
107 | #define VAL(N) value[(N)-1] |
---|
108 | static real variance[Fr - 1]; |
---|
109 | #define VAR(N) variance[(N)-1] |
---|
110 | static long location[Fr - 1]; |
---|
111 | #define LOC(N) location[(N)-1] |
---|
112 | static int location_width[Fr - 1]; |
---|
113 | #define WID(N) location_width[(N)-1] |
---|
114 | |
---|
115 | /* style functions */ |
---|
116 | static void data_normal(void); |
---|
117 | static void data_cartesian(void); |
---|
118 | static void data_passage(void); |
---|
119 | static void data_nosurvey(void); |
---|
120 | static void data_ignore(void); |
---|
121 | |
---|
122 | void |
---|
123 | get_pos(filepos *fp) |
---|
124 | { |
---|
125 | fp->ch = ch; |
---|
126 | fp->offset = ftell(file.fh); |
---|
127 | if (fp->offset == -1) |
---|
128 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
129 | } |
---|
130 | |
---|
131 | void |
---|
132 | set_pos(const filepos *fp) |
---|
133 | { |
---|
134 | ch = fp->ch; |
---|
135 | if (fseek(file.fh, fp->offset, SEEK_SET) == -1) |
---|
136 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
137 | } |
---|
138 | |
---|
139 | static void |
---|
140 | report_parent(parse * p) { |
---|
141 | if (p->parent) |
---|
142 | report_parent(p->parent); |
---|
143 | /* Force re-report of include tree for further errors in |
---|
144 | * parent files */ |
---|
145 | p->reported_where = false; |
---|
146 | /* TRANSLATORS: %s is replaced by the filename of the parent file, and %u |
---|
147 | * by the line number in that file. Your translation should also contain |
---|
148 | * %s:%u so that automatic parsing of error messages to determine the file |
---|
149 | * and line number still works. */ |
---|
150 | fprintf(STDERR, msg(/*In file included from %s:%u:\n*/5), p->filename, p->line); |
---|
151 | } |
---|
152 | |
---|
153 | static void |
---|
154 | error_list_parent_files(void) |
---|
155 | { |
---|
156 | if (!file.reported_where && file.parent) { |
---|
157 | report_parent(file.parent); |
---|
158 | /* Suppress reporting of full include tree for further errors |
---|
159 | * in this file */ |
---|
160 | file.reported_where = true; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | static void |
---|
165 | show_line(int col, int width) |
---|
166 | { |
---|
167 | /* Rewind to beginning of line. */ |
---|
168 | long cur_pos = ftell(file.fh); |
---|
169 | int tabs = 0; |
---|
170 | if (cur_pos < 0 || fseek(file.fh, file.lpos, SEEK_SET) == -1) |
---|
171 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
172 | |
---|
173 | /* Read the whole line and write it out. */ |
---|
174 | PUTC(' ', STDERR); |
---|
175 | while (1) { |
---|
176 | int c = GETC(file.fh); |
---|
177 | /* Note: isEol() is true for EOF */ |
---|
178 | if (isEol(c)) break; |
---|
179 | if (c == '\t') ++tabs; |
---|
180 | PUTC(c, STDERR); |
---|
181 | } |
---|
182 | fputnl(STDERR); |
---|
183 | |
---|
184 | /* If we have a location in the line for the error, indicate it. */ |
---|
185 | if (col) { |
---|
186 | PUTC(' ', STDERR); |
---|
187 | if (tabs == 0) { |
---|
188 | while (--col) PUTC(' ', STDERR); |
---|
189 | } else { |
---|
190 | /* Copy tabs from line, replacing other characters with spaces - this |
---|
191 | * means that the caret should line up correctly. */ |
---|
192 | if (fseek(file.fh, file.lpos, SEEK_SET) == -1) |
---|
193 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
194 | while (--col) { |
---|
195 | int c = GETC(file.fh); |
---|
196 | if (c != '\t') c = ' '; |
---|
197 | PUTC(c, STDERR); |
---|
198 | } |
---|
199 | } |
---|
200 | PUTC('^', STDERR); |
---|
201 | while (width > 1) { |
---|
202 | PUTC('~', STDERR); |
---|
203 | --width; |
---|
204 | } |
---|
205 | fputnl(STDERR); |
---|
206 | } |
---|
207 | |
---|
208 | /* Revert to where we were. */ |
---|
209 | if (fseek(file.fh, cur_pos, SEEK_SET) == -1) |
---|
210 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
211 | } |
---|
212 | |
---|
213 | char* |
---|
214 | grab_line(void) |
---|
215 | { |
---|
216 | /* Rewind to beginning of line. */ |
---|
217 | long cur_pos = ftell(file.fh); |
---|
218 | string p = S_INIT; |
---|
219 | if (cur_pos < 0 || fseek(file.fh, file.lpos, SEEK_SET) == -1) |
---|
220 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
221 | |
---|
222 | /* Read the whole line into a string. */ |
---|
223 | while (1) { |
---|
224 | int c = GETC(file.fh); |
---|
225 | /* Note: isEol() is true for EOF */ |
---|
226 | if (isEol(c)) break; |
---|
227 | s_catchar(&p, c); |
---|
228 | } |
---|
229 | |
---|
230 | /* Revert to where we were. */ |
---|
231 | if (fseek(file.fh, cur_pos, SEEK_SET) == -1) { |
---|
232 | s_free(&p); |
---|
233 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
234 | } |
---|
235 | |
---|
236 | return s_steal(&p); |
---|
237 | } |
---|
238 | |
---|
239 | static int caret_width = 0; |
---|
240 | |
---|
241 | static void |
---|
242 | compile_v_report_fpos(int severity, long fpos, int en, va_list ap) |
---|
243 | { |
---|
244 | int col = 0; |
---|
245 | error_list_parent_files(); |
---|
246 | if (fpos >= file.lpos) |
---|
247 | col = fpos - file.lpos - caret_width; |
---|
248 | v_report(severity, file.filename, file.line, col, en, ap); |
---|
249 | if (file.fh) show_line(col, caret_width); |
---|
250 | } |
---|
251 | |
---|
252 | static void |
---|
253 | compile_v_report(int diag_flags, int en, va_list ap) |
---|
254 | { |
---|
255 | int severity = (diag_flags & DIAG_SEVERITY_MASK); |
---|
256 | if (diag_flags & (DIAG_COL|DIAG_TOKEN)) { |
---|
257 | if (file.fh) { |
---|
258 | if (diag_flags & DIAG_TOKEN) caret_width = s_len(&token); |
---|
259 | compile_v_report_fpos(severity, ftell(file.fh), en, ap); |
---|
260 | if (diag_flags & DIAG_TOKEN) caret_width = 0; |
---|
261 | if (diag_flags & DIAG_SKIP) skipline(); |
---|
262 | return; |
---|
263 | } |
---|
264 | } |
---|
265 | error_list_parent_files(); |
---|
266 | v_report(severity, file.filename, file.line, 0, en, ap); |
---|
267 | if (file.fh) { |
---|
268 | if (diag_flags & DIAG_TOKEN) { |
---|
269 | show_line(0, s_len(&token)); |
---|
270 | } else { |
---|
271 | show_line(0, caret_width); |
---|
272 | } |
---|
273 | } |
---|
274 | if (diag_flags & DIAG_SKIP) skipline(); |
---|
275 | } |
---|
276 | |
---|
277 | void |
---|
278 | compile_diagnostic(int diag_flags, int en, ...) |
---|
279 | { |
---|
280 | va_list ap; |
---|
281 | va_start(ap, en); |
---|
282 | if (diag_flags & (DIAG_DATE|DIAG_NUM|DIAG_UINT|DIAG_WORD|DIAG_TAIL)) { |
---|
283 | int len = 0; |
---|
284 | skipblanks(); |
---|
285 | if (diag_flags & DIAG_WORD) { |
---|
286 | while (!isBlank(ch) && !isEol(ch)) { |
---|
287 | ++len; |
---|
288 | nextch(); |
---|
289 | } |
---|
290 | } else if (diag_flags & DIAG_UINT) { |
---|
291 | while (isdigit(ch)) { |
---|
292 | ++len; |
---|
293 | nextch(); |
---|
294 | } |
---|
295 | } else if (diag_flags & DIAG_DATE) { |
---|
296 | while (isdigit(ch) || ch == '.') { |
---|
297 | ++len; |
---|
298 | nextch(); |
---|
299 | } |
---|
300 | } else if (diag_flags & DIAG_TAIL) { |
---|
301 | int len_last_nonblank = len; |
---|
302 | while (!isComm(ch) && !isEol(ch)) { |
---|
303 | ++len; |
---|
304 | if (!isBlank(ch)) len_last_nonblank = len; |
---|
305 | nextch(); |
---|
306 | } |
---|
307 | len = len_last_nonblank; |
---|
308 | } else { |
---|
309 | if (isMinus(ch) || isPlus(ch)) { |
---|
310 | ++len; |
---|
311 | nextch(); |
---|
312 | } |
---|
313 | while (isdigit(ch)) { |
---|
314 | ++len; |
---|
315 | nextch(); |
---|
316 | } |
---|
317 | if (isDecimal(ch)) { |
---|
318 | ++len; |
---|
319 | nextch(); |
---|
320 | } |
---|
321 | while (isdigit(ch)) { |
---|
322 | ++len; |
---|
323 | nextch(); |
---|
324 | } |
---|
325 | } |
---|
326 | caret_width = len; |
---|
327 | compile_v_report(diag_flags|DIAG_COL, en, ap); |
---|
328 | caret_width = 0; |
---|
329 | } else if (diag_flags & DIAG_STRING) { |
---|
330 | string p = S_INIT; |
---|
331 | skipblanks(); |
---|
332 | caret_width = ftell(file.fh); |
---|
333 | read_string(&p); |
---|
334 | s_free(&p); |
---|
335 | /* We want to include any quotes, so can't use s_len(&p). */ |
---|
336 | caret_width = ftell(file.fh) - caret_width; |
---|
337 | compile_v_report(diag_flags|DIAG_COL, en, ap); |
---|
338 | caret_width = 0; |
---|
339 | } else { |
---|
340 | compile_v_report(diag_flags, en, ap); |
---|
341 | } |
---|
342 | va_end(ap); |
---|
343 | } |
---|
344 | |
---|
345 | static void |
---|
346 | compile_diagnostic_reading(int diag_flags, reading r, int en, ...) |
---|
347 | { |
---|
348 | va_list ap; |
---|
349 | int severity = (diag_flags & DIAG_SEVERITY_MASK); |
---|
350 | va_start(ap, en); |
---|
351 | caret_width = WID(r); |
---|
352 | compile_v_report_fpos(severity, LOC(r) + caret_width, en, ap); |
---|
353 | caret_width = 0; |
---|
354 | va_end(ap); |
---|
355 | } |
---|
356 | |
---|
357 | static void |
---|
358 | compile_error_reading_skip(reading r, int en, ...) |
---|
359 | { |
---|
360 | va_list ap; |
---|
361 | va_start(ap, en); |
---|
362 | caret_width = WID(r); |
---|
363 | compile_v_report_fpos(DIAG_ERR, LOC(r) + caret_width, en, ap); |
---|
364 | caret_width = 0; |
---|
365 | va_end(ap); |
---|
366 | skipline(); |
---|
367 | } |
---|
368 | |
---|
369 | void |
---|
370 | compile_diagnostic_at(int diag_flags, const char * filename, unsigned line, int en, ...) |
---|
371 | { |
---|
372 | va_list ap; |
---|
373 | int severity = (diag_flags & DIAG_SEVERITY_MASK); |
---|
374 | va_start(ap, en); |
---|
375 | v_report(severity, filename, line, 0, en, ap); |
---|
376 | va_end(ap); |
---|
377 | } |
---|
378 | |
---|
379 | void |
---|
380 | compile_diagnostic_pfx(int diag_flags, const prefix * pfx, int en, ...) |
---|
381 | { |
---|
382 | va_list ap; |
---|
383 | int severity = (diag_flags & DIAG_SEVERITY_MASK); |
---|
384 | va_start(ap, en); |
---|
385 | v_report(severity, pfx->filename, pfx->line, 0, en, ap); |
---|
386 | va_end(ap); |
---|
387 | } |
---|
388 | |
---|
389 | void |
---|
390 | compile_diagnostic_token_show(int diag_flags, int en) |
---|
391 | { |
---|
392 | string p = S_INIT; |
---|
393 | skipblanks(); |
---|
394 | while (!isBlank(ch) && !isEol(ch)) { |
---|
395 | s_catchar(&p, (char)ch); |
---|
396 | nextch(); |
---|
397 | } |
---|
398 | if (!s_empty(&p)) { |
---|
399 | caret_width = s_len(&p); |
---|
400 | compile_diagnostic(diag_flags|DIAG_COL, en, p); |
---|
401 | caret_width = 0; |
---|
402 | s_free(&p); |
---|
403 | } else { |
---|
404 | compile_diagnostic(DIAG_ERR|DIAG_COL, en, ""); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | static void |
---|
409 | compile_error_string(const char * s, int en, ...) |
---|
410 | { |
---|
411 | va_list ap; |
---|
412 | va_start(ap, en); |
---|
413 | caret_width = strlen(s); |
---|
414 | compile_v_report(DIAG_ERR|DIAG_COL, en, ap); |
---|
415 | va_end(ap); |
---|
416 | caret_width = 0; |
---|
417 | } |
---|
418 | |
---|
419 | /* This function makes a note where to put output files */ |
---|
420 | static void |
---|
421 | using_data_file(const char *fnm) |
---|
422 | { |
---|
423 | if (!fnm_output_base) { |
---|
424 | /* was: fnm_output_base = base_from_fnm(fnm); */ |
---|
425 | fnm_output_base = baseleaf_from_fnm(fnm); |
---|
426 | } else if (fnm_output_base_is_dir) { |
---|
427 | /* --output pointed to directory so use the leaf basename in that dir */ |
---|
428 | char *lf, *p; |
---|
429 | lf = baseleaf_from_fnm(fnm); |
---|
430 | p = use_path(fnm_output_base, lf); |
---|
431 | osfree(lf); |
---|
432 | osfree(fnm_output_base); |
---|
433 | fnm_output_base = p; |
---|
434 | fnm_output_base_is_dir = 0; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | static void |
---|
439 | skipword(void) |
---|
440 | { |
---|
441 | while (!isBlank(ch) && !isEol(ch)) nextch(); |
---|
442 | } |
---|
443 | |
---|
444 | extern void |
---|
445 | skipblanks(void) |
---|
446 | { |
---|
447 | while (isBlank(ch)) nextch(); |
---|
448 | } |
---|
449 | |
---|
450 | extern void |
---|
451 | skipline(void) |
---|
452 | { |
---|
453 | while (!isEol(ch)) nextch(); |
---|
454 | } |
---|
455 | |
---|
456 | static void |
---|
457 | process_eol(void) |
---|
458 | { |
---|
459 | int eolchar; |
---|
460 | |
---|
461 | skipblanks(); |
---|
462 | |
---|
463 | if (!isEol(ch)) { |
---|
464 | if (!isComm(ch)) |
---|
465 | compile_diagnostic(DIAG_ERR|DIAG_TAIL, /*End of line not blank*/15); |
---|
466 | skipline(); |
---|
467 | } |
---|
468 | |
---|
469 | eolchar = ch; |
---|
470 | file.line++; |
---|
471 | /* skip any different eol characters so we get line counts correct on |
---|
472 | * DOS text files and similar, but don't count several adjacent blank |
---|
473 | * lines as one */ |
---|
474 | while (ch != EOF) { |
---|
475 | nextch(); |
---|
476 | if (ch == eolchar || !isEol(ch)) { |
---|
477 | break; |
---|
478 | } |
---|
479 | if (ch == '\n') eolchar = ch; |
---|
480 | } |
---|
481 | file.lpos = ftell(file.fh) - 1; |
---|
482 | } |
---|
483 | |
---|
484 | static bool |
---|
485 | process_non_data_line(void) |
---|
486 | { |
---|
487 | skipblanks(); |
---|
488 | |
---|
489 | if (isData(ch)) return false; |
---|
490 | |
---|
491 | if (isKeywd(ch)) { |
---|
492 | nextch(); |
---|
493 | handle_command(); |
---|
494 | } |
---|
495 | |
---|
496 | process_eol(); |
---|
497 | |
---|
498 | return true; |
---|
499 | } |
---|
500 | |
---|
501 | static void |
---|
502 | read_reading(reading r, bool f_optional) |
---|
503 | { |
---|
504 | int n_readings; |
---|
505 | q_quantity q; |
---|
506 | switch (r) { |
---|
507 | case Tape: q = Q_LENGTH; break; |
---|
508 | case BackTape: q = Q_BACKLENGTH; break; |
---|
509 | case Comp: q = Q_BEARING; break; |
---|
510 | case BackComp: q = Q_BACKBEARING; break; |
---|
511 | case Clino: q = Q_GRADIENT; break; |
---|
512 | case BackClino: q = Q_BACKGRADIENT; break; |
---|
513 | case FrDepth: case ToDepth: q = Q_DEPTH; break; |
---|
514 | case Dx: q = Q_DX; break; |
---|
515 | case Dy: q = Q_DY; break; |
---|
516 | case Dz: q = Q_DZ; break; |
---|
517 | case FrCount: case ToCount: q = Q_COUNT; break; |
---|
518 | case Left: q = Q_LEFT; break; |
---|
519 | case Right: q = Q_RIGHT; break; |
---|
520 | case Up: q = Q_UP; break; |
---|
521 | case Down: q = Q_DOWN; break; |
---|
522 | default: |
---|
523 | q = Q_NULL; /* Suppress compiler warning */; |
---|
524 | BUG("Unexpected case"); |
---|
525 | } |
---|
526 | LOC(r) = ftell(file.fh); |
---|
527 | /* since we don't handle bearings in read_readings, it's never quadrant */ |
---|
528 | VAL(r) = read_numeric_multi(f_optional, false, &n_readings); |
---|
529 | WID(r) = ftell(file.fh) - LOC(r); |
---|
530 | VAR(r) = var(q); |
---|
531 | if (n_readings > 1) VAR(r) /= sqrt(n_readings); |
---|
532 | } |
---|
533 | |
---|
534 | static void |
---|
535 | read_bearing_or_omit(reading r) |
---|
536 | { |
---|
537 | int n_readings; |
---|
538 | bool quadrants = false; |
---|
539 | q_quantity q = Q_NULL; |
---|
540 | switch (r) { |
---|
541 | case Comp: |
---|
542 | q = Q_BEARING; |
---|
543 | if (pcs->f_bearing_quadrants) |
---|
544 | quadrants = true; |
---|
545 | break; |
---|
546 | case BackComp: |
---|
547 | q = Q_BACKBEARING; |
---|
548 | if (pcs->f_backbearing_quadrants) |
---|
549 | quadrants = true; |
---|
550 | break; |
---|
551 | default: |
---|
552 | q = Q_NULL; /* Suppress compiler warning */; |
---|
553 | BUG("Unexpected case"); |
---|
554 | } |
---|
555 | LOC(r) = ftell(file.fh); |
---|
556 | VAL(r) = read_bearing_multi_or_omit(quadrants, &n_readings); |
---|
557 | WID(r) = ftell(file.fh) - LOC(r); |
---|
558 | VAR(r) = var(q); |
---|
559 | if (n_readings > 1) VAR(r) /= sqrt(n_readings); |
---|
560 | } |
---|
561 | |
---|
562 | // Set up settings for reading Compass DAT or MAK. |
---|
563 | static void |
---|
564 | initialise_common_compass_settings(void) |
---|
565 | { |
---|
566 | short *t = ((short*)osmalloc(ossizeof(short) * 257)) + 1; |
---|
567 | int i; |
---|
568 | t[EOF] = SPECIAL_EOL; |
---|
569 | memset(t, 0, sizeof(short) * 33); |
---|
570 | for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES; |
---|
571 | t[127] = 0; |
---|
572 | for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES; |
---|
573 | t['\t'] |= SPECIAL_BLANK; |
---|
574 | t[' '] |= SPECIAL_BLANK; |
---|
575 | t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */ |
---|
576 | t['\n'] |= SPECIAL_EOL; |
---|
577 | t['\r'] |= SPECIAL_EOL; |
---|
578 | t['.'] |= SPECIAL_DECIMAL; |
---|
579 | t['-'] |= SPECIAL_MINUS; |
---|
580 | t['+'] |= SPECIAL_PLUS; |
---|
581 | |
---|
582 | settings *pcsNew = osnew(settings); |
---|
583 | *pcsNew = *pcs; /* copy contents */ |
---|
584 | pcsNew->begin_lineno = 0; |
---|
585 | pcsNew->Translate = t; |
---|
586 | pcsNew->Case = OFF; |
---|
587 | pcsNew->Truncate = INT_MAX; |
---|
588 | pcsNew->next = pcs; |
---|
589 | pcs = pcsNew; |
---|
590 | |
---|
591 | update_output_separator(); |
---|
592 | } |
---|
593 | |
---|
594 | /* For reading Compass MAK files which have a freeform syntax */ |
---|
595 | static void |
---|
596 | nextch_handling_eol(void) |
---|
597 | { |
---|
598 | nextch(); |
---|
599 | while (ch != EOF && isEol(ch)) { |
---|
600 | process_eol(); |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | static void |
---|
605 | data_file_compass_dat_or_clp(bool is_clp) |
---|
606 | { |
---|
607 | initialise_common_compass_settings(); |
---|
608 | default_units(pcs); |
---|
609 | default_calib(pcs); |
---|
610 | pcs->z[Q_DECLINATION] = HUGE_REAL; |
---|
611 | |
---|
612 | pcs->recorded_style = pcs->style = STYLE_NORMAL; |
---|
613 | pcs->units[Q_LENGTH] = METRES_PER_FOOT; |
---|
614 | pcs->infer = BIT(INFER_EQUATES) | |
---|
615 | BIT(INFER_EQUATES_SELF_OK) | |
---|
616 | BIT(INFER_EXPORTS) | |
---|
617 | BIT(INFER_PLUMBS); |
---|
618 | /* We need to update separator_map so we don't pick a separator character |
---|
619 | * which occurs in a station name. However Compass DAT allows everything |
---|
620 | * >= ASCII char 33 except 127 in station names so if we just added all |
---|
621 | * the valid station name characters we'd always pick space as the |
---|
622 | * separator for any dataset which included a DAT file, yet in practice |
---|
623 | * '.' is never used in any of the sample DAT files I've seen. So |
---|
624 | * instead we scan the characters actually used in station names when we |
---|
625 | * process CompassDATFr and CompassDATTo fields. |
---|
626 | */ |
---|
627 | |
---|
628 | #ifdef HAVE_SETJMP_H |
---|
629 | /* errors in nested functions can longjmp here */ |
---|
630 | if (setjmp(file.jbSkipLine)) { |
---|
631 | skipline(); |
---|
632 | process_eol(); |
---|
633 | } |
---|
634 | #endif |
---|
635 | |
---|
636 | while (ch != EOF && !ferror(file.fh)) { |
---|
637 | static const reading compass_order[] = { |
---|
638 | CompassDATFr, CompassDATTo, Tape, CompassDATComp, CompassDATClino, |
---|
639 | CompassDATLeft, CompassDATUp, CompassDATDown, CompassDATRight, |
---|
640 | CompassDATFlags, IgnoreAll |
---|
641 | }; |
---|
642 | static const reading compass_order_backsights[] = { |
---|
643 | CompassDATFr, CompassDATTo, Tape, CompassDATComp, CompassDATClino, |
---|
644 | CompassDATLeft, CompassDATUp, CompassDATDown, CompassDATRight, |
---|
645 | CompassDATBackComp, CompassDATBackClino, |
---|
646 | CompassDATFlags, IgnoreAll |
---|
647 | }; |
---|
648 | /* <Cave name> */ |
---|
649 | skipline(); |
---|
650 | process_eol(); |
---|
651 | /* SURVEY NAME: <Short name> */ |
---|
652 | get_token(); |
---|
653 | get_token(); |
---|
654 | /* if (ch != ':') ... */ |
---|
655 | nextch(); |
---|
656 | get_token(); |
---|
657 | skipline(); |
---|
658 | process_eol(); |
---|
659 | /* SURVEY DATE: 7 10 79 COMMENT:<Long name> */ |
---|
660 | get_token(); |
---|
661 | get_token(); |
---|
662 | copy_on_write_meta(pcs); |
---|
663 | if (ch == ':') { |
---|
664 | nextch(); |
---|
665 | int days = read_compass_date_as_days_since_1900(); |
---|
666 | pcs->meta->days1 = pcs->meta->days2 = days; |
---|
667 | } else { |
---|
668 | pcs->meta->days1 = pcs->meta->days2 = -1; |
---|
669 | } |
---|
670 | pcs->declination = HUGE_REAL; |
---|
671 | skipline(); |
---|
672 | process_eol(); |
---|
673 | /* SURVEY TEAM: */ |
---|
674 | get_token(); |
---|
675 | get_token(); |
---|
676 | skipline(); |
---|
677 | process_eol(); |
---|
678 | /* <Survey team> */ |
---|
679 | skipline(); |
---|
680 | process_eol(); |
---|
681 | /* DECLINATION: 1.00 FORMAT: DDDDLUDRADLN CORRECTIONS: 2.00 3.00 4.00 */ |
---|
682 | get_token(); |
---|
683 | nextch(); /* : */ |
---|
684 | skipblanks(); |
---|
685 | if (pcs->dec_filename == NULL) { |
---|
686 | pcs->z[Q_DECLINATION] = -read_numeric(false); |
---|
687 | pcs->z[Q_DECLINATION] *= pcs->units[Q_DECLINATION]; |
---|
688 | } else { |
---|
689 | (void)read_numeric(false); |
---|
690 | } |
---|
691 | get_token(); |
---|
692 | pcs->ordering = compass_order; |
---|
693 | if (S_EQ(&token, "FORMAT")) { |
---|
694 | /* This documents the format in the original survey notebook - we |
---|
695 | * don't need to fully parse it to be able to parse the survey data |
---|
696 | * in the file, which gets converted to a fixed order and units. |
---|
697 | */ |
---|
698 | nextch(); /* : */ |
---|
699 | get_token(); |
---|
700 | size_t token_len = s_len(&token); |
---|
701 | if (token_len >= 4 && s_str(&token)[3] == 'W') { |
---|
702 | /* Original "Inclination Units" were "Depth Gauge". */ |
---|
703 | pcs->recorded_style = STYLE_DIVING; |
---|
704 | } |
---|
705 | if (token_len >= 12) { |
---|
706 | char backsight_type = s_str(&token)[token_len >= 15 ? 13 : 11]; |
---|
707 | // B means redundant backsight; C means redundant backsights |
---|
708 | // but displayed "corrected" (i.e. reversed to make visually |
---|
709 | // comparing easier). |
---|
710 | if (backsight_type == 'B' || backsight_type == 'C') { |
---|
711 | /* We have backsights for compass and clino */ |
---|
712 | pcs->ordering = compass_order_backsights; |
---|
713 | } |
---|
714 | } |
---|
715 | get_token(); |
---|
716 | } |
---|
717 | |
---|
718 | // CORRECTIONS and CORRECTIONS2 have already been applied to data in |
---|
719 | // the CLP file. |
---|
720 | if (!is_clp) { |
---|
721 | if (S_EQ(&token, "CORRECTIONS") && ch == ':') { |
---|
722 | nextch(); /* : */ |
---|
723 | pcs->z[Q_BACKBEARING] = pcs->z[Q_BEARING] = -rad(read_numeric(false)); |
---|
724 | pcs->z[Q_BACKGRADIENT] = pcs->z[Q_GRADIENT] = -rad(read_numeric(false)); |
---|
725 | pcs->z[Q_LENGTH] = -METRES_PER_FOOT * read_numeric(false); |
---|
726 | get_token(); |
---|
727 | } |
---|
728 | |
---|
729 | /* get_token() only reads alphas so we must check for '2' here. */ |
---|
730 | if (S_EQ(&token, "CORRECTIONS") && ch == '2') { |
---|
731 | nextch(); /* 2 */ |
---|
732 | nextch(); /* : */ |
---|
733 | pcs->z[Q_BACKBEARING] = -rad(read_numeric(false)); |
---|
734 | pcs->z[Q_BACKGRADIENT] = -rad(read_numeric(false)); |
---|
735 | get_token(); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | #if 0 |
---|
740 | // FIXME Parse once we handle discovery dates... |
---|
741 | // NB: Need to skip unread CORRECTIONS* for the `is_clp` case. |
---|
742 | if (S_EQ(&token, "DISCOVERY") && ch == ':') { |
---|
743 | // Discovery date, e.g. DISCOVERY: 2 28 2024 |
---|
744 | nextch(); /* : */ |
---|
745 | int days = read_compass_date_as_days_since_1900(); |
---|
746 | } |
---|
747 | #endif |
---|
748 | skipline(); |
---|
749 | process_eol(); |
---|
750 | /* BLANK LINE */ |
---|
751 | skipline(); |
---|
752 | process_eol(); |
---|
753 | /* heading line */ |
---|
754 | skipline(); |
---|
755 | process_eol(); |
---|
756 | /* BLANK LINE */ |
---|
757 | skipline(); |
---|
758 | process_eol(); |
---|
759 | while (ch != EOF) { |
---|
760 | if (ch == '\x0c') { |
---|
761 | nextch(); |
---|
762 | process_eol(); |
---|
763 | break; |
---|
764 | } |
---|
765 | data_normal(); |
---|
766 | } |
---|
767 | clear_last_leg(); |
---|
768 | } |
---|
769 | pcs->ordering = NULL; /* Avoid free() of static array. */ |
---|
770 | pop_settings(); |
---|
771 | } |
---|
772 | |
---|
773 | static void |
---|
774 | data_file_compass_dat(void) |
---|
775 | { |
---|
776 | data_file_compass_dat_or_clp(false); |
---|
777 | } |
---|
778 | |
---|
779 | static void |
---|
780 | data_file_compass_clp(void) |
---|
781 | { |
---|
782 | data_file_compass_dat_or_clp(true); |
---|
783 | } |
---|
784 | |
---|
785 | static void |
---|
786 | data_file_compass_mak(void) |
---|
787 | { |
---|
788 | initialise_common_compass_settings(); |
---|
789 | short *t = pcs->Translate; |
---|
790 | // In a Compass MAK file a station name can't contain these three |
---|
791 | // characters due to how the syntax works. |
---|
792 | t['['] = t[','] = t[';'] = 0; |
---|
793 | |
---|
794 | #ifdef HAVE_SETJMP_H |
---|
795 | /* errors in nested functions can longjmp here */ |
---|
796 | if (setjmp(file.jbSkipLine)) { |
---|
797 | skipline(); |
---|
798 | process_eol(); |
---|
799 | } |
---|
800 | #endif |
---|
801 | |
---|
802 | int datum = 0; |
---|
803 | int utm_zone = 0; |
---|
804 | real base_x = 0.0, base_y = 0.0, base_z = 0.0; |
---|
805 | int base_utm_zone = 0; |
---|
806 | unsigned int base_line = 0; |
---|
807 | long base_lpos = 0; |
---|
808 | string path = S_INIT; |
---|
809 | s_donate(&path, path_from_fnm(file.filename)); |
---|
810 | struct mak_folder { |
---|
811 | struct mak_folder *next; |
---|
812 | int len; |
---|
813 | } *folder_stack = NULL; |
---|
814 | |
---|
815 | while (ch != EOF && !ferror(file.fh)) { |
---|
816 | switch (ch) { |
---|
817 | case '#': { |
---|
818 | /* include a file */ |
---|
819 | int ch_store; |
---|
820 | string dat_fnm = S_INIT; |
---|
821 | nextch_handling_eol(); |
---|
822 | while (ch != ',' && ch != ';' && ch != EOF) { |
---|
823 | while (isEol(ch)) process_eol(); |
---|
824 | s_catchar(&dat_fnm, (char)ch); |
---|
825 | nextch_handling_eol(); |
---|
826 | } |
---|
827 | if (!s_empty(&dat_fnm)) { |
---|
828 | if (base_utm_zone) { |
---|
829 | // Process the previous @ command using the datum from &. |
---|
830 | char *proj_str = img_compass_utm_proj_str(datum, |
---|
831 | base_utm_zone); |
---|
832 | if (proj_str) { |
---|
833 | // Temporarily reset line and lpos so dec_context and |
---|
834 | // dec_line refer to the @ command. |
---|
835 | unsigned saved_line = file.line; |
---|
836 | file.line = base_line; |
---|
837 | long saved_lpos = file.lpos; |
---|
838 | file.lpos = base_lpos; |
---|
839 | set_declination_location(base_x, base_y, base_z, |
---|
840 | proj_str); |
---|
841 | file.line = saved_line; |
---|
842 | file.lpos = saved_lpos; |
---|
843 | if (!pcs->proj_str) { |
---|
844 | pcs->proj_str = proj_str; |
---|
845 | if (!proj_str_out) { |
---|
846 | proj_str_out = osstrdup(proj_str); |
---|
847 | } |
---|
848 | } else { |
---|
849 | osfree(proj_str); |
---|
850 | } |
---|
851 | } |
---|
852 | } |
---|
853 | ch_store = ch; |
---|
854 | data_file(s_str(&path), s_str(&dat_fnm)); |
---|
855 | ch = ch_store; |
---|
856 | s_free(&dat_fnm); |
---|
857 | } |
---|
858 | while (ch != ';' && ch != EOF) { |
---|
859 | prefix *name; |
---|
860 | nextch_handling_eol(); |
---|
861 | filepos fp_name; |
---|
862 | get_pos(&fp_name); |
---|
863 | name = read_prefix(PFX_STATION|PFX_OPT); |
---|
864 | if (name) { |
---|
865 | scan_compass_station_name(name); |
---|
866 | skipblanks(); |
---|
867 | if (ch == '[') { |
---|
868 | /* fixed pt */ |
---|
869 | node *stn; |
---|
870 | real x, y, z; |
---|
871 | bool in_feet = false; |
---|
872 | // Compass treats these fixed points as entrances |
---|
873 | // ("distance from entrance" in a .DAT file counts |
---|
874 | // from 0.0 at these points) so we do too. |
---|
875 | name->sflags |= BIT(SFLAGS_FIXED) | |
---|
876 | BIT(SFLAGS_ENTRANCE); |
---|
877 | nextch_handling_eol(); |
---|
878 | if (ch == 'F' || ch == 'f') { |
---|
879 | in_feet = true; |
---|
880 | nextch_handling_eol(); |
---|
881 | } else if (ch == 'M' || ch == 'm') { |
---|
882 | nextch_handling_eol(); |
---|
883 | } else { |
---|
884 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting “F” or “M”*/103); |
---|
885 | } |
---|
886 | while (!isdigit(ch) && ch != '+' && ch != '-' && |
---|
887 | ch != '.' && ch != ']' && ch != EOF) { |
---|
888 | nextch_handling_eol(); |
---|
889 | } |
---|
890 | x = read_numeric(false); |
---|
891 | while (!isdigit(ch) && ch != '+' && ch != '-' && |
---|
892 | ch != '.' && ch != ']' && ch != EOF) { |
---|
893 | nextch_handling_eol(); |
---|
894 | } |
---|
895 | y = read_numeric(false); |
---|
896 | while (!isdigit(ch) && ch != '+' && ch != '-' && |
---|
897 | ch != '.' && ch != ']' && ch != EOF) { |
---|
898 | nextch_handling_eol(); |
---|
899 | } |
---|
900 | z = read_numeric(false); |
---|
901 | if (in_feet) { |
---|
902 | x *= METRES_PER_FOOT; |
---|
903 | y *= METRES_PER_FOOT; |
---|
904 | z *= METRES_PER_FOOT; |
---|
905 | } |
---|
906 | stn = StnFromPfx(name); |
---|
907 | if (!fixed(stn)) { |
---|
908 | POS(stn, 0) = x; |
---|
909 | POS(stn, 1) = y; |
---|
910 | POS(stn, 2) = z; |
---|
911 | fix(stn); |
---|
912 | } else { |
---|
913 | filepos fp; |
---|
914 | get_pos(&fp); |
---|
915 | set_pos(&fp_name); |
---|
916 | if (x != POS(stn, 0) || |
---|
917 | y != POS(stn, 1) || |
---|
918 | z != POS(stn, 2)) { |
---|
919 | compile_diagnostic(DIAG_ERR|DIAG_WORD, /*Station already fixed or equated to a fixed point*/46); |
---|
920 | } else { |
---|
921 | compile_diagnostic(DIAG_WARN|DIAG_WORD, /*Station already fixed at the same coordinates*/55); |
---|
922 | } |
---|
923 | set_pos(&fp); |
---|
924 | } |
---|
925 | while (ch != ']' && ch != EOF) nextch_handling_eol(); |
---|
926 | if (ch == ']') { |
---|
927 | nextch_handling_eol(); |
---|
928 | skipblanks(); |
---|
929 | } |
---|
930 | } else { |
---|
931 | /* FIXME: link station - ignore for now */ |
---|
932 | /* FIXME: perhaps issue warning? Other station names |
---|
933 | * can be "reused", which is problematic... */ |
---|
934 | } |
---|
935 | while (ch != ',' && ch != ';' && ch != EOF) |
---|
936 | nextch_handling_eol(); |
---|
937 | } |
---|
938 | } |
---|
939 | break; |
---|
940 | } |
---|
941 | case '$': |
---|
942 | /* UTM zone */ |
---|
943 | nextch(); |
---|
944 | skipblanks(); |
---|
945 | utm_zone = read_int(-60, 60); |
---|
946 | skipblanks(); |
---|
947 | if (ch == ';') nextch_handling_eol(); |
---|
948 | |
---|
949 | update_proj_str: |
---|
950 | if (!pcs->next || pcs->proj_str != pcs->next->proj_str) |
---|
951 | osfree(pcs->proj_str); |
---|
952 | pcs->proj_str = NULL; |
---|
953 | if (datum && utm_zone && abs(utm_zone) <= 60) { |
---|
954 | /* Set up coordinate system. */ |
---|
955 | char *proj_str = img_compass_utm_proj_str(datum, utm_zone); |
---|
956 | if (proj_str) { |
---|
957 | pcs->proj_str = proj_str; |
---|
958 | if (!proj_str_out) { |
---|
959 | proj_str_out = osstrdup(proj_str); |
---|
960 | } |
---|
961 | } |
---|
962 | } |
---|
963 | invalidate_pj_cached(); |
---|
964 | break; |
---|
965 | case '&': { |
---|
966 | /* Datum */ |
---|
967 | string p = S_INIT; |
---|
968 | int datum_len = 0; |
---|
969 | int c = 0; |
---|
970 | nextch(); |
---|
971 | skipblanks(); |
---|
972 | while (ch != ';' && !isEol(ch)) { |
---|
973 | s_catchar(&p, (char)ch); |
---|
974 | ++c; |
---|
975 | /* Ignore trailing blanks. */ |
---|
976 | if (!isBlank(ch)) datum_len = c; |
---|
977 | nextch(); |
---|
978 | } |
---|
979 | if (ch == ';') nextch_handling_eol(); |
---|
980 | datum = img_parse_compass_datum_string(s_str(&p), datum_len); |
---|
981 | s_free(&p); |
---|
982 | goto update_proj_str; |
---|
983 | } |
---|
984 | case '[': { |
---|
985 | // Enter subdirectory. |
---|
986 | struct mak_folder *p = folder_stack; |
---|
987 | folder_stack = osnew(struct mak_folder); |
---|
988 | folder_stack->next = p; |
---|
989 | folder_stack->len = s_len(&path); |
---|
990 | if (!s_empty(&path)) |
---|
991 | s_catchar(&path, FNM_SEP_LEV); |
---|
992 | nextch(); |
---|
993 | while (ch != ';' && !isEol(ch)) { |
---|
994 | if (ch == '\\') { |
---|
995 | ch = FNM_SEP_LEV; |
---|
996 | } |
---|
997 | s_catchar(&path, (char)ch); |
---|
998 | nextch(); |
---|
999 | } |
---|
1000 | if (ch == ';') nextch_handling_eol(); |
---|
1001 | break; |
---|
1002 | } |
---|
1003 | case ']': { |
---|
1004 | // Leave subdirectory. |
---|
1005 | struct mak_folder *p = folder_stack; |
---|
1006 | if (folder_stack == NULL) { |
---|
1007 | // FIXME: Error? Check what Compass does. |
---|
1008 | break; |
---|
1009 | } |
---|
1010 | s_truncate(&path, folder_stack->len); |
---|
1011 | folder_stack = folder_stack->next; |
---|
1012 | osfree(p); |
---|
1013 | nextch(); |
---|
1014 | skipblanks(); |
---|
1015 | if (ch == ';') nextch_handling_eol(); |
---|
1016 | break; |
---|
1017 | } |
---|
1018 | case '@': { |
---|
1019 | /* "Base Location" to calculate magnetic declination at: |
---|
1020 | * UTM East, UTM North, Elevation, UTM Zone, Convergence Angle |
---|
1021 | * The first three are in metres. |
---|
1022 | */ |
---|
1023 | nextch(); |
---|
1024 | real easting = read_numeric(false); |
---|
1025 | skipblanks(); |
---|
1026 | if (ch != ',') break; |
---|
1027 | nextch(); |
---|
1028 | real northing = read_numeric(false); |
---|
1029 | skipblanks(); |
---|
1030 | if (ch != ',') break; |
---|
1031 | nextch(); |
---|
1032 | real elevation = read_numeric(false); |
---|
1033 | skipblanks(); |
---|
1034 | if (ch != ',') break; |
---|
1035 | nextch(); |
---|
1036 | int zone = read_int(-60, 60); |
---|
1037 | skipblanks(); |
---|
1038 | if (ch != ',') break; |
---|
1039 | nextch(); |
---|
1040 | real convergence_angle = read_numeric(false); |
---|
1041 | /* We've now read them all successfully so store them. The |
---|
1042 | * Compass documentation gives an example which specifies the |
---|
1043 | * datum *AFTER* the base location, so we need to convert lazily. |
---|
1044 | */ |
---|
1045 | base_x = easting; |
---|
1046 | base_y = northing; |
---|
1047 | base_z = elevation; |
---|
1048 | base_utm_zone = zone; |
---|
1049 | base_line = file.line; |
---|
1050 | base_lpos = file.lpos; |
---|
1051 | // We ignore the stored UTM grid convergence angle since we get |
---|
1052 | // this from PROJ. |
---|
1053 | (void)convergence_angle; |
---|
1054 | if (ch == ';') nextch_handling_eol(); |
---|
1055 | break; |
---|
1056 | } |
---|
1057 | default: |
---|
1058 | nextch_handling_eol(); |
---|
1059 | break; |
---|
1060 | } |
---|
1061 | } |
---|
1062 | |
---|
1063 | while (folder_stack) { |
---|
1064 | // FIXME: Error? Check what Compass does. |
---|
1065 | struct mak_folder *next = folder_stack->next; |
---|
1066 | osfree(folder_stack); |
---|
1067 | folder_stack = next; |
---|
1068 | } |
---|
1069 | |
---|
1070 | pop_settings(); |
---|
1071 | s_free(&path); |
---|
1072 | } |
---|
1073 | |
---|
1074 | static void |
---|
1075 | data_file_survex(void) |
---|
1076 | { |
---|
1077 | int begin_lineno_store = pcs->begin_lineno; |
---|
1078 | pcs->begin_lineno = 0; |
---|
1079 | |
---|
1080 | if (ch == 0xef) { |
---|
1081 | /* Maybe a UTF-8 "BOM" - skip if so. */ |
---|
1082 | if (nextch() == 0xbb && nextch() == 0xbf) { |
---|
1083 | nextch(); |
---|
1084 | file.lpos = 3; |
---|
1085 | } else { |
---|
1086 | rewind(file.fh); |
---|
1087 | ch = 0xef; |
---|
1088 | } |
---|
1089 | } |
---|
1090 | |
---|
1091 | #ifdef HAVE_SETJMP_H |
---|
1092 | /* errors in nested functions can longjmp here */ |
---|
1093 | if (setjmp(file.jbSkipLine)) { |
---|
1094 | skipline(); |
---|
1095 | process_eol(); |
---|
1096 | } |
---|
1097 | #endif |
---|
1098 | |
---|
1099 | while (ch != EOF && !ferror(file.fh)) { |
---|
1100 | if (!process_non_data_line()) { |
---|
1101 | f_export_ok = false; |
---|
1102 | switch (pcs->style) { |
---|
1103 | case STYLE_NORMAL: |
---|
1104 | case STYLE_DIVING: |
---|
1105 | case STYLE_CYLPOLAR: |
---|
1106 | data_normal(); |
---|
1107 | break; |
---|
1108 | case STYLE_CARTESIAN: |
---|
1109 | data_cartesian(); |
---|
1110 | break; |
---|
1111 | case STYLE_PASSAGE: |
---|
1112 | data_passage(); |
---|
1113 | break; |
---|
1114 | case STYLE_NOSURVEY: |
---|
1115 | data_nosurvey(); |
---|
1116 | break; |
---|
1117 | case STYLE_IGNORE: |
---|
1118 | data_ignore(); |
---|
1119 | break; |
---|
1120 | default: |
---|
1121 | BUG("bad style"); |
---|
1122 | } |
---|
1123 | } |
---|
1124 | } |
---|
1125 | clear_last_leg(); |
---|
1126 | |
---|
1127 | /* don't allow *BEGIN at the end of a file, then *EXPORT in the |
---|
1128 | * including file */ |
---|
1129 | f_export_ok = false; |
---|
1130 | |
---|
1131 | if (pcs->begin_lineno) { |
---|
1132 | error_in_file(file.filename, pcs->begin_lineno, |
---|
1133 | /*BEGIN with no matching END in this file*/23); |
---|
1134 | /* Implicitly close any unclosed BEGINs from this file */ |
---|
1135 | do { |
---|
1136 | pop_settings(); |
---|
1137 | } while (pcs->begin_lineno); |
---|
1138 | } |
---|
1139 | |
---|
1140 | pcs->begin_lineno = begin_lineno_store; |
---|
1141 | } |
---|
1142 | |
---|
1143 | #define EXT3(C1, C2, C3) (((C3) << 16) | ((C2) << 8) | (C1)) |
---|
1144 | |
---|
1145 | extern void |
---|
1146 | data_file(const char *pth, const char *fnm) |
---|
1147 | { |
---|
1148 | parse file_store; |
---|
1149 | unsigned ext = 0; |
---|
1150 | |
---|
1151 | { |
---|
1152 | char *filename; |
---|
1153 | FILE *fh; |
---|
1154 | size_t len; |
---|
1155 | |
---|
1156 | if (!pth) { |
---|
1157 | /* file specified on command line - don't do special translation */ |
---|
1158 | fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename); |
---|
1159 | } else { |
---|
1160 | fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename); |
---|
1161 | } |
---|
1162 | |
---|
1163 | if (fh == NULL) { |
---|
1164 | compile_error_string(fnm, /*Couldn’t open file “%s”*/24, fnm); |
---|
1165 | return; |
---|
1166 | } |
---|
1167 | |
---|
1168 | len = strlen(filename); |
---|
1169 | if (len > 4 && filename[len - 4] == FNM_SEP_EXT) { |
---|
1170 | /* Read extension and pack into ext. */ |
---|
1171 | for (int i = 1; i < 4; ++i) { |
---|
1172 | unsigned char ext_ch = filename[len - i]; |
---|
1173 | ext = (ext << 8) | tolower(ext_ch); |
---|
1174 | } |
---|
1175 | } |
---|
1176 | |
---|
1177 | file_store = file; |
---|
1178 | if (file.fh) file.parent = &file_store; |
---|
1179 | file.fh = fh; |
---|
1180 | file.filename = filename; |
---|
1181 | file.line = 1; |
---|
1182 | file.lpos = 0; |
---|
1183 | file.reported_where = false; |
---|
1184 | nextch(); |
---|
1185 | } |
---|
1186 | |
---|
1187 | using_data_file(file.filename); |
---|
1188 | |
---|
1189 | switch (ext) { |
---|
1190 | case EXT3('d', 'a', 't'): |
---|
1191 | // Compass survey data. |
---|
1192 | data_file_compass_dat(); |
---|
1193 | break; |
---|
1194 | case EXT3('c', 'l', 'p'): |
---|
1195 | // Compass closed data. The format of .clp is the same as .dat, |
---|
1196 | // but it contains loop-closed data. This might be useful to |
---|
1197 | // read if you want to keep existing stations at the same |
---|
1198 | // adjusted positions, for example to be able to draw extensions |
---|
1199 | // on an existing drawn-up survey. Or if you managed to lose the |
---|
1200 | // original .dat but still have the .clp. |
---|
1201 | data_file_compass_clp(); |
---|
1202 | break; |
---|
1203 | case EXT3('m', 'a', 'k'): |
---|
1204 | // Compass project file. |
---|
1205 | data_file_compass_mak(); |
---|
1206 | break; |
---|
1207 | default: |
---|
1208 | // Native Survex data. |
---|
1209 | data_file_survex(); |
---|
1210 | break; |
---|
1211 | } |
---|
1212 | |
---|
1213 | if (ferror(file.fh)) |
---|
1214 | fatalerror_in_file(file.filename, 0, /*Error reading file*/18); |
---|
1215 | |
---|
1216 | (void)fclose(file.fh); |
---|
1217 | |
---|
1218 | file = file_store; |
---|
1219 | |
---|
1220 | /* don't free this - it may be pointed to by prefix.file */ |
---|
1221 | /* osfree(file.filename); */ |
---|
1222 | } |
---|
1223 | |
---|
1224 | static real |
---|
1225 | mod2pi(real a) |
---|
1226 | { |
---|
1227 | return a - floor(a / (2 * M_PI)) * (2 * M_PI); |
---|
1228 | } |
---|
1229 | |
---|
1230 | static real |
---|
1231 | handle_plumb(clino_type *p_ctype) |
---|
1232 | { |
---|
1233 | typedef enum { |
---|
1234 | CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL |
---|
1235 | } clino_tok; |
---|
1236 | static const sztok clino_tab[] = { |
---|
1237 | {"D", CLINO_DOWN}, |
---|
1238 | {"DOWN", CLINO_DOWN}, |
---|
1239 | {"H", CLINO_LEVEL}, |
---|
1240 | {"LEVEL", CLINO_LEVEL}, |
---|
1241 | {"U", CLINO_UP}, |
---|
1242 | {"UP", CLINO_UP}, |
---|
1243 | {NULL, CLINO_NULL} |
---|
1244 | }; |
---|
1245 | static const real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0}; |
---|
1246 | clino_tok tok; |
---|
1247 | |
---|
1248 | skipblanks(); |
---|
1249 | if (isalpha(ch)) { |
---|
1250 | filepos fp; |
---|
1251 | get_pos(&fp); |
---|
1252 | get_token(); |
---|
1253 | tok = match_tok(clino_tab, TABSIZE(clino_tab)); |
---|
1254 | if (tok != CLINO_NULL) { |
---|
1255 | *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB); |
---|
1256 | return clinos[tok]; |
---|
1257 | } |
---|
1258 | set_pos(&fp); |
---|
1259 | } else if (isSign(ch)) { |
---|
1260 | int chOld = ch; |
---|
1261 | nextch(); |
---|
1262 | if (toupper(ch) == 'V') { |
---|
1263 | nextch(); |
---|
1264 | *p_ctype = CTYPE_PLUMB; |
---|
1265 | return (!isMinus(chOld) ? M_PI_2 : -M_PI_2); |
---|
1266 | } |
---|
1267 | |
---|
1268 | if (isOmit(chOld)) { |
---|
1269 | *p_ctype = CTYPE_OMIT; |
---|
1270 | /* no clino reading, so assume 0 with large sd */ |
---|
1271 | return (real)0.0; |
---|
1272 | } |
---|
1273 | } else if (isOmit(ch)) { |
---|
1274 | /* OMIT char may not be a SIGN char too so we need to check here as |
---|
1275 | * well as above... */ |
---|
1276 | nextch(); |
---|
1277 | *p_ctype = CTYPE_OMIT; |
---|
1278 | /* no clino reading, so assume 0 with large sd */ |
---|
1279 | return (real)0.0; |
---|
1280 | } |
---|
1281 | return HUGE_REAL; |
---|
1282 | } |
---|
1283 | |
---|
1284 | static void |
---|
1285 | warn_readings_differ(int msgno, real diff, int units, |
---|
1286 | reading r_fore, reading r_back) |
---|
1287 | { |
---|
1288 | char buf[64]; |
---|
1289 | char *p; |
---|
1290 | diff /= get_units_factor(units); |
---|
1291 | snprintf(buf, sizeof(buf), "%.2f", fabs(diff)); |
---|
1292 | for (p = buf; *p; ++p) { |
---|
1293 | if (*p == '.') { |
---|
1294 | char *z = p; |
---|
1295 | while (*++p) { |
---|
1296 | if (*p != '0') z = p + 1; |
---|
1297 | } |
---|
1298 | p = z; |
---|
1299 | break; |
---|
1300 | } |
---|
1301 | } |
---|
1302 | strcpy(p, get_units_string(units)); |
---|
1303 | // FIXME: Highlight r_fore too. |
---|
1304 | (void)r_fore; |
---|
1305 | compile_diagnostic_reading(DIAG_WARN, r_back, msgno, buf); |
---|
1306 | } |
---|
1307 | |
---|
1308 | // If one (or both) compass readings are given, return Comp or BackComp |
---|
1309 | // so we can report plumb legs with compass readings. If neither are, |
---|
1310 | // return End. |
---|
1311 | static reading |
---|
1312 | handle_comp_units(void) |
---|
1313 | { |
---|
1314 | reading which_comp = End; |
---|
1315 | if (VAL(Comp) != HUGE_REAL) { |
---|
1316 | which_comp = Comp; |
---|
1317 | VAL(Comp) *= pcs->units[Q_BEARING]; |
---|
1318 | if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) { |
---|
1319 | /* TRANSLATORS: Suspicious means something like 410 degrees or -20 |
---|
1320 | * degrees */ |
---|
1321 | compile_diagnostic_reading(DIAG_WARN, Comp, /*Suspicious compass reading*/59); |
---|
1322 | VAL(Comp) = mod2pi(VAL(Comp)); |
---|
1323 | } |
---|
1324 | } |
---|
1325 | if (VAL(BackComp) != HUGE_REAL) { |
---|
1326 | if (which_comp == End) which_comp = BackComp; |
---|
1327 | VAL(BackComp) *= pcs->units[Q_BACKBEARING]; |
---|
1328 | if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) { |
---|
1329 | /* FIXME: different message for BackComp? */ |
---|
1330 | compile_diagnostic_reading(DIAG_WARN, BackComp, /*Suspicious compass reading*/59); |
---|
1331 | VAL(BackComp) = mod2pi(VAL(BackComp)); |
---|
1332 | } |
---|
1333 | } |
---|
1334 | return which_comp; |
---|
1335 | } |
---|
1336 | |
---|
1337 | static real compute_convergence(real lon, real lat) { |
---|
1338 | // PROJ < 8.1.0 dereferences the context without a NULL check inside |
---|
1339 | // proj_create_ellipsoidal_2D_cs() but PJ_DEFAULT_CTX is really just |
---|
1340 | // NULL so for affected PROJ versions we create a context temporarily to |
---|
1341 | // avoid a segmentation fault. |
---|
1342 | PJ_CONTEXT * ctx = PJ_DEFAULT_CTX; |
---|
1343 | #if PROJ_VERSION_MAJOR < 8 || \ |
---|
1344 | (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR < 1) |
---|
1345 | ctx = proj_context_create(); |
---|
1346 | #endif |
---|
1347 | |
---|
1348 | if (!proj_str_out) { |
---|
1349 | compile_diagnostic(DIAG_ERR, /*Output coordinate system not set*/488); |
---|
1350 | return 0.0; |
---|
1351 | } |
---|
1352 | PJ * pj = proj_create(ctx, proj_str_out); |
---|
1353 | PJ_COORD lp; |
---|
1354 | lp.lp.lam = lon; |
---|
1355 | lp.lp.phi = lat; |
---|
1356 | #if PROJ_VERSION_MAJOR < 8 || \ |
---|
1357 | (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR < 2) |
---|
1358 | /* Code adapted from fix in PROJ 8.2.0 to make proj_factors() work in |
---|
1359 | * cases we need (e.g. a CRS specified as "EPSG:<number>"). |
---|
1360 | */ |
---|
1361 | switch (proj_get_type(pj)) { |
---|
1362 | case PJ_TYPE_PROJECTED_CRS: { |
---|
1363 | /* If it is a projected CRS, then compute the factors on the conversion |
---|
1364 | * associated to it. We need to start from a temporary geographic CRS |
---|
1365 | * using the same datum as the one of the projected CRS, and with |
---|
1366 | * input coordinates being in longitude, latitude order in radian, |
---|
1367 | * to be consistent with the expectations of the lp input parameter. |
---|
1368 | */ |
---|
1369 | |
---|
1370 | PJ * geodetic_crs = proj_get_source_crs(ctx, pj); |
---|
1371 | if (!geodetic_crs) |
---|
1372 | break; |
---|
1373 | PJ * datum = proj_crs_get_datum(ctx, geodetic_crs); |
---|
1374 | #if PROJ_VERSION_MAJOR == 8 || \ |
---|
1375 | (PROJ_VERSION_MAJOR == 7 && PROJ_VERSION_MINOR >= 2) |
---|
1376 | /* PROJ 7.2.0 upgraded to EPSG 10.x which added the concept |
---|
1377 | * of a datum ensemble, and this version of PROJ also added |
---|
1378 | * an API to deal with these. |
---|
1379 | * |
---|
1380 | * If we're using PROJ < 7.2.0 then its EPSG database won't |
---|
1381 | * have datum ensembles, so we don't need any code to handle |
---|
1382 | * them. |
---|
1383 | */ |
---|
1384 | if (!datum) { |
---|
1385 | datum = proj_crs_get_datum_ensemble(ctx, geodetic_crs); |
---|
1386 | } |
---|
1387 | #endif |
---|
1388 | PJ * cs = proj_create_ellipsoidal_2D_cs( |
---|
1389 | ctx, PJ_ELLPS2D_LONGITUDE_LATITUDE, "Radian", 1.0); |
---|
1390 | PJ * temp = proj_create_geographic_crs_from_datum( |
---|
1391 | ctx, "unnamed crs", datum, cs); |
---|
1392 | proj_destroy(datum); |
---|
1393 | proj_destroy(cs); |
---|
1394 | proj_destroy(geodetic_crs); |
---|
1395 | PJ * newOp = proj_create_crs_to_crs_from_pj(ctx, temp, pj, NULL, NULL); |
---|
1396 | proj_destroy(temp); |
---|
1397 | if (newOp) { |
---|
1398 | proj_destroy(pj); |
---|
1399 | pj = newOp; |
---|
1400 | } |
---|
1401 | break; |
---|
1402 | } |
---|
1403 | default: |
---|
1404 | break; |
---|
1405 | } |
---|
1406 | #endif |
---|
1407 | #if PROJ_VERSION_MAJOR < 9 || \ |
---|
1408 | (PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 3) |
---|
1409 | if (pj) { |
---|
1410 | /* In PROJ < 9.3.0 proj_factors() returns a grid convergence which is |
---|
1411 | * off by 90° for a projected coordinate system with northing/easting |
---|
1412 | * axis order. We can't copy over the fix for this in PROJ 9.3.0's |
---|
1413 | * proj_factors() since it uses non-public PROJ functions, but |
---|
1414 | * normalising the output order here works too. |
---|
1415 | */ |
---|
1416 | PJ* pj_norm = proj_normalize_for_visualization(PJ_DEFAULT_CTX, pj); |
---|
1417 | proj_destroy(pj); |
---|
1418 | pj = pj_norm; |
---|
1419 | } |
---|
1420 | #endif |
---|
1421 | PJ_FACTORS factors = proj_factors(pj, lp); |
---|
1422 | proj_destroy(pj); |
---|
1423 | #if PROJ_VERSION_MAJOR < 8 || \ |
---|
1424 | (PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR < 1) |
---|
1425 | proj_context_destroy(ctx); |
---|
1426 | #endif |
---|
1427 | return factors.meridian_convergence; |
---|
1428 | } |
---|
1429 | |
---|
1430 | static real |
---|
1431 | handle_compass(real *p_var) |
---|
1432 | { |
---|
1433 | real compvar = VAR(Comp); |
---|
1434 | real comp = VAL(Comp); |
---|
1435 | real backcomp = VAL(BackComp); |
---|
1436 | real declination; |
---|
1437 | if (pcs->z[Q_DECLINATION] != HUGE_REAL) { |
---|
1438 | declination = -pcs->z[Q_DECLINATION]; |
---|
1439 | } else if (pcs->declination != HUGE_REAL) { |
---|
1440 | /* Cached value calculated for a previous compass reading taken on the |
---|
1441 | * same date (by the 'else' just below). |
---|
1442 | */ |
---|
1443 | declination = pcs->declination; |
---|
1444 | } else { |
---|
1445 | if (!pcs->meta || pcs->meta->days1 == -1) { |
---|
1446 | compile_diagnostic(DIAG_WARN, /*No survey date specified - using 0 for magnetic declination*/304); |
---|
1447 | declination = 0; |
---|
1448 | } else { |
---|
1449 | int avg_days = (pcs->meta->days1 + pcs->meta->days2) / 2; |
---|
1450 | double dat = julian_date_from_days_since_1900(avg_days); |
---|
1451 | /* thgeomag() takes (lat, lon, h, dat) - i.e. (y, x, z, date). */ |
---|
1452 | declination = thgeomag(pcs->dec_lat, pcs->dec_lon, pcs->dec_alt, dat); |
---|
1453 | if (declination < pcs->min_declination) { |
---|
1454 | pcs->min_declination = declination; |
---|
1455 | pcs->min_declination_days = avg_days; |
---|
1456 | } |
---|
1457 | if (declination > pcs->max_declination) { |
---|
1458 | pcs->max_declination = declination; |
---|
1459 | pcs->max_declination_days = avg_days; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | if (pcs->convergence == HUGE_REAL) { |
---|
1463 | /* Compute the convergence lazily. It only depends on the output |
---|
1464 | * coordinate system so we can cache it for reuse to apply to |
---|
1465 | * a declination value for a different date. |
---|
1466 | */ |
---|
1467 | pcs->convergence = compute_convergence(pcs->dec_lon, pcs->dec_lat); |
---|
1468 | } |
---|
1469 | declination -= pcs->convergence; |
---|
1470 | /* We cache the calculated declination as the calculation is relatively |
---|
1471 | * expensive. We also cache an "assumed 0" answer so that we only |
---|
1472 | * warn once per such survey rather than for every line with a compass |
---|
1473 | * reading. */ |
---|
1474 | pcs->declination = declination; |
---|
1475 | } |
---|
1476 | if (comp != HUGE_REAL) { |
---|
1477 | comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING]; |
---|
1478 | comp += declination; |
---|
1479 | } |
---|
1480 | if (backcomp != HUGE_REAL) { |
---|
1481 | backcomp = (backcomp - pcs->z[Q_BACKBEARING]) |
---|
1482 | * pcs->sc[Q_BACKBEARING]; |
---|
1483 | backcomp += declination; |
---|
1484 | backcomp -= M_PI; |
---|
1485 | if (comp != HUGE_REAL) { |
---|
1486 | real diff = comp - backcomp; |
---|
1487 | real adj = fabs(diff) > M_PI ? M_PI : 0; |
---|
1488 | diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI; |
---|
1489 | if (sqrd(diff / 3.0) > compvar + VAR(BackComp)) { |
---|
1490 | /* fore and back readings differ by more than 3 sds */ |
---|
1491 | /* TRANSLATORS: %s is replaced by the amount the readings disagree |
---|
1492 | * by, e.g. "2.5°" or "3ᵍ". */ |
---|
1493 | warn_readings_differ(/*COMPASS reading and BACKCOMPASS reading disagree by %s*/98, |
---|
1494 | diff, get_angle_units(Q_BEARING), Comp, BackComp); |
---|
1495 | } |
---|
1496 | comp = (comp / compvar + backcomp / VAR(BackComp)); |
---|
1497 | compvar = (compvar + VAR(BackComp)) / 4; |
---|
1498 | comp *= compvar; |
---|
1499 | comp += adj; |
---|
1500 | } else { |
---|
1501 | comp = backcomp; |
---|
1502 | compvar = VAR(BackComp); |
---|
1503 | } |
---|
1504 | } |
---|
1505 | *p_var = compvar; |
---|
1506 | return comp; |
---|
1507 | } |
---|
1508 | |
---|
1509 | static real |
---|
1510 | handle_clino(q_quantity q, reading r, real val, bool percent, clino_type *p_ctype) |
---|
1511 | { |
---|
1512 | bool range_0_180; |
---|
1513 | real z; |
---|
1514 | real diff_from_abs90; |
---|
1515 | val *= pcs->units[q]; |
---|
1516 | /* percentage scale */ |
---|
1517 | if (percent) val = atan(val); |
---|
1518 | /* We want to warn if there's a reading which it would be impossible |
---|
1519 | * to have read from the instrument (e.g. on a -90 to 90 degree scale |
---|
1520 | * you can't read "96" (it's probably a typo for "69"). However, the |
---|
1521 | * gradient reading from a topofil is typically in the range 0 to 180, |
---|
1522 | * with 90 being horizontal. |
---|
1523 | * |
---|
1524 | * Really we should allow the valid range to be specified, but for now |
---|
1525 | * we infer it from the zero error - if this is within 45 degrees of |
---|
1526 | * 90 then we assume the range is 0 to 180. |
---|
1527 | */ |
---|
1528 | z = pcs->z[q]; |
---|
1529 | range_0_180 = (z > M_PI_4 && z < 3*M_PI_4); |
---|
1530 | diff_from_abs90 = fabs(val) - M_PI_2; |
---|
1531 | if (diff_from_abs90 > EPSILON) { |
---|
1532 | if (!range_0_180) { |
---|
1533 | int clino_units = get_angle_units(q); |
---|
1534 | const char * units = get_units_string(clino_units); |
---|
1535 | real right_angle = M_PI_2 / get_units_factor(clino_units); |
---|
1536 | /* FIXME: different message for BackClino? */ |
---|
1537 | /* TRANSLATORS: %.f%s will be replaced with a right angle in the |
---|
1538 | * units currently in use, e.g. "90°" or "100ᵍ". And "absolute |
---|
1539 | * value" means the reading ignoring the sign (so it might be |
---|
1540 | * < -90° or > 90°. */ |
---|
1541 | compile_diagnostic_reading(DIAG_WARN, r, /*Clino reading over %.f%s (absolute value)*/51, |
---|
1542 | right_angle, units); |
---|
1543 | } |
---|
1544 | } else if (TSTBIT(pcs->infer, INFER_PLUMBS) && |
---|
1545 | diff_from_abs90 >= -EPSILON) { |
---|
1546 | *p_ctype = CTYPE_INFERPLUMB; |
---|
1547 | } |
---|
1548 | if (range_0_180 && *p_ctype != CTYPE_INFERPLUMB) { |
---|
1549 | /* FIXME: Warning message not ideal... */ |
---|
1550 | if (val < 0.0 || val - M_PI > EPSILON) { |
---|
1551 | int clino_units = get_angle_units(q); |
---|
1552 | const char * units = get_units_string(clino_units); |
---|
1553 | real right_angle = M_PI_2 / get_units_factor(clino_units); |
---|
1554 | compile_diagnostic_reading(DIAG_WARN, r, /*Clino reading over %.f%s (absolute value)*/51, |
---|
1555 | right_angle, units); |
---|
1556 | } |
---|
1557 | } |
---|
1558 | return val; |
---|
1559 | } |
---|
1560 | |
---|
1561 | static int |
---|
1562 | process_normal(prefix *fr, prefix *to, bool fToFirst, |
---|
1563 | clino_type ctype, clino_type backctype) |
---|
1564 | { |
---|
1565 | real tape = VAL(Tape); |
---|
1566 | real clin = VAL(Clino); |
---|
1567 | real backclin = VAL(BackClino); |
---|
1568 | |
---|
1569 | real dx, dy, dz; |
---|
1570 | real vx, vy, vz; |
---|
1571 | #ifndef NO_COVARIANCES |
---|
1572 | real cxy, cyz, czx; |
---|
1573 | #endif |
---|
1574 | |
---|
1575 | /* adjusted tape is negative -- probably the calibration is wrong */ |
---|
1576 | if (tape < (real)0.0) { |
---|
1577 | /* TRANSLATE different message for topofil? */ |
---|
1578 | compile_diagnostic_reading(DIAG_WARN, Tape, /*Negative adjusted tape reading*/79); |
---|
1579 | } |
---|
1580 | |
---|
1581 | reading comp_given = handle_comp_units(); |
---|
1582 | |
---|
1583 | if (ctype == CTYPE_READING) { |
---|
1584 | clin = handle_clino(Q_GRADIENT, Clino, clin, |
---|
1585 | pcs->f_clino_percent, &ctype); |
---|
1586 | } |
---|
1587 | |
---|
1588 | if (backctype == CTYPE_READING) { |
---|
1589 | backclin = handle_clino(Q_BACKGRADIENT, BackClino, backclin, |
---|
1590 | pcs->f_backclino_percent, &backctype); |
---|
1591 | } |
---|
1592 | |
---|
1593 | /* un-infer the plumb if the backsight was just a reading */ |
---|
1594 | if (ctype == CTYPE_INFERPLUMB && backctype == CTYPE_READING) { |
---|
1595 | ctype = CTYPE_READING; |
---|
1596 | } |
---|
1597 | |
---|
1598 | if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) { |
---|
1599 | /* TRANSLATORS: In data with backsights, the user has tried to give a |
---|
1600 | * plumb for the foresight and a clino reading for the backsight, or |
---|
1601 | * something similar. */ |
---|
1602 | compile_error_reading_skip(Clino, /*CLINO and BACKCLINO readings must be of the same type*/84); |
---|
1603 | return 0; |
---|
1604 | } |
---|
1605 | |
---|
1606 | if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB || |
---|
1607 | backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) { |
---|
1608 | /* plumbed */ |
---|
1609 | if (comp_given != End) { |
---|
1610 | if (ctype == CTYPE_PLUMB || |
---|
1611 | (ctype == CTYPE_INFERPLUMB && VAL(Comp) != 0.0) || |
---|
1612 | backctype == CTYPE_PLUMB || |
---|
1613 | (backctype == CTYPE_INFERPLUMB && VAL(BackComp) != 0.0)) { |
---|
1614 | /* TRANSLATORS: A "plumbed leg" is one measured using a plumbline |
---|
1615 | * (a weight on a string). So the problem here is that the leg is |
---|
1616 | * vertical, so a compass reading has no meaning! */ |
---|
1617 | compile_diagnostic_reading(DIAG_WARN, comp_given, /*Compass reading given on plumbed leg*/21); |
---|
1618 | } |
---|
1619 | } |
---|
1620 | |
---|
1621 | dx = dy = (real)0.0; |
---|
1622 | if (ctype != CTYPE_OMIT) { |
---|
1623 | if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) { |
---|
1624 | /* TRANSLATORS: We've been told the foresight and backsight are |
---|
1625 | * both "UP", or that they're both "DOWN". */ |
---|
1626 | compile_error_reading_skip(Clino, /*Plumbed CLINO and BACKCLINO readings can't be in the same direction*/92); |
---|
1627 | return 0; |
---|
1628 | } |
---|
1629 | dz = (clin > (real)0.0) ? tape : -tape; |
---|
1630 | } else { |
---|
1631 | dz = (backclin < (real)0.0) ? tape : -tape; |
---|
1632 | } |
---|
1633 | vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB); |
---|
1634 | vz = var(Q_POS) / 3.0 + VAR(Tape); |
---|
1635 | #ifndef NO_COVARIANCES |
---|
1636 | /* Correct values - no covariances in this case! */ |
---|
1637 | cxy = cyz = czx = (real)0.0; |
---|
1638 | #endif |
---|
1639 | } else { |
---|
1640 | /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ |
---|
1641 | * or CTYPE_OMIT */ |
---|
1642 | /* clino */ |
---|
1643 | real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V; |
---|
1644 | if (comp_given == End) { |
---|
1645 | /* TRANSLATORS: Here "legs" are survey legs, i.e. measurements between |
---|
1646 | * survey stations. */ |
---|
1647 | compile_error_reading_skip(Comp, /*Compass reading may not be omitted except on plumbed legs*/14); |
---|
1648 | return 0; |
---|
1649 | } |
---|
1650 | if (tape == (real)0.0) { |
---|
1651 | dx = dy = dz = (real)0.0; |
---|
1652 | vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */ |
---|
1653 | #ifndef NO_COVARIANCES |
---|
1654 | cxy = cyz = czx = (real)0.0; |
---|
1655 | #endif |
---|
1656 | #if DEBUG_DATAIN_1 |
---|
1657 | printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz); |
---|
1658 | #endif |
---|
1659 | } else { |
---|
1660 | real sinGcosG; |
---|
1661 | /* take into account variance in LEVEL case */ |
---|
1662 | real var_clin = var(Q_LEVEL); |
---|
1663 | real var_comp; |
---|
1664 | real comp = handle_compass(&var_comp); |
---|
1665 | /* ctype != CTYPE_READING is LEVEL case */ |
---|
1666 | if (ctype == CTYPE_READING) { |
---|
1667 | clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT]; |
---|
1668 | var_clin = VAR(Clino); |
---|
1669 | } |
---|
1670 | if (backctype == CTYPE_READING) { |
---|
1671 | backclin = (backclin - pcs->z[Q_BACKGRADIENT]) |
---|
1672 | * pcs->sc[Q_BACKGRADIENT]; |
---|
1673 | if (ctype == CTYPE_READING) { |
---|
1674 | if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) { |
---|
1675 | /* fore and back readings differ by more than 3 sds */ |
---|
1676 | /* TRANSLATORS: %s is replaced by the amount the readings disagree |
---|
1677 | * by, e.g. "2.5°" or "3ᵍ". */ |
---|
1678 | warn_readings_differ(/*CLINO reading and BACKCLINO reading disagree by %s*/99, |
---|
1679 | clin + backclin, get_angle_units(Q_GRADIENT), Clino, BackClino); |
---|
1680 | } |
---|
1681 | clin = (clin / var_clin - backclin / VAR(BackClino)); |
---|
1682 | var_clin = (var_clin + VAR(BackClino)) / 4; |
---|
1683 | clin *= var_clin; |
---|
1684 | } else { |
---|
1685 | clin = -backclin; |
---|
1686 | var_clin = VAR(BackClino); |
---|
1687 | } |
---|
1688 | } |
---|
1689 | |
---|
1690 | #if DEBUG_DATAIN |
---|
1691 | printf(" %4.2f %4.2f %4.2f\n", tape, comp, clin); |
---|
1692 | #endif |
---|
1693 | cosG = cos(clin); |
---|
1694 | LcosG = tape * cosG; |
---|
1695 | sinB = sin(comp); |
---|
1696 | cosB = cos(comp); |
---|
1697 | #if DEBUG_DATAIN_1 |
---|
1698 | printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG); |
---|
1699 | #endif |
---|
1700 | dx = LcosG * sinB; |
---|
1701 | dy = LcosG * cosB; |
---|
1702 | dz = tape * sin(clin); |
---|
1703 | /* printf("%.2f\n",clin); */ |
---|
1704 | #if DEBUG_DATAIN_1 |
---|
1705 | printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz); |
---|
1706 | #endif |
---|
1707 | dx2 = dx * dx; |
---|
1708 | L2 = tape * tape; |
---|
1709 | V = VAR(Tape) / L2; |
---|
1710 | dy2 = dy * dy; |
---|
1711 | cosG2 = cosG * cosG; |
---|
1712 | sinGcosG = sin(clin) * cosG; |
---|
1713 | dz2 = dz * dz; |
---|
1714 | v = dz2 * var_clin; |
---|
1715 | #ifdef NO_COVARIANCES |
---|
1716 | vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp + |
---|
1717 | (.5 + sinB * sinB * cosG2) * v); |
---|
1718 | vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp + |
---|
1719 | (.5 + cosB * cosB * cosG2) * v); |
---|
1720 | if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) { |
---|
1721 | /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */ |
---|
1722 | vz = var(Q_POS) / 3.0 + L2 * (real)0.1; |
---|
1723 | } else { |
---|
1724 | vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin; |
---|
1725 | } |
---|
1726 | /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */ |
---|
1727 | #else |
---|
1728 | vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp + |
---|
1729 | (sinB * sinB * v); |
---|
1730 | vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp + |
---|
1731 | (cosB * cosB * v); |
---|
1732 | if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) { |
---|
1733 | /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */ |
---|
1734 | vz = var(Q_POS) / 3.0 + L2 * (real)0.1; |
---|
1735 | } else { |
---|
1736 | vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin; |
---|
1737 | } |
---|
1738 | /* usual covariance formulae are fine in no clino case since |
---|
1739 | * dz = 0 so value of var_clin is ignored */ |
---|
1740 | cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2) |
---|
1741 | - var_comp * dx * dy; |
---|
1742 | czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz; |
---|
1743 | cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz; |
---|
1744 | #if 0 |
---|
1745 | printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz); |
---|
1746 | printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx); |
---|
1747 | #endif |
---|
1748 | #endif |
---|
1749 | #if DEBUG_DATAIN_1 |
---|
1750 | printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz); |
---|
1751 | #endif |
---|
1752 | } |
---|
1753 | } |
---|
1754 | #if DEBUG_DATAIN_1 |
---|
1755 | printf("Just before addleg, vx = %f\n", vx); |
---|
1756 | #endif |
---|
1757 | /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/ |
---|
1758 | addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz |
---|
1759 | #ifndef NO_COVARIANCES |
---|
1760 | , cyz, czx, cxy |
---|
1761 | #endif |
---|
1762 | ); |
---|
1763 | return 1; |
---|
1764 | } |
---|
1765 | |
---|
1766 | static int |
---|
1767 | process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange) |
---|
1768 | { |
---|
1769 | real tape = VAL(Tape); |
---|
1770 | |
---|
1771 | real dx, dy, dz; |
---|
1772 | real vx, vy, vz; |
---|
1773 | #ifndef NO_COVARIANCES |
---|
1774 | real cxy = 0, cyz = 0, czx = 0; |
---|
1775 | #endif |
---|
1776 | |
---|
1777 | handle_comp_units(); |
---|
1778 | |
---|
1779 | /* depth gauge readings increase upwards with default calibration */ |
---|
1780 | if (fDepthChange) { |
---|
1781 | SVX_ASSERT(VAL(FrDepth) == 0.0); |
---|
1782 | dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH]; |
---|
1783 | dz *= pcs->sc[Q_DEPTH]; |
---|
1784 | } else { |
---|
1785 | dz = VAL(ToDepth) - VAL(FrDepth); |
---|
1786 | dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH]; |
---|
1787 | } |
---|
1788 | |
---|
1789 | /* adjusted tape is negative -- probably the calibration is wrong */ |
---|
1790 | if (tape < (real)0.0) { |
---|
1791 | compile_diagnostic_reading(DIAG_WARN, Tape, /*Negative adjusted tape reading*/79); |
---|
1792 | } |
---|
1793 | |
---|
1794 | /* check if tape is less than depth change */ |
---|
1795 | if (tape < fabs(dz)) { |
---|
1796 | /* FIXME: allow margin of error based on variances? */ |
---|
1797 | /* TRANSLATORS: This means that the data fed in said this. |
---|
1798 | * |
---|
1799 | * It could be a gross error (e.g. the decimal point is missing from the |
---|
1800 | * depth gauge reading) or it could just be due to random error on a near |
---|
1801 | * vertical leg */ |
---|
1802 | compile_diagnostic_reading(DIAG_WARN, Tape, /*Tape reading is less than change in depth*/62); |
---|
1803 | } |
---|
1804 | |
---|
1805 | if (tape == (real)0.0 && dz == 0.0) { |
---|
1806 | dx = dy = dz = (real)0.0; |
---|
1807 | vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */ |
---|
1808 | } else if (VAL(Comp) == HUGE_REAL && |
---|
1809 | VAL(BackComp) == HUGE_REAL) { |
---|
1810 | /* plumb */ |
---|
1811 | dx = dy = (real)0.0; |
---|
1812 | if (dz < 0) tape = -tape; |
---|
1813 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1814 | dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) |
---|
1815 | / (VAR(Tape) * 2 * VAR(ToDepth)); |
---|
1816 | vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB); |
---|
1817 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1818 | vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) |
---|
1819 | / (VAR(Tape) + VAR(ToDepth)); |
---|
1820 | } else { |
---|
1821 | real L2, sinB, cosB, dz2, D2; |
---|
1822 | real var_comp; |
---|
1823 | real comp = handle_compass(&var_comp); |
---|
1824 | sinB = sin(comp); |
---|
1825 | cosB = cos(comp); |
---|
1826 | L2 = tape * tape; |
---|
1827 | dz2 = dz * dz; |
---|
1828 | D2 = L2 - dz2; |
---|
1829 | if (D2 <= (real)0.0) { |
---|
1830 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1831 | real vsum = VAR(Tape) + 2 * VAR(ToDepth); |
---|
1832 | dx = dy = (real)0.0; |
---|
1833 | vx = vy = var(Q_POS) / 3.0; |
---|
1834 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1835 | vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum; |
---|
1836 | if (dz > 0) { |
---|
1837 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1838 | dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum; |
---|
1839 | } else { |
---|
1840 | dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum; |
---|
1841 | } |
---|
1842 | } else { |
---|
1843 | real D = sqrt(D2); |
---|
1844 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1845 | real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2; |
---|
1846 | dx = D * sinB; |
---|
1847 | dy = D * cosB; |
---|
1848 | |
---|
1849 | vx = var(Q_POS) / 3.0 + |
---|
1850 | sinB * sinB * F / D2 + var_comp * dy * dy; |
---|
1851 | vy = var(Q_POS) / 3.0 + |
---|
1852 | cosB * cosB * F / D2 + var_comp * dx * dx; |
---|
1853 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1854 | vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth); |
---|
1855 | |
---|
1856 | #ifndef NO_COVARIANCES |
---|
1857 | cxy = sinB * cosB * (F / D2 + var_comp * D2); |
---|
1858 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1859 | cyz = -2 * VAR(ToDepth) * dy / D; |
---|
1860 | czx = -2 * VAR(ToDepth) * dx / D; |
---|
1861 | #endif |
---|
1862 | } |
---|
1863 | /* FIXME: If there's a clino reading, check it against the depth reading, |
---|
1864 | * and average. |
---|
1865 | * if (VAL(Clino) != HUGE_REAL || VAL(BackClino) != HUGE_REAL) { ... } |
---|
1866 | */ |
---|
1867 | } |
---|
1868 | addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz |
---|
1869 | #ifndef NO_COVARIANCES |
---|
1870 | , cxy, cyz, czx |
---|
1871 | #endif |
---|
1872 | ); |
---|
1873 | return 1; |
---|
1874 | } |
---|
1875 | |
---|
1876 | static int |
---|
1877 | process_cartesian(prefix *fr, prefix *to, bool fToFirst) |
---|
1878 | { |
---|
1879 | real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX]; |
---|
1880 | real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY]; |
---|
1881 | real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ]; |
---|
1882 | |
---|
1883 | addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz) |
---|
1884 | #ifndef NO_COVARIANCES |
---|
1885 | , 0, 0, 0 |
---|
1886 | #endif |
---|
1887 | ); |
---|
1888 | return 1; |
---|
1889 | } |
---|
1890 | |
---|
1891 | static void |
---|
1892 | data_cartesian(void) |
---|
1893 | { |
---|
1894 | prefix *fr = NULL, *to = NULL; |
---|
1895 | |
---|
1896 | bool fMulti = false; |
---|
1897 | |
---|
1898 | reading first_stn = End; |
---|
1899 | |
---|
1900 | const reading *ordering; |
---|
1901 | |
---|
1902 | again: |
---|
1903 | |
---|
1904 | for (ordering = pcs->ordering ; ; ordering++) { |
---|
1905 | skipblanks(); |
---|
1906 | switch (*ordering) { |
---|
1907 | case Fr: |
---|
1908 | fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT); |
---|
1909 | if (first_stn == End) first_stn = Fr; |
---|
1910 | break; |
---|
1911 | case To: |
---|
1912 | to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT); |
---|
1913 | if (first_stn == End) first_stn = To; |
---|
1914 | break; |
---|
1915 | case Station: |
---|
1916 | fr = to; |
---|
1917 | to = read_prefix(PFX_STATION); |
---|
1918 | first_stn = To; |
---|
1919 | break; |
---|
1920 | case Dx: case Dy: case Dz: |
---|
1921 | read_reading(*ordering, false); |
---|
1922 | break; |
---|
1923 | case Ignore: |
---|
1924 | skipword(); break; |
---|
1925 | case IgnoreAllAndNewLine: |
---|
1926 | skipline(); |
---|
1927 | /* fall through */ |
---|
1928 | case Newline: |
---|
1929 | if (fr != NULL) { |
---|
1930 | if (!process_cartesian(fr, to, first_stn == To)) |
---|
1931 | skipline(); |
---|
1932 | } |
---|
1933 | fMulti = true; |
---|
1934 | while (1) { |
---|
1935 | process_eol(); |
---|
1936 | skipblanks(); |
---|
1937 | if (isData(ch)) break; |
---|
1938 | if (!isComm(ch)) { |
---|
1939 | return; |
---|
1940 | } |
---|
1941 | } |
---|
1942 | break; |
---|
1943 | case IgnoreAll: |
---|
1944 | skipline(); |
---|
1945 | /* fall through */ |
---|
1946 | case End: |
---|
1947 | if (!fMulti) { |
---|
1948 | process_cartesian(fr, to, first_stn == To); |
---|
1949 | process_eol(); |
---|
1950 | return; |
---|
1951 | } |
---|
1952 | do { |
---|
1953 | process_eol(); |
---|
1954 | skipblanks(); |
---|
1955 | } while (isComm(ch)); |
---|
1956 | goto again; |
---|
1957 | default: BUG("Unknown reading in ordering"); |
---|
1958 | } |
---|
1959 | } |
---|
1960 | } |
---|
1961 | |
---|
1962 | static int |
---|
1963 | process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange) |
---|
1964 | { |
---|
1965 | real tape = VAL(Tape); |
---|
1966 | |
---|
1967 | real dx, dy, dz; |
---|
1968 | real vx, vy, vz; |
---|
1969 | #ifndef NO_COVARIANCES |
---|
1970 | real cxy = 0; |
---|
1971 | #endif |
---|
1972 | |
---|
1973 | handle_comp_units(); |
---|
1974 | |
---|
1975 | /* depth gauge readings increase upwards with default calibration */ |
---|
1976 | if (fDepthChange) { |
---|
1977 | SVX_ASSERT(VAL(FrDepth) == 0.0); |
---|
1978 | dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH]; |
---|
1979 | dz *= pcs->sc[Q_DEPTH]; |
---|
1980 | } else { |
---|
1981 | dz = VAL(ToDepth) - VAL(FrDepth); |
---|
1982 | dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH]; |
---|
1983 | } |
---|
1984 | |
---|
1985 | /* adjusted tape is negative -- probably the calibration is wrong */ |
---|
1986 | if (tape < (real)0.0) { |
---|
1987 | compile_diagnostic_reading(DIAG_WARN, Tape, /*Negative adjusted tape reading*/79); |
---|
1988 | } |
---|
1989 | |
---|
1990 | if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) { |
---|
1991 | /* plumb */ |
---|
1992 | dx = dy = (real)0.0; |
---|
1993 | vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB); |
---|
1994 | /* FIXME: Should use FrDepth sometimes... */ |
---|
1995 | vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth); |
---|
1996 | } else { |
---|
1997 | real sinB, cosB; |
---|
1998 | real var_comp; |
---|
1999 | real comp = handle_compass(&var_comp); |
---|
2000 | sinB = sin(comp); |
---|
2001 | cosB = cos(comp); |
---|
2002 | |
---|
2003 | dx = tape * sinB; |
---|
2004 | dy = tape * cosB; |
---|
2005 | |
---|
2006 | vx = var(Q_POS) / 3.0 + |
---|
2007 | VAR(Tape) * sinB * sinB + var_comp * dy * dy; |
---|
2008 | vy = var(Q_POS) / 3.0 + |
---|
2009 | VAR(Tape) * cosB * cosB + var_comp * dx * dx; |
---|
2010 | /* FIXME: Should use FrDepth sometimes... */ |
---|
2011 | vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth); |
---|
2012 | |
---|
2013 | #ifndef NO_COVARIANCES |
---|
2014 | cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB; |
---|
2015 | #endif |
---|
2016 | } |
---|
2017 | addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz |
---|
2018 | #ifndef NO_COVARIANCES |
---|
2019 | , cxy, 0, 0 |
---|
2020 | #endif |
---|
2021 | ); |
---|
2022 | return 1; |
---|
2023 | } |
---|
2024 | |
---|
2025 | /* Process tape/compass/clino, diving, and cylpolar styles of survey data |
---|
2026 | * Also handles topofil (fromcount/tocount or count) in place of tape */ |
---|
2027 | static void |
---|
2028 | data_normal(void) |
---|
2029 | { |
---|
2030 | prefix *fr = NULL, *to = NULL; |
---|
2031 | reading first_stn = End; |
---|
2032 | |
---|
2033 | bool fTopofil = false, fMulti = false; |
---|
2034 | bool fRev; |
---|
2035 | clino_type ctype, backctype; |
---|
2036 | bool fDepthChange; |
---|
2037 | unsigned long compass_dat_flags = 0; |
---|
2038 | |
---|
2039 | const reading *ordering; |
---|
2040 | |
---|
2041 | VAL(Tape) = VAL(BackTape) = HUGE_REAL; |
---|
2042 | VAL(Comp) = VAL(BackComp) = HUGE_REAL; |
---|
2043 | VAL(FrCount) = VAL(ToCount) = 0; |
---|
2044 | VAL(FrDepth) = VAL(ToDepth) = 0; |
---|
2045 | VAL(Left) = VAL(Right) = VAL(Up) = VAL(Down) = HUGE_REAL; |
---|
2046 | |
---|
2047 | fRev = false; |
---|
2048 | ctype = backctype = CTYPE_OMIT; |
---|
2049 | fDepthChange = false; |
---|
2050 | |
---|
2051 | /* ordering may omit clino reading, so set up default here */ |
---|
2052 | /* this is also used if clino reading is the omit character */ |
---|
2053 | VAL(Clino) = VAL(BackClino) = 0; |
---|
2054 | |
---|
2055 | again: |
---|
2056 | |
---|
2057 | /* We clear these flags in the normal course of events, but if there's an |
---|
2058 | * error in a reading, we might not, so make sure it has been cleared here. |
---|
2059 | */ |
---|
2060 | pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY)); |
---|
2061 | for (ordering = pcs->ordering; ; ordering++) { |
---|
2062 | skipblanks(); |
---|
2063 | switch (*ordering) { |
---|
2064 | case Fr: |
---|
2065 | fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON); |
---|
2066 | if (first_stn == End) first_stn = Fr; |
---|
2067 | break; |
---|
2068 | case To: |
---|
2069 | to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON); |
---|
2070 | if (first_stn == End) first_stn = To; |
---|
2071 | break; |
---|
2072 | case CompassDATFr: |
---|
2073 | // Compass DAT is always From then To. |
---|
2074 | first_stn = Fr; |
---|
2075 | fr = read_prefix(PFX_STATION); |
---|
2076 | scan_compass_station_name(fr); |
---|
2077 | break; |
---|
2078 | case CompassDATTo: |
---|
2079 | to = read_prefix(PFX_STATION); |
---|
2080 | scan_compass_station_name(to); |
---|
2081 | break; |
---|
2082 | case Station: |
---|
2083 | fr = to; |
---|
2084 | to = read_prefix(PFX_STATION); |
---|
2085 | first_stn = To; |
---|
2086 | break; |
---|
2087 | case Dir: { |
---|
2088 | typedef enum { |
---|
2089 | DIR_NULL=-1, DIR_FORE, DIR_BACK |
---|
2090 | } dir_tok; |
---|
2091 | static const sztok dir_tab[] = { |
---|
2092 | {"B", DIR_BACK}, |
---|
2093 | {"F", DIR_FORE}, |
---|
2094 | }; |
---|
2095 | dir_tok tok; |
---|
2096 | get_token(); |
---|
2097 | tok = match_tok(dir_tab, TABSIZE(dir_tab)); |
---|
2098 | switch (tok) { |
---|
2099 | case DIR_FORE: |
---|
2100 | break; |
---|
2101 | case DIR_BACK: |
---|
2102 | fRev = true; |
---|
2103 | break; |
---|
2104 | default: |
---|
2105 | compile_diagnostic(DIAG_ERR|DIAG_TOKEN|DIAG_SKIP, /*Found “%s”, expecting “F” or “B”*/131, s_str(&token)); |
---|
2106 | process_eol(); |
---|
2107 | return; |
---|
2108 | } |
---|
2109 | break; |
---|
2110 | } |
---|
2111 | case Tape: case BackTape: { |
---|
2112 | reading r = *ordering; |
---|
2113 | read_reading(r, true); |
---|
2114 | if (VAL(r) == HUGE_REAL) { |
---|
2115 | if (!isOmit(ch)) { |
---|
2116 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
2117 | /* Avoid also warning about omitted tape reading. */ |
---|
2118 | VAL(r) = 0; |
---|
2119 | } else { |
---|
2120 | nextch(); |
---|
2121 | } |
---|
2122 | } else if (VAL(r) < (real)0.0) { |
---|
2123 | compile_diagnostic_reading(DIAG_WARN, r, /*Negative tape reading*/60); |
---|
2124 | } |
---|
2125 | break; |
---|
2126 | } |
---|
2127 | case Count: |
---|
2128 | VAL(FrCount) = VAL(ToCount); |
---|
2129 | LOC(FrCount) = LOC(ToCount); |
---|
2130 | WID(FrCount) = WID(ToCount); |
---|
2131 | read_reading(ToCount, false); |
---|
2132 | fTopofil = true; |
---|
2133 | break; |
---|
2134 | case FrCount: |
---|
2135 | read_reading(FrCount, false); |
---|
2136 | break; |
---|
2137 | case ToCount: |
---|
2138 | read_reading(ToCount, false); |
---|
2139 | fTopofil = true; |
---|
2140 | break; |
---|
2141 | case Comp: case BackComp: |
---|
2142 | read_bearing_or_omit(*ordering); |
---|
2143 | break; |
---|
2144 | case Clino: case BackClino: { |
---|
2145 | reading r = *ordering; |
---|
2146 | clino_type * p_ctype = (r == Clino ? &ctype : &backctype); |
---|
2147 | read_reading(r, true); |
---|
2148 | if (VAL(r) == HUGE_REAL) { |
---|
2149 | VAL(r) = handle_plumb(p_ctype); |
---|
2150 | if (VAL(r) != HUGE_REAL) break; |
---|
2151 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
2152 | skipline(); |
---|
2153 | process_eol(); |
---|
2154 | return; |
---|
2155 | } |
---|
2156 | *p_ctype = CTYPE_READING; |
---|
2157 | break; |
---|
2158 | } |
---|
2159 | case FrDepth: case ToDepth: |
---|
2160 | read_reading(*ordering, false); |
---|
2161 | break; |
---|
2162 | case Depth: |
---|
2163 | VAL(FrDepth) = VAL(ToDepth); |
---|
2164 | LOC(FrDepth) = LOC(ToDepth); |
---|
2165 | WID(FrDepth) = WID(ToDepth); |
---|
2166 | read_reading(ToDepth, false); |
---|
2167 | break; |
---|
2168 | case DepthChange: |
---|
2169 | fDepthChange = true; |
---|
2170 | VAL(FrDepth) = 0; |
---|
2171 | read_reading(ToDepth, false); |
---|
2172 | break; |
---|
2173 | case CompassDATComp: |
---|
2174 | read_bearing_or_omit(Comp); |
---|
2175 | if (is_compass_NaN(VAL(Comp))) VAL(Comp) = HUGE_REAL; |
---|
2176 | break; |
---|
2177 | case CompassDATBackComp: |
---|
2178 | read_bearing_or_omit(BackComp); |
---|
2179 | if (is_compass_NaN(VAL(BackComp))) VAL(BackComp) = HUGE_REAL; |
---|
2180 | break; |
---|
2181 | case CompassDATClino: case CompassDATBackClino: { |
---|
2182 | reading r; |
---|
2183 | clino_type * p_ctype; |
---|
2184 | if (*ordering == CompassDATClino) { |
---|
2185 | r = Clino; |
---|
2186 | p_ctype = &ctype; |
---|
2187 | } else { |
---|
2188 | r = BackClino; |
---|
2189 | p_ctype = &backctype; |
---|
2190 | } |
---|
2191 | read_reading(r, false); |
---|
2192 | if (is_compass_NaN(VAL(r))) { |
---|
2193 | VAL(r) = 0; |
---|
2194 | *p_ctype = CTYPE_OMIT; |
---|
2195 | } else { |
---|
2196 | *p_ctype = CTYPE_READING; |
---|
2197 | } |
---|
2198 | break; |
---|
2199 | } |
---|
2200 | case CompassDATLeft: case CompassDATRight: |
---|
2201 | case CompassDATUp: case CompassDATDown: { |
---|
2202 | /* FIXME: need to actually make use of these entries! */ |
---|
2203 | reading actual = Left + (*ordering - CompassDATLeft); |
---|
2204 | read_reading(actual, false); |
---|
2205 | if (VAL(actual) < 0) VAL(actual) = HUGE_REAL; |
---|
2206 | break; |
---|
2207 | } |
---|
2208 | case CompassDATFlags: |
---|
2209 | if (ch == '#') { |
---|
2210 | filepos fp; |
---|
2211 | get_pos(&fp); |
---|
2212 | nextch(); |
---|
2213 | if (ch == '|') { |
---|
2214 | nextch(); |
---|
2215 | while (ch >= 'A' && ch <= 'Z') { |
---|
2216 | compass_dat_flags |= BIT(ch - 'A'); |
---|
2217 | /* Flags we handle: |
---|
2218 | * L (exclude from length) |
---|
2219 | * S (splay) |
---|
2220 | * P (no plot) (mapped to FLAG_SURFACE) |
---|
2221 | * X (exclude data) |
---|
2222 | * FIXME: Defined flags we currently ignore: |
---|
2223 | * C (no adjustment) (set all (co)variances to 0? Then |
---|
2224 | * we need to handle a loop of such legs or a traverse |
---|
2225 | * of such legs between two fixed points...) |
---|
2226 | */ |
---|
2227 | nextch(); |
---|
2228 | } |
---|
2229 | if (ch == '#') { |
---|
2230 | nextch(); |
---|
2231 | } else { |
---|
2232 | compass_dat_flags = 0; |
---|
2233 | set_pos(&fp); |
---|
2234 | } |
---|
2235 | } else { |
---|
2236 | set_pos(&fp); |
---|
2237 | } |
---|
2238 | } |
---|
2239 | break; |
---|
2240 | case Ignore: |
---|
2241 | skipword(); break; |
---|
2242 | case IgnoreAllAndNewLine: |
---|
2243 | skipline(); |
---|
2244 | /* fall through */ |
---|
2245 | case Newline: |
---|
2246 | if (fr != NULL) { |
---|
2247 | int r; |
---|
2248 | int save_flags; |
---|
2249 | int implicit_splay; |
---|
2250 | if (fTopofil) { |
---|
2251 | VAL(Tape) = VAL(ToCount) - VAL(FrCount); |
---|
2252 | LOC(Tape) = LOC(ToCount); |
---|
2253 | WID(Tape) = WID(ToCount); |
---|
2254 | } |
---|
2255 | /* Note: frdepth == todepth test works regardless of fDepthChange |
---|
2256 | * (frdepth always zero, todepth is change of depth) and also |
---|
2257 | * works for STYLE_NORMAL (both remain 0) */ |
---|
2258 | if (TSTBIT(pcs->infer, INFER_EQUATES) && |
---|
2259 | (VAL(Tape) == (real)0.0 || VAL(Tape) == HUGE_REAL) && |
---|
2260 | (VAL(BackTape) == (real)0.0 || VAL(BackTape) == HUGE_REAL) && |
---|
2261 | VAL(FrDepth) == VAL(ToDepth)) { |
---|
2262 | if (!TSTBIT(pcs->infer, INFER_EQUATES_SELF_OK) || fr != to) |
---|
2263 | process_equate(fr, to); |
---|
2264 | goto inferred_equate; |
---|
2265 | } |
---|
2266 | if (fRev) { |
---|
2267 | prefix *t = fr; |
---|
2268 | fr = to; |
---|
2269 | to = t; |
---|
2270 | } |
---|
2271 | if (fTopofil) { |
---|
2272 | VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT]; |
---|
2273 | } else if (VAL(Tape) != HUGE_REAL) { |
---|
2274 | VAL(Tape) *= pcs->units[Q_LENGTH]; |
---|
2275 | VAL(Tape) -= pcs->z[Q_LENGTH]; |
---|
2276 | VAL(Tape) *= pcs->sc[Q_LENGTH]; |
---|
2277 | } |
---|
2278 | if (VAL(BackTape) != HUGE_REAL) { |
---|
2279 | VAL(BackTape) *= pcs->units[Q_BACKLENGTH]; |
---|
2280 | VAL(BackTape) -= pcs->z[Q_BACKLENGTH]; |
---|
2281 | VAL(BackTape) *= pcs->sc[Q_BACKLENGTH]; |
---|
2282 | if (VAL(Tape) != HUGE_REAL) { |
---|
2283 | real diff = VAL(Tape) - VAL(BackTape); |
---|
2284 | if (sqrd(diff / 3.0) > VAR(Tape) + VAR(BackTape)) { |
---|
2285 | /* fore and back readings differ by more than 3 sds */ |
---|
2286 | /* TRANSLATORS: %s is replaced by the amount the readings disagree |
---|
2287 | * by, e.g. "0.12m" or "0.2ft". */ |
---|
2288 | warn_readings_differ(/*TAPE reading and BACKTAPE reading disagree by %s*/97, |
---|
2289 | diff, get_length_units(Q_LENGTH), Tape, BackTape); |
---|
2290 | } |
---|
2291 | VAL(Tape) = VAL(Tape) / VAR(Tape) + VAL(BackTape) / VAR(BackTape); |
---|
2292 | VAR(Tape) = (VAR(Tape) + VAR(BackTape)) / 4; |
---|
2293 | VAL(Tape) *= VAR(Tape); |
---|
2294 | } else { |
---|
2295 | VAL(Tape) = VAL(BackTape); |
---|
2296 | VAR(Tape) = VAR(BackTape); |
---|
2297 | } |
---|
2298 | } else if (VAL(Tape) == HUGE_REAL) { |
---|
2299 | compile_diagnostic_reading(DIAG_ERR, Tape, /*Tape reading may not be omitted*/94); |
---|
2300 | goto inferred_equate; |
---|
2301 | } |
---|
2302 | implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY); |
---|
2303 | pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY)); |
---|
2304 | save_flags = pcs->flags; |
---|
2305 | if (implicit_splay) { |
---|
2306 | pcs->flags |= BIT(FLAGS_SPLAY); |
---|
2307 | } |
---|
2308 | switch (pcs->style) { |
---|
2309 | case STYLE_NORMAL: |
---|
2310 | r = process_normal(fr, to, (first_stn == To) ^ fRev, |
---|
2311 | ctype, backctype); |
---|
2312 | break; |
---|
2313 | case STYLE_DIVING: |
---|
2314 | /* FIXME: Handle any clino readings */ |
---|
2315 | r = process_diving(fr, to, (first_stn == To) ^ fRev, |
---|
2316 | fDepthChange); |
---|
2317 | break; |
---|
2318 | case STYLE_CYLPOLAR: |
---|
2319 | r = process_cylpolar(fr, to, (first_stn == To) ^ fRev, |
---|
2320 | fDepthChange); |
---|
2321 | break; |
---|
2322 | default: |
---|
2323 | r = 0; /* avoid warning */ |
---|
2324 | BUG("bad style"); |
---|
2325 | } |
---|
2326 | pcs->flags = save_flags; |
---|
2327 | if (!r) skipline(); |
---|
2328 | |
---|
2329 | /* Swap fr and to back to how they were for next line */ |
---|
2330 | if (fRev) { |
---|
2331 | prefix *t = fr; |
---|
2332 | fr = to; |
---|
2333 | to = t; |
---|
2334 | } |
---|
2335 | } |
---|
2336 | |
---|
2337 | fRev = false; |
---|
2338 | ctype = backctype = CTYPE_OMIT; |
---|
2339 | fDepthChange = false; |
---|
2340 | |
---|
2341 | /* ordering may omit clino reading, so set up default here */ |
---|
2342 | /* this is also used if clino reading is the omit character */ |
---|
2343 | VAL(Clino) = VAL(BackClino) = 0; |
---|
2344 | LOC(Clino) = LOC(BackClino) = -1; |
---|
2345 | WID(Clino) = WID(BackClino) = 0; |
---|
2346 | |
---|
2347 | inferred_equate: |
---|
2348 | |
---|
2349 | fMulti = true; |
---|
2350 | while (1) { |
---|
2351 | process_eol(); |
---|
2352 | skipblanks(); |
---|
2353 | if (isData(ch)) break; |
---|
2354 | if (!isComm(ch)) { |
---|
2355 | return; |
---|
2356 | } |
---|
2357 | } |
---|
2358 | break; |
---|
2359 | case IgnoreAll: |
---|
2360 | skipline(); |
---|
2361 | /* fall through */ |
---|
2362 | case End: |
---|
2363 | if (!fMulti) { |
---|
2364 | int save_flags; |
---|
2365 | int implicit_splay; |
---|
2366 | /* Compass ignore flag is 'X' */ |
---|
2367 | if ((compass_dat_flags & BIT('X' - 'A'))) { |
---|
2368 | process_eol(); |
---|
2369 | return; |
---|
2370 | } |
---|
2371 | if (fRev) { |
---|
2372 | prefix *t = fr; |
---|
2373 | fr = to; |
---|
2374 | to = t; |
---|
2375 | } |
---|
2376 | if (fTopofil) { |
---|
2377 | VAL(Tape) = VAL(ToCount) - VAL(FrCount); |
---|
2378 | LOC(Tape) = LOC(ToCount); |
---|
2379 | WID(Tape) = WID(ToCount); |
---|
2380 | } |
---|
2381 | /* Note: frdepth == todepth test works regardless of fDepthChange |
---|
2382 | * (frdepth always zero, todepth is change of depth) and also |
---|
2383 | * works for STYLE_NORMAL (both remain 0) */ |
---|
2384 | if (TSTBIT(pcs->infer, INFER_EQUATES) && |
---|
2385 | (VAL(Tape) == (real)0.0 || VAL(Tape) == HUGE_REAL) && |
---|
2386 | (VAL(BackTape) == (real)0.0 || VAL(BackTape) == HUGE_REAL) && |
---|
2387 | VAL(FrDepth) == VAL(ToDepth)) { |
---|
2388 | if (!TSTBIT(pcs->infer, INFER_EQUATES_SELF_OK) || fr != to) |
---|
2389 | process_equate(fr, to); |
---|
2390 | process_eol(); |
---|
2391 | return; |
---|
2392 | } |
---|
2393 | if (fTopofil) { |
---|
2394 | VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT]; |
---|
2395 | } else if (VAL(Tape) != HUGE_REAL) { |
---|
2396 | VAL(Tape) *= pcs->units[Q_LENGTH]; |
---|
2397 | VAL(Tape) -= pcs->z[Q_LENGTH]; |
---|
2398 | VAL(Tape) *= pcs->sc[Q_LENGTH]; |
---|
2399 | } |
---|
2400 | if (VAL(BackTape) != HUGE_REAL) { |
---|
2401 | VAL(BackTape) *= pcs->units[Q_BACKLENGTH]; |
---|
2402 | VAL(BackTape) -= pcs->z[Q_BACKLENGTH]; |
---|
2403 | VAL(BackTape) *= pcs->sc[Q_BACKLENGTH]; |
---|
2404 | if (VAL(Tape) != HUGE_REAL) { |
---|
2405 | real diff = VAL(Tape) - VAL(BackTape); |
---|
2406 | if (sqrd(diff / 3.0) > VAR(Tape) + VAR(BackTape)) { |
---|
2407 | /* fore and back readings differ by more than 3 sds */ |
---|
2408 | /* TRANSLATORS: %s is replaced by the amount the readings disagree |
---|
2409 | * by, e.g. "0.12m" or "0.2ft". */ |
---|
2410 | warn_readings_differ(/*TAPE reading and BACKTAPE reading disagree by %s*/97, |
---|
2411 | diff, get_length_units(Q_LENGTH), Tape, BackTape); |
---|
2412 | } |
---|
2413 | VAL(Tape) = VAL(Tape) / VAR(Tape) + VAL(BackTape) / VAR(BackTape); |
---|
2414 | VAR(Tape) = (VAR(Tape) + VAR(BackTape)) / 4; |
---|
2415 | VAL(Tape) *= VAR(Tape); |
---|
2416 | } else { |
---|
2417 | VAL(Tape) = VAL(BackTape); |
---|
2418 | VAR(Tape) = VAR(BackTape); |
---|
2419 | } |
---|
2420 | } else if (VAL(Tape) == HUGE_REAL) { |
---|
2421 | compile_diagnostic_reading(DIAG_ERR, Tape, /*Tape reading may not be omitted*/94); |
---|
2422 | process_eol(); |
---|
2423 | return; |
---|
2424 | } |
---|
2425 | implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY); |
---|
2426 | pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY)); |
---|
2427 | save_flags = pcs->flags; |
---|
2428 | if (implicit_splay) { |
---|
2429 | pcs->flags |= BIT(FLAGS_SPLAY); |
---|
2430 | } |
---|
2431 | if ((compass_dat_flags & BIT('S' - 'A'))) { |
---|
2432 | /* 'S' means "splay". */ |
---|
2433 | pcs->flags |= BIT(FLAGS_SPLAY); |
---|
2434 | } |
---|
2435 | if ((compass_dat_flags & BIT('P' - 'A'))) { |
---|
2436 | /* 'P' means "Exclude this shot from plotting", but the use |
---|
2437 | * suggested in the Compass docs is for surface data, and legs |
---|
2438 | * with this flag "[do] not support passage modeling". |
---|
2439 | * |
---|
2440 | * Even if it's actually being used for a different |
---|
2441 | * purpose, Survex programs don't show surface legs |
---|
2442 | * by default so FLAGS_SURFACE matches fairly well. |
---|
2443 | */ |
---|
2444 | pcs->flags |= BIT(FLAGS_SURFACE); |
---|
2445 | } |
---|
2446 | if ((compass_dat_flags & BIT('L' - 'A'))) { |
---|
2447 | /* 'L' means "exclude from length" - map this to Survex's |
---|
2448 | * FLAGS_DUPLICATE. */ |
---|
2449 | pcs->flags |= BIT(FLAGS_DUPLICATE); |
---|
2450 | } |
---|
2451 | switch (pcs->style) { |
---|
2452 | case STYLE_NORMAL: |
---|
2453 | process_normal(fr, to, (first_stn == To) ^ fRev, |
---|
2454 | ctype, backctype); |
---|
2455 | break; |
---|
2456 | case STYLE_DIVING: |
---|
2457 | /* FIXME: Handle any clino readings */ |
---|
2458 | process_diving(fr, to, (first_stn == To) ^ fRev, |
---|
2459 | fDepthChange); |
---|
2460 | break; |
---|
2461 | case STYLE_CYLPOLAR: |
---|
2462 | process_cylpolar(fr, to, (first_stn == To) ^ fRev, |
---|
2463 | fDepthChange); |
---|
2464 | break; |
---|
2465 | default: |
---|
2466 | BUG("bad style"); |
---|
2467 | } |
---|
2468 | pcs->flags = save_flags; |
---|
2469 | |
---|
2470 | process_eol(); |
---|
2471 | return; |
---|
2472 | } |
---|
2473 | do { |
---|
2474 | process_eol(); |
---|
2475 | skipblanks(); |
---|
2476 | } while (isComm(ch)); |
---|
2477 | goto again; |
---|
2478 | default: |
---|
2479 | BUG("Unknown reading in ordering"); |
---|
2480 | } |
---|
2481 | } |
---|
2482 | } |
---|
2483 | |
---|
2484 | static int |
---|
2485 | process_lrud(prefix *stn) |
---|
2486 | { |
---|
2487 | SVX_ASSERT(next_lrud); |
---|
2488 | lrud * xsect = osnew(lrud); |
---|
2489 | xsect->stn = stn; |
---|
2490 | xsect->l = (VAL(Left) * pcs->units[Q_LEFT] - pcs->z[Q_LEFT]) * pcs->sc[Q_LEFT]; |
---|
2491 | xsect->r = (VAL(Right) * pcs->units[Q_RIGHT] - pcs->z[Q_RIGHT]) * pcs->sc[Q_RIGHT]; |
---|
2492 | xsect->u = (VAL(Up) * pcs->units[Q_UP] - pcs->z[Q_UP]) * pcs->sc[Q_UP]; |
---|
2493 | xsect->d = (VAL(Down) * pcs->units[Q_DOWN] - pcs->z[Q_DOWN]) * pcs->sc[Q_DOWN]; |
---|
2494 | xsect->meta = pcs->meta; |
---|
2495 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
2496 | xsect->next = NULL; |
---|
2497 | *next_lrud = xsect; |
---|
2498 | next_lrud = &(xsect->next); |
---|
2499 | |
---|
2500 | return 1; |
---|
2501 | } |
---|
2502 | |
---|
2503 | static void |
---|
2504 | data_passage(void) |
---|
2505 | { |
---|
2506 | prefix *stn = NULL; |
---|
2507 | const reading *ordering; |
---|
2508 | |
---|
2509 | for (ordering = pcs->ordering ; ; ordering++) { |
---|
2510 | skipblanks(); |
---|
2511 | switch (*ordering) { |
---|
2512 | case Station: |
---|
2513 | stn = read_prefix(PFX_STATION); |
---|
2514 | break; |
---|
2515 | case Left: case Right: case Up: case Down: { |
---|
2516 | reading r = *ordering; |
---|
2517 | read_reading(r, true); |
---|
2518 | if (VAL(r) == HUGE_REAL) { |
---|
2519 | if (!isOmit(ch)) { |
---|
2520 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
2521 | } else { |
---|
2522 | nextch(); |
---|
2523 | } |
---|
2524 | VAL(r) = -1; |
---|
2525 | } |
---|
2526 | break; |
---|
2527 | } |
---|
2528 | case Ignore: |
---|
2529 | skipword(); break; |
---|
2530 | case IgnoreAll: |
---|
2531 | skipline(); |
---|
2532 | /* fall through */ |
---|
2533 | case End: { |
---|
2534 | process_lrud(stn); |
---|
2535 | process_eol(); |
---|
2536 | return; |
---|
2537 | } |
---|
2538 | default: BUG("Unknown reading in ordering"); |
---|
2539 | } |
---|
2540 | } |
---|
2541 | } |
---|
2542 | |
---|
2543 | static int |
---|
2544 | process_nosurvey(prefix *fr, prefix *to, bool fToFirst) |
---|
2545 | { |
---|
2546 | nosurveylink *link; |
---|
2547 | |
---|
2548 | /* Suppress "unused fixed point" warnings for these stations */ |
---|
2549 | fr->sflags |= BIT(SFLAGS_USED); |
---|
2550 | to->sflags |= BIT(SFLAGS_USED); |
---|
2551 | |
---|
2552 | /* add to linked list which is dealt with after network is solved */ |
---|
2553 | link = osnew(nosurveylink); |
---|
2554 | if (fToFirst) { |
---|
2555 | link->to = StnFromPfx(to); |
---|
2556 | link->fr = StnFromPfx(fr); |
---|
2557 | } else { |
---|
2558 | link->fr = StnFromPfx(fr); |
---|
2559 | link->to = StnFromPfx(to); |
---|
2560 | } |
---|
2561 | link->flags = pcs->flags | (STYLE_NOSURVEY << FLAGS_STYLE_BIT0); |
---|
2562 | link->meta = pcs->meta; |
---|
2563 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
2564 | link->next = nosurveyhead; |
---|
2565 | nosurveyhead = link; |
---|
2566 | return 1; |
---|
2567 | } |
---|
2568 | |
---|
2569 | static void |
---|
2570 | data_nosurvey(void) |
---|
2571 | { |
---|
2572 | prefix *fr = NULL, *to = NULL; |
---|
2573 | |
---|
2574 | bool fMulti = false; |
---|
2575 | |
---|
2576 | reading first_stn = End; |
---|
2577 | |
---|
2578 | const reading *ordering; |
---|
2579 | |
---|
2580 | again: |
---|
2581 | |
---|
2582 | for (ordering = pcs->ordering ; ; ordering++) { |
---|
2583 | skipblanks(); |
---|
2584 | switch (*ordering) { |
---|
2585 | case Fr: |
---|
2586 | fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT); |
---|
2587 | if (first_stn == End) first_stn = Fr; |
---|
2588 | break; |
---|
2589 | case To: |
---|
2590 | to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT); |
---|
2591 | if (first_stn == End) first_stn = To; |
---|
2592 | break; |
---|
2593 | case Station: |
---|
2594 | fr = to; |
---|
2595 | to = read_prefix(PFX_STATION); |
---|
2596 | first_stn = To; |
---|
2597 | break; |
---|
2598 | case Ignore: |
---|
2599 | skipword(); break; |
---|
2600 | case IgnoreAllAndNewLine: |
---|
2601 | skipline(); |
---|
2602 | /* fall through */ |
---|
2603 | case Newline: |
---|
2604 | if (fr != NULL) { |
---|
2605 | if (!process_nosurvey(fr, to, first_stn == To)) |
---|
2606 | skipline(); |
---|
2607 | } |
---|
2608 | if (ordering[1] == End) { |
---|
2609 | do { |
---|
2610 | process_eol(); |
---|
2611 | skipblanks(); |
---|
2612 | } while (isComm(ch)); |
---|
2613 | if (!isData(ch)) { |
---|
2614 | return; |
---|
2615 | } |
---|
2616 | goto again; |
---|
2617 | } |
---|
2618 | fMulti = true; |
---|
2619 | while (1) { |
---|
2620 | process_eol(); |
---|
2621 | skipblanks(); |
---|
2622 | if (isData(ch)) break; |
---|
2623 | if (!isComm(ch)) { |
---|
2624 | return; |
---|
2625 | } |
---|
2626 | } |
---|
2627 | break; |
---|
2628 | case IgnoreAll: |
---|
2629 | skipline(); |
---|
2630 | /* fall through */ |
---|
2631 | case End: |
---|
2632 | if (!fMulti) { |
---|
2633 | (void)process_nosurvey(fr, to, first_stn == To); |
---|
2634 | process_eol(); |
---|
2635 | return; |
---|
2636 | } |
---|
2637 | do { |
---|
2638 | process_eol(); |
---|
2639 | skipblanks(); |
---|
2640 | } while (isComm(ch)); |
---|
2641 | goto again; |
---|
2642 | default: BUG("Unknown reading in ordering"); |
---|
2643 | } |
---|
2644 | } |
---|
2645 | } |
---|
2646 | |
---|
2647 | /* totally ignore a line of survey data */ |
---|
2648 | static void |
---|
2649 | data_ignore(void) |
---|
2650 | { |
---|
2651 | skipline(); |
---|
2652 | process_eol(); |
---|
2653 | } |
---|