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;
120 temperature_t *redundant_temp = NULL;
122 for (i = 0; i < dive->samples; i++) {
123 struct sample *sample = dive->sample + i;
124 int time = sample->time.seconds;
125 int depth = sample->depth.mm;
126 int temp = sample->temperature.mkelvin;
134 if (depth > maxdepth)
138 fixup_pressure(dive, sample);
142 * If we have consecutive identical
143 * temperature readings, throw away
144 * the redundant ones. We care about
147 if (lasttemp == temp) {
149 redundant_temp->mkelvin = 0;
150 redundant_temp = &sample->temperature;
152 redundant_temp = NULL;
156 if (!mintemp || temp < mintemp)
159 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
166 update_duration(&dive->duration, end - start);
168 depthtime /= (end - start);
170 update_depth(&dive->meandepth, depthtime);
171 update_temperature(&dive->watertemp, mintemp);
172 update_depth(&dive->maxdepth, maxdepth);
177 /* Don't pick a zero for MERGE_MIN() */
178 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
179 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
181 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
183 struct sample *p = prepare_sample(&dive);
188 p->time.seconds = time;
189 finish_sample(dive, p);
194 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
196 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
198 int asamples = a->samples;
199 int bsamples = b->samples;
200 struct sample *as = a->sample;
201 struct sample *bs = b->sample;
205 struct sample sample;
210 at = asamples ? as->time.seconds : -1;
211 bt = bsamples ? bs->time.seconds + offset : -1;
213 /* No samples? All done! */
214 if (at < 0 && bt < 0)
215 return fixup_dive(res);
217 /* Only samples from a? */
220 res = add_sample(as, at, res);
226 /* Only samples from b? */
229 res = add_sample(bs, bt, res);
240 /* same-time sample: add a merged sample. Take the non-zero ones */
243 sample.depth = as->depth;
244 if (as->temperature.mkelvin)
245 sample.temperature = as->temperature;
246 if (as->cylinderpressure.mbar)
247 sample.cylinderpressure = as->cylinderpressure;
248 if (as->cylinderindex)
249 sample.cylinderindex = as->cylinderindex;
251 res = add_sample(&sample, at, res);
260 static char *merge_text(const char *a, const char *b)
270 res = malloc(strlen(a) + strlen(b) + 9);
273 sprintf(res, "(%s) or (%s)", a, b);
277 /* Pick whichever has any info (if either). Prefer 'a' */
278 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
285 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
292 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
294 merge_cylinder_type(&res->type, &a->type, &b->type);
295 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
296 MERGE_MAX(res, a, b, start.mbar);
297 MERGE_MIN(res, a, b, end.mbar);
301 * This could do a lot more merging. Right now it really only
302 * merges almost exact duplicates - something that happens easily
303 * with overlapping dive downloads.
305 struct dive *try_to_merge(struct dive *a, struct dive *b)
310 if (a->when != b->when)
316 res->location = merge_text(a->location, b->location);
317 res->notes = merge_text(a->notes, b->notes);
318 MERGE_MAX(res, a, b, number);
319 MERGE_MAX(res, a, b, maxdepth.mm);
320 res->meandepth.mm = 0;
321 MERGE_MAX(res, a, b, duration.seconds);
322 MERGE_MAX(res, a, b, surfacetime.seconds);
323 MERGE_MAX(res, a, b, airtemp.mkelvin);
324 MERGE_MIN(res, a, b, watertemp.mkelvin);
325 for (i = 0; i < MAX_CYLINDERS; i++)
326 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
328 return merge_samples(res, a, b, 0);