6 struct dive *alloc_dive(void)
8 const int initial_samples = 5;
12 size = dive_size(initial_samples);
16 memset(dive, 0, size);
17 dive->alloc_samples = initial_samples;
21 struct sample *prepare_sample(struct dive **divep)
23 struct dive *dive = *divep;
25 int nr = dive->samples;
26 int alloc_samples = dive->alloc_samples;
27 struct sample *sample;
28 if (nr >= alloc_samples) {
31 alloc_samples = (alloc_samples * 3)/2 + 10;
32 size = dive_size(alloc_samples);
33 dive = realloc(dive, size);
36 dive->alloc_samples = alloc_samples;
39 sample = dive->sample + nr;
40 memset(sample, 0, sizeof(*sample));
46 void finish_sample(struct dive *dive, struct sample *sample)
52 * So when we re-calculate maxdepth and meandepth, we will
53 * not override the old numbers if they are close to the
56 * Why? Because a dive computer may well actually track the
57 * max depth and mean depth at finer granularity than the
58 * samples it stores. So it's possible that the max and mean
59 * have been reported more correctly originally.
61 * Only if the values calculated from the samples are clearly
62 * different do we override the normal depth values.
64 * This considers 1m to be "clearly different". That's
65 * a totally random number.
67 static void update_depth(depth_t *depth, int new)
72 if (abs(old - new) > 1000)
77 static void update_duration(duration_t *duration, int new)
80 duration->seconds = new;
83 static void update_temperature(temperature_t *temperature, int new)
86 int old = temperature->mkelvin;
88 if (abs(old - new) > 1000)
89 temperature->mkelvin = new;
93 static void fixup_pressure(struct dive *dive, struct sample *sample)
95 unsigned int pressure, index;
98 pressure = sample->cylinderpressure.mbar;
101 index = sample->cylinderindex;
102 if (index >= MAX_CYLINDERS)
104 cyl = dive->cylinder + index;
105 if (!cyl->start.mbar)
106 cyl->start.mbar = pressure;
107 if (!cyl->end.mbar || pressure < cyl->end.mbar)
108 cyl->end.mbar = pressure;
111 struct dive *fixup_dive(struct dive *dive)
114 double depthtime = 0;
116 int start = -1, end = -1;
117 int maxdepth = 0, mintemp = 0;
121 for (i = 0; i < dive->samples; i++) {
122 struct sample *sample = dive->sample + i;
123 int time = sample->time.seconds;
124 int depth = sample->depth.mm;
125 int temp = sample->temperature.mkelvin;
133 if (depth > maxdepth)
137 fixup_pressure(dive, sample);
141 * If we have consecutive identical
142 * temperature readings, throw away
143 * the redundant ones.
145 if (lasttemp == temp)
146 sample->temperature.mkelvin = 0;
150 if (!mintemp || temp < mintemp)
153 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
160 update_duration(&dive->duration, end - start);
162 depthtime /= (end - start);
164 update_depth(&dive->meandepth, depthtime);
165 update_temperature(&dive->watertemp, mintemp);
166 update_depth(&dive->maxdepth, maxdepth);
171 /* Don't pick a zero for MERGE_MIN() */
172 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
173 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
175 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
177 struct sample *p = prepare_sample(&dive);
182 p->time.seconds = time;
183 finish_sample(dive, p);
188 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
190 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
192 int asamples = a->samples;
193 int bsamples = b->samples;
194 struct sample *as = a->sample;
195 struct sample *bs = b->sample;
199 struct sample sample;
204 at = asamples ? as->time.seconds : -1;
205 bt = bsamples ? bs->time.seconds + offset : -1;
207 /* No samples? All done! */
208 if (at < 0 && bt < 0)
209 return fixup_dive(res);
211 /* Only samples from a? */
214 res = add_sample(as, at, res);
220 /* Only samples from b? */
223 res = add_sample(bs, bt, res);
234 /* same-time sample: add a merged sample. Take the non-zero ones */
237 sample.depth = as->depth;
238 if (as->temperature.mkelvin)
239 sample.temperature = as->temperature;
240 if (as->cylinderpressure.mbar)
241 sample.cylinderpressure = as->cylinderpressure;
242 if (as->cylinderindex)
243 sample.cylinderindex = as->cylinderindex;
245 res = add_sample(&sample, at, res);
254 static char *merge_text(const char *a, const char *b)
264 res = malloc(strlen(a) + strlen(b) + 9);
267 sprintf(res, "(%s) or (%s)", a, b);
271 /* Pick whichever has any info (if either). Prefer 'a' */
272 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
279 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
286 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
288 merge_cylinder_type(&res->type, &a->type, &b->type);
289 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
290 MERGE_MAX(res, a, b, start.mbar);
291 MERGE_MIN(res, a, b, end.mbar);
295 * This could do a lot more merging. Right now it really only
296 * merges almost exact duplicates - something that happens easily
297 * with overlapping dive downloads.
299 struct dive *try_to_merge(struct dive *a, struct dive *b)
304 if (a->when != b->when)
310 res->location = merge_text(a->location, b->location);
311 res->notes = merge_text(a->notes, b->notes);
312 MERGE_MAX(res, a, b, number);
313 MERGE_MAX(res, a, b, maxdepth.mm);
314 res->meandepth.mm = 0;
315 MERGE_MAX(res, a, b, duration.seconds);
316 MERGE_MAX(res, a, b, surfacetime.seconds);
317 MERGE_MAX(res, a, b, airtemp.mkelvin);
318 MERGE_MIN(res, a, b, watertemp.mkelvin);
319 for (i = 0; i < MAX_CYLINDERS; i++)
320 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
322 return merge_samples(res, a, b, 0);