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->cylinderpressure.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->cylinderpressure.mbar)
172 sample.cylinderpressure = as->cylinderpressure;
173 if (as->cylinderindex)
174 sample.cylinderindex = as->cylinderindex;
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);
202 /* Pick whichever has any info (if either). Prefer 'a' */
203 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
210 static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
217 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
219 merge_cylinder_type(&res->type, &a->type, &b->type);
220 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
224 * This could do a lot more merging. Right now it really only
225 * merges almost exact duplicates - something that happens easily
226 * with overlapping dive downloads.
228 struct dive *try_to_merge(struct dive *a, struct dive *b)
233 if (a->when != b->when)
237 res = malloc(dive_size(alloc_samples));
240 memset(res, 0, dive_size(alloc_samples));
243 res->name = merge_text(a->name, b->name);
244 res->location = merge_text(a->location, b->location);
245 res->notes = merge_text(a->notes, b->notes);
246 MERGE_MAX(res, a, b, maxdepth.mm);
247 res->meandepth.mm = 0;
248 MERGE_MAX(res, a, b, duration.seconds);
249 MERGE_MAX(res, a, b, surfacetime.seconds);
250 MERGE_MAX(res, a, b, airtemp.mkelvin);
251 MERGE_MIN(res, a, b, watertemp.mkelvin);
252 MERGE_MAX(res, a, b, beginning_pressure.mbar);
253 MERGE_MAX(res, a, b, end_pressure.mbar);
254 for (i = 0; i < MAX_CYLINDERS; i++)
255 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
257 return merge_samples(res, a, b, 0);