source: git/src/datain.c @ f11f2af

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

Merged from survex 1.0 branch.

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

  • Property mode set to 100644
File size: 36.9 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);
[0b71cfc]593      } else if (pcs->f90Up && diff_from_abs90 > -EPSILON) {
[fa42426]594         ctype = CTYPE_PLUMB;
[a420b49]595      }
596   }
[cb3d1e2]597
[fa42426]598   if (backctype == CTYPE_READING) {
[0b71cfc]599      real diff_from_abs90;
[b14f44f]600      backclin *= pcs->units[Q_BACKGRADIENT];
[fa42426]601      /* percentage scale */
602      if (pcs->f_backclino_percent) backclin = atan(backclin);
[0b71cfc]603      diff_from_abs90 = fabs(backclin) - M_PI_2;
604      if (diff_from_abs90 > EPSILON) {
605         /* FIXME: different message for BackClino? */
606         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
607      } else if (pcs->f90Up && diff_from_abs90 > -EPSILON) {
[fa42426]608         backctype = CTYPE_PLUMB;
[0b71cfc]609      }
610   }
611
[fa42426]612   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
613      compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
[b14f44f]614      return 0;
[0b71cfc]615   }
616
[fa42426]617   if (ctype == CTYPE_PLUMB || backctype == CTYPE_PLUMB) {
[a420b49]618      /* plumbed */
619      if (!fNoComp) {
[11e919a]620         /* FIXME: Different message for BackComp? */
[a420b49]621         compile_warning(/*Compass reading given on plumbed leg*/21);
622      }
623
624      dx = dy = (real)0.0;
[fa42426]625      if (ctype != CTYPE_OMIT) {
626         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
[b14f44f]627            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
[fa42426]628            compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
[b14f44f]629            return 0;
[0b71cfc]630         }
631         dz = (clin > (real)0.0) ? tape : -tape;
632      } else {
633         dz = (backclin < (real)0.0) ? tape : -tape;
634      }
[a420b49]635      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]636      vz = var(Q_POS) / 3.0 + VAR(Tape);
[4f8285d]637#ifndef NO_COVARIANCES
[26a805f]638      /* Correct values - no covariances in this case! */
639      cxy = cyz = czx = (real)0.0;
[4f8285d]640#endif
[a420b49]641   } else {
[fa42426]642      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
643       * or CTYPE_OMIT */
[a420b49]644      /* clino */
645      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
[421b7d2]646      if (fNoComp) {
[fa42426]647         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
[a420b49]648         return 0;
649      }
650      if (tape == (real)0.0) {
651         dx = dy = dz = (real)0.0;
652         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[4f8285d]653#ifndef NO_COVARIANCES
[a420b49]654         cxy = cyz = czx = (real)0.0;
[4f8285d]655#endif
[d1b1380]656#if DEBUG_DATAIN_1
[a420b49]657         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]658#endif
[a420b49]659      } else {
[dbb4e19]660         real sinGcosG;
[0b71cfc]661         /* take into account variance in LEVEL case */
662         real var_clin = var(Q_LEVEL);
[b6de07d]663         real var_comp;
[21c226e]664         real comp = handle_compass(&var_comp);
[fa42426]665         /* ctype != CTYPE_READING is LEVEL case */
666         if (ctype == CTYPE_READING) {
[a420b49]667            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
[21c226e]668            var_clin = VAR(Clino);
[0b71cfc]669         }
[fa42426]670         if (backctype == CTYPE_READING) {
[b14f44f]671            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
672               * pcs->sc[Q_BACKGRADIENT];
[fa42426]673            if (ctype == CTYPE_READING) {
[b14f44f]674               if (sqrd((clin + backclin) / 3.0) >
[21c226e]675                     var_clin + VAR(BackClino)) {
[b14f44f]676                  /* fore and back readings differ by more than 3 sds */
[fa42426]677                  warn_readings_differ(/*Clino reading and back clino reading disagree by %s degrees*/99, clin + backclin);
[b14f44f]678               }
[21c226e]679               clin = (clin / var_clin - backclin / VAR(BackClino));
680               var_clin = (var_clin + VAR(BackClino)) / 4;
[b14f44f]681               clin *= var_clin;
[0b71cfc]682            } else {
683               clin = -backclin;
[21c226e]684               var_clin = VAR(BackClino);
[0b71cfc]685            }
686         }
[d1b1380]687
688#if DEBUG_DATAIN
[a420b49]689         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
[d1b1380]690#endif
[a420b49]691         cosG = cos(clin);
692         LcosG = tape * cosG;
693         sinB = sin(comp);
694         cosB = cos(comp);
[d1b1380]695#if DEBUG_DATAIN_1
[a420b49]696         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
[d1b1380]697#endif
[a420b49]698         dx = LcosG * sinB;
699         dy = LcosG * cosB;
700         dz = tape * sin(clin);
[d1b1380]701/*      printf("%.2f\n",clin); */
702#if DEBUG_DATAIN_1
[a420b49]703         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
[d1b1380]704#endif
[a420b49]705         dx2 = dx * dx;
706         L2 = tape * tape;
[21c226e]707         V = VAR(Tape) / L2;
[a420b49]708         dy2 = dy * dy;
709         cosG2 = cosG * cosG;
[dbb4e19]710         sinGcosG = sin(clin) * cosG;
[a420b49]711         dz2 = dz * dz;
[0b71cfc]712         v = dz2 * var_clin;
[5b7c1b7]713#ifdef NO_COVARIANCES
[0b71cfc]714         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[a420b49]715               (.5 + sinB * sinB * cosG2) * v);
[0b71cfc]716         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[a420b49]717               (.5 + cosB * cosB * cosG2) * v);
[fa42426]718         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]719            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]720            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]721         } else {
[0b71cfc]722            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]723         }
[a420b49]724         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
[dbb4e19]725#else
[0b71cfc]726         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[dbb4e19]727            (sinB * sinB * v);
[0b71cfc]728         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[dbb4e19]729            (cosB * cosB * v);
[fa42426]730         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]731            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]732            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]733         } else {
[0b71cfc]734            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]735         }
[647407d]736         /* usual covariance formulae are fine in no clino case since
[0b71cfc]737          * dz = 0 so value of var_clin is ignored */
[21c226e]738         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
[0b71cfc]739               - var_comp * dx * dy;
[21c226e]740         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
741         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
[dbb4e19]742#if 0
743         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
744         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
745#endif
[4f8285d]746#endif
[d1b1380]747#if DEBUG_DATAIN_1
[a420b49]748         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]749#endif
[a420b49]750      }
751   }
[d1b1380]752#if DEBUG_DATAIN_1
[a420b49]753   printf("Just before addleg, vx = %f\n", vx);
[d1b1380]754#endif
[a420b49]755   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
[90bb053f]756   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[4f8285d]757#ifndef NO_COVARIANCES
[2140502]758                , cyz, czx, cxy
[4f8285d]759#endif
[2140502]760                );
[647407d]761
762#ifdef NEW3DFORMAT
763   if (fUseNewFormat) {
[407084d]764      /* new twiglet and insert into twig tree */
765      twig *twiglet = osnew(twig);
[90bb053f]766      twiglet->from = fr;
767      twiglet->to = to;
[407084d]768      twiglet->down = twiglet->right = NULL;
769      twiglet->source = twiglet->drawings
770        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
771      twiglet->up = limb->up;
772      limb->right = twiglet;
773      limb = twiglet;
774
775      /* record pre-fettling deltas */
[647407d]776      twiglet->delta[0] = dx;
777      twiglet->delta[1] = dy;
778      twiglet->delta[2] = dz;
779   }
780#endif
[a420b49]781   return 1;
[d1b1380]782}
783
[5a38209]784static int
[21c226e]785process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[a420b49]786{
[21c226e]787   real tape = VAL(Tape);
788
[a420b49]789   real dx, dy, dz;
790   real vx, vy, vz;
[f795df0]791#ifndef NO_COVARIANCES
792   real cxy = 0, cyz = 0, czx = 0;
793#endif
[a420b49]794
[21c226e]795   handle_comp_units();
[a420b49]796
[a6d094f]797   /* depth gauge readings increase upwards with default calibration */
[6114207]798   if (fDepthChange) {
[21c226e]799      SVX_ASSERT(VAL(FrDepth) == 0.0);
800      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
801      dz *= pcs->sc[Q_DEPTH];
[a6d094f]802   } else {
[21c226e]803      dz = VAL(ToDepth) - VAL(FrDepth);
804      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]805   }
[a420b49]806
807   /* adjusted tape is negative -- probably the calibration is wrong */
808   if (tape < (real)0.0) {
809      compile_warning(/*Negative adjusted tape reading*/79);
810   }
811
812   /* check if tape is less than depth change */
813   if (tape < fabs(dz)) {
[bd1913f]814      /* FIXME: allow margin of error based on variances? */
[a420b49]815      compile_warning(/*Tape reading is less than change in depth*/62);
816   }
817
[0a208f9]818   if (tape == (real)0.0 && dz == 0.0) {
[a420b49]819      dx = dy = dz = (real)0.0;
820      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[21c226e]821   } else if (VAL(Comp) == HUGE_REAL &&
822              VAL(BackComp) == HUGE_REAL) {
[385b703]823      /* plumb */
824      dx = dy = (real)0.0;
825      if (dz < 0) tape = -tape;
[21c226e]826      /* FIXME: Should use FrDepth sometimes... */
827      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
828         / (VAR(Tape) * 2 * VAR(ToDepth));
[385b703]829      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]830      /* FIXME: Should use FrDepth sometimes... */
831      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
832                              / (VAR(Tape) + VAR(ToDepth));
[a420b49]833   } else {
[f795df0]834      real L2, sinB, cosB, dz2, D2;
[b6de07d]835      real var_comp;
[21c226e]836      real comp = handle_compass(&var_comp);
[a420b49]837      sinB = sin(comp);
838      cosB = cos(comp);
839      L2 = tape * tape;
840      dz2 = dz * dz;
841      D2 = L2 - dz2;
842      if (D2 <= (real)0.0) {
[21c226e]843         /* FIXME: Should use FrDepth sometimes... */
844         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
[a420b49]845         dx = dy = (real)0.0;
[f795df0]846         vx = vy = var(Q_POS) / 3.0;
[21c226e]847         /* FIXME: Should use FrDepth sometimes... */
848         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
[f795df0]849         if (dz > 0) {
[21c226e]850            /* FIXME: Should use FrDepth sometimes... */
851            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]852         } else {
[21c226e]853            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]854         }
[a420b49]855      } else {
856         real D = sqrt(D2);
[21c226e]857         /* FIXME: Should use FrDepth sometimes... */
858         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
[a420b49]859         dx = D * sinB;
860         dy = D * cosB;
[f795df0]861
862         vx = var(Q_POS) / 3.0 +
[72095d3]863            sinB * sinB * F / D2 + var_comp * dy * dy;
[f795df0]864         vy = var(Q_POS) / 3.0 +
[72095d3]865            cosB * cosB * F / D2 + var_comp * dx * dx;
[21c226e]866         /* FIXME: Should use FrDepth sometimes... */
867         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[f795df0]868
869#ifndef NO_COVARIANCES
[72095d3]870         cxy = sinB * cosB * (F / D2 + var_comp * D2);
[21c226e]871         /* FIXME: Should use FrDepth sometimes... */
872         cyz = -2 * VAR(ToDepth) * dy / D;
873         czx = -2 * VAR(ToDepth) * dx / D;
[f795df0]874#endif
[a420b49]875      }
876   }
[5a38209]877   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[2140502]878#ifndef NO_COVARIANCES
[f795df0]879                , cxy, cyz, czx
[647407d]880#endif
[2140502]881                );
[647407d]882#ifdef NEW3DFORMAT
883   if (fUseNewFormat) {
[407084d]884      /*new twiglet and insert into twig tree*/
885      twig *twiglet = osnew(twig);
[90bb053f]886      twiglet->from = fr;
887      twiglet->to = to;
[407084d]888      twiglet->down = twiglet->right = NULL;
889      twiglet->source = twiglet->drawings
890        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
891      twiglet->up = limb->up;
892      limb->right = twiglet;
893      limb = twiglet;
894
895      /* record pre-fettling deltas */
[647407d]896      twiglet->delta[0] = dx;
897      twiglet->delta[1] = dy;
898      twiglet->delta[2] = dz;
899   }
900#endif
901
902   return 1;
903}
904
[5a38209]905static int
[21c226e]906process_cartesian(prefix *fr, prefix *to, bool fToFirst)
[5a38209]907{
[21c226e]908   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
909   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
910   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
[647407d]911
[21c226e]912   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
[647407d]913#ifndef NO_COVARIANCES
[c80bd34]914                , 0, 0, 0
[647407d]915#endif
[2140502]916                );
[647407d]917
918#ifdef NEW3DFORMAT
919   if (fUseNewFormat) {
[407084d]920      /* new twiglet and insert into twig tree */
921      twig *twiglet = osnew(twig);
[90bb053f]922      twiglet->from = fr;
923      twiglet->to = to;
[407084d]924      twiglet->down = twiglet->right = NULL;
925      twiglet->source = twiglet->drawings
926        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
927      twiglet->up = limb->up;
928      limb->right = twiglet;
929      limb = twiglet;
930
931      /* record pre-fettling deltas */
[647407d]932      twiglet->delta[0] = dx;
933      twiglet->delta[1] = dy;
934      twiglet->delta[2] = dz;
935   }
[4f8285d]936#endif
[647407d]937
938   return 1;
939}
940
941extern int
[5a38209]942data_cartesian(void)
[647407d]943{
[90bb053f]944   prefix *fr = NULL, *to = NULL;
[5a38209]945
946   bool fMulti = fFalse;
[647407d]947
[0395657]948   reading first_stn = End;
949
950   reading *ordering;
[647407d]951
[5a38209]952   again:
953
[647407d]954   for (ordering = pcs->ordering ; ; ordering++) {
955      skipblanks();
956      switch (*ordering) {
[4a6a094]957       case Fr:
[21c226e]958         fr = read_prefix_stn(fFalse, fTrue);
959         if (first_stn == End) first_stn = Fr;
960         break;
[4a6a094]961       case To:
[21c226e]962         to = read_prefix_stn(fFalse, fTrue);
963         if (first_stn == End) first_stn = To;
964         break;
[5a38209]965       case Station:
[21c226e]966         fr = to;
967         to = read_prefix_stn(fFalse, fFalse);
968         first_stn = To;
969         break;
970       case Dx: case Dy: case Dz:
971         read_reading(*ordering, fFalse);
972         break;
[647407d]973       case Ignore:
974         skipword(); break;
[5a38209]975       case IgnoreAllAndNewLine:
976         skipline();
977         /* fall through */
978       case Newline:
979         if (fr != NULL) {
980            int r;
[21c226e]981            r = process_cartesian(fr, to, first_stn == To);
[5a38209]982            if (!r) skipline();
983         }
984         fMulti = fTrue;
985         while (1) {
986            process_eol();
987            process_bol();
988            if (isData(ch)) break;
[ee6a621]989            if (!isComm(ch)) {
[993454b]990               push_back(ch);
[ee6a621]991               return 1;
992            }
[5a38209]993         }
994         break;
[647407d]995       case IgnoreAll:
996         skipline();
997         /* fall through */
998       case End:
[5a38209]999         if (!fMulti) {
[21c226e]1000            int r = process_cartesian(fr, to, first_stn == To);
[5a38209]1001            process_eol();
1002            return r;
1003         }
[eef4d8c]1004         do {
[5a38209]1005            process_eol();
1006            process_bol();
[eef4d8c]1007         } while (isComm(ch));
[5a38209]1008         goto again;
[0395657]1009       default: BUG("Unknown reading in ordering");
[647407d]1010      }
1011   }
[5a38209]1012}
[cb3d1e2]1013
[98a2eec]1014static int
[21c226e]1015process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[98a2eec]1016{
[21c226e]1017   real tape = VAL(Tape);
1018   real frdepth = VAL(FrDepth);
1019   real todepth = VAL(ToDepth);
1020
[98a2eec]1021   real dx, dy, dz;
1022   real vx, vy, vz;
1023#ifndef NO_COVARIANCES
1024   real cxy = 0;
1025#endif
1026
[21c226e]1027   handle_comp_units();
[98a2eec]1028
[a6d094f]1029   /* depth gauge readings increase upwards with default calibration */
[6114207]1030   if (fDepthChange) {
[21c226e]1031      SVX_ASSERT(VAL(FrDepth) == 0.0);
1032      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1033      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1034   } else {
[21c226e]1035      dz = VAL(ToDepth) - VAL(FrDepth);
1036      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1037   }
[98a2eec]1038
1039   /* adjusted tape is negative -- probably the calibration is wrong */
1040   if (tape < (real)0.0) {
1041      compile_warning(/*Negative adjusted tape reading*/79);
1042   }
1043
[21c226e]1044   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
[98a2eec]1045      /* plumb */
1046      dx = dy = (real)0.0;
1047      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1048      /* FIXME: Should use FrDepth sometimes... */
1049      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1050   } else {
1051      real sinB, cosB;
[b6de07d]1052      real var_comp;
[21c226e]1053      real comp = handle_compass(&var_comp);
[98a2eec]1054      sinB = sin(comp);
1055      cosB = cos(comp);
1056
1057      dx = tape * sinB;
1058      dy = tape * cosB;
1059
1060      vx = var(Q_POS) / 3.0 +
[21c226e]1061         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
[98a2eec]1062      vy = var(Q_POS) / 3.0 +
[21c226e]1063         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1064      /* FIXME: Should use FrDepth sometimes... */
1065      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1066
1067#ifndef NO_COVARIANCES
[21c226e]1068      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
[98a2eec]1069#endif
1070   }
1071   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1072#ifndef NO_COVARIANCES
1073                , cxy, 0, 0
1074#endif
1075                );
1076#ifdef NEW3DFORMAT
1077   if (fUseNewFormat) {
1078      /*new twiglet and insert into twig tree*/
1079      twig *twiglet = osnew(twig);
1080      twiglet->from = fr;
1081      twiglet->to = to;
1082      twiglet->down = twiglet->right = NULL;
1083      twiglet->source = twiglet->drawings
1084        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1085      twiglet->up = limb->up;
1086      limb->right = twiglet;
1087      limb = twiglet;
1088
1089      /* record pre-fettling deltas */
1090      twiglet->delta[0] = dx;
1091      twiglet->delta[1] = dy;
1092      twiglet->delta[2] = dz;
1093   }
1094#endif
1095
1096   return 1;
1097}
1098
[107b8bd]1099/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1100 * Also handles topofil (fromcount/tocount or count) in place of tape */
[54c4612]1101extern int
[107b8bd]1102data_normal(void)
[54c4612]1103{
1104   prefix *fr = NULL, *to = NULL;
[107b8bd]1105   reading first_stn = End;
[54c4612]1106
[107b8bd]1107   bool fTopofil = fFalse, fMulti = fFalse;
1108   bool fRev;
[e217d67]1109   clino_type ctype, backctype;
[107b8bd]1110   bool fDepthChange;
[54c4612]1111
1112   reading *ordering;
1113
[21c226e]1114   VAL(Tape) = 0;
1115   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1116   VAL(FrCount) = VAL(ToCount) = 0;
1117   VAL(FrDepth) = VAL(ToDepth) = 0;
[54c4612]1118
1119   fRev = fFalse;
[fa42426]1120   ctype = backctype = CTYPE_OMIT;
[6114207]1121   fDepthChange = fFalse;
[54c4612]1122
[107b8bd]1123   /* ordering may omit clino reading, so set up default here */
1124   /* this is also used if clino reading is the omit character */
[21c226e]1125   VAL(Clino) = VAL(BackClino) = 0;
1126
1127   again:
[107b8bd]1128
[54c4612]1129   for (ordering = pcs->ordering; ; ordering++) {
1130      skipblanks();
1131      switch (*ordering) {
1132       case Fr:
[107b8bd]1133          fr = read_prefix_stn(fFalse, fTrue);
1134          if (first_stn == End) first_stn = Fr;
1135          break;
[54c4612]1136       case To:
[107b8bd]1137          to = read_prefix_stn(fFalse, fTrue);
1138          if (first_stn == End) first_stn = To;
1139          break;
[54c4612]1140       case Station:
[107b8bd]1141          fr = to;
1142          to = read_prefix_stn(fFalse, fFalse);
1143          first_stn = To;
1144          break;
[ee1ec59]1145       case Dir: {
1146          typedef enum {
1147             DIR_NULL=-1, DIR_FORE, DIR_BACK
1148          } dir_tok;
1149          static sztok dir_tab[] = {
1150             {"B",     DIR_BACK},
1151             {"F",     DIR_FORE},
1152          };
1153          dir_tok tok;
1154          get_token();
1155          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1156          switch (tok) {
1157           case DIR_FORE:
[54c4612]1158             break;
[ee1ec59]1159           case DIR_BACK:
[54c4612]1160             fRev = fTrue;
1161             break;
1162           default:
[ee1ec59]1163             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1164                                buffer);
[54c4612]1165             process_eol();
1166             return 0;
1167          }
1168          break;
[ee1ec59]1169       }
[107b8bd]1170       case Tape:
[21c226e]1171          read_reading(Tape, fFalse);
1172          if (VAL(Tape) < (real)0.0)
1173             compile_warning(/*Negative tape reading*/60);
[107b8bd]1174          break;
1175       case Count:
[21c226e]1176          VAL(FrCount) = VAL(ToCount);
1177          read_reading(ToCount, fFalse);
[107b8bd]1178          break;
1179       case FrCount:
[21c226e]1180          read_reading(FrCount, fFalse);
[107b8bd]1181          break;
1182       case ToCount:
[21c226e]1183          read_reading(ToCount, fFalse);
[107b8bd]1184          fTopofil = fTrue;
1185          break;
1186       case Comp:
[21c226e]1187          read_bearing_or_omit(Comp);
[107b8bd]1188          break;
[5b7c1b7]1189       case BackComp:
[21c226e]1190          read_bearing_or_omit(BackComp);
[5b7c1b7]1191          break;
[5757725]1192       case Clino:
[21c226e]1193          read_reading(Clino, fTrue);
1194          if (VAL(Clino) == HUGE_REAL) {
1195             VAL(Clino) = handle_plumb(&ctype);
1196             if (VAL(Clino) != HUGE_REAL) break;
[107b8bd]1197             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1198             process_eol();
1199             return 0;
1200          }
[fa42426]1201          ctype = CTYPE_READING;
[5b7c1b7]1202          break;
1203       case BackClino:
[21c226e]1204          read_reading(BackClino, fTrue);
1205          if (VAL(BackClino) == HUGE_REAL) {
1206             VAL(BackClino) = handle_plumb(&backctype);
1207             if (VAL(BackClino) != HUGE_REAL) break;
[5b7c1b7]1208             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1209             process_eol();
1210             return 0;
1211          }
[fa42426]1212          backctype = CTYPE_READING;
[107b8bd]1213          break;
1214       case FrDepth:
[21c226e]1215          read_reading(FrDepth, fFalse);
[107b8bd]1216          break;
1217       case ToDepth:
[21c226e]1218          read_reading(ToDepth, fFalse);
[107b8bd]1219          break;
[54c4612]1220       case Depth:
[21c226e]1221          VAL(FrDepth) = VAL(ToDepth);
1222          read_reading(ToDepth, fFalse);
[54c4612]1223          break;
[6114207]1224       case DepthChange:
1225          fDepthChange = fTrue;
[21c226e]1226          VAL(FrDepth) = 0;
1227          read_reading(ToDepth, fFalse);
[a186573]1228          break;
[107b8bd]1229       case Ignore:
1230          skipword();
1231          break;
[54c4612]1232       case IgnoreAllAndNewLine:
[107b8bd]1233          skipline();
1234          /* fall through */
[54c4612]1235       case Newline:
[107b8bd]1236          if (fr != NULL) {
1237             int r;
[21c226e]1238             if (fTopofil)
1239                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1240             /* Note: frdepth == todepth test works regardless of fDepthChange
1241              * (frdepth always zero, todepth is change of depth) and also
1242              * works for STYLE_NORMAL (both remain 0) */
[21c226e]1243             if (pcs->f0Eq && VAL(Tape) == (real)0.0 &&
1244                 VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1245                process_equate(fr, to);
1246                goto inferred_equate;
1247             }
[e7576f6]1248             if (fRev) {
1249                prefix *t = fr;
1250                fr = to;
1251                to = t;
1252             }
[107b8bd]1253             if (fTopofil) {
[21c226e]1254                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1255             } else {
[21c226e]1256                VAL(Tape) *= pcs->units[Q_LENGTH];
1257                VAL(Tape) -= pcs->z[Q_LENGTH];
1258                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1259             }
1260             switch (pcs->style) {
1261              case STYLE_NORMAL:
[21c226e]1262                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1263                                   ctype, backctype);
[107b8bd]1264                break;
1265              case STYLE_DIVING:
[21c226e]1266                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1267                                   fDepthChange);
[107b8bd]1268                break;
1269              case STYLE_CYLPOLAR:
[21c226e]1270                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1271                                     fDepthChange);
[107b8bd]1272                break;
1273              default:
1274                BUG("bad style");
1275             }
1276             if (!r) skipline();
[e7576f6]1277             
1278             /* Swap fr and to back to how they were for next line */
1279             if (fRev) {
1280                prefix *t = fr;
1281                fr = to;
1282                to = t;
1283             }
[107b8bd]1284          }
[21c226e]1285
1286          fRev = fFalse;
1287          ctype = backctype = CTYPE_OMIT;
1288          fDepthChange = fFalse;
1289
1290          /* ordering may omit clino reading, so set up default here */
1291          /* this is also used if clino reading is the omit character */
1292          VAL(Clino) = VAL(BackClino) = 0;
1293
[107b8bd]1294          inferred_equate:
[e7576f6]1295
[107b8bd]1296          fMulti = fTrue;
1297          while (1) {
1298              process_eol();
1299              process_bol();
1300              if (isData(ch)) break;
1301              if (!isComm(ch)) {
1302                 push_back(ch);
1303                 return 1;
1304              }
1305          }
1306          break;
[54c4612]1307       case IgnoreAll:
[107b8bd]1308          skipline();
1309          /* fall through */
[54c4612]1310       case End:
[107b8bd]1311          if (!fMulti) {
1312             int r;
1313             if (fRev) {
1314                prefix *t = fr;
1315                fr = to;
1316                to = t;
1317             }
[21c226e]1318             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1319             /* Note: frdepth == todepth test works regardless of fDepthChange
1320              * (frdepth always zero, todepth is change of depth) and also
1321              * works for STYLE_NORMAL (both remain 0) */
[21c226e]1322             if (pcs->f0Eq && VAL(Tape) == (real)0.0 &&
1323                 VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1324                process_equate(fr, to);
1325                process_eol();
1326                return 1;
1327             }
1328             if (fTopofil) {
[21c226e]1329                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1330             } else {
[21c226e]1331                VAL(Tape) *= pcs->units[Q_LENGTH];
1332                VAL(Tape) -= pcs->z[Q_LENGTH];
1333                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1334             }
1335             switch (pcs->style) {
1336              case STYLE_NORMAL:
[21c226e]1337                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1338                                   ctype, backctype);
[107b8bd]1339                break;
1340              case STYLE_DIVING:
[21c226e]1341                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1342                                   fDepthChange);
[107b8bd]1343                break;
1344              case STYLE_CYLPOLAR:
[21c226e]1345                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1346                                     fDepthChange);
[107b8bd]1347                break;
1348              default:
1349                BUG("bad style");
1350             }
1351             process_eol();
1352             return r;
1353          }
1354          do {
1355             process_eol();
1356             process_bol();
1357          } while (isComm(ch));
1358          goto again;
1359       default:
1360          BUG("Unknown reading in ordering");
[54c4612]1361      }
1362   }
1363}
1364
[5a38209]1365static int
1366process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1367{
1368   nosurveylink *link;
[838a602a]1369   int shape;
[647407d]1370
1371#ifdef NEW3DFORMAT
1372   if (fUseNewFormat) {
[407084d]1373      /* new twiglet and insert into twig tree */
1374      twig *twiglet = osnew(twig);
[90bb053f]1375      twiglet->from = fr;
1376      twiglet->to = to;
[647407d]1377      twiglet->down = twiglet->right = NULL;
1378      twiglet->source = twiglet->drawings
1379        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1380      twiglet->up = limb->up;
1381      limb->right = twiglet;
1382      limb = twiglet;
[3bcbcb2]1383      /* delta is only used to calculate error - pass zero and cope
1384       * elsewhere */
[647407d]1385      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1386   }
1387#endif
1388
[838a602a]1389   /* Suppress "unused fixed point" warnings for these stations
1390    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1391    */
1392   shape = fr->shape;
1393   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1394   shape = to->shape;
1395   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1396
[647407d]1397   /* add to linked list which is dealt with after network is solved */
1398   link = osnew(nosurveylink);
[5a38209]1399   if (fToFirst) {
[90bb053f]1400      link->to = StnFromPfx(to);
1401      link->fr = StnFromPfx(fr);
[0395657]1402   } else {
[90bb053f]1403      link->fr = StnFromPfx(fr);
1404      link->to = StnFromPfx(to);
[0395657]1405   }
[b3bef47]1406   link->flags = pcs->flags;
[647407d]1407   link->next = nosurveyhead;
1408   nosurveyhead = link;
[a420b49]1409   return 1;
[d1b1380]1410}
[50f6901]1411
[5a38209]1412extern int
1413data_nosurvey(void)
1414{
1415   prefix *fr = NULL, *to = NULL;
1416
1417   bool fMulti = fFalse;
1418
1419   reading first_stn = End;
1420
1421   reading *ordering;
1422
1423   again:
1424
1425   for (ordering = pcs->ordering ; ; ordering++) {
1426      skipblanks();
1427      switch (*ordering) {
1428       case Fr:
[84c60fc]1429          fr = read_prefix_stn(fFalse, fTrue);
[5a38209]1430          if (first_stn == End) first_stn = Fr;
1431          break;
1432       case To:
[84c60fc]1433          to = read_prefix_stn(fFalse, fTrue);
[5a38209]1434          if (first_stn == End) first_stn = To;
1435          break;
1436       case Station:
1437          fr = to;
[84c60fc]1438          to = read_prefix_stn(fFalse, fFalse);
[5a38209]1439          first_stn = To;
1440          break;
1441       case Ignore:
1442         skipword(); break;
1443       case IgnoreAllAndNewLine:
1444         skipline();
1445         /* fall through */
1446       case Newline:
1447         if (fr != NULL) {
1448            int r;
1449            r = process_nosurvey(fr, to, first_stn == To);
1450            if (!r) skipline();
1451         }
[838a602a]1452         if (ordering[1] == End) {
1453            do {
1454               process_eol();
1455               process_bol();
1456            } while (isComm(ch));
[ee6a621]1457            if (!isData(ch)) {
[993454b]1458               push_back(ch);
[ee6a621]1459               return 1;
1460            }
[838a602a]1461            goto again;
1462         }
[5a38209]1463         fMulti = fTrue;
1464         while (1) {
1465            process_eol();
1466            process_bol();
1467            if (isData(ch)) break;
[ee6a621]1468            if (!isComm(ch)) {
[993454b]1469               push_back(ch);
[ee6a621]1470               return 1;
1471            }
[5a38209]1472         }
1473         break;
1474       case IgnoreAll:
1475         skipline();
1476         /* fall through */
1477       case End:
1478         if (!fMulti) {
[ee6a621]1479            int r = process_nosurvey(fr, to, first_stn == To);
[5a38209]1480            process_eol();
1481            return r;
1482         }
[eef4d8c]1483         do {
[5a38209]1484            process_eol();
1485            process_bol();
[eef4d8c]1486         } while (isComm(ch));
[5a38209]1487         goto again;
1488       default: BUG("Unknown reading in ordering");
1489      }
1490   }
1491}
1492
[50f6901]1493/* totally ignore a line of survey data */
1494extern int
1495data_ignore(void)
1496{
1497   skipline();
[ee6a621]1498   process_eol();
[570f4bf]1499   return 1;
[50f6901]1500}
Note: See TracBrowser for help on using the repository browser.