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)
27 if (abs(old - new) > 1000)
32 static void update_duration(duration_t *duration, int new)
35 duration->seconds = new;
38 static void update_temperature(temperature_t *temperature, int new)
41 int old = temperature->mkelvin;
43 if (abs(old - new) > 1000)
44 temperature->mkelvin = new;
48 static void fixup_pressure(struct dive *dive, struct sample *sample)
50 unsigned int pressure, index;
53 pressure = sample->cylinderpressure.mbar;
56 index = sample->cylinderindex;
57 if (index >= MAX_CYLINDERS)
59 cyl = dive->cylinder + index;
61 cyl->start.mbar = pressure;
62 if (!cyl->end.mbar || pressure < cyl->end.mbar)
63 cyl->end.mbar = pressure;
66 struct dive *fixup_dive(struct dive *dive)
71 int start = -1, end = -1;
72 int maxdepth = 0, mintemp = 0;
75 temperature_t *redundant_temp = NULL;
77 for (i = 0; i < dive->samples; i++) {
78 struct sample *sample = dive->sample + i;
79 int time = sample->time.seconds;
80 int depth = sample->depth.mm;
81 int temp = sample->temperature.mkelvin;
93 fixup_pressure(dive, sample);
97 * If we have consecutive identical
98 * temperature readings, throw away
99 * the redundant ones. We care about
102 if (lasttemp == temp) {
104 redundant_temp->mkelvin = 0;
105 redundant_temp = &sample->temperature;
107 redundant_temp = NULL;
111 if (!mintemp || temp < mintemp)
114 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
121 update_duration(&dive->duration, end - start);
123 depthtime /= (end - start);
125 update_depth(&dive->meandepth, depthtime);
126 update_temperature(&dive->watertemp, mintemp);
127 update_depth(&dive->maxdepth, maxdepth);
132 /* Don't pick a zero for MERGE_MIN() */
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)
136 static int alloc_samples;
138 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
140 int nr = dive->samples;
143 if (nr >= alloc_samples) {
144 alloc_samples = (alloc_samples + 64) * 3 / 2;
145 dive = realloc(dive, dive_size(alloc_samples));
149 dive->samples = nr+1;
150 d = dive->sample + nr;
153 d->time.seconds = time;
158 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
160 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
162 int asamples = a->samples;
163 int bsamples = b->samples;
164 struct sample *as = a->sample;
165 struct sample *bs = b->sample;
169 struct sample sample;
174 at = asamples ? as->time.seconds : -1;
175 bt = bsamples ? bs->time.seconds + offset : -1;
177 /* No samples? All done! */
178 if (at < 0 && bt < 0)
179 return fixup_dive(res);
181 /* Only samples from a? */
184 res = add_sample(as, at, res);
190 /* Only samples from b? */
193 res = add_sample(bs, bt, res);
204 /* same-time sample: add a merged sample. Take the non-zero ones */
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;
215 res = add_sample(&sample, at, res);
224 static char *merge_text(const char *a, const char *b)
234 res = malloc(strlen(a) + strlen(b) + 9);
237 sprintf(res, "(%s) or (%s)", a, b);
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)
249 static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
256 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
258 merge_cylinder_type(&res->type, &a->type, &b->type);
259 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
260 MERGE_MAX(res, a, b, start.mbar);
261 MERGE_MIN(res, a, b, end.mbar);
265 * This could do a lot more merging. Right now it really only
266 * merges almost exact duplicates - something that happens easily
267 * with overlapping dive downloads.
269 struct dive *try_to_merge(struct dive *a, struct dive *b)
274 if (a->when != b->when)
278 res = malloc(dive_size(alloc_samples));
281 memset(res, 0, dive_size(alloc_samples));
284 res->location = merge_text(a->location, b->location);
285 res->notes = merge_text(a->notes, b->notes);
286 MERGE_MAX(res, a, b, maxdepth.mm);
287 res->meandepth.mm = 0;
288 MERGE_MAX(res, a, b, duration.seconds);
289 MERGE_MAX(res, a, b, surfacetime.seconds);
290 MERGE_MAX(res, a, b, airtemp.mkelvin);
291 MERGE_MIN(res, a, b, watertemp.mkelvin);
292 for (i = 0; i < MAX_CYLINDERS; i++)
293 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
295 return merge_samples(res, a, b, 0);