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 maxdepth = 0, mintemp = 0;
40 temperature_t *redundant_temp = NULL;
42 for (i = 0; i < dive->samples; i++) {
43 struct sample *sample = dive->sample + i;
44 int time = sample->time.seconds;
45 int depth = sample->depth.mm;
46 int press = sample->cylinderpressure.mbar;
47 int temp = sample->temperature.mkelvin;
65 * If we have consecutive identical
66 * temperature readings, throw away
67 * the redundant ones. We care about
70 if (lasttemp == temp) {
72 redundant_temp->mkelvin = 0;
73 redundant_temp = &sample->temperature;
75 redundant_temp = NULL;
79 if (!mintemp || temp < mintemp)
82 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
88 dive->duration.seconds = end - start;
90 update_depth(&dive->meandepth, depthtime / (end - start));
92 dive->beginning_pressure.mbar = startpress;
94 dive->end_pressure.mbar = endpress;
96 dive->watertemp.mkelvin = mintemp;
99 update_depth(&dive->maxdepth, maxdepth);
104 /* Don't pick a zero for MERGE_MIN() */
105 #define MIN(a,b) ((a)<(b)?(a):(b))
106 #define MAX(a,b) ((a)>(b)?(a):(b))
107 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
108 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
110 static int alloc_samples;
112 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
114 int nr = dive->samples;
117 if (nr >= alloc_samples) {
118 alloc_samples = (alloc_samples + 64) * 3 / 2;
119 dive = realloc(dive, dive_size(alloc_samples));
123 dive->samples = nr+1;
124 d = dive->sample + nr;
127 d->time.seconds = time;
132 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
134 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
136 int asamples = a->samples;
137 int bsamples = b->samples;
138 struct sample *as = a->sample;
139 struct sample *bs = b->sample;
143 struct sample sample;
148 at = asamples ? as->time.seconds : -1;
149 bt = bsamples ? bs->time.seconds + offset : -1;
151 /* No samples? All done! */
152 if (at < 0 && bt < 0)
153 return fixup_dive(res);
155 /* Only samples from a? */
158 res = add_sample(as, at, res);
164 /* Only samples from b? */
167 res = add_sample(bs, bt, res);
178 /* same-time sample: add a merged sample. Take the non-zero ones */
181 sample.depth = as->depth;
182 if (as->temperature.mkelvin)
183 sample.temperature = as->temperature;
184 if (as->cylinderpressure.mbar)
185 sample.cylinderpressure = as->cylinderpressure;
186 if (as->cylinderindex)
187 sample.cylinderindex = as->cylinderindex;
189 res = add_sample(&sample, at, res);
198 static char *merge_text(const char *a, const char *b)
208 res = malloc(strlen(a) + strlen(b) + 9);
211 sprintf(res, "(%s) or (%s)", a, b);
215 /* Pick whichever has any info (if either). Prefer 'a' */
216 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
223 static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
230 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
232 merge_cylinder_type(&res->type, &a->type, &b->type);
233 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
237 * This could do a lot more merging. Right now it really only
238 * merges almost exact duplicates - something that happens easily
239 * with overlapping dive downloads.
241 struct dive *try_to_merge(struct dive *a, struct dive *b)
246 if (a->when != b->when)
250 res = malloc(dive_size(alloc_samples));
253 memset(res, 0, dive_size(alloc_samples));
256 res->location = merge_text(a->location, b->location);
257 res->notes = merge_text(a->notes, b->notes);
258 MERGE_MAX(res, a, b, maxdepth.mm);
259 res->meandepth.mm = 0;
260 MERGE_MAX(res, a, b, duration.seconds);
261 MERGE_MAX(res, a, b, surfacetime.seconds);
262 MERGE_MAX(res, a, b, airtemp.mkelvin);
263 MERGE_MIN(res, a, b, watertemp.mkelvin);
264 MERGE_MAX(res, a, b, beginning_pressure.mbar);
265 MERGE_MAX(res, a, b, end_pressure.mbar);
266 for (i = 0; i < MAX_CYLINDERS; i++)
267 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
269 return merge_samples(res, a, b, 0);