]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Remove weightsystem entry with no description
[ext/subsurface.git] / dive.c
1 /* dive.c */
2 /* maintains the internal dive list structure */
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "dive.h"
7
8 void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name)
9 {
10         struct event *ev, **p;
11         unsigned int size, len = strlen(name);
12
13         size = sizeof(*ev) + len + 1;
14         ev = malloc(size);
15         if (!ev)
16                 return;
17         memset(ev, 0, size);
18         memcpy(ev->name, name, len);
19         ev->time.seconds = time;
20         ev->type = type;
21         ev->flags = flags;
22         ev->value = value;
23         ev->next = NULL;
24
25         p = &dive->events;
26         while (*p)
27                 p = &(*p)->next;
28         *p = ev;
29         remember_event(name);
30 }
31
32 int get_pressure_units(unsigned int mb, const char **units)
33 {
34         int pressure;
35         const char* unit;
36
37         switch (output_units.pressure) {
38         case PASCAL:
39                 pressure = mb * 100;
40                 unit = "pascal";
41                 break;
42         case BAR:
43                 pressure = (mb + 500) / 1000;
44                 unit = "bar";
45                 break;
46         case PSI:
47                 pressure = mbar_to_PSI(mb);
48                 unit = "psi";
49                 break;
50         }
51         if (units)
52                 *units = unit;
53         return pressure;
54 }
55
56 double get_temp_units(unsigned int mk, const char **units)
57 {
58         double deg;
59         const char *unit;
60
61         if (output_units.temperature == FAHRENHEIT) {
62                 deg = mkelvin_to_F(mk);
63                 unit = UTF8_DEGREE "F";
64         } else {
65                 deg = mkelvin_to_C(mk);
66                 unit = UTF8_DEGREE "C";
67         }
68         if (units)
69                 *units = unit;
70         return deg;
71 }
72
73 double get_volume_units(unsigned int ml, int *frac, const char **units)
74 {
75         int decimals;
76         double vol;
77         const char *unit;
78
79         switch (output_units.volume) {
80         case LITER:
81                 vol = ml / 1000.0;
82                 unit = "l";
83                 decimals = 1;
84                 break;
85         case CUFT:
86                 vol = ml_to_cuft(ml);
87                 unit = "cuft";
88                 decimals = 2;
89                 break;
90         }
91         if (frac)
92                 *frac = decimals;
93         if (units)
94                 *units = unit;
95         return vol;
96 }
97
98 double get_depth_units(unsigned int mm, int *frac, const char **units)
99 {
100         int decimals;
101         double d;
102         const char *unit;
103
104         switch (output_units.length) {
105         case METERS:
106                 d = mm / 1000.0;
107                 unit = "m";
108                 decimals = d < 20;
109                 break;
110         case FEET:
111                 d = mm_to_feet(mm);
112                 unit = "ft";
113                 decimals = 0;
114                 break;
115         }
116         if (frac)
117                 *frac = decimals;
118         if (units)
119                 *units = unit;
120         return d;
121 }
122
123 struct dive *alloc_dive(void)
124 {
125         const int initial_samples = 5;
126         unsigned int size;
127         struct dive *dive;
128
129         size = dive_size(initial_samples);
130         dive = malloc(size);
131         if (!dive)
132                 exit(1);
133         memset(dive, 0, size);
134         dive->alloc_samples = initial_samples;
135         return dive;
136 }
137
138 struct sample *prepare_sample(struct dive **divep)
139 {
140         struct dive *dive = *divep;
141         if (dive) {
142                 int nr = dive->samples;
143                 int alloc_samples = dive->alloc_samples;
144                 struct sample *sample;
145                 if (nr >= alloc_samples) {
146                         unsigned int size;
147
148                         alloc_samples = (alloc_samples * 3)/2 + 10;
149                         size = dive_size(alloc_samples);
150                         dive = realloc(dive, size);
151                         if (!dive)
152                                 return NULL;
153                         dive->alloc_samples = alloc_samples;
154                         *divep = dive;
155                 }
156                 sample = dive->sample + nr;
157                 memset(sample, 0, sizeof(*sample));
158                 return sample;
159         }
160         return NULL;
161 }
162
163 void finish_sample(struct dive *dive)
164 {
165         dive->samples++;
166 }
167
168 /*
169  * So when we re-calculate maxdepth and meandepth, we will
170  * not override the old numbers if they are close to the
171  * new ones.
172  *
173  * Why? Because a dive computer may well actually track the
174  * max depth and mean depth at finer granularity than the
175  * samples it stores. So it's possible that the max and mean
176  * have been reported more correctly originally.
177  *
178  * Only if the values calculated from the samples are clearly
179  * different do we override the normal depth values.
180  *
181  * This considers 1m to be "clearly different". That's
182  * a totally random number.
183  */
184 static void update_depth(depth_t *depth, int new)
185 {
186         if (new) {
187                 int old = depth->mm;
188
189                 if (abs(old - new) > 1000)
190                         depth->mm = new;
191         }
192 }
193
194 static void update_duration(duration_t *duration, int new)
195 {
196         if (new)
197                 duration->seconds = new;
198 }
199
200 static void update_temperature(temperature_t *temperature, int new)
201 {
202         if (new) {
203                 int old = temperature->mkelvin;
204
205                 if (abs(old - new) > 1000)
206                         temperature->mkelvin = new;
207         }
208 }
209
210 static void fixup_pressure(struct dive *dive, struct sample *sample)
211 {
212         unsigned int pressure, index;
213         cylinder_t *cyl;
214
215         pressure = sample->cylinderpressure.mbar;
216         if (!pressure)
217                 return;
218         index = sample->cylinderindex;
219         if (index >= MAX_CYLINDERS)
220                 return;
221         cyl = dive->cylinder + index;
222         if (!cyl->sample_start.mbar)
223                 cyl->sample_start.mbar = pressure;
224         cyl->sample_end.mbar = pressure;
225 }
226
227 /*
228  * If the cylinder tank pressures are within half a bar
229  * (about 8 PSI) of the sample pressures, we consider it
230  * to be a rounding error, and throw them away as redundant.
231  */
232 static int same_rounded_pressure(pressure_t a, pressure_t b)
233 {
234         return abs(a.mbar - b.mbar) <= 500;
235 }
236
237 static void sanitize_gasmix(struct gasmix *mix)
238 {
239         unsigned int o2, he;
240
241         o2 = mix->o2.permille;
242         he = mix->he.permille;
243
244         /* Regular air: leave empty */
245         if (!he) {
246                 if (!o2)
247                         return;
248                 /* 20.9% or 21% O2 is just air */
249                 if (o2 >= 209 && o2 <= 210) {
250                         mix->o2.permille = 0;
251                         return;
252                 }
253         }
254
255         /* Sane mix? */
256         if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
257                 return;
258         fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
259         memset(mix, 0, sizeof(*mix));
260 }
261
262 /*
263  * See if the size/workingpressure looks like some standard cylinder
264  * size, eg "AL80".
265  */
266 static void match_standard_cylinder(cylinder_type_t *type)
267 {
268         double cuft;
269         int psi, len;
270         const char *fmt;
271         char buffer[20], *p;
272
273         /* Do we already have a cylinder description? */
274         if (type->description)
275                 return;
276
277         cuft = ml_to_cuft(type->size.mliter);
278         cuft *= to_ATM(type->workingpressure);
279         psi = to_PSI(type->workingpressure);
280
281         switch (psi) {
282         case 2300 ... 2500:     /* 2400 psi: LP tank */
283                 fmt = "LP%d";
284                 break;
285         case 2600 ... 2700:     /* 2640 psi: LP+10% */
286                 fmt = "LP%d";
287                 break;
288         case 2900 ... 3100:     /* 3000 psi: ALx tank */
289                 fmt = "AL%d";
290                 break;
291         case 3400 ... 3500:     /* 3442 psi: HP tank */
292                 fmt = "HP%d";
293                 break;
294         case 3700 ... 3850:     /* HP+10% */
295                 fmt = "HP%d+";
296                 break;
297         default:
298                 return;
299         }
300         len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
301         p = malloc(len+1);
302         if (!p)
303                 return;
304         memcpy(p, buffer, len+1);
305         type->description = p;
306 }
307
308
309 /*
310  * There are two ways to give cylinder size information:
311  *  - total amount of gas in cuft (depends on working pressure and physical size)
312  *  - physical size
313  *
314  * where "physical size" is the one that actually matters and is sane.
315  *
316  * We internally use physical size only. But we save the workingpressure
317  * so that we can do the conversion if required.
318  */
319 static void sanitize_cylinder_type(cylinder_type_t *type)
320 {
321         double volume_of_air, atm, volume;
322
323         /* If we have no working pressure, it had *better* be just a physical size! */
324         if (!type->workingpressure.mbar)
325                 return;
326
327         /* No size either? Nothing to go on */
328         if (!type->size.mliter)
329                 return;
330
331         if (input_units.volume == CUFT) {
332                 /* confusing - we don't really start from ml but millicuft !*/
333                 volume_of_air = cuft_to_l(type->size.mliter);
334                 atm = to_ATM(type->workingpressure);            /* working pressure in atm */
335                 volume = volume_of_air / atm;                   /* milliliters at 1 atm: "true size" */
336                 type->size.mliter = volume + 0.5;
337         }
338
339         /* Ok, we have both size and pressure: try to match a description */
340         match_standard_cylinder(type);
341 }
342
343 static void sanitize_cylinder_info(struct dive *dive)
344 {
345         int i;
346
347         for (i = 0; i < MAX_CYLINDERS; i++) {
348                 sanitize_gasmix(&dive->cylinder[i].gasmix);
349                 sanitize_cylinder_type(&dive->cylinder[i].type);
350         }
351 }
352
353 struct dive *fixup_dive(struct dive *dive)
354 {
355         int i,j;
356         double depthtime = 0;
357         int lasttime = 0;
358         int lastindex = -1;
359         int start = -1, end = -1;
360         int maxdepth = 0, mintemp = 0;
361         int lastdepth = 0;
362         int lasttemp = 0, lastpressure = 0;
363         int pressure_delta[MAX_CYLINDERS] = {INT_MAX, };
364
365         sanitize_cylinder_info(dive);
366         for (i = 0; i < dive->samples; i++) {
367                 struct sample *sample = dive->sample + i;
368                 int time = sample->time.seconds;
369                 int depth = sample->depth.mm;
370                 int temp = sample->temperature.mkelvin;
371                 int pressure = sample->cylinderpressure.mbar;
372                 int index = sample->cylinderindex;
373
374                 if (index == lastindex) {
375                         /* Remove duplicate redundant pressure information */
376                         if (pressure == lastpressure)
377                                 sample->cylinderpressure.mbar = 0;
378                         /* check for simply linear data in the samples
379                            +INT_MAX means uninitialized, -INT_MAX means not linear */
380                         if (pressure_delta[index] != -INT_MAX && lastpressure) {
381                                 if (pressure_delta[index] == INT_MAX) {
382                                         pressure_delta[index] = abs(pressure - lastpressure);
383                                 } else {
384                                         int cur_delta = abs(pressure - lastpressure);
385                                         if (cur_delta && abs(cur_delta - pressure_delta[index]) > 150) {
386                                                 /* ok the samples aren't just a linearisation
387                                                  * between start and end */
388                                                 pressure_delta[index] = -INT_MAX;
389                                         }
390                                 }
391                         }
392                 }
393                 lastindex = index;
394                 lastpressure = pressure;
395
396                 if (lastdepth)
397                         end = time;
398
399                 if (depth) {
400                         if (start < 0)
401                                 start = lasttime;
402                         if (depth > maxdepth)
403                                 maxdepth = depth;
404                 }
405
406                 fixup_pressure(dive, sample);
407
408                 if (temp) {
409                         /*
410                          * If we have consecutive identical
411                          * temperature readings, throw away
412                          * the redundant ones.
413                          */
414                         if (lasttemp == temp)
415                                 sample->temperature.mkelvin = 0;
416                         else
417                                 lasttemp = temp;
418
419                         if (!mintemp || temp < mintemp)
420                                 mintemp = temp;
421                 }
422                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
423                 lastdepth = depth;
424                 lasttime = time;
425         }
426         /* if all the samples for a cylinder have pressure data that
427          * is basically equidistant throw out the sample cylinder pressure
428          * information but make sure we still have a valid start and end
429          * pressure
430          * this happens when DivingLog decides to linearalize the
431          * pressure between beginning and end and for strange reasons
432          * decides to put that in the sample data as if it came from
433          * the dive computer; we don't want that (we'll visualize with
434          * constant SAC rate instead)
435          * WARNING WARNING - I have only seen this in single tank dives
436          * --- maybe I should try to create a multi tank dive and see what
437          * --- divinglog does there - but the code right now is only tested
438          * --- for the single tank case */
439         for (j = 0; j < MAX_CYLINDERS; j++) {
440                 if (abs(pressure_delta[j]) != INT_MAX) {
441                         cylinder_t *cyl = dive->cylinder + j;
442                         for (i = 0; i < dive->samples; i++)
443                                 if (dive->sample[i].cylinderindex == j)
444                                         dive->sample[i].cylinderpressure.mbar = 0;
445                         if (! cyl->start.mbar)
446                                 cyl->start.mbar = cyl->sample_start.mbar;
447                         if (! cyl->end.mbar)
448                                 cyl->end.mbar = cyl->sample_end.mbar;
449                         cyl->sample_start.mbar = 0;
450                         cyl->sample_end.mbar = 0;
451                 }
452         }
453         if (end < 0)
454                 return dive;
455
456         update_duration(&dive->duration, end - start);
457         if (start != end)
458                 depthtime /= (end - start);
459
460         update_depth(&dive->meandepth, depthtime);
461         update_temperature(&dive->watertemp, mintemp);
462         update_depth(&dive->maxdepth, maxdepth);
463
464         add_people(dive->buddy);
465         add_people(dive->divemaster);
466         add_location(dive->location);
467         for (i = 0; i < MAX_CYLINDERS; i++) {
468                 cylinder_t *cyl = dive->cylinder + i;
469                 add_cylinder_description(&cyl->type);
470                 if (same_rounded_pressure(cyl->sample_start, cyl->start))
471                         cyl->start.mbar = 0;
472                 if (same_rounded_pressure(cyl->sample_end, cyl->end))
473                         cyl->end.mbar = 0;
474         }
475
476         return dive;
477 }
478
479 /* Don't pick a zero for MERGE_MIN() */
480 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
481 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
482 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
483 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
484
485 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
486 {
487         struct sample *p = prepare_sample(&dive);
488
489         if (!p)
490                 return NULL;
491         *p = *sample;
492         p->time.seconds = time;
493         finish_sample(dive);
494         return dive;
495 }
496
497 /*
498  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
499  */
500 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
501 {
502         int asamples = a->samples;
503         int bsamples = b->samples;
504         struct sample *as = a->sample;
505         struct sample *bs = b->sample;
506
507         for (;;) {
508                 int at, bt;
509                 struct sample sample;
510
511                 if (!res)
512                         return NULL;
513
514                 at = asamples ? as->time.seconds : -1;
515                 bt = bsamples ? bs->time.seconds + offset : -1;
516
517                 /* No samples? All done! */
518                 if (at < 0 && bt < 0)
519                         return fixup_dive(res);
520
521                 /* Only samples from a? */
522                 if (bt < 0) {
523 add_sample_a:
524                         res = add_sample(as, at, res);
525                         as++;
526                         asamples--;
527                         continue;
528                 }
529
530                 /* Only samples from b? */
531                 if (at < 0) {
532 add_sample_b:
533                         res = add_sample(bs, bt, res);
534                         bs++;
535                         bsamples--;
536                         continue;
537                 }
538
539                 if (at < bt)
540                         goto add_sample_a;
541                 if (at > bt)
542                         goto add_sample_b;
543
544                 /* same-time sample: add a merged sample. Take the non-zero ones */
545                 sample = *bs;
546                 if (as->depth.mm)
547                         sample.depth = as->depth;
548                 if (as->temperature.mkelvin)
549                         sample.temperature = as->temperature;
550                 if (as->cylinderpressure.mbar)
551                         sample.cylinderpressure = as->cylinderpressure;
552                 if (as->cylinderindex)
553                         sample.cylinderindex = as->cylinderindex;
554
555                 res = add_sample(&sample, at, res);
556
557                 as++;
558                 bs++;
559                 asamples--;
560                 bsamples--;
561         }
562 }
563
564 static char *merge_text(const char *a, const char *b)
565 {
566         char *res;
567
568         if (!a || !*a)
569                 return (char *)b;
570         if (!b || !*b)
571                 return (char *)a;
572         if (!strcmp(a,b))
573                 return (char *)a;
574         res = malloc(strlen(a) + strlen(b) + 9);
575         if (!res)
576                 return (char *)a;
577         sprintf(res, "(%s) or (%s)", a, b);
578         return res;
579 }
580
581 #define SORT(a,b,field) \
582         if (a->field != b->field) return a->field < b->field ? -1 : 1
583
584 static int sort_event(struct event *a, struct event *b)
585 {
586         SORT(a,b,time.seconds);
587         SORT(a,b,type);
588         SORT(a,b,flags);
589         SORT(a,b,value);
590         return strcmp(a->name, b->name);
591 }
592
593 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
594 {
595         struct event *a, *b;
596         struct event **p = &res->events;
597
598         a = src1->events;
599         b = src2->events;
600         while (b) {
601                 b->time.seconds += offset;
602                 b = b->next;
603         }
604         b = src2->events;
605
606         while (a || b) {
607                 int s;
608                 if (!b) {
609                         *p = a;
610                         break;
611                 }
612                 if (!a) {
613                         *p = b;
614                         break;
615                 }
616                 s = sort_event(a, b);
617                 /* Pick b */
618                 if (s > 0) {
619                         *p = b;
620                         p = &b->next;
621                         b = b->next;
622                         continue;
623                 }
624                 /* Pick 'a' or neither */
625                 if (s < 0) {
626                         *p = a;
627                         p = &a->next;
628                 }
629                 a = a->next;
630                 continue;
631         }
632 }
633
634 /* Pick whichever has any info (if either). Prefer 'a' */
635 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
636 {
637         if (a->size.mliter)
638                 b = a;
639         *res = *b;
640 }
641
642 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
643 {
644         if (a->o2.permille)
645                 b = a;
646         *res = *b;
647 }
648
649 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
650 {
651         merge_cylinder_type(&res->type, &a->type, &b->type);
652         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
653         MERGE_MAX(res, a, b, start.mbar);
654         MERGE_MIN(res, a, b, end.mbar);
655 }
656
657 /*
658  * This could do a lot more merging. Right now it really only
659  * merges almost exact duplicates - something that happens easily
660  * with overlapping dive downloads.
661  */
662 struct dive *try_to_merge(struct dive *a, struct dive *b)
663 {
664         int i;
665         struct dive *res;
666
667         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
668                 return NULL;
669
670         res = alloc_dive();
671
672         res->when = a->when;
673         MERGE_NONZERO(res, a, b, latitude);
674         MERGE_NONZERO(res, a, b, longitude);
675         MERGE_TXT(res, a, b, location);
676         MERGE_TXT(res, a, b, notes);
677         MERGE_TXT(res, a, b, buddy);
678         MERGE_TXT(res, a, b, divemaster);
679         MERGE_MAX(res, a, b, rating);
680         MERGE_MAX(res, a, b, number);
681         MERGE_MAX(res, a, b, maxdepth.mm);
682         res->meandepth.mm = 0;
683         MERGE_MAX(res, a, b, duration.seconds);
684         MERGE_MAX(res, a, b, surfacetime.seconds);
685         MERGE_MAX(res, a, b, airtemp.mkelvin);
686         MERGE_MIN(res, a, b, watertemp.mkelvin);
687         for (i = 0; i < MAX_CYLINDERS; i++)
688                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
689         merge_events(res, a, b, 0);
690         return merge_samples(res, a, b, 0);
691 }