-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdive.c
2665 lines (2365 loc) · 64.9 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 <stdlib.h>
#include <limits.h>
#include "gettext.h"
#include "dive.h"
#include "libdivecomputer.h"
#include "device.h"
/* one could argue about the best place to have this variable -
* it's used in the UI, but it seems to make the most sense to have it
* here */
struct dive displayed_dive;
struct tag_entry *g_tag_list = NULL;
static const char *default_tags[] = {
QT_TRANSLATE_NOOP("gettextFromC", "boat"), QT_TRANSLATE_NOOP("gettextFromC", "shore"), QT_TRANSLATE_NOOP("gettextFromC", "drift"),
QT_TRANSLATE_NOOP("gettextFromC", "deep"), QT_TRANSLATE_NOOP("gettextFromC", "cavern"), QT_TRANSLATE_NOOP("gettextFromC", "ice"),
QT_TRANSLATE_NOOP("gettextFromC", "wreck"), QT_TRANSLATE_NOOP("gettextFromC", "cave"), QT_TRANSLATE_NOOP("gettextFromC", "altitude"),
QT_TRANSLATE_NOOP("gettextFromC", "pool"), QT_TRANSLATE_NOOP("gettextFromC", "lake"), QT_TRANSLATE_NOOP("gettextFromC", "river"),
QT_TRANSLATE_NOOP("gettextFromC", "night"), QT_TRANSLATE_NOOP("gettextFromC", "fresh"), QT_TRANSLATE_NOOP("gettextFromC", "student"),
QT_TRANSLATE_NOOP("gettextFromC", "instructor"), QT_TRANSLATE_NOOP("gettextFromC", "photo"), QT_TRANSLATE_NOOP("gettextFromC", "video"),
QT_TRANSLATE_NOOP("gettextFromC", "deco")
};
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);
}
static int same_event(struct event *a, struct event *b)
{
if (a->time.seconds != b->time.seconds)
return 0;
if (a->type != b->type)
return 0;
if (a->flags != b->flags)
return 0;
if (a->value != b->value)
return 0;
return !strcmp(a->name, b->name);
}
void remove_event(struct event* event)
{
struct event **ep = ¤t_dc->events;
while (ep && !same_event(*ep, event))
ep = &(*ep)->next;
if (ep) {
/* we can't link directly with event->next
* because 'event' can be a copy from another
* dive (for instance the displayed_dive
* that we use on the interface to show things). */
struct event *temp = (*ep)->next;
free(*ep);
*ep = temp;
}
}
/* since the name is an array as part of the structure (how silly is that?) we
* have to actually remove the existing event and replace it with a new one.
* WARNING, WARNING... this may end up freeing event in case that event is indeed
* WARNING, WARNING... part of this divecomputer on this dive! */
void update_event_name(struct dive *d, struct event* event, char *name)
{
if (!d || !event)
return;
struct divecomputer *dc = get_dive_dc(d, dc_number);
if (!dc)
return;
struct event **removep = &dc->events;
struct event *remove;
while ((*removep)->next && !same_event(*removep, event))
removep = &(*removep)->next;
if (!same_event(*removep, event))
return;
remove = *removep;
*removep = (*removep)->next;
add_event(dc, event->time.seconds, event->type, event->flags, event->value, name);
free(remove);
}
/* this returns a pointer to static variable - so use it right away after calling */
struct gasmix *get_gasmix_from_event(struct event *ev)
{
static struct gasmix g;
g.o2.permille = g.he.permille = 0;
if (ev && (ev->type == SAMPLE_EVENT_GASCHANGE || ev->type == SAMPLE_EVENT_GASCHANGE2)) {
g.o2.permille = 10 * ev->value & 0xffff;
if (ev->type == SAMPLE_EVENT_GASCHANGE2)
g.he.permille = 10 * (ev->value >> 16);
}
return &g;
}
int get_pressure_units(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:
default:
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:
default:
vol = ml / 1000.0;
unit = translate("gettextFromC", "ℓ");
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;
}
int units_to_sac(double volume)
{
if(get_units()->volume == CUFT)
return rint(cuft_to_l(volume) * 1000.0);
else
return rint(volume * 1000);
}
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(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:
default:
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:
default:
d = mms / 1000.0 * time_factor;
if (units_p->vertical_speed_time == MINUTES)
unit = translate("gettextFromC", "m/min");
else
unit = translate("gettextFromC", "m/s");
break;
case FEET:
d = mm_to_feet(mms) * time_factor;
if (units_p->vertical_speed_time == MINUTES)
unit = translate("gettextFromC", "ft/min");
else
unit = translate("gettextFromC", "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;
}
bool has_hr_data(struct divecomputer *dc)
{
int i;
struct sample *sample;
if (!dc)
return false;
sample = dc->sample;
for (i = 0; i < dc->samples; i++)
if (sample[i].heartbeat)
return true;
return false;
}
struct dive *alloc_dive(void)
{
struct dive *dive;
dive = malloc(sizeof(*dive));
if (!dive)
exit(1);
memset(dive, 0, sizeof(*dive));
dive->id = dive_getUniqID(dive);
return dive;
}
static void free_dc(struct divecomputer *dc);
static void free_pic(struct picture *picture);
/* this is very different from the copy_divecomputer later in this file;
* this function actually makes full copies of the content */
static void copy_dc(struct divecomputer *sdc, struct divecomputer *ddc)
{
*ddc = *sdc;
ddc->model = copy_string(sdc->model);
copy_samples(sdc, ddc);
copy_events(sdc, ddc);
}
/* copy an element in a list of pictures */
static void copy_pl(struct picture *sp, struct picture *dp)
{
*dp = *sp;
dp->filename = copy_string(sp->filename);
}
/* copy an element in a list of tags */
static void copy_tl(struct tag_entry *st, struct tag_entry *dt)
{
dt->tag = malloc(sizeof(struct divetag));
dt->tag->name = copy_string(st->tag->name);
dt->tag->source = copy_string(st->tag->source);
}
/* Clear everything but the first element;
* this works for taglist, picturelist, even dive computers */
#define STRUCTURED_LIST_FREE(_type, _start, _free) {\
_type *_ptr = _start; \
while(_ptr) { \
_type *_next = _ptr->next; \
_free(_ptr); \
_ptr = _next; \
}}
#define STRUCTURED_LIST_COPY(_type, _first, _dest, _cpy) {\
_type *_sptr = _first; \
_type **_dptr = &_dest; \
while(_sptr) { \
*_dptr = malloc(sizeof(_type)); \
_cpy(_sptr, *_dptr); \
_sptr = _sptr->next; \
_dptr = &(*_dptr)->next; \
} \
*_dptr = 0; \
}
/* copy_dive makes duplicates of many components of a dive;
* in order not to leak memory, we need to free those .
* copy_dive doesn't play with the divetrip and forward/backward pointers
* so we can ignore those */
void clear_dive(struct dive *d)
{
if (!d)
return;
/* free the strings */
free(d->buddy);
free(d->divemaster);
free(d->location);
free(d->notes);
free(d->suit);
/* free tags, additional dive computers, and pictures */
taglist_free(d->tag_list);
STRUCTURED_LIST_FREE(struct divecomputer, d->dc.next, free_dc);
STRUCTURED_LIST_FREE(struct picture, d->picture_list, free_pic);
memset(d, 0, sizeof(struct dive));
}
/* make a true copy that is independent of the source dive;
* all data structures are duplicated, so the copy can be modified without
* any impact on the source */
void copy_dive(struct dive *s, struct dive *d)
{
clear_dive(d);
/* simply copy things over, but then make actual copies of the
* relevant components that are referenced through pointers,
* so all the strings and the structured lists */
*d = *s;
d->buddy = copy_string(s->buddy);
d->divemaster = copy_string(s->divemaster);
d->location = copy_string(s->location);
d->notes = copy_string(s->notes);
d->suit = copy_string(s->suit);
STRUCTURED_LIST_COPY(struct picture, s->picture_list, d->picture_list, copy_pl);
STRUCTURED_LIST_COPY(struct tag_entry, s->tag_list, d->tag_list, copy_tl);
STRUCTURED_LIST_COPY(struct divecomputer, s->dc.next, d->dc.next, copy_dc);
/* this only copied dive computers 2 and up. The first dive computer is part
* of the struct dive, so let's make copies of its samples and events */
copy_samples(&s->dc, &d->dc);
copy_events(&s->dc, &d->dc);
}
/* make a clone of the source dive and clean out the source dive;
* this is specifically so we can create a dive in the displayed_dive and then
* add it to the divelist.
* Note the difference to copy_dive() / clean_dive() */
struct dive *clone_dive(struct dive *s)
{
struct dive *dive = alloc_dive();
*dive = *s; // so all the pointers in dive point to the things s pointed to
memset(s, 0, sizeof(struct dive)); // and now the pointers in s are gone
return dive;
}
#define CONDITIONAL_COPY_STRING(_component) \
if (what._component) \
d->_component = copy_string(s->_component)
// copy elements, depending on bits in what that are set
void selective_copy_dive(struct dive *s, struct dive *d, struct dive_components what, bool clear)
{
if (clear)
clear_dive(d);
CONDITIONAL_COPY_STRING(location);
CONDITIONAL_COPY_STRING(notes);
CONDITIONAL_COPY_STRING(divemaster);
CONDITIONAL_COPY_STRING(buddy);
CONDITIONAL_COPY_STRING(suit);
if (what.rating)
d->rating = s->rating;
if (what.visibility)
d->visibility = s->visibility;
if (what.gps) {
d->longitude = s->longitude;
d->latitude = s->latitude;
}
if (what.tags)
STRUCTURED_LIST_COPY(struct tag_entry, s->tag_list, d->tag_list, copy_tl);
if (what.cylinders)
copy_cylinders(s, d, false);
if (what.weights)
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
d->weightsystem[i] = s->weightsystem[i];
}
#undef CONDITIONAL_COPY_STRING
/* only copies events from the first dive computer */
void copy_events(struct divecomputer *s, struct divecomputer *d)
{
struct event *ev;
if (!s || !d)
return;
ev = s->events;
d->events = NULL;
while (ev != NULL) {
add_event(d, ev->time.seconds, ev->type, ev->flags, ev->value, ev->name);
ev = ev->next;
}
}
int nr_cylinders(struct dive *dive)
{
int nr;
for (nr = MAX_CYLINDERS; nr; --nr) {
cylinder_t *cylinder = dive->cylinder + nr - 1;
if (!cylinder_nodata(cylinder))
break;
}
return nr;
}
int nr_weightsystems(struct dive *dive)
{
int nr;
for (nr = MAX_WEIGHTSYSTEMS; nr; --nr) {
weightsystem_t *ws = dive->weightsystem + nr - 1;
if (!weightsystem_none(ws))
break;
}
return nr;
}
/* copy the equipment data part of the cylinders */
void copy_cylinders(struct dive *s, struct dive *d, bool used_only)
{
int i;
if (!s || !d)
return;
for (i = 0; i < MAX_CYLINDERS; i++) {
memset(&d->cylinder[i], 0, sizeof(cylinder_t));
if (!used_only || is_cylinder_used(s, i)) {
d->cylinder[i].type = s->cylinder[i].type;
d->cylinder[i].gasmix = s->cylinder[i].gasmix;
d->cylinder[i].depth = s->cylinder[i].depth;
d->cylinder[i].manually_added = true;
}
}
}
void copy_samples(struct divecomputer *s, struct divecomputer *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->samples;
d->samples = nr;
d->sample = malloc(nr * sizeof(struct sample));
if (d->sample)
memcpy(d->sample, s->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.
*/
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;
}
}
void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *mean, int *duration)
{
int i;
int depthtime[MAX_CYLINDERS] = { 0, };
int lasttime = 0, lastdepth = 0;
int idx = 0;
for (i = 0; i < MAX_CYLINDERS; i++)
mean[i] = duration[i] = 0;
struct event *ev = get_next_event(dc->events, "gaschange");
if (!ev) {
// special case - no gas changes
mean[0] = dc->meandepth.mm;
duration[0] = dc->duration.seconds;
return;
}
if (!dc->samples)
dc = fake_dc(dc);
for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i;
int time = sample->time.seconds;
int depth = sample->depth.mm;
/* Make sure to move the event past 'lasttime' */
while (ev && lasttime >= ev->time.seconds) {
idx = get_cylinder_index(dive, ev);
ev = get_next_event(ev->next, "gaschange");
}
/* Do we need to fake a midway sample at an event? */
if (ev && time > ev->time.seconds) {
int newtime = ev->time.seconds;
int newdepth = interpolate(lastdepth, depth, newtime - lasttime, time - lasttime);
time = newtime;
depth = newdepth;
i--;
}
/* We ignore segments at the surface */
if (depth > SURFACE_THRESHOLD || lastdepth > SURFACE_THRESHOLD) {
duration[idx] += time - lasttime;
depthtime[idx] += (time - lasttime) * (depth + lastdepth) / 2;
}
lastdepth = depth;
lasttime = time;
}
for (i = 0; i < MAX_CYLINDERS; i++) {
if (duration[i])
mean[i] = (depthtime[i] + duration[i] / 2) / duration[i];
}
}
static void fixup_pressure(struct dive *dive, struct sample *sample)
{
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;
}
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 (gasmix_is_air(mix)) {
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)rint(cuft));
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 = rint(volume);
}
/* 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;
unsigned int duration = 0;
for_each_dc(dive, dc)
duration = MAX(duration, dc->duration.seconds);
dive->duration.seconds = duration;
}
/*
* What do the dive computers say the water temperature is?
* (not in the samples, but as dc property for dcs that support that)
*/
unsigned int dc_watertemp(struct divecomputer *dc)
{
int sum = 0, nr = 0;
do {
if (dc->watertemp.mkelvin) {
sum += dc->watertemp.mkelvin;
nr++;
}
} while ((dc = dc->next) != NULL);
if (!nr)
return 0;
return (sum + nr / 2) / nr;
}
static void fixup_watertemp(struct dive *dive)
{
if (!dive->watertemp.mkelvin)
dive->watertemp.mkelvin = dc_watertemp(&dive->dc);
}
/*
* 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