-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdivecmd2grap.c
857 lines (798 loc) · 22.3 KB
/
divecmd2grap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/* $Id$ */
/*
* Copyright (c) 2017 Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/queue.h>
#include <assert.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <expat.h>
#include "parser.h"
enum pmode {
MODE_AGGREGATE = 0,
MODE_AGGREGATE_TEMP,
MODE_RESTING,
MODE_RESTING_SCATTER,
MODE_RSUMMARY,
MODE_SCATTER,
MODE_STACK,
MODE_STACK_TEMP,
MODE_SUMMARY,
MODE_TEMP,
MODE_VECTOR,
MODE__MAX
};
static const char *pmodetitles[MODE__MAX] = {
"Aggregate depths", /* MODE_AGGREGATE */
"Aggregate temperatures", /* MODE_AGGREGATE_TEMP */
"Recovery", /* MODE_RESTING */
"Recovery per time", /* MODE_RESTING_SCATTER */
"Depth and time summary", /* MODE_RSUMMARY */
"Depth per time", /* MODE_SCATTER */
"Stacked depths", /* MODE_STACK */
"Stacked temperatures", /* MODE_STACK_TEMP */
"Depth and time summary", /* MODE_SUMMARY */
"Temperature", /* MODE_TEMP */
"Depth vector", /* MODE_VECTOR */
};
static const char *pmodes[MODE__MAX] = {
"aggr", /* MODE_AGGREGATE */
"aggrtemp", /* MODE_AGGREGATE_TEMP */
"rest", /* MODE_RESTING */
"restscatter", /* MODE_RESTING_SCATTER */
"rsummary", /* MODE_RSUMMARY */
"scatter", /* MODE_SCATTER */
"stack", /* MODE_STACK */
"stacktemp", /* MODE_STACK_TEMP */
"summary", /* MODE_SUMMARY */
"temp", /* MODE_TEMP */
"vector", /* MODE_VECTOR */
};
/*
* How thick our lines will be (in points).
*/
#define LINE_THICKNESS 1.0
/*
* Our line colours.
* I just got these by looking at some gnuplot pallettes and roughly
* converting from hex to an X11 colour.
*/
#define COL_MAX 5
static const char *cols[COL_MAX] = {
"dodgerblue2",
"darkorange",
"mediumorchid",
"magenta4",
"limegreen",
};
int verbose = 0;
/* Print derivatives, aka velocity. */
static int derivs = 0;
/* Adjust values. */
static int adjust = 0;
static int
print_all(enum pmode mode, const struct diveq *dq, int first,
const struct divestat *st, const char *title)
{
struct dive *d, *dp;
struct samp *s;
size_t i = 0, j, maxtime = 0, maxrtime = 0,
ndives = 0, free = 0, rest, maxdtime = 0;
time_t t, lastt = 0;
double maxdepth = 0.0, lastdepth, x, y,
height = 3.8, width = 5.4, x2, y2,
mintemp = 100.0, maxtemp = 0.0;
struct dgroup *dg;
assert(MODE__MAX != mode);
assert( ! TAILQ_EMPTY(dq));
/* Start with basic accounting. */
TAILQ_FOREACH(d, dq, entries) {
free += MODE_FREEDIVE == d->mode;
ndives++;
}
/* These modes require a datetime stamp. */
if (MODE_AGGREGATE == mode ||
MODE_AGGREGATE_TEMP == mode ||
MODE_RESTING == mode ||
MODE_RESTING_SCATTER == mode)
TAILQ_FOREACH(d, dq, entries)
if (0 == d->datetime) {
warnx("%s: date and time required",
pmodes[mode]);
return 0;
}
if (MODE_RESTING == mode ||
MODE_RESTING_SCATTER == mode ||
MODE_VECTOR == mode ||
MODE_SUMMARY == mode ||
MODE_RSUMMARY == mode ||
MODE_TEMP == mode ||
MODE_SCATTER == mode)
if (ndives < 2) {
warnx("%s: multiple dives required",
pmodes[mode]);
return 0;
}
/* These modes require temperatures. */
if (MODE_STACK_TEMP == mode ||
MODE_TEMP == mode ||
MODE_AGGREGATE_TEMP == mode)
TAILQ_FOREACH(d, dq, entries)
if (0 == d->hastemp) {
warnx("%s: temperature required",
pmodes[mode]);
return 0;
}
/* Aggregate mode uses group extrema for axes. */
if (MODE_AGGREGATE == mode ||
MODE_AGGREGATE_TEMP == mode ||
MODE_RSUMMARY == mode)
TAILQ_FOREACH(d, dq, entries) {
t = (d->datetime + d->maxtime) -
d->group->mintime;
assert(t >= 0);
if ((size_t)t > maxtime)
maxtime = t;
}
/*
* The "rsummary" (relative summary) mode needs both maximum
* extent (maxdtime) and maximum per-dive time (maxtime).
*/
if (MODE_RSUMMARY == mode) {
maxdtime = maxtime;
maxtime = 0;
}
/* These (most) have time and depth extrema. */
if (MODE_SUMMARY == mode ||
MODE_RSUMMARY == mode ||
MODE_SCATTER == mode ||
MODE_TEMP == mode ||
MODE_VECTOR == mode ||
MODE_RESTING == mode ||
MODE_STACK == mode ||
MODE_STACK_TEMP == mode ||
MODE_RESTING_SCATTER == mode)
TAILQ_FOREACH(d, dq, entries) {
if (d->maxtime > maxtime)
maxtime = d->maxtime;
if (d->maxdepth > maxdepth)
maxdepth = d->maxdepth;
}
/*
* These have temperature extrema.
* XXX: maxtemp is really maxmintemp.
*/
if (MODE_AGGREGATE_TEMP == mode ||
MODE_TEMP == mode ||
MODE_STACK_TEMP == mode)
TAILQ_FOREACH(d, dq, entries) {
if (d->mintemp > maxtemp)
maxtemp = d->mintemp;
if (d->mintemp < mintemp)
mintemp = d->mintemp;
}
/* Require a non-zero spread. */
if (fabs(maxtemp - mintemp) < FLT_EPSILON)
maxtemp = mintemp + FLT_EPSILON;
/* These use subsequent dive times for extrema. */
if (MODE_RESTING == mode ||
MODE_RESTING_SCATTER == mode)
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
TAILQ_FOREACH(d, &dg->dives, gentries) {
dp = TAILQ_NEXT(d, gentries);
rest = NULL == dp ? 0 :
dp->datetime -
(d->datetime + d->maxtime);
if (rest > maxrtime)
maxrtime = rest;
}
}
/* Start with the frame of our box. */
if ( ! first)
puts(".bp");
printf(".G1\n"
"draw solid\n"
"frame invis ht %g wid %g left solid bot solid\n",
height, width);
/*
* Free-diving mode: print minimum optimal surface time.
* To do so, "square" the matrix and draw for twice the rest to
* surface time.
*/
if (NULL != title)
printf("label top \"%s\"\n", title);
if (MODE_RESTING_SCATTER == mode && free)
printf("line dashed 0.05 from 0,0 to %g,1\n",
2.0 * (maxtime / (double)maxrtime));
/* Now each mode ordered by alpha. */
switch (mode) {
case MODE_AGGREGATE_TEMP:
case MODE_STACK_TEMP:
printf("ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"grid right ticks off\n"
"grid top ticks off\n"
"label left \"Temp (\\[de]C)\" left 0.1\n"
"label bot \"Time (mm:ss)\"\n",
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60, maxtime % 60);
break;
case MODE_RESTING:
printf("ticks left out at "
"-1.0 \"%zu:%.02zu\", "
"-0.75 \"%zu:%.02zu\", "
"-0.5 \"%zu:%.02zu\", "
"-0.25 \"%zu:%.02zu\", "
"0.0, "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"grid left from -1 to 1 by 0.25 \"\"\n"
"ticks bot off\n"
"line from 0,0.0 to %zu,0.0\n"
"label right \"Rest time (mm:ss)\" up %g left 0.2\n"
"label left \"Dive time (mm:ss)\" down %g left 0.3\n"
"copy thru {\n"
" \"\\(bu\" size +3 color $5 at $1,$3\n"
" line dotted from $1,0 to $1,$3 color $5 thickness %g\n"
"%s"
" circle at $1,$2 color $5\n"
" line from $1,0 to $1,$2 color $5 thickness %g\n"
"}\n",
maxtime / 60, maxtime % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxrtime / 4) / 60, (maxrtime / 4) % 60,
(maxrtime / 2) / 60, (maxrtime / 2) % 60,
(3 * maxrtime / 4) / 60,
(3 * maxrtime / 4) % 60,
maxrtime / 60, maxrtime % 60,
ndives - 1, 0.25 * height, 0.25 * height,
LINE_THICKNESS,
free ? " \"\\(en\" at $1,$4\n" : "",
LINE_THICKNESS);
break;
case MODE_RESTING_SCATTER:
printf("ticks left out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"label left \"Dive time (mm:ss)\" left 0.15\n"
"label bot \"Rest time (mm:ss)\"\n"
"grid right ticks off\n"
"grid top ticks off\n"
"coord y 0,1\n"
"coord x 0,1\n"
"copy thru {\n"
" \"\\(bu\" size+3 color $5 at $2,-$3\n"
"}\n",
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60, maxtime % 60,
(maxrtime / 4) / 60, (maxrtime / 4) % 60,
(maxrtime / 2) / 60, (maxrtime / 2) % 60,
(3 * maxrtime / 4) / 60,
(3 * maxrtime / 4) % 60,
maxrtime / 60, maxrtime % 60);
break;
case MODE_SCATTER:
printf("ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"label left \"Depth (m)\" left 0.15\n"
"label bot \"Time (mm:ss)\"\n"
"grid right ticks off\n"
"grid top ticks off\n"
"coord y 0,-%g\n"
"coord x 0,1\n"
"copy thru {\n"
" \"\\(bu\" size +3 color $4 at $2,$3\n"
"}\n",
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60, maxtime % 60,
maxdepth);
break;
case MODE_RSUMMARY:
printf("ticks left out at "
"-1.0 \"-%.2f\", "
"-0.75 \"-%.2f\", "
"-0.5 \"-%.2f\", "
"-0.25 \"-%.2f\", "
"0.0, "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"grid left from -1 to 1 by 0.25 \"\"\n"
"line from 0,0.0 to 1.0,0.0\n"
"label right \"Time (mm:ss)\" up %g left 0.2\n"
"label left \"Depth (m)\" down %g left 0.3\n"
"copy thru {\n"
" \"\\(bu\" size +3 color $4 at $1,$3\n"
" line dashed 0.05 from $1,0 to $1,$3 color $4 thickness %g\n"
" circle at $1,$2 color $4\n"
" line from $1,0 to $1,$2 color $4 thickness %g\n"
"}\n",
maxdepth,
0.75 * maxdepth,
0.5 * maxdepth,
0.25 * maxdepth,
(maxtime / 4) / 60,
(maxtime / 4) % 60,
(maxtime / 2) / 60,
(maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60,
maxtime % 60,
(maxdtime / 4) / 60,
(maxdtime / 4) % 60,
(maxdtime / 2) / 60,
(maxdtime / 2) % 60,
(3 * maxdtime / 4) / 60,
(3 * maxdtime / 4) % 60,
maxdtime / 60,
maxdtime % 60,
0.25 * height, 0.25 * height,
LINE_THICKNESS, LINE_THICKNESS);
break;
case MODE_SUMMARY:
printf("ticks left out at "
"-1.0 \"-%.2f\", "
"-0.75 \"-%.2f\", "
"-0.5 \"-%.2f\", "
"-0.25 \"-%.2f\", "
"0.0, "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"grid left from -1 to 1 by 0.25 \"\"\n"
"ticks bot off\n"
"line from 0,0.0 to %zu,0.0\n"
"label right \"Time (mm:ss)\" up %g left 0.2\n"
"label left \"Depth (m)\" down %g left 0.3\n"
"copy thru {\n"
" \"\\(bu\" size +3 color $4 at $1,$3\n"
" line dashed 0.05 from $1,0 to $1,$3 color $4 thickness %g\n"
" circle at $1,$2 color $4\n"
" line from $1,0 to $1,$2 color $4 thickness %g\n"
"}\n",
maxdepth,
0.75 * maxdepth,
0.5 * maxdepth,
0.25 * maxdepth,
(maxtime / 4) / 60,
(maxtime / 4) % 60,
(maxtime / 2) / 60,
(maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60,
maxtime % 60, ndives - 1,
0.25 * height, 0.25 * height,
LINE_THICKNESS, LINE_THICKNESS);
break;
case MODE_TEMP:
printf("ticks left out at "
"-1.0 \"-%.2f\", "
"-0.75 \"-%.2f\", "
"-0.5 \"-%.2f\", "
"-0.25 \"-%.2f\", "
"0.0, "
"0.25 \"%.1f\", "
"0.5 \"%.1f\", "
"0.75 \"%.1f\", "
"1.0 \"%.1f\"\n"
"grid left from -1 to 1 by 0.25 \"\"\n"
"ticks bot off\n"
"line from 0,0.0 to %zu,0.0\n"
"label right \"Temp (\\[de]C)\" up %g left 0.2\n"
"label left \"Depth (m)\" down %g left 0.3\n"
"copy thru {\n"
" \"\\(bu\" size +3 color $4 at $1,$3\n"
" line dashed 0.05 from $1,0 to $1,$3 color $4 thickness %g\n"
" circle at $1,$2 color $4\n"
" line from $1,0 to $1,$2 color $4 thickness %g\n"
"}\n",
maxdepth, 0.75 * maxdepth,
0.5 * maxdepth, 0.25 * maxdepth,
adjust ? mintemp : 0.25 * maxtemp,
adjust ? mintemp + 0.33 * (maxtemp - mintemp) :
0.5 * maxtemp,
adjust ? mintemp + 0.66 * (maxtemp - mintemp) :
0.75 * maxtemp,
maxtemp, ndives - 1,
0.25 * height, 0.25 * height,
LINE_THICKNESS, LINE_THICKNESS);
break;
case MODE_VECTOR:
printf("ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"label left \"Depth (m)\" left 0.15\n"
"label bot \"Time (mm:ss)\"\n"
"grid right ticks off\n"
"grid top ticks off\n"
"coord y 0,-1\n"
"coord x 0,1\n",
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60, maxtime % 60);
break;
default:
printf("ticks bot out at "
"0.0 \"00:00\", "
"0.25 \"%zu:%.02zu\", "
"0.5 \"%zu:%.02zu\", "
"0.75 \"%zu:%.02zu\", "
"1.0 \"%zu:%.02zu\"\n"
"grid right ticks off\n"
"grid top ticks off\n"
"label left \"%s\" left 0.1\n"
"label bot \"Time (mm:ss)\"\n",
(maxtime / 4) / 60, (maxtime / 4) % 60,
(maxtime / 2) / 60, (maxtime / 2) % 60,
(3 * maxtime / 4) / 60,
(3 * maxtime / 4) % 60,
maxtime / 60, maxtime % 60,
! derivs ? "Depth (m)" :
"Velocity (vertical m/s)");
}
/* Now for the data, mode ordered by alpha. */
switch (mode) {
case MODE_AGGREGATE:
/*
* In aggregate mode, we iterate through each of the
* groups, then through all of the dives in those
* groups, because we connect between group lines.
* This is different from the other processing methods,
* were dives are independent.
*/
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
printf("new color \"%s\" thickness %g\n",
cols[dg->id % COL_MAX],
LINE_THICKNESS);
lastt = 0;
TAILQ_FOREACH(d, &dg->dives, gentries) {
lastdepth = 0.0;
x = (d->datetime - dg->mintime) /
(double)maxtime;
printf("%g 0\n", x);
TAILQ_FOREACH(s, &d->samps, entries) {
if ( ! (SAMP_DEPTH & s->flags))
continue;
t = s->time;
t += d->datetime;
t -= dg->mintime;
x = t / (double)maxtime;
y = (0 == derivs) ? -s->depth :
(lastt == t) ? 0.0 :
(lastdepth - s->depth) /
(t - lastt);
printf("%g %g\n", x, y);
lastdepth = s->depth;
lastt = t;
}
x = lastt / (double)maxtime;
printf("%g 0\n", x);
}
}
break;
case MODE_AGGREGATE_TEMP:
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
printf("new color \"%s\" thickness %g\n",
cols[dg->id % COL_MAX],
LINE_THICKNESS);
TAILQ_FOREACH(d, &dg->dives, gentries) {
TAILQ_FOREACH(s, &d->samps, entries) {
if ( ! (SAMP_TEMP & s->flags))
continue;
t = s->time;
t += d->datetime;
t -= dg->mintime;
x = t / (double)maxtime;
y = s->temp;
printf("%g %g\n", x, y);
}
}
}
break;
case MODE_RESTING:
case MODE_RESTING_SCATTER:
/*
* In the resting modes, we need to be within our group
* because we look to the next dive, which must also be
* in our group.
* FIXME: make this not so, so these can be ordered in
* the same way that MODE_SUMMARY is ordered.
*/
for (i = j = 0; i < st->groupsz; i++) {
dg = st->groups[i];
TAILQ_FOREACH(d, &dg->dives, gentries) {
dp = TAILQ_NEXT(d, gentries);
t = NULL == dp ? 0 :
dp->datetime -
(d->datetime + d->maxtime);
printf("%zu %g -%g %g \"%s\"\n", j++,
t / (double)maxrtime,
d->maxtime / (double)maxtime,
(d->maxtime * 2) / (double)maxrtime,
cols[d->group->id % COL_MAX]);
}
}
break;
case MODE_SCATTER:
TAILQ_FOREACH(d, dq, entries)
printf("%zu %g -%g \"%s\"\n", i++,
d->maxtime / (double)maxtime,
d->maxdepth,
cols[d->group->id % COL_MAX]);
break;
case MODE_STACK_TEMP:
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
printf("new color \"%s\" thickness %g\n",
cols[dg->id % COL_MAX],
LINE_THICKNESS);
TAILQ_FOREACH(d, &dg->dives, gentries) {
TAILQ_FOREACH(s, &d->samps, entries) {
if ( ! (SAMP_TEMP & s->flags))
continue;
t = s->time;
x = t / (double)maxtime;
y = s->temp;
printf("%g %g\n", x, y);
}
if (TAILQ_NEXT(d, gentries))
puts("new");
}
}
break;
case MODE_RSUMMARY:
TAILQ_FOREACH(d, dq, entries) {
t = d->datetime - d->group->mintime;
x = t / (double)maxdtime;
printf("%g %g -%g \"%s\"\n", x,
(double)d->maxtime / maxtime,
d->maxdepth / maxdepth,
cols[d->group->id % COL_MAX]);
}
break;
case MODE_SUMMARY:
TAILQ_FOREACH(d, dq, entries)
printf("%zu %g -%g \"%s\"\n", i++,
(double)d->maxtime / maxtime,
d->maxdepth / maxdepth,
cols[d->group->id % COL_MAX]);
break;
case MODE_TEMP:
TAILQ_FOREACH(d, dq, entries)
printf("%zu %g -%g \"%s\"\n", i++, adjust ?
0.25 + (0.75 *
((d->mintemp - mintemp) /
(maxtemp - mintemp))) :
d->mintemp / maxtemp,
d->maxdepth / maxdepth,
cols[d->group->id % COL_MAX]);
break;
case MODE_VECTOR:
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
j = 0;
TAILQ_FOREACH(d, &dg->dives, gentries) {
x = d->maxtime / (double)maxtime;
y = d->maxdepth / maxdepth;
printf("\"\\(bu\" size +3 "
"color \"%s\" at %g,-%g\n",
cols[d->group->id % COL_MAX],
x, y);
dp = TAILQ_NEXT(d, gentries);
if (NULL == dp)
break;
x2 = dp->maxtime / (double)maxtime;
y2 = dp->maxdepth / maxdepth;
printf("arrow from %g,-%g to %g,-%g "
"color \"grey%zu\"\n",
x, y, x2, y2, 60 - (size_t)(40 *
(j / (double)d->group->ndives)));
j++;
}
}
break;
default:
for (i = 0; i < st->groupsz; i++) {
dg = st->groups[i];
printf("new color \"%s\" thickness %g\n",
cols[dg->id % COL_MAX],
LINE_THICKNESS);
TAILQ_FOREACH(d, &dg->dives, gentries) {
puts("0 0");
lastdepth = 0.0;
TAILQ_FOREACH(s, &d->samps, entries) {
if ( ! (SAMP_DEPTH & s->flags))
continue;
t = s->time;
x = t / (double)maxtime;
y = 0 == derivs ? -s->depth :
lastt == t ? 0.0 :
(lastdepth - s->depth) /
(t - lastt);
printf("%g %g\n", x, y);
lastdepth = s->depth;
lastt = t;
}
x = lastt / (double)maxtime;
printf("%g 0\n", x);
if (NULL != (dp = TAILQ_NEXT(d, entries)))
puts("new");
}
}
break;
}
puts(".G2");
return(1);
}
int
main(int argc, char *argv[])
{
int c, rc = 1, first;
enum group group = GROUP_NONE;
size_t i;
XML_Parser p;
struct diveq dq;
struct divestat st;
enum pmode mode = MODE_STACK;
/* Pledge us early: only reading files. */
#if HAVE_PLEDGE
if (-1 == pledge("stdio rpath", NULL))
err(EXIT_FAILURE, "pledge");
#endif
while (-1 != (c = getopt(argc, argv, "adm:s:v")))
switch (c) {
case ('a'):
adjust = 1;
break;
case ('d'):
derivs = 1;
break;
case ('m'):
if (0 == strcasecmp(optarg, "all")) {
mode = MODE__MAX;
break;
}
for (mode = 0; mode < MODE__MAX; mode++)
if (0 == strcasecmp(optarg, pmodes[mode]))
break;
if (mode == MODE__MAX)
goto usage;
break;
case ('s'):
if (0 == strcasecmp(optarg, "date"))
group = GROUP_DATE;
else if (0 == strcasecmp(optarg, "diver"))
group = GROUP_DIVER;
else if (0 == strcasecmp(optarg, "none"))
group = GROUP_NONE;
else
goto usage;
break;
case ('v'):
verbose = 1;
break;
default:
goto usage;
}
argc -= optind;
argv += optind;
if (derivs &&
(MODE_AGGREGATE != mode &&
MODE__MAX != mode &&
MODE_STACK != mode))
warnx("-d: ignoring flag");
if (adjust &&
(MODE_TEMP != mode &&
MODE__MAX != mode))
warnx("-a: ignoring flag");
divecmd_init(&p, &dq, &st, group, GROUPSORT_DATETIME);
/*
* Handle all files or stdin.
* File "-" is interpreted as stdin.
*/
if (0 == argc)
rc = divecmd_parse("-", p, &dq, &st);
for (i = 0; i < (size_t)argc; i++)
if ( ! (rc = divecmd_parse(argv[i], p, &dq, &st)))
break;
#if HAVE_PLEDGE
if (-1 == pledge("stdio", NULL))
err(EXIT_FAILURE, "pledge");
#endif
XML_ParserFree(p);
if ( ! rc)
goto out;
if (TAILQ_EMPTY(&dq)) {
warnx("no dives to display");
goto out;
}
if (MODE__MAX == mode) {
for (first = 1, mode = 0; mode < MODE__MAX; mode++) {
rc = print_all(mode, &dq, first,
&st, pmodetitles[mode]);
if (rc)
first = 0;
}
rc = 1;
} else
rc = print_all(mode, &dq, 1, &st, NULL);
out:
divecmd_free(&dq, &st);
return rc ? EXIT_SUCCESS : EXIT_FAILURE;
usage:
fprintf(stderr, "usage: %s "
"[-adv] "
"[-m mode] "
"[-s splitgroup] "
"[file...]\n", getprogname());
return EXIT_FAILURE;
}