forked from subsurface/subsurface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dive.c
2083 lines (1838 loc) · 49.4 KB
/
dive.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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dive.c */
/* maintains the internal dive list structure */
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include "gettext.h"
#include "dive.h"
struct tag_entry *g_tag_list = NULL;
void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name)
{
struct event *ev, **p;
unsigned int size, len = strlen(name);
size = sizeof(*ev) + len + 1;
ev = malloc(size);
if (!ev)
return;
memset(ev, 0, size);
memcpy(ev->name, name, len);
ev->time.seconds = time;
ev->type = type;
ev->flags = flags;
ev->value = value;
p = &dc->events;
/* insert in the sorted list of events */
while (*p && (*p)->time.seconds <= time)
p = &(*p)->next;
ev->next = *p;
*p = ev;
remember_event(name);
}
int get_pressure_units(unsigned int mb, const char **units)
{
int pressure;
const char* unit;
struct units *units_p = get_units();
switch (units_p->pressure) {
case PASCAL:
pressure = mb * 100;
unit = translate("gettextFromC","pascal");
break;
case BAR:
pressure = (mb + 500) / 1000;
unit = translate("gettextFromC","bar");
break;
case PSI:
pressure = mbar_to_PSI(mb);
unit = translate("gettextFromC","psi");
break;
}
if (units)
*units = unit;
return pressure;
}
double get_temp_units(unsigned int mk, const char **units)
{
double deg;
const char *unit;
struct units *units_p = get_units();
if (units_p->temperature == FAHRENHEIT) {
deg = mkelvin_to_F(mk);
unit = UTF8_DEGREE "F";
} else {
deg = mkelvin_to_C(mk);
unit = UTF8_DEGREE "C";
}
if (units)
*units = unit;
return deg;
}
double get_volume_units(unsigned int ml, int *frac, const char **units)
{
int decimals;
double vol;
const char *unit;
struct units *units_p = get_units();
switch (units_p->volume) {
case LITER:
vol = ml / 1000.0;
unit = translate("gettextFromC","l");
decimals = 1;
break;
case CUFT:
vol = ml_to_cuft(ml);
unit = translate("gettextFromC","cuft");
decimals = 2;
break;
}
if (frac)
*frac = decimals;
if (units)
*units = unit;
return vol;
}
unsigned int units_to_depth(double depth)
{
if (get_units()->length == METERS)
return rint(depth * 1000);
return feet_to_mm(depth);
}
double get_depth_units(unsigned int mm, int *frac, const char **units)
{
int decimals;
double d;
const char *unit;
struct units *units_p = get_units();
switch (units_p->length) {
case METERS:
d = mm / 1000.0;
unit = translate("gettextFromC","m");
decimals = d < 20;
break;
case FEET:
d = mm_to_feet(mm);
unit = translate("gettextFromC","ft");
decimals = 0;
break;
}
if (frac)
*frac = decimals;
if (units)
*units = unit;
return d;
}
double get_vertical_speed_units(unsigned int mms, int *frac, const char **units)
{
double d;
const char *unit;
const struct units *units_p = get_units();
const double time_factor = units_p->vertical_speed_time == MINUTES ? 60.0 : 1.0;
switch (units_p->length) {
case METERS:
d = mms / 1000.0 * time_factor;
unit = translate("gettextFromC",(units_p->vertical_speed_time == MINUTES) ? "m/min" : "m/s");
break;
case FEET:
d = mm_to_feet(mms) * time_factor;
unit = translate("gettextFromC",(units_p->vertical_speed_time == MINUTES) ? "ft/min" : "ft/s");
break;
}
if (frac)
*frac = d < 10;
if (units)
*units = unit;
return d;
}
double get_weight_units(unsigned int grams, int *frac, const char **units)
{
int decimals;
double value;
const char* unit;
struct units *units_p = get_units();
if (units_p->weight == LBS) {
value = grams_to_lbs(grams);
unit = translate("gettextFromC","lbs");
decimals = 0;
} else {
value = grams / 1000.0;
unit = translate("gettextFromC","kg");
decimals = 1;
}
if (frac)
*frac = decimals;
if (units)
*units = unit;
return value;
}
struct dive *alloc_dive(void)
{
struct dive *dive;
dive = malloc(sizeof(*dive));
if (!dive)
exit(1);
memset(dive, 0, sizeof(*dive));
taglist_init(&(dive->tag_list));
return dive;
}
/* only copies events from the first dive computer */
void copy_events(struct dive *s, struct dive *d)
{
struct event *ev;
if (!s || !d)
return;
ev = s->dc.events;
d->dc.events = NULL;
while (ev != NULL) {
add_event(&d->dc, ev->time.seconds, ev->type, ev->flags, ev->value, ev->name);
ev = ev->next;
}
}
void copy_cylinders(struct dive *s, struct dive *d)
{
int i;
if (!s || !d)
return;
for (i = 0; i < MAX_CYLINDERS; i++)
d->cylinder[i] = s->cylinder[i];
}
void copy_samples(struct dive *s, struct dive *d)
{
/* instead of carefully copying them one by one and calling add_sample
* over and over again, let's just copy the whole blob */
if (!s || !d)
return;
int nr = s->dc.samples;
d->dc.samples = nr;
d->dc.sample = malloc(nr * sizeof(struct sample));
if (d->dc.sample)
memcpy(d->dc.sample, s->dc.sample, nr * sizeof(struct sample));
}
struct sample *prepare_sample(struct divecomputer *dc)
{
if (dc) {
int nr = dc->samples;
int alloc_samples = dc->alloc_samples;
struct sample *sample;
if (nr >= alloc_samples) {
struct sample *newsamples;
alloc_samples = (alloc_samples * 3)/2 + 10;
newsamples = realloc(dc->sample, alloc_samples * sizeof(struct sample));
if (!newsamples)
return NULL;
dc->alloc_samples = alloc_samples;
dc->sample = newsamples;
}
sample = dc->sample + nr;
memset(sample, 0, sizeof(*sample));
return sample;
}
return NULL;
}
void finish_sample(struct divecomputer *dc)
{
dc->samples++;
}
/*
* So when we re-calculate maxdepth and meandepth, we will
* not override the old numbers if they are close to the
* new ones.
*
* Why? Because a dive computer may well actually track the
* max depth and mean depth at finer granularity than the
* samples it stores. So it's possible that the max and mean
* have been reported more correctly originally.
*
* Only if the values calculated from the samples are clearly
* different do we override the normal depth values.
*
* This considers 1m to be "clearly different". That's
* a totally random number.
*/
static void update_depth(depth_t *depth, int new)
{
if (new) {
int old = depth->mm;
if (abs(old - new) > 1000)
depth->mm = new;
}
}
static void update_temperature(temperature_t *temperature, int new)
{
if (new) {
int old = temperature->mkelvin;
if (abs(old - new) > 1000)
temperature->mkelvin = new;
}
}
/*
* Calculate how long we were actually under water, and the average
* depth while under water.
*
* This ignores any surface time in the middle of the dive.
*/
static void fixup_dc_duration(struct divecomputer *dc)
{
int duration, i;
int lasttime, lastdepth, depthtime;
duration = 0;
lasttime = 0;
lastdepth = 0;
depthtime = 0;
for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i;
int time = sample->time.seconds;
int depth = sample->depth.mm;
/* We ignore segments at the surface */
if (depth > SURFACE_THRESHOLD || lastdepth > SURFACE_THRESHOLD) {
duration += time - lasttime;
depthtime += (time - lasttime)*(depth+lastdepth)/2;
}
lastdepth = depth;
lasttime = time;
}
if (duration) {
dc->duration.seconds = duration;
dc->meandepth.mm = (depthtime + duration/2) / duration;
}
}
static void fixup_pressure(struct dive *dive, struct sample *sample)
{
unsigned int pressure, index;
cylinder_t *cyl;
pressure = sample->cylinderpressure.mbar;
if (!pressure)
return;
index = sample->sensor;
/* FIXME! sensor -> cylinder mapping? */
if (index >= MAX_CYLINDERS)
return;
cyl = dive->cylinder + index;
if (!cyl->sample_start.mbar)
cyl->sample_start.mbar = pressure;
cyl->sample_end.mbar = pressure;
}
static void update_min_max_temperatures(struct dive *dive, temperature_t temperature)
{
if (temperature.mkelvin) {
if (!dive->maxtemp.mkelvin || temperature.mkelvin > dive->maxtemp.mkelvin)
dive->maxtemp = temperature;
if (!dive->mintemp.mkelvin || temperature.mkelvin < dive->mintemp.mkelvin)
dive->mintemp = temperature;
}
}
/*
* At high pressures air becomes less compressible, and
* does not follow the ideal gas law any more.
*
* This tries to correct for that, becoming the same
* as to_ATM() at lower pressures.
*
* THIS IS A ROUGH APPROXIMATION! The real numbers will
* depend on the exact gas mix and temperature.
*/
double surface_volume_multiplier(pressure_t pressure)
{
double bar = pressure.mbar / 1000.0;
if (bar > 200)
bar = 0.00038*bar*bar + 0.51629*bar + 81.542;
return bar_to_atm(bar);
}
int gas_volume(cylinder_t *cyl, pressure_t p)
{
return cyl->type.size.mliter * surface_volume_multiplier(p);
}
int wet_volume(double cuft, pressure_t p)
{
return cuft_to_l(cuft) * 1000 / surface_volume_multiplier(p);
}
/*
* If the cylinder tank pressures are within half a bar
* (about 8 PSI) of the sample pressures, we consider it
* to be a rounding error, and throw them away as redundant.
*/
static int same_rounded_pressure(pressure_t a, pressure_t b)
{
return abs(a.mbar - b.mbar) <= 500;
}
static void sanitize_gasmix(struct gasmix *mix)
{
unsigned int o2, he;
o2 = mix->o2.permille;
he = mix->he.permille;
/* Regular air: leave empty */
if (!he) {
if (!o2)
return;
/* 20.8% to 21% O2 is just air */
if (is_air(o2, he)) {
mix->o2.permille = 0;
return;
}
}
/* Sane mix? */
if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
return;
fprintf(stderr, "Odd gasmix: %u O2 %u He\n", o2, he);
memset(mix, 0, sizeof(*mix));
}
/*
* See if the size/workingpressure looks like some standard cylinder
* size, eg "AL80".
*/
static void match_standard_cylinder(cylinder_type_t *type)
{
double cuft;
int psi, len;
const char *fmt;
char buffer[40], *p;
/* Do we already have a cylinder description? */
if (type->description)
return;
cuft = ml_to_cuft(type->size.mliter);
cuft *= surface_volume_multiplier(type->workingpressure);
psi = to_PSI(type->workingpressure);
switch (psi) {
case 2300 ... 2500: /* 2400 psi: LP tank */
fmt = "LP%d";
break;
case 2600 ... 2700: /* 2640 psi: LP+10% */
fmt = "LP%d";
break;
case 2900 ... 3100: /* 3000 psi: ALx tank */
fmt = "AL%d";
break;
case 3400 ... 3500: /* 3442 psi: HP tank */
fmt = "HP%d";
break;
case 3700 ... 3850: /* HP+10% */
fmt = "HP%d+";
break;
default:
return;
}
len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
p = malloc(len+1);
if (!p)
return;
memcpy(p, buffer, len+1);
type->description = p;
}
/*
* There are two ways to give cylinder size information:
* - total amount of gas in cuft (depends on working pressure and physical size)
* - physical size
*
* where "physical size" is the one that actually matters and is sane.
*
* We internally use physical size only. But we save the workingpressure
* so that we can do the conversion if required.
*/
static void sanitize_cylinder_type(cylinder_type_t *type)
{
double volume_of_air, volume;
/* If we have no working pressure, it had *better* be just a physical size! */
if (!type->workingpressure.mbar)
return;
/* No size either? Nothing to go on */
if (!type->size.mliter)
return;
if (xml_parsing_units.volume == CUFT) {
/* confusing - we don't really start from ml but millicuft !*/
volume_of_air = cuft_to_l(type->size.mliter);
/* milliliters at 1 atm: "true size" */
volume = volume_of_air / surface_volume_multiplier(type->workingpressure);
type->size.mliter = volume + 0.5;
}
/* Ok, we have both size and pressure: try to match a description */
match_standard_cylinder(type);
}
static void sanitize_cylinder_info(struct dive *dive)
{
int i;
for (i = 0; i < MAX_CYLINDERS; i++) {
sanitize_gasmix(&dive->cylinder[i].gasmix);
sanitize_cylinder_type(&dive->cylinder[i].type);
}
}
/* some events should never be thrown away */
static bool is_potentially_redundant(struct event *event)
{
if (!strcmp(event->name, "gaschange"))
return FALSE;
if (!strcmp(event->name, "bookmark"))
return FALSE;
if (!strcmp(event->name, "heading"))
return FALSE;
return TRUE;
}
/* match just by name - we compare the details in the code that uses this helper */
static struct event *find_previous_event(struct divecomputer *dc, struct event *event)
{
struct event *ev = dc->events;
struct event *previous = NULL;
if (!event->name)
return NULL;
while (ev && ev != event) {
if (ev->name && !strcmp(ev->name, event->name))
previous = ev;
ev = ev->next;
}
return previous;
}
static void fixup_surface_pressure(struct dive *dive)
{
struct divecomputer *dc;
int sum = 0, nr = 0;
for_each_dc(dive, dc) {
if (dc->surface_pressure.mbar) {
sum += dc->surface_pressure.mbar;
nr++;
}
}
if (nr)
dive->surface_pressure.mbar = (sum + nr/2)/nr;
}
static void fixup_water_salinity(struct dive *dive)
{
struct divecomputer *dc;
int sum = 0, nr = 0;
for_each_dc(dive, dc) {
if (dc->salinity) {
sum += dc->salinity;
nr++;
}
}
if (nr)
dive->salinity = (sum + nr/2)/nr;
}
static void fixup_meandepth(struct dive *dive)
{
struct divecomputer *dc;
int sum = 0, nr = 0;
for_each_dc(dive, dc) {
if (dc->meandepth.mm) {
sum += dc->meandepth.mm;
nr++;
}
}
if (nr)
dive->meandepth.mm = (sum + nr / 2) / nr;
}
static void fixup_duration(struct dive *dive)
{
struct divecomputer *dc;
int duration = 0;
for_each_dc(dive, dc)
duration = MAX(duration, dc->duration.seconds);
dive->duration.seconds = duration;
}
static void fixup_watertemp(struct dive *dive)
{
struct divecomputer *dc;
int sum = 0, nr = 0;
for_each_dc(dive, dc) {
if (dc->watertemp.mkelvin) {
sum += dc->watertemp.mkelvin;
nr++;
}
}
if (nr)
dive->watertemp.mkelvin = (sum + nr / 2) / nr;
}
/*
* What do the dive computers say the air temperature is?
*/
unsigned int dc_airtemp(struct divecomputer *dc)
{
int sum = 0, nr = 0;
do {
if (dc->airtemp.mkelvin) {
sum += dc->airtemp.mkelvin;
nr++;
}
} while ((dc = dc->next) != NULL);
if (!nr)
return 0;
return (sum + nr / 2) / nr;
}
static void fixup_airtemp(struct dive *dive)
{
if (!dive->airtemp.mkelvin)
dive->airtemp.mkelvin = dc_airtemp(&dive->dc);
}
/* zero out the airtemp in the dive structure if it was just created by
* running fixup on the dive. keep it if it had been edited by hand */
static void un_fixup_airtemp(struct dive *a)
{
if (a->airtemp.mkelvin && a->airtemp.mkelvin == dc_airtemp(&a->dc))
a->airtemp.mkelvin = 0;
}
/*
* events are stored as a linked list, so the concept of
* "consecutive, identical events" is somewhat hard to
* implement correctly (especially given that on some dive
* computers events are asynchronous, so they can come in
* between what would be the non-constant sample rate).
*
* So what we do is that we throw away clearly redundant
* events that are fewer than 61 seconds apart (assuming there
* is no dive computer with a sample rate of more than 60
* seconds... that would be pretty pointless to plot the
* profile with)
*
* We first only mark the events for deletion so that we
* still know when the previous event happened.
*/
static void fixup_dc_events(struct divecomputer *dc)
{
struct event *event;
event = dc->events;
while (event) {
struct event *prev;
if (is_potentially_redundant(event)) {
prev = find_previous_event(dc, event);
if (prev && prev->value == event->value &&
prev->flags == event->flags &&
event->time.seconds - prev->time.seconds < 61)
event->deleted = TRUE;
}
event = event->next;
}
event = dc->events;
while (event) {
if (event->next && event->next->deleted) {
struct event *nextnext = event->next->next;
free(event->next);
event->next = nextnext;
} else {
event = event->next;
}
}
}
static void fixup_dive_dc(struct dive *dive, struct divecomputer *dc)
{
int i, j;
double depthtime = 0;
int lasttime = 0;
int lastindex = -1;
int maxdepth = dc->maxdepth.mm;
int mintemp = 0;
int lastdepth = 0;
int lasttemp = 0, lastpressure = 0;
int pressure_delta[MAX_CYLINDERS] = {INT_MAX, };
/* Fixup duration and mean depth */
fixup_dc_duration(dc);
update_min_max_temperatures(dive, dc->watertemp);
for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i;
int time = sample->time.seconds;
int depth = sample->depth.mm;
int temp = sample->temperature.mkelvin;
int pressure = sample->cylinderpressure.mbar;
int index = sample->sensor;
if (index == lastindex) {
/* Remove duplicate redundant pressure information */
if (pressure == lastpressure)
sample->cylinderpressure.mbar = 0;
/* check for simply linear data in the samples
+INT_MAX means uninitialized, -INT_MAX means not linear */
if (pressure_delta[index] != -INT_MAX && lastpressure) {
if (pressure_delta[index] == INT_MAX) {
pressure_delta[index] = abs(pressure - lastpressure);
} else {
int cur_delta = abs(pressure - lastpressure);
if (cur_delta && abs(cur_delta - pressure_delta[index]) > 150) {
/* ok the samples aren't just a linearisation
* between start and end */
pressure_delta[index] = -INT_MAX;
}
}
}
}
lastindex = index;
lastpressure = pressure;
if (depth > SURFACE_THRESHOLD) {
if (depth > maxdepth)
maxdepth = depth;
}
fixup_pressure(dive, sample);
if (temp) {
/*
* If we have consecutive identical
* temperature readings, throw away
* the redundant ones.
*/
if (lasttemp == temp)
sample->temperature.mkelvin = 0;
else
lasttemp = temp;
if (!mintemp || temp < mintemp)
mintemp = temp;
}
update_min_max_temperatures(dive, sample->temperature);
depthtime += (time - lasttime) * (lastdepth + depth) / 2;
lastdepth = depth;
lasttime = time;
if (sample->cns > dive->maxcns)
dive->maxcns = sample->cns;
}
/* if all the samples for a cylinder have pressure data that
* is basically equidistant throw out the sample cylinder pressure
* information but make sure we still have a valid start and end
* pressure
* this happens when DivingLog decides to linearalize the
* pressure between beginning and end and for strange reasons
* decides to put that in the sample data as if it came from
* the dive computer; we don't want that (we'll visualize with
* constant SAC rate instead)
* WARNING WARNING - I have only seen this in single tank dives
* --- maybe I should try to create a multi tank dive and see what
* --- divinglog does there - but the code right now is only tested
* --- for the single tank case */
for (j = 0; j < MAX_CYLINDERS; j++) {
if (abs(pressure_delta[j]) != INT_MAX) {
cylinder_t *cyl = dive->cylinder + j;
for (i = 0; i < dc->samples; i++)
if (dc->sample[i].sensor == j)
dc->sample[i].cylinderpressure.mbar = 0;
if (! cyl->start.mbar)
cyl->start.mbar = cyl->sample_start.mbar;
if (! cyl->end.mbar)
cyl->end.mbar = cyl->sample_end.mbar;
cyl->sample_start.mbar = 0;
cyl->sample_end.mbar = 0;
}
}
update_temperature(&dc->watertemp, mintemp);
update_depth(&dc->maxdepth, maxdepth);
if (maxdepth > dive->maxdepth.mm)
dive->maxdepth.mm = maxdepth;
fixup_dc_events(dc);
}
struct dive *fixup_dive(struct dive *dive)
{
int i;
struct divecomputer *dc;
sanitize_cylinder_info(dive);
dive->maxcns = dive->cns;
for_each_dc(dive, dc)
fixup_dive_dc(dive, dc);
fixup_water_salinity(dive);
fixup_surface_pressure(dive);
fixup_meandepth(dive);
fixup_duration(dive);
fixup_watertemp(dive);
fixup_airtemp(dive);
for (i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = dive->cylinder + i;
add_cylinder_description(&cyl->type);
if (same_rounded_pressure(cyl->sample_start, cyl->start))
cyl->start.mbar = 0;
if (same_rounded_pressure(cyl->sample_end, cyl->end))
cyl->end.mbar = 0;
}
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
weightsystem_t *ws = dive->weightsystem + i;
add_weightsystem_description(ws);
}
return dive;
}
/* Don't pick a zero for MERGE_MIN() */
#define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
#define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
#define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
#define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
static struct sample *add_sample(struct sample *sample, int time, struct divecomputer *dc)
{
struct sample *p = prepare_sample(dc);
if (p) {
*p = *sample;
p->time.seconds = time;
finish_sample(dc);
}
return p;
}
/*
* This is like add_sample(), but if the distance from the last sample
* is excessive, we add two surface samples in between.
*
* This is so that if you merge two non-overlapping dives, we make sure
* that the time in between the dives is at the surface, not some "last
* sample that happened to be at a depth of 1.2m".
*/
static void merge_one_sample(struct sample *sample, int time, struct divecomputer *dc)
{
int last = dc->samples-1;
if (last >= 0) {
static struct sample surface;
struct sample *prev = dc->sample + last;
int last_time = prev->time.seconds;
int last_depth = prev->depth.mm;
/*
* Only do surface events if the samples are more than
* a minute apart, and shallower than 5m
*/
if (time > last_time + 60 && last_depth < 5000) {
add_sample(&surface, last_time+20, dc);
add_sample(&surface, time - 20, dc);
}
}
add_sample(sample, time, dc);
}
/*
* Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
*/
static void merge_samples(struct divecomputer *res, struct divecomputer *a, struct divecomputer *b, int offset)
{
int asamples = a->samples;
int bsamples = b->samples;
struct sample *as = a->sample;
struct sample *bs = b->sample;
/*
* We want a positive sample offset, so that sample
* times are always positive. So if the samples for
* 'b' are before the samples for 'a' (so the offset
* is negative), we switch a and b around, and use
* the reverse offset.
*/
if (offset < 0) {
offset = -offset;
asamples = bsamples;
bsamples = a->samples;
as = bs;
bs = a->sample;
}
for (;;) {
int at, bt;
struct sample sample;
if (!res)
return;
at = asamples ? as->time.seconds : -1;
bt = bsamples ? bs->time.seconds + offset : -1;
/* No samples? All done! */
if (at < 0 && bt < 0)
return;
/* Only samples from a? */
if (bt < 0) {
add_sample_a:
merge_one_sample(as, at, res);
as++;
asamples--;
continue;
}
/* Only samples from b? */
if (at < 0) {
add_sample_b:
merge_one_sample(bs, bt, res);
bs++;
bsamples--;
continue;
}
if (at < bt)
goto add_sample_a;
if (at > bt)
goto add_sample_b;
/* same-time sample: add a merged sample. Take the non-zero ones */
sample = *bs;
if (as->depth.mm)
sample.depth = as->depth;
if (as->temperature.mkelvin)
sample.temperature = as->temperature;
if (as->cylinderpressure.mbar)
sample.cylinderpressure = as->cylinderpressure;
if (as->sensor)
sample.sensor = as->sensor;
if (as->cns)
sample.cns = as->cns;
if (as->po2)
sample.po2 = as->po2;
if (as->ndl.seconds)
sample.ndl = as->ndl;
if (as->stoptime.seconds)
sample.stoptime = as->stoptime;
if (as->stopdepth.mm)
sample.stopdepth = as->stopdepth;
if (as->in_deco)
sample.in_deco = TRUE;
merge_one_sample(&sample, at, res);
as++;
bs++;
asamples--;
bsamples--;
}
}
static char *merge_text(const char *a, const char *b)
{
char *res;
if (!a && !b)
return NULL;
if (!a || !*a)
return b ? strdup(b) : NULL;
if (!b || !*b)
return strdup(a);
if (!strcmp(a,b))
return a ? strdup(a) : NULL;
res = malloc(strlen(a) + strlen(b) + 32);
if (!res)
return (char *)a;
sprintf(res, translate("gettextFromC","(%s) or (%s)"), a, b);
return res;
}
#define SORT(a,b,field) \
if (a->field != b->field) return a->field < b->field ? -1 : 1
static int sort_event(struct event *a, struct event *b)
{