source: git/src/datain.c @ af89ae7

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 af89ae7 was 27b8b59, checked in by Olly Betts <olly@…>, 23 years ago

Sync with 1.0 branch.

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

  • Property mode set to 100644
File size: 37.0 KB
RevLine 
[b0d908e]1/* datain.c
[d1b1380]2 * Reads in survey files, dealing with special characters, keywords & data
[107b8bd]3 * Copyright (C) 1991-2002 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[d1b1380]18 */
19
[a420b49]20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
[d1b1380]23
24#include <limits.h>
[a420b49]25#include <stdarg.h>
[d1b1380]26
27#include "debug.h"
[a420b49]28#include "cavern.h"
[07025e3]29#include "filename.h"
30#include "message.h"
[d1b1380]31#include "filelist.h"
32#include "netbits.h"
[5853657]33#include "netskel.h"
[d1b1380]34#include "readval.h"
35#include "datain.h"
36#include "commands.h"
37#include "out.h"
[39fba51]38#include "str.h"
[d1b1380]39
[2b078c4]40#define EPSILON (REAL_EPSILON * 1000)
[d1b1380]41
[58cc1fb]42#define var(I) (pcs->Var[(I)])
[d1b1380]43
[58cc1fb]44int ch;
[d1b1380]45
[fa42426]46typedef enum {CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_HORIZ} clino_type;
[5b7c1b7]47
[eb18f4d]48/* Don't explicitly initialise as we can't set the jmp_buf - this has
49 * static scope so will be initialised like this anyway */
[b0d908e]50parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
[a420b49]51
[932f7e9]52bool f_export_ok;
53
[21c226e]54static real value[Fr - 1];
55#define VAL(N) value[(N)-1]
56static real variance[Fr - 1];
57#define VAR(N) variance[(N)-1]
58
[c80bd34]59static filepos fpLineStart;
[d1b1380]60
[c7451a9]61void
[c80bd34]62get_pos(filepos *fp)
[c7451a9]63{
[c80bd34]64   fp->ch = ch;
65   fp->offset = ftell(file.fh);
66   if (fp->offset == -1)
[c7451a9]67      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
68}
69
[c80bd34]70void
71set_pos(const filepos *fp)
[c7451a9]72{
[c80bd34]73   ch = fp->ch;
74   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
[c7451a9]75      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
76}
77
[44bf1d9]78static void
[993454b]79push_back(int c)
80{
81   if (c != EOF && ungetc(c, file.fh) == EOF)
82      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
83}
84
[a420b49]85static void
86error_list_parent_files(void)
87{
[b0d908e]88   if (!file.reported_where && file.parent) {
[421b7d2]89      parse *p = file.parent;
[46cb98f]90      const char *m = msg(/*In file included from*/5);
[84c60fc]91      size_t len = strlen(m);
[b0d908e]92
[84c60fc]93      fprintf(STDERR, m);
[46cb98f]94      m = msg(/*from*/3);
[b0d908e]95
96      /* Suppress reporting of full include tree for further errors
97       * in this file */
98      file.reported_where = fTrue;
99
[a420b49]100      while (p) {
[b0d908e]101         /* Force re-report of include tree for further errors in
102          * parent files */
103         p->reported_where = fFalse;
[46cb98f]104         fprintf(STDERR, " %s:%d", p->filename, p->line);
[a420b49]105         p = p->parent;
[84f3ed6]106         if (p) fprintf(STDERR, ",\n%*s", (int)len, m);
[a420b49]107      }
108      fprintf(STDERR, ":\n");
109   }
110}
111
112void
113compile_error(int en, ...)
114{
115   va_list ap;
116   va_start(ap, en);
117   error_list_parent_files();
118   v_report(1, file.filename, file.line, en, ap);
119   va_end(ap);
120}
121
[fa42426]122void
123compile_error_skip(int en, ...)
124{
125   va_list ap;
126   va_start(ap, en);
127   error_list_parent_files();
128   v_report(1, file.filename, file.line, en, ap);
129   va_end(ap);
130   skipline();
131}
132
[39fba51]133void
134compile_error_token(int en)
135{
136   char *p = NULL;
137   static int len;
138   s_zero(&p);
139   skipblanks();
140   while (!isBlank(ch) && !isEol(ch)) {
141      s_catchar(&p, &len, ch);
142      nextch();
143   }
[fa42426]144   compile_error_skip(en, p ? p : "");
[39fba51]145   osfree(p);
146}
147
[a420b49]148void
149compile_warning(int en, ...)
150{
151   va_list ap;
152   va_start(ap, en);
153   error_list_parent_files();
154   v_report(0, file.filename, file.line, en, ap);
155   va_end(ap);
156}
157
158/* This function makes a note where to put output files */
159static void
160using_data_file(const char *fnm)
161{
162   if (!fnm_output_base) {
[4bab027]163      /* was: fnm_output_base = base_from_fnm(fnm); */
164      fnm_output_base = baseleaf_from_fnm(fnm);
165   } else if (fnm_output_base_is_dir) {
166      /* --output pointed to directory so use the leaf basename in that dir */
167      char *lf, *p;
168      lf = baseleaf_from_fnm(fnm);
169      p = use_path(fnm_output_base, lf);
170      osfree(lf);
171      osfree(fnm_output_base);
172      fnm_output_base = p;
173      fnm_output_base_is_dir = 0;
[58cc1fb]174   }
175}
176
[a420b49]177static void
178skipword(void)
179{
[58cc1fb]180   while (!isBlank(ch) && !isEol(ch)) nextch();
[d1b1380]181}
182
[a420b49]183extern void
184skipblanks(void)
185{
[58cc1fb]186   while (isBlank(ch)) nextch();
[d1b1380]187}
188
[a420b49]189extern void
190skipline(void)
191{
[58cc1fb]192   while (!isEol(ch)) nextch();
[d1b1380]193}
194
[90bb053f]195#ifndef NO_PERCENTAGE
196static long int filelen;
197#endif
198
199static void
200process_bol(void)
201{
202   /* Note start of line for error reporting */
[c80bd34]203   get_pos(&fpLineStart);
[90bb053f]204
205#ifndef NO_PERCENTAGE
206   /* print %age of file done */
[c80bd34]207   if (filelen > 0)
208      printf("%d%%\r", (int)(100 * fpLineStart.offset / filelen));
[90bb053f]209#endif
210
211   nextch();
212   skipblanks();
213}
214
215static void
216process_eol(void)
217{
218   int eolchar;
219
220   skipblanks();
221
222   if (!isEol(ch)) {
[44bf1d9]223      if (!isComm(ch)) compile_error(/*End of line not blank*/15);
[90bb053f]224      skipline();
225   }
226
227   eolchar = ch;
228   file.line++;
229   /* skip any different eol characters so we get line counts correct on
230    * DOS text files and similar, but don't count several adjacent blank
231    * lines as one */
232   while (ch != EOF) {
233      nextch();
234      if (ch == eolchar || !isEol(ch)) {
[993454b]235         push_back(ch);
[90bb053f]236         break;
237      }
238   }
239}
240
241static bool
242process_non_data_line(void)
243{
244   process_bol();
245
246   if (isData(ch)) return fFalse;
247
248   if (isKeywd(ch)) {
249      nextch();
250      handle_command();
251   }
252
253   process_eol();
254
255   return fTrue;
256}
257
[21c226e]258static void
259read_reading(reading r, bool f_optional)
260{
261   int n_readings;
262   q_quantity q;
263   VAL(r) = read_numeric(f_optional, &n_readings);
264   switch (r) {
265      case Tape: q = Q_LENGTH; break;
266      case Comp: q = Q_BEARING; break;
267      case BackComp: q = Q_BACKBEARING; break;
268      case Clino: q = Q_GRADIENT; break;
269      case BackClino: q = Q_BACKGRADIENT; break;
270      case FrDepth: case ToDepth: q = Q_DEPTH; break;
271      case Dx: q = Q_DX; break;
272      case Dy: q = Q_DY; break;
273      case Dz: q = Q_DZ; break;
274      case FrCount: case ToCount: q = Q_COUNT; break;
275      default: BUG("Unexpected case");
276   }
277   VAR(r) = var(q);
278   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
279}
280
281static void
282read_bearing_or_omit(reading r)
283{
284   int n_readings;
285   q_quantity q;
286   VAL(r) = read_numeric_or_omit(&n_readings);
287   switch (r) {
288      case Comp: q = Q_BEARING; break;
289      case BackComp: q = Q_BACKBEARING; break;
290      default: BUG("Unexpected case");
291   }
292   VAR(r) = var(q);
293   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
294}
295
[a420b49]296extern void
297data_file(const char *pth, const char *fnm)
298{
[47c7a94]299   int begin_lineno_store;
[7f08c83]300   parse file_store;
[d1b1380]301
[7f08c83]302   {
303      char *filename;
[f4b609d]304      FILE *fh;
305      if (!pth) {
306         /* file specified on command line - don't do special translation */
307         fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
308      } else {
309         fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
310      }
[bd1913f]311
[7f08c83]312      if (fh == NULL) {
[759fb47]313         compile_error(/*Couldn't open data file `%s'*/24, fnm);
[7f08c83]314         return;
315      }
316
317      file_store = file;
318      if (file.fh) file.parent = &file_store;
319      file.fh = fh;
320      file.filename = filename;
321      file.line = 1;
[b0d908e]322      file.reported_where = fFalse;
[a420b49]323   }
[cb3d1e2]324
[5b68ae1]325   if (fPercent) printf("%s:\n", fnm);
[d1b1380]326
[a420b49]327   using_data_file(file.filename);
[d1b1380]328
[47c7a94]329   begin_lineno_store = pcs->begin_lineno;
330   pcs->begin_lineno = 0;
[cb3d1e2]331
[d1b1380]332#ifndef NO_PERCENTAGE
[58cc1fb]333   /* Try to find how long the file is...
334    * However, under ANSI fseek( ..., SEEK_END) may not be supported */
335   filelen = 0;
336   if (fPercent) {
[c80bd34]337      if (fseek(file.fh, 0l, SEEK_END) == 0) {
338         filepos fp;
339         get_pos(&fp);
340         filelen = fp.offset;
341      }
[58cc1fb]342      rewind(file.fh); /* reset file ptr to start & clear any error state */
343   }
[d1b1380]344#endif
345
[7d5f3c0]346#ifdef HAVE_SETJMP_H
[a420b49]347   /* errors in nested functions can longjmp here */
348   if (setjmp(file.jbSkipLine)) {
[076d33d]349      process_eol();
[a420b49]350   }
[d1b1380]351#endif
[cb3d1e2]352
[c7451a9]353   while (!feof(file.fh) && !ferror(file.fh)) {
[90bb053f]354      if (!process_non_data_line()) {
[107b8bd]355         int r;
[647407d]356#ifdef NEW3DFORMAT
[407084d]357         twig *temp = limb;
[647407d]358#endif
[407084d]359         f_export_ok = fFalse;
[107b8bd]360         switch (pcs->style) {
361          case STYLE_NORMAL:
362          case STYLE_DIVING:
363          case STYLE_CYLPOLAR:
364            r = data_normal();
365            break;
366          case STYLE_CARTESIAN:
367            r = data_cartesian();
368            break;
369          case STYLE_NOSURVEY:
370            r = data_nosurvey();
371            break;
372          case STYLE_IGNORE:
373            r = data_ignore();
374            break;
375          default:
376            BUG("bad style");
377         }
[90bb053f]378         /* style function returns 0 => error */
[647407d]379#ifdef NEW3DFORMAT
[21c226e]380         if (!r) {
[90bb053f]381            /* we have just created a very naughty twiglet, and it must be
382             * punished */
[647407d]383            osfree(limb);
384            limb = temp;
[a420b49]385         }
[21c226e]386#endif
[647407d]387      }
[a420b49]388   }
389
[932f7e9]390   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
391    * including file */
392   f_export_ok = fFalse;
393
[a420b49]394#ifndef NO_PERCENTAGE
395   if (fPercent) putnl();
[4f8285d]396#endif
[d1b1380]397
[47c7a94]398   if (pcs->begin_lineno) {
[0e867ba6]399      error_in_file(file.filename, pcs->begin_lineno,
400                    /*BEGIN with no matching END in this file*/23);
[47c7a94]401      /* Implicitly close any unclosed BEGINs from this file */
402      do {
403         settings *pcsParent = pcs->next;
[4c07c51]404         SVX_ASSERT(pcsParent);
[647407d]405         free_settings(pcs);
[47c7a94]406         pcs = pcsParent;
407      } while (pcs->begin_lineno);
408   }
409
410   pcs->begin_lineno = begin_lineno_store;
[cb3d1e2]411
[22c9877]412   if (ferror(file.fh))
[bfe1242]413      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
[cb3d1e2]414
[22c9877]415   (void)fclose(file.fh);
[7f08c83]416
[22c9877]417   file = file_store;
[4d9eecd]418
419   /* don't free this - it may be pointed to by prefix.file */
420   /* osfree(file.filename); */
[a420b49]421}
[d1b1380]422
[b6de07d]423static real
424mod2pi(real a)
425{
426   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
427}
428
[dcec245]429static real
[5b7c1b7]430handle_plumb(clino_type *p_ctype)
[dcec245]431{
432   typedef enum {
433      CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
434   } clino_tok;
435   static sztok clino_tab[] = {
436      {"D",     CLINO_DOWN},
437      {"DOWN",  CLINO_DOWN},
438      {"H",     CLINO_LEVEL},
439      {"LEVEL", CLINO_LEVEL},
440      {"U",     CLINO_UP},
441      {"UP",    CLINO_UP},
442      {NULL,    CLINO_NULL}
443   };
[5b7c1b7]444   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
[dcec245]445   clino_tok tok;
446
447   skipblanks();
448   if (isalpha(ch)) {
[c80bd34]449      filepos fp;
450      get_pos(&fp);
[dcec245]451      get_token();
452      tok = match_tok(clino_tab, TABSIZE(clino_tab));
453      if (tok != CLINO_NULL) {
[fa42426]454         *p_ctype = (clinos[tok] == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
[dcec245]455         return clinos[tok];
456      }
[c80bd34]457      set_pos(&fp);
[dcec245]458   } else if (isSign(ch)) {
459      int chOld = ch;
460      nextch();
461      if (toupper(ch) == 'V') {
462         nextch();
[fa42426]463         *p_ctype = CTYPE_PLUMB;
[bceebf4]464         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
[dcec245]465      }
466
[bd1913f]467      if (isOmit(chOld)) {
[fa42426]468         *p_ctype = CTYPE_OMIT;
[dcec245]469         /* no clino reading, so assume 0 with large sd */
470         return (real)0.0;
471      }
[5b7c1b7]472   } else if (isOmit(ch)) {
473      /* OMIT char may not be a SIGN char too so we need to check here as
474       * well as above... */
475      nextch();
[fa42426]476      *p_ctype = CTYPE_OMIT;
[5b7c1b7]477      /* no clino reading, so assume 0 with large sd */
478      return (real)0.0;
[dcec245]479   }
480   return HUGE_REAL;
481}
482
[b6de07d]483static void
484warn_readings_differ(int msg, real diff)
485{
486   char buf[64];
487   char *p;
488   sprintf(buf, "%.2f", deg(fabs(diff)));
489   p = strchr(buf, '.');
490   if (p) {
491      char *z = p;
492      while (*++p) {
493         if (*p != '0') z = p + 1;
494      }
495      if (*z) *z = '\0';
496   }
497   compile_warning(msg, buf);
498}
499
500static bool
[21c226e]501handle_comp_units(void)
[b6de07d]502{
503   bool fNoComp = fTrue;
[21c226e]504   if (VAL(Comp) != HUGE_REAL) {
[b6de07d]505      fNoComp = fFalse;
[21c226e]506      VAL(Comp) *= pcs->units[Q_BEARING];
507      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
[b6de07d]508         compile_warning(/*Suspicious compass reading*/59);
[21c226e]509         VAL(Comp) = mod2pi(VAL(Comp));
[b6de07d]510      }
511   }
[21c226e]512   if (VAL(BackComp) != HUGE_REAL) {
[b6de07d]513      fNoComp = fFalse;
[21c226e]514      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
515      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
[b6de07d]516         /* FIXME: different message for BackComp? */
517         compile_warning(/*Suspicious compass reading*/59);
[21c226e]518         VAL(BackComp) = mod2pi(VAL(BackComp));
[b6de07d]519      }
520   }
521   return fNoComp;
522}
523
524static real
[21c226e]525handle_compass(real *p_var)
[b6de07d]526{
[21c226e]527   real var = VAR(Comp);
528   real comp = VAL(Comp);
529   real backcomp = VAL(BackComp);
[b6de07d]530   if (comp != HUGE_REAL) {
531      comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
532      comp -= pcs->z[Q_DECLINATION];
533   }
534   if (backcomp != HUGE_REAL) {
535      backcomp = (backcomp - pcs->z[Q_BACKBEARING])
536              * pcs->sc[Q_BACKBEARING];
537      backcomp -= pcs->z[Q_DECLINATION];
538      backcomp -= M_PI;
539      if (comp != HUGE_REAL) {
540         real diff = comp - backcomp;
541         real adj = fabs(diff) > M_PI ? M_PI : 0;
542         diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
[21c226e]543         if (sqrd(diff / 2.0) > var + VAR(Q_BACKBEARING)) {
[770157e]544            /* fore and back readings differ by more than 2 sds */
[fa42426]545            warn_readings_differ(/*Compass reading and back compass reading disagree by %s degrees*/98, diff);
[b6de07d]546         }
[21c226e]547         comp = (comp / var + backcomp / VAR(BackComp));
548         var = (var + VAR(BackComp)) / 4;
[b6de07d]549         comp *= var;
550         comp += adj;
551      } else {
552         comp = backcomp;
[21c226e]553         var = VAR(BackComp);
[b6de07d]554      }
555   }
556   *p_var = var;
557   return comp;
558}
559
[90bb053f]560static int
[21c226e]561process_normal(prefix *fr, prefix *to, bool fToFirst,
562               clino_type ctype, clino_type backctype)
[a420b49]563{
[21c226e]564   real tape = VAL(Tape);
565   real comp = VAL(Comp);
566   real clin = VAL(Clino);
567   real backclin = VAL(BackClino);
568
[a420b49]569   real dx, dy, dz;
570   real vx, vy, vz;
571#ifndef NO_COVARIANCES
[cb3d1e2]572   real cxy, cyz, czx;
[a420b49]573#endif
[d1b1380]574
[90bb053f]575   bool fNoComp;
[a420b49]576
[107b8bd]577   /* adjusted tape is negative -- probably the calibration is wrong */
578   if (tape < (real)0.0) {
579      /* TRANSLATE different message for topofil? */
580      compile_warning(/*Negative adjusted tape reading*/79);
[647407d]581   }
[d1b1380]582
[21c226e]583   fNoComp = handle_comp_units();
[d1b1380]584
[fa42426]585   if (ctype == CTYPE_READING) {
[0b71cfc]586      real diff_from_abs90;
[a420b49]587      clin *= pcs->units[Q_GRADIENT];
[fa42426]588      /* percentage scale */
589      if (pcs->f_clino_percent) clin = atan(clin);
[0b71cfc]590      diff_from_abs90 = fabs(clin) - M_PI_2;
591      if (diff_from_abs90 > EPSILON) {
[a420b49]592         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
[27b8b59]593      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
594                 diff_from_abs90 > -EPSILON) {
[fa42426]595         ctype = CTYPE_PLUMB;
[a420b49]596      }
597   }
[cb3d1e2]598
[fa42426]599   if (backctype == CTYPE_READING) {
[0b71cfc]600      real diff_from_abs90;
[b14f44f]601      backclin *= pcs->units[Q_BACKGRADIENT];
[fa42426]602      /* percentage scale */
603      if (pcs->f_backclino_percent) backclin = atan(backclin);
[0b71cfc]604      diff_from_abs90 = fabs(backclin) - M_PI_2;
605      if (diff_from_abs90 > EPSILON) {
606         /* FIXME: different message for BackClino? */
607         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
[27b8b59]608      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
609                 diff_from_abs90 > -EPSILON) {
[fa42426]610         backctype = CTYPE_PLUMB;
[0b71cfc]611      }
612   }
613
[fa42426]614   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
615      compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
[b14f44f]616      return 0;
[0b71cfc]617   }
618
[fa42426]619   if (ctype == CTYPE_PLUMB || backctype == CTYPE_PLUMB) {
[a420b49]620      /* plumbed */
621      if (!fNoComp) {
[11e919a]622         /* FIXME: Different message for BackComp? */
[a420b49]623         compile_warning(/*Compass reading given on plumbed leg*/21);
624      }
625
626      dx = dy = (real)0.0;
[fa42426]627      if (ctype != CTYPE_OMIT) {
628         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
[b14f44f]629            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
[fa42426]630            compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
[b14f44f]631            return 0;
[0b71cfc]632         }
633         dz = (clin > (real)0.0) ? tape : -tape;
634      } else {
635         dz = (backclin < (real)0.0) ? tape : -tape;
636      }
[a420b49]637      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]638      vz = var(Q_POS) / 3.0 + VAR(Tape);
[4f8285d]639#ifndef NO_COVARIANCES
[26a805f]640      /* Correct values - no covariances in this case! */
641      cxy = cyz = czx = (real)0.0;
[4f8285d]642#endif
[a420b49]643   } else {
[fa42426]644      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
645       * or CTYPE_OMIT */
[a420b49]646      /* clino */
647      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
[421b7d2]648      if (fNoComp) {
[fa42426]649         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
[a420b49]650         return 0;
651      }
652      if (tape == (real)0.0) {
653         dx = dy = dz = (real)0.0;
654         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[4f8285d]655#ifndef NO_COVARIANCES
[a420b49]656         cxy = cyz = czx = (real)0.0;
[4f8285d]657#endif
[d1b1380]658#if DEBUG_DATAIN_1
[a420b49]659         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]660#endif
[a420b49]661      } else {
[dbb4e19]662         real sinGcosG;
[0b71cfc]663         /* take into account variance in LEVEL case */
664         real var_clin = var(Q_LEVEL);
[b6de07d]665         real var_comp;
[21c226e]666         real comp = handle_compass(&var_comp);
[fa42426]667         /* ctype != CTYPE_READING is LEVEL case */
668         if (ctype == CTYPE_READING) {
[a420b49]669            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
[21c226e]670            var_clin = VAR(Clino);
[0b71cfc]671         }
[fa42426]672         if (backctype == CTYPE_READING) {
[b14f44f]673            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
674               * pcs->sc[Q_BACKGRADIENT];
[fa42426]675            if (ctype == CTYPE_READING) {
[b14f44f]676               if (sqrd((clin + backclin) / 3.0) >
[21c226e]677                     var_clin + VAR(BackClino)) {
[b14f44f]678                  /* fore and back readings differ by more than 3 sds */
[fa42426]679                  warn_readings_differ(/*Clino reading and back clino reading disagree by %s degrees*/99, clin + backclin);
[b14f44f]680               }
[21c226e]681               clin = (clin / var_clin - backclin / VAR(BackClino));
682               var_clin = (var_clin + VAR(BackClino)) / 4;
[b14f44f]683               clin *= var_clin;
[0b71cfc]684            } else {
685               clin = -backclin;
[21c226e]686               var_clin = VAR(BackClino);
[0b71cfc]687            }
688         }
[d1b1380]689
690#if DEBUG_DATAIN
[a420b49]691         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
[d1b1380]692#endif
[a420b49]693         cosG = cos(clin);
694         LcosG = tape * cosG;
695         sinB = sin(comp);
696         cosB = cos(comp);
[d1b1380]697#if DEBUG_DATAIN_1
[a420b49]698         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
[d1b1380]699#endif
[a420b49]700         dx = LcosG * sinB;
701         dy = LcosG * cosB;
702         dz = tape * sin(clin);
[d1b1380]703/*      printf("%.2f\n",clin); */
704#if DEBUG_DATAIN_1
[a420b49]705         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
[d1b1380]706#endif
[a420b49]707         dx2 = dx * dx;
708         L2 = tape * tape;
[21c226e]709         V = VAR(Tape) / L2;
[a420b49]710         dy2 = dy * dy;
711         cosG2 = cosG * cosG;
[dbb4e19]712         sinGcosG = sin(clin) * cosG;
[a420b49]713         dz2 = dz * dz;
[0b71cfc]714         v = dz2 * var_clin;
[5b7c1b7]715#ifdef NO_COVARIANCES
[0b71cfc]716         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[a420b49]717               (.5 + sinB * sinB * cosG2) * v);
[0b71cfc]718         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[a420b49]719               (.5 + cosB * cosB * cosG2) * v);
[fa42426]720         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]721            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]722            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]723         } else {
[0b71cfc]724            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]725         }
[a420b49]726         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
[dbb4e19]727#else
[0b71cfc]728         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[dbb4e19]729            (sinB * sinB * v);
[0b71cfc]730         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[dbb4e19]731            (cosB * cosB * v);
[fa42426]732         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]733            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]734            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]735         } else {
[0b71cfc]736            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]737         }
[647407d]738         /* usual covariance formulae are fine in no clino case since
[0b71cfc]739          * dz = 0 so value of var_clin is ignored */
[21c226e]740         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
[0b71cfc]741               - var_comp * dx * dy;
[21c226e]742         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
743         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
[dbb4e19]744#if 0
745         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
746         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
747#endif
[4f8285d]748#endif
[d1b1380]749#if DEBUG_DATAIN_1
[a420b49]750         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]751#endif
[a420b49]752      }
753   }
[d1b1380]754#if DEBUG_DATAIN_1
[a420b49]755   printf("Just before addleg, vx = %f\n", vx);
[d1b1380]756#endif
[a420b49]757   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
[90bb053f]758   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[4f8285d]759#ifndef NO_COVARIANCES
[2140502]760                , cyz, czx, cxy
[4f8285d]761#endif
[2140502]762                );
[647407d]763
764#ifdef NEW3DFORMAT
765   if (fUseNewFormat) {
[407084d]766      /* new twiglet and insert into twig tree */
767      twig *twiglet = osnew(twig);
[90bb053f]768      twiglet->from = fr;
769      twiglet->to = to;
[407084d]770      twiglet->down = twiglet->right = NULL;
771      twiglet->source = twiglet->drawings
772        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
773      twiglet->up = limb->up;
774      limb->right = twiglet;
775      limb = twiglet;
776
777      /* record pre-fettling deltas */
[647407d]778      twiglet->delta[0] = dx;
779      twiglet->delta[1] = dy;
780      twiglet->delta[2] = dz;
781   }
782#endif
[a420b49]783   return 1;
[d1b1380]784}
785
[5a38209]786static int
[21c226e]787process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[a420b49]788{
[21c226e]789   real tape = VAL(Tape);
790
[a420b49]791   real dx, dy, dz;
792   real vx, vy, vz;
[f795df0]793#ifndef NO_COVARIANCES
794   real cxy = 0, cyz = 0, czx = 0;
795#endif
[a420b49]796
[21c226e]797   handle_comp_units();
[a420b49]798
[a6d094f]799   /* depth gauge readings increase upwards with default calibration */
[6114207]800   if (fDepthChange) {
[21c226e]801      SVX_ASSERT(VAL(FrDepth) == 0.0);
802      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
803      dz *= pcs->sc[Q_DEPTH];
[a6d094f]804   } else {
[21c226e]805      dz = VAL(ToDepth) - VAL(FrDepth);
806      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]807   }
[a420b49]808
809   /* adjusted tape is negative -- probably the calibration is wrong */
810   if (tape < (real)0.0) {
811      compile_warning(/*Negative adjusted tape reading*/79);
812   }
813
814   /* check if tape is less than depth change */
815   if (tape < fabs(dz)) {
[bd1913f]816      /* FIXME: allow margin of error based on variances? */
[a420b49]817      compile_warning(/*Tape reading is less than change in depth*/62);
818   }
819
[0a208f9]820   if (tape == (real)0.0 && dz == 0.0) {
[a420b49]821      dx = dy = dz = (real)0.0;
822      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[21c226e]823   } else if (VAL(Comp) == HUGE_REAL &&
824              VAL(BackComp) == HUGE_REAL) {
[385b703]825      /* plumb */
826      dx = dy = (real)0.0;
827      if (dz < 0) tape = -tape;
[21c226e]828      /* FIXME: Should use FrDepth sometimes... */
829      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
830         / (VAR(Tape) * 2 * VAR(ToDepth));
[385b703]831      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]832      /* FIXME: Should use FrDepth sometimes... */
833      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
834                              / (VAR(Tape) + VAR(ToDepth));
[a420b49]835   } else {
[f795df0]836      real L2, sinB, cosB, dz2, D2;
[b6de07d]837      real var_comp;
[21c226e]838      real comp = handle_compass(&var_comp);
[a420b49]839      sinB = sin(comp);
840      cosB = cos(comp);
841      L2 = tape * tape;
842      dz2 = dz * dz;
843      D2 = L2 - dz2;
844      if (D2 <= (real)0.0) {
[21c226e]845         /* FIXME: Should use FrDepth sometimes... */
846         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
[a420b49]847         dx = dy = (real)0.0;
[f795df0]848         vx = vy = var(Q_POS) / 3.0;
[21c226e]849         /* FIXME: Should use FrDepth sometimes... */
850         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
[f795df0]851         if (dz > 0) {
[21c226e]852            /* FIXME: Should use FrDepth sometimes... */
853            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]854         } else {
[21c226e]855            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]856         }
[a420b49]857      } else {
858         real D = sqrt(D2);
[21c226e]859         /* FIXME: Should use FrDepth sometimes... */
860         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
[a420b49]861         dx = D * sinB;
862         dy = D * cosB;
[f795df0]863
864         vx = var(Q_POS) / 3.0 +
[72095d3]865            sinB * sinB * F / D2 + var_comp * dy * dy;
[f795df0]866         vy = var(Q_POS) / 3.0 +
[72095d3]867            cosB * cosB * F / D2 + var_comp * dx * dx;
[21c226e]868         /* FIXME: Should use FrDepth sometimes... */
869         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[f795df0]870
871#ifndef NO_COVARIANCES
[72095d3]872         cxy = sinB * cosB * (F / D2 + var_comp * D2);
[21c226e]873         /* FIXME: Should use FrDepth sometimes... */
874         cyz = -2 * VAR(ToDepth) * dy / D;
875         czx = -2 * VAR(ToDepth) * dx / D;
[f795df0]876#endif
[a420b49]877      }
878   }
[5a38209]879   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[2140502]880#ifndef NO_COVARIANCES
[f795df0]881                , cxy, cyz, czx
[647407d]882#endif
[2140502]883                );
[647407d]884#ifdef NEW3DFORMAT
885   if (fUseNewFormat) {
[407084d]886      /*new twiglet and insert into twig tree*/
887      twig *twiglet = osnew(twig);
[90bb053f]888      twiglet->from = fr;
889      twiglet->to = to;
[407084d]890      twiglet->down = twiglet->right = NULL;
891      twiglet->source = twiglet->drawings
892        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
893      twiglet->up = limb->up;
894      limb->right = twiglet;
895      limb = twiglet;
896
897      /* record pre-fettling deltas */
[647407d]898      twiglet->delta[0] = dx;
899      twiglet->delta[1] = dy;
900      twiglet->delta[2] = dz;
901   }
902#endif
903
904   return 1;
905}
906
[5a38209]907static int
[21c226e]908process_cartesian(prefix *fr, prefix *to, bool fToFirst)
[5a38209]909{
[21c226e]910   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
911   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
912   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
[647407d]913
[21c226e]914   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
[647407d]915#ifndef NO_COVARIANCES
[c80bd34]916                , 0, 0, 0
[647407d]917#endif
[2140502]918                );
[647407d]919
920#ifdef NEW3DFORMAT
921   if (fUseNewFormat) {
[407084d]922      /* new twiglet and insert into twig tree */
923      twig *twiglet = osnew(twig);
[90bb053f]924      twiglet->from = fr;
925      twiglet->to = to;
[407084d]926      twiglet->down = twiglet->right = NULL;
927      twiglet->source = twiglet->drawings
928        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
929      twiglet->up = limb->up;
930      limb->right = twiglet;
931      limb = twiglet;
932
933      /* record pre-fettling deltas */
[647407d]934      twiglet->delta[0] = dx;
935      twiglet->delta[1] = dy;
936      twiglet->delta[2] = dz;
937   }
[4f8285d]938#endif
[647407d]939
940   return 1;
941}
942
943extern int
[5a38209]944data_cartesian(void)
[647407d]945{
[90bb053f]946   prefix *fr = NULL, *to = NULL;
[5a38209]947
948   bool fMulti = fFalse;
[647407d]949
[0395657]950   reading first_stn = End;
951
952   reading *ordering;
[647407d]953
[5a38209]954   again:
955
[647407d]956   for (ordering = pcs->ordering ; ; ordering++) {
957      skipblanks();
958      switch (*ordering) {
[4a6a094]959       case Fr:
[21c226e]960         fr = read_prefix_stn(fFalse, fTrue);
961         if (first_stn == End) first_stn = Fr;
962         break;
[4a6a094]963       case To:
[21c226e]964         to = read_prefix_stn(fFalse, fTrue);
965         if (first_stn == End) first_stn = To;
966         break;
[5a38209]967       case Station:
[21c226e]968         fr = to;
969         to = read_prefix_stn(fFalse, fFalse);
970         first_stn = To;
971         break;
972       case Dx: case Dy: case Dz:
973         read_reading(*ordering, fFalse);
974         break;
[647407d]975       case Ignore:
976         skipword(); break;
[5a38209]977       case IgnoreAllAndNewLine:
978         skipline();
979         /* fall through */
980       case Newline:
981         if (fr != NULL) {
982            int r;
[21c226e]983            r = process_cartesian(fr, to, first_stn == To);
[5a38209]984            if (!r) skipline();
985         }
986         fMulti = fTrue;
987         while (1) {
988            process_eol();
989            process_bol();
990            if (isData(ch)) break;
[ee6a621]991            if (!isComm(ch)) {
[993454b]992               push_back(ch);
[ee6a621]993               return 1;
994            }
[5a38209]995         }
996         break;
[647407d]997       case IgnoreAll:
998         skipline();
999         /* fall through */
1000       case End:
[5a38209]1001         if (!fMulti) {
[21c226e]1002            int r = process_cartesian(fr, to, first_stn == To);
[5a38209]1003            process_eol();
1004            return r;
1005         }
[eef4d8c]1006         do {
[5a38209]1007            process_eol();
1008            process_bol();
[eef4d8c]1009         } while (isComm(ch));
[5a38209]1010         goto again;
[0395657]1011       default: BUG("Unknown reading in ordering");
[647407d]1012      }
1013   }
[5a38209]1014}
[cb3d1e2]1015
[98a2eec]1016static int
[21c226e]1017process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[98a2eec]1018{
[21c226e]1019   real tape = VAL(Tape);
1020   real frdepth = VAL(FrDepth);
1021   real todepth = VAL(ToDepth);
1022
[98a2eec]1023   real dx, dy, dz;
1024   real vx, vy, vz;
1025#ifndef NO_COVARIANCES
1026   real cxy = 0;
1027#endif
1028
[21c226e]1029   handle_comp_units();
[98a2eec]1030
[a6d094f]1031   /* depth gauge readings increase upwards with default calibration */
[6114207]1032   if (fDepthChange) {
[21c226e]1033      SVX_ASSERT(VAL(FrDepth) == 0.0);
1034      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1035      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1036   } else {
[21c226e]1037      dz = VAL(ToDepth) - VAL(FrDepth);
1038      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1039   }
[98a2eec]1040
1041   /* adjusted tape is negative -- probably the calibration is wrong */
1042   if (tape < (real)0.0) {
1043      compile_warning(/*Negative adjusted tape reading*/79);
1044   }
1045
[21c226e]1046   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
[98a2eec]1047      /* plumb */
1048      dx = dy = (real)0.0;
1049      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1050      /* FIXME: Should use FrDepth sometimes... */
1051      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1052   } else {
1053      real sinB, cosB;
[b6de07d]1054      real var_comp;
[21c226e]1055      real comp = handle_compass(&var_comp);
[98a2eec]1056      sinB = sin(comp);
1057      cosB = cos(comp);
1058
1059      dx = tape * sinB;
1060      dy = tape * cosB;
1061
1062      vx = var(Q_POS) / 3.0 +
[21c226e]1063         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
[98a2eec]1064      vy = var(Q_POS) / 3.0 +
[21c226e]1065         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1066      /* FIXME: Should use FrDepth sometimes... */
1067      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1068
1069#ifndef NO_COVARIANCES
[21c226e]1070      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
[98a2eec]1071#endif
1072   }
1073   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1074#ifndef NO_COVARIANCES
1075                , cxy, 0, 0
1076#endif
1077                );
1078#ifdef NEW3DFORMAT
1079   if (fUseNewFormat) {
1080      /*new twiglet and insert into twig tree*/
1081      twig *twiglet = osnew(twig);
1082      twiglet->from = fr;
1083      twiglet->to = to;
1084      twiglet->down = twiglet->right = NULL;
1085      twiglet->source = twiglet->drawings
1086        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1087      twiglet->up = limb->up;
1088      limb->right = twiglet;
1089      limb = twiglet;
1090
1091      /* record pre-fettling deltas */
1092      twiglet->delta[0] = dx;
1093      twiglet->delta[1] = dy;
1094      twiglet->delta[2] = dz;
1095   }
1096#endif
1097
1098   return 1;
1099}
1100
[107b8bd]1101/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1102 * Also handles topofil (fromcount/tocount or count) in place of tape */
[54c4612]1103extern int
[107b8bd]1104data_normal(void)
[54c4612]1105{
1106   prefix *fr = NULL, *to = NULL;
[107b8bd]1107   reading first_stn = End;
[54c4612]1108
[107b8bd]1109   bool fTopofil = fFalse, fMulti = fFalse;
1110   bool fRev;
[e217d67]1111   clino_type ctype, backctype;
[107b8bd]1112   bool fDepthChange;
[54c4612]1113
1114   reading *ordering;
1115
[21c226e]1116   VAL(Tape) = 0;
1117   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1118   VAL(FrCount) = VAL(ToCount) = 0;
1119   VAL(FrDepth) = VAL(ToDepth) = 0;
[54c4612]1120
1121   fRev = fFalse;
[fa42426]1122   ctype = backctype = CTYPE_OMIT;
[6114207]1123   fDepthChange = fFalse;
[54c4612]1124
[107b8bd]1125   /* ordering may omit clino reading, so set up default here */
1126   /* this is also used if clino reading is the omit character */
[21c226e]1127   VAL(Clino) = VAL(BackClino) = 0;
1128
1129   again:
[107b8bd]1130
[54c4612]1131   for (ordering = pcs->ordering; ; ordering++) {
1132      skipblanks();
1133      switch (*ordering) {
1134       case Fr:
[107b8bd]1135          fr = read_prefix_stn(fFalse, fTrue);
1136          if (first_stn == End) first_stn = Fr;
1137          break;
[54c4612]1138       case To:
[107b8bd]1139          to = read_prefix_stn(fFalse, fTrue);
1140          if (first_stn == End) first_stn = To;
1141          break;
[54c4612]1142       case Station:
[107b8bd]1143          fr = to;
1144          to = read_prefix_stn(fFalse, fFalse);
1145          first_stn = To;
1146          break;
[ee1ec59]1147       case Dir: {
1148          typedef enum {
1149             DIR_NULL=-1, DIR_FORE, DIR_BACK
1150          } dir_tok;
1151          static sztok dir_tab[] = {
1152             {"B",     DIR_BACK},
1153             {"F",     DIR_FORE},
1154          };
1155          dir_tok tok;
1156          get_token();
1157          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1158          switch (tok) {
1159           case DIR_FORE:
[54c4612]1160             break;
[ee1ec59]1161           case DIR_BACK:
[54c4612]1162             fRev = fTrue;
1163             break;
1164           default:
[ee1ec59]1165             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1166                                buffer);
[54c4612]1167             process_eol();
1168             return 0;
1169          }
1170          break;
[ee1ec59]1171       }
[107b8bd]1172       case Tape:
[21c226e]1173          read_reading(Tape, fFalse);
1174          if (VAL(Tape) < (real)0.0)
1175             compile_warning(/*Negative tape reading*/60);
[107b8bd]1176          break;
1177       case Count:
[21c226e]1178          VAL(FrCount) = VAL(ToCount);
1179          read_reading(ToCount, fFalse);
[107b8bd]1180          break;
1181       case FrCount:
[21c226e]1182          read_reading(FrCount, fFalse);
[107b8bd]1183          break;
1184       case ToCount:
[21c226e]1185          read_reading(ToCount, fFalse);
[107b8bd]1186          fTopofil = fTrue;
1187          break;
1188       case Comp:
[21c226e]1189          read_bearing_or_omit(Comp);
[107b8bd]1190          break;
[5b7c1b7]1191       case BackComp:
[21c226e]1192          read_bearing_or_omit(BackComp);
[5b7c1b7]1193          break;
[5757725]1194       case Clino:
[21c226e]1195          read_reading(Clino, fTrue);
1196          if (VAL(Clino) == HUGE_REAL) {
1197             VAL(Clino) = handle_plumb(&ctype);
1198             if (VAL(Clino) != HUGE_REAL) break;
[107b8bd]1199             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1200             process_eol();
1201             return 0;
1202          }
[fa42426]1203          ctype = CTYPE_READING;
[5b7c1b7]1204          break;
1205       case BackClino:
[21c226e]1206          read_reading(BackClino, fTrue);
1207          if (VAL(BackClino) == HUGE_REAL) {
1208             VAL(BackClino) = handle_plumb(&backctype);
1209             if (VAL(BackClino) != HUGE_REAL) break;
[5b7c1b7]1210             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1211             process_eol();
1212             return 0;
1213          }
[fa42426]1214          backctype = CTYPE_READING;
[107b8bd]1215          break;
1216       case FrDepth:
[21c226e]1217          read_reading(FrDepth, fFalse);
[107b8bd]1218          break;
1219       case ToDepth:
[21c226e]1220          read_reading(ToDepth, fFalse);
[107b8bd]1221          break;
[54c4612]1222       case Depth:
[21c226e]1223          VAL(FrDepth) = VAL(ToDepth);
1224          read_reading(ToDepth, fFalse);
[54c4612]1225          break;
[6114207]1226       case DepthChange:
1227          fDepthChange = fTrue;
[21c226e]1228          VAL(FrDepth) = 0;
1229          read_reading(ToDepth, fFalse);
[a186573]1230          break;
[107b8bd]1231       case Ignore:
1232          skipword();
1233          break;
[54c4612]1234       case IgnoreAllAndNewLine:
[107b8bd]1235          skipline();
1236          /* fall through */
[54c4612]1237       case Newline:
[107b8bd]1238          if (fr != NULL) {
1239             int r;
[21c226e]1240             if (fTopofil)
1241                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1242             /* Note: frdepth == todepth test works regardless of fDepthChange
1243              * (frdepth always zero, todepth is change of depth) and also
1244              * works for STYLE_NORMAL (both remain 0) */
[27b8b59]1245             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1246                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1247                process_equate(fr, to);
1248                goto inferred_equate;
1249             }
[e7576f6]1250             if (fRev) {
1251                prefix *t = fr;
1252                fr = to;
1253                to = t;
1254             }
[107b8bd]1255             if (fTopofil) {
[21c226e]1256                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1257             } else {
[21c226e]1258                VAL(Tape) *= pcs->units[Q_LENGTH];
1259                VAL(Tape) -= pcs->z[Q_LENGTH];
1260                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1261             }
1262             switch (pcs->style) {
1263              case STYLE_NORMAL:
[21c226e]1264                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1265                                   ctype, backctype);
[107b8bd]1266                break;
1267              case STYLE_DIVING:
[21c226e]1268                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1269                                   fDepthChange);
[107b8bd]1270                break;
1271              case STYLE_CYLPOLAR:
[21c226e]1272                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1273                                     fDepthChange);
[107b8bd]1274                break;
1275              default:
1276                BUG("bad style");
1277             }
1278             if (!r) skipline();
[e7576f6]1279             
1280             /* Swap fr and to back to how they were for next line */
1281             if (fRev) {
1282                prefix *t = fr;
1283                fr = to;
1284                to = t;
1285             }
[107b8bd]1286          }
[21c226e]1287
1288          fRev = fFalse;
1289          ctype = backctype = CTYPE_OMIT;
1290          fDepthChange = fFalse;
1291
1292          /* ordering may omit clino reading, so set up default here */
1293          /* this is also used if clino reading is the omit character */
1294          VAL(Clino) = VAL(BackClino) = 0;
1295
[107b8bd]1296          inferred_equate:
[e7576f6]1297
[107b8bd]1298          fMulti = fTrue;
1299          while (1) {
1300              process_eol();
1301              process_bol();
1302              if (isData(ch)) break;
1303              if (!isComm(ch)) {
1304                 push_back(ch);
1305                 return 1;
1306              }
1307          }
1308          break;
[54c4612]1309       case IgnoreAll:
[107b8bd]1310          skipline();
1311          /* fall through */
[54c4612]1312       case End:
[107b8bd]1313          if (!fMulti) {
1314             int r;
1315             if (fRev) {
1316                prefix *t = fr;
1317                fr = to;
1318                to = t;
1319             }
[21c226e]1320             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1321             /* Note: frdepth == todepth test works regardless of fDepthChange
1322              * (frdepth always zero, todepth is change of depth) and also
1323              * works for STYLE_NORMAL (both remain 0) */
[27b8b59]1324             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1325                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1326                process_equate(fr, to);
1327                process_eol();
1328                return 1;
1329             }
1330             if (fTopofil) {
[21c226e]1331                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1332             } else {
[21c226e]1333                VAL(Tape) *= pcs->units[Q_LENGTH];
1334                VAL(Tape) -= pcs->z[Q_LENGTH];
1335                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1336             }
1337             switch (pcs->style) {
1338              case STYLE_NORMAL:
[21c226e]1339                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1340                                   ctype, backctype);
[107b8bd]1341                break;
1342              case STYLE_DIVING:
[21c226e]1343                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1344                                   fDepthChange);
[107b8bd]1345                break;
1346              case STYLE_CYLPOLAR:
[21c226e]1347                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1348                                     fDepthChange);
[107b8bd]1349                break;
1350              default:
1351                BUG("bad style");
1352             }
1353             process_eol();
1354             return r;
1355          }
1356          do {
1357             process_eol();
1358             process_bol();
1359          } while (isComm(ch));
1360          goto again;
1361       default:
1362          BUG("Unknown reading in ordering");
[54c4612]1363      }
1364   }
1365}
1366
[5a38209]1367static int
1368process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1369{
1370   nosurveylink *link;
[838a602a]1371   int shape;
[647407d]1372
1373#ifdef NEW3DFORMAT
1374   if (fUseNewFormat) {
[407084d]1375      /* new twiglet and insert into twig tree */
1376      twig *twiglet = osnew(twig);
[90bb053f]1377      twiglet->from = fr;
1378      twiglet->to = to;
[647407d]1379      twiglet->down = twiglet->right = NULL;
1380      twiglet->source = twiglet->drawings
1381        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1382      twiglet->up = limb->up;
1383      limb->right = twiglet;
1384      limb = twiglet;
[3bcbcb2]1385      /* delta is only used to calculate error - pass zero and cope
1386       * elsewhere */
[647407d]1387      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1388   }
1389#endif
1390
[838a602a]1391   /* Suppress "unused fixed point" warnings for these stations
1392    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1393    */
1394   shape = fr->shape;
1395   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1396   shape = to->shape;
1397   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1398
[647407d]1399   /* add to linked list which is dealt with after network is solved */
1400   link = osnew(nosurveylink);
[5a38209]1401   if (fToFirst) {
[90bb053f]1402      link->to = StnFromPfx(to);
1403      link->fr = StnFromPfx(fr);
[0395657]1404   } else {
[90bb053f]1405      link->fr = StnFromPfx(fr);
1406      link->to = StnFromPfx(to);
[0395657]1407   }
[b3bef47]1408   link->flags = pcs->flags;
[647407d]1409   link->next = nosurveyhead;
1410   nosurveyhead = link;
[a420b49]1411   return 1;
[d1b1380]1412}
[50f6901]1413
[5a38209]1414extern int
1415data_nosurvey(void)
1416{
1417   prefix *fr = NULL, *to = NULL;
1418
1419   bool fMulti = fFalse;
1420
1421   reading first_stn = End;
1422
1423   reading *ordering;
1424
1425   again:
1426
1427   for (ordering = pcs->ordering ; ; ordering++) {
1428      skipblanks();
1429      switch (*ordering) {
1430       case Fr:
[84c60fc]1431          fr = read_prefix_stn(fFalse, fTrue);
[5a38209]1432          if (first_stn == End) first_stn = Fr;
1433          break;
1434       case To:
[84c60fc]1435          to = read_prefix_stn(fFalse, fTrue);
[5a38209]1436          if (first_stn == End) first_stn = To;
1437          break;
1438       case Station:
1439          fr = to;
[84c60fc]1440          to = read_prefix_stn(fFalse, fFalse);
[5a38209]1441          first_stn = To;
1442          break;
1443       case Ignore:
1444         skipword(); break;
1445       case IgnoreAllAndNewLine:
1446         skipline();
1447         /* fall through */
1448       case Newline:
1449         if (fr != NULL) {
1450            int r;
1451            r = process_nosurvey(fr, to, first_stn == To);
1452            if (!r) skipline();
1453         }
[838a602a]1454         if (ordering[1] == End) {
1455            do {
1456               process_eol();
1457               process_bol();
1458            } while (isComm(ch));
[ee6a621]1459            if (!isData(ch)) {
[993454b]1460               push_back(ch);
[ee6a621]1461               return 1;
1462            }
[838a602a]1463            goto again;
1464         }
[5a38209]1465         fMulti = fTrue;
1466         while (1) {
1467            process_eol();
1468            process_bol();
1469            if (isData(ch)) break;
[ee6a621]1470            if (!isComm(ch)) {
[993454b]1471               push_back(ch);
[ee6a621]1472               return 1;
1473            }
[5a38209]1474         }
1475         break;
1476       case IgnoreAll:
1477         skipline();
1478         /* fall through */
1479       case End:
1480         if (!fMulti) {
[ee6a621]1481            int r = process_nosurvey(fr, to, first_stn == To);
[5a38209]1482            process_eol();
1483            return r;
1484         }
[eef4d8c]1485         do {
[5a38209]1486            process_eol();
1487            process_bol();
[eef4d8c]1488         } while (isComm(ch));
[5a38209]1489         goto again;
1490       default: BUG("Unknown reading in ordering");
1491      }
1492   }
1493}
1494
[50f6901]1495/* totally ignore a line of survey data */
1496extern int
1497data_ignore(void)
1498{
1499   skipline();
[ee6a621]1500   process_eol();
[570f4bf]1501   return 1;
[50f6901]1502}
Note: See TracBrowser for help on using the repository browser.