source: git/src/datain.c @ b03a097f

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 b03a097f was 63dc4eb, checked in by Olly Betts <olly@…>, 22 years ago

Synchronised with 1.0 branch.

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

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