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)
202 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
204 struct sample *p = prepare_sample(&dive);
209 p->time.seconds = time;
210 finish_sample(dive, p);
215 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
217 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
219 int asamples = a->samples;
220 int bsamples = b->samples;
221 struct sample *as = a->sample;
222 struct sample *bs = b->sample;
226 struct sample sample;
231 at = asamples ? as->time.seconds : -1;
232 bt = bsamples ? bs->time.seconds + offset : -1;
234 /* No samples? All done! */
235 if (at < 0 && bt < 0)
236 return fixup_dive(res);
238 /* Only samples from a? */
241 res = add_sample(as, at, res);
247 /* Only samples from b? */
250 res = add_sample(bs, bt, res);
261 /* same-time sample: add a merged sample. Take the non-zero ones */
264 sample.depth = as->depth;
265 if (as->temperature.mkelvin)
266 sample.temperature = as->temperature;
267 if (as->cylinderpressure.mbar)
268 sample.cylinderpressure = as->cylinderpressure;
269 if (as->cylinderindex)
270 sample.cylinderindex = as->cylinderindex;
272 res = add_sample(&sample, at, res);
281 static char *merge_text(const char *a, const char *b)
291 res = malloc(strlen(a) + strlen(b) + 9);
294 sprintf(res, "(%s) or (%s)", a, b);
298 /* Pick whichever has any info (if either). Prefer 'a' */
299 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
306 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
313 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
315 merge_cylinder_type(&res->type, &a->type, &b->type);
316 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
317 MERGE_MAX(res, a, b, start.mbar);
318 MERGE_MIN(res, a, b, end.mbar);
322 * This could do a lot more merging. Right now it really only
323 * merges almost exact duplicates - something that happens easily
324 * with overlapping dive downloads.
326 struct dive *try_to_merge(struct dive *a, struct dive *b)
331 if (a->when != b->when)
337 res->location = merge_text(a->location, b->location);
338 res->notes = merge_text(a->notes, b->notes);
339 MERGE_MAX(res, a, b, number);
340 MERGE_MAX(res, a, b, maxdepth.mm);
341 res->meandepth.mm = 0;
342 MERGE_MAX(res, a, b, duration.seconds);
343 MERGE_MAX(res, a, b, surfacetime.seconds);
344 MERGE_MAX(res, a, b, airtemp.mkelvin);
345 MERGE_MIN(res, a, b, watertemp.mkelvin);
346 for (i = 0; i < MAX_CYLINDERS; i++)
347 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
349 return merge_samples(res, a, b, 0);