source: git/src/datain.c @ e9ea53b

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since e9ea53b was e9ea53b, checked in by Olly Betts <olly@…>, 11 years ago

src/datain.c: Set pcs->infer for Compass DAT in terms of INFER_*
constants rather than to the literal 7.

  • Property mode set to 100644
File size: 49.3 KB
Line 
1/* datain.c
2 * Reads in survey files, dealing with special characters, keywords & data
3 * Copyright (C) 1991-2003,2005,2009,2010,2011,2012,2013,2014 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 "filename.h"
32#include "message.h"
33#include "filelist.h"
34#include "netbits.h"
35#include "netskel.h"
36#include "readval.h"
37#include "datain.h"
38#include "commands.h"
39#include "out.h"
40#include "str.h"
41
42#define EPSILON (REAL_EPSILON * 1000)
43
44#define var(I) (pcs->Var[(I)])
45
46/* true if x is not-a-number value in Compass (999.0 or -999.0)    */
47/* Compass uses 999.0 but understands Karst data which used -999.0 */
48#define is_compass_NaN(x) ( fabs(fabs(x)-999.0) <  EPSILON )
49
50int ch;
51
52typedef enum {
53    CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_INFERPLUMB, CTYPE_HORIZ
54} clino_type;
55
56/* Don't explicitly initialise as we can't set the jmp_buf - this has
57 * static scope so will be initialised like this anyway */
58parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
59
60bool f_export_ok;
61
62static real value[Fr - 1];
63#define VAL(N) value[(N)-1]
64static real variance[Fr - 1];
65#define VAR(N) variance[(N)-1]
66
67/* style functions */
68static void data_normal(void);
69static void data_cartesian(void);
70static void data_passage(void);
71static void data_nosurvey(void);
72static void data_ignore(void);
73
74void
75get_pos(filepos *fp)
76{
77   fp->ch = ch;
78   fp->offset = ftell(file.fh);
79   if (fp->offset == -1)
80      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
81}
82
83void
84set_pos(const filepos *fp)
85{
86   ch = fp->ch;
87   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
88      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
89}
90
91static void
92push_back(int c)
93{
94   if (c != EOF && ungetc(c, file.fh) == EOF)
95      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
96}
97
98static void
99report_parent(parse * p) {
100    if (p->parent)
101        report_parent(p->parent);
102    /* Force re-report of include tree for further errors in
103     * parent files */
104    p->reported_where = fFalse;
105    /* TRANSLATORS: %s is replaced by the filename of the parent file, and %u
106     * by the line number in that file. */
107    fprintf(STDERR, msg(/*In file included from %s:%u:\n*/5), p->filename, p->line);
108}
109
110static void
111error_list_parent_files(void)
112{
113   if (!file.reported_where && file.parent) {
114      report_parent(file.parent);
115      /* Suppress reporting of full include tree for further errors
116       * in this file */
117      file.reported_where = fTrue;
118   }
119}
120
121void
122compile_error(int en, ...)
123{
124   int col = 0;
125   va_list ap;
126   va_start(ap, en);
127   error_list_parent_files();
128   if (en < 0) {
129      en = -en;
130      col = ftell(file.fh) - file.lpos;
131   }
132   v_report(1, file.filename, file.line, col, en, ap);
133   va_end(ap);
134}
135
136void
137compile_error_skip(int en, ...)
138{
139   int col = 0;
140   va_list ap;
141   va_start(ap, en);
142   error_list_parent_files();
143   if (en < 0) {
144      en = -en;
145      col = ftell(file.fh) - file.lpos;
146   }
147   v_report(1, file.filename, file.line, col, en, ap);
148   va_end(ap);
149   skipline();
150}
151
152void
153compile_error_token(int en)
154{
155   char *p = NULL;
156   int len = 0;
157   skipblanks();
158   while (!isBlank(ch) && !isEol(ch)) {
159      s_catchar(&p, &len, (char)ch);
160      nextch();
161   }
162   compile_error(en, p ? p : "");
163   osfree(p);
164}
165
166void
167compile_warning(int en, ...)
168{
169   int col = 0;
170   va_list ap;
171   va_start(ap, en);
172   error_list_parent_files();
173   if (en < 0) {
174      en = -en;
175      col = ftell(file.fh) - file.lpos;
176   }
177   v_report(0, file.filename, file.line, col, en, ap);
178   va_end(ap);
179}
180
181/* This function makes a note where to put output files */
182static void
183using_data_file(const char *fnm)
184{
185   if (!fnm_output_base) {
186      /* was: fnm_output_base = base_from_fnm(fnm); */
187      fnm_output_base = baseleaf_from_fnm(fnm);
188   } else if (fnm_output_base_is_dir) {
189      /* --output pointed to directory so use the leaf basename in that dir */
190      char *lf, *p;
191      lf = baseleaf_from_fnm(fnm);
192      p = use_path(fnm_output_base, lf);
193      osfree(lf);
194      osfree(fnm_output_base);
195      fnm_output_base = p;
196      fnm_output_base_is_dir = 0;
197   }
198}
199
200static void
201skipword(void)
202{
203   while (!isBlank(ch) && !isEol(ch)) nextch();
204}
205
206extern void
207skipblanks(void)
208{
209   while (isBlank(ch)) nextch();
210}
211
212extern void
213skipline(void)
214{
215   while (!isEol(ch)) nextch();
216}
217
218#ifndef NO_PERCENTAGE
219static long int filelen;
220#endif
221
222static void
223process_bol(void)
224{
225#ifndef NO_PERCENTAGE
226   /* print %age of file done */
227   if (filelen > 0) {
228      filepos fp;
229      get_pos(&fp);
230      printf("%d%%\r", (int)(100 * fp.offset / filelen));
231   }
232#endif
233
234   nextch();
235   skipblanks();
236}
237
238static void
239process_eol(void)
240{
241   int eolchar;
242
243   skipblanks();
244
245   if (!isEol(ch)) {
246      if (!isComm(ch)) compile_error(-/*End of line not blank*/15);
247      skipline();
248   }
249
250   eolchar = ch;
251   file.line++;
252   /* skip any different eol characters so we get line counts correct on
253    * DOS text files and similar, but don't count several adjacent blank
254    * lines as one */
255   while (ch != EOF) {
256      nextch();
257      if (ch == eolchar || !isEol(ch)) {
258         push_back(ch);
259         break;
260      }
261   }
262   file.lpos = ftell(file.fh);
263}
264
265static bool
266process_non_data_line(void)
267{
268   process_bol();
269
270   if (isData(ch)) return fFalse;
271
272   if (isKeywd(ch)) {
273      nextch();
274      handle_command();
275   }
276
277   process_eol();
278
279   return fTrue;
280}
281
282static void
283read_reading(reading r, bool f_optional)
284{
285   int n_readings;
286   q_quantity q;
287   switch (r) {
288      case Tape: q = Q_LENGTH; break;
289      case Comp: q = Q_BEARING; break;
290      case BackComp: q = Q_BACKBEARING; break;
291      case Clino: q = Q_GRADIENT; break;
292      case BackClino: q = Q_BACKGRADIENT; break;
293      case FrDepth: case ToDepth: q = Q_DEPTH; break;
294      case Dx: q = Q_DX; break;
295      case Dy: q = Q_DY; break;
296      case Dz: q = Q_DZ; break;
297      case FrCount: case ToCount: q = Q_COUNT; break;
298      case Left: q = Q_LEFT; break;
299      case Right: q = Q_RIGHT; break;
300      case Up: q = Q_UP; break;
301      case Down: q = Q_DOWN; break;
302      default:
303        q = Q_NULL; /* Suppress compiler warning */;
304        BUG("Unexpected case");
305   }
306   VAL(r) = read_numeric(f_optional, &n_readings);
307   VAR(r) = var(q);
308   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
309}
310
311static void
312read_bearing_or_omit(reading r)
313{
314   int n_readings;
315   q_quantity q = Q_NULL;
316   VAL(r) = read_numeric_or_omit(&n_readings);
317   switch (r) {
318      case Comp: q = Q_BEARING; break;
319      case BackComp: q = Q_BACKBEARING; break;
320      default:
321        q = Q_NULL; /* Suppress compiler warning */;
322        BUG("Unexpected case");
323   }
324   VAR(r) = var(q);
325   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
326}
327
328/* For reading Compass MAK files which have a freeform syntax */
329static void
330nextch_handling_eol(void)
331{
332   nextch();
333   while (ch != EOF && isEol(ch)) {
334      process_eol();
335      nextch();
336   }
337}
338
339#define LITLEN(S) (sizeof(S"") - 1)
340#define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
341                        (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
342                        strcasecmp((F) + (L) - LITLEN(E), E) == 0)
343extern void
344data_file(const char *pth, const char *fnm)
345{
346   int begin_lineno_store;
347   parse file_store;
348   volatile enum {FMT_SVX, FMT_DAT, FMT_MAK} fmt = FMT_SVX;
349
350   {
351      char *filename;
352      FILE *fh;
353      size_t len;
354
355      if (!pth) {
356         /* file specified on command line - don't do special translation */
357         fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
358      } else {
359         fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
360      }
361
362      if (fh == NULL) {
363         compile_error(/*Couldn’t open data file “%s”*/24, fnm);
364         return;
365      }
366
367      len = strlen(filename);
368      if (has_ext(filename, len, "dat")) {
369         fmt = FMT_DAT;
370      } else if (has_ext(filename, len, "mak")) {
371         fmt = FMT_MAK;
372      }
373
374      file_store = file;
375      if (file.fh) file.parent = &file_store;
376      file.fh = fh;
377      file.filename = filename;
378      file.line = 1;
379      file.lpos = 0;
380      file.reported_where = fFalse;
381   }
382
383   if (fPercent) printf("%s:\n", fnm);
384
385   using_data_file(file.filename);
386
387   begin_lineno_store = pcs->begin_lineno;
388   pcs->begin_lineno = 0;
389
390#ifndef NO_PERCENTAGE
391   /* Try to find how long the file is...
392    * However, under ANSI fseek( ..., SEEK_END) may not be supported */
393   filelen = 0;
394   if (fPercent) {
395      if (fseek(file.fh, 0l, SEEK_END) == 0) {
396         filepos fp;
397         get_pos(&fp);
398         filelen = fp.offset;
399      }
400      rewind(file.fh); /* reset file ptr to start & clear any error state */
401   }
402#endif
403
404   if (fmt == FMT_DAT) {
405      short *t;
406      int i;
407      settings *pcsNew;
408
409      pcsNew = osnew(settings);
410      *pcsNew = *pcs; /* copy contents */
411      pcsNew->begin_lineno = 0;
412      pcsNew->next = pcs;
413      pcs = pcsNew;
414      default_units(pcs);
415      default_calib(pcs);
416
417      pcs->style = STYLE_NORMAL;
418      pcs->units[Q_LENGTH] = METRES_PER_FOOT;
419      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
420
421      t[EOF] = SPECIAL_EOL;
422      memset(t, 0, sizeof(short) * 33);
423      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
424      t[127] = 0;
425      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
426      t['\t'] |= SPECIAL_BLANK;
427      t[' '] |= SPECIAL_BLANK;
428      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
429      t['\n'] |= SPECIAL_EOL;
430      t['\r'] |= SPECIAL_EOL;
431      t['.'] |= SPECIAL_DECIMAL;
432      t['-'] |= SPECIAL_MINUS;
433      t['+'] |= SPECIAL_PLUS;
434      pcs->Translate = t;
435      pcs->Case = OFF;
436      pcs->Truncate = INT_MAX;
437      pcs->infer = BIT(INFER_EQUATES)|BIT(INFER_EXPORTS)|BIT(INFER_PLUMBS);
438   } else if (fmt == FMT_MAK) {
439      short *t;
440      int i;
441      settings *pcsNew;
442
443      pcsNew = osnew(settings);
444      *pcsNew = *pcs; /* copy contents */
445      pcsNew->begin_lineno = 0;
446      pcsNew->next = pcs;
447      pcs = pcsNew;
448
449      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
450
451      t[EOF] = SPECIAL_EOL;
452      memset(t, 0, sizeof(short) * 33);
453      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
454      t[127] = 0;
455      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
456      t['['] = t[','] = t[';'] = 0;
457      t['\t'] |= SPECIAL_BLANK;
458      t[' '] |= SPECIAL_BLANK;
459      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
460      t['\n'] |= SPECIAL_EOL;
461      t['\r'] |= SPECIAL_EOL;
462      t['.'] |= SPECIAL_DECIMAL;
463      t['-'] |= SPECIAL_MINUS;
464      t['+'] |= SPECIAL_PLUS;
465      pcs->Translate = t;
466      pcs->Case = OFF;
467      pcs->Truncate = INT_MAX;
468   }
469
470#ifdef HAVE_SETJMP_H
471   /* errors in nested functions can longjmp here */
472   if (setjmp(file.jbSkipLine)) {
473      skipline();
474      process_eol();
475   }
476#endif
477
478   if (fmt == FMT_DAT) {
479      while (!feof(file.fh) && !ferror(file.fh)) {
480         static reading compass_order[] = {
481            Fr, To, Tape, CompassDATComp, CompassDATClino,
482            CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
483            CompassDATFlags, IgnoreAll
484         };
485         static reading compass_order_backsights[] = {
486            Fr, To, Tape, CompassDATComp, CompassDATClino,
487            CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
488            CompassDATBackComp, CompassDATBackClino,
489            CompassDATFlags, IgnoreAll
490         };
491         /* <Cave name> */
492         process_bol();
493         skipline();
494         process_eol();
495         /* SURVEY NAME: <Short name> */
496         get_token();
497         get_token();
498         /* if (ch != ':') ... */
499         nextch();
500         get_token();
501         skipline();
502         process_eol();
503         /* SURVEY DATE: 7 10 79  COMMENT:<Long name> */
504         /* Note: Larry says a 2 digit year is always 19XX */
505         /* NB order is *month* *day* year */
506         get_token();
507         get_token();
508         copy_on_write_meta(pcs);
509         if (ch == ':') {
510             int year, month, day;
511
512             nextch();
513
514             month = read_uint();
515             day = read_uint();
516             year = read_uint();
517             /* Note: Larry says a 2 digit year is always 19XX */
518             if (year < 100) year += 1900;
519
520             pcs->meta->days1 = pcs->meta->days2 = days_since_1900(year, month, day);
521         } else {
522             pcs->meta->days1 = pcs->meta->days2 = -1;
523         }
524         skipline();
525         process_eol();
526         /* SURVEY TEAM: */
527         get_token();
528         get_token();
529         skipline();
530         process_eol();
531         /* <Survey team> */
532         nextch();
533         skipline();
534         process_eol();
535         /* DECLINATION: 1.00  FORMAT: DDDDLUDRADLN  CORRECTIONS: 2.00 3.00 4.00 */
536         get_token();
537         nextch(); /* : */
538         skipblanks();
539         pcs->z[Q_DECLINATION] = -read_numeric(fFalse, NULL);
540         pcs->z[Q_DECLINATION] *= pcs->units[Q_DECLINATION];
541         get_token();
542         pcs->ordering = compass_order;
543         if (strcmp(buffer, "FORMAT") == 0) {
544            nextch(); /* : */
545            get_token();
546            if (strlen(buffer) >= 12 && buffer[11] == 'B') {
547               /* We have backsights for compass and clino */
548               pcs->ordering = compass_order_backsights;
549            }
550            get_token();
551         }
552         if (strcmp(buffer, "CORRECTIONS") == 0) {
553            nextch(); /* : */
554            pcs->z[Q_BEARING] = -rad(read_numeric(fFalse, NULL));
555            pcs->z[Q_GRADIENT] = -rad(read_numeric(fFalse, NULL));
556            pcs->z[Q_LENGTH] = -read_numeric(fFalse, NULL);
557         } else {
558            pcs->z[Q_BEARING] = 0;
559            pcs->z[Q_GRADIENT] = 0;
560            pcs->z[Q_LENGTH] = 0;
561         }
562         skipline();
563         process_eol();
564         /* BLANK LINE */
565         process_bol();
566         skipline();
567         process_eol();
568         /* heading line */
569         process_bol();
570         skipline();
571         process_eol();
572         /* BLANK LINE */
573         process_bol();
574         skipline();
575         process_eol();
576         while (!feof(file.fh)) {
577            process_bol();
578            if (ch == '\x0c') {
579               nextch();
580               process_eol();
581               break;
582            }
583            data_normal();
584         }
585      }
586      {
587         settings *pcsParent = pcs->next;
588         SVX_ASSERT(pcsParent);
589         pcs->ordering = NULL;
590         free_settings(pcs);
591         pcs = pcsParent;
592      }
593   } else if (fmt == FMT_MAK) {
594      nextch_handling_eol();
595      while (!feof(file.fh) && !ferror(file.fh)) {
596         if (ch == '#') {
597            /* include a file */
598            int ch_store;
599            char *dat_pth = path_from_fnm(file.filename);
600            char *dat_fnm = NULL;
601            int dat_fnm_len;
602            nextch_handling_eol();
603            while (ch != ',' && ch != ';' && ch != EOF) {
604               while (isEol(ch)) process_eol();
605               s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
606               nextch_handling_eol();
607            }
608            while (ch != ';' && ch != EOF) {
609               prefix *name;
610               nextch_handling_eol();
611               name = read_prefix(PFX_STATION|PFX_OPT);
612               if (name) {
613                  skipblanks();
614                  if (ch == '[') {
615                     /* fixed pt */
616                     node *stn;
617                     real x, y, z;
618                     name->sflags |= BIT(SFLAGS_FIXED);
619                     nextch_handling_eol();
620                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
621                            ch != '.' && ch != ']' && ch != EOF) {
622                        nextch_handling_eol();
623                     }
624                     x = read_numeric(fFalse, NULL);
625                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
626                            ch != '.' && ch != ']' && ch != EOF) {
627                        nextch_handling_eol();
628                     }
629                     y = read_numeric(fFalse, NULL);
630                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
631                            ch != '.' && ch != ']' && ch != EOF) {
632                        nextch_handling_eol();
633                     }
634                     z = read_numeric(fFalse, NULL);
635                     stn = StnFromPfx(name);
636                     if (!fixed(stn)) {
637                        POS(stn, 0) = x;
638                        POS(stn, 1) = y;
639                        POS(stn, 2) = z;
640                        fix(stn);
641                     } else {
642                        if (x != POS(stn, 0) || y != POS(stn, 1) ||
643                            z != POS(stn, 2)) {
644                           compile_error(/*Station already fixed or equated to a fixed point*/46);
645                        } else {
646                           compile_warning(/*Station already fixed at the same coordinates*/55);
647                        }
648                     }
649                     while (ch != ']' && ch != EOF) nextch_handling_eol();
650                     if (ch == ']') {
651                        nextch_handling_eol();
652                        skipblanks();
653                     }
654                  } else {
655                     /* FIXME: link station - ignore for now */
656                     /* FIXME: perhaps issue warning? */
657                  }
658                  while (ch != ',' && ch != ';' && ch != EOF)
659                     nextch_handling_eol();
660               }
661            }
662            if (dat_fnm) {
663               ch_store = ch;
664               data_file(dat_pth, dat_fnm);
665               ch = ch_store;
666               osfree(dat_fnm);
667            }
668         } else {
669            /* FIXME: also check for % and $ later */
670            nextch_handling_eol();
671         }
672      }
673      {
674         settings *pcsParent = pcs->next;
675         SVX_ASSERT(pcsParent);
676         free_settings(pcs);
677         pcs = pcsParent;
678      }
679   } else {
680      while (!feof(file.fh) && !ferror(file.fh)) {
681         if (!process_non_data_line()) {
682            f_export_ok = fFalse;
683            switch (pcs->style) {
684             case STYLE_NORMAL:
685             case STYLE_DIVING:
686             case STYLE_CYLPOLAR:
687               data_normal();
688               break;
689             case STYLE_CARTESIAN:
690               data_cartesian();
691               break;
692             case STYLE_PASSAGE:
693               data_passage();
694               break;
695             case STYLE_NOSURVEY:
696               data_nosurvey();
697               break;
698             case STYLE_IGNORE:
699               data_ignore();
700               break;
701             default:
702               BUG("bad style");
703            }
704         }
705      }
706   }
707
708   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
709    * including file */
710   f_export_ok = fFalse;
711
712#ifndef NO_PERCENTAGE
713   if (fPercent) putnl();
714#endif
715
716   if (pcs->begin_lineno) {
717      error_in_file(file.filename, pcs->begin_lineno,
718                    /*BEGIN with no matching END in this file*/23);
719      /* Implicitly close any unclosed BEGINs from this file */
720      do {
721         settings *pcsParent = pcs->next;
722         SVX_ASSERT(pcsParent);
723         free_settings(pcs);
724         pcs = pcsParent;
725      } while (pcs->begin_lineno);
726   }
727
728   pcs->begin_lineno = begin_lineno_store;
729
730   if (ferror(file.fh))
731      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
732
733   (void)fclose(file.fh);
734
735   file = file_store;
736
737   /* don't free this - it may be pointed to by prefix.file */
738   /* osfree(file.filename); */
739}
740
741static real
742mod2pi(real a)
743{
744   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
745}
746
747static real
748handle_plumb(clino_type *p_ctype)
749{
750   typedef enum {
751      CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
752   } clino_tok;
753   static sztok clino_tab[] = {
754      {"D",     CLINO_DOWN},
755      {"DOWN",  CLINO_DOWN},
756      {"H",     CLINO_LEVEL},
757      {"LEVEL", CLINO_LEVEL},
758      {"U",     CLINO_UP},
759      {"UP",    CLINO_UP},
760      {NULL,    CLINO_NULL}
761   };
762   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
763   clino_tok tok;
764
765   skipblanks();
766   if (isalpha(ch)) {
767      filepos fp;
768      get_pos(&fp);
769      get_token();
770      tok = match_tok(clino_tab, TABSIZE(clino_tab));
771      if (tok != CLINO_NULL) {
772         *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
773         return clinos[tok];
774      }
775      set_pos(&fp);
776   } else if (isSign(ch)) {
777      int chOld = ch;
778      nextch();
779      if (toupper(ch) == 'V') {
780         nextch();
781         *p_ctype = CTYPE_PLUMB;
782         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
783      }
784
785      if (isOmit(chOld)) {
786         *p_ctype = CTYPE_OMIT;
787         /* no clino reading, so assume 0 with large sd */
788         return (real)0.0;
789      }
790   } else if (isOmit(ch)) {
791      /* OMIT char may not be a SIGN char too so we need to check here as
792       * well as above... */
793      nextch();
794      *p_ctype = CTYPE_OMIT;
795      /* no clino reading, so assume 0 with large sd */
796      return (real)0.0;
797   }
798   return HUGE_REAL;
799}
800
801static void
802warn_readings_differ(int msgno, real diff)
803{
804   char buf[64];
805   char *p;
806   sprintf(buf, "%.2f", deg(fabs(diff)));
807   p = strchr(buf, '.');
808   if (p) {
809      char *z = p;
810      while (*++p) {
811         if (*p != '0') z = p + 1;
812      }
813      if (*z) *z = '\0';
814   }
815   compile_warning(msgno, buf);
816}
817
818static bool
819handle_comp_units(void)
820{
821   bool fNoComp = fTrue;
822   if (VAL(Comp) != HUGE_REAL) {
823      fNoComp = fFalse;
824      VAL(Comp) *= pcs->units[Q_BEARING];
825      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
826         /* TRANSLATORS: Suspicious means something like 410 degrees or -20
827          * degrees */
828         compile_warning(/*Suspicious compass reading*/59);
829         VAL(Comp) = mod2pi(VAL(Comp));
830      }
831   }
832   if (VAL(BackComp) != HUGE_REAL) {
833      fNoComp = fFalse;
834      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
835      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
836         /* FIXME: different message for BackComp? */
837         compile_warning(/*Suspicious compass reading*/59);
838         VAL(BackComp) = mod2pi(VAL(BackComp));
839      }
840   }
841   return fNoComp;
842}
843
844static real
845handle_compass(real *p_var)
846{
847   real compvar = VAR(Comp);
848   real comp = VAL(Comp);
849   real backcomp = VAL(BackComp);
850   if (comp != HUGE_REAL) {
851      comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
852      comp -= pcs->z[Q_DECLINATION];
853   }
854   if (backcomp != HUGE_REAL) {
855      backcomp = (backcomp - pcs->z[Q_BACKBEARING])
856              * pcs->sc[Q_BACKBEARING];
857      backcomp -= pcs->z[Q_DECLINATION];
858      backcomp -= M_PI;
859      if (comp != HUGE_REAL) {
860         real diff = comp - backcomp;
861         real adj = fabs(diff) > M_PI ? M_PI : 0;
862         diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
863         if (sqrd(diff / 2.0) > compvar + VAR(Q_BACKBEARING)) {
864            /* fore and back readings differ by more than 2 sds */
865            /* TRANSLATORS: Degrees are the angular measurement where there are
866             * 360 in a full circle. */
867            warn_readings_differ(/*COMPASS reading and BACKCOMPASS reading disagree by %s degrees*/98, diff);
868         }
869         comp = (comp / compvar + backcomp / VAR(BackComp));
870         compvar = (compvar + VAR(BackComp)) / 4;
871         comp *= compvar;
872         comp += adj;
873      } else {
874         comp = backcomp;
875         compvar = VAR(BackComp);
876      }
877   }
878   *p_var = compvar;
879   return comp;
880}
881
882static int
883process_normal(prefix *fr, prefix *to, bool fToFirst,
884               clino_type ctype, clino_type backctype)
885{
886   real tape = VAL(Tape);
887   real clin = VAL(Clino);
888   real backclin = VAL(BackClino);
889
890   real dx, dy, dz;
891   real vx, vy, vz;
892#ifndef NO_COVARIANCES
893   real cxy, cyz, czx;
894#endif
895
896   bool fNoComp;
897
898   /* adjusted tape is negative -- probably the calibration is wrong */
899   if (tape < (real)0.0) {
900      /* TRANSLATE different message for topofil? */
901      compile_warning(/*Negative adjusted tape reading*/79);
902   }
903
904   fNoComp = handle_comp_units();
905
906   if (ctype == CTYPE_READING) {
907      bool range_0_180;
908      real z;
909      real diff_from_abs90;
910      clin *= pcs->units[Q_GRADIENT];
911      /* percentage scale */
912      if (pcs->f_clino_percent) clin = atan(clin);
913      /* We want to warn if there's a reading which it would be impossible
914       * to have read from the instrument (e.g. on a -90 to 90 degree scale
915       * you can't read "96" (it's probably a typo for "69").  However, the
916       * gradient reading from a topofil is typically in the range 0 to 180,
917       * with 90 being horizontal.
918       *
919       * Really we should allow the valid range to be specified, but for now
920       * we infer it from the zero error - if this is within 45 degrees of
921       * 90 then we assume the range is 0 to 180.
922       */
923      z = pcs->z[Q_GRADIENT];
924      range_0_180 = (z > M_PI_4 && z < 3*M_PI_4);
925      diff_from_abs90 = fabs(clin) - M_PI_2;
926      if (diff_from_abs90 > EPSILON) {
927         if (!range_0_180)
928            /* TRANSLATORS: Degrees are the angular measurement where there are
929             * 360 in a full circle. */
930            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
931      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
932                 diff_from_abs90 >= -EPSILON) {
933         ctype = CTYPE_INFERPLUMB;
934      }
935      if (range_0_180 && ctype != CTYPE_INFERPLUMB) {
936         /* FIXME: Warning message not ideal... */
937         if (clin < 0.0 || clin - M_PI > EPSILON)
938            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
939      }
940   }
941
942   if (backctype == CTYPE_READING) {
943      backclin *= pcs->units[Q_BACKGRADIENT];
944      /* percentage scale */
945      if (pcs->f_backclino_percent) backclin = atan(backclin);
946      if (ctype != CTYPE_READING) {
947         real diff_from_abs90 = fabs(backclin) - M_PI_2;
948         if (diff_from_abs90 > EPSILON) {
949            /* FIXME: different message for BackClino? */
950            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
951         } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
952                    diff_from_abs90 >= -EPSILON) {
953            backctype = CTYPE_INFERPLUMB;
954         }
955      }
956   }
957
958   /* un-infer the plumb if the backsight was just a reading */
959   if (ctype == CTYPE_INFERPLUMB && backctype == CTYPE_READING) {
960       ctype = CTYPE_READING;
961   }
962
963   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
964      /* TRANSLATORS: In data with backsights, the user has tried to give a
965       * plumb for the foresight and a clino reading for the backsight, or
966       * something similar. */
967      compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
968      return 0;
969   }
970
971   if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB ||
972       backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) {
973      /* plumbed */
974      if (!fNoComp) {
975         if (ctype == CTYPE_PLUMB ||
976             (ctype == CTYPE_INFERPLUMB && VAL(Comp) != 0.0) ||
977             backctype == CTYPE_PLUMB ||
978             (backctype == CTYPE_INFERPLUMB && VAL(BackComp) != 0.0)) {
979            /* FIXME: Different message for BackComp? */
980            /* TRANSLATORS: A "plumbed leg" is one measured using a plumbline
981             * (a weight on a string).  So the problem here is that the leg is
982             * vertical, so a compass reading has no meaning! */
983            compile_warning(/*Compass reading given on plumbed leg*/21);
984         }
985      }
986
987      dx = dy = (real)0.0;
988      if (ctype != CTYPE_OMIT) {
989         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
990            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
991            compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
992            return 0;
993         }
994         dz = (clin > (real)0.0) ? tape : -tape;
995      } else {
996         dz = (backclin < (real)0.0) ? tape : -tape;
997      }
998      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
999      vz = var(Q_POS) / 3.0 + VAR(Tape);
1000#ifndef NO_COVARIANCES
1001      /* Correct values - no covariances in this case! */
1002      cxy = cyz = czx = (real)0.0;
1003#endif
1004   } else {
1005      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
1006       * or CTYPE_OMIT */
1007      /* clino */
1008      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
1009      if (fNoComp) {
1010         /* TRANSLATORS: Here "legs" are survey legs, i.e. measurements between
1011          * survey stations. */
1012         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
1013         return 0;
1014      }
1015      if (tape == (real)0.0) {
1016         dx = dy = dz = (real)0.0;
1017         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
1018#ifndef NO_COVARIANCES
1019         cxy = cyz = czx = (real)0.0;
1020#endif
1021#if DEBUG_DATAIN_1
1022         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
1023#endif
1024      } else {
1025         real sinGcosG;
1026         /* take into account variance in LEVEL case */
1027         real var_clin = var(Q_LEVEL);
1028         real var_comp;
1029         real comp = handle_compass(&var_comp);
1030         /* ctype != CTYPE_READING is LEVEL case */
1031         if (ctype == CTYPE_READING) {
1032            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
1033            var_clin = VAR(Clino);
1034         }
1035         if (backctype == CTYPE_READING) {
1036            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
1037               * pcs->sc[Q_BACKGRADIENT];
1038            if (ctype == CTYPE_READING) {
1039               if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) {
1040                  /* fore and back readings differ by more than 3 sds */
1041                  /* TRANSLATORS: Degrees are the angular measurement where
1042                   * there are 360 in a full circle. */
1043                  warn_readings_differ(/*CLINO reading and BACKCLINO reading disagree by %s degrees*/99, clin + backclin);
1044               }
1045               clin = (clin / var_clin - backclin / VAR(BackClino));
1046               var_clin = (var_clin + VAR(BackClino)) / 4;
1047               clin *= var_clin;
1048            } else {
1049               clin = -backclin;
1050               var_clin = VAR(BackClino);
1051            }
1052         }
1053
1054#if DEBUG_DATAIN
1055         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
1056#endif
1057         cosG = cos(clin);
1058         LcosG = tape * cosG;
1059         sinB = sin(comp);
1060         cosB = cos(comp);
1061#if DEBUG_DATAIN_1
1062         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
1063#endif
1064         dx = LcosG * sinB;
1065         dy = LcosG * cosB;
1066         dz = tape * sin(clin);
1067/*      printf("%.2f\n",clin); */
1068#if DEBUG_DATAIN_1
1069         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
1070#endif
1071         dx2 = dx * dx;
1072         L2 = tape * tape;
1073         V = VAR(Tape) / L2;
1074         dy2 = dy * dy;
1075         cosG2 = cosG * cosG;
1076         sinGcosG = sin(clin) * cosG;
1077         dz2 = dz * dz;
1078         v = dz2 * var_clin;
1079#ifdef NO_COVARIANCES
1080         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
1081               (.5 + sinB * sinB * cosG2) * v);
1082         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
1083               (.5 + cosB * cosB * cosG2) * v);
1084         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
1085            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
1086            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
1087         } else {
1088            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
1089         }
1090         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
1091#else
1092         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
1093            (sinB * sinB * v);
1094         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
1095            (cosB * cosB * v);
1096         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
1097            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
1098            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
1099         } else {
1100            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
1101         }
1102         /* usual covariance formulae are fine in no clino case since
1103          * dz = 0 so value of var_clin is ignored */
1104         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
1105               - var_comp * dx * dy;
1106         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
1107         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
1108#if 0
1109         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
1110         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
1111#endif
1112#endif
1113#if DEBUG_DATAIN_1
1114         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
1115#endif
1116      }
1117   }
1118#if DEBUG_DATAIN_1
1119   printf("Just before addleg, vx = %f\n", vx);
1120#endif
1121   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
1122   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1123#ifndef NO_COVARIANCES
1124                , cyz, czx, cxy
1125#endif
1126                );
1127   return 1;
1128}
1129
1130static int
1131process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1132{
1133   real tape = VAL(Tape);
1134
1135   real dx, dy, dz;
1136   real vx, vy, vz;
1137#ifndef NO_COVARIANCES
1138   real cxy = 0, cyz = 0, czx = 0;
1139#endif
1140
1141   handle_comp_units();
1142
1143   /* depth gauge readings increase upwards with default calibration */
1144   if (fDepthChange) {
1145      SVX_ASSERT(VAL(FrDepth) == 0.0);
1146      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1147      dz *= pcs->sc[Q_DEPTH];
1148   } else {
1149      dz = VAL(ToDepth) - VAL(FrDepth);
1150      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1151   }
1152
1153   /* adjusted tape is negative -- probably the calibration is wrong */
1154   if (tape < (real)0.0) {
1155      compile_warning(/*Negative adjusted tape reading*/79);
1156   }
1157
1158   /* check if tape is less than depth change */
1159   if (tape < fabs(dz)) {
1160      /* FIXME: allow margin of error based on variances? */
1161      /* TRANSLATORS: This means that the data fed in said this.
1162       *
1163       * It could be a gross error (e.g. the decimal point is missing from the
1164       * depth gauge reading) or it could just be due to random error on a near
1165       * vertical leg */
1166      compile_warning(/*Tape reading is less than change in depth*/62);
1167   }
1168
1169   if (tape == (real)0.0 && dz == 0.0) {
1170      dx = dy = dz = (real)0.0;
1171      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
1172   } else if (VAL(Comp) == HUGE_REAL &&
1173              VAL(BackComp) == HUGE_REAL) {
1174      /* plumb */
1175      dx = dy = (real)0.0;
1176      if (dz < 0) tape = -tape;
1177      /* FIXME: Should use FrDepth sometimes... */
1178      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1179         / (VAR(Tape) * 2 * VAR(ToDepth));
1180      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1181      /* FIXME: Should use FrDepth sometimes... */
1182      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1183                              / (VAR(Tape) + VAR(ToDepth));
1184   } else {
1185      real L2, sinB, cosB, dz2, D2;
1186      real var_comp;
1187      real comp = handle_compass(&var_comp);
1188      sinB = sin(comp);
1189      cosB = cos(comp);
1190      L2 = tape * tape;
1191      dz2 = dz * dz;
1192      D2 = L2 - dz2;
1193      if (D2 <= (real)0.0) {
1194         /* FIXME: Should use FrDepth sometimes... */
1195         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
1196         dx = dy = (real)0.0;
1197         vx = vy = var(Q_POS) / 3.0;
1198         /* FIXME: Should use FrDepth sometimes... */
1199         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
1200         if (dz > 0) {
1201            /* FIXME: Should use FrDepth sometimes... */
1202            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
1203         } else {
1204            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
1205         }
1206      } else {
1207         real D = sqrt(D2);
1208         /* FIXME: Should use FrDepth sometimes... */
1209         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
1210         dx = D * sinB;
1211         dy = D * cosB;
1212
1213         vx = var(Q_POS) / 3.0 +
1214            sinB * sinB * F / D2 + var_comp * dy * dy;
1215         vy = var(Q_POS) / 3.0 +
1216            cosB * cosB * F / D2 + var_comp * dx * dx;
1217         /* FIXME: Should use FrDepth sometimes... */
1218         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1219
1220#ifndef NO_COVARIANCES
1221         cxy = sinB * cosB * (F / D2 + var_comp * D2);
1222         /* FIXME: Should use FrDepth sometimes... */
1223         cyz = -2 * VAR(ToDepth) * dy / D;
1224         czx = -2 * VAR(ToDepth) * dx / D;
1225#endif
1226      }
1227   }
1228   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1229#ifndef NO_COVARIANCES
1230                , cxy, cyz, czx
1231#endif
1232                );
1233   return 1;
1234}
1235
1236static int
1237process_cartesian(prefix *fr, prefix *to, bool fToFirst)
1238{
1239   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
1240   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
1241   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
1242
1243   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
1244#ifndef NO_COVARIANCES
1245                , 0, 0, 0
1246#endif
1247                );
1248   return 1;
1249}
1250
1251static void
1252data_cartesian(void)
1253{
1254   prefix *fr = NULL, *to = NULL;
1255
1256   bool fMulti = fFalse;
1257
1258   reading first_stn = End;
1259
1260   reading *ordering;
1261
1262   again:
1263
1264   for (ordering = pcs->ordering ; ; ordering++) {
1265      skipblanks();
1266      switch (*ordering) {
1267       case Fr:
1268         fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1269         if (first_stn == End) first_stn = Fr;
1270         break;
1271       case To:
1272         to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1273         if (first_stn == End) first_stn = To;
1274         break;
1275       case Station:
1276         fr = to;
1277         to = read_prefix(PFX_STATION);
1278         first_stn = To;
1279         break;
1280       case Dx: case Dy: case Dz:
1281         read_reading(*ordering, fFalse);
1282         break;
1283       case Ignore:
1284         skipword(); break;
1285       case IgnoreAllAndNewLine:
1286         skipline();
1287         /* fall through */
1288       case Newline:
1289         if (fr != NULL) {
1290            if (!process_cartesian(fr, to, first_stn == To))
1291               skipline();
1292         }
1293         fMulti = fTrue;
1294         while (1) {
1295            process_eol();
1296            process_bol();
1297            if (isData(ch)) break;
1298            if (!isComm(ch)) {
1299               push_back(ch);
1300               return;
1301            }
1302         }
1303         break;
1304       case IgnoreAll:
1305         skipline();
1306         /* fall through */
1307       case End:
1308         if (!fMulti) {
1309            process_cartesian(fr, to, first_stn == To);
1310            process_eol();
1311            return;
1312         }
1313         do {
1314            process_eol();
1315            process_bol();
1316         } while (isComm(ch));
1317         goto again;
1318       default: BUG("Unknown reading in ordering");
1319      }
1320   }
1321}
1322
1323static int
1324process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1325{
1326   real tape = VAL(Tape);
1327
1328   real dx, dy, dz;
1329   real vx, vy, vz;
1330#ifndef NO_COVARIANCES
1331   real cxy = 0;
1332#endif
1333
1334   handle_comp_units();
1335
1336   /* depth gauge readings increase upwards with default calibration */
1337   if (fDepthChange) {
1338      SVX_ASSERT(VAL(FrDepth) == 0.0);
1339      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1340      dz *= pcs->sc[Q_DEPTH];
1341   } else {
1342      dz = VAL(ToDepth) - VAL(FrDepth);
1343      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1344   }
1345
1346   /* adjusted tape is negative -- probably the calibration is wrong */
1347   if (tape < (real)0.0) {
1348      compile_warning(/*Negative adjusted tape reading*/79);
1349   }
1350
1351   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
1352      /* plumb */
1353      dx = dy = (real)0.0;
1354      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1355      /* FIXME: Should use FrDepth sometimes... */
1356      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1357   } else {
1358      real sinB, cosB;
1359      real var_comp;
1360      real comp = handle_compass(&var_comp);
1361      sinB = sin(comp);
1362      cosB = cos(comp);
1363
1364      dx = tape * sinB;
1365      dy = tape * cosB;
1366
1367      vx = var(Q_POS) / 3.0 +
1368         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
1369      vy = var(Q_POS) / 3.0 +
1370         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1371      /* FIXME: Should use FrDepth sometimes... */
1372      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1373
1374#ifndef NO_COVARIANCES
1375      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
1376#endif
1377   }
1378   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1379#ifndef NO_COVARIANCES
1380                , cxy, 0, 0
1381#endif
1382                );
1383   return 1;
1384}
1385
1386/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1387 * Also handles topofil (fromcount/tocount or count) in place of tape */
1388static void
1389data_normal(void)
1390{
1391   prefix *fr = NULL, *to = NULL;
1392   reading first_stn = End;
1393
1394   bool fTopofil = fFalse, fMulti = fFalse;
1395   bool fRev;
1396   clino_type ctype, backctype;
1397   bool fDepthChange;
1398   unsigned long compass_dat_flags = 0;
1399
1400   reading *ordering;
1401
1402   VAL(Tape) = 0;
1403   VAL(Comp) = VAL(BackComp) = HUGE_REAL;
1404   VAL(FrCount) = VAL(ToCount) = 0;
1405   VAL(FrDepth) = VAL(ToDepth) = 0;
1406   VAL(Left) = VAL(Right) = VAL(Up) = VAL(Down) = HUGE_REAL;
1407
1408   fRev = fFalse;
1409   ctype = backctype = CTYPE_OMIT;
1410   fDepthChange = fFalse;
1411
1412   /* ordering may omit clino reading, so set up default here */
1413   /* this is also used if clino reading is the omit character */
1414   VAL(Clino) = VAL(BackClino) = 0;
1415
1416   again:
1417
1418   /* We clear these flags in the normal course of events, but if there's an
1419    * error in a reading, we might not, so make sure it has been cleared here.
1420    */
1421   pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1422   for (ordering = pcs->ordering; ; ordering++) {
1423      skipblanks();
1424      switch (*ordering) {
1425       case Fr:
1426          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
1427          if (first_stn == End) first_stn = Fr;
1428          break;
1429       case To:
1430          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
1431          if (first_stn == End) first_stn = To;
1432          break;
1433       case Station:
1434          fr = to;
1435          to = read_prefix(PFX_STATION);
1436          first_stn = To;
1437          break;
1438       case Dir: {
1439          typedef enum {
1440             DIR_NULL=-1, DIR_FORE, DIR_BACK
1441          } dir_tok;
1442          static sztok dir_tab[] = {
1443             {"B",     DIR_BACK},
1444             {"F",     DIR_FORE},
1445          };
1446          dir_tok tok;
1447          get_token();
1448          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1449          switch (tok) {
1450           case DIR_FORE:
1451             break;
1452           case DIR_BACK:
1453             fRev = fTrue;
1454             break;
1455           default:
1456             file.lpos += strlen(buffer);
1457             compile_error_skip(-/*Found “%s”, expecting “F” or “B”*/131,
1458                                buffer);
1459             process_eol();
1460             return;
1461          }
1462          break;
1463       }
1464       case Tape:
1465          read_reading(Tape, fFalse);
1466          if (VAL(Tape) < (real)0.0)
1467             compile_warning(-/*Negative tape reading*/60);
1468          break;
1469       case Count:
1470          VAL(FrCount) = VAL(ToCount);
1471          read_reading(ToCount, fFalse);
1472          fTopofil = fTrue;
1473          break;
1474       case FrCount:
1475          read_reading(FrCount, fFalse);
1476          break;
1477       case ToCount:
1478          read_reading(ToCount, fFalse);
1479          fTopofil = fTrue;
1480          break;
1481       case Comp: case BackComp:
1482          read_bearing_or_omit(*ordering);
1483          break;
1484       case Clino: case BackClino: {
1485          reading r = *ordering;
1486          clino_type * p_ctype = (r == Clino ? &ctype : &backctype);
1487          read_reading(r, fTrue);
1488          if (VAL(r) == HUGE_REAL) {
1489             VAL(r) = handle_plumb(p_ctype);
1490             if (VAL(r) != HUGE_REAL) break;
1491             compile_error_token(-/*Expecting numeric field, found “%s”*/9);
1492             skipline();
1493             process_eol();
1494             return;
1495          }
1496          *p_ctype = CTYPE_READING;
1497          break;
1498       }
1499       case FrDepth: case ToDepth:
1500          read_reading(*ordering, fFalse);
1501          break;
1502       case Depth:
1503          VAL(FrDepth) = VAL(ToDepth);
1504          read_reading(ToDepth, fFalse);
1505          break;
1506       case DepthChange:
1507          fDepthChange = fTrue;
1508          VAL(FrDepth) = 0;
1509          read_reading(ToDepth, fFalse);
1510          break;
1511       case CompassDATComp:
1512          read_bearing_or_omit(Comp);
1513          if (is_compass_NaN(VAL(Comp))) VAL(Comp) = HUGE_REAL;
1514          break;
1515       case CompassDATBackComp:
1516          read_bearing_or_omit(BackComp);
1517          if (is_compass_NaN(VAL(BackComp))) VAL(BackComp) = HUGE_REAL;
1518          break;
1519       case CompassDATClino: case CompassDATBackClino: {
1520          reading r;
1521          clino_type * p_ctype;
1522          if (*ordering == CompassDATClino) {
1523             r = Clino;
1524             p_ctype = &ctype;
1525          } else {
1526             r = BackClino;
1527             p_ctype = &backctype;
1528          }
1529          read_reading(r, fFalse);
1530          if (is_compass_NaN(VAL(r))) {
1531             VAL(r) = HUGE_REAL;
1532             *p_ctype = CTYPE_OMIT;
1533          } else {
1534             *p_ctype = CTYPE_READING;
1535          }
1536          break;
1537       }
1538       case CompassDATLeft: case CompassDATRight:
1539       case CompassDATUp: case CompassDATDown: {
1540          /* FIXME: need to actually make use of these entries! */
1541          reading actual = Left + (*ordering - CompassDATLeft);
1542          read_reading(actual, fFalse);
1543          if (VAL(actual) < 0) VAL(actual) = HUGE_REAL;
1544          break;
1545       }
1546       case CompassDATFlags:
1547          if (ch == '#') {
1548             nextch();
1549             if (ch == '|') {
1550                filepos fp;
1551                get_pos(&fp);
1552                nextch();
1553                while (ch >= 'A' && ch <= 'Z') {
1554                   compass_dat_flags |= BIT(ch - 'A');
1555                   /* FIXME: we currently understand:
1556                    *   L (exclude from length)
1557                    *   X (exclude data)
1558                    * but should also handle at least some of:
1559                    *   C (no adjustment)
1560                    *   P (no plot)
1561                    */
1562                   nextch();
1563                }
1564                if (ch == '#') {
1565                   nextch();
1566                } else {
1567                   compass_dat_flags = 0;
1568                   set_pos(&fp);
1569                   push_back('|');
1570                   ch = '#';
1571                }
1572             } else {
1573                push_back(ch);
1574                ch = '#';
1575             }
1576          }
1577          break;
1578       case Ignore:
1579          skipword(); break;
1580       case IgnoreAllAndNewLine:
1581          skipline();
1582          /* fall through */
1583       case Newline:
1584          if (fr != NULL) {
1585             int r;
1586             int save_flags;
1587             int implicit_splay;
1588             if (fTopofil)
1589                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1590             /* Note: frdepth == todepth test works regardless of fDepthChange
1591              * (frdepth always zero, todepth is change of depth) and also
1592              * works for STYLE_NORMAL (both remain 0) */
1593             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1594                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1595                process_equate(fr, to);
1596                goto inferred_equate;
1597             }
1598             if (fRev) {
1599                prefix *t = fr;
1600                fr = to;
1601                to = t;
1602             }
1603             if (fTopofil) {
1604                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1605             } else {
1606                VAL(Tape) *= pcs->units[Q_LENGTH];
1607                VAL(Tape) -= pcs->z[Q_LENGTH];
1608                VAL(Tape) *= pcs->sc[Q_LENGTH];
1609             }
1610             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1611             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1612             save_flags = pcs->flags;
1613             if (implicit_splay) {
1614                pcs->flags |= BIT(FLAGS_SPLAY);
1615             }
1616             switch (pcs->style) {
1617              case STYLE_NORMAL:
1618                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1619                                   ctype, backctype);
1620                break;
1621              case STYLE_DIVING:
1622                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1623                                   fDepthChange);
1624                break;
1625              case STYLE_CYLPOLAR:
1626                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1627                                     fDepthChange);
1628                break;
1629              default:
1630                r = 0; /* avoid warning */
1631                BUG("bad style");
1632             }
1633             pcs->flags = save_flags;
1634             if (!r) skipline();
1635
1636             /* Swap fr and to back to how they were for next line */
1637             if (fRev) {
1638                prefix *t = fr;
1639                fr = to;
1640                to = t;
1641             }
1642          }
1643
1644          fRev = fFalse;
1645          ctype = backctype = CTYPE_OMIT;
1646          fDepthChange = fFalse;
1647
1648          /* ordering may omit clino reading, so set up default here */
1649          /* this is also used if clino reading is the omit character */
1650          VAL(Clino) = VAL(BackClino) = 0;
1651
1652          inferred_equate:
1653
1654          fMulti = fTrue;
1655          while (1) {
1656              process_eol();
1657              process_bol();
1658              if (isData(ch)) break;
1659              if (!isComm(ch)) {
1660                 push_back(ch);
1661                 return;
1662              }
1663          }
1664          break;
1665       case IgnoreAll:
1666          skipline();
1667          /* fall through */
1668       case End:
1669          if (!fMulti) {
1670             int save_flags;
1671             int implicit_splay;
1672             /* Compass ignore flag is 'X' */
1673             if ((compass_dat_flags & BIT('X' - 'A'))) {
1674                process_eol();
1675                return;
1676             }
1677             if (fRev) {
1678                prefix *t = fr;
1679                fr = to;
1680                to = t;
1681             }
1682             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1683             /* Note: frdepth == todepth test works regardless of fDepthChange
1684              * (frdepth always zero, todepth is change of depth) and also
1685              * works for STYLE_NORMAL (both remain 0) */
1686             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1687                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1688                process_equate(fr, to);
1689                process_eol();
1690                return;
1691             }
1692             if (fTopofil) {
1693                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1694             } else {
1695                VAL(Tape) *= pcs->units[Q_LENGTH];
1696                VAL(Tape) -= pcs->z[Q_LENGTH];
1697                VAL(Tape) *= pcs->sc[Q_LENGTH];
1698             }
1699             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1700             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1701             save_flags = pcs->flags;
1702             if (implicit_splay) {
1703                pcs->flags |= BIT(FLAGS_SPLAY);
1704             }
1705             if ((compass_dat_flags & BIT('L' - 'A'))) {
1706                /* 'L' means "exclude from length" - map this to Survex's
1707                 * FLAGS_DUPLICATE. */
1708                pcs->flags |= BIT(FLAGS_DUPLICATE);
1709             }
1710             switch (pcs->style) {
1711              case STYLE_NORMAL:
1712                process_normal(fr, to, (first_stn == To) ^ fRev,
1713                               ctype, backctype);
1714                break;
1715              case STYLE_DIVING:
1716                process_diving(fr, to, (first_stn == To) ^ fRev,
1717                               fDepthChange);
1718                break;
1719              case STYLE_CYLPOLAR:
1720                process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1721                                 fDepthChange);
1722                break;
1723              default:
1724                BUG("bad style");
1725             }
1726             pcs->flags = save_flags;
1727
1728             process_eol();
1729             return;
1730          }
1731          do {
1732             process_eol();
1733             process_bol();
1734          } while (isComm(ch));
1735          goto again;
1736       default:
1737          BUG("Unknown reading in ordering");
1738      }
1739   }
1740}
1741
1742static int
1743process_lrud(prefix *stn)
1744{
1745   SVX_ASSERT(next_lrud);
1746   lrud * xsect = osnew(lrud);
1747   xsect->stn = stn;
1748   xsect->l = (VAL(Left) * pcs->units[Q_LEFT] - pcs->z[Q_LEFT]) * pcs->sc[Q_LEFT];
1749   xsect->r = (VAL(Right) * pcs->units[Q_RIGHT] - pcs->z[Q_RIGHT]) * pcs->sc[Q_RIGHT];
1750   xsect->u = (VAL(Up) * pcs->units[Q_UP] - pcs->z[Q_UP]) * pcs->sc[Q_UP];
1751   xsect->d = (VAL(Down) * pcs->units[Q_DOWN] - pcs->z[Q_DOWN]) * pcs->sc[Q_DOWN];
1752   xsect->meta = pcs->meta;
1753   if (pcs->meta) ++pcs->meta->ref_count;
1754   xsect->next = NULL;
1755   *next_lrud = xsect;
1756   next_lrud = &(xsect->next);
1757
1758   return 1;
1759}
1760
1761static void
1762data_passage(void)
1763{
1764   prefix *stn = NULL;
1765   reading *ordering;
1766
1767   for (ordering = pcs->ordering ; ; ordering++) {
1768      skipblanks();
1769      switch (*ordering) {
1770       case Station:
1771         stn = read_prefix(PFX_STATION);
1772         break;
1773       case Left: case Right: case Up: case Down:
1774         read_reading(*ordering, fTrue);
1775         if (VAL(*ordering) == HUGE_REAL) {
1776            if (!isOmit(ch)) {
1777               compile_error_token(-/*Expecting numeric field, found “%s”*/9);
1778            } else {
1779               nextch();
1780            }
1781            VAL(*ordering) = -1;
1782         }
1783         break;
1784       case Ignore:
1785         skipword(); break;
1786       case IgnoreAll:
1787         skipline();
1788         /* fall through */
1789       case End: {
1790         process_lrud(stn);
1791         process_eol();
1792         return;
1793       }
1794       default: BUG("Unknown reading in ordering");
1795      }
1796   }
1797}
1798
1799static int
1800process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1801{
1802   nosurveylink *link;
1803
1804   /* Suppress "unused fixed point" warnings for these stations */
1805   fr->sflags |= BIT(SFLAGS_USED);
1806   to->sflags |= BIT(SFLAGS_USED);
1807
1808   /* add to linked list which is dealt with after network is solved */
1809   link = osnew(nosurveylink);
1810   if (fToFirst) {
1811      link->to = StnFromPfx(to);
1812      link->fr = StnFromPfx(fr);
1813   } else {
1814      link->fr = StnFromPfx(fr);
1815      link->to = StnFromPfx(to);
1816   }
1817   link->flags = pcs->flags | (STYLE_NOSURVEY << FLAGS_STYLE_BIT0);
1818   link->meta = pcs->meta;
1819   if (pcs->meta) ++pcs->meta->ref_count;
1820   link->next = nosurveyhead;
1821   nosurveyhead = link;
1822   return 1;
1823}
1824
1825static void
1826data_nosurvey(void)
1827{
1828   prefix *fr = NULL, *to = NULL;
1829
1830   bool fMulti = fFalse;
1831
1832   reading first_stn = End;
1833
1834   reading *ordering;
1835
1836   again:
1837
1838   for (ordering = pcs->ordering ; ; ordering++) {
1839      skipblanks();
1840      switch (*ordering) {
1841       case Fr:
1842          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1843          if (first_stn == End) first_stn = Fr;
1844          break;
1845       case To:
1846          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1847          if (first_stn == End) first_stn = To;
1848          break;
1849       case Station:
1850          fr = to;
1851          to = read_prefix(PFX_STATION);
1852          first_stn = To;
1853          break;
1854       case Ignore:
1855         skipword(); break;
1856       case IgnoreAllAndNewLine:
1857         skipline();
1858         /* fall through */
1859       case Newline:
1860         if (fr != NULL) {
1861            if (!process_nosurvey(fr, to, first_stn == To))
1862               skipline();
1863         }
1864         if (ordering[1] == End) {
1865            do {
1866               process_eol();
1867               process_bol();
1868            } while (isComm(ch));
1869            if (!isData(ch)) {
1870               push_back(ch);
1871               return;
1872            }
1873            goto again;
1874         }
1875         fMulti = fTrue;
1876         while (1) {
1877            process_eol();
1878            process_bol();
1879            if (isData(ch)) break;
1880            if (!isComm(ch)) {
1881               push_back(ch);
1882               return;
1883            }
1884         }
1885         break;
1886       case IgnoreAll:
1887         skipline();
1888         /* fall through */
1889       case End:
1890         if (!fMulti) {
1891            (void)process_nosurvey(fr, to, first_stn == To);
1892            process_eol();
1893            return;
1894         }
1895         do {
1896            process_eol();
1897            process_bol();
1898         } while (isComm(ch));
1899         goto again;
1900       default: BUG("Unknown reading in ordering");
1901      }
1902   }
1903}
1904
1905/* totally ignore a line of survey data */
1906static void
1907data_ignore(void)
1908{
1909   skipline();
1910   process_eol();
1911}
Note: See TracBrowser for help on using the repository browser.