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