source: git/src/datain.c @ 8dd6a56

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 8dd6a56 was 3e784b5, checked in by Olly Betts <olly@…>, 23 years ago

Fixed M_2_PI misunderstanding.

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

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