2 /* maintains the internal dive list structure */
8 void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name)
10 struct event *ev, **p;
11 unsigned int size, len = strlen(name);
13 size = sizeof(*ev) + len + 1;
18 memcpy(ev->name, name, len);
19 ev->time.seconds = time;
32 int get_pressure_units(unsigned int mb, const char **units)
37 switch (output_units.pressure) {
43 pressure = (mb + 500) / 1000;
47 pressure = mbar_to_PSI(mb);
56 double get_temp_units(unsigned int mk, const char **units)
61 if (output_units.temperature == FAHRENHEIT) {
62 deg = mkelvin_to_F(mk);
63 unit = UTF8_DEGREE "F";
65 deg = mkelvin_to_C(mk);
66 unit = UTF8_DEGREE "C";
73 double get_volume_units(unsigned int ml, int *frac, const char **units)
79 switch (output_units.volume) {
98 double get_depth_units(unsigned int mm, int *frac, const char **units)
104 switch (output_units.length) {
123 struct dive *alloc_dive(void)
125 const int initial_samples = 5;
129 size = dive_size(initial_samples);
133 memset(dive, 0, size);
134 dive->alloc_samples = initial_samples;
138 struct sample *prepare_sample(struct dive **divep)
140 struct dive *dive = *divep;
142 int nr = dive->samples;
143 int alloc_samples = dive->alloc_samples;
144 struct sample *sample;
145 if (nr >= alloc_samples) {
148 alloc_samples = (alloc_samples * 3)/2 + 10;
149 size = dive_size(alloc_samples);
150 dive = realloc(dive, size);
153 dive->alloc_samples = alloc_samples;
156 sample = dive->sample + nr;
157 memset(sample, 0, sizeof(*sample));
163 void finish_sample(struct dive *dive, struct sample *sample)
169 * So when we re-calculate maxdepth and meandepth, we will
170 * not override the old numbers if they are close to the
173 * Why? Because a dive computer may well actually track the
174 * max depth and mean depth at finer granularity than the
175 * samples it stores. So it's possible that the max and mean
176 * have been reported more correctly originally.
178 * Only if the values calculated from the samples are clearly
179 * different do we override the normal depth values.
181 * This considers 1m to be "clearly different". That's
182 * a totally random number.
184 static void update_depth(depth_t *depth, int new)
189 if (abs(old - new) > 1000)
194 static void update_duration(duration_t *duration, int new)
197 duration->seconds = new;
200 static void update_temperature(temperature_t *temperature, int new)
203 int old = temperature->mkelvin;
205 if (abs(old - new) > 1000)
206 temperature->mkelvin = new;
210 static void fixup_pressure(struct dive *dive, struct sample *sample)
212 unsigned int pressure, index;
215 pressure = sample->cylinderpressure.mbar;
218 index = sample->cylinderindex;
219 if (index >= MAX_CYLINDERS)
221 cyl = dive->cylinder + index;
222 if (!cyl->sample_start.mbar)
223 cyl->sample_start.mbar = pressure;
224 cyl->sample_end.mbar = pressure;
228 * If the cylinder tank pressures are within half a bar
229 * (about 8 PSI) of the sample pressures, we consider it
230 * to be a rounding error, and throw them away as redundant.
232 static int same_rounded_pressure(pressure_t a, pressure_t b)
234 return abs(a.mbar - b.mbar) <= 500;
237 struct dive *fixup_dive(struct dive *dive)
240 double depthtime = 0;
243 int start = -1, end = -1;
244 int maxdepth = 0, mintemp = 0;
246 int lasttemp = 0, lastpressure = 0;
247 int pressure_delta[MAX_CYLINDERS] = {INT_MAX, };
249 for (i = 0; i < dive->samples; i++) {
250 struct sample *sample = dive->sample + i;
251 int time = sample->time.seconds;
252 int depth = sample->depth.mm;
253 int temp = sample->temperature.mkelvin;
254 int pressure = sample->cylinderpressure.mbar;
255 int index = sample->cylinderindex;
257 if (index == lastindex) {
258 /* Remove duplicate redundant pressure information */
259 if (pressure == lastpressure)
260 sample->cylinderpressure.mbar = 0;
261 /* check for simply linear data in the samples
262 +INT_MAX means uninitialized, -INT_MAX means not linear */
263 if (pressure_delta[index] != -INT_MAX && lastpressure) {
264 if (pressure_delta[index] == INT_MAX) {
265 pressure_delta[index] = abs(pressure - lastpressure);
267 int cur_delta = abs(pressure - lastpressure);
268 if (cur_delta && abs(cur_delta - pressure_delta[index]) > 150) {
269 /* ok the samples aren't just a linearisation
270 * between start and end */
271 pressure_delta[index] = -INT_MAX;
277 lastpressure = pressure;
285 if (depth > maxdepth)
289 fixup_pressure(dive, sample);
293 * If we have consecutive identical
294 * temperature readings, throw away
295 * the redundant ones.
297 if (lasttemp == temp)
298 sample->temperature.mkelvin = 0;
302 if (!mintemp || temp < mintemp)
305 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
309 /* if all the samples for a cylinder have pressure data that
310 * is basically equidistant throw out the sample cylinder pressure
311 * information but make sure we still have a valid start and end
313 * this happens when DivingLog decides to linearalize the
314 * pressure between beginning and end and for strange reasons
315 * decides to put that in the sample data as if it came from
316 * the dive computer; we don't want that (we'll visualize with
317 * constant SAC rate instead)
318 * WARNING WARNING - I have only seen this in single tank dives
319 * --- maybe I should try to create a multi tank dive and see what
320 * --- divinglog does there - but the code right now is only tested
321 * --- for the single tank case */
322 for (j = 0; j < MAX_CYLINDERS; j++) {
323 if (abs(pressure_delta[j]) != INT_MAX) {
324 cylinder_t *cyl = dive->cylinder + j;
325 for (i = 0; i < dive->samples; i++)
326 if (dive->sample[i].cylinderindex == j)
327 dive->sample[i].cylinderpressure.mbar = 0;
328 if (! cyl->start.mbar)
329 cyl->start.mbar = cyl->sample_start.mbar;
331 cyl->end.mbar = cyl->sample_end.mbar;
332 cyl->sample_start.mbar = 0;
333 cyl->sample_end.mbar = 0;
339 update_duration(&dive->duration, end - start);
341 depthtime /= (end - start);
343 update_depth(&dive->meandepth, depthtime);
344 update_temperature(&dive->watertemp, mintemp);
345 update_depth(&dive->maxdepth, maxdepth);
347 add_people(dive->buddy);
348 add_people(dive->divemaster);
349 add_location(dive->location);
350 for (i = 0; i < MAX_CYLINDERS; i++) {
351 cylinder_t *cyl = dive->cylinder + i;
352 add_cylinder_description(&cyl->type);
353 if (same_rounded_pressure(cyl->sample_start, cyl->start))
355 if (same_rounded_pressure(cyl->sample_end, cyl->end))
362 /* Don't pick a zero for MERGE_MIN() */
363 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
364 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
365 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
366 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
368 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
370 struct sample *p = prepare_sample(&dive);
375 p->time.seconds = time;
376 finish_sample(dive, p);
381 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
383 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
385 int asamples = a->samples;
386 int bsamples = b->samples;
387 struct sample *as = a->sample;
388 struct sample *bs = b->sample;
392 struct sample sample;
397 at = asamples ? as->time.seconds : -1;
398 bt = bsamples ? bs->time.seconds + offset : -1;
400 /* No samples? All done! */
401 if (at < 0 && bt < 0)
402 return fixup_dive(res);
404 /* Only samples from a? */
407 res = add_sample(as, at, res);
413 /* Only samples from b? */
416 res = add_sample(bs, bt, res);
427 /* same-time sample: add a merged sample. Take the non-zero ones */
430 sample.depth = as->depth;
431 if (as->temperature.mkelvin)
432 sample.temperature = as->temperature;
433 if (as->cylinderpressure.mbar)
434 sample.cylinderpressure = as->cylinderpressure;
435 if (as->cylinderindex)
436 sample.cylinderindex = as->cylinderindex;
438 res = add_sample(&sample, at, res);
447 static char *merge_text(const char *a, const char *b)
457 res = malloc(strlen(a) + strlen(b) + 9);
460 sprintf(res, "(%s) or (%s)", a, b);
464 #define SORT(a,b,field) \
465 if (a->field != b->field) return a->field < b->field ? -1 : 1
467 static int sort_event(struct event *a, struct event *b)
469 SORT(a,b,time.seconds);
473 return strcmp(a->name, b->name);
476 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
479 struct event **p = &res->events;
484 b->time.seconds += offset;
499 s = sort_event(a, b);
507 /* Pick 'a' or neither */
517 /* Pick whichever has any info (if either). Prefer 'a' */
518 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
525 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
532 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
534 merge_cylinder_type(&res->type, &a->type, &b->type);
535 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
536 MERGE_MAX(res, a, b, start.mbar);
537 MERGE_MIN(res, a, b, end.mbar);
541 * This could do a lot more merging. Right now it really only
542 * merges almost exact duplicates - something that happens easily
543 * with overlapping dive downloads.
545 struct dive *try_to_merge(struct dive *a, struct dive *b)
550 if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
556 MERGE_NONZERO(res, a, b, latitude);
557 MERGE_NONZERO(res, a, b, longitude);
558 MERGE_TXT(res, a, b, location);
559 MERGE_TXT(res, a, b, notes);
560 MERGE_TXT(res, a, b, buddy);
561 MERGE_TXT(res, a, b, divemaster);
562 MERGE_MAX(res, a, b, number);
563 MERGE_MAX(res, a, b, maxdepth.mm);
564 res->meandepth.mm = 0;
565 MERGE_MAX(res, a, b, duration.seconds);
566 MERGE_MAX(res, a, b, surfacetime.seconds);
567 MERGE_MAX(res, a, b, airtemp.mkelvin);
568 MERGE_MIN(res, a, b, watertemp.mkelvin);
569 for (i = 0; i < MAX_CYLINDERS; i++)
570 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
571 merge_events(res, a, b, 0);
572 return merge_samples(res, a, b, 0);