source: git/src/datain.c @ 32f05df

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 32f05df was b5a3219, checked in by Olly Betts <olly@…>, 22 years ago

Work towards "colour by date"...

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

  • Property mode set to 100644
File size: 45.7 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, 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   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 *pth = path_from_fnm(file.filename);
566            char *fnm = NULL;
567            int fnm_len;
568            nextch_handling_eol();
569            while (ch != ',' && ch != ';' && ch != EOF) {
570               while (isEol(ch)) process_eol();
571               s_catchar(&fnm, &fnm_len, 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 (fnm) {
629               ch_store = ch;
630               data_file(pth, fnm);
631               ch = ch_store;
632               osfree(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            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 msg, 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(msg, 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 var = 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) > var + 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 / var + backcomp / VAR(BackComp));
842         var = (var + VAR(BackComp)) / 4;
843         comp *= var;
844         comp += adj;
845      } else {
846         comp = backcomp;
847         var = VAR(BackComp);
848      }
849   }
850   *p_var = var;
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   real frdepth = VAL(FrDepth);
1325   real todepth = VAL(ToDepth);
1326
1327   real dx, dy, dz;
1328   real vx, vy, vz;
1329#ifndef NO_COVARIANCES
1330   real cxy = 0;
1331#endif
1332
1333   handle_comp_units();
1334
1335   /* depth gauge readings increase upwards with default calibration */
1336   if (fDepthChange) {
1337      SVX_ASSERT(VAL(FrDepth) == 0.0);
1338      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1339      dz *= pcs->sc[Q_DEPTH];
1340   } else {
1341      dz = VAL(ToDepth) - VAL(FrDepth);
1342      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1343   }
1344
1345   /* adjusted tape is negative -- probably the calibration is wrong */
1346   if (tape < (real)0.0) {
1347      compile_warning(/*Negative adjusted tape reading*/79);
1348   }
1349
1350   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
1351      /* plumb */
1352      dx = dy = (real)0.0;
1353      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1354      /* FIXME: Should use FrDepth sometimes... */
1355      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1356   } else {
1357      real sinB, cosB;
1358      real var_comp;
1359      real comp = handle_compass(&var_comp);
1360      sinB = sin(comp);
1361      cosB = cos(comp);
1362
1363      dx = tape * sinB;
1364      dy = tape * cosB;
1365
1366      vx = var(Q_POS) / 3.0 +
1367         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
1368      vy = var(Q_POS) / 3.0 +
1369         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1370      /* FIXME: Should use FrDepth sometimes... */
1371      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1372
1373#ifndef NO_COVARIANCES
1374      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
1375#endif
1376   }
1377   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1378#ifndef NO_COVARIANCES
1379                , cxy, 0, 0
1380#endif
1381                );
1382#ifdef CHASM3DX
1383   if (fUseNewFormat) {
1384      /* new twiglet and insert into twig tree */
1385      twig *twiglet = osnew(twig);
1386      twiglet->from = fr;
1387      twiglet->to = to;
1388      twiglet->down = twiglet->right = NULL;
1389      twiglet->source = twiglet->drawings
1390        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1391      twiglet->up = limb->up;
1392      limb->right = twiglet;
1393      limb = twiglet;
1394
1395      /* record pre-fettling deltas */
1396      twiglet->delta[0] = dx;
1397      twiglet->delta[1] = dy;
1398      twiglet->delta[2] = dz;
1399   }
1400#endif
1401
1402   return 1;
1403}
1404
1405/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1406 * Also handles topofil (fromcount/tocount or count) in place of tape */
1407extern int
1408data_normal(void)
1409{
1410   prefix *fr = NULL, *to = NULL;
1411   reading first_stn = End;
1412
1413   bool fTopofil = fFalse, fMulti = fFalse;
1414   bool fRev;
1415   clino_type ctype, backctype;
1416   bool fDepthChange;
1417   unsigned long compass_dat_flags = 0;
1418
1419   reading *ordering;
1420
1421   VAL(Tape) = 0;
1422   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1423   VAL(FrCount) = VAL(ToCount) = 0;
1424   VAL(FrDepth) = VAL(ToDepth) = 0;
1425
1426   fRev = fFalse;
1427   ctype = backctype = CTYPE_OMIT;
1428   fDepthChange = fFalse;
1429
1430   /* ordering may omit clino reading, so set up default here */
1431   /* this is also used if clino reading is the omit character */
1432   VAL(Clino) = VAL(BackClino) = 0;
1433
1434   again:
1435
1436   for (ordering = pcs->ordering; ; ordering++) {
1437      skipblanks();
1438      switch (*ordering) {
1439       case Fr:
1440          fr = read_prefix_stn(fFalse, fTrue);
1441          if (first_stn == End) first_stn = Fr;
1442          break;
1443       case To:
1444          to = read_prefix_stn(fFalse, fTrue);
1445          if (first_stn == End) first_stn = To;
1446          break;
1447       case Station:
1448          fr = to;
1449          to = read_prefix_stn(fFalse, fFalse);
1450          first_stn = To;
1451          break;
1452       case Dir: {
1453          typedef enum {
1454             DIR_NULL=-1, DIR_FORE, DIR_BACK
1455          } dir_tok;
1456          static sztok dir_tab[] = {
1457             {"B",     DIR_BACK},
1458             {"F",     DIR_FORE},
1459          };
1460          dir_tok tok;
1461          get_token();
1462          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1463          switch (tok) {
1464           case DIR_FORE:
1465             break;
1466           case DIR_BACK:
1467             fRev = fTrue;
1468             break;
1469           default:
1470             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1471                                buffer);
1472             process_eol();
1473             return 0;
1474          }
1475          break;
1476       }
1477       case Tape:
1478          read_reading(Tape, fFalse);
1479          if (VAL(Tape) < (real)0.0)
1480             compile_warning(/*Negative tape reading*/60);
1481          break;
1482       case Count:
1483          VAL(FrCount) = VAL(ToCount);
1484          read_reading(ToCount, fFalse);
1485          break;
1486       case FrCount:
1487          read_reading(FrCount, fFalse);
1488          break;
1489       case ToCount:
1490          read_reading(ToCount, fFalse);
1491          fTopofil = fTrue;
1492          break;
1493       case Comp:
1494          read_bearing_or_omit(Comp);
1495          break;
1496       case BackComp:
1497          read_bearing_or_omit(BackComp);
1498          break;
1499       case Clino:
1500          read_reading(Clino, fTrue);
1501          if (VAL(Clino) == HUGE_REAL) {
1502             VAL(Clino) = handle_plumb(&ctype);
1503             if (VAL(Clino) != HUGE_REAL) break;
1504             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1505             process_eol();
1506             return 0;
1507          }
1508          ctype = CTYPE_READING;
1509          break;
1510       case BackClino:
1511          read_reading(BackClino, fTrue);
1512          if (VAL(BackClino) == HUGE_REAL) {
1513             VAL(BackClino) = handle_plumb(&backctype);
1514             if (VAL(BackClino) != HUGE_REAL) break;
1515             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1516             process_eol();
1517             return 0;
1518          }
1519          backctype = CTYPE_READING;
1520          break;
1521       case FrDepth:
1522          read_reading(FrDepth, fFalse);
1523          break;
1524       case ToDepth:
1525          read_reading(ToDepth, fFalse);
1526          break;
1527       case Depth:
1528          VAL(FrDepth) = VAL(ToDepth);
1529          read_reading(ToDepth, fFalse);
1530          break;
1531       case DepthChange:
1532          fDepthChange = fTrue;
1533          VAL(FrDepth) = 0;
1534          read_reading(ToDepth, fFalse);
1535          break;
1536       case CompassDATFlags:
1537          if (ch == '#') {
1538             nextch();
1539             if (ch == '|') {
1540                filepos fp;
1541                get_pos(&fp);
1542                nextch();
1543                while (ch >= 'A' && ch <= 'Z') {
1544                   compass_dat_flags |= BIT(ch - 'A');
1545                   nextch();
1546                }
1547                if (ch == '#') {
1548                   nextch();
1549                } else {
1550                   compass_dat_flags = 0;
1551                   set_pos(&fp);
1552                   push_back('|');
1553                   ch = '#';
1554                }
1555             } else {
1556                push_back(ch);
1557                ch = '#';
1558             }
1559          }
1560          break;
1561       case Ignore:
1562          skipword(); break;
1563       case IgnoreAllAndNewLine:
1564          skipline();
1565          /* fall through */
1566       case Newline:
1567          if (fr != NULL) {
1568             int r;
1569             if (fTopofil)
1570                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1571             /* Note: frdepth == todepth test works regardless of fDepthChange
1572              * (frdepth always zero, todepth is change of depth) and also
1573              * works for STYLE_NORMAL (both remain 0) */
1574             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1575                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1576                process_equate(fr, to);
1577                goto inferred_equate;
1578             }
1579             if (fRev) {
1580                prefix *t = fr;
1581                fr = to;
1582                to = t;
1583             }
1584             if (fTopofil) {
1585                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1586             } else {
1587                VAL(Tape) *= pcs->units[Q_LENGTH];
1588                VAL(Tape) -= pcs->z[Q_LENGTH];
1589                VAL(Tape) *= pcs->sc[Q_LENGTH];
1590             }
1591             switch (pcs->style) {
1592              case STYLE_NORMAL:
1593                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1594                                   ctype, backctype);
1595                break;
1596              case STYLE_DIVING:
1597                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1598                                   fDepthChange);
1599                break;
1600              case STYLE_CYLPOLAR:
1601                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1602                                     fDepthChange);
1603                break;
1604              default:
1605                BUG("bad style");
1606             }
1607             if (!r) skipline();
1608             
1609             /* Swap fr and to back to how they were for next line */
1610             if (fRev) {
1611                prefix *t = fr;
1612                fr = to;
1613                to = t;
1614             }
1615          }
1616
1617          fRev = fFalse;
1618          ctype = backctype = CTYPE_OMIT;
1619          fDepthChange = fFalse;
1620
1621          /* ordering may omit clino reading, so set up default here */
1622          /* this is also used if clino reading is the omit character */
1623          VAL(Clino) = VAL(BackClino) = 0;
1624
1625          inferred_equate:
1626
1627          fMulti = fTrue;
1628          while (1) {
1629              process_eol();
1630              process_bol();
1631              if (isData(ch)) break;
1632              if (!isComm(ch)) {
1633                 push_back(ch);
1634                 return 1;
1635              }
1636          }
1637          break;
1638       case IgnoreAll:
1639          skipline();
1640          /* fall through */
1641       case End:
1642          if (!fMulti) {
1643             int r;
1644             /* Compass ignore flag is 'X' */
1645             if ((compass_dat_flags & BIT('X' - 'A'))) {
1646                process_eol();
1647                return 1;
1648             }
1649             if (fRev) {
1650                prefix *t = fr;
1651                fr = to;
1652                to = t;
1653             }
1654             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1655             /* Note: frdepth == todepth test works regardless of fDepthChange
1656              * (frdepth always zero, todepth is change of depth) and also
1657              * works for STYLE_NORMAL (both remain 0) */
1658             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1659                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1660                process_equate(fr, to);
1661                process_eol();
1662                return 1;
1663             }
1664             if (fTopofil) {
1665                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1666             } else {
1667                VAL(Tape) *= pcs->units[Q_LENGTH];
1668                VAL(Tape) -= pcs->z[Q_LENGTH];
1669                VAL(Tape) *= pcs->sc[Q_LENGTH];
1670             }
1671             switch (pcs->style) {
1672              case STYLE_NORMAL:
1673                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1674                                   ctype, backctype);
1675                break;
1676              case STYLE_DIVING:
1677                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1678                                   fDepthChange);
1679                break;
1680              case STYLE_CYLPOLAR:
1681                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1682                                     fDepthChange);
1683                break;
1684              default:
1685                BUG("bad style");
1686             }
1687             process_eol();
1688             return r;
1689          }
1690          do {
1691             process_eol();
1692             process_bol();
1693          } while (isComm(ch));
1694          goto again;
1695       default:
1696          BUG("Unknown reading in ordering");
1697      }
1698   }
1699}
1700
1701static int
1702process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1703{
1704   nosurveylink *link;
1705   int shape;
1706
1707#ifdef CHASM3DX
1708   if (fUseNewFormat) {
1709      /* new twiglet and insert into twig tree */
1710      twig *twiglet = osnew(twig);
1711      twiglet->from = fr;
1712      twiglet->to = to;
1713      twiglet->down = twiglet->right = NULL;
1714      twiglet->source = twiglet->drawings
1715        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1716      twiglet->up = limb->up;
1717      limb->right = twiglet;
1718      limb = twiglet;
1719      /* delta is only used to calculate error - pass zero and cope
1720       * elsewhere */
1721      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1722   }
1723#endif
1724
1725   /* Suppress "unused fixed point" warnings for these stations
1726    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1727    */
1728   shape = fr->shape;
1729   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1730   shape = to->shape;
1731   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1732
1733   /* add to linked list which is dealt with after network is solved */
1734   link = osnew(nosurveylink);
1735   if (fToFirst) {
1736      link->to = StnFromPfx(to);
1737      link->fr = StnFromPfx(fr);
1738   } else {
1739      link->fr = StnFromPfx(fr);
1740      link->to = StnFromPfx(to);
1741   }
1742   link->flags = pcs->flags;
1743   link->next = nosurveyhead;
1744   nosurveyhead = link;
1745   return 1;
1746}
1747
1748extern int
1749data_nosurvey(void)
1750{
1751   prefix *fr = NULL, *to = NULL;
1752
1753   bool fMulti = fFalse;
1754
1755   reading first_stn = End;
1756
1757   reading *ordering;
1758
1759   again:
1760
1761   for (ordering = pcs->ordering ; ; ordering++) {
1762      skipblanks();
1763      switch (*ordering) {
1764       case Fr:
1765          fr = read_prefix_stn(fFalse, fTrue);
1766          if (first_stn == End) first_stn = Fr;
1767          break;
1768       case To:
1769          to = read_prefix_stn(fFalse, fTrue);
1770          if (first_stn == End) first_stn = To;
1771          break;
1772       case Station:
1773          fr = to;
1774          to = read_prefix_stn(fFalse, fFalse);
1775          first_stn = To;
1776          break;
1777       case Ignore:
1778         skipword(); break;
1779       case IgnoreAllAndNewLine:
1780         skipline();
1781         /* fall through */
1782       case Newline:
1783         if (fr != NULL) {
1784            int r;
1785            r = process_nosurvey(fr, to, first_stn == To);
1786            if (!r) skipline();
1787         }
1788         if (ordering[1] == End) {
1789            do {
1790               process_eol();
1791               process_bol();
1792            } while (isComm(ch));
1793            if (!isData(ch)) {
1794               push_back(ch);
1795               return 1;
1796            }
1797            goto again;
1798         }
1799         fMulti = fTrue;
1800         while (1) {
1801            process_eol();
1802            process_bol();
1803            if (isData(ch)) break;
1804            if (!isComm(ch)) {
1805               push_back(ch);
1806               return 1;
1807            }
1808         }
1809         break;
1810       case IgnoreAll:
1811         skipline();
1812         /* fall through */
1813       case End:
1814         if (!fMulti) {
1815            int r = process_nosurvey(fr, to, first_stn == To);
1816            process_eol();
1817            return r;
1818         }
1819         do {
1820            process_eol();
1821            process_bol();
1822         } while (isComm(ch));
1823         goto again;
1824       default: BUG("Unknown reading in ordering");
1825      }
1826   }
1827}
1828
1829/* totally ignore a line of survey data */
1830extern int
1831data_ignore(void)
1832{
1833   skipline();
1834   process_eol();
1835   return 1;
1836}
Note: See TracBrowser for help on using the repository browser.