7 * So when we re-calculate maxdepth and meandepth, we will
8 * not override the old numbers if they are close to the
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.
16 * Only if the values calulcated from the samples are clearly
17 * different do we override the normal depth values.
19 * This considers 1m to be "clearly different". That's
20 * a totally random number.
22 static void update_depth(depth_t *depth, int new)
26 if (abs(old - new) > 1000)
30 struct dive *fixup_dive(struct dive *dive)
35 int start = -1, end = -1;
36 int startpress = 0, endpress = 0;
37 int starttemp = 0, endtemp = 0;
38 int maxdepth = 0, mintemp = 0;
41 for (i = 0; i < dive->samples; i++) {
42 struct sample *sample = dive->sample + i;
43 int time = sample->time.seconds;
44 int depth = sample->depth.mm;
45 int press = sample->tankpressure.mbar;
46 int temp = sample->temperature.mkelvin;
66 if (!mintemp || temp < mintemp)
69 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
75 dive->duration.seconds = end - start;
77 update_depth(&dive->meandepth, depthtime / (end - start));
79 dive->beginning_pressure.mbar = startpress;
81 dive->end_pressure.mbar = endpress;
83 dive->watertemp.mkelvin = mintemp;
86 update_depth(&dive->maxdepth, maxdepth);
91 /* Don't pick a zero for MERGE_MIN() */
92 #define MIN(a,b) ((a)<(b)?(a):(b))
93 #define MAX(a,b) ((a)>(b)?(a):(b))
94 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
95 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
97 static int alloc_samples;
99 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
101 int nr = dive->samples;
104 if (nr >= alloc_samples) {
105 alloc_samples = (alloc_samples + 64) * 3 / 2;
106 dive = realloc(dive, dive_size(alloc_samples));
110 dive->samples = nr+1;
111 d = dive->sample + nr;
114 d->time.seconds = time;
119 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
121 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
123 int asamples = a->samples;
124 int bsamples = b->samples;
125 struct sample *as = a->sample;
126 struct sample *bs = b->sample;
130 struct sample sample;
135 at = asamples ? as->time.seconds : -1;
136 bt = bsamples ? bs->time.seconds + offset : -1;
138 /* No samples? All done! */
139 if (at < 0 && bt < 0)
140 return fixup_dive(res);
142 /* Only samples from a? */
145 res = add_sample(as, at, res);
151 /* Only samples from b? */
154 res = add_sample(bs, bt, res);
165 /* same-time sample: add a merged sample. Take the non-zero ones */
168 sample.depth = as->depth;
169 if (as->temperature.mkelvin)
170 sample.temperature = as->temperature;
171 if (as->tankpressure.mbar)
172 sample.tankpressure = as->tankpressure;
174 sample.tankindex = as->tankindex;
176 res = add_sample(&sample, at, res);
185 static char *merge_text(const char *a, const char *b)
195 res = malloc(strlen(a) + strlen(b) + 9);
198 sprintf(res, "(%s) or (%s)", a, b);
203 * This could do a lot more merging. Right now it really only
204 * merges almost exact duplicates - something that happens easily
205 * with overlapping dive downloads.
207 struct dive *try_to_merge(struct dive *a, struct dive *b)
212 if (a->when != b->when)
216 res = malloc(dive_size(alloc_samples));
219 memset(res, 0, dive_size(alloc_samples));
222 res->name = merge_text(a->name, b->name);
223 res->location = merge_text(a->location, b->location);
224 res->notes = merge_text(a->notes, b->notes);
225 MERGE_MAX(res, a, b, maxdepth.mm);
226 res->meandepth.mm = 0;
227 MERGE_MAX(res, a, b, duration.seconds);
228 MERGE_MAX(res, a, b, surfacetime.seconds);
229 MERGE_MAX(res, a, b, airtemp.mkelvin);
230 MERGE_MIN(res, a, b, watertemp.mkelvin);
231 MERGE_MAX(res, a, b, beginning_pressure.mbar);
232 MERGE_MAX(res, a, b, end_pressure.mbar);
233 for (i = 0; i < MAX_MIXES; i++) {
234 if (a->gasmix[i].o2.permille) {
235 res->gasmix[i] = a->gasmix[i];
238 res->gasmix[i] = b->gasmix[i];
240 return merge_samples(res, a, b, 0);