2 /* maintains the internal dive list structure */
8 double get_depth_units(unsigned int mm, int *frac, const char **units)
14 switch (output_units.length) {
33 struct dive *alloc_dive(void)
35 const int initial_samples = 5;
39 size = dive_size(initial_samples);
43 memset(dive, 0, size);
44 dive->alloc_samples = initial_samples;
48 struct sample *prepare_sample(struct dive **divep)
50 struct dive *dive = *divep;
52 int nr = dive->samples;
53 int alloc_samples = dive->alloc_samples;
54 struct sample *sample;
55 if (nr >= alloc_samples) {
58 alloc_samples = (alloc_samples * 3)/2 + 10;
59 size = dive_size(alloc_samples);
60 dive = realloc(dive, size);
63 dive->alloc_samples = alloc_samples;
66 sample = dive->sample + nr;
67 memset(sample, 0, sizeof(*sample));
73 void finish_sample(struct dive *dive, struct sample *sample)
79 * So when we re-calculate maxdepth and meandepth, we will
80 * not override the old numbers if they are close to the
83 * Why? Because a dive computer may well actually track the
84 * max depth and mean depth at finer granularity than the
85 * samples it stores. So it's possible that the max and mean
86 * have been reported more correctly originally.
88 * Only if the values calculated from the samples are clearly
89 * different do we override the normal depth values.
91 * This considers 1m to be "clearly different". That's
92 * a totally random number.
94 static void update_depth(depth_t *depth, int new)
99 if (abs(old - new) > 1000)
104 static void update_duration(duration_t *duration, int new)
107 duration->seconds = new;
110 static void update_temperature(temperature_t *temperature, int new)
113 int old = temperature->mkelvin;
115 if (abs(old - new) > 1000)
116 temperature->mkelvin = new;
120 static void fixup_pressure(struct dive *dive, struct sample *sample)
122 unsigned int pressure, index;
125 pressure = sample->cylinderpressure.mbar;
128 index = sample->cylinderindex;
129 if (index >= MAX_CYLINDERS)
131 cyl = dive->cylinder + index;
132 if (!cyl->start.mbar)
133 cyl->start.mbar = pressure;
134 if (!cyl->end.mbar || pressure < cyl->end.mbar)
135 cyl->end.mbar = pressure;
138 struct dive *fixup_dive(struct dive *dive)
141 double depthtime = 0;
143 int start = -1, end = -1;
144 int maxdepth = 0, mintemp = 0;
148 for (i = 0; i < dive->samples; i++) {
149 struct sample *sample = dive->sample + i;
150 int time = sample->time.seconds;
151 int depth = sample->depth.mm;
152 int temp = sample->temperature.mkelvin;
160 if (depth > maxdepth)
164 fixup_pressure(dive, sample);
168 * If we have consecutive identical
169 * temperature readings, throw away
170 * the redundant ones.
172 if (lasttemp == temp)
173 sample->temperature.mkelvin = 0;
177 if (!mintemp || temp < mintemp)
180 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
187 update_duration(&dive->duration, end - start);
189 depthtime /= (end - start);
191 update_depth(&dive->meandepth, depthtime);
192 update_temperature(&dive->watertemp, mintemp);
193 update_depth(&dive->maxdepth, maxdepth);
198 /* Don't pick a zero for MERGE_MIN() */
199 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
200 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
201 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
203 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
205 struct sample *p = prepare_sample(&dive);
210 p->time.seconds = time;
211 finish_sample(dive, p);
216 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
218 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
220 int asamples = a->samples;
221 int bsamples = b->samples;
222 struct sample *as = a->sample;
223 struct sample *bs = b->sample;
227 struct sample sample;
232 at = asamples ? as->time.seconds : -1;
233 bt = bsamples ? bs->time.seconds + offset : -1;
235 /* No samples? All done! */
236 if (at < 0 && bt < 0)
237 return fixup_dive(res);
239 /* Only samples from a? */
242 res = add_sample(as, at, res);
248 /* Only samples from b? */
251 res = add_sample(bs, bt, res);
262 /* same-time sample: add a merged sample. Take the non-zero ones */
265 sample.depth = as->depth;
266 if (as->temperature.mkelvin)
267 sample.temperature = as->temperature;
268 if (as->cylinderpressure.mbar)
269 sample.cylinderpressure = as->cylinderpressure;
270 if (as->cylinderindex)
271 sample.cylinderindex = as->cylinderindex;
273 res = add_sample(&sample, at, res);
282 static char *merge_text(const char *a, const char *b)
292 res = malloc(strlen(a) + strlen(b) + 9);
295 sprintf(res, "(%s) or (%s)", a, b);
299 /* Pick whichever has any info (if either). Prefer 'a' */
300 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
307 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
314 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
316 merge_cylinder_type(&res->type, &a->type, &b->type);
317 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
318 MERGE_MAX(res, a, b, start.mbar);
319 MERGE_MIN(res, a, b, end.mbar);
323 * This could do a lot more merging. Right now it really only
324 * merges almost exact duplicates - something that happens easily
325 * with overlapping dive downloads.
327 struct dive *try_to_merge(struct dive *a, struct dive *b)
332 if (a->when != b->when)
338 MERGE_TXT(res, a, b, location);
339 MERGE_TXT(res, a, b, notes);
340 MERGE_TXT(res, a, b, buddy);
341 MERGE_TXT(res, a, b, divemaster);
342 MERGE_MAX(res, a, b, number);
343 MERGE_MAX(res, a, b, maxdepth.mm);
344 res->meandepth.mm = 0;
345 MERGE_MAX(res, a, b, duration.seconds);
346 MERGE_MAX(res, a, b, surfacetime.seconds);
347 MERGE_MAX(res, a, b, airtemp.mkelvin);
348 MERGE_MIN(res, a, b, watertemp.mkelvin);
349 for (i = 0; i < MAX_CYLINDERS; i++)
350 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
352 return merge_samples(res, a, b, 0);