]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Add new helper function to get temperature and unit
[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 double get_temp_units(unsigned int mk, const char **units)
33 {
34         double deg;
35         const char *unit;
36
37         if (output_units.temperature == FAHRENHEIT) {
38                 deg = mkelvin_to_F(mk);
39                 unit = UTF8_DEGREE "F";
40         } else {
41                 deg = mkelvin_to_C(mk);
42                 unit = UTF8_DEGREE "C";
43         }
44         if (units)
45                 *units = unit;
46         return deg;
47 }
48
49 double get_depth_units(unsigned int mm, int *frac, const char **units)
50 {
51         int decimals;
52         double d;
53         const char *unit;
54
55         switch (output_units.length) {
56         case METERS:
57                 d = mm / 1000.0;
58                 unit = "m";
59                 decimals = d < 20;
60                 break;
61         case FEET:
62                 d = mm_to_feet(mm);
63                 unit = "ft";
64                 decimals = 0;
65                 break;
66         }
67         if (frac)
68                 *frac = decimals;
69         if (units)
70                 *units = unit;
71         return d;
72 }
73
74 struct dive *alloc_dive(void)
75 {
76         const int initial_samples = 5;
77         unsigned int size;
78         struct dive *dive;
79
80         size = dive_size(initial_samples);
81         dive = malloc(size);
82         if (!dive)
83                 exit(1);
84         memset(dive, 0, size);
85         dive->alloc_samples = initial_samples;
86         return dive;
87 }
88
89 struct sample *prepare_sample(struct dive **divep)
90 {
91         struct dive *dive = *divep;
92         if (dive) {
93                 int nr = dive->samples;
94                 int alloc_samples = dive->alloc_samples;
95                 struct sample *sample;
96                 if (nr >= alloc_samples) {
97                         unsigned int size;
98
99                         alloc_samples = (alloc_samples * 3)/2 + 10;
100                         size = dive_size(alloc_samples);
101                         dive = realloc(dive, size);
102                         if (!dive)
103                                 return NULL;
104                         dive->alloc_samples = alloc_samples;
105                         *divep = dive;
106                 }
107                 sample = dive->sample + nr;
108                 memset(sample, 0, sizeof(*sample));
109                 return sample;
110         }
111         return NULL;
112 }
113
114 void finish_sample(struct dive *dive, struct sample *sample)
115 {
116         dive->samples++;
117 }
118
119 /*
120  * So when we re-calculate maxdepth and meandepth, we will
121  * not override the old numbers if they are close to the
122  * new ones.
123  *
124  * Why? Because a dive computer may well actually track the
125  * max depth and mean depth at finer granularity than the
126  * samples it stores. So it's possible that the max and mean
127  * have been reported more correctly originally.
128  *
129  * Only if the values calculated from the samples are clearly
130  * different do we override the normal depth values.
131  *
132  * This considers 1m to be "clearly different". That's
133  * a totally random number.
134  */
135 static void update_depth(depth_t *depth, int new)
136 {
137         if (new) {
138                 int old = depth->mm;
139
140                 if (abs(old - new) > 1000)
141                         depth->mm = new;
142         }
143 }
144
145 static void update_duration(duration_t *duration, int new)
146 {
147         if (new)
148                 duration->seconds = new;
149 }
150
151 static void update_temperature(temperature_t *temperature, int new)
152 {
153         if (new) {
154                 int old = temperature->mkelvin;
155
156                 if (abs(old - new) > 1000)
157                         temperature->mkelvin = new;
158         }
159 }
160
161 /*
162  * If you have more than 32 cylinders, you'd better have a 64-bit build.
163  * And if you have more than 64 cylinders, you need to use another tool,
164  * or fix this up to do something odd.
165  */
166 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
167 {
168         unsigned long mask;
169         unsigned int pressure, index;
170         cylinder_t *cyl;
171
172         pressure = sample->cylinderpressure.mbar;
173         if (!pressure)
174                 return flags;
175         index = sample->cylinderindex;
176         if (index >= MAX_CYLINDERS)
177                 return flags;
178         cyl = dive->cylinder + index;
179         if (!cyl->start.mbar)
180                 cyl->start.mbar = pressure;
181         /*
182          * If we already have an end pressure, without
183          * ever having seen a sample for this cylinder,
184          * that means that somebody set the end pressure
185          * by hand
186          */
187         mask = 1ul << index;
188         if (cyl->end.mbar) {
189                 if (!(flags & mask))
190                         return flags;
191         }
192         flags |= mask;
193
194         /* we need to handle the user entering beginning and end tank pressures
195          * - maybe even IF we have samples. But for now if we have air pressure
196          * data in the samples, we use that instead of the minimum
197          * if (!cyl->end.mbar || pressure < cyl->end.mbar)
198          */
199         cyl->end.mbar = pressure;
200         return flags;
201 }
202
203 struct dive *fixup_dive(struct dive *dive)
204 {
205         int i;
206         double depthtime = 0;
207         int lasttime = 0;
208         int start = -1, end = -1;
209         int maxdepth = 0, mintemp = 0;
210         int lastdepth = 0;
211         int lasttemp = 0;
212         unsigned long flags = 0;
213
214         for (i = 0; i < dive->samples; i++) {
215                 struct sample *sample = dive->sample + i;
216                 int time = sample->time.seconds;
217                 int depth = sample->depth.mm;
218                 int temp = sample->temperature.mkelvin;
219
220                 if (lastdepth)
221                         end = time;
222
223                 if (depth) {
224                         if (start < 0)
225                                 start = lasttime;
226                         if (depth > maxdepth)
227                                 maxdepth = depth;
228                 }
229
230                 flags = fixup_pressure(dive, sample, flags);
231
232                 if (temp) {
233                         /*
234                          * If we have consecutive identical
235                          * temperature readings, throw away
236                          * the redundant ones.
237                          */
238                         if (lasttemp == temp)
239                                 sample->temperature.mkelvin = 0;
240                         else
241                                 lasttemp = temp;
242
243                         if (!mintemp || temp < mintemp)
244                                 mintemp = temp;
245                 }
246                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
247                 lastdepth = depth;
248                 lasttime = time;
249         }
250         if (end < 0)
251                 return dive;
252
253         update_duration(&dive->duration, end - start);
254         if (start != end)
255                 depthtime /= (end - start);
256
257         update_depth(&dive->meandepth, depthtime);
258         update_temperature(&dive->watertemp, mintemp);
259         update_depth(&dive->maxdepth, maxdepth);
260
261         add_people(dive->buddy);
262         add_people(dive->divemaster);
263         add_location(dive->location);
264         for (i = 0; i < MAX_CYLINDERS; i++) {
265                 cylinder_type_t *type = &dive->cylinder[i].type;
266                 add_cylinder_description(type);
267         }
268
269         return dive;
270 }
271
272 /* Don't pick a zero for MERGE_MIN() */
273 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
274 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
275 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
276 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
277
278 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
279 {
280         struct sample *p = prepare_sample(&dive);
281
282         if (!p)
283                 return NULL;
284         *p = *sample;
285         p->time.seconds = time;
286         finish_sample(dive, p);
287         return dive;
288 }
289
290 /*
291  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
292  */
293 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
294 {
295         int asamples = a->samples;
296         int bsamples = b->samples;
297         struct sample *as = a->sample;
298         struct sample *bs = b->sample;
299
300         for (;;) {
301                 int at, bt;
302                 struct sample sample;
303
304                 if (!res)
305                         return NULL;
306
307                 at = asamples ? as->time.seconds : -1;
308                 bt = bsamples ? bs->time.seconds + offset : -1;
309
310                 /* No samples? All done! */
311                 if (at < 0 && bt < 0)
312                         return fixup_dive(res);
313
314                 /* Only samples from a? */
315                 if (bt < 0) {
316 add_sample_a:
317                         res = add_sample(as, at, res);
318                         as++;
319                         asamples--;
320                         continue;
321                 }
322
323                 /* Only samples from b? */
324                 if (at < 0) {
325 add_sample_b:
326                         res = add_sample(bs, bt, res);
327                         bs++;
328                         bsamples--;
329                         continue;
330                 }
331
332                 if (at < bt)
333                         goto add_sample_a;
334                 if (at > bt)
335                         goto add_sample_b;
336
337                 /* same-time sample: add a merged sample. Take the non-zero ones */
338                 sample = *bs;
339                 if (as->depth.mm)
340                         sample.depth = as->depth;
341                 if (as->temperature.mkelvin)
342                         sample.temperature = as->temperature;
343                 if (as->cylinderpressure.mbar)
344                         sample.cylinderpressure = as->cylinderpressure;
345                 if (as->cylinderindex)
346                         sample.cylinderindex = as->cylinderindex;
347
348                 res = add_sample(&sample, at, res);
349
350                 as++;
351                 bs++;
352                 asamples--;
353                 bsamples--;
354         }
355 }
356
357 static char *merge_text(const char *a, const char *b)
358 {
359         char *res;
360
361         if (!a || !*a)
362                 return (char *)b;
363         if (!b || !*b)
364                 return (char *)a;
365         if (!strcmp(a,b))
366                 return (char *)a;
367         res = malloc(strlen(a) + strlen(b) + 9);
368         if (!res)
369                 return (char *)a;
370         sprintf(res, "(%s) or (%s)", a, b);
371         return res;
372 }
373
374 #define SORT(a,b,field) \
375         if (a->field != b->field) return a->field < b->field ? -1 : 1
376
377 static int sort_event(struct event *a, struct event *b)
378 {
379         SORT(a,b,time.seconds);
380         SORT(a,b,type);
381         SORT(a,b,flags);
382         SORT(a,b,value);
383         return strcmp(a->name, b->name);
384 }
385
386 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
387 {
388         struct event *a, *b;
389         struct event **p = &res->events;
390
391         a = src1->events;
392         b = src2->events;
393         while (b) {
394                 b->time.seconds += offset;
395                 b = b->next;
396         }
397         b = src2->events;
398
399         while (a || b) {
400                 int s;
401                 if (!b) {
402                         *p = a;
403                         break;
404                 }
405                 if (!a) {
406                         *p = b;
407                         break;
408                 }
409                 s = sort_event(a, b);
410                 /* Pick b */
411                 if (s > 0) {
412                         *p = b;
413                         p = &b->next;
414                         b = b->next;
415                         continue;
416                 }
417                 /* Pick 'a' or neither */
418                 if (s < 0) {
419                         *p = a;
420                         p = &a->next;
421                 }
422                 a = a->next;
423                 continue;
424         }
425 }
426
427 /* Pick whichever has any info (if either). Prefer 'a' */
428 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
429 {
430         if (a->size.mliter)
431                 b = a;
432         *res = *b;
433 }
434
435 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
436 {
437         if (a->o2.permille)
438                 b = a;
439         *res = *b;
440 }
441
442 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
443 {
444         merge_cylinder_type(&res->type, &a->type, &b->type);
445         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
446         MERGE_MAX(res, a, b, start.mbar);
447         MERGE_MIN(res, a, b, end.mbar);
448 }
449
450 /*
451  * This could do a lot more merging. Right now it really only
452  * merges almost exact duplicates - something that happens easily
453  * with overlapping dive downloads.
454  */
455 struct dive *try_to_merge(struct dive *a, struct dive *b)
456 {
457         int i;
458         struct dive *res;
459
460         if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
461                 return NULL;
462
463         res = alloc_dive();
464
465         res->when = a->when;
466         MERGE_NONZERO(res, a, b, latitude);
467         MERGE_NONZERO(res, a, b, longitude);
468         MERGE_TXT(res, a, b, location);
469         MERGE_TXT(res, a, b, notes);
470         MERGE_TXT(res, a, b, buddy);
471         MERGE_TXT(res, a, b, divemaster);
472         MERGE_MAX(res, a, b, number);
473         MERGE_MAX(res, a, b, maxdepth.mm);
474         res->meandepth.mm = 0;
475         MERGE_MAX(res, a, b, duration.seconds);
476         MERGE_MAX(res, a, b, surfacetime.seconds);
477         MERGE_MAX(res, a, b, airtemp.mkelvin);
478         MERGE_MIN(res, a, b, watertemp.mkelvin);
479         for (i = 0; i < MAX_CYLINDERS; i++)
480                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
481         merge_events(res, a, b, 0);
482         return merge_samples(res, a, b, 0);
483 }