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 calculated 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 temperature_t *redundant_temp = NULL;
43 for (i = 0; i < dive->samples; i++) {
44 struct sample *sample = dive->sample + i;
45 int time = sample->time.seconds;
46 int depth = sample->depth.mm;
47 int press = sample->cylinderpressure.mbar;
48 int temp = sample->temperature.mkelvin;
66 * If we have consecutive identical
67 * temperature readings, throw away
68 * the redundant ones. We care about
71 if (lasttemp == temp) {
73 redundant_temp->mkelvin = 0;
74 redundant_temp = &sample->temperature;
76 redundant_temp = NULL;
83 if (!mintemp || temp < mintemp)
86 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
92 dive->duration.seconds = end - start;
94 update_depth(&dive->meandepth, depthtime / (end - start));
96 dive->beginning_pressure.mbar = startpress;
98 dive->end_pressure.mbar = endpress;
100 dive->watertemp.mkelvin = mintemp;
103 update_depth(&dive->maxdepth, maxdepth);
108 /* Don't pick a zero for MERGE_MIN() */
109 #define MIN(a,b) ((a)<(b)?(a):(b))
110 #define MAX(a,b) ((a)>(b)?(a):(b))
111 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
112 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
114 static int alloc_samples;
116 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
118 int nr = dive->samples;
121 if (nr >= alloc_samples) {
122 alloc_samples = (alloc_samples + 64) * 3 / 2;
123 dive = realloc(dive, dive_size(alloc_samples));
127 dive->samples = nr+1;
128 d = dive->sample + nr;
131 d->time.seconds = time;
136 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
138 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
140 int asamples = a->samples;
141 int bsamples = b->samples;
142 struct sample *as = a->sample;
143 struct sample *bs = b->sample;
147 struct sample sample;
152 at = asamples ? as->time.seconds : -1;
153 bt = bsamples ? bs->time.seconds + offset : -1;
155 /* No samples? All done! */
156 if (at < 0 && bt < 0)
157 return fixup_dive(res);
159 /* Only samples from a? */
162 res = add_sample(as, at, res);
168 /* Only samples from b? */
171 res = add_sample(bs, bt, res);
182 /* same-time sample: add a merged sample. Take the non-zero ones */
185 sample.depth = as->depth;
186 if (as->temperature.mkelvin)
187 sample.temperature = as->temperature;
188 if (as->cylinderpressure.mbar)
189 sample.cylinderpressure = as->cylinderpressure;
190 if (as->cylinderindex)
191 sample.cylinderindex = as->cylinderindex;
193 res = add_sample(&sample, at, res);
202 static char *merge_text(const char *a, const char *b)
212 res = malloc(strlen(a) + strlen(b) + 9);
215 sprintf(res, "(%s) or (%s)", a, b);
219 /* Pick whichever has any info (if either). Prefer 'a' */
220 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
227 static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
234 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
236 merge_cylinder_type(&res->type, &a->type, &b->type);
237 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
241 * This could do a lot more merging. Right now it really only
242 * merges almost exact duplicates - something that happens easily
243 * with overlapping dive downloads.
245 struct dive *try_to_merge(struct dive *a, struct dive *b)
250 if (a->when != b->when)
254 res = malloc(dive_size(alloc_samples));
257 memset(res, 0, dive_size(alloc_samples));
260 res->name = merge_text(a->name, b->name);
261 res->location = merge_text(a->location, b->location);
262 res->notes = merge_text(a->notes, b->notes);
263 MERGE_MAX(res, a, b, maxdepth.mm);
264 res->meandepth.mm = 0;
265 MERGE_MAX(res, a, b, duration.seconds);
266 MERGE_MAX(res, a, b, surfacetime.seconds);
267 MERGE_MAX(res, a, b, airtemp.mkelvin);
268 MERGE_MIN(res, a, b, watertemp.mkelvin);
269 MERGE_MAX(res, a, b, beginning_pressure.mbar);
270 MERGE_MAX(res, a, b, end_pressure.mbar);
271 for (i = 0; i < MAX_CYLINDERS; i++)
272 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
274 return merge_samples(res, a, b, 0);