source: git/src/datain.c @ 527f14f

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 527f14f was 4c07c51, checked in by Olly Betts <olly@…>, 23 years ago

Renamed ASSERT to SVXASSERT to avoid clash with Allegro which also defines
it. Renamed ASSERT2 to SVXASSERT2 to match.

git-svn-id: file:///home/survex-svn/survex/trunk@2029 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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