source: git/src/datain.c @ 72095d3

RELEASE/1.0RELEASE/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 72095d3 was 72095d3, checked in by Olly Betts <olly@…>, 23 years ago

BackCompass? now works in DIVING and CYLPOLAR styles.

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

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