source: git/src/datain.c @ ca7769e

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

Next step towards LRUD support.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2885 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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