]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Abstract out dive/sample allocation a bit
[ext/subsurface.git] / dive.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "dive.h"
5
6 struct dive *alloc_dive(void)
7 {
8         const int initial_samples = 5;
9         unsigned int size;
10         struct dive *dive;
11
12         size = dive_size(initial_samples);
13         dive = malloc(size);
14         if (!dive)
15                 exit(1);
16         memset(dive, 0, size);
17         dive->alloc_samples = initial_samples;
18         return dive;
19 }
20
21 struct sample *prepare_sample(struct dive **divep)
22 {
23         struct dive *dive = *divep;
24         if (dive) {
25                 int nr = dive->samples;
26                 int alloc_samples = dive->alloc_samples;
27                 struct sample *sample;
28                 if (nr >= alloc_samples) {
29                         unsigned int size;
30
31                         alloc_samples = (alloc_samples * 3)/2 + 10;
32                         size = dive_size(alloc_samples);
33                         dive = realloc(dive, size);
34                         if (!dive)
35                                 return NULL;
36                         dive->alloc_samples = alloc_samples;
37                         *divep = dive;
38                 }
39                 sample = dive->sample + nr;
40                 memset(sample, 0, sizeof(*sample));
41                 return sample;
42         }
43         return NULL;
44 }
45
46 void finish_sample(struct dive *dive, struct sample *sample)
47 {
48         dive->samples++;
49 }
50
51 /*
52  * So when we re-calculate maxdepth and meandepth, we will
53  * not override the old numbers if they are close to the
54  * new ones.
55  *
56  * Why? Because a dive computer may well actually track the
57  * max depth and mean depth at finer granularity than the
58  * samples it stores. So it's possible that the max and mean
59  * have been reported more correctly originally.
60  *
61  * Only if the values calculated from the samples are clearly
62  * different do we override the normal depth values.
63  *
64  * This considers 1m to be "clearly different". That's
65  * a totally random number.
66  */
67 static void update_depth(depth_t *depth, int new)
68 {
69         if (new) {
70                 int old = depth->mm;
71
72                 if (abs(old - new) > 1000)
73                         depth->mm = new;
74         }
75 }
76
77 static void update_duration(duration_t *duration, int new)
78 {
79         if (new)
80                 duration->seconds = new;
81 }
82
83 static void update_temperature(temperature_t *temperature, int new)
84 {
85         if (new) {
86                 int old = temperature->mkelvin;
87
88                 if (abs(old - new) > 1000)
89                         temperature->mkelvin = new;
90         }
91 }
92
93 static void fixup_pressure(struct dive *dive, struct sample *sample)
94 {
95         unsigned int pressure, index;
96         cylinder_t *cyl;
97
98         pressure = sample->cylinderpressure.mbar;
99         if (!pressure)
100                 return;
101         index = sample->cylinderindex;
102         if (index >= MAX_CYLINDERS)
103                 return;
104         cyl = dive->cylinder + index;
105         if (!cyl->start.mbar)
106                 cyl->start.mbar = pressure;
107         if (!cyl->end.mbar || pressure < cyl->end.mbar)
108                 cyl->end.mbar = pressure;
109 }
110
111 struct dive *fixup_dive(struct dive *dive)
112 {
113         int i;
114         double depthtime = 0;
115         int lasttime = 0;
116         int start = -1, end = -1;
117         int maxdepth = 0, mintemp = 0;
118         int lastdepth = 0;
119         int lasttemp = 0;
120         temperature_t *redundant_temp = NULL;
121
122         for (i = 0; i < dive->samples; i++) {
123                 struct sample *sample = dive->sample + i;
124                 int time = sample->time.seconds;
125                 int depth = sample->depth.mm;
126                 int temp = sample->temperature.mkelvin;
127
128                 if (lastdepth)
129                         end = time;
130
131                 if (depth) {
132                         if (start < 0)
133                                 start = lasttime;
134                         if (depth > maxdepth)
135                                 maxdepth = depth;
136                 }
137
138                 fixup_pressure(dive, sample);
139
140                 if (temp) {
141                         /*
142                          * If we have consecutive identical
143                          * temperature readings, throw away
144                          * the redundant ones. We care about
145                          * the "edges" only.
146                          */
147                         if (lasttemp == temp) {
148                                 if (redundant_temp)
149                                         redundant_temp->mkelvin = 0;
150                                 redundant_temp = &sample->temperature;
151                         } else {
152                                 redundant_temp = NULL;
153                                 lasttemp = temp;
154                         }
155
156                         if (!mintemp || temp < mintemp)
157                                 mintemp = temp;
158                 }
159                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
160                 lastdepth = depth;
161                 lasttime = time;
162         }
163         if (end < 0)
164                 return dive;
165
166         update_duration(&dive->duration, end - start);
167         if (start != end)
168                 depthtime /= (end - start);
169
170         update_depth(&dive->meandepth, depthtime);
171         update_temperature(&dive->watertemp, mintemp);
172         update_depth(&dive->maxdepth, maxdepth);
173
174         return dive;
175 }
176
177 /* Don't pick a zero for MERGE_MIN() */
178 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
179 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
180
181 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
182 {
183         struct sample *p = prepare_sample(&dive);
184
185         if (!p)
186                 return NULL;
187         *p = *sample;
188         p->time.seconds = time;
189         finish_sample(dive, p);
190         return dive;
191 }
192
193 /*
194  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
195  */
196 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
197 {
198         int asamples = a->samples;
199         int bsamples = b->samples;
200         struct sample *as = a->sample;
201         struct sample *bs = b->sample;
202
203         for (;;) {
204                 int at, bt;
205                 struct sample sample;
206
207                 if (!res)
208                         return NULL;
209
210                 at = asamples ? as->time.seconds : -1;
211                 bt = bsamples ? bs->time.seconds + offset : -1;
212
213                 /* No samples? All done! */
214                 if (at < 0 && bt < 0)
215                         return fixup_dive(res);
216
217                 /* Only samples from a? */
218                 if (bt < 0) {
219 add_sample_a:
220                         res = add_sample(as, at, res);
221                         as++;
222                         asamples--;
223                         continue;
224                 }
225
226                 /* Only samples from b? */
227                 if (at < 0) {
228 add_sample_b:
229                         res = add_sample(bs, bt, res);
230                         bs++;
231                         bsamples--;
232                         continue;
233                 }
234
235                 if (at < bt)
236                         goto add_sample_a;
237                 if (at > bt)
238                         goto add_sample_b;
239
240                 /* same-time sample: add a merged sample. Take the non-zero ones */
241                 sample = *bs;
242                 if (as->depth.mm)
243                         sample.depth = as->depth;
244                 if (as->temperature.mkelvin)
245                         sample.temperature = as->temperature;
246                 if (as->cylinderpressure.mbar)
247                         sample.cylinderpressure = as->cylinderpressure;
248                 if (as->cylinderindex)
249                         sample.cylinderindex = as->cylinderindex;
250
251                 res = add_sample(&sample, at, res);
252
253                 as++;
254                 bs++;
255                 asamples--;
256                 bsamples--;
257         }
258 }
259
260 static char *merge_text(const char *a, const char *b)
261 {
262         char *res;
263
264         if (!a || !*a)
265                 return (char *)b;
266         if (!b || !*b)
267                 return (char *)a;
268         if (!strcmp(a,b))
269                 return (char *)a;
270         res = malloc(strlen(a) + strlen(b) + 9);
271         if (!res)
272                 return (char *)a;
273         sprintf(res, "(%s) or (%s)", a, b);
274         return res;
275 }
276
277 /* Pick whichever has any info (if either). Prefer 'a' */
278 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
279 {
280         if (a->size.mliter)
281                 b = a;
282         *res = *b;
283 }
284
285 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
286 {
287         if (a->o2.permille)
288                 b = a;
289         *res = *b;
290 }
291
292 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
293 {
294         merge_cylinder_type(&res->type, &a->type, &b->type);
295         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
296         MERGE_MAX(res, a, b, start.mbar);
297         MERGE_MIN(res, a, b, end.mbar);
298 }
299
300 /*
301  * This could do a lot more merging. Right now it really only
302  * merges almost exact duplicates - something that happens easily
303  * with overlapping dive downloads.
304  */
305 struct dive *try_to_merge(struct dive *a, struct dive *b)
306 {
307         int i;
308         struct dive *res;
309
310         if (a->when != b->when)
311                 return NULL;
312
313         res = alloc_dive();
314
315         res->when = a->when;
316         res->location = merge_text(a->location, b->location);
317         res->notes = merge_text(a->notes, b->notes);
318         MERGE_MAX(res, a, b, number);
319         MERGE_MAX(res, a, b, maxdepth.mm);
320         res->meandepth.mm = 0;
321         MERGE_MAX(res, a, b, duration.seconds);
322         MERGE_MAX(res, a, b, surfacetime.seconds);
323         MERGE_MAX(res, a, b, airtemp.mkelvin);
324         MERGE_MIN(res, a, b, watertemp.mkelvin);
325         for (i = 0; i < MAX_CYLINDERS; i++)
326                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
327
328         return merge_samples(res, a, b, 0);
329 }