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